Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable plotReplicates to handle many replicates #39

Merged
merged 1 commit into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ All notable changes to this project will be documented in this file.

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

0.8.0
-----

Added
+++++
- Added ``attempt_shared_legend`` parameter to ``CurveFits.plotReplicates`` to enable plotting many replicates that differ among sera/viruses.

0.7.0
-----

Expand Down
2 changes: 1 addition & 1 deletion neutcurve/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

__author__ = "Jesse Bloom"
__email__ = "jbloom@fredhutch.org"
__version__ = "0.7.0"
__version__ = "0.8.0"
__url__ = "https://github.com/jbloomlab/neutcurve"

from neutcurve.curvefits import CurveFits # noqa: F401
Expand Down
41 changes: 34 additions & 7 deletions neutcurve/curvefits.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ def plotReplicates(
subplot_titles="{serum} vs {virus}",
show_average=False,
average_only=False,
attempt_shared_legend=True,
**kwargs,
):
"""Plot grid with replicates for each serum / virus on same plot.
Expand All @@ -841,6 +842,9 @@ def plotReplicates(
`average_only` (bool)
Show **only** the replicate-average on each plot. No
legend in this case.
`attempt_shared_legend` (bool)
Do we attempt to share the same replicate key for all panels or
give each its own?
`**kwargs`
Other keyword arguments that can be passed to
:meth:`CurveFits.plotGrid`.
Expand All @@ -864,7 +868,8 @@ def plotReplicates(
nplottable = max(len(colors), len(markers))
if average_only:
replicates = ["average"]
else:
nreplicates = 1
elif attempt_shared_legend:
replicates = collections.OrderedDict()
if show_average:
replicates["average"] = True
Expand All @@ -874,11 +879,24 @@ def plotReplicates(
if replicate != "average":
replicates[replicate] = True
replicates = list(collections.OrderedDict(replicates).keys())
if len(replicates) > nplottable:
nreplicates = len(replicates)
else:
replicates_by_serum_virus = collections.defaultdict(list)
for serum, virus in itertools.product(sera, viruses):
key = (serum, virus)
if virus in self.viruses[serum]:
assert len(self.replicates[key])
if show_average:
replicates_by_serum_virus[key].append("average")
for replicate in self.replicates[(serum, virus)]:
if replicate != "average":
replicates_by_serum_virus[key].append(replicate)
nreplicates = max(len(reps) for reps in replicates_by_serum_virus.values())
if nreplicates > nplottable:
raise ValueError(
"Too many unique replicates. There are"
f"{len(replicates)} ({', '.join(replicates)}) "
f"but only {nplottable} `colors` or `markers`."
f"Too many unique replicates. There are {nreplicates} on a single plot "
f"but only {nplottable} `colors` or `markers`. Either specify more "
"colors/markers, or try setting `attempt_shared_legend=False`"
)

# build list of plots appropriate for `plotGrid`
Expand All @@ -887,7 +905,12 @@ def plotReplicates(
if virus in self.viruses[serum]:
title = subplot_titles.format(serum=serum, virus=virus)
curvelist = []
for i, replicate in enumerate(replicates):
if attempt_shared_legend:
rep_list = replicates
else:
rep_list = replicates_by_serum_virus[(serum, virus)]
assert len(rep_list)
for i, replicate in enumerate(rep_list):
if replicate in self.replicates[(serum, virus)]:
curvelist.append(
{
Expand Down Expand Up @@ -917,7 +940,11 @@ def plotReplicates(
for iplot, plot in enumerate(plotlist):
plots[(iplot // ncol, iplot % ncol)] = plot

return self.plotGrid(plots, **kwargs)
return self.plotGrid(
plots,
attempt_shared_legend=attempt_shared_legend,
**kwargs,
)

def _sera_viruses_lists(self, sera, viruses):
"""Check and build lists of `sera` and their `viruses`.
Expand Down
Loading