Skip to content

Commit

Permalink
Modified Imfit.fit() to return fitResult object (like it says it does…
Browse files Browse the repository at this point in the history
… in the docstring);

modified Imfit.doFit() to always return fitResult object (getSummary keyword removed).

Documentation (docstring) updates.
  • Loading branch information
perwin committed Dec 2, 2021
1 parent ac4e791 commit b087f0c
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 82 deletions.
8 changes: 1 addition & 7 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: PyImfit
name: PyImfit CI

on:
push:
Expand Down Expand Up @@ -31,12 +31,6 @@ jobs:
python -m pip install --upgrade pip
pip install flake8 astropy pytest scons
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
# - name: Lint with flake8
# run: |
# # stop the build if there are Python syntax errors or undefined names
# flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
# flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Build and test import of package
run: |
pip install -e . ; python -c "import pyimfit"
Expand Down
5 changes: 4 additions & 1 deletion new_notes_and_todo_for_python-wrapper_code.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ PLAN:
[x] Code to generate function-set-as-dict from FunctionSetDescription object
--- getFuncSetAsDict
[x] Add test to test_descriptions.py for FunctionSetDescription.getFuncSetAsDict()
[ ] Write code till test passes
[x] Write code till test passes

[x] Code to generate model-as-dict from ModelDescription object
--- getModelAsDict
Expand Down Expand Up @@ -192,6 +192,8 @@ PLAN:
* Figure out fix for handling makeimage-style image generation
-- Currently, we ignore NCOLS/NROWS image-description parameters in a config file
(config.py) [that is, we load and store the values, but otherwise ignore them]
-- Remember that model-image size combines with PSF size to set the internal model-image array,
so it's not something that can be easily changed
-- Ideally, we want some way to specify image size via NCOLS/NROWS image-description parameters,
as is currently done by makeimage
-- right now, the only way to specify image size is
Expand Down Expand Up @@ -731,6 +733,7 @@ ModelObjectWrapper:




=====================================

HOWTO unit testing, regression testing, etc.:
Expand Down
14 changes: 9 additions & 5 deletions pyimfit/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

def parse_config_file( fileName: str ) -> ModelDescription:
"""
Read an Imfit model-configuration file.
Read and parse an Imfit model-configuration file.
Parameters
----------
Expand All @@ -62,7 +62,7 @@ def parse_config_file( fileName: str ) -> ModelDescription:

def parse_config( lines: List[str] ) -> ModelDescription:
"""
Parses an Imfit model configuration from a list of strings.
Parse an Imfit model configuration from a list of strings.
Parameters
----------
Expand Down Expand Up @@ -113,8 +113,12 @@ def clean_lines( lines: List[str] ) -> List[str]:
Parameters
----------
lines : list of str
Returns
-------
cleaned_lines : list of str
"""
clean = []
cleaned_lines = []
for line in lines:
# Clean the comments.
line = line.split(commentChar, 1)[0]
Expand All @@ -123,8 +127,8 @@ def clean_lines( lines: List[str] ) -> List[str]:
# Skip the empty lines.
if line == '':
continue
clean.append(line)
return clean
cleaned_lines.append(line)
return cleaned_lines



Expand Down
82 changes: 45 additions & 37 deletions pyimfit/descriptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,23 @@ def __init__( self, name: str, value: float, limits: Optional[Sequence[float]]=N
@property
def name(self):
"""
The label of the parameter. Examples: "x0", "I_e".
The label of the parameter (str). Examples: "x0", "I_e".
"""
return self._name


@property
def value(self):
"""
The value of the parameter.
The value of the parameter (float).
"""
return self._value


@property
def limits(self):
"""
The low and upper limits for the parameter, as a tuple.
The low and upper limits for the parameter, as a tuple of float.
"""
return self._limits

Expand Down Expand Up @@ -148,8 +148,7 @@ def setTolerance( self, tol: float ):
Parameters
----------
tol : float
Fractional offset for lower and upper limits
Must lie between ``0.0`` and ``1.0``.
Fractional offset for lower and upper limits; must lie between 0.0 and 1.0.
"""
if tol > 1.0 or tol < 0.0:
raise ValueError('Tolerance must be between 0.0 and 1.0.')
Expand Down Expand Up @@ -211,7 +210,7 @@ def getStringDescription( self, noLimits=False, error: Optional[float]=None ):
Returns
-------
outputString : string
outputString : str
"""
outputString = "{0}\t\t{1}".format(self.name, self.value)
if error is not None:
Expand Down Expand Up @@ -252,12 +251,6 @@ def __eq__(self, rhs):

def __str__(self):
return self.getStringDescription()
# if self._fixed:
# return '{0:s} {1:f} fixed'.format(self._name, self._value)
# elif self.limits is not None:
# return '{0:s} {1:f} {2:f},{3:f}'.format(self._name, self._value, self._limits[0], self._limits[1])
# else:
# return '{0:s} {1:f}'.format(self._name, self._value)



Expand Down Expand Up @@ -325,7 +318,7 @@ def dict_to_FunctionDescription(cls, inputDict):
Returns
-------
fset : :class:`FunctionDescription`
fdesc : :class:`FunctionDescription`
The function description.
"""
funcName = inputDict['name']
Expand Down Expand Up @@ -361,6 +354,13 @@ def label(self):


