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

cleanup position unit #733

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

cleanup position unit #733

wants to merge 1 commit into from

Conversation

rpauszek
Copy link
Contributor

Why this PR?
Now with PositionUnit I thought it would be nice to cleanup and centralize everything having to do with position units for kymos, tracking and msd estimation. I think especially useful is the string formatting for diffusion constants handled as a method on the enum so we can guarantee consistency.

@rpauszek rpauszek requested review from a team as code owners January 30, 2025 13:23
@rpauszek rpauszek requested review from tommasogritti, tobiasjj and JoepVanlier and removed request for a team January 30, 2025 13:23
Copy link
Member

@JoepVanlier JoepVanlier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like an improvement over what we had. I like that it is in one place now, despite the fact that I feel a bit uncertain about the place where this functionality should live.

Comment on lines 1159 to 1160
unit=f"{kymotracks._calibration.unit}^2 / s",
_unit_label=f"{kymotracks._calibration.unit.label}²/s",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to replace it with as_ function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

Comment on lines 1122 to 1132
def as_diffusion(self):
return {
"unit": f"{self}^2 / s",
"_unit_label": f"{self.label}²/s",
}

def as_squared(self):
return {
"unit": f"{self}^2",
"_unit_label": f"{self.label}²",
}
Copy link
Member

@JoepVanlier JoepVanlier Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel a bit conflicted about these helper functions. I do like that it is in one place now. It does feel a little odd to put this in the PositionUnit itself. For now this is probably OK and an improvement from what we had before, but if we get more of these in the future, we might want to consider making a unit formatter or something like that, to separate the logic and not pollute the unit class too much with implementations for specific situations. In this case, the output feels very specifically tailored to those dataclasses we have in the diffusion module. It's an improvement from what we had before though.

A second thing is the naming. The as_ would imply to me that I'd be getting a new representation of this unit, rather than labels for a different unit alltogether. I'd sooner call it get_*_labels or something along those lines to make that a little more clear.

I think it would also make sense to add a type annotation for the return type in this case (as it is not immediately clear to me from the name).

Sorry for all the nitpicking. 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do wonder whether it isn't time to rope in an actual unit library to track units for us in these cases so that we also get composite units. Considering that _unit_labelis private in all the dataclass input args, one could consider changing these. You could have them take a UnitInfo | String directly for the unit argument and unpack to add the unit as string and the _unit_label in __post_init__. If we ensure that it can take both strings and units that shouldn't be breaking. That said, that would make more sense if we actually had composite units, because as it stands right now, you'd still need getters of the type you implemented, so this is probably not something to do already now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I agree with pretty much all of this and I had tried going down that route initially but it got pretty complicated pretty fast. I don't particularly like these small super-specific methods, but as far as I can see we only need something like this for the diffusion stuff at the moment so figured this way was the biggest payoff for the least amount of effort. I had thought about changing up the DiffusionEstimate dataclass too but you can't process constructor arguments with __post_init__ for frozen dataclasses so that's a complication. I'm sure there's a way to get all of this working, but again for just this one instance so far it seemed wise to go with the path of least resistance.

I do really like the name suggestion though, so I did change that 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense yeah. We can always introduce a different abstraction later once we know a few more places where we'd need similar functionality.

@rpauszek rpauszek force-pushed the ray/calibrate-tether-upside-down branch from 5385da7 to 73effc8 Compare January 31, 2025 13:40
Base automatically changed from ray/calibrate-tether-upside-down to main January 31, 2025 13:46
@rpauszek rpauszek force-pushed the ray/cleanup-position-unit branch 2 times, most recently from 1e82d2d to d9ddbff Compare February 3, 2025 10:09
@rpauszek rpauszek requested a review from JoepVanlier February 3, 2025 10:22
Copy link
Member

@JoepVanlier JoepVanlier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@JoepVanlier JoepVanlier force-pushed the ray/cleanup-position-unit branch from d9ddbff to 9ff03fa Compare February 28, 2025 15:29
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