def setUpClass(self): """ Setup the file where all the tests are performed. """ self.gf = create_file(self.full_path,overwrite=True) self.gf.root().create_field("data",self._typecode) self.gf.close()
def setUpClass(self): self.file_name = "mdim_io_test_non_array_on_field_{tc}.nxs".format(tc=self._typecode) self.full_path = os.path.join(self.file_path,self.file_name) self.gf = create_file(self.full_path,overwrite=True) self.gf.root().create_field("data",self._typecode) self.gf.close()
def test_truncating(self): """ Test truncating file creation. If a file already exists the new file will overwrite the existing one. """ f = create_file(self.filename) f.close() t1 = os.path.getmtime(self.filename) time.sleep(1) f = create_file(self.filename,overwrite=True) self.assertTrue(f.is_valid) self.assertFalse(f.readonly) f.close() t2 = os.path.getmtime(self.filename) self.assertGreater(t2,t1)
def setUp(self): self.nexus_file = create_file(self.full_path,overwrite=True) self.root = self.nexus_file.root() e = self.root.create_group("entry:NXentry") i = e.create_group("instrument:NXinstrument") e.create_group("data:NXdata") e.create_group("sample:NXsample") e.create_group("control:NXmonitor")
def test_unresolvable_link(self): f = nexus.create_file("unresolvable_link.nxs",overwrite=True) r = f.root() nexus.xml_to_nexus(dead_link_file,r,lambda x: True) r.close() f.close() result = self._check_output(args=["unresolvable_link.nxs"]) print(result)
def setUp(self): f = nexus.create_file(self.det_full_path,True) r = f.root() nexus.xml_to_nexus(detector_file_struct,r) r.close() f.close() f = nexus.create_file(self.master1_full_path,True) r = f.root() nexus.xml_to_nexus(master_file_struct_1,r) r.close() f.close() f = nexus.create_file(self.master2_full_path,True) r = f.root() nexus.xml_to_nexus(master_file_struct_2,r) r.close() f.close()
def create_file(filename, overwrite=False, libver=None): """ create a new file :param filename: file name :type filename: :obj:`str` :param overwrite: overwrite flag :type overwrite: :obj:`bool` :param libver: library version: 'lastest' or 'earliest' :type libver: :obj:`str` :returns: file object :rtype: :class:`PNIFile` """ return PNIFile(nx.create_file(filename, overwrite), filename)
def main(self): import pni.io.nx.h5 as nx import numpy f = nx.create_file("test.h5", True, 0) g = f.create_field("test", "int32", [1000, 2048], [1, 4]) buf = numpy.zeros(shape=(1000, 2048), dtype="int32") print "buf", buf.shape for i in range(len(buf.shape)): if buf.shape[i] > g.shape[i]: g.grow(i, buf.shape[i] - g.shape[i]) g.write(buf) g.close() f.close()
def setUp(self): self.nexus_file = create_file(self.full_path,overwrite=True) self.root = self.nexus_file.root() self.paths = [] e = self.root.create_group("entry:NXentry") self.paths.append(e.path) i = e.create_group("instrument:NXinstrument") self.paths.append(i.path) self.paths.append(i.create_group("detector:NXdetector").path) self.paths.append(i.create_group("monochromator:NXmonochromator").path) self.paths.append(i.create_group("source:NXsource").path) self.paths.append(e.create_group("data:NXdata").path) self.paths.append(e.create_group("sample:NXsample").path) self.paths.append(e.create_group("control:NXmonitor").path) self.paths.append(e.create_field("title","string").path) self.paths.append(e.create_field("experiment_identifier","string").path) self.paths.append(e.create_field("experiment_description","string").path)
def setUp(self): self.gf = create_file(self.full_path,overwrite=True) self.field = self.gf.root().create_field("data","string") self.field.attributes.create("attr_uint8","uint8") self.field.attributes.create("attr_int8","int8") self.field.attributes.create("attr_uint16","uint16") self.field.attributes.create("attr_int16","int16") self.field.attributes.create("attr_uint32","uint32") self.field.attributes.create("attr_int32","int32") self.field.attributes.create("attr_uint64","uint64") self.field.attributes.create("attr_int64","int64") self.field.attributes.create("attr_float32","float32") self.field.attributes.create("attr_float64","float64") self.field.attributes.create("attr_float128","float128") self.field.attributes.create("attr_complex32","complex32") self.field.attributes.create("attr_complex64","complex64") self.field.attributes.create("attr_complex128","complex128") self.field.attributes.create("attr_string","string") self.field.attributes.create("attr_bool","bool")
def test_non_truncating(self): """ Test non-truncating file creation. If a file already exists a subsequent create_file should throw an exception instead of overwriting the existing file. """ #create the file f = create_file(self.filename) self.assertIsInstance(f,nxfile) self.assertTrue(f.is_valid) self.assertFalse(f.readonly) #close the file f.close() self.assertFalse(f.is_valid) #cannot create the file because it already exists self.assertRaises(ObjectError,create_file,self.filename)
def setUp(self): self.nexus_file = create_file(self.full_path,overwrite=True) self.root = self.nexus_file.root() e = self.root.create_group("entry:NXentry") i = e.create_group("instrument:NXinstrument") e.create_group("data:NXdata") e.create_group("sample:NXsample") e.create_group("control:NXmonitor") e.create_field("title","string") e.create_field("experiment_identifier","string") e.create_field("experiment_description","string") self.names = ["instrument:NXinstrument", "data:NXdata", "sample:NXsample", "control:NXmonitor", "title", "experiment_identifier", "experiment_description"] self.names.sort()
def init_file(fname): f = nexus.create_file(fname,overwrite=True) r = f.root() nexus.xml_to_nexus(file_struct,r) return f
def setUp(self): self.gf = nexus.create_file(self.full_path, True) self.root = self.gf.root()
import sys import numpy import time nx = 1024 ny = 2048 dshapemn = [nx, ny] shapemn = [0, nx, ny] chunkmn = [1, nx, ny] fpath = "/tmp/pcotest_00750/pco4000" prefix = "mypcoscan_" postfix = ".h5" start = 0 last = 13 file2 = nexus.create_file("file2.nxs", True) root = file2.root() en = root.create_group("entry", "NXentry") ins = en.create_group("instrument", "NXinstrument") de = ins.create_group("detector", "NXdetector") datamn = de.create_field("data", "uint32", shape=shapemn, chunk=chunkmn) for i in range(50): amn = numpy.ones(shape=dshapemn) amn.fill(i) datamn.grow() print(datamn.shape) print(amn.shape) datamn[i, :, :] = amn file2.flush() time.sleep(1) file2.close()
#!/usr/bin/env python #file: nxgroup_ex2.py from __future__ import print_function import pni.io.nx.h5 as nexus nxfile = nexus.create_file("nxgroup_ex2.nxs",overwrite=True) #create groups entry = nxfile.root().create_group("scan_1","NXentry") entry.create_group("instrument",'NXinstrument') entry.create_group("sample","NXsample").\ create_group("transformations","NXtransformation") entry.create_group("control","NXmonitor") #iterate over groups for child in entry.recursive: print(child.path) nxfile.close()
def setUp(self): self.gf = create_file(self.full_path,overwrite=True) self.root = self.gf.root()
def setUpClass(self): f=create_file(self.full_path,overwrite=True) r = f.root() r.create_field("data","float32")
<group name="entry" type="NXentry"> <group name="instrument" type="NXinstrument"> <group name="detector" type="NXdetector"> <field name="data" type="uint32" units="cps"> <dimensions rank="1"> <dim index="1" value="10"/> </dimensions> 12 9 199 150 123 99 65 87 94 55 </field> </group> </group> </group> """ detector_file = nexus.create_file("detector.nxs",True) root =detector_file.root() nexus.xml_to_nexus(detector_file_struct,root,utils.write_everything) root.close() detector_file.close() master_file = nexus.create_file("master_file.nxs",True) root = master_file.root() nexus.xml_to_nexus(master_file_struct,root) try: dg = nexus.get_object(root,"/:NXentry/:NXinstrument/:NXdetector") except KeyError: print("Could not find detector group in master file!") sys.exit(1) nexus.link("detector.nxs://entry/instrument/detector/data",dg,"data")
def setUp(self): self.nxfile = nx.create_file("Issue_53_Test.nx",overwrite=True)
def setUp(self): self._file = create_file(self.filename,overwrite=True)
import pni.io.nx.h5 as nx fl = nx.create_file("pniio_test_boolattr.h5", True) rt = fl.root() rt.attributes.create("bool_true", "bool")[...] = True rt.attributes.create("bool_false", "bool")[...] = False rt.attributes.create("bool_array", "bool", shape=[4])[...] \ = [False, True, True, False] rt.create_field("ds_bool_scalar_false", "bool")[...] = False rt.create_field("ds_bool_scalar_true", "bool")[...] = True rt.create_field("ds_bool_array", "bool", [4], [4])[...] \ = [False, True, True, False] fl.close()
import numpy import pni.io.nx.h5 as nexus f = nexus.create_file("scalar.nxs",True) r = f.root() d = r.create_field("data","uint16") d1 = 14 d.write(d1) o = d.read() print(d1,o) d2 = numpy.array([16]) d.write(d2) o = d.read() print(d2,o)
#!/usr/bin/env python #file: nxfile_ex1.py import pni.io.nx.h5 as nexus nxfile = nexus.create_file("nxfile_ex1.nxs",overwrite=True) nxfile.close()
def setUpClass(self): f=create_file(self.full_path,overwrite=True)
def setUp(self): self.nxfile = nx.create_file("issue_48_test.nx",overwrite=True) self.root = self.nxfile.root()
<field name="material" type="string">Si + Ge</field> </group> </group> """ detector_struct = \ """ <group name="mythen" type="NXdetector"> <field name="layout" type="string">linear</field> <field name="x_pixel_size" type="float64" units="um">50</field> <field name="y_pixel_size" type="float64" units="mm">5</field> </group> """ def write_if(obj): return (isinstance(obj,nx.nxfield) or isinstance(obj,nx.nxattribute)) and obj.size ==1 f = nx.create_file("xml2.nxs",True) root = f.root() #create the basic object nx.xml_to_nexus(file_struct,root,write_if) instrument = nx.get_object(root,"/:NXentry/:NXinstrument") nx.xml_to_nexus(detector_struct,instrument,write_if) f.close()
def setUpClass(self): self.file_name = "scalar_io_test_on_group_{tc}.nxs".format(tc=self._typecode) self.full_path = os.path.join(self.file_path,self.file_name) self.gf = create_file(self.full_path,overwrite=True) self.gf.close()
def setUp(self): self.file = nexus.create_file(self.full_path,True) self.root = self.file.root() nexus.xml_to_nexus(file_struct,self.root) self.nxdata = nexus.get_object(self.root,"/:NXentry/:NXdata")
#!/usr/bin/env python # File: attribute_io.py from __future__ import print_function import numpy import pni.io.nx.h5 as nexus f = nexus.create_file("attribute_io.nxs", overwrite=True) samptr = ( f.root() .create_group("scan_1", "NXentry") .create_group("instrument", "NXinstrument") .create_group("sample", "NXsample") .create_group("transformations", "NXtransformations") ) samptr.parent.create_field("depends_on", "string")[...] = "transformation/tt" tt = samptr.create_field("tt", "float64", shape=(10,)) tt[...] = numpy.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) tt.attributes.create("transformation_type", "str")[...] = "rotation" a = tt.attributes.create("vector", "float64", shape=(3,)) a[...] = numpy.array([-1, 0, 0]) print("r=", a[...]) print("x=", a[0]) print("y=", a[1]) print("z=", a[2])
<dim index="1" value="10"/> </dimensions> 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10. </field> </group> </group> <group name="data" type="NXdata"> </group> </group> """ f = nexus.create_file("internal_link.nxs",True) r = f.root() nexus.xml_to_nexus(file_struct,r,utils.write_everything) nxdata = nexus.get_object(r,"/:NXentry/:NXdata") #link an object data = nexus.get_object(r,"/:NXentry/:NXinstrument/:NXdetector/data") nexus.link(data,nxdata,"data") #link to a path nexus.link("/entry/sample/transformations/om",nxdata,"om") nexus.link("/entry/detector/transformations/tt",nxdata,"tt") #finalize the nxdata structure for easy plotting nxdata.attributes.create("signal","string")[...]="data"
from __future__ import print_function import pni.io.nx.h5 as nx import numpy f = nx.create_file("test_attributes.nxs",True) r = f.root() a = r.attributes.create("test_scalar","string") a[...] = "hello world" a = r.attributes.create("test_array","string",shape=(2,3)) data = numpy.array([["hello","world","this"],["is","a","test"]]) print(data) print(data.dtype) a[...] = data a.close() r.close() f.close() f = nx.open_file("test_attributes.nxs") r = f.root() a = r.attributes["test_scalar"] print(a[...]) a = r.attributes["test_array"] print(a[...]) print(a[...].flat[...])
<group name="mythen" type="NXdetector"> <field name="layout" type="string">linear</field> <field name="x_pixel_size" type="float64" units="um">50</field> <field name="y_pixel_size" type="float64" units="mm">5</field> </group> </group> <group name="data" type="NXdata"/> <group name="control" type="NXmonitor"/> <group name="sample" type="NXsample"> <field name="name" type="string">VK007</field> <field name="material" type="string">Si + Ge</field> </group> </group> """ f = nx.create_file("rec_group_iter.nxs",True) root = f.root() def write_if(obj): return (isinstance(obj,nx.nxfield) or isinstance(obj,nx.nxattribute)) and obj.size ==1 nx.xml_to_nexus(file_struct,root,write_if) for x in root.recursive: print nx.get_path(x) if isinstance(x,nx.nxfield): print "data: ",x[...] f.close()
f = r.create_field("name","string") attr = f.attributes.create("temperature","float32") attr.write(1.23) attr.close() g.close() f.close() r.close() nruns = int(sys.argv[1]) with open(lfile,"w") as mlog: for i in range(nruns): logline = "%i " %(i) with open(mfile) as stat_file: logline += stat_file.readline() mlog.write(logline) #call the test function nxfile = nexus.create_file("test_mem.nxs",True) test_function(nxfile) nxfile.close() sys.stdout.write(lfile+"\n")