Example #1
0
    def save_loader_script(self, event=None, output_directory=None):
        """
        oputput_directory parameter is strictly for use in tests.
        """

        if not self.save_loader_check():
            return

        specutils_dir = os.path.join(os.path.expanduser('~'), '.specutils')

        if not os.path.exists(specutils_dir):
            os.mkdir(specutils_dir)

        loader_name = self.ui.loader_name.text()

        # If the loader name already exists in the registry, raise a warning
        # and ask the user to pick another name
        if loader_name in registry.get_formats(Spectrum1D, 'Read')['Format']:
            QMessageBox.information(
                self, "Loader name already exists.",
                "A loader with the name '{}' already exists in the registry. "
                "Please choose a different name.".format(loader_name))

            return

        out_path = os.path.join(specutils_dir, loader_name)

        filename = compat.getsavefilename(parent=self,
                                          caption='Export loader to .py file',
                                          basedir=out_path)[0]
        if filename == '':
            return

        self.save_register_new_loader(filename)
Example #2
0
    def read_file(self, file_name, file_filter):
        """
        Convenience method that directly reads a spectrum from a file.
        This exists mostly to facilitate development workflow. In time it
        could be augmented to support fancier features such as wildcards,
        file lists, mixed file types, and the like.
        Note that the filter string is hard coded here; its details might
        depend on the intrincacies of the registries, loaders, and data
        classes. In other words, this is brittle code.
        """
        file_name = str(file_name)
        file_ext = os.path.splitext(file_name)[-1]

        if file_filter == 'Auto':
            all_formats = io_registry.get_formats(Spectrum1DRef)['Format']
        else:
            all_formats = [file_filter]

        for format in all_formats:
            try:
                data = Spectrum1DRef.read(file_name, format=format)
                return data
            except:
                logging.error("Incompatible loader for selected data: {"
                              "}".format(file_filter))
Example #3
0
    def _create_loader_filters(self):
        # Create a dictionary mapping the registry loader names to the
        # qt-specified loader names
        def compose_filter_string(reader):
            """
            Generates the Qt loader string to pass to the file load dialog.
            """
            return ' '.join(['*.{}'.format(y) for y in reader.extensions]
                            if reader.extensions is not None else '*')

        loader_name_map = {
            '{} ({})'.format(
                x['Format'], compose_filter_string(
                    get_reader(x['Format'], SpectrumList))): x['Format']
            for x in io_registry.get_formats(SpectrumList) if x['Read'] == 'Yes'}

        # Include an auto load function that lets the io machinery find the
        # most appropriate loader to use
        auto_filter = 'Auto (*)'
        loader_name_map[auto_filter] = None

        filters = list(loader_name_map.keys())
        # Make sure that the "Auto (*)" loader shows up first. Being a bit
        # pedantic about this even though we can probably just rely on
        # dictionary ordering here.
        index = filters.index(auto_filter)
        filters.insert(0, filters.pop(index))

        return filters, loader_name_map
Example #4
0
    def open_file_dialog(self):
        """
        Given a list of filters, prompts the user to select an existing file
        and returns the file path and filter.

        Returns
        -------
        file_name : str
            Path to the selected file.
        selected_filter : str
            The chosen filter (this indicates which custom loader from the
            registry to use).
        """
        dialog = QFileDialog(self)
        dialog.setFileMode(QFileDialog.ExistingFile)
        dialog.setNameFilters(["Auto (*)"] + [
            x + " (*)"
            for x in io_registry.get_formats(Spectrum1DRef)['Format']
        ])

        if dialog.exec_():
            file_names = dialog.selectedFiles()
            selected_filter = dialog.selectedNameFilter().replace(" (*)", "")

            return file_names[0], selected_filter

        return None, None
Example #5
0
    def decorator(func):
        """
        Parameters
        ----------
        func : function
            Function added to the registry in order to read data files.
        """

        logging.info("Added {} to loader registry.".format(label))

        func.loader_wrapper = True
        func.priority = priority

        format = label + " ({})".format(" ".join(extensions))

        if format in io_registry.get_formats()['Format']:
            return

        io_registry.register_reader(format, Spectrum1DRef, func)

        if writer is not None:
            io_registry.register_writer(format, Spectrum1DRef, writer)

        io_registry.register_identifier(format, Spectrum1DRef,
                                        identifier)

        @wraps(func)
        def wrapper(*args, **kwargs):
            return func(*args, **kwargs)
        return wrapper
