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

MRC parsing bug in cryodrgn_utils invert_contrast and flip_hand #358

Closed
zhonge opened this issue Mar 24, 2024 · 2 comments
Closed

MRC parsing bug in cryodrgn_utils invert_contrast and flip_hand #358

zhonge opened this issue Mar 24, 2024 · 2 comments

Comments

@zhonge
Copy link
Collaborator

zhonge commented Mar 24, 2024

$ cryodrgn_utils flip_hand vol_000.mrc -o tmp.mrc
Traceback (most recent call last):
  File "/Users/zhonge/anaconda3/envs/cryodrgn/bin/cryodrgn_utils", line 8, in <module>
    sys.exit(util_commands())
  File "/Users/zhonge/dev/cryodrgn/cryodrgn/command_line.py", line 53, in util_commands
    _get_commands(os.path.join(os.path.dirname(__file__), "commands_utils"))
  File "/Users/zhonge/dev/cryodrgn/cryodrgn/command_line.py", line 43, in _get_commands
    args.func(args)
  File "/Users/zhonge/dev/cryodrgn/cryodrgn/commands_utils/flip_hand.py", line 24, in main
    MRCFile.write(
  File "/Users/zhonge/dev/cryodrgn/cryodrgn/mrc.py", line 260, in write
    header = MRCHeader.make_default_header(
  File "/Users/zhonge/dev/cryodrgn/cryodrgn/mrc.py", line 142, in make_default_header
    dmin, dmax, dmean, rms = data.min(), data.max(), data.mean(), data.std()
AttributeError: 'MRCFileSource' object has no attribute 'min'
$ cryodrgn_utils invert_contrast vol_000.mrc -o tmp.mrc
Traceback (most recent call last):
  File "/Users/zhonge/anaconda3/envs/cryodrgn/bin/cryodrgn_utils", line 8, in <module>
    sys.exit(util_commands())
  File "/Users/zhonge/dev/cryodrgn/cryodrgn/command_line.py", line 53, in util_commands
    _get_commands(os.path.join(os.path.dirname(__file__), "commands_utils"))
  File "/Users/zhonge/dev/cryodrgn/cryodrgn/command_line.py", line 43, in _get_commands
    args.func(args)
  File "/Users/zhonge/dev/cryodrgn/cryodrgn/commands_utils/invert_contrast.py", line 22, in main
    MRCFile.write(args.o, src, transform_fn=lambda data, indices: -data)
  File "/Users/zhonge/dev/cryodrgn/cryodrgn/mrc.py", line 260, in write
    header = MRCHeader.make_default_header(
  File "/Users/zhonge/dev/cryodrgn/cryodrgn/mrc.py", line 142, in make_default_header
    dmin, dmax, dmean, rms = data.min(), data.max(), data.mean(), data.std()
AttributeError: 'MRCFileSource' object has no attribute 'min'
@michal-g
Copy link
Collaborator

I was able to reproduce these errors with an MRCFile volume generated by abinit_homo, and both commands worked fine with the ImageSource volumes currently used for testing. This suggests we need to do some deeper cleaning of the MRCFile and ImageSource classes and the code where they are used, but for now I have fixed the behavior in mrc.make_default_header that was causing this behaviour:

        if is_vol:
            if data is None:
                raise ValueError("If is_vol=True, data array must be specified")

            if isinstance(data, (np.ndarray, torch.Tensor)):
                dmin, dmax, dmean, rms = data.min(), data.max(), data.mean(), data.std()
            elif isinstance(data, ImageSource):
                imgdata = data.images()
                dmin = imgdata.min().item()
                dmax = imgdata.max().item()
                dmean = imgdata.mean().item()
                rms = imgdata.std().item()
            else:
                raise TypeError(f"Unrecognized type of data: `{type(data).__name__}`!")

        else:  # use undefined values for image stacks
            dmin, dmax, dmean, rms = -1, -2, -3, -1

I've also added more cases to test_flip_hand and test_invert_contrast that specifically cover this example!

@michal-g
Copy link
Collaborator

michal-g commented Apr 1, 2024

Aforementioned fixes were included in the v3.2.0 release!

@michal-g michal-g closed this as completed Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants