Skip to content

Commit

Permalink
pint typing fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cgevans committed Jan 3, 2024
1 parent 19d1484 commit 497c891
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 152 deletions.
16 changes: 8 additions & 8 deletions src/alhambra_mixes/abbreviated.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
Mix = Mix
Exp = Experiment

µM = ureg("µM")
uM = ureg("uM")
nM = ureg("nM")
mM = ureg("mM")
nL = ureg("nL")
µL = ureg("µL")
uL = ureg("uL")
mL = ureg("mL")
µM = ureg.Unit("µM")
uM = ureg.Unit("uM")
nM = ureg.Unit("nM")
mM = ureg.Unit("mM")
nL = ureg.Unit("nL")
µL = ureg.Unit("µL")
uL = ureg.Unit("uL")
mL = ureg.Unit("mL")
98 changes: 49 additions & 49 deletions src/alhambra_mixes/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def name(self) -> str: # pragma: no cover

def tx_volume(
self,
mix_vol: Quantity[Decimal] = Q_(DNAN, uL),
mix_vol: DecimalQuantity = Q_(DNAN, uL),
actions: Sequence[AbstractAction] = tuple(),
) -> Quantity[Decimal]: # pragma: no cover
) -> DecimalQuantity: # pragma: no cover
"""The total volume transferred by the action to the sample. May depend on the total mix volume.
Parameters
Expand All @@ -61,14 +61,14 @@ def tx_volume(
def _mixlines(
self,
tablefmt: str | TableFormat,
mix_vol: Quantity[Decimal],
mix_vol: DecimalQuantity,
actions: Sequence[AbstractAction] = tuple(),
) -> Sequence[MixLine]: # pragma: no cover
...

@abstractmethod
def all_components(
self, mix_vol: Quantity[Decimal], actions: Sequence[AbstractAction] = tuple()
self, mix_vol: DecimalQuantity, actions: Sequence[AbstractAction] = tuple()
) -> pd.DataFrame: # pragma: no cover
"""A dataframe containing all base components added by the action.
Expand All @@ -95,8 +95,8 @@ def with_reference(
...

def dest_concentration(
self, mix_vol: Quantity[Decimal], actions: Sequence[AbstractAction] = tuple()
) -> Quantity[Decimal]:
self, mix_vol: DecimalQuantity, actions: Sequence[AbstractAction] = tuple()
) -> DecimalQuantity:
"""The destination concentration added to the mix by the action.
Raises
Expand All @@ -109,8 +109,8 @@ def dest_concentration(
raise ValueError("Single destination concentration not defined.")

def dest_concentrations(
self, mix_vol: Quantity[Decimal], actions: Sequence[AbstractAction] = tuple()
) -> Sequence[Quantity[Decimal]]:
self, mix_vol: DecimalQuantity, actions: Sequence[AbstractAction] = tuple()
) -> Sequence[DecimalQuantity]:
raise ValueError

@property
Expand All @@ -121,9 +121,9 @@ def components(self) -> list[AbstractComponent]:
@abstractmethod
def each_volumes(
self,
total_volume: Quantity[Decimal],
total_volume: DecimalQuantity,
actions: Sequence[AbstractAction] = tuple(),
) -> list[Quantity[Decimal]]:
) -> list[DecimalQuantity]:
...

@classmethod
Expand Down Expand Up @@ -200,7 +200,7 @@ def with_reference(
)

@property
def source_concentrations(self) -> list[Quantity[Decimal]]:
def source_concentrations(self) -> list[DecimalQuantity]:
concs = [c.concentration for c in self.components]
return concs

Expand Down Expand Up @@ -242,7 +242,7 @@ def _structure(
return cls(**d)

def all_components(
self, mix_vol: Quantity[Decimal], actions: Sequence[AbstractAction] = tuple()
self, mix_vol: DecimalQuantity, actions: Sequence[AbstractAction] = tuple()
) -> pd.DataFrame:
newdf = _empty_components()

Expand All @@ -267,8 +267,8 @@ def all_components(
def _compactstrs(
self,
tablefmt: str | TableFormat,
dconcs: Sequence[Quantity[Decimal]],
eavols: Sequence[Quantity[Decimal]],
dconcs: Sequence[DecimalQuantity],
eavols: Sequence[DecimalQuantity],
) -> Sequence[MixLine]:
# locs = [(c.name,) + c.location for c in self.components]
# names = [c.name for c in self.components]
Expand Down Expand Up @@ -296,18 +296,18 @@ def _compactstrs(
)

names: list[list[str]] = []
source_concs: list[Quantity[Decimal]] = []
dest_concs: list[Quantity[Decimal]] = []
source_concs: list[DecimalQuantity] = []
dest_concs: list[DecimalQuantity] = []
numbers: list[int] = []
ea_vols: list[Quantity[Decimal]] = []
tot_vols: list[Quantity[Decimal]] = []
ea_vols: list[DecimalQuantity] = []
tot_vols: list[DecimalQuantity] = []
plates: list[str] = []
wells_list: list[list[WellPos]] = []

for plate, plate_comps in locdf.groupby("plate"): # type: str, pd.DataFrame
for vol, plate_vol_comps in plate_comps.groupby(
"ea_vols"
): # type: Quantity[Decimal], pd.DataFrame
): # type: DecimalQuantity, pd.DataFrame
if pd.isna(plate_vol_comps["well"].iloc[0]):
if not pd.isna(plate_vol_comps["well"]).all():
raise ValueError
Expand Down Expand Up @@ -410,7 +410,7 @@ class FixedVolume(ActionWithComponents):
| c1, c2, c3 | 200.00 nM | 66.67 nM | 3 | 5.00 µl | 15.00 µl | | |
"""

