cgevans / mixes

BSD 3-Clause "New" or "Revised" License
0 stars 1 forks source link

fix KeyError on deserializing `Experiment` with `_SplitMix` #35

Closed dave-doty closed 1 year ago

dave-doty commented 1 year ago

Create a mix using the function split_mix() from the mixes module, add that mix using Experiment.add_mix(), then save the experiment to a JSON file. Loading the JSON file using Experiment.load() results in this error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Input In [4], in <cell line: 6>()
      3 reference = load_reference('../alhambra_reference_30-monomers.csv')
      4 # reference.df
----> 6 old_experiment = Experiment.load('../sds14-30-monomers-qPCR/sds14.json')
      8 experiment = Experiment()
      9 monomer_np_mix_1uM = old_experiment['monomers np 1uM']

File /mnt/c/Dropbox/git/mixes/src/alhambra_mixes/experiments.py:243, in Experiment.load(cls, filename_or_stream)
    240     s = filename_or_stream
    241     close = False
--> 243 exp = cls._structure(json.load(s))
    244 if close:
    245     s.close()

File /mnt/c/Dropbox/git/mixes/src/alhambra_mixes/experiments.py:225, in Experiment._structure(cls, d)
    223 del d["class"]
    224 for k, v in d["components"].items():
--> 225     d["components"][k] = _structure(v)
    226 return cls(**d)

File /mnt/c/Dropbox/git/mixes/src/alhambra_mixes/dictstructure.py:24, in _structure(x, experiment)
     22 def _structure(x: dict[str, Any], experiment: "Experiment" | None = None) -> Any:
     23     if isinstance(x, dict) and ("class" in x):
---> 24         c = _STRUCTURE_CLASSES[x["class"]]
     25         del x["class"]
     26         if hasattr(c, "_structure"):

KeyError: '_SplitMix'

When I get more time I can come back and write a minimal reproducible example, but I thought maybe Constantine would see how to fix this right away.

It would also be good to write a unit test that captures this bug.

dave-doty commented 1 year ago

This was an easy fix so I just committed it directly to main.