Exemple #1
0
def toJSON(args):

    spectrum_file = args.input
    spectrum_data = read_spectra_file_simple(spectrum_file)
    spectrum = SimpleSpectrum('fits2JSON', spectrum_data)
    asjson = utils.toMarzJSON(spectrum)
    print(json.dumps(asjson, indent=2), flush=True)
Exemple #2
0
    def reduce(self, argument):
        """Implementation of the Marz CLI

        Parameters
        ----------
        argument : str
            Path to the FITS file containing the spectrum to be fit

        Returns
        -------
        Float
            The fitted redshift of the input spectrum
        String
            The name of the fitted template of the input spectrum
        """

        import os
        from subprocess import run, Popen, PIPE, STDOUT

        spectrum = SimpleSpectrum('fits2JSON', argument)
        spectrum_json = json.dumps(utils.toMarzJSON(spectrum), indent=2)

        plugin_dir = os.path.dirname(os.path.abspath(__file__))

        marzcli_path = plugin_dir + '/marzcli.js'
        cmd = plugin_dir + '/marzcli.sh ' + marzcli_path

        if sys.version_info[:3] >= (3, 7):
            proc = run(cmd,
                       shell=True,
                       input=spectrum_json,
                       text=True,
                       stdout=PIPE,
                       stderr=STDOUT,
                       close_fds=True)
        else:
            proc = run(cmd,
                       shell=True,
                       input=spectrum_json,
                       universal_newlines=True,
                       stdout=PIPE,
                       stderr=STDOUT,
                       close_fds=True)

        lines = proc.stdout.split('\n')

        print("=" * 20 + " Marz CLI Plugin " + "=" * 20)
        for line in lines[:-6:-1]:
            print(line)

        tokens = lines[-5].split(',')
        bestfit_redshift = float(tokens[8])
        bestfit_template = tokens[7].strip()
        print(f"BEST REDSHIFT = {bestfit_redshift}")
        print(f"BEST TEMPLATE = {bestfit_template}")
        return bestfit_redshift, bestfit_template
Exemple #3
0
 def test_fits2json_2(self, shared_datadir):
     from ssv.viewer import read_spectra_file, read_spectra_file_simple, SimpleSpectrum
     from ssv import utils
     spectrum_file = shared_datadir / "marz/spec-4444-55538-1000.fits"
     formats = loaders.whatformat(spectrum_file)
     if len(formats) > 1:
         loaders.unregister(formats[0])
     spectrum_data = read_spectra_file_simple(spectrum_file)
     loaders.restore_registered_loaders()
     spectrum = SimpleSpectrum('fits2JSON', spectrum_data)
     asjson = utils.toMarzJSON(spectrum)
Exemple #4
0
 def test_fits2json_1(self, shared_datadir):
     from ssv.viewer import SimpleSpectrum
     from ssv import utils
     spectrum_file = shared_datadir / "marz/emlLinearVacuumNoHelio.fits"
     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()
     spectrum = SimpleSpectrum('fits2JSON', spectrum_data)
     asjson = utils.toMarzJSON(spectrum)
Exemple #5
0
def jsonFitsFile(request, fits_id):
    fitsfile = FitsFiles.objects.get(pk=fits_id)
    #
    datadir = Path('../../../')

    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()
    spectrum = SimpleSpectrum('Test SSV', spectrum_data)
    js_data = utils.toMarzJSON(spectrum)
    return JsonResponse(js_data, safe=False)
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)
Exemple #7
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)
Exemple #8
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()