| 1 |
"""@package nxs |
| 2 |
Python NeXus interface. |
| 3 |
|
| 4 |
NeXus_ is a common data format for neutron, Xray and muon science. |
| 5 |
The files contain multidimensional data elements grouped into a |
| 6 |
hierarchical structure. The data sets are self-describing, with |
| 7 |
a description of the instrument configuration including the units |
| 8 |
used as well as the data measured. |
| 9 |
|
| 10 |
The NeXus file interface requires compiled libraries to read the |
| 11 |
underlying HDF_ files. Binary packages are available for some |
| 12 |
platforms from the NeXus site. Details of where the nxs package |
| 13 |
searches for the libraries are recorded in `nxs.napi`. |
| 14 |
""" |
| 15 |
#testing low level functionality |
| 16 |
|
| 17 |
import unittest |
| 18 |
import sys |
| 19 |
import os |
| 20 |
import numpy |
| 21 |
|
| 22 |
sys.path.append("../") |
| 23 |
|
| 24 |
#load the testing module |
| 25 |
import nxs.napi |
| 26 |
|
| 27 |
|
| 28 |
class NAPILowLevelTestCase(unittest.TestCase): |
| 29 |
#static members |
| 30 |
_fname = "NAPI_LL_TEST.nxs" |
| 31 |
|
| 32 |
_shape1 = [20,30] |
| 33 |
_shape2 = [10,20,30] |
| 34 |
_dshape = [nxs.UNLIMITED,20,30] |
| 35 |
|
| 36 |
def setUp(self): |
| 37 |
self._file = nxs.napi.open(self._fname,"w4") |
| 38 |
|
| 39 |
self._file.makegroup("scan_1","NXentry") |
| 40 |
|
| 41 |
pass |
| 42 |
|
| 43 |
def tearDown(self): |
| 44 |
self._file.close() |
| 45 |
|
| 46 |
try: |
| 47 |
os.remove(self._fname) |
| 48 |
except: |
| 49 |
pass |
| 50 |
|
| 51 |
|
| 52 |
def test_file_open(self): |
| 53 |
self.assertRaises(nxs.napi.NeXusError,nxs.napi.open,"bla.nxs","r") |
| 54 |
|
| 55 |
#should produce no exception |
| 56 |
f = nxs.napi.open("bla.nxs","w") |
| 57 |
|
| 58 |
f = nxs.napi.open("bla.nxs","rw") |
| 59 |
|
| 60 |
f.close() |
| 61 |
os.remove("bla.nxs") |
| 62 |
|
| 63 |
def test_file_attributes(self): |
| 64 |
|
| 65 |
self._file.makegroup("/data","NXentry") |
| 66 |
self._file.opengroup("/data") |
| 67 |
self._file.closegroup() |
| 68 |
|
| 69 |
|
| 70 |
def test_file_groups(self): |
| 71 |
|
| 72 |
#should not work - group does not exist |
| 73 |
self.assertRaises(nxs.napi.NeXusError,self._file.opengroup,"data") |
| 74 |
self.assertRaises(nxs.napi.NeXusError,self._file.opengroup,"/data/test") |
| 75 |
|
| 76 |
#this should work |
| 77 |
self._file.makegroup("data","NXentry") |
| 78 |
|
| 79 |
#try to open the group in various ways |
| 80 |
self.assertRaises(nxs.napi.NeXusError,self._file.opengrouppath,"/data/test/dir") |
| 81 |
self.assertRaises(ValueError,self._file.opengroup,"data","NXinstrument") |
| 82 |
|
| 83 |
#the documentation and the code is not very consistent with |
| 84 |
#its exceptions - NeXusError sometimes ValueError |
| 85 |
|
| 86 |
self._file.opengrouppath("/data") #should work |
| 87 |
self._file.openpath("/data") #works too |
| 88 |
|
| 89 |
self.assertRaises(nxs.napi.NeXusError,self._file.openpath,"/data/test/dir") |
| 90 |
|
| 91 |
|
| 92 |
def test_file_compdata(self): |
| 93 |
pass |
| 94 |
|
| 95 |
def test_file_simpledata(self): |
| 96 |
pass |
| 97 |
|
| 98 |
|
| 99 |
def __test_slab_data(self,typecode): |
| 100 |
self._file.opengroup("scan_1") |
| 101 |
|
| 102 |
#--------------write data to disk--------------------------- |
| 103 |
def write_slab(np,d,offset,shape): |
| 104 |
for i in range(np): |
| 105 |
offset[0] = i*shape[0] |
| 106 |
d[...] = i |
| 107 |
self._file.putslab(d,offset,shape) |
| 108 |
|
| 109 |
#-------------read data from disk---------------------------- |
| 110 |
def read_slab(np,d,offset,shape): |
| 111 |
for i in range(np): |
| 112 |
d[...] = i |
| 113 |
offset[0] = i*shape[0] |
| 114 |
o = numpy.squeeze(self._file.getslab(offset,shape)) |
| 115 |
self.assertListEqual(d.flatten().tolist(),o.flatten().tolist()) |
| 116 |
|
| 117 |
#--------------------IO per frame----------------------------- |
| 118 |
#create and open dataset |
| 119 |
self._file.makedata("data",dtype=typecode,shape=self._dshape) |
| 120 |
self._file.opendata("data") |
| 121 |
|
| 122 |
#create data |
| 123 |
d = numpy.ones(self._shape1,dtype=typecode) |
| 124 |
offset = [0,0,0] |
| 125 |
shape = [1]+self._shape1 |
| 126 |
|
| 127 |
#write data |
| 128 |
write_slab(10,d,offset,shape) |
| 129 |
|
| 130 |
#read data back |
| 131 |
d[...] = 1 |
| 132 |
read_slab(10,d,offset,shape) |
| 133 |
|
| 134 |
#close dataset |
| 135 |
self._file.closedata() |
| 136 |
|
| 137 |
#--------------------IO per frame with compression----------------- |
| 138 |
#create and open dataset |
| 139 |
self._file.compmakedata("data3",dtype=typecode,shape=self._dshape, |
| 140 |
mode="lzw", |
| 141 |
chunks=[1]+self._shape1) |
| 142 |
self._file.opendata("data") |
| 143 |
|
| 144 |
#create data |
| 145 |
d = numpy.ones(self._shape1,dtype=typecode) |
| 146 |
offset = [0,0,0] |
| 147 |
shape = [1]+self._shape1 |
| 148 |
|
| 149 |
#write data |
| 150 |
write_slab(10,d,offset,shape) |
| 151 |
|
| 152 |
#read data back |
| 153 |
d[...] = 1 |
| 154 |
read_slab(10,d,offset,shape) |
| 155 |
|
| 156 |
#close dataset |
| 157 |
self._file.closedata() |
| 158 |
|
| 159 |
#------------------volume IO---------------------------------- |
| 160 |
#create new dataset |
| 161 |
self._file.makedata("data2",dtype=typecode,shape=self._dshape) |
| 162 |
self._file.opendata("data2") |
| 163 |
|
| 164 |
#create data |
| 165 |
d = numpy.ones(self._shape2,dtype=typecode) |
| 166 |
offset = [0,0,0] |
| 167 |
shape = self._shape2 |
| 168 |
|
| 169 |
#write data |
| 170 |
write_slab(10,d,offset,shape) |
| 171 |
|
| 172 |
#read data back |
| 173 |
d[...] = 1 |
| 174 |
read_slab(10,d,offset,shape) |
| 175 |
|
| 176 |
#close the dataset |
| 177 |
self._file.closedata() |
| 178 |
|
| 179 |
self._file.closegroup() |
| 180 |
|
| 181 |
def test_file_slabdata_uint16(self): |
| 182 |
self.__test_slab_data("uint16") |
| 183 |
|
| 184 |
def test_file_slabdata_int16(self): |
| 185 |
self.__test_slab_data("int16") |
| 186 |
|
| 187 |
def test_file_slabdata_uint32(self): |
| 188 |
self.__test_slab_data("uint32") |
| 189 |
|
| 190 |
def test_file_slabdata_uint32(self): |
| 191 |
self.__test_slab_data("int32") |
| 192 |
|
| 193 |
def test_file_slabdata_float32(self): |
| 194 |
self.__test_slab_data("float32") |
| 195 |
|
| 196 |
def test_file_slabdata_float64(self): |
| 197 |
self.__test_slab_data("float64") |
| 198 |
|
| 199 |
|
| 200 |
|