def run(mpfile, **kwargs): input_dir = mpfile.hdata['_hdata']['input_dir'] identifier = get_composition_from_string('PbZr20Ti80O3') print identifier # 'SP128_NSO_LPFM0000.ibw' too big to display in notebook files = ['BR_60016 (1).ibw', 'SP128_NSO_VPFM0000.ibw'] for f in files: file_name = os.path.join(input_dir, f) df = load_data(file_name) name = f.split('.')[0] mpfile.add_data_table(identifier, df, name) print 'imported', f xrd_file = os.path.join(input_dir, 'Program6_JA_6_2th0m Near SRO (002)_2.xrdml.xml') data = read_xrdml(xrd_file) df = DataFrame(np.stack((data['2Theta'],data['data']),1), columns=['2Theta','Intensity']) opts = {'yaxis': {'type': 'log'}} # see plotly docs mpfile.add_data_table(identifier, df, 'NearSRO', plot_options=opts) print 'imported', os.path.basename(xrd_file) rsm_file = os.path.join(input_dir, 'JA 42 RSM 103 STO 001.xrdml.xml') rvals, df = load_RSM(rsm_file) mpfile.add_hierarchical_data({'rsm_range': { 'x': '{} {}'.format(rvals[0], rvals[1]), 'y': '{} {}'.format(rvals[2], rvals[3]), }}, identifier=identifier) mpfile.add_data_table(identifier, df, 'RSM') print 'imported', os.path.basename(rsm_file)
def xrdml_file(self, file): data = xrdtools.read_xrdml(file) #data['Omega'] = data['Omega'][:,0] #data['2Theta'] = data['2Theta'][0] #om, tt = np.meshgrid(data['Omega'], data['2Theta']) om = data['Omega'] tt = data['2Theta'] return (np.transpose(om), np.transpose(tt), np.transpose(data['data']))
def xrdml(): """Command line tool to export measurement data from xrdml files. Allowed keyword arguments: -------------------------- -o, --output : str Choices: 'stdout', 'txt' [default: 'txt'] --delimiter : str Default: '\t' --fmt : str Default: '%.18e' """ parser = ArgumentParser('Export measurement data for xrdml files.') parser.add_argument('filenames', metavar='filenames', type=str, nargs='+', help='filenames for which to export the data') parser.add_argument('-o', '--output', metavar='output', choices=['stdout', 'txt'], default='txt', help='the format to which the data should be exported') parser.add_argument('--delimiter', metavar='delimiter', type=str, default='\t', help='define a delimiter') parser.add_argument('--fmt', metavar='fmt', type=str, default='%.18e', help='define the output format') args = parser.parse_args() for filename in args.filenames: data = xrdtools.read_xrdml(filename) output = np.array([]) labels = [] if data['measType'] == 'Scan': output = np.vstack([data['x'], data['data']]) labels = [data.get('xlabel', ''), 'Intensity'] elif data['measType'] == 'Area measurement': output = np.vstack([data['2Theta'].ravel(), data['Omega'].ravel(), data['data'].ravel()]) labels = [data.get('xlabel', ''), data.get('ylabel', ''), 'Intensity'] else: print('Measurement type is not supported.') continue if args.output == 'txt': file_out = filename.replace('.xrdml', '.txt') elif args.output == 'stdout': file_out = sys.stdout delimiter = args.delimiter.decode('string-escape') fmt = args.fmt np.savetxt(file_out, output.T, fmt=fmt, delimiter=delimiter, header=delimiter.join(labels))
def test_read_xrdml_area(self): filename = os.path.abspath('tests/test_area.xrdml') data = read_xrdml(filename) self.assertEqual(data['comment'], {'1': ''}) self.assertEqual(data['kAlpha1'], 1.540598) self.assertEqual(data['kAlpha2'], 1.544426) self.assertEqual(data['kBeta'], 1.39225) self.assertEqual(data['kAlphaRatio'], 0.5) self.assertEqual(data['kType'], 'K-Alpha 1') self.assertEqual(data['Lambda'], 1.540598) self.assertEqual(data['slitHeight'], 0.38) self.assertEqual(data['scanAxis'], 'Omega-2Theta') self.assertEqual(data['stepAxis'], 'Omega') self.assertEqual(data['time'], [3.]) self.assertEqual(data['sample'], 'B11091') self.assertEqual(data['substrate'], 'SrTiO3') self.assertEqual(data['hkl'], {'h': 0, 'k': 1, 'l': 3}) self.assertEqual(data['2Theta'].size, 5700) self.assertEqual(data['Omega'].size, 5700) self.assertEqual(data['data'].size, 5700) self.assertEqual(data['Phi'], [0.]) self.assertEqual(data['Psi'], [-0.4]) self.assertEqual(data['X'], [2.37]) self.assertEqual(data['Y'], [-0.98]) self.assertEqual(data['Z'], [9.695]) self.assertEqual(data['yunit'], 'deg') self.assertEqual(data['xunit'], 'deg') self.assertEqual(data['xlabel'], 'Omega-2Theta') self.assertEqual(data['ylabel'], 'Omega') self.assertEqual(data['status'], 'Completed') self.assertEqual(data['measType'], 'Area measurement') keys = [ 'comment', 'kAlpha1', 'kAlpha2', 'kType', 'sample', 'slitHeight', 'stepAxis', 'X', 'Phi', 'Psi', 'yunit', 'filename', 'xunit', 'xlabel', 'Omega', 'status', 'ylabel', '2Theta', 'scannb', 'hkl', 'Y', 'Z', 'data', 'measType', 'Lambda', 'scanAxis', 'substrate', 'kBeta', 'time', 'kAlphaRatio' ] for key in keys: self.assertIn(key, data.keys())
def test_read_xrdml_area(self): filename = os.path.abspath('tests/test_area.xrdml') data = read_xrdml(filename) self.assertEqual(data['comment'], {'1': ''}) self.assertEqual(data['kAlpha1'], 1.540598) self.assertEqual(data['kAlpha2'], 1.544426) self.assertEqual(data['kBeta'], 1.39225) self.assertEqual(data['kAlphaRatio'], 0.5) self.assertEqual(data['kType'], 'K-Alpha 1') self.assertEqual(data['Lambda'], 1.540598) self.assertEqual(data['slitHeight'], 0.38) self.assertEqual(data['scanAxis'], 'Omega-2Theta') self.assertEqual(data['stepAxis'], 'Omega') self.assertEqual(data['time'], [3.]) self.assertEqual(data['sample'], 'B11091') self.assertEqual(data['substrate'], 'SrTiO3') self.assertEqual(data['hkl'], {'h': 0, 'k': 1, 'l': 3}) self.assertEqual(data['2Theta'].size, 5700) self.assertEqual(data['Omega'].size, 5700) self.assertEqual(data['data'].size, 5700) self.assertEqual(data['Phi'], [0.]) self.assertEqual(data['Psi'], [-0.4]) self.assertEqual(data['X'], [2.37]) self.assertEqual(data['Y'], [-0.98]) self.assertEqual(data['Z'], [9.695]) self.assertEqual(data['yunit'], 'deg') self.assertEqual(data['xunit'], 'deg') self.assertEqual(data['xlabel'], 'Omega-2Theta') self.assertEqual(data['ylabel'], 'Omega') self.assertEqual(data['status'], 'Completed') self.assertEqual(data['measType'], 'Area measurement') keys = ['comment', 'kAlpha1', 'kAlpha2', 'kType', 'sample', 'slitHeight', 'stepAxis', 'X', 'Phi', 'Psi', 'yunit', 'filename', 'xunit', 'xlabel', 'Omega', 'status', 'ylabel', '2Theta', 'scannb', 'hkl', 'Y', 'Z', 'data', 'measType', 'Lambda', 'scanAxis', 'substrate', 'kBeta', 'time', 'kAlphaRatio'] for key in keys: self.assertIn(key, data.keys())
def test_read_xrdml_scan(self): filename = os.path.abspath('tests/test_scan.xrdml') data = read_xrdml(filename) self.assertEqual(data['comment'], {'1': ''}) self.assertEqual(data['kAlpha1'], 1.540598) self.assertEqual(data['kAlpha2'], 1.544426) self.assertEqual(data['kBeta'], 1.39225) self.assertEqual(data['kAlphaRatio'], 0.0) self.assertEqual(data['kType'], 'K-Alpha 1') self.assertEqual(data['Lambda'], 1.540598) self.assertEqual(data['slitHeight'], 0.19) self.assertEqual(data['scanAxis'], '2Theta-Omega') self.assertEqual(data['time'].size, 750) self.assertEqual(data['sample'], 'B10135') self.assertEqual(data['x'].size, 750) self.assertEqual(data['2Theta'].size, 750) self.assertEqual(data['Omega'].size, 750) self.assertEqual(data['Phi'], 0.0) self.assertEqual(data['Psi'], -0.6) self.assertEqual(data['Y'], 0.26) self.assertEqual(data['X'], 1.17) self.assertEqual(data['Z'], 9.607) self.assertEqual(data['data'].size, 750) self.assertEqual(data['xunit'], 'deg') self.assertEqual(data['xlabel'], '2Theta-Omega') self.assertEqual(data['status'], 'Completed') self.assertEqual(data['scannb'], [0]) self.assertEqual(data['measType'], 'Scan') keys = [ 'comment', 'kAlpha1', 'kAlpha2', 'kType', 'sample', 'slitHeight', 'Phi', 'Psi', 'filename', 'xunit', 'xlabel', 'Omega', 'status', '2Theta', 'scannb', 'Y', 'X', 'Z', 'data', 'measType', 'Lambda', 'scanAxis', 'kBeta', 'time', 'x', 'kAlphaRatio' ] for key in keys: self.assertIn(key, data.keys())
def run(mpfile, **kwargs): input_dir = mpfile.hdata["_hdata"]["input_dir"] identifier = get_composition_from_string("PbZr20Ti80O3") print identifier # 'SP128_NSO_LPFM0000.ibw' too big to display in notebook files = ["BR_60016 (1).ibw", "SP128_NSO_VPFM0000.ibw"] for f in files: file_name = os.path.join(input_dir, f) df = load_data(file_name) name = f.split(".")[0] mpfile.add_data_table(identifier, df, name) print "imported", f xrd_file = os.path.join(input_dir, "Program6_JA_6_2th0m Near SRO (002)_2.xrdml.xml") data = read_xrdml(xrd_file) df = DataFrame( np.stack((data["2Theta"], data["data"]), 1), columns=["2Theta", "Intensity"] ) opts = {"yaxis": {"type": "log"}} # see plotly docs mpfile.add_data_table(identifier, df, "NearSRO", plot_options=opts) print "imported", os.path.basename(xrd_file) rsm_file = os.path.join(input_dir, "JA 42 RSM 103 STO 001.xrdml.xml") rvals, df = load_RSM(rsm_file) mpfile.add_hierarchical_data( { "rsm_range": { "x": "{} {}".format(rvals[0], rvals[1]), "y": "{} {}".format(rvals[2], rvals[3]), } }, identifier=identifier, ) mpfile.add_data_table(identifier, df, "RSM") print "imported", os.path.basename(rsm_file)
def run(mpfile, **kwargs): input_dir = mpfile.hdata['_hdata']['input_dir'] identifier = get_composition_from_string('PbZr20Ti80O3') print identifier # 'SP128_NSO_LPFM0000.ibw' too big to display in notebook files = ['BR_60016 (1).ibw', 'SP128_NSO_VPFM0000.ibw'] for f in files: file_name = os.path.join(input_dir, f) df = load_data(file_name) name = f.split('.')[0] mpfile.add_data_table(identifier, df, name) print 'imported', f xrd_file = os.path.join(input_dir, 'Program6_JA_6_2th0m Near SRO (002)_2.xrdml.xml') data = read_xrdml(xrd_file) df = DataFrame(np.stack((data['2Theta'], data['data']), 1), columns=['2Theta', 'Intensity']) opts = {'yaxis': {'type': 'log'}} # see plotly docs mpfile.add_data_table(identifier, df, 'NearSRO', plot_options=opts) print 'imported', os.path.basename(xrd_file) rsm_file = os.path.join(input_dir, 'JA 42 RSM 103 STO 001.xrdml.xml') rvals, df = load_RSM(rsm_file) mpfile.add_hierarchical_data( { 'rsm_range': { 'x': '{} {}'.format(rvals[0], rvals[1]), 'y': '{} {}'.format(rvals[2], rvals[3]), } }, identifier=identifier) mpfile.add_data_table(identifier, df, 'RSM') print 'imported', os.path.basename(rsm_file)
def xrdml(): """Command line tool to export measurement data from xrdml files. Allowed keyword arguments: -------------------------- -o, --output : str Choices: 'stdout', 'txt' [default: 'txt'] --delimiter : str Default: '\t' --fmt : str Default: '%.18e' """ parser = ArgumentParser('Export measurement data for xrdml files.') parser.add_argument('filenames', metavar='filenames', type=str, nargs='+', help='filenames for which to export the data') parser.add_argument('-o', '--output', metavar='output', choices=['stdout', 'txt'], default='txt', help='the format to which the data should be exported') parser.add_argument('--delimiter', metavar='delimiter', type=str, default='\t', help='define a delimiter') parser.add_argument('--fmt', metavar='fmt', type=str, default='%.18e', help='define the output format') args = parser.parse_args() for filename in args.filenames: data = xrdtools.read_xrdml(filename) output = np.array([]) labels = [] if data['measType'] == 'Scan': output = np.vstack([data['x'], data['data']]) labels = [data.get('xlabel', ''), 'Intensity'] elif data['measType'] == 'Area measurement': output = np.vstack([ data['2Theta'].ravel(), data['Omega'].ravel(), data['data'].ravel() ]) labels = [ data.get('xlabel', ''), data.get('ylabel', ''), 'Intensity' ] else: print('Measurement type is not supported.') continue if args.output == 'txt': file_out = filename.replace('.xrdml', '.txt') elif args.output == 'stdout': file_out = sys.stdout delimiter = args.delimiter.decode('string-escape') fmt = args.fmt np.savetxt(file_out, output.T, fmt=fmt, delimiter=delimiter, header=delimiter.join(labels))
def readXRD(sample_scrap_dir): """ -Takes location of local copy of data and extracts python-readable data from XRDML files -Had to create dummy variable correct_items to remove unwanted files first -Correct list is reassigned to scrap_contents after """ scrap_contents = os.listdir(sample_scrap_dir) # Check for desired filetype, if not move to junk folder for item in scrap_contents: if (os.path.isfile(sample_scrap_dir + item) == True and item.lower().endswith(".xrdml") == False): shutil.move(sample_scrap_dir + item, sample_scrap_dir + "junk/") else: pass # Update scrap folder contents after cleaning scrap_contents = os.listdir(sample_scrap_dir) """ This section: -Iterate over files we want to extract data from now -Clean filenames to make easier descriptors later -Raw data is placed into scrap/*sample*/raw -Data is spat out into csv files in scrap/*sample*/py_data """ # Initialize scan_data dict that will contain key-assigned data that is later fed into pandas DataFrame scan_data = {} # List useless XRD scan names that need to be cleaned useless_exts = [" Tomas_quick gonio scan_1", " Tomas_long gonio scan_2"] #Clean XRDML filenames to make plot labeling easier later for item in scrap_contents: for ext in useless_exts: if ext in item: newname = item.replace(ext, "") os.rename(sample_scrap_dir + item, sample_scrap_dir + newname) # Update scrap folder contents after cleaning filenames scrap_contents = os.listdir(sample_scrap_dir) # Read all details extracted by xrdtools into scan_data dict for item in scrap_contents: if (os.path.isfile(sample_scrap_dir + item) == True and item.startswith("CIF") != True): scan_data[item] = xrdtools.read_xrdml(sample_scrap_dir + item) shutil.move(sample_scrap_dir + item, sample_scrap_dir + "raw/") else: pass clean_key_data = {} for key in scan_data.keys(): clean_key_data[key.replace(".xrdml", "")] = scan_data[key] """ This section: -Takes dict data and outputs .csv files -Returns a dict of pandas DataFrames """ # Dict of pandas DataFrames of entire sample pool indexed by clean keys meta_export_table = {} for key in clean_key_data.keys(): export_table = pd.DataFrame(data=None, columns=["2theta", "counts"]) export_table["2theta"] = clean_key_data[key]["x"] export_table["counts"] = clean_key_data[key]["data"] export_table.to_csv(os.path.join(sample_scrap_dir, "py_data/") + key + ".csv", index=False) meta_export_table[key] = export_table del export_table return meta_export_table
pfRes = 5 expAlphaMax = (80/5)+1 hkl = [] hkl.append([1,1,1]) hkl.append([2,0,0]) hkl.append([2,2,0]) symHKL = [genSymHKL(h,None) for h in hkl] ### computation ### blockPrint() expPFs = {} expPFs[0] = xrdtools.read_xrdml(pf111path) expPFs[1] = xrdtools.read_xrdml(pf200path) expPFs[2] = xrdtools.read_xrdml(pf220path) enablePrint() phi1 = np.deg2rad(np.arange(0,phi1max+odRes,odRes)) Phi = np.deg2rad(np.arange(0,PhiMax+odRes,odRes)) phi2 = np.deg2rad(np.arange(0,phi2max+odRes,odRes)) alpha = np.deg2rad(np.arange(0,alphaMax+pfRes,pfRes)) beta = np.deg2rad(np.arange(2.5,betaMax+pfRes,pfRes)) pfRes = np.deg2rad(pfRes) #for later use during computation odRes = np.deg2rad(odRes) [phi1,Phi,phi2] = np.meshgrid(phi1,Phi,phi2,indexing='ij')
def __init__(self, files, hkls, cs, pf_type, subtype=None, names=None, resolution=None): """ list of file names list of (hkl) poles type of pole fig 'xrdml' 'nd' optional: names (list str) optional: resolution (int) """ if pf_type == 'xrdml': self.data = {} self.hkl = hkls self.twotheta = {} self._numHKL = len(hkls) for i, (f, h) in enumerate(zip(files, hkls)): if f.endswith('xrdml') and os.path.exists(f): _blockPrint() temp = xrdtools.read_xrdml(f) _enablePrint() if subtype == 'bkgd': self.data[i] = temp['data'] self.twotheta[i] = temp['2Theta'] self.subtype = 'bkgd' continue elif subtype == 'defocus': self.data[i] = temp['data'] self.twotheta[i] = temp['2Theta'] self.subtype = 'defocus' continue self.data[i] = temp['data'] self.twotheta[i] = temp['2Theta'] self.subtype = 'poleFig' #assuming equal grid spacing if resolution is None: self.res = np.diff( temp['Phi'], 1)[0] #pull diff from first 2 points else: self.res = resolution #polar self._alphasteps = np.arange( 0, temp['data'].shape[0] * self.res, self.res) #azimuth self._betasteps = np.arange( 2.5, temp['data'].shape[1] * self.res, self.res) #grids self.alpha, self.beta = np.meshgrid(self._alphasteps, self._betasteps, indexing='ij') #convert to rad self.alpha = np.deg2rad(self.alpha) self.beta = np.deg2rad(self.beta) self.res = np.deg2rad(self.res) self.cellVolume = self.res * (np.cos(self.alpha - (self.res / 2)) - np.cos(self.alpha + (self.res / 2))) else: raise TypeError( 'file type not recognized/found, must be ".xrdml"') self.symHKL = symmetrise(cs, hkls) self.symHKL = normalize(self.symHKL) elif pf_type == 'nd': self.data = {} self.y = {} self.y_pol = {} self.hkl = hkls self._numHKL = len(hkls) for i, (f, h) in enumerate(zip(files, hkls)): if f.endswith('.pf') and os.path.exists(f): """ output from pyReducePF """ temp = np.genfromtxt(f) self.data[i] = temp[:, 3] self.y[i] = temp[:, :3] self.y_pol[i] = XYZtoSPH(self.y[i]) self.subtype = 'nd_poleFig' self.symHKL = symmetrise(cs, hkls) self.symHKL = normalize(self.symHKL) elif pf_type == 'recalc': self.data = {} self.hkl = {} self._numHKL = len(hkls) for i, h in enumerate(hkls): #trick to reuse variable... may not be clear self.data[i] = files[:, :, i] self.hkl[i] = h self.subtype = 'poleFig' #assuming equal grid spacing if resolution is None: self.res = None else: self.res = resolution #polar self._alphasteps = np.arange(0, files.shape[0] * self.res, self.res) #azimuth self._betasteps = np.arange(2.5, files.shape[1] * self.res, self.res) #grids self.alpha, self.beta = np.meshgrid(self._alphasteps, self._betasteps, indexing='ij') #convert to rad self.alpha = np.deg2rad(self.alpha) self.beta = np.deg2rad(self.beta) self.res = np.deg2rad(self.res) try: self.cellVolume = self.res * (np.cos(self.alpha - (self.res / 2)) - np.cos(self.alpha + (self.res / 2))) except: pass else: raise NotImplementedError('pf type not recognized')
def test_read_xrdml_scan(self): filename = os.path.abspath('tests/test_scan.xrdml') data = read_xrdml(filename) self.assertEqual(data['comment'], {'1': ''}) self.assertEqual(data['kAlpha1'], 1.540598) self.assertEqual(data['kAlpha2'], 1.544426) self.assertEqual(data['kBeta'], 1.39225) self.assertEqual(data['kAlphaRatio'], 0.0) self.assertEqual(data['kType'], 'K-Alpha 1') self.assertEqual(data['Lambda'], 1.540598) self.assertEqual(data['slitHeight'], 0.19) self.assertEqual(data['scanAxis'], '2Theta-Omega') self.assertEqual(data['time'].size, 750) self.assertEqual(data['sample'], 'B10135') self.assertEqual(data['x'].size, 750) self.assertEqual(data['2Theta'].size, 750) self.assertEqual(data['Omega'].size, 750) self.assertEqual(data['Phi'], 0.0) self.assertEqual(data['Psi'], -0.6) self.assertEqual(data['Y'], 0.26) self.assertEqual(data['X'], 1.17) self.assertEqual(data['Z'], 9.607) self.assertEqual(data['data'].size, 750) self.assertEqual(data['xunit'], 'deg') self.assertEqual(data['xlabel'], '2Theta-Omega') self.assertEqual(data['status'], 'Completed') self.assertEqual(data['scannb'], [0]) self.assertEqual(data['measType'], 'Scan') keys = ['comment', 'kAlpha1', 'kAlpha2', 'kType', 'sample', 'slitHeight', 'Phi', 'Psi', 'filename', 'xunit', 'xlabel', 'Omega', 'status', '2Theta', 'scannb', 'Y', 'X', 'Z', 'data', 'measType', 'Lambda', 'scanAxis', 'kBeta', 'time', 'x', 'kAlphaRatio'] for key in keys: self.assertIn(key, data.keys())