Example #6
0
    def save_loader_script(self, event=None, output_directory=None):
        """
        oputput_directory parameter is strictly for use in tests.
        """

        if not self.save_loader_check():
            return

        specutils_dir = os.path.join(os.path.expanduser('~'), '.specutils')

        if not os.path.exists(specutils_dir):
            os.mkdir(specutils_dir)

        loader_name = self.ui.loader_name.text()

        # If the loader name already exists in the registry, raise a warning
        # and ask the user to pick another name
        if loader_name in registry.get_formats(Spectrum1D, 'Read')['Format']:
            QMessageBox.information(
                self,
                "Loader name already exists.",
                "A loader with the name '{}' already exists in the registry. "
                "Please choose a different name.".format(loader_name))

            return

        out_path = os.path.join(specutils_dir, loader_name)

        filename = compat.getsavefilename(parent=self,
                                          caption='Export loader to .py file',
                                          basedir=out_path)[0]
        if filename == '':
            return

        self.save_register_new_loader(filename)
Example #7
0
    def __init__(self):
        self.mjdMid = None
        self.mjdSpan = None
        self.tStart = None
        self.tStop = None
        self.ncoeff = None
        self.coeffs = None
        self.obs = None
        self.fileName = None
        self.fileFormat = None
        self.newFileName = None
        self.polycoTable = None
        self.polycoFormat = [
            {
                'format': 'tempo',
                'read_method': tempo_polyco_table_reader,
                'write_method': tempo_polyco_table_writer
            },
        ]

        # Register the table built-in reading and writing format
        for fmt in self.polycoFormat:
            if fmt['format'] not in registry.get_formats()['Format']:
                if fmt['read_method'] != None:
                    registry.register_reader(fmt['format'], table.Table,
                                             fmt['read_method'])

                if fmt['write_method'] != None:
                    registry.register_writer(fmt['format'], table.Table,
                                             fmt['write_method'])
Example #8
0
    def save_register_new_loader(self, filename):
        """
        Save and register new loader file to specutils loader directory.
        If a loader with the current name already exists it will be
        deleted.

        Parameters
        ----------
        filename: str
          Loader filename. If filename does not end in ".py", ".py" will be appended
          to the end of the string.
        """
        filename = "{}.py".format(filename) if not filename.endswith(".py") else filename

        string = self.as_new_loader()

        with open(filename, 'w') as f:
            f.write(string)

        # If a loader by this name exists, delete it
        if self.new_loader_dict['name'] in registry.get_formats()['Format']:
            registry.unregister_reader(self.new_loader_dict['name'], Spectrum1D)
            registry.unregister_identifier(self.new_loader_dict['name'], Spectrum1D)

        # Add new loader to registry
        spec = importlib.util.spec_from_file_location(os.path.basename(filename)[:-3], filename)
        mod = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(mod)

        QMessageBox.information(self,
                                "Loader saved successful.",
                                "Custom loader was saved successfully.")
Example #9
0
    def _on_load_data(self):
        """
        When the user loads a data file, this method is triggered. It provides
        a file open dialog and from the dialog attempts to create a new
        :class:`~specutils.Spectrum1D` object and thereafter adds it to the
        data model.
        """
        # This ensures that users actively have to select a file type before
        # being able to select a file. This should make it harder to
        # accidentally load a file using the wrong type, which results in weird
        # errors.
        default_filter = '-- Select file type --'

        filters = [default_filter] + [x['Format'] + " (*)"
                   for x in io_registry.get_formats(Spectrum1D)
                   if x['Read'] == 'Yes']

        file_path, fmt = compat.getopenfilename(parent=self,
                                                caption="Load spectral data file",
                                                filters=";;".join(filters),
                                                selectedfilter=default_filter)

        if not file_path:
            return

        self.load_data(file_path, file_loader=" ".join(fmt.split()[:-1]))
Example #10
0
    def read_file(self, file_name, file_filter):
        """
        Convenience method that directly reads a spectrum from a file.
        This exists mostly to facilitate development workflow. In time it
        could be augmented to support fancier features such as wildcards,
        file lists, mixed file types, and the like.
        Note that the filter string is hard coded here; its details might
        depend on the intrincacies of the registries, loaders, and data
        classes. In other words, this is brittle code.
        """
        file_name = str(file_name)
        file_ext = os.path.splitext(file_name)[-1]

        if file_filter == 'Auto':
            all_formats = io_registry.get_formats(Spectrum1DRef)['Format']
        else:
            all_formats = [file_filter]

        for format in all_formats:
            try:
                data = Spectrum1DRef.read(file_name, format=format)
                return data
            except:
                logging.error("Incompatible loader for selected data: {"
                              "}".format(file_filter))
