Example #1
0
    def from_spectrum(self, spectrum, ancestors=[], visible=True):
        for key, value in spectrum.to_dict().items():
            self.__dict__[key] = value

        self.flambda = fl.convert_flux(spectrum.flux, spectrum.wavelength,
                                       spectrum.flux_unit, FluxUnit.F_lambda,
                                       WavelenghUnit.ANGSTROM)
        self.visible = visible
        self.ancestors = ancestors
Example #2
0
    def _unsmooth_trace(self,
                        trace_names,
                        application_data,
                        do_update_client=True):
        for trace_name in trace_names:
            traces = application_data['traces']
            trace = traces[trace_name]

            # use original flux stored as flambda
            flux = fl.convert_flux(
                flux=trace['flambda'],
                wavelength=trace['wavelength'],
                from_flux_unit=FluxUnit.F_lambda,
                to_flux_unit=trace.get('flux_unit'),
                to_wavelength_unit=trace.get('wavelength_unit'))
            trace['flux'] = flux
            traces[trace_name] = trace
            application_data['traces'] = traces

        if do_update_client:
            self.update_client()
Example #3
0
    def _fit_model_to_fluxOLD(self,
                              trace_names,
                              application_data,
                              fitting_models,
                              selected_data,
                              custom_model=None,
                              custom_fitter=None,
                              do_update_client=True,
                              include_fit_substracted_trace=False):
        # http://learn.astropy.org/rst-tutorials/User-Defined-Model.html
        # https://docs.astropy.org/en/stable/modeling/new-model.html
        # https://docs.astropy.org/en/stable/modeling/index.html
        # https://docs.astropy.org/en/stable/modeling/reference_api.html

        curve_mapping = {
            name: ind
            for ind, name in enumerate(application_data['traces'])
        }

        for fitting_model in fitting_models:
            for trace_name in trace_names:
                #ind = trace_indexes[trace_name]

                trace = application_data['traces'].get(trace_name)
                curve_number = curve_mapping[trace_name]

                x = np.asarray([
                    point['x'] for point in selected_data["points"]
                    if point['curveNumber'] == curve_number
                ])
                y = np.asarray([
                    point['y'] for point in selected_data["points"]
                    if point['curveNumber'] == curve_number
                ])
                ind = [
                    point['pointIndex'] for point in selected_data["points"]
                    if point['curveNumber'] == curve_number
                ]

                y_err = np.asarray(
                    trace["flux_error"]
                )[ind] if trace["flux_error"] is not None or len(
                    trace["flux_error"]) > 0 else None

                min_x, max_x = np.min(x), np.max(x)

                if custom_model is None and custom_fitter is None:

                    location_param = np.mean(x)
                    amplitude_param = np.max(np.abs(y))
                    spread_param = (max_x - min_x) / len(x)

                    if fitting_model == FittingModels.GAUSSIAN_PLUS_LINEAR:
                        data_model = models.Gaussian1D(
                            amplitude=amplitude_param,
                            mean=location_param,
                            stddev=spread_param) + models.Polynomial1D(
                                degree=1)
                    elif fitting_model == FittingModels.LORENTZIAN_PLUS_LINEAR:
                        data_model = models.Lorentz1D(
                            amplitude=amplitude_param,
                            x_0=location_param,
                            fwhm=spread_param) + models.Polynomial1D(degree=1)
                    elif fitting_model == FittingModels.VOIGT_PLUS_LINEAR:
                        data_model = models.Voigt1D(
                            x_0=location_param,
                            amplitude_L=amplitude_param,
                            fwhm_L=spread_param,
                            fwhm_G=spread_param) + models.Polynomial1D(
                                degree=1)
                    else:
                        raise Exception("Unsupported fitting model " +
                                        str(fitting_model))

                    fitting_model_name = fitting_model
                    fitter = fitting.LevMarLSQFitter()
                    fitted_model = fitter(data_model, x, y, weights=1. / y_err)

                else:
                    fitting_model_name = str(custom_model)
                    fitter = custom_fitter
                    fitted_model = fitter(custom_model,
                                          x,
                                          y,
                                          weights=1. / y_err)

                x_grid = np.linspace(min_x, max_x, 5 * len(x))
                y_grid = fitted_model(x_grid)

                parameter_errors = np.sqrt(
                    np.diag(fitter.fit_info['param_cov'])
                ) if fitter.fit_info['param_cov'] is not None else None

                fitted_trace_name = "fit" + str(
                    len(application_data['fitted_models']) +
                    1) + "_" + trace_name
                ancestors = trace['ancestors'] + [trace_name]
                flambda = [f for f in np.asarray(trace['flambda'])[ind]]
                fitted_trace = Trace(name=fitted_trace_name,
                                     wavelength=[x for x in x_grid],
                                     flux=[y for y in y_grid],
                                     ancestors=ancestors,
                                     spectrum_type=SpectrumType.FIT,
                                     color="black",
                                     linewidth=1,
                                     alpha=1.0,
                                     wavelength_unit=trace['wavelength_unit'],
                                     flux_unit=trace['flux_unit'],
                                     flambda=flambda,
                                     catalog=trace['catalog']).to_dict()

                self._set_color_for_new_trace(fitted_trace, application_data)
                self._add_trace_to_data(application_data, fitted_trace_name,
                                        fitted_trace, False)

                if include_fit_substracted_trace:
                    fitted_trace_name = "fit_substr_" + str(
                        len(application_data['fitted_models']) +
                        1) + "_" + trace_name
                    ancestors = trace['ancestors'] + [trace_name]

                    y_grid2 = fitted_model(x)
                    flux = y - y_grid2

                    f_labmda = fl.convert_flux(
                        flux=x,
                        wavelength=y,
                        from_flux_unit=trace['flux_unit'],
                        to_flux_unit=FluxUnit.F_lambda,
                        to_wavelength_unit=trace.get('flux_unit'))

                    fitted_trace = Trace(
                        name=fitted_trace_name,
                        wavelength=[x for x in x],
                        flux=[y for y in flux],
                        ancestors=ancestors,
                        spectrum_type=SpectrumType.FIT,
                        color="black",
                        linewidth=1,
                        alpha=1.0,
                        wavelength_unit=trace['wavelength_unit'],
                        flux_unit=trace['flux_unit'],
                        flambda=f_labmda,
                        catalog=trace['catalog']).to_dict()

                    self._set_color_for_new_trace(fitted_trace,
                                                  application_data)
                    self._add_trace_to_data(application_data,
                                            fitted_trace_name,
                                            fitted_trace,
                                            do_update_client=False)

                fitted_info = {}
                fitted_info['name'] = fitted_trace_name
                fitted_info['ancestors'] = ancestors
                fitted_info['model'] = fitting_model_name
                fitted_info['parameters'] = {
                    x: y
                    for (x, y) in zip(fitted_model.param_names,
                                      fitted_model.parameters)
                }
                fitted_info['parameter_errors'] = {
                    x: y
                    for (x,
                         y) in zip(fitted_model.param_names, parameter_errors)
                } if parameter_errors is not None else None
                fitted_info['selection_indexes'] = ind
                fitted_info['wavelength_unit'] = trace['wavelength_unit']
                fitted_info['flux_unit'] = trace['flux_unit']

                # add to application data:
                fitted_models = application_data['fitted_models']
                fitted_models[fitted_trace_name] = fitted_info
                application_data['fitted_models'] = fitted_models

                application_data['traces'][fitted_trace_name] = fitted_trace
                application_data['fitted_models'][
                    fitted_trace_name] = fitted_info
                self.write_info(
                    "fitting model2122 : " +
                    str(application_data['fitted_models'][fitted_trace_name]))

        if do_update_client:
            self.update_client()
