From e97e175554c7c3e6d84ab3164cd03129df05669c Mon Sep 17 00:00:00 2001 From: Wolfgang Kerzendorf Date: Mon, 24 Jun 2013 16:37:31 -0400 Subject: [PATCH 1/2] added new model parameter API. --- api_change_model_parameters.py | 55 ++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 api_change_model_parameters.py diff --git a/api_change_model_parameters.py b/api_change_model_parameters.py new file mode 100644 index 0000000..e52ac88 --- /dev/null +++ b/api_change_model_parameters.py @@ -0,0 +1,55 @@ +""" +The current API for models has most of the functionality we want, but, in my opinion, feels a bit clunky. I've been +working with SQLAlchemy a lot and am very happy with their API for table models. +Here's a quick Demo of one of my Table models: +""" + + +class GMOSMOSRawFITS(Base): + __tablename__ = 'gmos_mos_raw_fits' + + id = Column(Integer, ForeignKey('fits_file.id'), primary_key=True) + mjd = Column(Float) + instrument_id = Column(Integer, ForeignKey('instrument.id')) + observation_block_id = Column(Integer, ForeignKey('observation_block.id')) + observation_class_id = Column(Integer, ForeignKey('observation_class.id')) + observation_type_id = Column(Integer, ForeignKey('observation_type.id')) + object_id = Column(Integer, ForeignKey('object.id')) + mask_id = Column(Integer, ForeignKey('gmos_mask.id')) + +# I propose an API that borrows from the Table models: + +class BlackBody(modeling.Model): + + + temperature = Parameter(unit='K', equivalency=None, fixed=False, tied=False, bounds=()) + + frequency = ModelInput(unit='Hz', equivalency=u.spectral()) + intensity = ModelOutput(unit='erg/cm2') + + + +mybb = BlackBody() +mybb.temperature = 10000 +intensity = mybb(1 * u.angstrom) + +#I feel that in this API one can immediately see from the Model definition what this models does. What it's inputs and +#outputs are. If floats are given as input, the model would convert this directly to a Quantity with the specified unit. +#If quantities are given as inputs, the model would try to convert to the specified unit. +#ModelInput and ModelOutput immediately show what's going in and out. + + +#For models with multiple parameters like Polynomials there's a class for Parameter sets. + +class Poly1DModel(modeling.Model): + + c = ParameterSet(5, syntax='_%s', unit=None, equivalency=None, fixed=False, tied=False, bounds=()) + +mylittlepoly = Poly1DModel() +mylittlepoly.c_0 = 10. +mylittlepoly.fixed +False + +#I'm happy to try to implement this, but would need some help with the python magic. + + From e5698012669a9cabec3ae0cae465fc05eae5e46c Mon Sep 17 00:00:00 2001 From: Wolfgang Kerzendorf Date: Thu, 27 Jun 2013 21:19:33 -0400 Subject: [PATCH 2/2] changed the API slightly due to various suggestions --- api_change_model_parameters.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/api_change_model_parameters.py b/api_change_model_parameters.py index e52ac88..0dfb143 100644 --- a/api_change_model_parameters.py +++ b/api_change_model_parameters.py @@ -29,8 +29,15 @@ class BlackBody(modeling.Model): -mybb = BlackBody() -mybb.temperature = 10000 +>>> mybb = BlackBody() +>>> mybb.temperature = 10000 +>>> mybb.temperature + +>>> mybb = BlackBoddy(temperature=1000) +>>> mybb = BlackBoddy(temperature=500*u.Celsius) #let's pretend that u.Celsius exists +>>> mybb.temperature + + intensity = mybb(1 * u.angstrom) #I feel that in this API one can immediately see from the Model definition what this models does. What it's inputs and @@ -42,12 +49,12 @@ class BlackBody(modeling.Model): #For models with multiple parameters like Polynomials there's a class for Parameter sets. class Poly1DModel(modeling.Model): - - c = ParameterSet(5, syntax='_%s', unit=None, equivalency=None, fixed=False, tied=False, bounds=()) + # ParameterSet -> ParameterCollection due to naming conflict + c = ParameterCollection(5, syntax='_%s', unit=None, equivalency=None, fixed=False, tied=False, bounds=()) mylittlepoly = Poly1DModel() mylittlepoly.c_0 = 10. -mylittlepoly.fixed +mylittlepoly.c_0.fixed False #I'm happy to try to implement this, but would need some help with the python magic.