Example #11
0
    def _export_layer(self):
        from astropy.io import registry as io_registry

        all_formats = io_registry.get_formats(Spectrum1DRef)['Format'].data
        writable_formats = io_registry.get_formats(Spectrum1DRef)['Write'].data

        write_mask = [True if x == 'Yes' else False for x in writable_formats]
        all_formats = all_formats[np.array(write_mask)]
        all_filters = ";;".join(list(all_formats))

        data = self.current_layer

        path, format = compat.getsavefilename(filters=all_filters)

        if path and format:
            data.write(path, format=format)
Example #12
0
def inherit_table_io(cls):
    """Inherit file I/O registrations from `~astropy.table.Table`

    This decorator is modeled on `~gwpy.table.inherit_io_registrations`,
    authored by Duncan Macleod, for more see https://gwpy.github.io
    """
    for row in registry.get_formats(data_class=Table):
        name = row["Format"]
        # read
        if row["Read"].lower() == "yes":
            registry.register_reader(
                name,
                cls,
                registry.get_reader(name, Table),
                force=False,
            )
        # write
        if row["Write"].lower() == "yes":
            registry.register_writer(
                name,
                cls,
                registry.get_writer(name, Table),
                force=False,
            )
        # identify
        if row["Auto-identify"].lower() == "yes":
            registry.register_identifier(
                name,
                cls,
                registry._identifiers[(name, Table)],
                force=False,
            )
    return cls
Example #13
0
    def _create_loader_filters(self):
        # Create a dictionary mapping the registry loader names to the
        # qt-specified loader names
        def compose_filter_string(reader):
            """
            Generates the Qt loader string to pass to the file load dialog.
            """
            return ' '.join(['*.{}'.format(y)
                             for y in reader.extensions] if reader.
                            extensions is not None else '*')

        loader_name_map = {
            '{} ({})'.format(
                x['Format'],
                compose_filter_string(get_reader(x['Format'], SpectrumList))):
            x['Format']
            for x in io_registry.get_formats(SpectrumList)
            if x['Read'] == 'Yes'
        }

        # Include an auto load function that lets the io machinery find the
        # most appropriate loader to use
        auto_filter = 'Auto (*)'
        loader_name_map[auto_filter] = None

        filters = list(loader_name_map.keys())
        # Make sure that the "Auto (*)" loader shows up first. Being a bit
        # pedantic about this even though we can probably just rely on
        # dictionary ordering here.
        index = filters.index(auto_filter)
        filters.insert(0, filters.pop(index))

        return filters, loader_name_map
Example #14
0
    def save_register_new_loader(self, filename):
        filename = "{}.py".format(
            filename) if not filename.endswith(".py") else filename

        string = self.as_new_loader()

        with open(filename, 'w') as f:
            f.write(string)

        # If a loader by this name exists, delete it
        if self.new_loader_dict['name'] in registry.get_formats()['Format']:
            registry.unregister_reader(self.new_loader_dict['name'],
                                       Spectrum1D)
            registry.unregister_identifier(self.new_loader_dict['name'],
                                           Spectrum1D)

        # Add new loader to registry
        spec = importlib.util.spec_from_file_location(
            os.path.basename(filename)[:-3], filename)
        mod = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(mod)

        message_box = QMessageBox()
        message_box.setText("Loader saved successful.")
        message_box.setIcon(QMessageBox.Information)
        message_box.setInformativeText("Custom loader was saved successfully.")

        message_box.exec()
Example #15
0
def inherit_io_registrations(cls):
    parent = cls.__mro__[1]
    for row in registry.get_formats(data_class=parent):
        name = row["Format"]
        # read
        if row["Read"].lower() == "yes":
            registry.register_reader(
                name,
                cls,
                registry.get_reader(name, parent),
                force=False,
            )
        # write
        if row["Write"].lower() == "yes":
            registry.register_writer(
                name,
                cls,
                registry.get_writer(name, parent),
                force=False,
            )
        # identify
        if row["Auto-identify"].lower() == "yes":
            registry.register_identifier(
                name,
                cls,
                registry._identifiers[(name, parent)],
                force=False,
            )
    return cls