Example #4
0
    def _fit_model_to_flux(self,
                           trace_names,
                           application_data,
                           model_fitters,
                           selected_data,
                           do_update_client=False,
                           add_fit_substracted_trace=False):

        curve_mapping = {
            name: ind
            for ind, name in enumerate(application_data['traces'])
        }

        fitted_info_list = []
        for model_fitter in model_fitters:
            for trace_name in trace_names:

                trace = application_data['traces'].get(trace_name)
                curve_number = curve_mapping[trace_name]

                x = np.asarray([
                    point['x'] for point in selected_data["points"]
                    if point['curveNumber'] == curve_number
                ])
                y = np.asarray([
                    point['y'] for point in selected_data["points"]
                    if point['curveNumber'] == curve_number
                ])
                ind = [
                    point['pointIndex'] for point in selected_data["points"]
                    if point['curveNumber'] == curve_number
                ]

                y_err = np.asarray(
                    trace["flux_error"]
                )[ind] if trace["flux_error"] is not None or len(
                    trace["flux_error"]) > 0 else None

                fitter = model_fitter.fitter
                model = model_fitter.model
                fitting_model_type = model_fitter.model_type

                fitted_model = fitter(model, x, y, weights=1. / y_err)

                min_x, max_x = np.min(x), np.max(x)
                x_grid = np.linspace(min_x, max_x, 5 * len(x))
                y_grid = fitted_model(x_grid)

                parameter_errors = np.sqrt(
                    np.diag(fitter.fit_info.get('param_cov'))
                ) if fitter.fit_info.get('param_cov') is not None else None
                parameters_covariance_matrix = fitter.fit_info.get('param_cov')

                fitted_trace_name = "fit" + str(
                    len(application_data['fitted_models']) +
                    1) + "_" + trace_name
                ancestors = trace['ancestors'] + [trace_name]
                flambda = [f for f in np.asarray(trace['flambda'])[ind]]
                fitted_trace = Trace(name=fitted_trace_name,
                                     wavelength=[x for x in x_grid],
                                     flux=[y for y in y_grid],
                                     ancestors=ancestors,
                                     spectrum_type=SpectrumType.FIT,
                                     color="black",
                                     linewidth=1,
                                     alpha=1.0,
                                     wavelength_unit=trace['wavelength_unit'],
                                     flux_unit=trace['flux_unit'],
                                     flambda=flambda,
                                     catalog=trace['catalog']).to_dict()

                self._set_color_for_new_trace(fitted_trace, application_data)
                self._add_trace_to_data(application_data, fitted_trace_name,
                                        fitted_trace, False)

                if add_fit_substracted_trace:
                    fitted_trace_name = "fitsub_" + str(
                        len(application_data['fitted_models']) +
                        1) + "_" + trace_name
                    ancestors = trace['ancestors'] + [trace_name]

                    flux = np.array(trace.get('flux'))
                    diff = y - fitted_model(x)
                    flux[ind] = diff

                    f_labmda = fl.convert_flux(
                        flux=x,
                        wavelength=y,
                        from_flux_unit=trace['flux_unit'],
                        to_flux_unit=FluxUnit.F_lambda,
                        to_wavelength_unit=WavelenghUnit.ANGSTROM)

                    fitted_trace = Trace(
                        name=fitted_trace_name,
                        wavelength=trace.get("wavelength"),
                        flux=[y for y in flux],
                        ancestors=ancestors,
                        spectrum_type=SpectrumType.FIT,
                        color="black",
                        linewidth=1,
                        alpha=1.0,
                        wavelength_unit=trace['wavelength_unit'],
                        flux_unit=trace['flux_unit'],
                        flambda=f_labmda,
                        catalog=trace['catalog']).to_dict()

                    self._set_color_for_new_trace(fitted_trace,
                                                  application_data)
                    self._add_trace_to_data(application_data,
                                            fitted_trace_name,
                                            fitted_trace,
                                            do_update_client=False)

                fitted_info = {}
                fitted_info['name'] = fitted_trace_name
                fitted_info['ancestors'] = ancestors
                fitted_info['model'] = fitting_model_type
                fitted_info['parameter_names'] = [
                    x for x in fitted_model.param_names
                ],
                fitted_info['parameters'] = {
                    x: y
                    for (x, y) in zip(fitted_model.param_names,
                                      fitted_model.parameters)
                }
                fitted_info['covariance'] = parameters_covariance_matrix
                fitted_info['parameter_errors'] = {
                    x: y
                    for (x,
                         y) in zip(fitted_model.param_names, parameter_errors)
                } if parameter_errors is not None else None
                fitted_info['selection_indexes'] = ind
                fitted_info['wavelength_unit'] = trace['wavelength_unit']
                fitted_info['flux_unit'] = trace['flux_unit']

                # add to application data:
                fitted_models = application_data['fitted_models']
                fitted_models[fitted_trace_name] = fitted_info
                application_data['fitted_models'] = fitted_models

                application_data['traces'][fitted_trace_name] = fitted_trace
                application_data['fitted_models'][
                    fitted_trace_name] = fitted_info

                current_fitting_model_types = application_data[
                    'fitting_model_types']
                if fitting_model_type not in current_fitting_model_types:
                    current_fitting_model_types.append(fitting_model_type)
                    application_data[
                        'fitting_model_types'] = current_fitting_model_types

                #self.write_info("fitting model : " + str(application_data['fitted_models'][fitted_trace_name]) )
                fitted_info_list.append(fitted_info)

        if do_update_client:
            self.update_client()

        return fitted_info_list
