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

Refactor the way analysis settings are set #110

Open
wants to merge 10 commits into
base: v2.0.0
Choose a base branch
from

Conversation

crnh
Copy link
Collaborator

@crnh crnh commented Jan 13, 2025

Proposed change

This is a follow-up on #108.

In the current implementation, analysis settings can be specified using both a settings object and keyword arguments.
In some cases, this implementation can prevent changing modified settings back to their default values.
Implementations that don't exhibit this behavior rely on **kwargs, which means we lose the specification of the default arguments in the function signature.

The __init__ method of analysis wrappers now only allows to specify the settings using keyword arguments. Initializing an analysis with a settings object can be done using the from_settings classmethod, e.g. ZernikeStandardCoefficients.from_settings(ZernikeStandardCoefficientsSettings(sampling="64x64", maximum_term=45)). A downside of this implementation is that we lose the ability to create a new 'default' set of settings and then easily overwrite some of these settings, which was a major reason to choose the previously proposed implementation, but we can probably live with that.

Other (related) changes:

  • The type of the analysis settings object is now determined from the class signature, e.g. class PolarizationTransmission( BaseAnalysisWrapper[PolarizationTransmissionResult, PolarizationTransmissionSettings]): ... is sufficient to let BaseAnalysisWrapper find out that settings should have the PolarizationTransmissionSettings type. As a result, it is no longer necessary to create a PolarizationTransmissionSettings instance in PolarizationTransmission.__init__.
  • All analysis classes contained some configuration parameters (e.g. the analysis type) that were specified as class attributes. These are now specified as arguments instead. See the example below:
# Old specification
class PolarizationTransmission(
    BaseAnalysisWrapper[PolarizationTransmissionResult, PolarizationTransmissionSettings],
):
    TYPE = "Transmission"
    _needs_config_file = True
    _needs_text_output_file = True

    ...

# New specification
class PolarizationTransmission(
    BaseAnalysisWrapper[PolarizationTransmissionResult, PolarizationTransmissionSettings],
    analysis_type="Transmission",
    needs_config_file=True,
    needs_text_output_file=True,
):
    ...

Type of change

  • Example (a notebook demonstrating how to use ZOSPy for a specific application)
  • Bugfix (non-breaking change which fixes an issue)
  • New analysis (a wrapper around an OpticStudio analysis)
  • New feature (other than an analysis)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests
  • Documentation (improvement of either the docstrings or the documentation website)

Additional information

  • Python version: 3.9-3.12
  • OpticStudio version: 24R2

Related issues

Checklist

  • I have followed the contribution guidelines
  • The code has been linted, formatted and tested locally using tox.
  • Local tests pass. Please fix any problems before opening a PR. If this is not possible, specify what doesn't work and why you can't fix it.
  • I added new tests for any features contributed, or updated existing tests.
  • I updated CHANGELOG.md with my changes (except for refactorings and changes in the documentation).

If you contributed an analysis:

  • I did not use AttrDicts for the analysis result data (please use dataclasses instead).

If you contributed an example:

  • I contributed my example as a Jupyter notebook.

crnh added 4 commits January 6, 2025 13:17
- Only allow specification through keyword arguments, use `from_settings` classmethod to use a settings object
- Use __init_subclass__ to automatically determine the settings object type from the class signature
- Use __init_subclass__ to specify class-level settings as arguments instead of attributes
@crnh crnh requested review from LucVV and jwmbeenakker January 13, 2025 15:01
@crnh crnh added skip changelog It is not necessary to update the changelog for this PR open science fund labels Jan 13, 2025
@crnh
Copy link
Collaborator Author

crnh commented Jan 13, 2025

The classmethod is now called from_settings but we do not really create an analysis wrapper from those settings, but rather with those settings. with_settings may therefore be a better name. @LucVV which one do you prefer?

@jwmbeenakker
Copy link
Contributor

Todo at a later stage: add an example on how to iteratively update only one value of the settings class

@crnh
Copy link
Collaborator Author

crnh commented Jan 15, 2025

I added a new update_settings method to allow for batch-updating settings on an analysis using values from a dictionary. This method was already used internally to change the settings, so effectively this doesn't change much.

@LucVV LucVV mentioned this pull request Feb 5, 2025
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
open science fund skip changelog It is not necessary to update the changelog for this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants