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 test_mdim_attribute(self): xml_to_nexus(mdim_attribute,self.field,lambda obj: True) a = get_object(self.root,"/data@vector") self.assertTrue(check_attribute(a,"vector","float32", (3,))) d = a[...] self.assertAlmostEqual(d[0],0.0) self.assertAlmostEqual(d[1],0.0) self.assertAlmostEqual(d[2],1.0)
def test_mdim_field_default(self): xml_to_nexus(mdim_field_default,self.root,lambda obj: True) f = get_object(self.root,"/mdim_field_default") self.assertTrue(check_field(f,"mdim_field_default","uint32", (2,3))) d = f[...] for (ref_value,data) in zip(range(1,7),d.flat): self.assertEqual(ref_value,data)
def test_multi_group(self): xml_to_nexus(multi_group,self.root) self.assertEqual(len(self.root),3) self.assertTrue(self._check_name_type(self.root["scan_1"],"scan_1", "NXentry")) self.assertTrue(self._check_name_type(self.root["scan_2"],"scan_2", "NXentry")) self.assertTrue(self._check_name_type(self.root["scan_3"],"scan_3", "NXentry"))
def test_nested_group(self): xml_to_nexus(nested_group,self.root) self.assertTrue(self._check_name_type(get_object(self.root,":NXentry"), "scan","NXentry")) self.assertTrue(self._check_name_type( get_object(self.root,":NXentry/:NXinstrument"), "instrument","NXinstrument")) self.assertTrue(self._check_name_type( get_object(self.root,":NXentry/:NXsample"),"sample","NXsample")) self.assertTrue(self._check_name_type( get_object(self.root,":NXentry/:NXmonitor"), "control","NXmonitor")) self.assertTrue(self._check_name_type( get_object(self.root,":NXentry/:NXinstrument/:NXdetector"), "detector","NXdetector")) self.assertTrue(self._check_name_type( get_object(self.root,":NXentry/:NXinstrument/:NXsource"), "source","NXsource"))
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 test_scalar_field(self): xml_to_nexus(scalar_field,self.root) f = get_object(self.root,"/scalar_field") self.assertTrue(check_field(f,"scalar_field","float64",(1,)))
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")
def init_file(fname): f = nexus.create_file(fname,overwrite=True) r = f.root() nexus.xml_to_nexus(file_struct,r) return f
def test_mdim_field_custom(self): xml_to_nexus(mdim_field_custom,self.root) f = get_object(self.root,"/mdim_field_custom") self.assertTrue(check_field(f,"mdim_field_custom","uint32", (1,1024,512)))
def setUpClass(self): f=create_file(self.full_path,overwrite=True) f.close() f = create_file(self.det_path,overwrite=True) r = f.root() xml_to_nexus(detector_file,r)
def test_external_link(self): os.chdir(os.path.dirname(os.path.abspath(__file__))) xml_to_nexus(master_entry,self.root) f = get_object(self.root,"/entry_2/:NXdata/data") self.assertTrue(check_field(f,"data","uint32",(1,)))
def test_mdim_attribute(self): xml_to_nexus(mdim_attribute,self.field) a = get_object(self.root,"/data@vector") self.assertTrue(check_attribute(a,"vector","float32", (3,)))
import pni.io.nx.h5 as nx file_struct = \ """ <group name="entry" type="NXentry"> <group name="instrument" type="NXinstrument"/> <group name="data" type="NXdata"/> <group name="control" type="NXmonitor"/> </group> """ f = nx.create_file("xml1.nxs",True) root = f.root() nx.xml_to_nexus(file_struct,root) f.close()
def test_internal_link(self): xml_to_nexus(internal_link,self.root) f = get_object(self.root,"/entry_1/:NXdata/data") self.assertTrue(check_field(f,"data","uint32",(1,)))
def test_scalar_attribute(self): xml_to_nexus(scalar_attribute,self.field) a = get_object(self.root,"/data@transformation_type") self.assertTrue(check_attribute(a,"transformation_type","string",(1,)))
def test_scalar_field(self): xml_to_nexus(scalar_field,self.root,lambda obj: True) f = get_object(self.root,"/scalar_field") self.assertTrue(check_field(f,"scalar_field","float64",(1,))) self.assertAlmostEqual(f[...],100.)
<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()
<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") data = nexus.get_object(root,"/:NXentry/:NXinstrument/:NXdetector/data")
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" nxdata.attributes.create("axes","string",shape=(2,))[...] = \ numpy.array(["om","tt"])
def test_single_group(self): xml_to_nexus(single_group,self.root) g = get_object(self.root,"/:NXentry") self.assertTrue(self._check_name_type(g,"entry","NXentry"))
<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()
def test_scalar_attribute(self): xml_to_nexus(scalar_attribute,self.field,lambda obj: True) a = get_object(self.root,"/data@transformation_type") self.assertTrue(check_attribute(a,"transformation_type","string",(1,))) self.assertEqual(a[...],"rotation")