mrsimulator icon indicating copy to clipboard operation
mrsimulator copied to clipboard

'MixingEnum' object has no attribute 'json'

Open deepanshs opened this issue 1 year ago • 2 comments

Mixing Enum has no attribute json

To reproduce

mth = Method(
    channels=["1H"],
    magnetic_flux_density=9.4,
    spectral_dimensions=[SpectralDimension(
        count=1024,
        spectral_width=2e6,
        events=[
            SpectralEvent(transition_queries=[{"ch1": {"P": [-1]}}]),
            MixingEvent(query=MixingEnum.NoMixing),
            SpectralEvent(transition_queries=[{"ch1": {"P": [-1]}}])
        ]
    )]
)
mth.json()

deepanshs avatar May 29 '23 18:05 deepanshs

The simple solution to this bug is to add the following method to the MixingEnum class:

def json(self, **kwargs):
    """Return a JSON-compliant serialization of the enumeration"""
    return self.value if isinstance(self.value, str) else self.value.json(**kwargs)

However, upon writing test, I noticed that a MixingQuery with all zero angles and phases gives an empty dict as it's JSON representation. This is because both angle and phase default to zero for the RotationQuery class, but do we want to seralize a NoMixing to an empty dict? Take the following example:

MixingQuery(
    ch1={"angle": 0, "phase": 0},
    ch2={"angle": 0, "phase": 0},
    ch3={"angle": 0, "phase": 0},
).json()
{}

MixingEnum.NoMixing.json()  # with above code implemented
{}

It would be clearer if for MixingEnum.NoMixing the full dictionary was seralized, that is

MixingEnum.NoMixing.json()  # with above code implemented
{
    "ch1": {"angle": 0, "phase": 0},
    "ch2": {"angle": 0, "phase": 0},
    "ch3": {"angle": 0, "phase": 0},
}

mgiammar avatar Jun 01 '23 17:06 mgiammar

Fixed by #270

mgiammar avatar Jun 13 '23 17:06 mgiammar