Skip to content

Commit

Permalink
update to work with new binarymap (scipy sparse arrays rather than …
Browse files Browse the repository at this point in the history
…matrices) (#185)

* use `binarymap` v0.7 (`scipy.sparse` arrays rather than matrices)

* remove pin on `urllib3`
  • Loading branch information
jbloom authored Sep 21, 2024
1 parent 5377215 commit a29f3c9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ All notable changes to this project will be documented in this file.

The format is based on `Keep a Changelog <https://keepachangelog.com>`_.

6.12
----
- Switch to using ``scipy.sparse`` arrays rather than matrices to keep up with `this <https://github.com/jbloomlab/binarymap/issues/6>`_ `change <https://github.com/jbloomlab/binarymap/pull/7>`_ to ``binarymap`` (now require ``binarymap`` >= 0.7).
- Remove pin on ``urllib3``

6.11
----
- In ``lineplot_and_heatmap``, add ``mean_abs`` and ``sum_abs`` as possible site statistics, reflecting the mean and sum of the absolute values of the mutation values.
Expand Down
4 changes: 2 additions & 2 deletions polyclonal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
"""

__author__ = "`the Bloom lab <https://research.fhcrc.org/bloom/en.html>`_"
__author__ = "`the Bloom lab <https://jbloomlab.org>`_"
__email__ = "[email protected]"
__version__ = "6.11"
__version__ = "6.12"
__url__ = "https://github.com/jbloomlab/polyclonal"

from polyclonal.alphabets import AAS
Expand Down
16 changes: 8 additions & 8 deletions polyclonal/polyclonal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1671,7 +1671,7 @@ def _loss_dloss(self, params, delta):
)
assert pred_pvs.shape == self._pvs.shape
assert dpred_pvs_dparams.shape == (len(params), len(self._pvs))
assert type(dpred_pvs_dparams) is scipy.sparse.csr_matrix
assert type(dpred_pvs_dparams) is scipy.sparse.csr_array
residuals = pred_pvs - self._pvs
loss, dloss_dr = self._scaled_pseudo_huber(delta, residuals, True)
assert loss.shape == dloss_dr.shape == self._pvs.shape
Expand Down Expand Up @@ -2972,7 +2972,7 @@ def _compute_1d_pvs(self, params, one_binarymap, binarymaps, cs, calc_grad=False
Differs from :meth:`Polyclonal._compute_pv` in that it works if just
one or multiple BinaryMap objects.
If `calc_grad` is `True`, also returns `scipy.sparse.csr_matrix`
If `calc_grad` is `True`, also returns `scipy.sparse.csr_array`
of gradient as described in :meth:`Polyclonal._compute_pv`.
"""
Expand Down Expand Up @@ -3029,7 +3029,7 @@ def _compute_pv(self, params, bmap, cs, calc_grad=False):
``p_vc`` is 1D array ordered by concentration and then variant
variant. So elements are `ivariant + iconcentration * nvariants`,
and length is nconcentrations * nvariants. If ``calc_grad=True``,
then ``dpvc_dparams`` is `scipy.sparse.csr_matrix` of shape
then ``dpvc_dparams`` is `scipy.sparse.csr_array` of shape
(len(params), nconcentrations * nvariants). Note that
len(params) is nepitopes * (1 + binarylength).
Expand Down Expand Up @@ -3103,18 +3103,18 @@ def _compute_pv(self, params, bmap, cs, calc_grad=False):
dpvc_dbetaparams = dpevc_dbeta.reshape(
bmap.binarylength * len(self.epitopes), n_vc
)
assert type(dpvc_dbetaparams) is scipy.sparse.coo_matrix
assert type(dpvc_dbetaparams) is scipy.sparse.coo_array
# combine to make dpvc_dparams, noting activities before betas
# in params
dpvc_dparams = scipy.sparse.vstack(
[
scipy.sparse.csr_matrix(dpvc_da),
scipy.sparse.csr_matrix(dpvc_dn),
scipy.sparse.csr_matrix(dpvc_dt),
scipy.sparse.csr_array(dpvc_da),
scipy.sparse.csr_array(dpvc_dn),
scipy.sparse.csr_array(dpvc_dt),
dpvc_dbetaparams.tocsr(),
]
)
assert type(dpvc_dparams) is scipy.sparse.csr_matrix
assert type(dpvc_dparams) is scipy.sparse.csr_array
assert dpvc_dparams.shape == (len(params), n_vc)
return p_vc, dpvc_dparams

Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
license="GPLv3",
install_requires=[
"altair>=5.0.0",
"binarymap>=0.6",
"binarymap>=0.7",
"biopython>=1.79",
"frozendict>=2.0.7",
"matplotlib>=3.1",
Expand All @@ -54,7 +54,6 @@
"pyarrow",
"requests",
"scipy>=1.7.1",
"urllib3==1.26.15", # https://github.com/googleapis/python-bigquery/issues/1565
],
platforms="Linux and Mac OS X.",
packages=["polyclonal"],
Expand Down
2 changes: 1 addition & 1 deletion tests/test_gradients.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@
" calc_grad=False,\n",
" )\n",
" numerical_grad = (p_vc_eps - p_vc) / eps\n",
" analytical_grad = dpvc_dparams[iparam].toarray()\n",
" analytical_grad = dpvc_dparams[[iparam], :].toarray()\n",
" diff = numpy.sqrt(((numerical_grad - analytical_grad) ** 2).sum())\n",
" mag = numpy.sqrt((analytical_grad**2).sum())\n",
" if (diff > 1e-7) or (diff / mag > 1e-3):\n",
Expand Down

0 comments on commit a29f3c9

Please sign in to comment.