Example #16
0
    def __init__(self):
        self.mjdMid = None
        self.mjdSpan = None
        self.tStart = None
        self.tStop = None
        self.ncoeff = None
        self.coeffs = None
        self.obs = None
        self.fileName = None
        self.fileFormat = None
        self.newFileName = None
        self.polycoTable = None
        self.polycoFormat = [{'format': 'tempo',
                            'read_method' : tempo_polyco_table_reader,
                            'write_method' : tempo_polyco_table_writer},]

        # Register the table built-in reading and writing format
        for fmt in self.polycoFormat:
            if fmt['format'] not in registry.get_formats()['Format']:
                if fmt['read_method'] != None:
                    registry.register_reader(fmt['format'], table.Table,
                                             fmt['read_method'])

                if fmt['write_method'] != None:
                    registry.register_writer(fmt['format'], table.Table,
                                            fmt['write_method'])
Example #17
0
    def __init__(self):
        self.mjdMid = None
        self.mjdSpan = None
        self.tStart = None
        self.tStop = None
        self.ncoeff = None
        self.coeffs = None
        self.obs = None
        self.fileName = None
        self.fileFormat = None
        self.newFileName = None
        self.polycoTable = None
        self.polycoFormat = [{
            "format": "tempo",
            "read_method": tempo_polyco_table_reader,
            "write_method": tempo_polyco_table_writer,
        }]

        # Register the table built-in reading and writing format
        for fmt in self.polycoFormat:
            if fmt["format"] not in registry.get_formats()["Format"]:
                if fmt["read_method"] is not None:
                    registry.register_reader(fmt["format"], table.Table,
                                             fmt["read_method"])

                if fmt["write_method"] is not None:
                    registry.register_writer(fmt["format"], table.Table,
                                             fmt["write_method"])
Example #18
0
    def open_file_dialog(self):
        """
        Given a list of filters, prompts the user to select an existing file
        and returns the file path and filter.

        Returns
        -------
        file_name : str
            Path to the selected file.
        selected_filter : str
            The chosen filter (this indicates which custom loader from the
            registry to use).
        """

        filters = ["Auto (*)"] + [x for x in
                                  io_registry.get_formats(
                                      Spectrum1DRef)['Format']]

        file_names, self._file_filter = compat.getopenfilenames(basedir=self._directory,
                                                                filters=";;".join(filters),
                                                                selectedfilter=self._file_filter)

        if len(file_names) == 0:
            return None, None

        self._directory = file_names[0]

        return file_names[0], self._file_filter
Example #19
0
def inherit_io_registrations(cls):
    parent = cls.__mro__[1]
    for row in registry.get_formats(data_class=parent):
        name = row["Format"]
        # read
        if row["Read"].lower() == "yes":
            registry.register_reader(
                name,
                cls,
                registry.get_reader(name, parent),
                force=False,
            )
        # write
        if row["Write"].lower() == "yes":
            registry.register_writer(
                name,
                cls,
                registry.get_writer(name, parent),
                force=False,
            )
        # identify
        if row["Auto-identify"].lower() == "yes":
            registry.register_identifier(
                name,
                cls,
                registry._identifiers[(name, parent)],
                force=False,
            )
    return cls
    def open_file_dialog(self):
        """
        Given a list of filters, prompts the user to select an existing file
        and returns the file path and filter.

        Returns
        -------
        file_name : str
            Path to the selected file.
        selected_filter : str
            The chosen filter (this indicates which custom loader from the
            registry to use).
        """
        dialog = QFileDialog(self)
        dialog.setFileMode(QFileDialog.ExistingFile)
        dialog.setNameFilters(["Auto (*)"] + [x + " (*)" for x in
                               io_registry.get_formats(Spectrum1DRef)[
                                   'Format']])

        if dialog.exec_():
            file_names = dialog.selectedFiles()
            selected_filter = dialog.selectedNameFilter().replace(" (*)", "")

            return file_names[0], selected_filter

        return None, None
Example #21
0
    def save_register_new_loader(self, filename):
        """
        Save and register new loader file to specutils loader directory.
        If a loader with the current name already exists it will be
        deleted.

        Parameters
        ----------
        filename: str
          Loader filename. If filename does not end in ".py", ".py" will be appended
          to the end of the string.
        """
        filename = "{}.py".format(
            filename) if not filename.endswith(".py") else filename

        string = self.as_new_loader()

        with open(filename, 'w') as f:
            f.write(string)

        # If a loader by this name exists, delete it
        if self.new_loader_dict['name'] in registry.get_formats()['Format']:
            registry.unregister_reader(self.new_loader_dict['name'],
                                       Spectrum1D)
            registry.unregister_identifier(self.new_loader_dict['name'],
                                           Spectrum1D)

        # Add new loader to registry
        spec = importlib.util.spec_from_file_location(
            os.path.basename(filename)[:-3], filename)
        mod = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(mod)

        QMessageBox.information(self, "Loader saved successful.",
                                "Custom loader was saved successfully.")
