Ejemplo n.º 1
0
    def __init__(self,
                 modelFitters,
                 modelNames=None,
                 modelWeights=None,
                 fitterMethods=None,
                 numRestart=0,
                 isParallel=False,
                 logger=Logger()):
        """
        Parameters
        ----------
        modelFitters: list-modelFiter
        modelWeights: list-float
            how models are weighted in least squares
        modelNames: list-str
        fitterMethods: list-optimization methods
        numRestart: int
            number of times the minimization is restarted with random
            initial values for parameters to fit.
        isParallel: bool
            runs each fitter in parallel
        logger: Logger

        Raises
        ------
        ValueError: len(modelNames) == len(modelFitters)
        """
        self.numModel = len(modelFitters)
        self.modelWeights = modelWeights
        if self.modelWeights is None:
            self.modelWeights = np.repeat(1, self.numModel)
        self.modelNames = modelNames
        if self.modelNames is None:
            self.modelNames = [str(v) for v in range(len(modelSpecifications))]
        self._numRestart = numRestart
        self._isParallel = isParallel
        self.logger = logger
        # Validation checks
        if self.numModel != len(self.modelNames):
            msg = "Length of modelNames must be same as number of modelFitters."
            raise ValueError(msg)
        #
        self.fitterDct = {n: f for n, f in zip(modelNames, modelFitters)}
        # Construct tha parameters for each model
        self.parametersCollection = [f.params for f in self.fitterDct.values()]
        self.parameterManager = _ParameterManager(self.modelNames,
                                                  self.parametersCollection)
        self._fitterMethods = ModelFitter.makeMethods(
            fitterMethods, cn.METHOD_FITTER_DEFAULTS)
        # Residuals calculations
        self.residualsServers = [
            ResidualsServer(f, None, None, logger=self.logger)
            for f in self.fitterDct.values()
        ]
        self.manager = None
        # Results
        self.optimizer = None
Ejemplo n.º 2
0
    def testMakeMethods(self):
        if IGNORE_TEST:
            return
        self._init()
        METHOD = "dummy"

        def test(methods):
            result = ModelFitter.makeMethods(methods, None)
            self.assertTrue(isinstance(result, list))
            optimizerMethod = result[0]
            self.assertEqual(optimizerMethod.method, METHOD)
            self.assertTrue(isinstance(optimizerMethod.kwargs, dict))

        #
        test([METHOD])
        test([METHOD, METHOD])
        test([_helpers.OptimizerMethod(method=METHOD, kwargs={})])
        #
        methods = _helpers.OptimizerMethod(method=METHOD, kwargs={"a": 1})
        result = ModelFitter.makeMethods([methods, methods], None)
Ejemplo n.º 3
0
 def test(methods):
     result = ModelFitter.makeMethods(methods, None)
     self.assertTrue(isinstance(result, list))
     optimizerMethod = result[0]
     self.assertEqual(optimizerMethod.method, METHOD)
     self.assertTrue(isinstance(optimizerMethod.kwargs, dict))