def view_graph(request): """ function for viewing graph input: filename which can be retrieved from the DB output: rendered graph as png image. need to think how you can get radiobuttons to display overlays of graphs need to think about user seperate folders or how to save these files/store in db """ filename = 'C:/ftirdb/ftirdb/data/infrared_spectra/ozone.jdx' jcamp_dict = JCAMP_reader(filename) #plt.plot(jcamp_dict['wavelengths'], jcamp_dict['xsec']) #plt.xlabel(jcamp_dict['xunits']) #plt.ylabel(jcamp_dict['yunits']) filename2 = 'C:/ftirdb/ftirdb/data/infrared_spectra/toluene.jdx' jcamp_dict2 = JCAMP_reader(filename2) plt.plot(jcamp_dict2['x'], jcamp_dict2['y'], label='filename2', alpha=0.8, color='blue') plt.savefig('C:/ftirdb/ftirdb/static/fig2.png') plt.plot(jcamp_dict['x'], jcamp_dict['y'], label='filename', alpha=0.7, color='red') #plt.savefig('C:/ftirdb/ftirdb/static/fig.png') #plt.plot(jcamp_dict2['x']-jcamp_dict['x'],jcamp_dict2['y']-jcamp_dict['y'], color = 'green') return {}
def read_jdx(self): x_date = JCAMP_reader(self.file_name)['x'] y_date = JCAMP_reader(self.file_name)['y'] y_unit = JCAMP_reader(self.file_name)['yunits'] if y_unit == 'ABSORBANCE': y_date = 100 / 10**y_date if y_unit == 'TRANSMITTANCE': y_date = y_date return [x_date, y_date]
def add_spectra2list(file,title,molid,list_spectra,t): print('add_spectra2list '+file+" "+title) filename, file_extension = os.path.splitext(file) print('this is a '+file_extension+' file') if file_extension == '.jdx': print('Importing jdx file') jcamp_dict = JCAMP_reader(file) x=np.array(jcamp_dict['x']) if jcamp_dict['yunits'] == 'TRANSMITTANCE': print('Unit is TRANSMITTANCE, changing to absorbance') y = np.array(-np.log(jcamp_dict['y'])) else: print('Unit is '+jcamp_dict['yunits']+', keeping it') y = np.array(jcamp_dict['y']) db.insert(molid,x,y,title) elif file_extension == '.csv': import csv x=[] y=[] with open('G:\Francois\RDDC\LAST_RESULT\TNT_ANHARM\TNT.csv', 'rt') as csvfile: spamreader = csv.reader(csvfile, delimiter=',', quotechar='|') i=0 for row in spamreader: i=i+1 if(i>5): x.append(row[0]) y.append(float(row[1])) db.insert(molid,np.array(x),np.array(y),title) updatespectra(molid,list_spectra) t.destroy()
def read_acqu_pars(fp): """ Read acquisition parameters from JCAMP-DX file :param fp: File or string of fid :type fp: file,str :returns: FID as complexnumpy array :rtype: dict """ acqu_dict = JCAMP_reader(fp) #many parameters have $ prepending #we need to remove them acqu_dict = { re.sub('^\$','',k):v for k,v in acqu_dict.items()} return acqu_dict
def view_graph(request): filename = 'C:/ftirdb/ftirdb/data/infrared_spectra/1-butene.jdx' jcamp_dict = JCAMP_reader(filename) plt.plot(jcamp_dict['x'], jcamp_dict['y']) plt.title(filename) plt.xlabel(jcamp_dict['xunits']) plt.ylabel(jcamp_dict['yunits']) plt.savefig('C:/ftirdb/ftirdb/static/fig.png') return {}
def _readJCAMP(path, spectra, **kwargs): try: first_reader = True file = jdx.jdx_file_reader(path, **kwargs) except Exception: file = JCAMP_reader(path, **kwargs) first_reader = False if(first_reader): if file.size == 1: spectra.addSpectrum(file[0]['x'], file[0]['y']) elif file.size > 1: for spectrum in file: spectra.addSpectrum(spectrum['x'], spectrum['y']) else: spectra.addSpectrum(file['x'], file['y'])
def test_read_uv(self): for root, dirs, files in os.walk("./data/uvvus_spectra"): for filename in files: full_filename = os.path.join(root, filename) jcamp_dict = JCAMP_reader(full_filename) self.assertIsInstance(jcamp_dict['x'], numpy.ndarray) self.assertIsInstance(jcamp_dict['y'], numpy.ndarray) self.assertEqual(len(jcamp_dict['x']), len(jcamp_dict['y'])) self.assertEqual(len(jcamp_dict['x']), jcamp_dict['npoints']) self.assertAlmostEqual(min(jcamp_dict['x']), jcamp_dict['minx']) self.assertAlmostEqual(min(jcamp_dict['y']), jcamp_dict['miny']) self.assertAlmostEqual(max(jcamp_dict['x']), jcamp_dict['maxx']) self.assertAlmostEqual(max(jcamp_dict['y']), jcamp_dict['maxy'])
def test_read_mass(self): for root, dirs, files in os.walk("./data/mass_spectra"): for filename in files: full_filename = os.path.join(root, filename) jcamp_dict = JCAMP_reader(full_filename) self.assertIsInstance(jcamp_dict['x'], numpy.ndarray) self.assertIsInstance(jcamp_dict['y'], numpy.ndarray) self.assertEqual(len(jcamp_dict['x']), len(jcamp_dict['y'])) ## Mass files seem to be more complex, and current parsing fails on ## Some assumptions # self.assertEqual(len(jcamp_dict['x']), jcamp_dict['npoints']) if 'minx' in jcamp_dict: self.assertAlmostEqual(min(jcamp_dict['x']), jcamp_dict['minx']) self.assertAlmostEqual(min(jcamp_dict['y']), jcamp_dict['miny']) self.assertAlmostEqual(max(jcamp_dict['x']), jcamp_dict['maxx']) self.assertAlmostEqual(max(jcamp_dict['y']), jcamp_dict['maxy'])
def test_jcamp_reader_dict(self): filename = './data/infrared_spectra/methane.jdx' jcamp_dict = JCAMP_reader(filename) self.assertEqual(jcamp_dict['origin'], 'DOW CHEMICAL COMPANY') self.assertEqual(jcamp_dict['deltax'], 0.935748) self.assertEqual(jcamp_dict['sampling procedure'], 'TRANSMISSION') self.assertEqual(jcamp_dict['firstx'], 449.47) self.assertEqual(jcamp_dict['firsty'], 0.953) self.assertEqual(jcamp_dict['data type'], 'INFRARED SPECTRUM') self.assertEqual(jcamp_dict['owner'], 'COBLENTZ SOCIETY') self.assertEqual(jcamp_dict['maxx'], 3801.32) self.assertEqual(jcamp_dict['maxy'], 1.03697) self.assertEqual(jcamp_dict['end'], '') self.assertEqual(jcamp_dict['title'], 'METHANE') self.assertEqual(jcamp_dict['lastx'], 3801.32) self.assertEqual(jcamp_dict['state'], 'GAS') self.assertEqual(jcamp_dict['yunits'], 'TRANSMITTANCE') self.assertEqual(jcamp_dict['cas registry no'], '74-82-8') self.assertEqual(jcamp_dict['spectrometer/data system'], 'DOW KBr FOREPRISM') self.assertEqual(jcamp_dict['$nist image'], 'cob8873') self.assertEqual(jcamp_dict['path length'], '5 CM') self.assertEqual( jcamp_dict['data processing'], 'DIGITIZED BY NIST FROM HARD COPY (FROM TWO SEGMENTS)') self.assertEqual(jcamp_dict['date'], 1964) self.assertEqual(jcamp_dict['molform'], 'C H4') self.assertEqual(jcamp_dict['instrument parameters'], 'GRATING CHANGED AT 5.0, 7.5, 15.0 MICRON') self.assertEqual(jcamp_dict['class'], 'COBLENTZ') self.assertEqual(jcamp_dict['xfactor'], 1.0) self.assertEqual(jcamp_dict['partial_pressure'], '150 mmHg') self.assertEqual(jcamp_dict['minx'], 449.47) self.assertEqual(jcamp_dict['xunits'], '1/CM') self.assertEqual(jcamp_dict['source reference'], 'COBLENTZ NO. 8873') self.assertEqual(jcamp_dict['miny'], 0.028) self.assertEqual(jcamp_dict['jcamp-dx'], 4.24) self.assertEqual(jcamp_dict['xydata'], '(X++(Y..Y))') self.assertEqual(jcamp_dict['yfactor'], 1) self.assertEqual(jcamp_dict['resolution'], 4) self.assertEqual(jcamp_dict['$nist source'], 'COBLENTZ')
def test_jcamp_calc_xsec(self): filename = './data/infrared_spectra/methane.jdx' jcamp_dict = JCAMP_reader(filename) JCAMP_calc_xsec(jcamp_dict) self.assertTrue('xsec' in jcamp_dict)
def spectraPage(request): """This page takes a project with project_ID in the URL and returns a page with a dictionary of all the values, it also contains buttons for adding samples and experiments. When page is linked from here the child/parent relationship is created""" if 'form.submitted' in request.params: if request.params['form.submitted'] == 'sample': #retrieve project ID and send to sample page return {'projectForm': 'sample'} else: return {'projectForm': 'experiment'} #next_url = request.route_url('projectPage', pagename=4) #return HTTPFound(location=next_url) else: search = request.matchdict['spectra_ID'] #search = request.params['body'] """ searchspectra = request.dbsession.query(spectra).filter_by(experiment_ID=search).all() spectradic = {} #return the dictionary of all values from the row for u in searchspectra: new = u.__dict__ spectradic.update( new ) """ print(search) ppd = request.dbsession.query(post_processing_and_deposited_spectra ).filter_by(spectra_ID=search).all() depodic = {} for u in ppd: new = u.__dict__ depodic.update(new) print(depodic) print('here') plt.figure(1) plt.tight_layout() filename = pathlib.PureWindowsPath( 'C:/ftirdb/ftirdb/data/infrared_spectra/' + depodic['sample_power_spectrum']) jcamp_dict = JCAMP_reader(filename) plt.plot(jcamp_dict['x'], jcamp_dict['y'], label='filename', alpha=0.7, color='blue') plt.xlabel(jcamp_dict['xunits']) plt.ylabel(jcamp_dict['yunits']) plt.savefig(pathlib.PureWindowsPath('C:/ftirdb/ftirdb/static/fig.png'), bbox_inches="tight") plt.figure(2) plt.tight_layout() filename2 = pathlib.PureWindowsPath( 'C:/ftirdb/ftirdb/data/infrared_spectra/' + depodic['background_power_spectrum']) jcamp_dict2 = JCAMP_reader(filename2) print(jcamp_dict2['x']) print(jcamp_dict2['xunits']) plt.plot(jcamp_dict2['x'], jcamp_dict2['y'], label='filename', alpha=0.7, color='green') plt.xlabel(jcamp_dict2['xunits']) plt.ylabel(jcamp_dict2['yunits']) plt.savefig( pathlib.PureWindowsPath('C:/ftirdb/ftirdb/static/fig2.png'), bbox_inches="tight") plt.figure(3) plt.tight_layout() filename3 = pathlib.PureWindowsPath( 'C:/ftirdb/ftirdb/data/infrared_spectra/' + depodic['initial_result_spectrum']) jcamp_dict3 = JCAMP_reader(filename3) plt.plot(jcamp_dict3['x'], jcamp_dict3['y'], label='filename', alpha=0.7, color='red') plt.xlabel(jcamp_dict3['xunits']) plt.ylabel(jcamp_dict3['yunits']) plt.savefig( pathlib.PureWindowsPath('C:/ftirdb/ftirdb/static/fig3.png'), bbox_inches="tight") plt.figure(4) plt.tight_layout() filename4 = pathlib.PureWindowsPath( 'C:/ftirdb/ftirdb/data/infrared_spectra/' + depodic['final_published_spectrum']) jcamp_dict4 = JCAMP_reader(filename4) plt.plot(jcamp_dict3['x'], jcamp_dict3['y'], label='filename', alpha=0.7, color='magenta') plt.xlabel(jcamp_dict4['xunits']) plt.ylabel(jcamp_dict4['yunits']) plt.savefig( pathlib.PureWindowsPath('C:/ftirdb/ftirdb/static/fig4.png'), bbox_inches="tight") #file names ready to be downloaded jcampname1 = depodic['sample_power_spectrum'] jcampname2 = depodic['background_power_spectrum'] jcampname3 = depodic['initial_result_spectrum'] jcampname4 = depodic['final_published_spectrum'] #need to work on display of this return { 'deop': depodic, 'sample_power_spectrum': 'ftirdb:static/fig.png', 'background_power_spectrum': 'ftirdb:static/fig2.png', 'initial_result_spectrum': 'ftirdb:static/fig3.png', 'filename': jcampname1, 'filename2': jcampname2, 'filename3': jcampname3, 'filename4': jcampname4 }
from jcamp import JCAMP_reader import matplotlib.pyplot as plt filename = './ftirdb/data/infrared_spectra/acetone.jdx' jcamp_dict = JCAMP_reader(filename) plt.plot(jcamp_dict['x'], jcamp_dict['y']) plt.title(filename) plt.xlabel(jcamp_dict['xunits']) plt.ylabel(jcamp_dict['yunits']) plt.savefig('./ftirdb/static/fig.png')
def experimentPage(request): """This page takes a project with project_ID in the URL and returns a page with a dictionary of all the values, it also contains buttons for adding samples and experiments. When page is linked from here the child/parent relationship is created""" search = request.matchdict['experiment'] #search = request.params['body'] searchdb = request.dbsession.query(experiment).filter_by(experiment_ID=search).all() dic = {} for u in searchdb: new = u.__dict__ dic.update( new ) search2 = request.dbsession.query(experimental_conditions).filter_by(experiment_ID=search).all() dic2 = {} for u in search2: new = u.__dict__ dic2.update( new ) search3 = request.dbsession.query(data_aquisition).filter_by(experiment_ID=search).all() dic3 = {} for u in search3: new = u.__dict__ dic3.update( new ) search4 = request.dbsession.query(spectra.spectra_ID).filter_by(experiment_ID=search).all() spec = {} #dic of related spectra for u in search4: num = u[0] search = request.dbsession.query(post_processing_and_deposited_spectra.final_published_spectrum).filter_by(spectra_ID=num).first() spec[(num)] = search[0] #dic of related spectra 'final published file name to download from experiment page' dic4 = {} print(spec) print('here') import random for k,v in spec.items(): colory = '#' + ("%06x" % random.randint(0, 0xFFFFFF)) jcamp_dict = JCAMP_reader(pathlib.PureWindowsPath('C:/ftirdb/ftirdb/data/infrared_spectra/' + spec[k])) print(k) plt.plot(jcamp_dict['x'], jcamp_dict['y'], label='filename', color=colory) plt.xlabel(jcamp_dict['xunits']) plt.ylabel(jcamp_dict['yunits']) plt.savefig(pathlib.PureWindowsPath('C:/ftirdb/ftirdb/static/experiment.png')) if 'form.submitted' in request.params: if request.params['form.submitted'] == 'spectrometer': #retrieve experiment ID and send to spectrometer page print(request) exp_ID = dic['experiment_ID'] next_url = request.route_url('spectrometerForm', experiment_ID = exp_ID) return HTTPFound(location=next_url) else: next_url = request.route_url('spectraForm') return HTTPFound(location=next_url) #return HTTPFound(location=next_url) else: return {'experiment': dic,'spectra':spec,'conditions':dic2,'data_aquisition':dic3, 'dic4':dic4 }
def setUp(self): filename = './data/infrared_spectra/methane.jdx' self.jcamp_dict = JCAMP_reader(filename)
def __init__(self, filename): dictionary = JCAMP_reader(filename) for k, v in dictionary.items(): setattr(self, k.replace(' ', ''), v) self.color = None
def test_read_mass(self): for root, dirs, files in os.walk("./data/mass_spectra"): for filename in files: full_filename = os.path.join(root, filename) jcamp_dict = JCAMP_reader(full_filename) self.test_xy_minmax(jcamp_dict)