Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_model(self, f=None):
self.read_parameters()
fit = Fit(self.data, self.functions)
if self.plot_checkbox.isChecked():
x = fit.x
else:
xmin, xmax = self.get_limits()
x = np.linspace(xmin, xmax, 1001)
return NXdata(NXfield(fit.get_model(x, f), name='model'),
NXfield(x, name=fit.data.nxaxes[0].nxname),
title='Fit Results')
if len(scan.G) > 0:
entry.G = NXlog()
desc = "SPEC geometry arrays, meanings defined by SPEC diffractometer support"
# e.g.: SPECD/four.mac
# http://certif.com/spec_manual/fourc_4_9.html
entry.G.attrs['description'] = desc
for item, value in scan.G.items():
entry.G[item] = NXfield(list(map(float, value.split())))
if scan.T != '':
entry['counting_basis'] = NXfield('SPEC scan with constant counting time')
entry['T'] = NXfield(float(scan.T))
entry['T'].units = 'seconds'
entry['T'].description = 'SPEC scan with constant counting time'
elif scan.M != '':
entry['counting_basis'] = NXfield('SPEC scan with constant monitor count')
entry['M'] = NXfield(float(scan.M))
entry['M'].units = 'counts'
entry['M'].description = 'SPEC scan with constant monitor count'
if scan.Q != '':
entry['Q'] = NXfield(list(map(float,scan.Q)))
entry['Q'].description = 'hkl at start of scan'
root['scan_' + str(key)] = entry
self.progress_bar.setValue(int(key))
self.update_progress()
return root
def fix_spe(spe_file):
entry = NXentry()
entry.title = spe_file.nxname
entry.incident_energy = spe_file['data/NXSPE_info/fixed_energy']
entry.data = spe_file.data.data
entry.data.error.rename('errors')
s = raw_input("Emin Emax Phimin Phimax dPhi: ")
xmin, xmax, ymin, ymax, dy = [float(i) for i in s.split(' ')]
mfit(entry.data, xmin, xmax, ymin, ymax, dy)
return entry
scan.interpret()
entry = NXentry()
entry.title = str(scan)
entry.date = utils.iso8601(scan.date)
entry.command = scan.scanCmd
entry.scan_number = NXfield(scan.scanNum)
entry.comments = '\n'.join(scan.comments)
entry.data = self.scan_NXdata(scan) # store the scan data
entry.positioners = self.metadata_NXlog(scan.positioner,
'SPEC positioners (#P & #O lines)')
if hasattr(scan, 'metadata') and len(scan.metadata) > 0:
entry.metadata = self.metadata_NXlog(scan.metadata,
'SPEC metadata (UNICAT-style #H & #V lines)')
if len(scan.G) > 0:
entry.G = NXlog()
desc = "SPEC geometry arrays, meanings defined by SPEC diffractometer support"
# e.g.: SPECD/four.mac
# http://certif.com/spec_manual/fourc_4_9.html
entry.G.attrs['description'] = desc
for item, value in scan.G.items():
entry.G[item] = NXfield(list(map(float, value.split())))
if scan.T != '':
entry['counting_basis'] = NXfield('SPEC scan with constant counting time')
entry['T'] = NXfield(float(scan.T))
entry['T'].units = 'seconds'
entry['T'].description = 'SPEC scan with constant counting time'
elif scan.M != '':
entry['counting_basis'] = NXfield('SPEC scan with constant monitor count')
entry['M'] = NXfield(float(scan.M))
entry['M'].units = 'counts'
entry['M'].description = 'SPEC scan with constant monitor count'
return '\n'.join(tree.split('\n')[0:50])+'\n...'
else:
return tree
elif role == QtCore.Qt.DecorationRole:
if isinstance(self.node, NXroot):
if self.node.nxfilemode == 'r':
if self.node._file_modified:
return self._locked_modified
else:
return self._locked
elif self.node.nxfilemode == 'rw':
if self.node._file_modified:
return self._unlocked_modified
else:
return self._unlocked
elif isinstance(self.node, NXlink):
return self._linked
else:
return None
def save_fit(self):
"""Saves fit results to an NXentry"""
self.read_parameters()
group = NXprocess()
group['data'] = self.data
for f in self.functions:
group[f.name] = self.get_model(f)
parameters = NXparameters()
for p in f.parameters:
parameters[p.name] = NXfield(p.value, error=p.stderr,
initial_value=p.init_value,
min=str(p.min), max=str(p.max))
group[f.name].insert(parameters)
if self.fit is not None:
group['program'] = 'lmfit'
group['version'] = lmfit.__version__
group['title'] = 'Fit Results'
group['fit'] = self.get_model()
fit = NXparameters()
fit.nfev = self.fit.result.nfev
fit.chisq = self.fit.result.chisqr
fit.redchi = self.fit.result.redchi
fit.message = self.fit.result.message
group['statistics'] = fit
def scan_NXdata(self, scan):
'''
return the scan data in an NXdata object
'''
nxdata = NXdata()
if len(scan.data) == 0: # what if no data?
# since no data available, provide trivial, fake data
# this keeps the NXdata base class compliant with the NeXus standard
nxdata.attrs['description'] = 'SPEC scan has no data'
nxdata['noSpecData_y'] = NXfield([0, 0]) # primary Y axis
nxdata['noSpecData_x'] = NXfield([0, 0]) # primary X axis
nxdata.nxsignal = nxdata['noSpecData_y']
nxdata.nxaxes = [nxdata['noSpecData_x'], ]
return nxdata
nxdata.attrs['description'] = 'SPEC scan data'
scan_type = scan.scanCmd.split()[0]
if scan_type in ('mesh', 'hklmesh'):
# hklmesh H 1.9 2.1 100 K 1.9 2.1 100 -800000
def save_fit(self):
"""Saves fit results to an NXentry"""
self.read_parameters()
group = NXprocess()
group['data'] = self.data
for f in self.functions:
group[f.name] = self.get_model(f)
parameters = NXparameters()
for p in f.parameters:
parameters[p.name] = NXfield(p.value, error=p.stderr,
initial_value=p.init_value,
min=str(p.min), max=str(p.max))
group[f.name].insert(parameters)
if self.fit is not None:
group['program'] = 'lmfit'
group['version'] = lmfit.__version__
group['title'] = 'Fit Results'
group['fit'] = self.get_model()
fit = NXparameters()
fit.nfev = self.fit.result.nfev
#histogram and normalize
norm, nbin = np.histogramdd((Ein,Qin), bins=(Eb,Qb))
hist, hbin = np.histogramdd((Ein,Qin), bins=(Eb,Qb), weights=datain)
if self.entry['data'].nxerrors:
histe, hbin = np.histogramdd((Ein,Qin), bins=(Eb,Qb), weights=errorsin*errorsin)
histe = histe**0.5
err = histe/norm
I = NXfield(hist/norm, name='S(Q,E)')
Qb = NXfield(Qb[:-1]+dQ/2., name='Q')
Eb = NXfield(Eb[:-1]+dE/2., name='E')
result = NXdata(I, (Eb, Qb))
if self.entry.data.nxerrors:
result.errors = NXfield(err)
return result
qmax = Q.max()
emin = E.min()
emax = E.max()
NQ = int((qmax-qmin)/dQ) + 1
NE = int((emax-emin)/dE) + 1
Qb = np.linspace(qmin, qmax, NQ)
Eb = np.linspace(emin, emax, NE)
#histogram and normalize
norm, nbin = np.histogramdd((Ein,Qin), bins=(Eb,Qb))
hist, hbin = np.histogramdd((Ein,Qin), bins=(Eb,Qb), weights=datain)
if self.entry['data'].nxerrors:
histe, hbin = np.histogramdd((Ein,Qin), bins=(Eb,Qb), weights=errorsin*errorsin)
histe = histe**0.5
err = histe/norm
I = NXfield(hist/norm, name='S(Q,E)')
Qb = NXfield(Qb[:-1]+dQ/2., name='Q')
Eb = NXfield(Eb[:-1]+dE/2., name='E')
result = NXdata(I, (Eb, Qb))
if self.entry.data.nxerrors:
result.errors = NXfield(err)
return result