def addParameter( self, p: ParameterDescription ):
"""
Add a new ParameterDescription object.
Params
------
p : ParameterDecription
"""
if not isinstance(p, ParameterDescription):
raise ValueError('p is not a ParameterDescription object.')
self._parameters.append(p)
Expand Down Expand Up @@ -555,8 +555,7 @@ def __init__( self, label: Optional[str]="", x0param: Optional[ParameterDescript
@classmethod
def dict_to_FunctionSetDescription(cls, inputDict: dict):
"""
This is a convenience method to generate a FunctionSetDescription object
from a dict
A convenience method to generate a FunctionSetDescription object from a dict
Parameters
----------
Expand Down Expand Up @@ -734,6 +733,7 @@ def getFuncSetAsDict(self):
Returns a dict describing the function set (suitable for use in e.g. dict_to_FunctionSetDescription())
{'X0': list, 'Y0': list, 'function_list': [list of dicts describing functions]}
OR
{'label': str, 'X0': list, 'Y0': list, 'function_list': [list of dicts describing functions]}
"""
funcSetDict = {}
Expand Down Expand Up @@ -849,8 +849,8 @@ def __init__( self, functionSetsList: Optional[List[FunctionSetDescription]]=Non
@classmethod
def load(cls, fileName: str):
"""
This is a convenience method to generate a ModelDescription object
from a standard Imfit configuration file.
A convenience method to generate a ModelDescription object from a standard Imfit
configuration file.
Parameters
----------
Expand All @@ -877,7 +877,7 @@ def load(cls, fileName: str):
@classmethod
def dict_to_ModelDescription(cls, inputDict: dict):
"""
This is a convenience method to generate a ModelDescription object from a dict.
A convenience method to generate a ModelDescription object from a dict.
Parameters
----------
Expand Down Expand Up @@ -963,7 +963,7 @@ def addFunctionSet(self, fs: FunctionSetDescription):

def updateOptions( self, optionsDict: Dict[str,float] ):
"""
Updates the internal image-descriptions dict, replacing current values for keys
Update the internal image-descriptions dict, replacing current values for keys
already in the dict and added key-value pairs for keys not already present.
Parameters
Expand All @@ -975,11 +975,10 @@ def updateOptions( self, optionsDict: Dict[str,float] ):

def replaceOptions( self, optionsDict: Dict[str,float] ):
"""
Replaces the current image-descriptions dict. This differs from updateOptions()
Replace the current image-descriptions dict. This differs from updateOptions()
in that it will completely replace the current image-descriptions dict,
discarding any key-value pairs with keys not in the replacement dict.
Parameters
----------
optionDict : dict
Expand All @@ -996,8 +995,12 @@ def _contains(self, label: str):

def functionSetIndices(self):
"""
Returns the indices in the full parameters list corresponding
Get the indices in the full parameters list corresponding
to the starts of individual function sets/blocks.
Returns
-------
indices : list of int
"""
indices = [0]
for i in range(self.nFunctionSets - 1):
Expand All @@ -1008,11 +1011,11 @@ def functionSetIndices(self):

def functionList(self):
"""
List of the FunctionDescription objects making up this model.
Get list of the FunctionDescription objects making up this model.
Returns
-------
func_list : list of FunctionDescription objects
functions : list of FunctionDescription objects
"""
functions = []
for function_set in self._functionSets:
Expand All @@ -1022,11 +1025,11 @@ def functionList(self):

def functionNameList(self):
"""
List of names of the image functions making up this model.
Get list of names of the image functions making up this model.
Returns
-------
func_list : list of str
functionNames : list of str
List of the function names.
"""
functionNames = []
Expand All @@ -1037,11 +1040,11 @@ def functionNameList(self):

def functionLabelList(self):
"""
List of labels for the image functions making up this model.
Get list of labels for the image functions making up this model.
Returns
-------
func_list : list of str
functionLabels : list of str
List of the function labels.
"""
functionLabels = []
Expand All @@ -1052,12 +1055,13 @@ def functionLabelList(self):

def functionSetNameList(self):
"""
List of the functions composing this model, as strings, grouped by function set.
Get list of the functions composing this model, as strings, grouped by function set.
Returns
-------
func_set_list : list of list of string
List of the function names, grouped by function set: [[functions_in_set1], [functions_in_set2], ...]
functionSetList : list of list of string
List of the function names, grouped by function set:
[[functions_in_set1], [functions_in_set2], ...]
"""
functionSetList = []
for function_set in self._functionSets:
Expand All @@ -1068,11 +1072,11 @@ def functionSetNameList(self):

def parameterList(self):
"""
A list of the parameters (ParameterDescription objects) making up this model.
Get list of the parameters (ParameterDescription objects) making up this model.
Returns
-------
param_list : list of :class:`ParameterDescription`
params : list of :class:`ParameterDescription`
List of the parameters.
"""
params = []
Expand All @@ -1083,7 +1087,7 @@ def parameterList(self):

def getRawParameters(self):
"""
Returns a Numpy array of the ModelDescription's current parameter values
Get Numpy array of the ModelDescription's current parameter values
Returns
-------
Expand All @@ -1095,7 +1099,7 @@ def getRawParameters(self):

def getParameterLimits(self):
"""
Returns a list containing lower and upper limits for all parameters in the model.
Get list containing lower and upper limits for all parameters in the model.
Returns
-------
Expand All @@ -1108,7 +1112,7 @@ def getParameterLimits(self):

def getStringDescription( self, noLimits=False, errors: Optional[Sequence[float]]=None, saveOptions=False ):
"""
Returns a list of strings suitable for inclusion in an imfit/makeimage config file.
Get list of strings suitable for inclusion in an imfit/makeimage config file.
Parameters
----------
Expand All @@ -1123,7 +1127,7 @@ def getStringDescription( self, noLimits=False, errors: Optional[Sequence[float]
Returns
-------
outputStrings : list of string
outputLines : list of string
list of newline-terminated strings describing the model.
If errors is supplied, then parameter strings will contain "# +/- <error>" at end
"""
Expand Down Expand Up @@ -1151,6 +1155,10 @@ def getStringDescription( self, noLimits=False, errors: Optional[Sequence[float]
def getModelAsDict( self ):
"""
Returns the model in dict form (suitable for use in e.g. dict_to_ModelDescription)
Returns
-------
modelDict : dict
"""
modelDict = {}
fsetDictList = [function_set.getFuncSetAsDict() for function_set in self._functionSets ]
Expand Down

0 comments on commit b087f0c

Please sign in to comment.