def __init__(self, precursor_peak, title, pos, LA, RA): Spectrum.__init__(self, precursor_peak, title) self.position = pos # bp number self.nterm_bins = [] self.cterm_bins = [] self.LA = LA self.RA = RA
def green_spectrum(): empty = Spectrum() data = empty.data green_index = 532 - int(empty.data[0][0]) for i in range(green_index - 10, green_index + 10): data[i][1] = uniform(0,1) return Spectrum(data)
def fpy(beta, h, Ts, Te, F_sample, acc): N = len(acc) freq = np.linspace(0, F_sample, N) T = 1 / freq T[0] = N Spec = Spectrum(dt, Ts, Te, T) Sa = Spec.responseSpectrum(acc, dt, T, h, beta) return Sa
def loadSpectrumWandersPeterson(filename): name = filename.split('/')[-1] date = name[2:6] data = np.loadtxt(filename).T spectrum = Spectrum(wavelength = data[0], flux = data[1], ferr = data[2], date = date) spectrum.lineWindow = (4870, 5010) spectrum.contWindow = [(4600,4740),(4790,4840),(5130,5300)] return spectrum
def yellow_spectrum(): # Generate an empty spectrum (all wavelengths with 0 intensity) empty = Spectrum() data = empty.data # Set green wavelengths to full intensity green_index = 532 - int(empty.data[0][0]) for i in range(green_index - 10, green_index + 10): data[i][1] = uniform(0,1) # Set red wavelengths to full intensity red_index = 625 - int(empty.data[0][0]) for i in range(red_index - 10, red_index + 10): data[i][1] = uniform(0,1) # Return a yellow spectrum return Spectrum(data)
def _onSourceAddGALPROPDiffuseSource(self): if debug: print 'Source/Add GALPROP Diffuse Source' # Create a new GALPROP diffuse Source. name = 'GALPROP Diffuse Source %d' % self._getSourceID() # Create a constant value spectrum for the GALPROP source. spectrum = Spectrum(type='ConstantValue') # Adjust the Value parameter. parameter = spectrum.getParameterByName('Value') parameter.setValue(1.0) parameter.setScale(1.0) parameter.setMin(0.0) parameter.setMax(10.0) parameter.setFree(True) spectrum.setParameterByName('Value', parameter) # Create a map cube spatial model. spatialModel = SpatialModel( type='MapCubeFunction', file=ModelEditorApp._GalpropDiffuseSourcePath) # Adjust the Normalization parameter. parameter = spatialModel.getParameterByName('Normalization') parameter.setValue(1.0) parameter.setScale(1.0) parameter.setMin(0.001) parameter.setMax(1000.0) parameter.setFree(False) spatialModel.setParameterByName('Normalization', parameter) # Assemble the source. source = Source(name=name, type='DiffuseSource', spectrum=spectrum, spatialModel=spatialModel) # Fetch the current SourceLibraryDocument. sourceLibraryDocument = self._sourceLibraryDocumentEditor.get() # Append the new Source to the list of Sources. sourceLibraryDocument.addSource(source) # Populate the editor with the changes. self._sourceLibraryDocumentEditor.set(sourceLibraryDocument) # Select the new source. self._sourceLibraryDocumentEditor.selectSource(name)
def example(): #### Create a new RTXFeedback object spectrum_library = SpectrumLibrary() spectrum_library.filename = "../refData/sigmaups1_consensus_final_true_lib.msp" spectrum_library.read_header() spectrum_buffer = spectrum_library.get_spectrum(spectrum_index_number=2000) spectrum = Spectrum() spectrum.parse(spectrum_buffer) buffer = spectrum.write(format="text") print(buffer) print() return ()
def save_plots(self, pdf_dir, png_dir): #create spectrum list spectrums = [] for i in range(self._refls.shape[0]): dm = np.vstack((self._waves, self._refls[i, :])).transpose() spectrums.append(Spectrum(data=dm)) #plot spectrums and save fig1_title = self._name fig1 = plot_spectrums(spectrums, spectrums_color='cyan', title=fig1_title) fig1.savefig(os.path.join(pdf_dir, fig1_title + ".pdf")) fig1.savefig(os.path.join(png_dir, fig1_title + ".png")) #plot spectrums + mean and save fig2_title = self._name + "_with_mean" fig2 = plot_spectrums(spectrums, spectrums_color='cyan', title=fig2_title, stat_spectrum=self.mean, stat_spectrum_color='black') fig2.savefig(os.path.join(pdf_dir, fig2_title + ".pdf")) fig2.savefig(os.path.join(png_dir, fig2_title + ".png")) #plot spectrums + median and save fig3_title = self._name + "_with_median" fig3 = plot_spectrums(spectrums, spectrums_color='cyan', title=fig3_title, stat_spectrum=self.median, stat_spectrum_color='black') fig3.savefig(os.path.join(pdf_dir, fig3_title + ".pdf")) fig3.savefig(os.path.join(png_dir, fig3_title + ".png"))
def median(self): medians = np.median(self._refls, axis=0) dm = np.vstack((self._waves, medians)).transpose() return Spectrum(data=dm, idstr=self._name + "_median", company="NA", instrument="NA")
def std(self): stds = np.std(self._refls, axis=0) dm = np.vstack((self._waves, stds)).transpose() return Spectrum(data=dm, idstr=self._name + "_std", company="NA", instrument="NA")
def _MSHspec(self,source,i,dist): """Set parameters for the MSH 15-52 source Parameters: - self - This CatalogSourceExtractor object - source - The Source object we are modifying - i - The spectral index of the source from the catalog - dist - Distance from source to ROI center - not used Return value: - none - Sets the Spectrum component of the provided Source object Usage: self._VXspec(self,source,i,dist) Description: This method sets the necessary parameters for a MSH 15-52 source. By default all parameters of the source are fixed. Note: The index values (i) in the catalog are given as positive values (i.e. used in the form of flux = E^-i dE). However the convention in the model editor seems to be to use negative values so the index sign is flipped when creating the source. If it is desirable to use positive index values, simply swap out the current index parameter assignment for the one commented out. """ integral = Parameter(name="Integral",value = 0.291, scale = 1e-8, min = 0.0001, max = 10000.0, free = False) index = Parameter(name="Index",value = -i, scale = 1.0, min = -5.0, max = -1.0, free = False) #flip the sign, positive in catalog but negative here # index = Parameter(name="Index",value = i, scale = -1.0, min = 1.0, max = 5.0, free = False) # use this one if you want to have positive index values lowerLimit = Parameter(name="LowerLimit",value = 1000, scale = 1.0, min = 30.0, max = 5e5, free = False) upperLimit = Parameter(name="UpperLimit",value = 1e5, scale = 1.0, min = 30.0, max = 5e5, free = False) spectrum = Spectrum(type='PowerLaw2',parameters=[integral,index,lowerLimit,upperLimit]) source.setSpectrum(spectrum)
def _read_aux(path, label, datatype): ret = [] if os.path.isdir(path): folders = os.listdir(path) print(folders) for folder in folders: files = os.listdir(path + "/" + folder) i = 0 if folder == "ConcreteMixer_onsite.wav": i = -2000 for file in files: if i < 1000: if datatype == "spectro": val = Spectrum.compute_specgram_and_delta(path + "/" + folder + "/" + file) elif datatype == "signal": val = Waver.get_waveform(path + "/" + folder + "/" + file) else: print("valid input is either spectro or signal") ret.append(val) else: break i += 1 return ret
def get(self): """Return the contents of the editor. Parameters: self: This object. Return value: Copy of Spectrum being edited. Description: Return a new Spectrum containing the current state of the editor. """ # Fetch the type. type = self._typeOptionMenu.getvalue() # Fetch the file (mapping empty string to None). file = self._fileEntryField.getvalue() if file == '': file = None # Get the contents of each active ParameterEditor. parameters = [parameterEditor.get() \ for parameterEditor in \ self._parameterEditors[:self._nActiveParameterEditors]] # Create the new Spectrum. spectrum = Spectrum(type, file, parameters) # Return the new Spectrum. return spectrum
def __init__(self, parent=None, spectrum=None, *args, **kwargs): """Initialize this SpectrumEditor. Parameters: self: This object. parent: (tkinter.Frame) Parent object for this widget. spectrum: (Spectrum) Spectrum to initialize fields. Return value: None. Description: Initialize this SpectrumEditor. """ # Initialize the parent class (which calls _makeWidgets and # set for this class). if spectrum is None: spectrum = Spectrum() ElementEditor.__init__(self, parent, spectrum, *args, **kwargs)
def _onSelectType(self, type): """Process selection events in the type list box. Parameters: self: This object. type: (string) New Spectrum type string. Return value: None. Description: Process the selection of a new Spectrum type in the type OptionMenu. If a new Spectrum type is selected, clear the current Spectrum and create a default Spectrum of the new type. """ # If the new type is the same as the previous type, no change # is needed. if type == self._referenceElement.getType(): return # A new Spectrum type has been selected, so create a new one. spectrum = Spectrum(type=type) # Set the editor with the new Spectrum. self.set(spectrum) self.commit()
def _PL2spec(self, source, F, i, dist, sig): """Set parameters for a PowerLaw2 sepctrum source Parameters: - self - This CatalogSourceExtractor object - source - The Source object we are modifying - F - The integrated flux of the source calculated from the catalog parameters - i - The spectral index of the source from the catalog - dist - Distance from source to ROI center - sig - Source significance from catalog Return value: - none - Sets the Spectrum component of the provided Source object Usage: self._PL2spec(self,source,F,i,dist,sig) Description: This method sets the necessary parameters for a LogParabola spectrum source. By default the parameters of the source are fixed unless the source is within the radius limit defined by the user for variable sources (which defaults to the extraction region from the FT1 file) and the source is above the specified significance limit (default 4). In that case the Integral and Index parameters are allowed to vary. Note: The index values (i) in the catalog are given as positive values (i.e. used in the form of flux = E^-i dE). However the convention in the model editor seems to be to use negative values so the index sign is flipped when creating the source. If it is desirable to use positive index values, simply swap out the current index parameter assignment for the one commented out. """ fscale = int(floor(log10(F))) pfValue = F / 10**fscale integral = Parameter(name="Integral", value=pfValue, scale=10**fscale, min=0.0001, max=10000.0, free=False) index = Parameter( name="Index", value=-i, scale=1.0, min=-5.0, max=-1.0, free=False) #flip the sign, positive in catalog but negative here # index = Parameter(name="Index",value = i, scale = -1.0, min = 1.0, max = 5.0, free = False) # use this one if you want to have positive index values lowerLimit = Parameter(name="LowerLimit", value=100, scale=1.0, min=30.0, max=5e5, free=False) upperLimit = Parameter(name="UpperLimit", value=1e5, scale=1.0, min=30.0, max=5e5, free=False) if (dist <= self.radLim and dist <= self.radius and sig >= self.catParams['sigLimit']): index.setFree(True) integral.setFree(True) spectrum = Spectrum( type='PowerLaw2', parameters=[integral, index, lowerLimit, upperLimit]) source.setSpectrum(spectrum)
def uniquify(self, spectrum, tol = 0.01): diffs = np.diff(spectrum.wavelengths) idxs = np.logical_not(diffs < tol) idxs = np.hstack((idxs, np.array([True]))) return Spectrum(data = spectrum.data[idxs], idstr = spectrum.idstr, company = spectrum.company, instrument = spectrum.instrument)
def correct(self, spectrum): self._dm = np.copy(spectrum.data) self._correct_postzones() self._correct_prezones() return Spectrum(data=self._dm, idstr=spectrum.idstr, company=spectrum.company, instrument=spectrum.instrument)
def testGetPrefixPeaks(): ion1 = Peak(1, 274.112, 40.1) # ion2 = Peak(1, 361.121, 80.1) spec = Spectrum() spec.addPeak(ion1) spec.addPeak(ion2) seq = 'SWR' acids = [] acids += [AminoAcid(s, '', aa_table[s]) for s in seq] peptide = Peptide(acids) spec.setAnnotation(peptide) pg = PeakGenerator(spec) ppeaks = pg.getTheoreticalPrefixPeaks(True, 1) print 'theoretical prefix peaks are: ---------' for peak in ppeaks: print peak.getMz(), ':', peak.getPosition() print 'theoretical suffix peaks are:----------' speaks = pg.getTheoreticalPrefixPeaks(False, 1) for peak in speaks: print peak.getMz(), ':', peak.getPosition() print 'theoretical precursor peak is : -------' rpeak = pg.getTheoreticalPrecursorPeak(1) print rpeak.getMz(), ':', rpeak.getPosition() compared_peaks = [] compared_peaks += [ peak for peak in spec.getPeaks() if peak.getIntensity() > 0 ] print 'compared peaks are: ---------' for peak in compared_peaks: print peak
def _LPspec(self, source, f, i, p, b, dist, sig): """Set parameters for a LogParabola sepctrum source Parameters: - self - This CatalogSourceExtractor object - source - The Source object we are modifying - F - The integrated flux of the source calculated from the catalog parameters - i - The spectral index of the source from the catalog - p - The pivot energy of the source from the catalog - used to set the Eb parameter - b - The beta index from the catalog - dist - Distance from source to ROI center - sig - Source significance from catalog Return value: - none - Sets the Spectrum component of the provided Source object Usage: self._LPspec(self,source,f,i,p,b,dist,sig) Description: This method sets the necessary parameters for a PowerLaw2 spectrum source. By default the parameters of the source are fixed unless the source is within the radius limit defined by the user for variable sources (which defaults to the extraction region from the FT1 file) and the source is above the specified significance limit (default 4). In that case the norm, alpha, and beta parameters are allowed to vary. Note: The index values (i & b) in the catalog are given as positive values (i.e. used in the form of flux = E^-i dE). However the convention in the model editor seems to be to use negative values so the index sign is flipped when creating the source. If it is desirable to use positive index values, simply swap out the current index parameter assignment for the one commented out. """ fscale = int(floor(log10(f))) pfValue = f / 10**fscale norm = Parameter(name="norm", value=pfValue, scale=10**fscale, min=0.0001, max=10000.0, free=False) alpha = Parameter( name="alpha", value=-i, scale=1.0, min=-5.0, max=0, free=False) # flip the sign, positive in catalog but negative here beta = Parameter( name="beta", value=-b, scale=1.0, min=-10.0, max=0, free=False) # flip the sign, positive in catalog but negative here # alpha = Parameter(name="alpha",value = i, scale = -1.0, min = 0, max = 5.0, free = False) # use this one if you want to have positive index values # beta = Parameter(name="beta",value = b, scale = -1.0, min = 0, max = 10.0, free = False) # use this one if you want to have positive index values Eb = Parameter( name="Eb", value=p, scale=1.0, min=30., max=5e5, free=False) # flip the sign, positive in catalog but negative here if (dist <= self.radLim and dist <= self.radius and sig >= self.catParams['sigLimit']): alpha.setFree(True) beta.setFree(True) integral.setFree(True) spectrum = Spectrum(type='LogParabola', parameters=[norm, alpha, beta, Eb]) source.setSpectrum(spectrum)
def save_spectrums(self, out_dir): if not out_dir or np.size(self._waves) == 0: return for i in range(self._refls.shape[0]): dm = np.vstack((self._waves, self._refls[i, :])).transpose() Spectrum(data=dm, idstr=self._idstrs[i], company=self._companys[i], instrument=self._instruments[i]).write_csv(out_dir)
def build_spectrum(spectrum_filename): """ Takes a spectrum filename in familiar format and produces a Spectrum """ hdulist = fits.open(spectrum_filename) data = hdulist[1].data spec = Spectrum(data['wave'], data['flux'], data['error']) return spec
def _read_aux(path, label): ret = [] if (not os.path.isdir(path)) and path.endswith('.wav'): val = (Waver.get_waveform(path), Spectrum.compute_specgram_and_delta(path), label) ret.append(val) elif os.path.isdir(path): folders = os.listdir(path) for folder in folders: ret += _read_aux(os.path.join(path, str(folder)), label) return ret
def testGetPrefixPeaks(): ion1 = Peak(1,274.112,40.1) # ion2 = Peak(1,361.121,80.1) spec = Spectrum() spec.addPeak(ion1) spec.addPeak(ion2) seq = 'SWR' acids = [] acids += [AminoAcid(s,'',aa_table[s]) for s in seq] peptide = Peptide(acids) spec.setAnnotation(peptide) pg = PeakGenerator(spec) ppeaks = pg.getTheoreticalPrefixPeaks(True,1) print 'theoretical prefix peaks are: ---------' for peak in ppeaks: print peak.getMz(),':', peak.getPosition() print 'theoretical suffix peaks are:----------' speaks = pg.getTheoreticalPrefixPeaks(False,1) for peak in speaks: print peak.getMz(),':',peak.getPosition() print 'theoretical precursor peak is : -------' rpeak = pg.getTheoreticalPrecursorPeak(1) print rpeak.getMz(),':',rpeak.getPosition() compared_peaks = [] compared_peaks += [peak for peak in spec.getPeaks() if peak.getIntensity() > 0] print 'compared peaks are: ---------' for peak in compared_peaks : print peak
def read(self, filename): d = json.loads(open(filename).read()) num_spectra = len(d['Spectra']) idstr = get_directory_filename_extension(filename)[1] company = 'piccolo' instrument = 'piccolo' spectrums = [] for i in range(num_spectra): #the metadata for the spectrum metadata = d['Spectra'][i]['Metadata'] #processing the measurements (pixels) #read the pixel values pixels = np.array(d['Spectra'][i]['Pixels'], dtype = np.double) # print('pixels = {}'.format(pixels[-10:-1])) num_pixels = len(pixels) #get nonlinearity coefficients from metadata nlin_coeffs = np.array(metadata['NonlinearityCorrectionCoefficients']) #build pmat to contain [1, p, p^2, p^3, ...] pmat = np.ndarray((num_pixels, len(nlin_coeffs)), dtype = np.double) pmat[:, 0] = 1.0 for c in range(1, len(nlin_coeffs)): pmat[:, c] = pmat[:, (c - 1)]*pixels #apply the nonlinearity coefficients pixels = np.dot(pmat, nlin_coeffs) # print('\n becomes \n') # print('pixels = {}'.format(pixels[-10:-1])) # print(nlin_coeffs) #processing the wavelengths #create a 0 start index list widxs = np.arange(0, num_pixels) #get wavelength polynomial coefficients from metadata wave_coeffs = np.array(metadata['WavelengthCalibrationCoefficients']) #build matrix that looks like [1, x, x^2, x^3, ....] wmat = np.ndarray((num_pixels, len(wave_coeffs)), dtype = np.double) wmat[:, 0] = 1 for c in range(1, len(wave_coeffs)): wmat[:, c] = wmat[:, (c - 1)]*widxs #apply polynomial coefficients to get real wavelengths waves = np.dot(wmat, wave_coeffs) #create and append spectrum spectrums.append(Spectrum(data = np.column_stack((waves, pixels)), idstr = idstr, company = company, instrument = instrument, metadata = metadata)) # print(===============================================\n\n") return spectrums
def __init__(self, name=_defaultName, type=_defaultType, spectrum=_defaultSpectrum, spatialModel=_defaultSpatialModel, dom=None, *args, **kwargs): """Initialize this Source. Parameters: self: This object. name: (string) Name for this Source. type: (string) Type string for this Source. spectrum: (Spectrum) Object containing the spectral model for this Source. spatialModel: (SpatialModel) Object containing the spatial model for this Source. dom: (xml.dom.minidom.Element) Object representing a <source> element to use when creating this Source. If this parameter is provided, the other parameters are ignored. """ # Initialize the parent class. Do not pass the dom argument, # since it will be used if needed when fromDom is called # below. Element.__init__(self, Source._tagName, *args, **kwargs) # Set attributes. if dom: if isinstance(dom, xml.dom.minidom.Element): self.fromDom(dom) else: raise TypeError, 'Not a DOM element (%s)!' % dom else: self.setName(name) self.setType(type) if spectrum is None: spectrum = Spectrum(type=Source._defaultSpectrumType) self.setSpectrum(spectrum) if spatialModel is None: spatialModel = SpatialModel(type = \ Source._defaultSpatialModelType) self.setSpatialModel(spatialModel)
def readSpectrum(self,input_file): '''parses a spectrum file with mgf format. created on Aug 31, 2015 by mht.''' # spec = None # title = None is_parser = False with open(input_file) as input_data: for line in input_data: if len(line) == 0: continue # if line.startswith('MASS'): continue if line.startswith('BEGIN IONS'): is_parser = True spec = Spectrum() elif(is_parser): if line.startswith('TITLE'): title = line[line.index('=')+1:].strip() spec.setTitle(title) elif line.startswith('SEQ'): annotation = line[line.index('=')+1:].strip() if spec.getAnnotation() is None: spec.setAnnotation(annotation) elif line.startswith('PEPMASS'): pep_str = line[line.index('=')+1:].strip() pep_str = pep_str.split(' ') pre_mass = float(pep_str[0]) elif line.startswith('CHARGE'): charge = line[line.index('=')+1:line.index('+')].strip() pre_charge = int(charge) elif line[0].isdigit(): mass,intensity = map(float,line.split(' ')) spec.addPeak(Peak(1,mass,intensity)) elif line.startswith('END IONS'): assert(spec is not None) spec.setPrecursor(Peak(pre_charge,pre_mass*pre_charge,1)) # spec.sortPeaks() yield spec # return spec, replace return
def _COspec(self,source,f,i,p,c,dist,sig): """Set parameters for a PLSuperExpCutoff sepctrum source Parameters: - self - This CatalogSourceExtractor object - source - The Source object we are modifying - f - The flux of the source from the catalog - i - The spectral index of the source from the catalog - p - The pivot energy of the source from the catalog - used to set the Scale parameter - c - The cutoff energy from the catalog - dist - Distance from source to ROI center - sig - Source significance from catalog Return value: - none - Sets the Spectrum component of the provided Source object Usage: self._COspec(self,source,f,i,p,c,dist,sig) Description: This method sets the necessary parameters for a PLSuperExpCutoff spectrum source. By default the parameters of the source are fixed unless the source is within the radius limit defined by the user for variable sources (which defaults to the extraction region from the FT1 file) and the source is above the specified significance limit (default 4). In that case the Index1, Cutoff, and Prefactor parameters are allowed to vary. Note: The index values (i) in the catalog are given as positive values (i.e. used in the form of flux = E^-i dE). However the convention in the model editor seems to be to use negative values so the index sign is flipped when creating the source. If it is desirable to use positive index values, simply swap out the current index parameter assignment for the one commented out. """ fscale=int(floor(log10(f))) pfValue = f/10**fscale prefactor = Parameter(name="Prefactor",value = pfValue, scale = 10**fscale, min = 0.001, max = 1000.0, free = False) index1 = Parameter(name="Index1",value = -i, scale = 1.0, min = -5.0, max = 0, free = False) #flip the sign, positive in catalog but negative here index2 = Parameter(name="Index2",value = -1.0, scale = 1.0, min = -5.0, max = 0, free = False) #flip the sign, positive in catalog but negative here # index = Parameter(name="Index",value = i, scale = -1.0, min = 0, max = 5.0, free = False) # use this one if you want to have positive index values # index2 = Parameter(name="Index2",value = 1.0, scale = -1.0, min = 0, max = 5.0, free = False) #flip the sign, positive in catalog but negative here if c<=1e5: cutoff = Parameter(name="Cutoff", value = c , scale = 1.0, min = 10, max = 1e5) else: cutoff = Parameter(name="Cutoff", value = c , scale = 1.0, min = 10, max = 2*c) scale = Parameter(name="Scale",value = p, scale = 1.0, min = 30.0, max = 5e5, free = False) if (dist<=self.radLim and dist <= self.radius and sig >= self.catParams['sigLimit']): index1.setFree(True) if c<=1e5:cutoff.setFree(True) prefactor.setFree(True) spectrum = Spectrum(type='PLSuperExpCutoff',parameters=[prefactor,index1,scale,cutoff,index2]) source.setSpectrum(spectrum)
def read_spectrums(self, filename, ancillary1=""): #check validity of filename if not os.path.exists(filename) or not os.path.isfile(filename): print("{}: {} is invalid".format(__file__, filename)) sys.exit(0) if not os.path.exists(ancillary1) or not os.path.isfile(ancillary1): print("{}: {} is invalid".format(__file__, ancillary1)) sys.exit(0) #choose reader based on file extension ext = get_directory_filename_extension(filename)[2] dms, idstrs, cos, instrs = [], [], [], [] if ext == "sli": (dms, idstrs, cos, instrs) = EnviReader().read_spectrums(filename, ancillary1) return [ Spectrum(dms[i], idstrs[i], cos[i], instrs[i]) for i in range(len(dms)) ]
def fromDom(self, dom): """Initialize this Source from a xml.dom.minidom.Element. Parameters: self: This object. dom (xml.dom.minidom.Element): DOM element to use as the source of this Source. Return value: None. Description: Use the specified DOM element as the source of the content of this Source. Set all data attributes using the corresponding attribute and element nodes of the DOM element. """ # Call the inherited method. Element.fromDom(self, dom) # name self.setName(dom.getAttribute('name')) # type self.setType(dom.getAttribute('type')) # <spectrum> element domSpectrum = dom.getElementsByTagName('spectrum').item(0) spectrum = Spectrum(dom=domSpectrum) self.setSpectrum(spectrum) # <spatialModel> element domSpatialModel = dom.getElementsByTagName('spatialModel').item(0) spatialModel = SpatialModel(dom=domSpatialModel) self.setSpatialModel(spatialModel)
def susp(prog, ver, H): try: spect = Spectrum("", prog, ver) susp = list() # abre um novo arquivo de trace with open(prog + '/' + ver + '/app_base.info', 'r') as trace: lines = trace.read().splitlines() for line in lines: line = line.replace(':', ',').split(',') if line[0] == 'DA': susp.append(list()) susp[-1].append(int(line[1])) for h in H: susp[-1].append(spect.metrics[h][len(susp) - 1]) return susp except ValueError: return list() except IndexError: return list()
def read_spectrum(self, filename, ancillary_filename=""): #check validity of filename if not os.path.exists(filename) or not os.path.isfile(filename): print("{}: {} is invalid".format(__file__, filename)) sys.exit(0) #choose reader based on file extension ext = get_directory_filename_extension(filename)[2] dm, idstr, co, instr = np.array([]), "", "", "" if ext == "csv" or ext == "txt": (dm, idstr, co, instr) = CsvReader().read_spectrum(filename) elif ext == "asd" or ext == 'ASD': (dm, idstr, co, instr) = AsdReader().read_spectrum(filename) elif ext == "sed": (dm, idstr, co, instr) = SedReader().read_spectrum(filename) elif ext == "sig": (dm, idstr, co, instr) = SigReader().read_spectrum(filename) # print("Sdal Reader: idstr = {}".format(idstr)) else: print("{}: Invalid file type {}".format(__file__, ext)) sys.exit(0) return Spectrum(dm, idstr, co, instr)
def resample(self, spectrum): #make local copies to aid readability waves = spectrum.wavelengths refls = spectrum.reflectances if self._wavestart < waves[0] or self._wavestop > waves[-1]: print("WaveResampler: Trying to resample out of range") spectmplt = "Spectrum wavelength range: {} to {}" print(spectmplt.format(waves[0], waves[-1])) reqtmplt = "Specified wavelength range: {} to {}" print(reqtmplt.format(self._wavestart, self._wavestop)) sys.exit(0) wstart = self._wavestart wstop = self._wavestop numsamples = int((wstop - wstart) / self._spacing) + 1 rswaves = np.linspace(wstart, wstop, numsamples) rsrefls = np.array([]) if self._resampletype == "linear": #get the interpolation fit inrefls = interp1d(waves, refls, kind=self._resampletype) #do resampling at resampled wavelengths rsrefls = inrefls(rswaves) elif self._resampletype == "cubic": spliner = ChsInterpolator() #get the interpolation fit spliner.fit(waves, refls) #do resampling at resampled wavelengths rsrefls = spliner.predict(rswaves) pass else: tmplt = "{} Unknown resampling type {}" print(tmplt.format(__file__, self._resampletype)) sys.exit(0) return Spectrum(data=np.column_stack((rswaves, rsrefls)), idstr=spectrum.idstr, company=spectrum.company, instrument=spectrum.instrument, metadata=spectrum.metadata())
elif sys.argv[1] == 'hlm': npa = nco(p4,width=12,depth=1024,samples=16384,finalwidth=12,dodither=None,dointerpolate=0) elif sys.argv[1] == "pure": samples = 16384 phase = 0x2670e38e npa = np.zeros(samples) pf = float(phase)/float(0xffffffff) maxdelta = 0 ly = 0 for i in range(samples): x = i*2*np.pi*pf #x = np.round(x,2) #x = x + np.random.randint(-5,6) / 1000.0 ##print x,phase_dither y = np.cos(x) delta = np.abs(y - ly) if delta > maxdelta: maxdelta = delta npa[i] = y print "Maxdelta is",maxdelta*2048 s = Spectrum(npa,dt,window=signal.flattop) peaks = s.findPeaks(order=4,clipdb=90) s.printPeaks(peaks) s.plot()
from Spectrum import Spectrum, loadCaptureData from scipy import signal import numpy as np ## Load CaptureData object cd = loadCaptureData("data/sh12_1000") ## Create spectrum object ## Averages is the number of partitions to average ## Normalize scales top output to 0 dB ## Z is the impedance that the scope is measuring voltage across ## Window is the window to apply to fft input data, signal.flattop is best for power s = Spectrum(cd,averages=24,normalize=False,Z=50.0,window=signal.flattop) ## Create a frequency slice if you want to restrict further operations ## over a particular frequency range, None for no frequency slice ##sl = (0,200) sl = None ## Find peaks ## clipdb specifies the db value below which to no longer look for peaks ## For example clipdb of 70 will not look for peaks smaller than -70 dbc peaks = s.findPeaks(sl,clipdb=70) ## Print the peaks s.printPeaks(peaks)
if is_known: sig_ions[key_ion] = frequency return sig_ions def updateFeatures(feature_dict,features): if features == None: return for gof in features: if gof in feature_dict: freq = feature_dict[gof] else: freq = 0 freq += 1 feature_dict[gof] = freq if __name__ == '__main__': ion1 = Peak(2,274.112,40.1) # ion2 = Peak(2,361.121,80.1) spec = Spectrum() spec.addPeak(ion1) spec.addPeak(ion2) seq = 'SWR' acids = [] for s in seq: acids.append(AminoAcid(s, '', aa_table[s])) pep = Peptide(acids) spec.setAnnotation(pep) specs = [] specs.append(spec) ist = IonSelector(0.5,10) sig_ions = ist.findSigIons(1,specs) for ion,freq in sig_ions.items(): print ion,':',ion.getPosition(),':',freq
def getspectrum(bdp, vlsr=0.0, smooth=(), recalc=False, segment={"method": "ADMIT", "minchan":5, "maxgap":3, "numsigma":2.0, "iterate":True, "nomean":True}): """ Method to convert an input BDP into a list based spectrum. The spectrum will be smoothed if requested by the input parameters. Parameters ---------- bdp : BDP The input BDP, currently either a CubeSpectrum or CubeStats only. vlsr : float The velocity of the source. Default: 0.0 smooth : tuple If smoothing is to be done... recalc : bool True if the noise is to be recalculted from smoothed spectra Default: False Returns ------- A series of numpy arrays describing the spectrum. (freq, chans, spectrum) """ # if the input bdp is a cubestats then we get both the max and the min # value arrays, this make the AT sensitive to both emission and # absorption lines multi = False # PJT: bit awkward, previously we needed just a boolean stat, # now I've used two, but this doesn't scale well. Why not an enumerated type. pvcorr = False stat = False if bdp._type == bt.CUBESTATS_BDP: stat = True # get the per channel noise tempsi = ma.array(bdp.table.getColumnByName("sigma", typ=np.float64), fill_value=0.0) # get the peak values mspectrum = ma.array(bdp.table.getFullColumnByName("max", typ=np.float64) / tempsi, fill_value=0.0) # get the minimum values nspectrum = ma.array(abs(bdp.table.getFullColumnByName("min", typ=np.float64)) / tempsi, fill_value=0.0) avgnoise = np.average(tempsi) # merge the peak/minimum into one spectrum #spectrum = mergestats(mspectrum, nspectrum, tempsi) mspectrum = ma.masked_equal(mspectrum, 0.0) nspectrum = ma.masked_equal(nspectrum, 0.0) # get the frequency for each channel freq = bdp.table.getColumnByName("frequency", typ=np.float64) # get the channel number for each channel chans = bdp.table.getColumnByName("channel", typ=np.float64) # convert the spectrum to a peak/noise (i.e. sigma) valued spectrum #spectrum /= tempsi # doppler shift the input spectra, which are in sky frequency, to the proper # source rest frame freq = utils.undoppler(freq, vlsr) if isinstance(mspectrum.mask, bool) or isinstance(mspectrum.mask, np.bool_): mspectrum.mask = np.array([mspectrum.mask] * len(mspectrum.data)) if isinstance(nspectrum.mask, bool) or isinstance(nspectrum.mask, np.bool_): nspectrum.mask = np.array([nspectrum.mask] * len(nspectrum.data)) spec = [Spectrum(mspectrum, copy.deepcopy(freq), copy.deepcopy(chans)), Spectrum(nspectrum, copy.deepcopy(freq), copy.deepcopy(chans))] for s in spec: s.mask_invalid() s.fix_invalid(0.0) s.mask_equal(0.0) segment["spectrum"] = s.spec() segment["freq"] = s.freq() sfinder = SegmentFinder.SegmentFinder(**segment) sep, cut, noise, mean = sfinder.find() s.set_noise(noise) # smooth the spectrum if requested if len(smooth) > 0 and smooth[0] is not None: filter = Filter1D.Filter1D(spec[0].spec(), smooth[0], **Filter1D.Filter1D.convertargs(smooth)) spec[0].set_spec(ma.MaskedArray(filter.run(), mspectrum.mask)) filter = Filter1D.Filter1D(spec[1].spec(), smooth[0], **Filter1D.Filter1D.convertargs(smooth)) spec[1].set_spec(ma.MaskedArray(filter.run(), nspectrum.mask)) if recalc: for s in spec: segment["spectrum"] = s.spec() segment["freq"] = s.freq() sfinder = SegmentFinder.SegmentFinder(**segment) sep, cut, noise, mean = sfinder.find() s.set_noise(noise) # if the input bdp is a CubseSpectrum then get all of the available # spectra elif bdp._type == bt.CUBESPECTRUM_BDP: spectrum = ma.array(bdp.table.getFullColumnByName("flux", typ=np.float64), fill_value=0.0) # get the frequencies of the channels freq = bdp.table.getFullColumnByName("frequency", typ=np.float64) # get the channel number of each channel chans = bdp.table.getFullColumnByName("channel", typ=np.float64) # CubeSpectrum can have more than one spectra if len(spectrum.shape) == 2: multi = True for i in range(len(spectrum)): spectrum[i] = ma.masked_equal(spectrum[i], 0.0) else: spectrum = [ma.masked_equal(spectrum, 0.0)] freq = [freq] chans = [chans] # smooth the spectra if requested spec = [] for i in range(len(spectrum)): if isinstance(spectrum[i].mask, bool) or isinstance(spectrum[i].mask, np.bool_): spectrum[i].mask = np.array([spectrum[i].mask] * len(spectrum[i].data)) for i in range(len(freq)): # source rest frame freq[i] = utils.undoppler(freq[i], vlsr) tempspec = Spectrum(spectrum[i], freq[i], chans[i]) tempspec.mask_invalid() tempspec.fix_invalid(0.0) tempspec.mask_equal(0.0) segment["spectrum"] = tempspec.spec() segment["freq"] = tempspec.freq() sfinder = SegmentFinder.SegmentFinder(**segment) sep, cut, noise, mean = sfinder.find() tempspec.set_noise(noise) spec.append(tempspec) if len(smooth) > 0 and smooth[0] is not None: for s in spec: filter = Filter1D.Filter1D(s.spec(), smooth[0], **Filter1D.Filter1D.convertargs(smooth)) s.set_spec(ma.MaskedArray(filter.run(), mask=spectrum[i].mask)) if recalc: segment["spectrum"] = s.spec() segment["freq"] = sfreq() sfinder = SegmentFinder.SegmentFinder(**segment) sep, cut, noise, mean = sfinder.find() s.set_noise(noise) # doppler shift the input spectra, which are in sky frequency, to the proper elif bdp._type == bt.PVCORR_BDP: if len(bdp.table) == 0: return None, None, None, None pvcorr = True chans = bdp.table.getColumnByName("channel", typ=np.float64) freq = bdp.table.getColumnByName("frequency", typ=np.float64) spectrum = ma.array(bdp.table.getFullColumnByName("pvcorr", typ=np.float64), fill_value=0.0) # doppler shift the input spectra, which are in sky frequency, to the proper # source rest frame freq = utils.undoppler(freq, vlsr) spectrum = ma.masked_equal(spectrum, 0.0) spec = Spectrum(spectrum, freq, chans) spec.mask_invalid() spec.fix_invalid(0.0) spec.mask_equal(0.0) else: raise Exception("Data from BDP type %s is not supported." % (bdp._type)) return spec
def __init__(self, precursor_peak, title, positionmz): Spectrum.__init__(self,precursor_peak,title) self.positionmz = positionmz # Noise mass self.nterm_bins = [] self.cterm_bins = []
def process_overlap(self, spec): #find the forward difference for the wavelengths diffs = np.diff(spec.wavelengths) #find where the wavelength differences are negative idxs = np.nonzero(diffs <= -0.05)[0] idxs = idxs + 1 idxs = np.hstack((np.array([0]), idxs, np.size(spec.wavelengths))) #create pieces os spectrums with increasing wavelengths pcs = [] data = spec.data uniquifier = WaveUniquifier() for k in range(1, len(idxs)): i1 = idxs[k - 1] i2 = idxs[k] s = Spectrum(data = data[i1:i2, :], idstr = spec.idstr, company = spec.company, instrument = spec.instrument) pcs.append(uniquifier.uniquify(s)) #resample the pieces into 1 nm wavelengths rspcs = [] resampler = WaveResampler() for s in pcs: wr = s.wavelength_range() start = math.ceil(wr[0]) stop = math.floor(wr[1]) resampler = WaveResampler(rstype = self._rstype, wavestart = start, wavestop = stop, spacing = 1.0) rspcs.append(resampler.resample(s)) # print(rspcs[-1].wavelengths[0], rspcs[-1].wavelengths[-1]) # print("------------------------") #chop and stitch if len(rspcs) > 1: #find the wavelengths to chop at critwaves = [rspcs[0].wavelengths[0]] for i in range(1, len(rspcs)): #find the overlapping indices lstart, lstop, rstart, rstop = -1, -1, -1, -1 rstart = 0 lstart = closest_array_index(rspcs[i].wavelengths[0], rspcs[i - 1].wavelengths) lstop = len(rspcs[i - 1].wavelengths) rstop = closest_array_index(rspcs[i - 1].wavelengths[-1], rspcs[i].wavelengths) + 1 lrefls = rspcs[i - 1].reflectances[lstart:lstop] rrefls = rspcs[i].reflectances[rstart:rstop] lwaves = rspcs[i - 1].wavelengths[lstart:lstop] critwaves.append(lwaves[np.argmin(np.abs(lrefls - rrefls))]) critwaves.append(rspcs[-1].wavelengths[-1]) # print("critwaves = {}".format(critwaves)) subdms = [] for i in range(len(rspcs)): start = closest_array_index(critwaves[i], rspcs[i].wavelengths) stop = closest_array_index(critwaves[i + 1], rspcs[i].wavelengths) + 1 subdms.append(rspcs[i].data[start:stop, :]) # print(rspcs[i].data[start:stop, :]) # print("========================") return uniquifier.uniquify(Spectrum(data = np.vstack(tuple(subdms)), idstr = spec.idstr, company = spec.company, instrument = spec.instrument)) else: return rspcs[0]