Example #22
0
    def read_file(self, file_name, file_filter):
        """
        Convenience method that directly reads a spectrum from a file.

        Parameters
        ----------
        file_name: str
            Name of the file to read.

        file_filter: str
            Type of file to read. If `Auto`, try all known formats.

        Returns
        -------
        data: Spectrum1DRef
            The file's data or None if no known formats are found.

        Notes
        -----
        This exists mostly to facilitate development workflow. In time it
        could be augmented to support fancier features such as wildcards,
        file lists, mixed file types, and the like.
        Note that the filter string is hard coded here; its details might
        depend on the intrincacies of the registries, loaders, and data
        classes. In other words, this is brittle code.
        """
        file_filter = 'Auto (*)' if file_filter is None else file_filter

        logging.info("Attempting to read file {} with {}.".format(
            file_name, file_filter))

        if not (file_name and file_filter):
            return

        file_name = str(file_name)
        file_ext = os.path.splitext(file_name)[-1]
        all_formats = io_registry.get_formats(Spectrum1DRef)['Format']

        if file_filter == 'Auto (*)':
            #-- sort loaders by priorty given in the definition
            all_priority = [
                getattr(io_registry.get_reader(fmt, Spectrum1DRef), 'priority',
                        0) for fmt in all_formats
            ]
            all_registry = sorted(zip(all_formats, all_priority),
                                  key=lambda item: item[1],
                                  reverse=True)
            all_formats = [item[0] for item in all_registry]
        else:
            all_formats = [x for x in all_formats if file_filter in x]

        for format in all_formats:
            logging.info("Trying to load with {}".format(format))
            try:
                data = Spectrum1DRef.read(file_name, format=format)
                return data
            except Exception as e:
                logging.error("Incompatible loader for selected data: {"
                              "} because {}".format(file_filter, e))
Example #23
0
    def add_polyco_file_format(self, formatName, methodMood, readMethod = None,
                                writeMethod = None):
        """
        Add a polyco file format and its reading/writing method to the class.
        Then register it to the table reading.

        Parameters
        ---------
        formatName : str
            The name for the format.
        methodMood : str
            ['r','w','rw']. 'r'  represent as reading
                            'w'  represent as writting
                            'rw' represent as reading and writting
        readMethod : method
            The method for reading the file format.
        writeMethod : method
            The method for writting the file to disk.

        """
        # Check if the format already exist.
        if (formatName in [f['format'] for f in self.polycoFormat]
            or formatName in registry.get_formats()['Format']):
            errorMssg = 'Format name \''+formatName+ '\' is already exist. '
            raise Exception(errorMssg)

        pFormat = {'format' : formatName}

        if methodMood == 'r':
            if readMethod == None:
                raise BaseException('Argument readMethod should not be \'None\'.')

            pFormat['read_method'] = readMethod
            pFormat['write_method'] = writeMethod
            registry.register_reader(pFormat['format'], table.Table,
                                    pFormat['read_method'])
        elif methodMood == 'w':
            if writeMethod == None:
                raise BaseException('Argument writeMethod should not be \'None\'.')

            pFormat['read_method'] = readMethod
            pFormat['write_method'] = writeMethod
            registry.register_writer(pFormat['format'], table.Table,
                                    pFormat['write_method'])
        elif methodMood == 'rw':
            if readMethod == None or writeMethod == None:
                raise BaseException('Argument readMethod and writeMethod '
                                    'should not be \'None\'.')

            pFormat['read_method'] = readMethod
            pFormat['write_method'] = writeMethod

            registry.register_reader(pFormat['format'], table.Table,
                                    pFormat['read_method'])
            registry.register_writer(pFormat['format'], table.Table,
                                    pFormat['write_method'])

        self.polycoFormat.append(pFormat)
