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

'MixingEnum' object has no attribute 'json' #260

Open
deepanshs opened this issue May 29, 2023 · 2 comments
Open

'MixingEnum' object has no attribute 'json' #260

deepanshs opened this issue May 29, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@deepanshs
Copy link
Owner

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 deepanshs added the bug Something isn't working label May 29, 2023
@mgiammar
Copy link
Collaborator

mgiammar commented Jun 1, 2023

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
Copy link
Collaborator

mgiammar commented Jun 13, 2023

Fixed by #270

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants