def test_unresolvable_link(self): data = nexus.get_object(self.root,"/:NXentry/:NXdata") nexus.link("/entry/instrument/monochromator/energy",data,"data") l = nexus.get_object(self.root,"/:NXentry/:NXdata/data") self.assertTrue(isinstance(l,nexus.nxlink)) self.assertEqual(l.status,nexus.nxlink_status.INVALID) self.assertEqual(l.type,nexus.nxlink_type.SOFT) self.assertEqual(l.path,"/entry:NXentry/data:NXdata/data")
def test_link_field_from_path(self): os.chdir(os.path.dirname(os.path.abspath(__file__))) f = nexus.open_file(self.master2_full_path,readonly=False) r = f.root() detector_group = nexus.get_object(r,"/:NXentry/:NXinstrument/:NXdetector") nexus.link("external_link_test_detector.nxs://entry/instrument/mca/data", detector_group,"mca_data") d = detector_group["mca_data"] self.assertEqual(d.name,"data") self.assertEqual(d.path,"/entry:NXentry/instrument:NXinstrument/mca:NXdetector/data") data = nexus.get_object(r,"/:NXentry/:NXdata") d = data["plot_data"]
def test_loop_by_name(self): e = get_object(self.root,"entry:NXentry") for (name,nref) in zip(e.names(),self.names): child = e[name] self.assertEqual(make_relative(e.path,child.path),nref)
def test_loop_by_index(self): e = get_object(self.root,"entry:NXentry") self.assertEqual(len(e),7) for index in range(len(e)): child = e[index] self.assertEqual(make_relative(e.path,child.path), self.names[index])
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_link_group_from_path(self): """ Test link creation from a path. The target is only specified by its path. """ os.chdir(os.path.dirname(os.path.abspath(__file__))) f = nexus.open_file(self.master1_full_path,readonly=False) r = f.root() instrument_group = nexus.get_object(r,"/:NXentry/:NXinstrument") nexus.link("external_link_test_detector.nxs://entry/instrument/mca", instrument_group,"detector") d = instrument_group["detector"] self.assertEqual(d.name,"mca") self.assertEqual(d.path,"/entry:NXentry/instrument:NXinstrument/mca:NXdetector") data = nexus.get_object(r,"/:NXentry/:NXdata") d = data["plot_data"]
def test_link_from_object(self): """ Test link creation from an object. The target is specified by an object. """ det_data = nexus.get_object(self.root,"/:NXentry/:NXinstrument/:NXdetector/data") nexus.link(det_data,self.nxdata,"plot_data") d = self.nxdata["plot_data"] self.assertEqual(d.name,"plot_data") self.assertEqual(d.size,0) self.assertEqual(d.dtype,"uint16") self.assertEqual(d.path,"/entry:NXentry/data:NXdata/plot_data")
def write_data(f,detector,np): frame_buffer = numpy.zeros(detector.shape,dtype=detector.dtype) r = f.root() detector_group = nexus.get_object(r,"/:NXentry/:NXinstrument/:NXdetector") data = detector_group.create_field("data",detector.dtype, shape=(0,detector.shape[0],detector.shape[1])) print(data.shape) for i in range(np): data.grow(0) frame_buffer[...] = i data[-1,...] = frame_buffer
def test_nxdata(self): data_group = nexus.get_object(self.root,"/:NXentry/:NXdata") links = nexus.get_links(data_group) self.assertEqual(links[0].status,nexus.nxlink_status.VALID) self.assertEqual(links[0].type,nexus.nxlink_type.SOFT) self.assertEqual(links[1].status,nexus.nxlink_status.INVALID) self.assertEqual(links[1].type,nexus.nxlink_type.SOFT) data = links[0].resolve() self.assertTrue(isinstance(data,nexus.nxfield)) self.assertEqual(data.size,0) self.assertEqual(data.dtype,"uint16")
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 test_key_error(self): e = get_object(self.root,"/:NXentry") self.assertRaises(KeyError,e.__getitem__,"bla")
def test_index_error(self): e = get_object(self.root,"/:NXentry") self.assertRaises(IndexError,e.__getitem__,len(e)+1)
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 read_data(f): data = nexus.get_object(f.root(),"/:NXentry/:NXinstrument/:NXdetector/data") for i in range(data.shape[0]): print(data[i,...])
<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 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 test_size_property(self): self.assertEqual(self.root.size,1) self.assertEqual(get_object(self.root,"/:NXentry").size,4) self.assertEqual(get_object(self.root,"/:NXentry/:NXdata").size,0)
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")
def test_iteration(self): e = get_object(self.root,"entry:NXentry") for (c,p) in zip(e,self.names): self.assertEqual(make_relative(e.path,c.path),p)
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"))
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,)))
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_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 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 test_path_property(self): g = get_object(self.root,"/:NXentry/:NXinstrument/") self.assertEqual(g.path,"/entry:NXentry/instrument:NXinstrument")
def test_parent_property(self): g = get_object(self.root,"/:NXentry/:NXinstrument") self.assertEqual(g.parent.name,"entry") self.assertEqual(g.parent.parent.name,"/")
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.)
</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"]) nxdata.attributes.create("tt_indices","uint32")[...] = 0
def test_filename_property(self): g = get_object(self.root,"/:NXentry/:NXinstrument") self.assertEqual(g.filename,self.full_path)