Example #24
0
    def add_polyco_file_format(self, formatName, methodMood, readMethod = None,
                                writeMethod = None):
        """
        Add a polyco file format and its reading/writing method to the class.
        Then register it to the table reading.

        Parameters
        ---------
        formatName : str
            The name for the format.
        methodMood : str
            ['r','w','rw']. 'r'  represent as reading
                            'w'  represent as writting
                            'rw' represent as reading and writting
        readMethod : method
            The method for reading the file format.
        writeMethod : method
            The method for writting the file to disk.

        """
        # Check if the format already exist.
        if (formatName in [f['format'] for f in self.polycoFormat]
            or formatName in registry.get_formats()['Format']):
            errorMssg = 'Format name \''+formatName+ '\' is already exist. '
            raise Exception(errorMssg)

        pFormat = {'format' : formatName}

        if methodMood == 'r':
            if readMethod == None:
                raise BaseException('Argument readMethod should not be \'None\'.')

            pFormat['read_method'] = readMethod
            pFormat['write_method'] = writeMethod
            registry.register_reader(pFormat['format'], table.Table,
                                    pFormat['read_method'])
        elif methodMood == 'w':
            if writeMethod == None:
                raise BaseException('Argument writeMethod should not be \'None\'.')

            pFormat['read_method'] = readMethod
            pFormat['write_method'] = writeMethod
            registry.register_writer(pFormat['format'], table.Table,
                                    pFormat['write_method'])
        elif methodMood == 'rw':
            if readMethod == None or writeMethod == None:
                raise BaseException('Argument readMethod and writeMethod '
                                    'should not be \'None\'.')

            pFormat['read_method'] = readMethod
            pFormat['write_method'] = writeMethod

            registry.register_reader(pFormat['format'], table.Table,
                                    pFormat['read_method'])
            registry.register_writer(pFormat['format'], table.Table,
                                    pFormat['write_method'])

        self.polycoFormat.append(pFormat)
Example #25
0
 def write_(*args, **kwargs):
     for fmt in get_formats(data_class=container, readwrite='Write'):
         if fmt['Format'].startswith('gwf.'):
             kwargs['format'] = fmt['Format']
             try:
                 return container.write(*args, **kwargs)
             except ImportError:
                 continue
     raise ImportError("No GWF API available with which to write these "
                       "data. Please install one of the third-party GWF "
                       "libraries and try again")
Example #26
0
def list_of_formats():
    """
    Get the list of astropy formats and return it

    :return ftable: astropy.table.Table instance containing the formats allow
                    for reading and writing astropy tables
    """
    ftable = get_formats(Table)

    ftable['read?'] = ftable['Read'] == 'Yes'
    ftable['write?'] = ftable['Write'] == 'Yes'

    # return format table
    return ftable
Example #27
0
    def decorator(func):
        logging.info("Added {} to writer registry.".format(label))

        func.loader_wrapper = True

        loaders = io_registry.get_formats(Spectrum1DRef)['Format'].data
        format = [x for x in loaders if label in x]

        format = label if len(format) == 0 else format[0]

        io_registry.register_writer(format, Spectrum1DRef, func)

        @wraps(func)
        def wrapper(*args, **kwargs):
            return func(*args, **kwargs)
        return wrapper
Example #28
0
    def _on_load_data(self):
        """
        When the user loads a data file, this method is triggered. It provides
        a file open dialog and from the dialog attempts to create a new
        :class:`~specutils.SpectrumList` object and thereafter adds the
        contents to the data model.
        """

        # Create a dictionary mapping the registry loader names to the
        # qt-specified loader names
        def compose_filter_string(reader):
            return ' '.join(['*.{}'.format(y)
                             for y in reader.extensions] if reader.
                            extensions is not None else '*')

        loader_name_map = {
            '{} ({})'.format(
                x['Format'],
                compose_filter_string(get_reader(x['Format'], SpectrumList))):
            x['Format']
            for x in io_registry.get_formats(SpectrumList)
            if x['Read'] == 'Yes'
        }

        # Include an auto load function that lets the io machinery find the
        # most appropriate loader to use
        loader_name_map['Auto (*)'] = None

        # This ensures that users actively have to select a file type before
        # being able to select a file. This should make it harder to
        # accidentally load a file using the wrong type, which results in weird
        # errors.
        filters = ['Select loader...'] + list(loader_name_map.keys())

        file_path, fmt = compat.getopenfilename(
            parent=self,
            basedir=os.getcwd(),
            caption="Load spectral data file",
            filters=";;".join(filters))

        if not file_path:
            return

        self.load_data(file_path, file_loader=loader_name_map[fmt])