Example #5
0
    def _get_rescaled_axis_in_trace(self,
                                    trace,
                                    to_wavelength_unit=WavelenghUnit.ANGSTROM,
                                    to_flux_unit=FluxUnit.F_lambda):
        # https://synphot.readthedocs.io/en/latest/synphot/units.html
        # https://synphot.readthedocs.io/en/latest/api/synphot.units.convert_flux.html#synphot.units.convert_flux

        # for wavelength axis:
        trace['wavelength'] = fl.convert_wavelength(
            wavelength=trace['wavelength'],
            from_wavelength_unit=trace['wavelength_unit'],
            to_wavelength_unit=to_wavelength_unit)
        trace['wavelength_unit'] = to_wavelength_unit

        # for flux axis:
        if trace.get(
                'flux_unit'
        ) == FluxUnit.AB_magnitude and to_flux_unit != FluxUnit.AB_magnitude:
            if trace.get("flambda") is not None and len(
                    trace.get("flambda")) > 0:
                trace['flux'] = fl.convert_flux(
                    flux=trace.get("flambda"),
                    wavelength=trace['wavelength'],
                    from_flux_unit=FluxUnit.F_lambda,
                    to_flux_unit=to_flux_unit,
                    to_wavelength_unit=to_wavelength_unit)
                trace['flux_error'] = fl.convert_flux(
                    flux=trace.get("flambda_error"),
                    wavelength=trace['wavelength'],
                    from_flux_unit=FluxUnit.F_lambda,
                    to_flux_unit=to_flux_unit,
                    to_wavelength_unit=to_wavelength_unit)
            else:
                trace['flux'] = fl.convert_flux(
                    flux=trace['flux'],
                    wavelength=trace['wavelength'],
                    from_flux_unit=trace.get('flux_unit'),
                    to_flux_unit=to_flux_unit,
                    to_wavelength_unit=to_wavelength_unit)
                trace['flux_error'] = fl.convert_flux(
                    flux=trace['flux_error'],
                    wavelength=trace['wavelength'],
                    from_flux_unit=trace.get('flux_unit'),
                    to_flux_unit=to_flux_unit,
                    to_wavelength_unit=to_wavelength_unit)
        else:
            trace['flux'] = fl.convert_flux(
                flux=trace['flux'],
                wavelength=trace['wavelength'],
                from_flux_unit=trace.get('flux_unit'),
                to_flux_unit=to_flux_unit,
                to_wavelength_unit=to_wavelength_unit)
            trace['flux_error'] = fl.convert_flux(
                flux=trace['flux_error'],
                wavelength=trace['wavelength'],
                from_flux_unit=trace.get('flux_unit'),
                to_flux_unit=to_flux_unit,
                to_wavelength_unit=to_wavelength_unit)

        trace['flux_unit'] = to_flux_unit
        return trace
