Exemplo n.º 1
0
def vegaFitsFile(request, fits_id):
    fitsfile = FitsFiles.objects.get(pk=fits_id)
    #
    datadir = Path('../../../')

    templatedir = Path('../../../tests/data/marz/')
    TEMPLATE_FILENAME = 'MarzTemplates.json'

    anyFILE = datadir / fitsfile.filepath

    formats = ssv.loaders.whatformat(anyFILE)
    if len(formats) > 1:
        ssv.loaders.unregister(formats[0])

    spectrum_data = read_spectra_file_simple(anyFILE)
    ssv.loaders.restore_registered_loaders()

    template_data = read_template_file(templatedir / TEMPLATE_FILENAME)
    spectrum = SimpleSpectrum('Test SSV', spectrum_data)
    spectrum.set_visible_traces('reduced')

    spectrum2 = SimpleSpectrum('Test SSV 2', spectrum_data)
    spectrum2.set_visible_traces('sky')
    spectrum2.set_transform_functions('sky', [utils.remove_spurious_points])
    spectrum2.offset_flux(-100, 'sky')

    lines = SimpleSpectralLines()

    templates = SimpleSpectrum('Templates', template_data)
    templates.set_visible_traces('Quasar')
    #templates.subtract_continuum()

    viewer = SimpleSpectrumViewer('Simple')
    viewer.add_spectrum(spectrum)
    viewer.add_spectrum(spectrum2)
    #if showTemplates:
    #    viewer.add_spectrum(templates)
    viewer.add_lines(lines)
    viewer.show_grid(True)
    viewer.show_legend(True)

    js_data = viewer.build_chart().to_json()
    return JsonResponse(json.loads(js_data), safe=False)
Exemplo n.º 2
0
def main():
    args = parse_args(sys.argv[1:])
    print("fits file to use {}".format(args.input))
    spectrum_filename = os.path.basename(args.input)
    spectrum_file = args.input
    formats = ssv.loaders.whatformat(spectrum_file)
    if len(formats) > 1:
        ssv.loaders.unregister(formats[0])
    spectrum_data = read_spectra_file_simple(spectrum_file)
    ssv.loaders.restore_registered_loaders()
    template_data = read_template_file(templatedir / TEMPLATE_FILENAME)

    # best_fit_redshift, best_fit_template = fit_template_and_redshift(str(spectrum_file))

    template_list = [template.meta['purpose'] for template in template_data]
    best_fit_redshift = 0
    best_fit_template = template_list[0]
    best_fit_template_index = template_list.index(best_fit_template)

    smoothing_function_list = [box_smooth, gaussian_smooth, trapezoid_smooth, median_smooth]

    ##### Streamlit Layout #####

    st.sidebar.write(spectrum_filename)
    show_sky = st.sidebar.checkbox("Show Sky", True, key=1)
    show_templates = st.sidebar.checkbox("Show Templates", True, key=2)
    show_variance = st.sidebar.checkbox("Show Variance", False, key=3)
    show_processed_data = st.sidebar.checkbox("Process Data", True, key=5)
    show_continuum_subtracted = st.sidebar.checkbox("Subtract Continuum", False, key=6)

    top_controls = st.beta_container()

    chart_container = st.beta_container()

    # lower_controls = st.beta_container()
    lower_controls = st.beta_columns(2)
    template_choice = lower_controls[0].selectbox("Template", template_list, index=best_fit_template_index)
    smoothing_choice = lower_controls[0].selectbox("Smoothing Function", smoothing_function_list, format_func=lambda x: x.__name__, index=0)
    smoothing_width = lower_controls[0].slider("Smoothing Width", min_value=1, max_value=51, step=2, value=7)
    show_scaled_spectra = lower_controls[1].checkbox("Scale Spectra", True, key=4)
    scaling_maximum = lower_controls[1].text_input("Scaling Maximum Value", value=1.0) # This should be replaced with a number_input
    scaling_max = float(scaling_maximum)                                               # but currently it doesn't seem to save value between runs

    # Streamlit widgets automatically run the script from top to bottom. Since
    # this button is not connected to any other logic, it just causes a plain
    # rerun.
    st.button("Re-run")


    ##### Main Code #####


    spectrum = SimpleSpectrum('Test SSV', spectrum_data)
    spectrum.set_visible_traces('reduced')
    flux_range = spectrum.flux_range('reduced')
    spectrum.set_variance_visible('reduced', show_variance)
    spectrum.set_trace_visible('sky', show_sky)
    spectrum.set_transform_functions('reduced', transform_function_array(scaled=show_scaled_spectra, scaling_max=scaling_max, processed=show_processed_data, subtract_continuum=show_continuum_subtracted, smoothing_function=smoothing_choice, smoothing_width=smoothing_width))
    spectrum.set_transform_functions('sky', transform_function_array(scaled=show_scaled_spectra, scaling_max=scaling_max, smoothing_function=smoothing_choice, smoothing_width=smoothing_width))

    # spectrum2 = SimpleSpectrum('Test SSV 2', spectrum_data)
    # spectrum2.set_visible_traces('sky')
    # spectrum2.offset_flux(-100, 'sky')

    lines = SimpleSpectralLines()
    lines.redshift_wavelength(best_fit_redshift)

    templates = SimpleSpectrum('Templates', template_data)
    templates.set_visible_traces(template_choice)
    templates.set_trace_visible(template_choice, show_templates)
    templates.redshift_wavelength(best_fit_redshift, best_fit_template)
    templates.set_transform_functions(template_choice, transform_function_array(scaled=show_scaled_spectra, scaling_max=scaling_max, processed=show_processed_data, subtract_continuum=show_continuum_subtracted, smoothing_function=smoothing_choice, smoothing_width=smoothing_width))

    viewer = SimpleSpectrumViewer('Simple')
    viewer.add_spectrum(spectrum)
    viewer.add_spectrum(templates)
    viewer.add_lines(lines)
    viewer.show_grid(True)
    viewer.show_legend(True)
    viewer.set_chart_width_height(height=500)

    chart_container.altair_chart(viewer.build_chart(), use_container_width=True)