Example #29
0
    def _on_load_data(self):
        """
        When the user loads a data file, this method is triggered. It provides
        a file open dialog and from the dialog attempts to create a new
        :class:`~specutils.Spectrum1D` object and thereafter adds it to the
        data model.
        """
        filters = [
            x + " (*)" for x in io_registry.get_formats(Spectrum1D)['Format']
        ]

        file_path, fmt = compat.getopenfilename(
            parent=self,
            caption="Load spectral data file",
            filters=";;".join(filters))

        if not file_path:
            return

        self.load_data(file_path, file_loader=fmt.split()[0])
Example #30
0
from simple_plot import plotData, timeString
from werkzeug import secure_filename
import difflib

UPLOAD_FOLDER = 'uploads/'
ALLOWED_EXTENSIONS = set(['fits', 'csv', 'txt', 'ipac', 'dat', 'tsv'])
valid_column_names = [
    'Ignore', 'IDs', 'SurfaceDensity', 'VelocityDispersion', 'Radius',
    'IsSimulated', 'Username'
]
use_column_names = ['SurfaceDensity', 'VelocityDispersion', 'Radius']
use_units = ['Msun/pc^2', 'km/s', 'pc']

from astropy.io import registry
from astropy.table import Table
table_formats = registry.get_formats(Table)

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

# Allow zipping in jinja templates: http://stackoverflow.com/questions/5208252/ziplist1-list2-in-jinja2
import jinja2
env = jinja2.Environment()
env.globals.update(zip=zip)


# http://stackoverflow.com/questions/21306134/iterating-over-multiple-lists-in-python-flask-jinja2-templates
@app.template_global(name='zip')
def _zip(*args, **kwargs):  #to not overwrite builtin zip in globals
    """ This function allows the use of "zip" in jinja2 templates """
    return __builtins__.zip(*args, **kwargs)
Example #31
0
    data_class=EventTable,
    columns=True,
    selection=True,
):
    """Wrap an existing registered reader to use GWpy's input decorators

    Parameters
    ----------
    name : `str`
        the name of the registered format

    data_class : `type`, optional
        the class for whom the format is registered

    columns : `bool`, optional
        use the `read_with_columns` decorator

    selection : `bool`, optional
        use the `read_with_selection` decorator
    """
    reader = registry.get_reader(name, data_class)
    wrapped = (
        read_with_columns(  # use ``columns``
            read_with_selection(  # use ``selection``
                reader), ))
    return registry.register_reader(name, data_class, wrapped, force=True)


for row in registry.get_formats(data_class=EventTable, readwrite="Read"):
    decorate_registered_reader(row["Format"], data_class=EventTable)
Example #32
0
 def data_loader_formats(self):
     return io_registry.get_formats(Spectrum1D)['Format']
Example #33
0
def test_speclist_autoidentify():

    formats = registry.get_formats(SpectrumList)
    assert (formats['Auto-identify'] == 'Yes').all()
Example #34
0
def test_speclist_autoidentify():

    formats = registry.get_formats(SpectrumList)
    assert (formats['Auto-identify'] == 'Yes').all()
Example #35
0
    def add_polyco_file_format(self,
                               formatName,
                               methodMood,
                               readMethod=None,
                               writeMethod=None):
        """
        Add a polyco file format and its reading/writing method to the class.
        Then register it to the table reading.

        Parameters
        ---------
        formatName : str
            The name for the format.
        methodMood : str
            ['r','w','rw']. 'r'  represent as reading
                            'w'  represent as writting
                            'rw' represent as reading and writting
        readMethod : method
            The method for reading the file format.
        writeMethod : method
            The method for writting the file to disk.

        """
        # Check if the format already exist.
        if (formatName in [f["format"] for f in self.polycoFormat]
                or formatName in registry.get_formats()["Format"]):
            errorMssg = "Format name '" + formatName + "' is already exist. "
            raise ValueError(errorMssg)

        pFormat = {"format": formatName}

        if methodMood == "r":
            if readMethod is None:
                raise ValueError("Argument readMethod should not be 'None'.")

            pFormat["read_method"] = readMethod
            pFormat["write_method"] = writeMethod
            registry.register_reader(pFormat["format"], table.Table,
                                     pFormat["read_method"])
        elif methodMood == "w":
            if writeMethod is None:
                raise ValueError("Argument writeMethod should not be 'None'.")

            pFormat["read_method"] = readMethod
            pFormat["write_method"] = writeMethod
            registry.register_writer(pFormat["format"], table.Table,
                                     pFormat["write_method"])
        elif methodMood == "rw":
            if readMethod is None or writeMethod is None:
                raise ValueError("Argument readMethod and writeMethod "
                                 "should not be 'None'.")

            pFormat["read_method"] = readMethod
            pFormat["write_method"] = writeMethod

            registry.register_reader(pFormat["format"], table.Table,
                                     pFormat["read_method"])
            registry.register_writer(pFormat["format"], table.Table,
                                     pFormat["write_method"])

        self.polycoFormat.append(pFormat)
