def get_histogram(monitor): from mcstas2.utils.carray import bpptr2npyarr core = monitor.core() nx = core.nx ny = core.ny n = nx * ny shape = nx, ny # xmin = core.x_min; xmax = core.x_max # ymin = core.y_min; ymax = core.y_max xmin = attr(core, "xmin", "x_min") xmax = attr(core, "xmax", "x_max") ymin = attr(core, "ymin", "y_min") ymax = attr(core, "ymax", "y_max") dx = (xmax - xmin) / nx dy = (ymax - ymin) / ny Iarr = bpptr2npyarr(core.getPSD_p_00(), 'double', n).copy() E2arr = bpptr2npyarr(core.getPSD_p2_00(), 'double', n).copy() Iarr.shape = E2arr.shape = shape from histogram import histogram, axis, arange xaxis = axis('x', arange(xmin, xmax, dx)) yaxis = axis('y', arange(ymin, ymax, dy)) h = histogram('I(x,y)', [xaxis, yaxis], data=Iarr, errors=E2arr) return h
def get_histogram( monitor ): from mcstas2.utils.carray import bpptr2npyarr core = monitor.core() xmin = 0; xmax = 1 ymin = 0; ymax = 1 dx = 0.1 dy = 0.1 # xmin = core.xmin; xmax = core.xmax # ymin = core.ymin; ymax = core.ymax # dx = xmax - xmin # dy = ymax - ymin # OUTPUT PARAMETERS (DEFS, Vars) #Iarr = bpptr2npyarr( core.getDEFS( ), 'double', n ).copy() #? #E2arr = bpptr2npyarr( core.getPSD_p2_00( ), 'double', n ).copy() #Iarr.shape = E2arr.shape = shape from histogram import histogram, axis, arange xaxis = axis( 'x', arange( xmin, xmax, dx ) ) yaxis = axis( 'y', arange( ymin, ymax, dy ) ) h = histogram( 'I(x,y)', [xaxis,yaxis], data = [2,yaxis], errors=[2,yaxis]) #Iarr) #, errors = E2arr ) return h
def get_histogram(monitor): from mcstas2.utils.carray import bpptr2npyarr core = monitor.core() xmin = 0 xmax = 1 ymin = 0 ymax = 1 dx = 0.1 dy = 0.1 # xmin = core.xmin; xmax = core.xmax # ymin = core.ymin; ymax = core.ymax # dx = xmax - xmin # dy = ymax - ymin # OUTPUT PARAMETERS (DEFS, Vars) #Iarr = bpptr2npyarr( core.getDEFS( ), 'double', n ).copy() #? #E2arr = bpptr2npyarr( core.getPSD_p2_00( ), 'double', n ).copy() #Iarr.shape = E2arr.shape = shape from histogram import histogram, axis, arange xaxis = axis('x', arange(xmin, xmax, dx)) yaxis = axis('y', arange(ymin, ymax, dy)) h = histogram('I(x,y)', [xaxis, yaxis], data=[2, yaxis], errors=[2, yaxis]) #Iarr) #, errors = E2arr ) return h
def process(path): basename = os.path.basename(path) from nslice.Run import Run run = Run(path) print "instrument=%s, Ei=%s, psi=%s" % ( run.instrument, run.Ei, run.psi) from nslice.XtalOrientation import XtalOrientation a = b = 8.87; c = 5.2 from math import pi twopi = 2*pi ra,rb,rc = [twopi/a, 0,0], [0,twopi/b,0], [0,0,twopi/c] u,v = [1,0,0], [0,1,0] xtal_ori = XtalOrientation(ra,rb,rc, u,v, run.psi) h,k,l,E = run.compute_hklE(xtal_ori) I, error = run.read_data() h.shape = k.shape = l.shape = E.shape = I.shape = error.shape = -1 hklEIE = np.vstack((h,k,l,E,I,error)) from nslice.slice import slice_hE H, edges = slice_hE( hklEIE, k=[0.95,1.05], l=[-1,1], h=(-1, 6, 0.02), E=(-5, 10, 0.1), ) import histogram, histogram.hdf as hh axes = [ histogram.axis('h', boundaries=edges[0]), histogram.axis('E', unit='meV', boundaries=edges[1]), ] h = histogram.histogram('I(h,E)', axes=axes, data=H) hh.dump(h, 'I_hE.h5') return
def events2Idpt( events, instrument, tofparams ): from mccomponents.detector.utils import \ getDetectorHierarchyDimensions dims = getDetectorHierarchyDimensions( instrument ) # 1st attempt to create axes from histogram import histogram, axis, arange axes = [ axis('%sID' % name.lower(), range(n)) for name, n in dims ] # the first level of detectors (tubes, packs, or others) need # special attention. ids of that level could be not continuous. detectorSystem = instrument.getDetectorSystem() assert len(detectorSystem.elements()) == dims[0][1] ids = [ element.id() for element in detectorSystem.elements() ] axes[0] = axis( axes[0].name(), ids ) detaxes = axes #tof axis tmin, tmax, tstep = tofparams tofaxis = axis( 'tof', boundaries = arange( tmin, tmax+tstep/10., tstep ), unit = 'second' ) #all axes axes = detaxes + [tofaxis] #histogram hist = histogram( 'Idpt', axes ) #get the numpy array which will accept events npixels = hist.size()/tofaxis.size() Ipixtof = hist.data().storage().asNumarray() Ipixtof.shape = npixels, -1 events2Ipixtof( events, Ipixtof ) return hist
def computeVolume(self, paths=None, **kwds): H = None sa = None edges = None for path in paths or self.paths: print path edges1, H1, sa1 = self.computeVolumeForOneRun(path, **kwds) if H is None: edges, H, sa = edges1, H1, sa1 else: H += H1 sa += sa1 continue import histogram axes = [ histogram.axis(kwds['x'], boundaries=edges[0]), histogram.axis(kwds['y'], boundaries=edges[1]), histogram.axis(kwds['z'], boundaries=edges[2]), ] return histogram.histogram( 'I(%(x)s,%(y)s,%(z)s)' % kwds, axes=axes, data=H / sa, )
def makeSlice(events, xaxis, Eaxis, Qtox): """convert input events into a histogram (slice) Qtox: convert Q to x. also return a mask. since some Q points should be discarded """ import numpy as np data = np.array(list(events)) # convert Q to x Q = data[:, :3] x, mask = Qtox(Q) E = data[mask, 3] sample = np.vstack((x, E)).T # xbins = np.arange(*xaxis) Ebins = np.arange(*Eaxis) bins = xbins, Ebins weights = data[mask, -1] I, edges = np.histogramdd(sample, bins=bins, weights=weights) import histogram as H, histogram.hdf as hh axes = [ H.axis('x', boundaries=edges[0]), H.axis('E', boundaries=edges[1]), ] return H.histogram('IxE', axes=axes, data=I)
def get_histogram( monitor ): from mcstas2.utils.carray import bpptr2npyarr core = monitor.core() nx = core.nxchan; ny =core.nychan; nb = core.nbchan n = nx * ny * nb shape = nx, ny, nb xmin = -core.xwidth/2; xmax = core.xwidth/2 ymin = -core.yheight/2; ymax = core.yheight/2 dx = (xmax - xmin)/nx dy = (ymax - ymin)/ny if core.bmax!=0: bmax=core.bmax bmin=core.bmin db=(bmax-bmin)/nb else : db = core.deltab bmin=0; bmax=nb*db+bmin Iarr = bpptr2npyarr( core.getTOF_p_00( ), 'double', n ).copy() E2arr = bpptr2npyarr( core.getTOF_p2_00( ), 'double', n ).copy() Iarr.shape = E2arr.shape = shape from histogram import histogram, axis, arange xaxis = axis( 'x', boundaries=arange( xmin, xmax+dx/10, dx ) ) yaxis = axis( 'y', boundaries=arange( ymin, ymax+dy/10, dy ) ) baxis = axis( 'b', boundaries=arange( bmin, bmax+db/10, db ) ) h = histogram( 'I(x,y,b)', [xaxis,yaxis,baxis], data = Iarr, errors = E2arr ) return h
def load_mcvine_psf_qE(self, adjust_energy_center=False): path = self.sim_path qaxis = self.qaxis; Eaxis = self.Eaxis dEs = np.load(os.path.join(path, 'dEs.npy')) dqs = np.load(os.path.join(path, 'dxs.npy')) probs = np.load(os.path.join(path, 'probs.npy')) hist, qedges, Eedges = np.histogram2d(dqs, dEs, bins=(np.arange(*qaxis), np.arange(*Eaxis)), weights=probs) # remove outliers median = np.median(hist[hist>0]) normal = hist[hist<median*100]; normal_max = np.max(normal) hist[hist>median*100] = normal_max # adjust energy center if adjust_energy_center: hist_E = hist.sum(axis=0) Ebincenters = (Eedges[1:] + Eedges[:-1])/2 Ecenter = (Ebincenters*hist_E).sum()/hist_E.sum() Eedges -= Ecenter # res(q, E) histogram qaxis = H.axis('q', boundaries=qedges) Eaxis = H.axis('E', boundaries=Eedges) self.mcvine_psf_qE = reshist = H.histogram('res', (qaxis, Eaxis), data=hist) self.qEgrids = np.meshgrid(reshist.q, reshist.E) # res(E) histogram self.mcvine_psf_E = reshist.sum('q') # res(q) histogram Emin = Eedges[0]; Emax = Eedges[-1]; middle = (Emin+Emax)/2; Erange = Emax-Emin self.mcvine_psf_q = reshist[(), (middle-Erange/20, middle+Erange/20)].sum('E') return
def sqehist(E, g, **kwds): "a simple wrapper of method sqe to return a histogram" Q,E,S = sqe(E,g, **kwds) import histogram as H Qaxis = H.axis('Q', Q, '1./angstrom') Eaxis = H.axis('E', E, 'meV') return H.histogram("SP SQE", [Qaxis, Eaxis], S)
def makeSlice(events, xaxis, Eaxis, Qtox): """convert input events into a histogram (slice) Qtox: convert Q to x. also return a mask. since some Q points should be discarded """ import numpy as np data = np.array(list(events)) # convert Q to x Q = data[:, :3] x,mask = Qtox(Q) E = data[mask, 3] sample = np.vstack((x, E)).T # xbins = np.arange(*xaxis) Ebins = np.arange(*Eaxis) bins = xbins, Ebins weights = data[mask, -1] I, edges = np.histogramdd(sample, bins=bins, weights=weights) import histogram as H, histogram.hdf as hh axes = [ H.axis('x', boundaries=edges[0]), H.axis('E', boundaries=edges[1]), ] return H.histogram('IxE', axes=axes, data = I)
def get_histogram(monitor): from mcstas2.utils.carray import bpptr2npyarr core = monitor.core() nx = core.nx ny = core.ny assert nx == int(nx) nx = int(nx) assert ny == int(ny) ny = int(ny) n = nx * ny shape = nx, ny Iarr = bpptr2npyarr(core.getPSD_p_00(), 'double', n).copy() E2arr = bpptr2npyarr(core.getPSD_p2_00(), 'double', n).copy() Iarr.shape = E2arr.shape = shape from histogram import histogram, axis, arange dx = 360. / nx xaxis = axis('x', arange(0, 360, dx), unit='deg') dy = 180. / ny yaxis = axis('y', arange(-90, 90, dy), unit='deg') h = histogram('I(x,y)', [xaxis, yaxis], data=Iarr, errors=E2arr) return h
def get_histogram( monitor ): from mcstas2.utils.carray import bpptr2npyarr core = monitor.core() nx = core.nx; ny =core.ny n = nx * ny shape = nx, ny # xmin = core.x_min; xmax = core.x_max # ymin = core.y_min; ymax = core.y_max xmin = attr(core, "xmin", "x_min"); xmax = attr(core, "xmax", "x_max"); ymin = attr(core, "ymin", "y_min"); ymax = attr(core, "ymax", "y_max"); dx = (xmax - xmin)/nx dy = (ymax - ymin)/ny Iarr = bpptr2npyarr( core.getPSD_p_00( ), 'double', n ).copy() E2arr = bpptr2npyarr( core.getPSD_p2_00( ), 'double', n ).copy() Iarr.shape = E2arr.shape = shape from histogram import histogram, axis, arange xaxis = axis( 'x', arange( xmin, xmax, dx ) ) yaxis = axis( 'y', arange( ymin, ymax, dy ) ) h = histogram( 'I(x,y)', [xaxis,yaxis], data = Iarr, errors = E2arr ) return h
def test_values2indexes(self): "Histogram: values2indexes" from histogram import histogram, axis, arange x = axis( 'x', arange(1., 10., 1.) ) y = axis( 'y', arange(-1., 1., 0.1) ) h = histogram('h', (x,y) ) self.assertVectorEqual( h.values2indexes( (2, 0.2) ), (1,12) ) return
def test_axisFromId(self): "Histogram: axisFromId" from histogram import histogram, axis, arange x = axis( 'x', arange(1., 10., 1.) ) y = axis( 'y', arange(-1., 1., 0.1) ) h = histogram('h', (x,y) ) self.assertEqual( h.axisFromId(1), x ) self.assertEqual( h.axisFromId(2), y ) return
def test__idiv__2(self): "histogram: h/=h1" h = self._histogram.copy() h2 = self._histogram2 h /= h2 self.assertVectorAlmostEqual( h[1.5,1], (1,2.0/3) ) h = self._histogram.copy() h2 = createHistogram( noerror = True ) h /= h2 self.assertVectorAlmostEqual( h[1.5,1], (1,1./3) ) from histogram import histogram, axis, arange, datasetFromFunction x = axis('x', arange(1, 2, .2 ) ) y = axis('y', arange(0, 3, .5 ) ) def fa(x,y): return x*y + x ha = histogram('a', [x,y], datasetFromFunction( fa, (x,y) ) ) for xi in x.binCenters(): for yi in y.binCenters(): self.assertAlmostEqual( fa(xi,yi), ha[xi,yi][0] ) def fb(x,y): return x hb = histogram('b', [x,y], datasetFromFunction( fb, (x,y) ) ) hc = ha/hb for xi in x.binCenters(): for yi in y.binCenters(): self.assertAlmostEqual( yi+1, hc[xi,yi][0] ) def fberr(x,y): return 0*x hb = histogram('b', [x,y], datasetFromFunction( fb, (x,y) ), datasetFromFunction( fberr, (x,y) ) ) hc = ha/hb for xi in x.binCenters(): for yi in y.binCenters(): self.assertAlmostEqual( yi+1, hc[xi,yi][0] ) #involve units h1 = histogram( 'h1', [ ('x', [1,2,3]), ], unit = 'meter', data = [1,2,3], errors = [1,2,3], ) h2 = histogram( 'h2', [ ('x', [1,2,3]), ], data = [1,1,1], errors = [1,1,1], unit = 'second', ) h3 = h1/h2 self.assertVectorAlmostEqual( h3.I, (1,2,3) ) self.assertVectorAlmostEqual( h3.E2, (2,6,12) ) return
def test0(self): import histogram as H qaxis = H.axis('Q', boundaries=H.arange(0, 13.0, 0.1)) eaxis = H.axis('energy', boundaries=H.arange(-50, 50, 1.0)) sqe = H.histogram('sqe', [qaxis, eaxis], fromfunction=lambda q, e: q * q + e * e) from mccomponents.sample.idf import writeSQE writeSQE(sqe, datapath) return
def setParameters( self, Q_params, E_params, ARCSxml, Ei, emission_time): keys = [ 'detector-system-dimensions', 'moderator-sample distance', 'pixelID-position mapping binary file', 'detector axes', 'solidangles', ] infos = self._readInstrumentInfo(ARCSxml, keys=keys) npacks, ndetsperpack, npixelsperdet = infos[ 'detector-system-dimensions'] mod2sample = infos['moderator-sample distance'] pixelPositionsFilename = infos[ 'pixelID-position mapping binary file'] self._debug.log( "pixel-positions-filename=%s" % pixelPositionsFilename) self._debug.log( 'E_params (unit: meV) = %s' % (E_params, ) ) self._debug.log( 'Q_params (unit: angstrom^-1) = %s' % (Q_params, ) ) self._debug.log( 'mod2sample distance = %s' % mod2sample ) self._debug.log( 'Incident energy (unit: meV) = %s' % (Ei, ) ) self._debug.log( 'emission_time (unit: microsecond) = %s' % (emission_time, ) ) E_begin, E_end, E_step = E_params # meV Q_begin, Q_end, Q_step = Q_params # angstrom^-1 Q_axis = histogram.axis('Q', boundaries = histogram.arange( Q_begin, Q_end, Q_step) ) E_axis = histogram.axis('energy', boundaries = histogram.arange( E_begin, E_end, E_step) ) h = histogram.histogram( 'I(Q,E)', [ Q_axis, E_axis, ], data_type = 'int', ) pixelPositions = arcseventdata.readpixelpositions( pixelPositionsFilename, npacks, ndetsperpack, npixelsperdet ) # remember a few things so that we can do normalization mpiRank = self.mpiRank if mpiRank == 0: pixelSolidAngles = infos['solidangles'].I self._remember( Ei, pixelPositions, pixelSolidAngles ) self.out_histogram = h self.pixelPositions = pixelPositions self.Ei = Ei self.mod2sample = mod2sample self.emission_time = emission_time return
def read(directory, omega2_idf_file='Omega2', qgridinfo_file='Qgridinfo', weightedq_file='WeightedQ', nD=3): ''' read omega2 data file in idf format nD is the dimension of the qgrid. it is assumed that the qgrid is regular, and is defined in qgridinfo_file The qgridinfo_file is a python file that defines vars bi, {i=1..nD} and ni, {i=1..nD} ''' import os from idf.Omega2 import read info, omega2 = read(os.path.join(directory, omega2_idf_file)) qgridinfo = _retrieveQgridinfo(directory, qgridinfo_file, weightedq_file, nD=nD) # nQ, nbXnD = omega2.shape # import operator nis = [qgridinfo['n' + str(i)] for i in range(1, nD + 1)] assert nQ == reduce(operator.__mul__, nis),\ "Q points mismatch: nQ=%s, {ni}=%s" % (nQ, nis) # number of basis nb = nbXnD / nD # import histogram as H # Q axes Qaxes = [] for i in range(nD): ni = qgridinfo['n' + str(i + 1)] bi = qgridinfo['b' + str(i + 1)] Qiaxis = H.axis('Q' + str(i + 1), H.arange(0, 1 + 1e-10, 1. / (ni - 1)), '1./angstrom', attributes={'b': bi}) Qaxes.append(Qiaxis) continue # basis basisaxis = H.axis('basisID', range(nb)) # dimension Daxis = H.axis('dimensionID', range(nD)) # axes = Qaxes + [basisaxis, Daxis] # omega2.shape = [axis.size() for axis in axes] h = H.histogram('Omega2', axes, data=omega2) return h
def test(): from histogram import histogram, axis, arange, plot xaxis = axis('x', arange(5), unit='meter') yaxis = axis('y', arange(7), unit='cm') axes = [xaxis, yaxis] h = histogram("intensity", axes, fromfunction=lambda x, y: x**2 + y**2) print(h) plot(h) help(h) slice = h[3, ()]
def test(): from histogram import histogram, axis, arange, plot xaxis = axis('x', arange(5), unit='meter') yaxis = axis('y', arange(7), unit='cm') axes = [xaxis, yaxis] h = histogram( "intensity", axes, fromfunction=lambda x,y: x**2+y**2) print h plot(h) help(h) slice = h[3, ()]
def testReplaceAxis(self): 'Hisogram: replaceAxis' from histogram import histogram, axis, arange a = axis('a', arange(1,10,1.0)) b = axis('b', arange(1,100,10.)) h = histogram( 'h', [a] ) self.assert_(a is h.axisFromName('a')) h.replaceAxis(name='a', axis=b) self.assert_(b is h.axisFromName('a')) return
def createSqe(): import histogram as H qaxis = H.axis( 'Q', boundaries = H.arange(0, 13.0, 0.1) ) eaxis = H.axis( 'energy', boundaries = H.arange(-50, 50, 1.0) ) sqe = H.histogram( 'sqe', [ qaxis, eaxis ], fromfunction = sqe_f ) return sqe
def testReplaceAxis(self): 'Hisogram: replaceAxis' from histogram import histogram, axis, arange a = axis('a', arange(1,10,1.0)) b = axis('b', arange(1,100,10.)) h = histogram( 'h', [a] ) self.assertTrue(a is h.axisFromName('a')) h.replaceAxis(name='a', axis=b) self.assertTrue(b is h.axisFromName('a')) return
def test0(self): import histogram as H qaxis = H.axis( 'Q', boundaries = H.arange(0, 13.0, 0.1) ) eaxis = H.axis( 'energy', boundaries = H.arange(-50, 50, 1.0) ) sqe = H.histogram( 'sqe', [ qaxis, eaxis ], fromfunction = lambda q,e: q*q+e*e ) from mccomponents.sample.idf import writeSQE writeSQE( sqe, datapath ) return
def interp(iqehist, newE): """compute a new IQE histogram using the new energy array * iqehist: input IQE histogram * newE: new energy centers array """ from scipy import interpolate mask = iqehist.I != iqehist.I # find energy boundaries of dynamic range for each Q def get_boundary_indexes(a): nz = np.nonzero(a)[0] if nz.size: return nz[0], nz[-1] else: return 0, 0 boundary_indexes = [get_boundary_indexes(row) for row in np.logical_not(mask)] try: E = iqehist.energy except: E = iqehist.E Eranges = [(E[ind1], E[ind2]) for ind1, ind2 in boundary_indexes] # iqehist.I[mask] = 0 iqehist.E2[mask] = 0 Q = iqehist.Q f = interpolate.interp2d(E, Q, iqehist.I, kind="linear") E2f = interpolate.interp2d(E, Q, iqehist.E2, kind="linear") dE = E[1] - E[0] Emin = E[0] // dE * dE Emax = E[-1] // dE * dE # Enew = np.arange(Emin, Emax+dE/2, dE) newS = f(newE, Q) newS_E2 = E2f(newE, Q) # create new histogram Eaxis = H.axis("E", newE, unit="meV") Qaxis = H.axis("Q", Q, unit="1./angstrom") newHist = H.histogram("IQE", [Qaxis, Eaxis], data=newS, errors=newS_E2) # for Erange, q in zip(Eranges, Q): Emin, Emax = Erange if Emin > newE[0]: Emin = min(Emin, newE[-1]) newHist[q, (None, Emin)].I[:] = np.nan newHist[q, (None, Emin)].E2[:] = np.nan if Emax < newE[-1]: Emax = max(Emax, newE[0]) newHist[q, (Emax, None)].I[:] = np.nan newHist[q, (Emax, None)].E2[:] = np.nan continue return newHist
def getSqeHistogramFromMantidWS(reduced, outfile, qaxis=None, eaxis=None): from mantid import simpleapi as msa # if eaxis is not specified, use the data in reduced workspace if eaxis is None: Edim = reduced.getXDimension() emin = Edim.getMinimum() emax = Edim.getMaximum() de = Edim.getX(1) - Edim.getX(0) eaxis = emin, de, emax qmin, dq, qmax = qaxis nq = int(round((qmax - qmin) / dq)) emin, de, emax = eaxis ne = int(round((emax - emin) / de)) md = msa.ConvertToMD( InputWorkspace=reduced, QDimensions='|Q|', dEAnalysisMode='Direct', MinValues="%s,%s" % (qmin, emin), MaxValues="%s,%s" % (qmax, emax), ) binned = msa.BinMD( InputWorkspace=md, AxisAligned=1, AlignedDim0="|Q|,%s,%s,%s" % (qmin, qmax, nq), AlignedDim1="DeltaE,%s,%s,%s" % (emin, emax, ne), ) # convert to histogram import histogram as H, histogram.hdf as hh data = binned.getSignalArray().copy() err2 = binned.getErrorSquaredArray().copy() nev = binned.getNumEventsArray() data /= nev err2 /= (nev * nev) import numpy as np qaxis = H.axis('Q', boundaries=np.arange(qmin, qmax + dq / 2., dq), unit='1./angstrom') eaxis = H.axis('E', boundaries=np.arange(emin, emax + de / 2., de), unit='meV') hist = H.histogram('IQE', (qaxis, eaxis), data=data, errors=err2) if outfile.endswith('.nxs'): import warnings warnings.warn( "reduce function no longer writes iqe.nxs nexus file. it only writes iqe.h5 histogram file" ) outfile = outfile[:-4] + '.h5' hh.dump(hist, outfile) return
def read(directory, omega2_idf_file='Omega2', qgridinfo_file='Qgridinfo', weightedq_file='WeightedQ', nD=3): ''' read omega2 data file in idf format nD is the dimension of the qgrid. it is assumed that the qgrid is regular, and is defined in qgridinfo_file The qgridinfo_file is a python file that defines vars bi, {i=1..nD} and ni, {i=1..nD} ''' import os from idf.Omega2 import read info, omega2 = read(os.path.join(directory,omega2_idf_file)) qgridinfo = _retrieveQgridinfo(directory, qgridinfo_file, weightedq_file, nD=nD) # nQ, nbXnD = omega2.shape # import operator nis = [qgridinfo['n'+str(i)] for i in range(1, nD+1)] assert nQ == reduce(operator.__mul__, nis),\ "Q points mismatch: nQ=%s, {ni}=%s" % (nQ, nis) # number of basis nb = nbXnD / nD # import histogram as H # Q axes Qaxes = [] for i in range(nD): ni = qgridinfo['n'+str(i+1)] bi = qgridinfo['b'+str(i+1)] Qiaxis = H.axis('Q'+str(i+1), H.arange(0, 1+1e-10, 1./(ni-1)), '1./angstrom', attributes = {'b': bi} ) Qaxes.append(Qiaxis) continue # basis basisaxis = H.axis('basisID', range(nb)) # dimension Daxis = H.axis('dimensionID', range(nD)) # axes = Qaxes+[basisaxis, Daxis] # omega2.shape = [axis.size() for axis in axes] h = H.histogram('Omega2', axes, data = omega2) return h
def resample( hist1, size ): import histogram as H assert hist1.dimension() == 1 assert hist1.shape()[0] > display_size*10 xaxis = hist1.axes()[0] xbb = xaxis.binBoundaries().asNumarray() front, back = xbb[0], xbb[-1] step = (back-front)/size newxbb = H.arange( front, back+step/2, step) newxaxis = H.axis( xaxis.name(), boundaries = newxbb, unit = xaxis.unit() ) newhist = H.histogram( hist1.name(), [ newxaxis ] ) newxc = newxaxis.binCenters() for x in newxc[1:-1] : newhist[ x ] = hist1[ (x-step/2, x+step/2) ].sum() continue newhist[ newxc[0] ]= hist1[ (newxbb[0], newxc[0] + step/2) ].sum() newhist[ newxc[-1] ]= hist1[ (newxc[-1]-step/2, newxbb[-1]-step*1.e-10) ].sum() return newhist
def test_combined_1(self): 'Axis: slice and changeUnit' taxis= axis('t', range(10), unit='second' ) from histogram.SlicingInfo import SlicingInfo slice = taxis[ SlicingInfo( (3,9) ) ] self.assertRaises( RuntimeError, slice.changeUnit, 'millisecond' ) return
def resample(hist1, size): import histogram as H assert hist1.dimension() == 1 assert hist1.shape()[0] > display_size * 10 xaxis = hist1.axes()[0] xbb = xaxis.binBoundaries().asNumarray() front, back = xbb[0], xbb[-1] step = 1. * (back - front) / size newxbb = H.arange(front, back + step / 2., step) newxaxis = H.axis(xaxis.name(), boundaries=newxbb, unit=xaxis.unit()) newhist = H.histogram(hist1.name(), [newxaxis]) newxc = newxaxis.binCenters() for x in newxc[1:-1]: newhist[x] = hist1[(x - step / 2., x + step / 2.)].sum() continue newhist[newxc[0]] = hist1[(newxbb[0], newxc[0] + step / 2.)].sum() newhist[newxc[-1]] = hist1[(newxc[-1] - step / 2., newxbb[-1] - step * 1.e-10)].sum() return newhist
def setParameters( self, ARCSxml, d_params, emission_time): info.log( 'd_params (unit: AA) = %s' % (d_params, ) ) info.log( 'emission_time (unit: microsecond) = %s' % (emission_time, ) ) infos = self._readInstrumentInfo(ARCSxml) npacks, ndetsperpack, npixelsperdet = infos[ 'detector-system-dimensions'] mod2sample = infos['moderator-sample distance'] pixelPositionsFilename = infos[ 'pixelID-position mapping binary file'] d_begin, d_end, d_step = d_params # angstrom d_axis = histogram.axis('d spacing', boundaries = histogram.arange( d_begin, d_end, d_step) ) detaxes = infos['detector axes'] h = histogram.histogram( 'I(pdpd)', detaxes + [d_axis], data_type = 'int', ) info.log( "reading pixelID->position map..." ) pixelPositions = arcseventdata.readpixelpositions( pixelPositionsFilename, npacks, ndetsperpack, npixelsperdet) self.out_histogram = h self.pixelPositions = pixelPositions self.mod2sample = mod2sample self.emission_time = emission_time return
def slice2hist(ifile, ofile): import histogram as H, histogram.hdf as hh from mantid import simpleapi as msa def eliminateUnitDimension(shape): for d in shape: if d>1: yield d return ws = msa.Load(ifile) I = ws.getSignalArray() I.shape = tuple(eliminateUnitDimension(I.shape)) E2 = ws.getErrorSquaredArray() E2.shape = I.shape axes = [] for i in range(ws.getNumDims()): dim = ws.getDimension(i) if dim.getNBins() > 1: axis = H.axis( dim.getName(), unit="1", centers = [dim.getX(ind) for ind in range(dim.getNBins())] ) axes.append(axis) continue h = H.histogram("slice", axes, data=I, errors=E2) hh.dump(h, ofile) return
def getpixelinfo_mergedpixels(ARCSxml, pixelresolution): npixels = 128 assert npixels%pixelresolution==0 from arcseventdata.combinepixels import combinepixels, geometricInfo, geometricInfo_MergedPixels from histogram import axis pixelaxis = axis('pixelID', range(0, 128, pixelresolution)) info.log('merging pixels') phi_p, psi_p, dist_p, sa_p, dphi_p, dpsi_p = combinepixels(ARCSxml, pixelaxis, pixelresolution) positions, radii, heights = geometricInfo(ARCSxml) positions, radii, heights = geometricInfo_MergedPixels(positions, radii, heights, pixelresolution) widths = radii*2 phis = phi_p.I psis = psi_p.I dists = dist_p.I sas = sa_p.I # 06/30/2009: was told that the last two columns are angles da1 = widths/dists da2 = sas/da1 dists.shape = phis.shape = psis.shape = widths.shape = heights.shape = -1, da1.shape = da2.shape = -1, #return dists, phis, psis, widths, heights return dists, phis, psis, da1, da2
def test_combined_1(self): 'Axis: slice and changeUnit' taxis = axis('t', list(range(10)), unit='second') from histogram.SlicingInfo import SlicingInfo slice = taxis[SlicingInfo((3, 9))] self.assertRaises(RuntimeError, slice.changeUnit, 'millisecond') return
def e2Id( events, n, pixelpositions, dspacing_params = None, Idspacing = None, preNeXus_tofUnit = 1.e-7, mod2sample = 13.5): '''e2Id(events, n, pixelpositions, Idspacing = None, dspacing_params = None, npacks = 115, ndetsperpack = 8, npixelsperdet = 128, preNeXus_tofUnit = 1.e-7, mod2sample = 13.5) --> integrate events to I(d spacing) histogram Either Idspacing or dspacing_params must be given Idspacing: histogram I(d spacing). If this is None, a histogram will be created using dspacing_params. dspacing_params: begin, end, and step of dspacing. unit: angstrom If Idspacing is given, this is ignored. events: neutron events n: number of neutron events to process pixelpositions: array of pixel positions preNeXus_tofUnit: unit of event.tof in the pre-Nexus data format. unit: second mod2sample: moderator-sample distance. unit: meter return: the I(d spacing) histogram ''' if Idspacing is None: if dspacing_params is None: raise ValueError, "Must provide either the (min, max, step) of d axis or the I(d) histogram" dspacing_begin, dspacing_end, dspacing_step = dspacing_params # angstrom import histogram daxis = histogram.axis( 'd', boundaries = histogram.arange(dspacing_begin, dspacing_end, dspacing_step), unit = 'angstrom', ) Idspacing = histogram.histogram( 'I(d spacing)', [daxis], data_type = 'int', ) pass events2Idspacing( events, n, Idspacing, pixelpositions, tofUnit = preNeXus_tofUnit, mod2sample = mod2sample) return Idspacing
def setParameters( self, ARCSxml, tof_params, pack_params = (1,115), pixel_step = 1 ): infos = self._readInstrumentInfo(ARCSxml) npacks, ndetsperpack, npixelsperdet = infos[ 'detector-system-dimensions'] mod2sample = infos['moderator-sample distance'] pixelPositionsFilename = infos[ 'pixelID-position mapping binary file'] info.log( 'tof_params (unit: microsecond) = %s' % (tof_params, ) ) import numpy tof_begin, tof_end, tof_step = numpy.array(tof_params)*1.e-6 #convert from microseconds to seconds tof_axis = histogram.axis( 'tof', boundaries = histogram.arange(tof_begin, tof_end, tof_step), unit = 'second' ) detaxes = infos['detector axes'] #pixel axis need to be changed if pixel resolution is not 1 if pixel_step != 1: pixelAxis = detaxes[2] npixelspertube = pixelAxis.size() pixelAxis = histogram.axis( 'pixelID', boundaries = range(0, npixelspertube+1, pixel_step), ) detaxes[2] = pixelAxis #pack axis startpack, endpack = pack_params packaxis = histogram.axis( 'detectorpackID', range( startpack, endpack+1 ) ) detaxes[0] = packaxis h = histogram.histogram( 'I(pdpt)', detaxes + [tof_axis], data_type = 'int', ) self.out_histogram = h return
def computeSlice(self, paths=None, **kwds): H = None; sa = None; edges = None for path in paths or self.paths: print path edges1, H1, sa1 = self.computeSliceForOneRun(path, **kwds) if H is None: edges, H, sa = edges1, H1, sa1 else: H += H1; sa += sa1 continue import histogram axes = [ histogram.axis(kwds['x'], boundaries=edges[0]), histogram.axis(kwds['y'], boundaries=edges[1]), ] return histogram.histogram('I(%(x)s,%(y)s)'%kwds, axes=axes, data=H/sa)
def makeDispersion(): nAtoms = 2 nBranches = 3 * nAtoms nQx = 10; nQy = 12; nQz = 14 Qaxes = [ ([1,0,0], nQx), ([0,1,0], nQy), ([0,0,1], nQz), ] import histogram as H qx = H.axis('qx', H.arange(0, 1+1e-10, 1./(nQx-1))) qy = H.axis('qy', H.arange(0, 1+1e-10, 1./(nQy-1))) qz = H.axis('qz', H.arange(0, 1+1e-10, 1./(nQz-1))) br = H.axis('branchId', range(nBranches)) atoms = H.axis('atomId', range(nAtoms)) pols = H.axis('polId', range(3)) realimags = H.axis('realimagId', range(2)) eps = H.histogram('eps', [qx,qy,qz,br,atoms,pols,realimags]) eps.I[:] = 1 e = H.histogram('e', [qx,qy,qz,br], fromfunction = lambda qx,qy,qz,br: (qx*qx+qy*qy+qz*qz)/3.*50, ) disp = b.linearlyinterpolateddispersion_3d(nAtoms, Qaxes, eps.I, e.I) disp = b.periodicdispersion(disp, ((1,0,0), (0,1,0), (0,0,1))) return disp
def makeDispersion(): nAtoms = 2 nBranches = 3 * nAtoms nQx = 10 nQy = 12 nQz = 14 Qaxes = [ ([1, 0, 0], nQx), ([0, 1, 0], nQy), ([0, 0, 1], nQz), ] import histogram as H qx = H.axis('qx', H.arange(0, 1 + 1e-10, 1. / (nQx - 1))) qy = H.axis('qy', H.arange(0, 1 + 1e-10, 1. / (nQy - 1))) qz = H.axis('qz', H.arange(0, 1 + 1e-10, 1. / (nQz - 1))) br = H.axis('branchId', range(nBranches)) atoms = H.axis('atomId', range(nAtoms)) pols = H.axis('polId', range(3)) realimags = H.axis('realimagId', range(2)) eps = H.histogram('eps', [qx, qy, qz, br, atoms, pols, realimags]) eps.I[:] = 1 e = H.histogram( 'e', [qx, qy, qz, br], fromfunction=lambda qx, qy, qz, br: (qx * qx + qy * qy + qz * qz) / 3. * 50, ) disp = b.linearlyinterpolateddispersion_3d(nAtoms, Qaxes, eps.I, e.I) disp = b.periodicdispersion(disp, ((1, 0, 0), (0, 1, 0), (0, 0, 1))) return disp
def run(scan=None, poolsize=None, output=None, **volume_opts): scan = load_mod(scan)['scan'] def worker(work_q, H, sa): while not work_q.empty(): f = work_q.get() print f one(f, H, sa, scan, volume_opts) continue return shape, edges = scan.volumeOutputDims(**volume_opts) size = shape[0] * shape[1] * shape[2] from multiprocessing import Process, Queue, Array shared_H = Array('d', size) shared_sa = Array('d', size) work_q = Queue() for path in scan.paths: work_q.put(path) processes = [] for w in xrange(poolsize): p = Process(target=worker, args=(work_q, shared_H, shared_sa)) p.start() processes.append(p) continue for p in processes: p.join() import histogram axes = [ histogram.axis(volume_opts['x'], boundaries=edges[0]), histogram.axis(volume_opts['y'], boundaries=edges[1]), histogram.axis(volume_opts['z'], boundaries=edges[2]), ] H = mparr2nparr(shared_H) sa = mparr2nparr(shared_sa) H.shape = sa.shape = shape h = histogram.histogram('I(%(x)s,%(y)s,%(z)s)' % volume_opts, axes=axes, data=H / sa) import histogram.hdf as hh hh.dump(h, output) return
def test_getattribute(self): 'Histogram: __getattribute__' from histogram import histogram, axis, arange x = axis( 'x', arange(1., 10., 1.) ) y = axis( 'y', arange(-1., 1., 0.1) ) h = histogram('h', (x,y) ) self.assertVectorEqual( h.x, x.binCenters() ) self.assertVectorEqual( h.y, y.binCenters() ) t1 = h.I; t2 = h.data().storage().asNumarray() t1.shape = t2.shape = -1, self.assertVectorEqual( t1, t2 ) t1 = h.E2; t2 = h.errors().storage().asNumarray() t1.shape = t2.shape = -1, self.assertVectorEqual( t1, t2 ) return
def test_changeUnit(self): 'Axis: changeUnit' taxis = axis('t', list(range(3)), unit='second') taxis.changeUnit('millisecond') self.assertVectorAlmostEqual(taxis.binBoundaries().asNumarray(), [-500, 500, 1500, 2500]) self.assertVectorAlmostEqual(taxis.binCenters(), [0, 1000, 2000]) self.assertEqual(taxis.cellIndexFromValue(0), 0) return
def run(scan=None, poolsize=None, output=None, **volume_opts): scan = load_mod(scan)['scan'] def worker(work_q, H, sa): while not work_q.empty(): f = work_q.get() print f one(f, H, sa, scan, volume_opts) continue return shape, edges = scan.volumeOutputDims(**volume_opts) size = shape[0] * shape[1] * shape[2] from multiprocessing import Process, Queue, Array shared_H = Array('d', size) shared_sa = Array('d', size) work_q = Queue() for path in scan.paths: work_q.put(path) processes = [] for w in xrange(poolsize): p = Process(target=worker, args=(work_q, shared_H, shared_sa)) p.start() processes.append(p) continue for p in processes: p.join() import histogram axes = [ histogram.axis(volume_opts['x'], boundaries=edges[0]), histogram.axis(volume_opts['y'], boundaries=edges[1]), histogram.axis(volume_opts['z'], boundaries=edges[2]), ] H = mparr2nparr(shared_H) sa = mparr2nparr(shared_sa) H.shape = sa.shape = shape h = histogram.histogram( 'I(%(x)s,%(y)s,%(z)s)'%volume_opts, axes=axes, data=H/sa) import histogram.hdf as hh hh.dump(h, output) return
def test_changeUnit(self): 'Axis: changeUnit' taxis= axis('t', range(3), unit='second' ) taxis.changeUnit( 'millisecond' ) self.assertVectorAlmostEqual( taxis.binBoundaries().asNumarray(), [-500, 500, 1500, 2500] ) self.assertVectorAlmostEqual( taxis.binCenters(), [0, 1000, 2000] ) self.assertEqual( taxis.cellIndexFromValue( 0 ), 0 ) return
def get_histogram( monitor ): from mcstas2.utils.carray import bpptr2npyarr core = monitor.core() n = core.nchan Iarr = bpptr2npyarr( core.getTOF_p( ), 'double', n ).copy() E2arr = bpptr2npyarr( core.getTOF_p2( ), 'double', n ).copy() from histogram import histogram, axis, arange dt = (core.tmax-core.tmin)/core.nchan taxis = axis( 'tof', arange( core.tmin+dt/2., core.tmax+dt/2-0.1*dt, dt ), unit = 's' ) h = histogram( 'I(tof)', [taxis], data = Iarr, errors = E2arr ) return h
def process(path): basename, ext = os.path.splitext(os.path.basename(path)) from nslice.Run import Run run = Run(path) print "instrument=%s, Ei=%s, psi=%s" % (run.instrument, run.Ei, run.psi) from nslice.XtalOrientation import XtalOrientation a = b = 8.87 c = 5.2 from math import pi twopi = 2 * pi ra, rb, rc = [twopi / a, 0, 0], [0, twopi / b, 0], [0, 0, twopi / c] u, v = [1, 0, 0], [0, 1, 0] xtal_ori = XtalOrientation(ra, rb, rc, u, v, run.psi) h, k, l, E = run.compute_hklE(xtal_ori) I, error = run.read_data() h.shape = k.shape = l.shape = E.shape = I.shape = error.shape = -1 hklEIE = np.vstack((h, k, l, E, I, error)) from nslice.slice import slice H, edges = slice( hklEIE, x='h', y='k', u='l', v='E', E=[-2, 2], l=[-5, 5], h=(-5, 5, 0.02), k=(-5, 5, 0.02), ) import histogram, histogram.hdf as hh axes = [ histogram.axis('h', boundaries=edges[0]), histogram.axis('k', boundaries=edges[1]), ] h = histogram.histogram('I(h,k)', axes=axes, data=H) hh.dump(h, '%s-I_hk.h5' % basename) return
def sqehist(E, g, **kwds): """a simple wrapper of method sqe to return a histogram Please see method sqe for details of all keyword parameters Parameters ---------- E:numpy array of floats energies in meV g:numpy array of floats density of states at the specified energies """ Q, E, S = sqe(E, g, **kwds) import histogram as H Qaxis = H.axis('Q', Q, '1./angstrom') Eaxis = H.axis('E', E, 'meV') return H.histogram("SP SQE", [Qaxis, Eaxis], S)
def get_histogram(monitor): from mcstas2.utils.carray import bpptr2npyarr core = monitor.core() nQ = core.nQ nE = core.nE n = nQ * nE shape = nQ, nE Iarr = bpptr2npyarr(core.getIQE_p(), 'double', n).copy() E2arr = bpptr2npyarr(core.getIQE_p2(), 'double', n).copy() Iarr.shape = E2arr.shape = shape from histogram import histogram, axis, arange dE = (core.Emax - core.Emin) / nE Eaxis = axis('energy', arange(core.Emin, core.Emax, dE), unit='meV') dQ = (core.Qmax - core.Qmin) / nQ Qaxis = axis('Q', arange(core.Qmin, core.Qmax, dQ), unit='angstrom**-1') h = histogram('I(Q,E)', [Qaxis, Eaxis], data=Iarr, errors=E2arr) return h
def get_histogram(monitor): from mcstas2.utils.carray import bpptr2npyarr core = monitor.core() n = core.nchan Iarr = bpptr2npyarr(core.getE_p(), 'double', n).copy() E2arr = bpptr2npyarr(core.getE_p2(), 'double', n).copy() from histogram import histogram, axis, arange dE = (core.Emax - core.Emin) / core.nchan Eaxis = axis('energy', arange(core.Emin + dE / 2, core.Emax, dE), unit='meV') h = histogram('I(E)', [Eaxis], data=Iarr, errors=E2arr) return h
def get_histogram(monitor): from mcstas2.utils.carray import bpptr2npyarr core = monitor.core() n = core.nchan Iarr = bpptr2npyarr(core.getL_p(), 'double', n).copy() E2arr = bpptr2npyarr(core.getL_p2(), 'double', n).copy() from histogram import histogram, axis, arange dL = (core.Lmax - core.Lmin) / core.nchan Laxis = axis('wavelength', arange(core.Lmin + dL / 2, core.Lmax + dL / 2, dL), unit='angstrom') h = histogram('I(L)', [Laxis], data=Iarr, errors=E2arr) return h
def get_histogram(monitor): from mcstas2.utils.carray import bpptr2npyarr core = monitor.core() n = core.nchan Iarr = bpptr2npyarr(core.getTOF_p(), 'double', n).copy() E2arr = bpptr2npyarr(core.getTOF_p2(), 'double', n).copy() from histogram import histogram, axis, arange dt = (core.tmax - core.tmin) / core.nchan taxis = axis('tof', arange(core.tmin + dt / 2., core.tmax + dt / 2 - 0.1 * dt, dt), unit='s') h = histogram('I(tof)', [taxis], data=Iarr, errors=E2arr) return h
def readSQE( datapath, Q = 'Q', E = 'E', Sqe = 'Sqe' ): 'read idf Q,E,Sqe and construct a S(Q,E) histogram' qpath = os.path.join( datapath, Q ) import Q q = Q.read( qpath)[1] q.shape = q.size, epath = os.path.join( datapath, E ) import E e = E.read( epath)[1] e.shape = e.size, sqepath = os.path.join( datapath, Sqe ) import Sqe s = Sqe.read( sqepath)[1] import histogram as H qaxis = H.axis( 'Q', boundaries = q ) eaxis = H.axis( 'energy', boundaries = e ) return H.histogram( 'S(Q,E)', [qaxis, eaxis], data = s )
def test1(self): "multiphonon.forward.dos2sqe" from dos import loadDOS E, g = loadDOS() Eaxis = H.axis('E', unit='meV', centers=E) doshist = H.histogram('DOS', [Eaxis], g) dE = E[1] - E[0] iqe = hh.load(os.path.join(datadir, 'V-iqe.h5')) from multiphonon.sqe import interp newiqe = interp(iqe, newE=np.arange(iqe.energy[0], 80., dE)) hh.dump(newiqe, 'V-iqe-interped.h5') from multiphonon.forward import dos2sqe sqe = dos2sqe(doshist, 0.01, newiqe, 300, 50.94, 120.) return