Exemplo n.º 3
0
    def test_ozdes_smooth(self, shared_datadir):
        spectrum_file = shared_datadir / OZDES_TEST_FILENAME
        formats = ssv.ssvloaders.whatformat(spectrum_file)
        if len(formats) > 1:
            ssv.ssvloaders.unregister(formats[0])
        spectrum_data = utils.read_spectra_file(spectrum_file)
        ssv.ssvloaders.restore_registered_loaders()

        show_sky = True
        show_templates = True
        show_variance = True
        show_processed_data = True
        show_continuum_subtracted = False
        show_scaled_spectra = True
        scaling_max = 1.0
        smoothing_choice = box_smooth
        smoothing_width = 3

        spectrum = SimpleSpectrum('Test SSV', spectrum_data)
        spectrum.set_visible_traces('reduced')
        flux_range = spectrum.flux_range('reduced')
        spectrum.set_variance_visible('reduced', show_variance)
        spectrum.set_trace_visible('sky', show_sky)
        spectrum.set_transform_functions(
            'reduced',
            transform_function_array(
                scaled=show_scaled_spectra,
                scaling_max=scaling_max,
                processed=show_processed_data,
                subtract_continuum=show_continuum_subtracted,
                smoothing_function=smoothing_choice,
                smoothing_width=smoothing_width))
        spectrum.set_transform_functions(
            'sky',
            transform_function_array(scaled=show_scaled_spectra,
                                     scaling_max=scaling_max,
                                     smoothing_function=smoothing_choice,
                                     smoothing_width=smoothing_width))

        best_fit_redshift = 0
        best_fit_template = 'Quasar'
        best_fit_template_index = 12

        lines = SimpleSpectralLines()
        lines.redshift_wavelength(best_fit_redshift)

        templatedir = Path('./tests/data/marz/')
        TEMPLATE_FILENAME = 'MarzTemplates.json'
        template_data = utils.read_template_file(templatedir /
                                                 TEMPLATE_FILENAME)
        templates = SimpleSpectrum('Templates', template_data)
        template_choice = 'Quasar'
        templates.set_visible_traces(template_choice)
        templates.set_trace_visible(template_choice, show_templates)
        templates.redshift_wavelength(best_fit_redshift, best_fit_template)
        templates.set_transform_functions(
            template_choice,
            transform_function_array(
                scaled=show_scaled_spectra,
                scaling_max=scaling_max,
                processed=show_processed_data,
                subtract_continuum=show_continuum_subtracted,
                smoothing_function=smoothing_choice,
                smoothing_width=smoothing_width))

        viewer = SimpleSpectrumViewer('Simple')
        viewer.add_spectrum(spectrum)
        viewer.add_spectrum(templates)
        viewer.add_lines(lines)
        viewer.show_grid(True)
        viewer.show_legend(True)
        viewer.set_chart_width_height(height=500)

        viewer.build_chart()