def new_dataset(spj, element): ncols = int(element.attrib.pop('ncols',0)) typecodes = element.attrib.pop('typecodes','') ds = Dataset(**element.attrib) # metadata for eMetaitem in element.findall('Metadata/Metaitem'): key = eMetaitem.attrib['key'] value = eMetaitem.text ds.metadata[key] = unicode(value) # actual Table if element.tag == 'Table': # Extract additional column information. # This information will be passed on to 'set_table_import', # which will pass it on to the internal importer. column_props = list() for i in range(ncols): column_props.append(dict()) for eColumn in element.findall('Column'): n = int(eColumn.get('n')) p = column_props[n] for eInfo in eColumn.findall('Info'): key = eInfo.get('key', None) if key is not None: p[key] = unicode(eInfo.text) filename = os.path.join('datasets', dataset_filename(ds.key)) # TODO: replace DEFAULT_FF with read value ds.set_table_import(spj, filename, typecodes, column_props, DEFAULT_FF) return ds
def new_dataset(self, key='dataset', undolist=None): """ Add a new Dataset object to the Project. The `data` field contains a nearly empty numarray (1 row, 2 columns, all zero). If no key is given, then one is created. If the key already exists, then the method assures that it is unique within the Project. Returns newly created Dataset. """ if undolist is None: undolist = self.journal key = pdict.unique_key(self.datasets, key) ds = Dataset() pdict.setitem(self.datasets, key, ds) ds.data = Table(nrows=1,ncols=2) ds.data.column(0).designation = 'X' ds.data.column(1).designation = 'Y' self.sig_emit("notify::datasets") ui = UndoInfo(self.remove_objects, [ds], False) ui.describe("Create new Dataset '%s'" % key) undolist.append(ui) return ds
def add_experimental_plot(self, project, undolist=None): if undolist is None: undolist = project.journal ul = UndoList().describe("Experimental Plot") ds = Dataset() ds.key = pdict.unique_key(project.datasets, "exp_ds") ds.data = Table(ncols=2, nrows=5) ds.data[0] = [1,2,3,4,5] ds.data[1] = [1,4,9,16,25] ds2 = Dataset() ds2.key = pdict.unique_key(project.datasets, "exp_ds2") ds2.data = Table(ncols=2, nrows=4) ds2.data[0] = [10,17,3,8] ds2.data[1] = [1,89,48,1] plot = Plot() plot.key = pdict.unique_key(project.plots, "exp_plot") layer1 = Layer(type="line2d", lines=[Line(source=ds,cx=0,cy=1), Line(source=ds2,cx=0,cy=1)], x=0.0, y=0.0, width=1.0, height=0.5) layer2 = Layer(type="line2d", lines=[Line(source=ds2,cx=0,cy=1)], x=0.0, y=0.5, width=1.0, height=0.5) plot.layers = [layer1, layer2] project.add_datasets([ds,ds2], undolist=ul) project.add_plot(plot, undolist=ul) undolist.append(ul)
def import_datasets(self, filenames, importer, progresslist=None, undolist=None): if undolist is None: undolist = self.journal if isinstance(importer, basestring): importer = ImporterRegistry.new_instance(importer) elif not isinstance(importer, Importer): raise TypeError("'importer' needs to be a key or a valid Importer instance.") # To ensure a proper undo, the Datasets are imported one by one # to a temporary dict. When finished, they are added as a whole. new_datasets = list() progresslist = progresslist or self.app.progresslist pl = progresslist(filenames) for filename in pl: try: tbl = importer.read_table_from_file(filename) except ImportError, msg: pl.fail(msg) continue ds = Dataset(key=basename(filename), data=tbl) ds.metadata["Import-Source"] = unicode(filename) ds.metadata["Import-Filter"] = unicode(importer.blurb) new_datasets.append(ds) pl.succeed()
def new_dataset(spj, element): ds = Dataset(**element.attrib) # metadata for eMetaitem in element.findall('Metadata/Metaitem'): key = eMetaitem.attrib['key'] value = eMetaitem.text ds.metadata[key] = unicode(value) # actual Table if element.tag == 'Table': ### extract metadata special to Table objects typecodes = element.get('typecodes', '') # fill columns, if information is available columns = list() for tc in typecodes: columns.append(Column(data=array((),tc))) for eColumn in element.findall('Column'): n = int(eColumn.get('n')) column = columns[n] for eInfo in eColumn.findall('Info'): key = eInfo.get('key', None) if key is not None: column.set_value(key, eInfo.text) filename = os.path.join('datasets', dataset_filename(ds.key)) # TODO: replace DEFAULT_FF with read value ds.set_table_import(spj, filename, typecodes, columns, DEFAULT_FF) return ds
def quick_xps_import(self, filename, ranges, undolist=None): project = self.app.project if undolist is None: undolist = project.journal importer = ImporterRegistry.new_instance('XPS') table = importer.read_table_from_file(filename, ranges=ranges) ds = Dataset(key=basename(filename), data=table) ds.metadata['Import-Source'] = unicode(filename) ds.metadata['Import-Filter'] = unicode('XPS') ul = UndoList().describe("Quick import Dataset(s)") project.add_datasets([ds], undolist=ul) undolist.append(ul)
def demo_zno(): ds = Dataset(key="ZnO-10-Abs1") ds.data = read_table_from_file("../../../Examples/Data/zn10abs1.abs", "ASCII", delimiter="\t") tbl = ds.data tbl.column(0).set_values("key", "Wavelength", "label", "Wavelength (nm)") tbl.column(1).set_values(key="Absorption", designation="Y", label="Optical Absorption (arb. units)") layer = Layer( type="line2d", lines=[Line(source=ds)], axes={"x": Axis(label="Wavelength [nm]"), "y": Axis(label="Absorption [a.u.]")}, ) pl = Plot(label=u"Optical Absorption of ZnO Quantum Dots", layers=[layer], key=ds.key) spj = Project(plots=[pl], datasets=[ds]) save_project(spj, "zno.spj")
def add_experimental_plot(self, project, undolist=None): if undolist is None: undolist = project.journal ul = UndoList().describe("Experimental Plot") ds = Dataset() ds.key = pdict.unique_key(project.datasets, "exp_ds") ds.data = Table(colcount=2, rowcount=5) ds.data[0] = [1,2,3,4,5] ds.data[1] = [1,4,9,16,25] ds2 = Dataset() ds2.key = pdict.unique_key(project.datasets, "exp_ds2") ds2.data = Table(colcount=2, rowcount=4) ds2.data[0] = [10,17,3,8] ds2.data[1] = [1,89,48,1] ulist.append( project.datasets, ds, undolist=ul ) ulist.append( project.datasets, ds2, undolist=ul ) plot = Plot() plot.key = pdict.unique_key(project.plots, "exp_plot") layer1 = Layer(type="line2d", lines=[Line(source=ds,cx=0,cy=1), Line(source=ds2)], x=0.0, y=0.0, width=1.0, height=0.5) layer2 = Layer(type="line2d", lines=[Line(source=ds2,cx=0,cy=1)], x=0.0, y=0.5, width=1.0, height=0.5) plot.layers = [layer1, layer2] # plot.layers.arrange(rowcount=1, colcount=2) ulist.append( project.plots, plot, undolist=ul ) uwrap.emit_last(project.datasets, "changed") undolist.append(ul)
N = len(filenames) for filename in filenames: yield ("Importing %s" % filename, n/N) try: tbl = importer.read_table_from_file(filename) except ImportError, msg: self.error_message(msg) continue except error.UserCancel: self.error_message("Import aborted by user") continue root, ext = os.path.splitext(os.path.basename(filename)) filename = utils.encode_as_key(root) ds = Dataset(key=filename, data=tbl) ds.metadata['Import-Source'] = unicode(filename) ds.metadata['Import-Filter'] = unicode(importer.blurb) new_datasets.append(ds) n+=1 yield (None,n/N) yield (-1,None) if len(new_datasets) > 0: ul = UndoList().describe("Import Dataset(s)") project.add_datasets(new_datasets, undolist=ul) undolist.append(ul) else: