Ejemplo n.º 1
0
    def test_header_missing(self, tmpdir):
        # write test file
        filename = str(tmpdir / 'testfile.fits')
        with create_testfile(filename, headers={}):
            # create init
            init = InitFromVhelio()

            # run it
            cmp = Component('star')
            init(cmp, filename)

            # v should not be set
            assert 'v' not in cmp.param_names
Ejemplo n.º 2
0
    def test_file_missing(self, tmpdir):
        # filename should not exist
        f = tmpdir / 'testfile.fits'
        if f.exists():
            f.unlink()

        # create init
        init = InitFromVhelio()

        # run it
        cmp = Component('star')
        init(cmp, str(f))

        # v should not be set
        assert 'v' not in cmp.param_names
Ejemplo n.º 3
0
    def test_default(self, tmpdir):
        # write test file
        filename = str(tmpdir / 'testfile.fits')
        with create_testfile(filename,
                             headers={
                                 'RA': 12,
                                 'DEC': -30,
                                 'DATE-OBS': '2018-10-17 12:00:00'
                             }):
            # create init
            init = InitFromVhelio()

            # run it
            cmp = Component('star')
            init(cmp, filename)

            # compare
            assert abs(cmp['v'] + 11.83) < 0.1
Ejemplo n.º 4
0
    def test_default(self):
        # init
        init = InitFromValues({'v': 10, 'Teff': 5800})

        # init mock component
        cmp = Component('star')
        cmp.set('v')
        cmp.set('Teff')

        # run it
        init(cmp, None)

        # compare
        assert 10 == cmp['v']
        assert 5800 == cmp['Teff']
Ejemplo n.º 5
0
    def test_parameters(self, test_csv):
        """Test user defined list of parameters to set."""

        # init
        init = InitFromCsv(test_csv, parameters=['teff'])

        # init mock component
        cmp = Component('star')
        cmp.set('v')
        cmp.set('Teff')

        # run it
        init(cmp, 'a.fits')

        # compare
        assert None == cmp['v']
        assert 4800 == cmp['Teff']
Ejemplo n.º 6
0
    def test_missing_value(self, test_csv):
        """Testing case of missing value in CSV."""

        # init
        init = InitFromCsv(test_csv)

        # init mock component
        cmp = Component('star')
        cmp.set('v')
        cmp.set('Teff')

        # run it
        init(cmp, 'b.fits')

        # compare
        assert -10 == cmp['v']
        assert None == cmp['Teff']
Ejemplo n.º 7
0
    def test_missing_file(self, test_csv):
        """Testing case of filename not existing in CSV."""

        # init
        init = InitFromCsv(test_csv)

        # init mock component
        cmp = Component('star')
        cmp.set('v')
        cmp.set('Teff')

        # run it
        init(cmp, 'c.fits')

        # compare
        assert None == cmp['v']
        assert None == cmp['Teff']
Ejemplo n.º 8
0
    def test_default(self, test_csv):
        """Test default behaviour, filling all values from CSV."""

        # init
        init = InitFromCsv(test_csv)

        # init mock component
        cmp = Component('star')
        cmp.set('v')
        cmp.set('Teff')

        # run it
        init(cmp, 'a.fits')

        # compare
        assert 100 == cmp['v']
        assert 4800 == cmp['Teff']
Ejemplo n.º 9
0
    def test_dummy(self, test_csv):
        """Test user defined parameter list with non-existing columns."""

        # init
        init = InitFromCsv(test_csv, parameters=['dummy'])

        # init mock component
        cmp = Component('star')
        cmp.set('v')
        cmp.set('Teff')

        # run it
        init(cmp, 'a.fits')

        # compare
        assert None == cmp['v']
        assert None == cmp['Teff']
Ejemplo n.º 10
0
    def test_missing_col(self, test_csv):
        """Testing case of missing column in CSV."""

        # init
        init = InitFromCsv(test_csv)

        # init mock component
        cmp = Component('star')
        cmp.set('v')
        cmp.set('Teff')
        cmp.set('logg')

        # run it
        init(cmp, 'a.fits')

        # compare
        assert 100 == cmp['v']
        assert 4800 == cmp['Teff']
        assert None == cmp['logg']
