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

User-defined features #902

Draft
wants to merge 11 commits into
base: release-2.5
Choose a base branch
from

Conversation

kecnry
Copy link
Member

@kecnry kecnry commented Jun 13, 2024

This PR adds support for defining custom features in-line (and likely supersedes #891 and eventually #892).

Current "simple" example:

import phoebe

b = phoebe.default_binary()
b.add_dataset('lc', compute_phases=phoebe.linspace(0,1,101))

from phoebe.features import DatasetFeature
from phoebe.parameters import FloatParameter, ParameterSet, constraint
from astropy import units as u
import numpy as np

class SinusoidalThirdLight(DatasetFeature):
    #allowed_dataset_kinds = ['lc']

    @classmethod
    def get_parameters(self, feature, **kwargs):
        params = []
        params += [FloatParameter(qualifier='amplitude',
                                  latexfmt=r'A_\mathrm{{ {feature} }}',
                                  value=kwargs.get('amplitude', 1.0),
                                  default_unit=u.W/u.m**2,
                                  description='Amplitude of the third light sinusoidal contribution')]
        params += [FloatParameter(qualifier='period',
                                  latexfmt=r'P_\mathrm{{ {feature} }}',
                                  value=kwargs.get('period', 1.0),
                                  default_unit=u.d,
                                  description='Period of the third light sinusoidal contribution')]
        params += [FloatParameter(qualifier='freq',
                                  latexfmt=r'f_\mathrm{{ {feature} }}',
                                  value=kwargs.get('freq', 2*np.pi/3.0),
                                  default_unit=u.rad/u.d, advanced=True,
                                  description='Frequency of the third light sinusoidal contribution')]

        constraints = [(constraint.freq, feature, 'feature')]

        return ParameterSet(params), constraints

    def modify_model(self, b, feature_ps, model_ps):
        from astropy import units as u
        import numpy as np
        _skip_filter_checks = {'check_default': False, 'check_visible': False}

        period = feature_ps.get_value(qualifier='period', unit=u.d, **_skip_filter_checks)
        t0 = b.get_value(qualifier='t0', context='system', unit=u.d, **_skip_filter_checks)
        for flux_param in model_ps.filter(qualifier='fluxes', **_skip_filter_checks).tolist():
            ampl = feature_ps.get_value(qualifier='amplitude', unit=flux_param.default_unit, **_skip_filter_checks)
            times = model_ps.get_value(qualifier='times', dataset=flux_param.dataset, unit=u.d, **_skip_filter_checks)
            flux_param.set_value(flux_param.get_value() + ampl * np.sin(2 * np.pi * (times - t0)) / period, ignore_readonly=True, **_skip_filter_checks)

b.add_feature(SinusoidalThirdLight, dataset='lc01')
b.run_compute()

TODO:

  • support for component features (and example)
  • try to remove need to define imports in-method (or document otherwise)
  • support overwriting code for an existing feature (perhaps just with add_feature(..., overwrite=True, **b.get_feature(...))
  • decide between registering vs storing code, etc.
  • entries need to show up in list_available_features
  • example for migrating spots
  • example for sinusoidal intensities
  • test coverage
  • documentation

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.

1 participant