Example #6
0
    def _smooth_trace(self,
                      trace_names,
                      application_data,
                      smoother,
                      do_update_client=True,
                      do_substract=False,
                      as_new_trace=False,
                      new_trace_name=None):
        for trace_name in trace_names:
            if trace_name in application_data['traces']:
                traces = application_data['traces']
                trace = traces[trace_name]

                flux = fl.convert_flux(
                    flux=trace['flambda'],
                    wavelength=trace['wavelength'],
                    from_flux_unit=FluxUnit.F_lambda,
                    to_flux_unit=trace.get('flux_unit'),
                    to_wavelength_unit=trace.get('wavelength_unit'))

                smoothed_flux = smoother.get_smoothed_flux(flux)

                if do_substract:
                    smoothed_flux = flux - smoothed_flux

                if not as_new_trace:
                    trace['flux'] = smoothed_flux
                    traces[trace_name] = trace
                else:

                    if new_trace_name is None:
                        names = [
                            name for name in application_data['traces']
                            if trace_name in application_data['traces'][name]
                            ["ancestors"] and SpectrumType.SMOOTHED in
                            application_data['traces'][name]["spectrum_type"]
                        ]
                        smoothed_trace_name = "smoothed_" + str(
                            len(names) + 1) + "_" + trace_name
                    else:
                        smoothed_trace_name = new_trace_name

                    ancestors = trace['ancestors'] + [trace_name]

                    f_labmda = fl.convert_flux(
                        flux=[y for y in smoothed_flux],
                        wavelength=[x for x in trace["wavelength"]],
                        from_flux_unit=trace['flux_unit'],
                        to_flux_unit=FluxUnit.F_lambda,
                        to_wavelength_unit=WavelenghUnit.ANGSTROM)

                    smoothed_trace = Trace(
                        name=smoothed_trace_name,
                        wavelength=[x for x in trace["wavelength"]],
                        flux=[y for y in smoothed_flux],
                        flux_error=trace.get('flux_error'),
                        ancestors=ancestors,
                        spectrum_type=SpectrumType.SMOOTHED,
                        color="black",
                        linewidth=1,
                        alpha=1.0,
                        wavelength_unit=trace['wavelength_unit'],
                        flux_unit=trace['flux_unit'],
                        flambda=f_labmda,
                        flambda_error=trace.get("flambda_error"),
                        catalog=trace['catalog']).to_dict()

                    self._set_color_for_new_trace(smoothed_trace,
                                                  application_data)
                    traces[smoothed_trace_name] = smoothed_trace

                application_data['traces'] = traces

                # if kernel is custom, add it to the data dict:
                current_smoothing_kernels = application_data[
                    'smoothing_kernel_types']
                self.write_info("current_smoothing_kernels1: " +
                                str(current_smoothing_kernels))
                if smoother.kernel_func_type not in current_smoothing_kernels:
                    current_smoothing_kernels.append(smoother.kernel_func_type)
                self.write_info("current_smoothing_kernels2: " +
                                str(current_smoothing_kernels))
                application_data[
                    'smoothing_kernel_types'] = current_smoothing_kernels
                self.write_info(
                    "application_data['smoothing_kernel_types']: " +
                    str(application_data['smoothing_kernel_types']))

        if do_update_client:
            self.update_client()