Ejemplo n.º 11
0
    def test_default(self, tmpdir):
        # write test file
        with create_testfile(str(tmpdir / 'testfile.fits'),
                             results={'star': {
                                 'v': 42.,
                                 'Teff': 5800
                             }}):
            # create init
            init = InitFromPath(str(tmpdir))

            # init mock component
            cmp = Component('star')
            cmp.set('v')
            cmp.set('Teff')

            # run it
            init(cmp, 'testfile.fits')

            # compare
            assert 42 == cmp['v']
            assert 5800 == cmp['Teff']
Ejemplo n.º 12
0
    def __call__(self, filename: str) -> List[float]:
        """Process the given file.

        Args:
            filename: Name of file to process.

        Returns:
            List of final values of parameters, ordered in the same way as the return value of parameters()
        """

        # init results dict with Nones
        parameters = self.parameters()
        results = {p: None for p in parameters}

        success = []

        # list with all fit parameters
        fit_params = []
        for routine in self._routines:
            fit_params.extend(routine.fit_parameters())

        fit_params = list(set(fit_params))

        # dictionary that contains the fit results of all iteration steps, used for convergence test
        results_total = {p: [] for p in fit_params}

        # if routine checks for convergence set the threshold now
        if self._iterations is None and self._max_iterations > 1:
            # initialize thresholds
            tmp = {}
            for cmp_name, cmp in self.objects['components'].items():
                # loop over all parameters of this component
                for param_name in cmp.param_names:
                    if param_name.lower() == 'teff':
                        tmp['{} {}'.format(cmp.prefix, param_name)] = 25.
                    elif param_name.lower() == 'v' or param_name.lower(
                    ) == 'sig':
                        tmp['{} {}'.format(cmp.prefix, param_name)] = 1.
                    else:
                        tmp['{} {}'.format(cmp.prefix, param_name)] = 0.05

            if self._threshold is not None:
                for cmp_name, values in self._threshold.items():
                    for param, threshold in values.items():
                        tmp['{} {}'.format(cmp_name,
                                           param)] = np.float(threshold)

            self._threshold = tmp

            maxiter = self._max_iterations
        else:
            if self._iterations is None and (self._max_iterations == 1
                                             or self._max_iterations is None):
                maxiter = 1
            else:
                maxiter = self._iterations

            self._max_iterations = None

        # loop iterations
        for it in range(maxiter):
            self.objects['init_iter'] = {}
            for cmp_name, cmp in self.objects['components'].items():
                if isinstance(cmp, TelluricsComponent):
                    continue

                self.objects['init_iter'][cmp_name] = Component(name=cmp_name)
                for param_name in cmp.param_names:
                    self.objects['init_iter'][cmp_name].set(
                        name=param_name, value=cmp[param_name])

            # loop main routines
            for routine in self._routines:
                # set poly degree for this routine
                routine._poly_degree = self._poly_degree

                # get parameters for this routine
                params = routine.parameters()

                # run routine
                res = routine(filename)

                # store results
                for i, p in enumerate(params):
                    # if parameter is a fit parameter in another iteration step don't overwrite previous result
                    # otherwise the error will be set to zero
                    if p in fit_params and p not in routine.fit_parameters():
                        # initialize dictionary
                        if results[p] is None:
                            results[p] = [res[i * 2], res[i * 2 + 1]]

                        continue

                    # copy both results and errors!
                    results[p] = [res[i * 2], res[i * 2 + 1]]

                # was iteration a success?
                success.append(res[-1])

            # check for convergence?
            if self._max_iterations is not None:
                # save results of all steps
                for p in fit_params:
                    results_total[p].append(results[p][0])

                # run at least for 2 iterations
                if it == 0:
                    continue

                # check for convergence
                if self.convergence(results_total):
                    # fit is successful if each iteration was a success
                    success = np.all(success)

                    # fit converged
                    converged = True

                    # convert results dict into results list
                    res = []
                    for p in parameters:
                        res.extend(results[p])

                    # add iteration, success and convergence to results
                    res.append(it + 1)
                    res.append(success)
                    res.append(converged)

                    if self._damped:
                        res.append(1.)

                    return res
                elif it == self._max_iterations - 1 and not self._damped:
                    # fit is successful if each iteration was a success
                    success = np.all(success)

                    # fit did not converge
                    converged = False

                    # convert results dict into results list
                    res = []
                    for p in parameters:
                        res.extend(results[p])

                    # add iteration, success and convergence to results
                    res.append(it + 1)
                    res.append(success)
                    res.append(converged)

                    return res

        # if fit did not converge try again with damping factor
        if self._max_iterations is not None and self._damped:
            for p in self._threshold:
                self._threshold[p] /= 3

            iterations = self._max_iterations
            for damping_factor in self._factors:
                # reset parameters to initial values
                for cmp_name, cmp in self.objects['components'].items():
                    cmp.init(filename)

                results = {p: None for p in parameters}
                success = []
                results_total = {p: [] for p in fit_params}

                # loop over fit parameters
                for it in range(3 * maxiter):
                    self.objects['init_iter'] = {}
                    for cmp_name, cmp in self.objects['components'].items():
                        if isinstance(cmp, TelluricsComponent):
                            continue

                        self.objects['init_iter'][cmp_name] = Component(
                            name=cmp_name)
                        for param_name in cmp.param_names:
                            self.objects['init_iter'][cmp_name].set(
                                name=param_name, value=cmp[param_name])

                    # loop main routines
                    for routine in self._routines:
                        # set poly degree for this routine
                        routine._poly_degree = self._poly_degree

                        # get parameters for this routine
                        params = routine.parameters()

                        # run routine
                        res = routine(filename)

                        # store results
                        for i, p in enumerate(params):
                            # if parameter is a fit parameter in another iteration step don't overwrite previous result
                            # otherwise the error will be set to zero
                            if p in fit_params and p not in routine.fit_parameters(
                            ):
                                # initialize dictionary
                                if results[p] is None:
                                    results[p] = [res[i * 2], res[i * 2 + 1]]

                                continue

                            # copy both results and errors!
                            results[p] = [res[i * 2], res[i * 2 + 1]]

                        # calculate damped result
                        for p in params:
                            if p not in routine.fit_parameters():
                                continue

                            for cmp_name, cmp in self.objects[
                                    'components'].items():
                                if not p.startswith(cmp.prefix):
                                    continue

                                name = p[len(cmp.prefix) + 1:]

                                cmp[name] = (
                                    1 - damping_factor
                                ) * self.objects['init_iter'][cmp_name][
                                    name] + damping_factor * results[p][0]
                                results[p][0] = (
                                    1 - damping_factor
                                ) * self.objects['init_iter'][cmp_name][
                                    name] + damping_factor * results[p][0]

                        # was iteration a success?
                        success.append(res[-1])

                    # save results of all steps
                    for p in fit_params:
                        results_total[p].append(results[p][0])

                    # run at least for max_iterations // 2 iterations
                    if it < self._max_iterations // 2:
                        continue

                    # check for convergence
                    if self.convergence(results_total):
                        # fit is successful if each iteration was a success
                        success = np.all(success)

                        # fit converged
                        converged = True

                        # convert results dict into results list
                        res = []
                        for p in parameters:
                            res.extend(results[p])

                        # add iteration, success and convergence to results
                        res.append(iterations + it + 1)
                        res.append(success)
                        res.append(converged)
                        res.append(damping_factor)

                        return res

                iterations += 3 * self._max_iterations

            # fit is successful if each iteration was a success
            success = np.all(success)

            # fit did not converge
            converged = False

            # convert results dict into results list
            res = []
            for p in parameters:
                res.extend(results[p])

            # add iteration, success and convergence to results
            res.append(iterations)
            res.append(success)
            res.append(converged)
            res.append(self._factors[-1])

            return res

        # fit is successful if each iteration was a success
        success = np.all(success)

        # convert results dict into results list
        res = []
        for p in parameters:
            res.extend(results[p])

        # add iteration and success to results
        res.append(self._iterations)
        res.append(success)

        return res