fixed_volume: Quantity[Decimal] = attrs.field(
fixed_volume: DecimalQuantity = attrs.field(
converter=_parse_vol_required, on_setattr=attrs.setters.convert
)
set_name: str | None = None
Expand All @@ -431,9 +431,9 @@ def __new__(cls, *args, **kwargs):

def dest_concentrations(
self,
mix_vol: Quantity[Decimal] = Q_(DNAN, uL),
mix_vol: DecimalQuantity = Q_(DNAN, uL),
actions: Sequence[AbstractAction] = tuple(),
) -> list[Quantity[Decimal]]:
) -> list[DecimalQuantity]:
return [
x * y
for x, y in zip(
Expand All @@ -443,10 +443,10 @@ def dest_concentrations(

def each_volumes(
self,
mix_vol: Quantity[Decimal] = Q_(DNAN, uL),
mix_vol: DecimalQuantity = Q_(DNAN, uL),
actions: Sequence[AbstractAction] = tuple(),
) -> list[Quantity[Decimal]]:
return [self.fixed_volume.to(uL)] * len(self.components)
) -> list[DecimalQuantity]:
return [cast(DecimalQuantity, self.fixed_volume.to(uL))] * len(self.components)

@property
def name(self) -> str:
Expand All @@ -458,7 +458,7 @@ def name(self) -> str:
def _mixlines(
self,
tablefmt: str | TableFormat,
mix_vol: Quantity[Decimal],
mix_vol: DecimalQuantity,
actions: Sequence[AbstractAction] = tuple(),
) -> list[MixLine]:
dconcs = self.dest_concentrations(mix_vol)
Expand Down Expand Up @@ -551,7 +551,7 @@ class EqualConcentration(FixedVolume):
def __init__(
self,
components: Sequence[AbstractComponent | str] | AbstractComponent | str,
fixed_volume: str | Quantity[Decimal],
fixed_volume: str | DecimalQuantity,
set_name: str | None = None,
compact_display: bool = True,
method: Literal["max_volume", "min_volume", "check"]
Expand All @@ -577,17 +577,17 @@ def __init__(
] = "min_volume"

@property
def source_concentrations(self) -> List[Quantity[Decimal]]:
def source_concentrations(self) -> List[DecimalQuantity]:
concs = super().source_concentrations
if any(x != concs[0] for x in concs) and (self.method == "check"):
raise ValueError("Not all components have equal concentration.")
return concs

def each_volumes(
self,
mix_vol: Quantity[Decimal] = Q_(DNAN, uL),
mix_vol: DecimalQuantity = Q_(DNAN, uL),
actions: Sequence[AbstractAction] = tuple(),
) -> list[Quantity[Decimal]]:
) -> list[DecimalQuantity]:
# match self.equal_conc:
if self.method == "min_volume":
sc = self.source_concentrations
Expand All @@ -603,22 +603,22 @@ def each_volumes(
sc = self.source_concentrations
if any(x != sc[0] for x in sc):
raise ValueError("Concentrations")
return [self.fixed_volume.to(uL)] * len(self.components)
return [cast(DecimalQuantity, self.fixed_volume.to(uL))] * len(self.components)
raise ValueError(f"equal_conc={repr(self.method)} not understood")

def tx_volume(
self,
mix_vol: Quantity[Decimal] = Q_(DNAN, uL),
mix_vol: DecimalQuantity = Q_(DNAN, uL),
actions: Sequence[AbstractAction] = tuple(),
) -> Quantity[Decimal]:
) -> DecimalQuantity:
if isinstance(self.method, Sequence) and (self.method[0] == "max_fill"):
return self.fixed_volume * len(self.components)
return sum(self.each_volumes(mix_vol), ureg("0.0 uL"))

def _mixlines(
self,
tablefmt: str | TableFormat,
mix_vol: Quantity[Decimal],
mix_vol: DecimalQuantity,
actions: Sequence[AbstractAction] = tuple(),
) -> list[MixLine]:
ml = super()._mixlines(tablefmt, mix_vol)
Expand Down Expand Up @@ -683,29 +683,29 @@ class FixedConcentration(ActionWithComponents):
| *Total:* | | 40.00 nM | | | 25.00 µl | | |
"""

fixed_concentration: Quantity[Decimal] = attrs.field(
fixed_concentration: DecimalQuantity = attrs.field(
converter=_parse_conc_required, on_setattr=attrs.setters.convert
)
set_name: str | None = None
compact_display: bool = True
min_volume: Quantity[Decimal] = attrs.field(
min_volume: DecimalQuantity = attrs.field(
converter=_parse_vol_optional_none_zero,
default=ZERO_VOL,
on_setattr=attrs.setters.convert,
)

def dest_concentrations(
self,
mix_vol: Quantity[Decimal] = Q_(DNAN, uL),
mix_vol: DecimalQuantity = Q_(DNAN, uL),
actions: Sequence[AbstractAction] = tuple(),
) -> list[Quantity[Decimal]]:
) -> list[DecimalQuantity]:
return [self.fixed_concentration] * len(self.components)

def each_volumes(
self,
mix_vol: Quantity[Decimal] = Q_(DNAN, uL),
mix_vol: DecimalQuantity = Q_(DNAN, uL),
actions: Sequence[AbstractAction] = tuple(),
) -> list[Quantity[Decimal]]:
) -> list[DecimalQuantity]:
ea_vols = [
mix_vol * r
for r in _ratio(self.fixed_concentration, self.source_concentrations)
Expand All @@ -727,7 +727,7 @@ def each_volumes(
def _mixlines(
self,
tablefmt: str | TableFormat,
mix_vol: Quantity[Decimal],
mix_vol: DecimalQuantity,
actions: Sequence[AbstractAction] = tuple(),
) -> list[MixLine]:
dconcs = self.dest_concentrations(mix_vol)
Expand Down Expand Up @@ -772,18 +772,18 @@ class ToConcentration(ActionWithComponents):
takes into account other contents of the mix, and only adds enough to reach a particular final
concentration."""

fixed_concentration: Quantity[Decimal] = attrs.field(
fixed_concentration: DecimalQuantity = attrs.field(
converter=_parse_conc_required, on_setattr=attrs.setters.convert
)
compact_display: bool = True
min_volume: Quantity[Decimal] = attrs.field(
min_volume: DecimalQuantity = attrs.field(
converter=_parse_vol_optional,
default=Q_(DNAN, uL),
on_setattr=attrs.setters.convert,
)

def _othercomps(
self, mix_vol: Quantity[Decimal], actions: Sequence[AbstractAction] = tuple()
self, mix_vol: DecimalQuantity, actions: Sequence[AbstractAction] = tuple()
):
cps = _empty_components()

Expand Down Expand Up @@ -820,10 +820,10 @@ def _othercomps(

def dest_concentrations(
self,
mix_vol: Quantity[Decimal],
mix_vol: DecimalQuantity,
actions: Sequence[AbstractAction] = tuple(),
_othercomps: pd.DataFrame | None = None,
) -> Sequence[Quantity[Decimal]]:
) -> Sequence[DecimalQuantity]:
if _othercomps is None and actions:
_othercomps = self._othercomps(mix_vol, actions)
if _othercomps is not None:
Expand All @@ -839,10 +839,10 @@ def dest_concentrations(

def each_volumes(
self,
mix_vol: Quantity[Decimal] = Q_(DNAN, uL),
mix_vol: DecimalQuantity = Q_(DNAN, uL),
actions: Sequence[AbstractAction] = tuple(),
_othercomps: pd.DataFrame | None = None,
) -> list[Quantity[Decimal]]:
) -> list[DecimalQuantity]:
ea_vols = [
mix_vol * r
for r in _ratio(
Expand All @@ -867,7 +867,7 @@ def each_volumes(
def _mixlines(
self,
tablefmt: str | TableFormat,
mix_vol: Quantity[Decimal],
mix_vol: DecimalQuantity,
actions: Sequence[AbstractAction] = tuple(),
) -> list[MixLine]:
othercomps = self._othercomps(mix_vol, actions)
Expand Down
6 changes: 3 additions & 3 deletions src/alhambra_mixes/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ def printed_name(self, tablefmt: str | TableFormat) -> str:

def _update_volumes(
self,
consumed_volumes: Dict[str, Quantity[Decimal]] = {},
made_volumes: Dict[str, Quantity[Decimal]] = {},
) -> Tuple[Dict[str, Quantity[Decimal]], Dict[str, Quantity[Decimal]]]:
consumed_volumes: Dict[str, DecimalQuantity] = {},
made_volumes: Dict[str, DecimalQuantity] = {},
) -> Tuple[Dict[str, DecimalQuantity], Dict[str, DecimalQuantity]]:
"""
Given a
"""
Expand Down
14 changes: 7 additions & 7 deletions src/alhambra_mixes/experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import attrs

from .dictstructure import _structure, _unstructure
from .units import DNAN, Q_, ZERO_VOL, Decimal, uL, Quantity
from .units import DNAN, Q_, ZERO_VOL, Decimal, uL, Quantity, DecimalQuantity
from .mixes import Mix
from .mixes import VolumeError

Expand Down Expand Up @@ -102,10 +102,10 @@ def add_mix(
name: str = "",
test_tube_name: str | None = None,
*,
fixed_total_volume: Quantity[Decimal] | str = Q_(DNAN, uL),
fixed_concentration: str | Quantity[Decimal] | None = None,
fixed_total_volume: DecimalQuantity | str = Q_(DNAN, uL),
fixed_concentration: str | DecimalQuantity | None = None,
buffer_name: str = "Buffer",
min_volume: Quantity[Decimal] | str = Q_("0.5", uL),
min_volume: DecimalQuantity | str = Q_("0.5", uL),
check_volumes: bool | None = None,
apply_reference: bool = True,
check_existing: bool | Literal["equal"] = "equal",
Expand Down Expand Up @@ -199,9 +199,9 @@ def __iter__(self) -> Iterator[AbstractComponent]:

def consumed_and_produced_volumes(
self,
) -> Mapping[str, Tuple[Quantity[Decimal], Quantity[Decimal]]]:
consumed_volume: Dict[str, Quantity[Decimal]] = {}
produced_volume: Dict[str, Quantity[Decimal]] = {}
) -> Mapping[str, Tuple[DecimalQuantity, DecimalQuantity]]:
consumed_volume: Dict[str, DecimalQuantity] = {}
produced_volume: Dict[str, DecimalQuantity] = {}
for component in self.components.values():
component._update_volumes(consumed_volume, produced_volume)
return {
Expand Down
Loading

0 comments on commit 497c891

Please sign in to comment.