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

Add position prediction #24

Merged
merged 27 commits into from
Mar 27, 2024
Merged

Add position prediction #24

merged 27 commits into from
Mar 27, 2024

Conversation

mdw771
Copy link

@mdw771 mdw771 commented Mar 11, 2024

Added feature

Position prediction module is added in ptychonn/pospred.

Usage

See pospred_readme.md.

Test

A unit tester is added as tests/test_multiiter_pos_calculation.py. The tester takes predicted images and configurations from tests/data/pospred/pred_test235 and tests/data/pospred/config_235.toml and performs a 3-iteration run of position prediction. Results are compared with reference data in tests/data_gold/pospred/calc_pos_235.csv.

Copy link
Collaborator

@carterbox carterbox left a comment

Choose a reason for hiding this comment

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

Explicit parameters are good. What feature do defaultdicts provide that dataclasses do not? (What is the reason the default dicts are still be used?)

tests/pospred_readme.md Outdated Show resolved Hide resolved
ptychonn/pospred/configs.py Outdated Show resolved Hide resolved
class InferenceConfigDict(ConfigDict):

# ===== PtychoNN configs =====
batch_size: Any = 1
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's the purpose of providing type hints, if everything is type "Any"?

Copy link
Author

Choose a reason for hiding this comment

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

They were placeholders when I refactored it from defaultdict to dataclass and will be filled in in the next commit.

ptychonn/pospred/configs.py Outdated Show resolved Hide resolved
ptychonn/pospred/core.py Outdated Show resolved Hide resolved
ptychonn/pospred/core.py Outdated Show resolved Hide resolved
ptychonn/pospred/message_logger.py Outdated Show resolved Hide resolved
ptychonn/pospred/configs.py Outdated Show resolved Hide resolved
tests/pospred_readme.md Outdated Show resolved Hide resolved
@mdw771
Copy link
Author

mdw771 commented Mar 12, 2024

Comments have been addressed as of commit 41faf63.

ptychonn/pospred/__init__.py Outdated Show resolved Hide resolved
ptychonn/pospred/configs.py Outdated Show resolved Hide resolved
ptychonn/position/configs.py Outdated Show resolved Hide resolved
self.random_seed = 123
self.tol = configs.nonhybrid_registration_tol
self.min_roi_stddev = configs.min_roi_stddev
self.debug = False
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not use the python built-in __debug__ instead of adding a parameter this object?

Copy link
Author

Choose a reason for hiding this comment

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

My knowledge might be incomplete and inaccurate, but apparently __debug__ is True by default and the only way to turn it off is to run the script with -O or set the PYTHONOPTIMIZE environment variable. This is opposite to what I wanted since I want the debugging code to not run by default.

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK. Makes sense.


schedule_learning_rate: bool = True

pretrained_model_path: str = None
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
pretrained_model_path: str = None
pretrained_model_path: typing.Optional[str] = None

Any parameter that has None as the default value is Optional.

Copy link
Author

Choose a reason for hiding this comment

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

Since this is now in the PtychoNN package, modeling training and inference should use the protocols that already exist in the package, so I removed the parameters related to PtychoNN training and inference in f5a3af2.

num_lines_for_validation: int = 805
"""Number of lines used for testing"""

dataset: Any = None
Copy link
Collaborator

Choose a reason for hiding this comment

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

How can the dataset be optional? Maybe you want to move this parameter to the top and make it required by not providing a default value?

Copy link
Author

Choose a reason for hiding this comment

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

Similar to the comment above, since this is now a part of the PtychoNN package, it should use the existing protocols for training and inference so I removed the config parameters in the position module in f5a3af2.

Copy link
Collaborator

Choose a reason for hiding this comment

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

No parameters should have a type hint of Any unless they can truly be anything. Parameters that can be int or None, for example, should either be typing.Optional[int] or int | None. The | syntax requires a minimum python version of 3.10.

Copy link
Author

Choose a reason for hiding this comment

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

Replaced Anys with more specific type hints in fdc7ae7.

tests/test_multiiter_pos_calculation.py Show resolved Hide resolved
Comment on lines 8 to 25
class Reconstructor:
def __init__(self, config_dict: InferenceConfig):
"""
Inference engine for PtychoNN.

:param config_dict: dict. Configuration dictionary.
"""
self.config_dict = config_dict
self.device = None

def build(self):
if torch.cuda.is_available():
self.device = torch.device("cuda")
else:
self.device = torch.device("cpu")

def batch_infer(self, x):
pass
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can unused definitions like this one be removed?

Copy link
Author

Choose a reason for hiding this comment

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

Removed unused definitions and modules in bc80a68.

@carterbox
Copy link
Collaborator

LGTM. Thanks, @mdw771!

@carterbox carterbox merged commit c8935ad into mcherukara:package Mar 27, 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

Successfully merging this pull request may close these issues.

2 participants