Source code for sylib.library.plugins.data.table.importers.plugin_table_importer

# This file is part of Sympathy for Data.
# Copyright (c) 2013, Combine Control Systems AB
#
# SYMPATHY FOR DATA COMMERCIAL LICENSE
# You should have received a link to the License with Sympathy for Data.
import h5py

from sympathy.api import importers
from sympathy.api import table


def all_equal(iterator):
    try:
        iterator = iter(iterator)
        first = next(iterator)
        return all(first == rest for rest in iterator)
    except StopIteration:
        return True


[docs] class DataImportTable(importers.TableDataImporterBase): """Importer for Table files.""" IMPORTER_NAME = "Table" DISPLAY_NAME = 'SyData' def __init__(self, fq_infilename, parameters): super().__init__(fq_infilename, parameters) def name(self): return self.IMPORTER_NAME def valid_for_file(self): """Is fq_filename a valid Table.""" try: can_open_hdf5 = h5py.is_hdf5(self._fq_infilename) except Exception: can_open_hdf5 = False if can_open_hdf5: if self.is_type(): return True valid = True # Fallback method of determining if the data can be considered a # table by data inspection. Can be useful for reading data from # other programs that have the right form. with h5py.File(self._fq_infilename, 'r') as infile: # Filter reserved items items = [infile[name] for name in infile if not name.startswith('__sy_')] # All remaining items must be datasets. valid &= all([isinstance(ds, h5py.Dataset) for ds in items]) # All datasets must have the same length. valid &= all_equal([len(ds) for ds in items]) return valid return False def is_type(self): if not table.Table.is_type(self._fq_infilename): return False return True def import_data(self, out_file, parameters=None, progress=None): raise self.invalid_file(self._fq_infilename)