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

Reverse x and y in cryosparc micrograph_shape array. Convert modf to round. #21

Closed
wants to merge 1 commit into from

Conversation

turalaksel
Copy link

In cryosparcv2.4.0, x and y in micrograph_shape are reversed. To get the right COORDX and COORDY reversed array should be used. I also replaced modf with round to obtain the correct integer pixel coordinates.

….0, x and y in micrograph_shape are reversed
@asarnow
Copy link
Owner

asarnow commented Nov 8, 2018

Re: micrograph_shape, that makes sense as standard array shape is (nrow, ncol). Reversing a 1D array (in this case, actually a tuple) is arr[::-1]. I'll put this in after I verify it's correct, thanks for the report.

modf is used to maintain subpixel localization accuracy by transferring the non-integer part to the origins. The micrograph coordinates were already integers (it doesn't matter if they're also integer type or not, and FYI round does not convert floats to ints either). This information is probably not very useful (it's also likely to be overwritten when the alignment parameters are copied later), but there's no reason not to keep it. Anyway, the correct thing to do would be floor (rounds down like integer division / modf) if we were going to discard non-integer part.

@asarnow asarnow closed this Nov 8, 2018
@turalaksel
Copy link
Author

cs['location/micrograph_shape'] is a 2D array. At first, I thought it is a 1D array as well and I tried arr[::-1]. But that doesn't work. So you need to reverse the columns which are x and y in this 2D array.

round brings the value to the closest integer value and type is still floats. floor won't always give you the closest integer value. For instance, if you do floor(12.9999) it will give you 12, not 13. But for this operation, you would want 13, not 12. In this example, round(12.9999) will give you 13.

@asarnow
Copy link
Owner

asarnow commented Nov 8, 2018

The indexers are the same for 1 and N-D arrays, i.e. in this case it's arr2d[:, ::-1]. This almost fixes the coordinates, but not quite. (I updated this in pyem/master just now).

Cryosparc is using normalized coordinates, and Relion indexes from 0. Thus, to have the correct range of pixel coordinates one should use floor instead of round. In other words, if you use round, then you can get a coordinate of (Nx, Ny) which would be an array bounds error.

But maybe it should actually be round, but using micrograph_shape - 1. I'm not completely sure, in my test now the coordinates are still slightly off (more than 1 pixel) from how they are displayed in cryoSPARC.

@asarnow
Copy link
Owner

asarnow commented Nov 8, 2018

BTW I am testing by using csparc2star.py to convert a manual pick extraction, then using star.py --to-micrographs to create per-micrograph star files (with the coordinates) in the same directory as the micrographs and finally relion_display --coords <micrograph particle star> --i <micrograph.mrc> --angpix 1.2156 --lowpass 20 --particle_radius 100 --pick --scale 0.2 to display the converted coordinates.

Thanks again for pointing this out and helping test!

@turalaksel
Copy link
Author

Thanks, Daniel for sharing pyem and fixing the issue. pyem has been very useful.

@asarnow
Copy link
Owner

asarnow commented Nov 8, 2018

There's still a slight discrepancy as shown in this screen shot:

image

I'm not sure what it's caused by because it seems to be more than a 1-pixel error.

@turalaksel
Copy link
Author

turalaksel commented Nov 8, 2018

I think the original commit I posted works fine. So, actually round gives the right answer.

In case you may want to try, the way I tested is

  1. Import particle star file linked to a previously imported micrograph mrc files in cryosparc.
  2. Download the imported particles and convert it star using csparc2star.py
  3. Compare the CoordinateX and CoordinateY columns in original and converted star files.

I compared all the CoordinateX and CoordinateY between the original and csparc2star converted star files with 243430 particles and all the values are the same.

I also did the similar tests after a 2D classification on cryosparc and downloading the cs file to make sure that the shift parameter is carried correctly. For shift (OriginX and OriginY in star file), the columns shouldn't need to be reversed.

@asarnow
Copy link
Owner

asarnow commented Nov 9, 2018

It's more than a 1 pixel difference (the most round vs floor can possibly be). It may simply be a scaling artifact in relion_display.

@asarnow
Copy link
Owner

asarnow commented Nov 11, 2018

@turalaksel FYI I've decided you're right about np.round since that's what cryoSPARC actually does internally. Still not sure why Relion display is skewed, but it should refine out regardless. Latest commit uses int(np.round()) as you recommended.

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

Successfully merging this pull request may close these issues.

2 participants