Example #36
0
        selection=True,
):
    """Wrap an existing registered reader to use GWpy's input decorators

    Parameters
    ----------
    name : `str`
        the name of the registered format

    data_class : `type`, optional
        the class for whom the format is registered

    columns : `bool`, optional
        use the `read_with_columns` decorator

    selection : `bool`, optional
        use the `read_with_selection` decorator
    """
    reader = registry.get_reader(name, data_class)
    wrapped = (  # noqa
        read_with_columns(  # use ``columns``
        read_with_selection(  # use ``selection``
            reader
        ))
    )
    return registry.register_reader(name, data_class, wrapped, force=True)


for row in registry.get_formats(data_class=EventTable, readwrite="Read"):
    decorate_registered_reader(row["Format"], data_class=EventTable)
Example #37
0
def table_writing_formats():
    t = registry.get_formats(table.Table, readwrite="Write")
    return {fmt: "" for fmt, dep in t["Format", "Deprecated"] if dep != "Yes"}
Example #38
0
FigureStrBase='Output_Sigma_sigma_r_'
TableStrBase='Output_Table_'
TooOld=300

import glob
import random
import time
import datetime
from datetime import datetime
import matplotlib
import matplotlib.pylab as plt
from astropy.table import vstack

from astropy.io import registry
from astropy.table import Table
table_formats = registry.get_formats(Table)

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MPLD3_FOLDER'] = MPLD3_FOLDER
app.config['DATABASE_FOLDER'] = DATABASE_FOLDER
app.config['PNG_PLOT_FOLDER'] = PNG_PLOT_FOLDER
app.config['TABLE_FOLDER'] = TABLE_FOLDER

for path in (UPLOAD_FOLDER, MPLD3_FOLDER, DATABASE_FOLDER, PNG_PLOT_FOLDER, TABLE_FOLDER):
    if not os.path.isdir(path):
        os.mkdir(path)

# En

# Allow zipping in jinja templates: http://stackoverflow.com/questions/5208252/ziplist1-list2-in-jinja2
Example #39
0
    def load_data(self, file_path, file_loader=None, display=False):
        """
        Load spectral data given file path and loader.

        Parameters
        ----------
        file_path : str
            Path to location of the spectrum file.
        file_loader : str
            Format specified for the astropy io interface.
        display : bool
            Automatically add the loaded spectral data to the plot.

        Returns
        -------
        : :class:`~specviz.core.items.DataItem`
            The `DataItem` instance that has been added to the internal model.
        """
        # In the case that the user has selected auto load, loop through every
        # available loader and choose the one that 1) the registry identifier
        # function allows, and 2) is the highest priority.
        try:
            try:
                speclist = SpectrumList.read(file_path, format=file_loader)
            except IORegistryError as e:
                # In this case, assume that the registry has found several
                # loaders that fit the same identifier, choose the highest
                # priority one.
                fmts = io_registry.get_formats(Spectrum1D, 'Read')['Format']

                logging.warning(
                    "Loaders for '%s' matched for this data set. "
                    "Iterating based on priority."
                    "", ', '.join(fmts))

                for fmt in fmts:
                    try:
                        speclist = SpectrumList.read(file_path, format=fmt)
                    except:
                        logging.warning(
                            "Attempted load with '%s' failed, "
                            "trying next loader.", fmt)

            name = file_path.split('/')[-1].split('.')[0]

            data_items = []

            if len(speclist) == 1:
                data_items.append(self._add_and_plot_data(speclist[0], name))
            else:
                for i, spec in enumerate(speclist):
                    # TODO: try to use more informative metadata in the name
                    specname = '{}-{}'.format(name, i)
                    data_items.append(self._add_and_plot_data(spec, specname))

            for di in data_items:
                self.force_plot(di)

            # TODO: is this return value useful? Potentially just for testing
            return data_items

        except:
            message_box = QMessageBox()
            message_box.setText("Error loading data set.")
            message_box.setIcon(QMessageBox.Critical)
            message_box.setInformativeText("{}\n{}".format(
                sys.exc_info()[0],
                sys.exc_info()[1]))

            message_box.exec()