def main( options=None, args=None ): """The main method""" global COLORS if options.scriptMode: matplotlib.use( 'Agg' ) import pylab thisFigure = pylab.figure( ) if options.outputFile: thisFigure.set_size_inches(( float( options.width ) / options.dpi, float( options.height ) / options.dpi )) pylab.subplots_adjust( left = 0.6 / options.width * options.dpi, right = 1.0 - 0.2 / options.width * options.dpi, top = 1.0 - 0.45 / options.height * options.dpi, bottom = 0.5 / options.height * options.dpi ) barOffset = options.lineWidth / 2 barAlpha = options.markerAlpha * 2 / 3 rawFiles = [ ] rawTypes = [ '.csv', '.mzdata', '.mzxml', '.mzxml.xml', '.json', '.json.gz' ] for i in range( len( args )-1, -1, -1 ): arg = args[ i ] try: # check the extension to see if this is xmass input data for type_ in rawTypes: if arg.lower( ).endswith( type_ ): rawFiles.append( args.pop( i )) continue except ValueError: pass if rawFiles: for r in rawFiles: ref = mzlib.RawData( ) if not ( ref.read( r )): sys.stderr.write( "Error: Unable to load data from '%s'" % r ) sys.exit( -1 ) if options.shortFilename: filename = os.path.basename( r ) else: filename = r # apply any filters if options.mass: ref.onlyMz( options.mass, options.massWindow ) if options.maxTime or options.minTime: if options.maxTime: ref.onlyScans( options.minTime, options.maxTime ) else: ref.onlyScans( options.minTime ) rt = [ scan[ "retentionTime" ] for scan in ref if scan[ "msLevel" ] == 1 ] if options.bpc: yAxis = ref.bpc( 1 ) else: yAxis = ref.tic( 1 ) if filters: if options.lpfThreshold and options.hpfThreshold: yAxis = filters.bpf( yAxis, options.hpfThreshold, options.lpfThreshold ) elif options.lpfThreshold: yAxis = filters.lpf( yAxis, options.lpfThreshold ) elif options.hpfThreshold: yAxis = filters.hpf( yAxis, options.hpfThreshold ) if options.normalize: if len( yAxis ): max_ = max( yAxis ) if max_: yAxis = [ x / max_ for x in yAxis ] if options.normalize: label = filename + " (normalized)" else: label = filename pylab.plot( rt, yAxis, COLORS['ref'][0] , alpha = options.markerAlpha, linewidth=options.lineWidth, label = label ) COLORS['ref'] = COLORS['ref'][1:] + [ COLORS['ref'][0]] if not options.scriptMode: def findMedian( sortedArray ): arrayLen = len( sortedArray ) if arrayLen % 2: median = sortedArray[ arrayLen // 2 ] * sortedArray[ arrayLen // 2 + 1 ] else: median = sortedArray[ arrayLen // 2 ] return median array = list( yAxis ); array.sort( ) median = findMedian( array ) q1 = findMedian( array[ : len( array ) // 2 ]) q3 = findMedian( array[ int( math.ceil( len( array ) / 2.0 )) : ]) min_ = min( yAxis ) max_ = max( yAxis ) print( "Plot statistics for %s:" % label ) print( "\tRange: %g (%g - %g)" % ( max_ - min_, min_, max_ )) print( "\tMean: %g" % numerical.mean( yAxis )) print( "\tMedian: %g" % median ) print( "\tInterquartile Range: %g (%g - %g)" % ( q3 - q1, q1, q3 )) print( "\tStandard Deviation: %g" % numerical.std( yAxis )) print( "\tVariance: %g" % numerical.var( yAxis )) # The following section of code is specific to the OmicsDP data formats. # You can safely delete this section if you are using this software outside # of that environment. # BEGIN READING DLTs for arg in args: scan = numerical.empty( 0, numerical.uint64 ) # scan number barRt = numerical.empty( 0, numerical.float64 ) # retention time barIntensity = numerical.empty( 0, numerical.float64 ) barNoise = numerical.empty( 0, numerical.float64 ) labels = [ ] try: f = open( arg ) lines = DictReader( f ) except IOError: sys.stderr.write("Error: unable to read file '%s'\n" % arg ) sys.exit( -1 ) if options.shortFilename: filename = os.path.basename( arg ) else: filename = arg for line in lines: try: scanValue = int( line[ 'Scan' ]) rtValue = float( line[ 'RT(min)'] ) mzValue = float( line[ 'M/Z' ] ) noiseValue = float( line[ 'LC_Noise' ] ) intValue = float( line[ 'Int' ] ) if ( rtValue < options.minTime or ( options.maxTime and rtValue > options.maxTime )): continue if ((( not noiseValue ) or intValue/noiseValue < options.filterLevel ) or ( options.mass and abs( options.mass - mzValue ) > options.massWindow )): if options.verbosity: sys.stderr.write( "Dropping line %s" % ( line )) continue # using plot( ) produces a more responsive graph than vlines( ) if len( scan ) and scanValue == scan[ -1 ]: if options.bpc: if intValue > barIntensity[ -2 ]: barIntensity[ -2 ] = intValue barNoise[ -2 ] = noiseValue labels[ -1 ] = "(%.2f," % ( mzValue - 0.005 ) #truncate, don't round else: barIntensity[ -2 ] += intValue barNoise[ -2 ] += noiseValue labels[ -1 ] += " %.2f," % ( mzValue - 0.005 ) #truncate, don't round else: # appending [0, value, 0] allows us to plot a bar graph using lines barRt = numerical.append( barRt, [ rtValue, rtValue, rtValue ]) barIntensity = numerical.append( barIntensity, [ 0, intValue, 0 ]) barNoise = numerical.append( barNoise, [ 0, noiseValue, 0 ]) scan = numerical.append( scan, scanValue ) if ( len( labels )): labels[ -1 ] = labels[ -1 ][ :-1 ] + ')' # replace the last , with ) labels.append( "(%.2f," % ( mzValue - 0.005 )) #truncate, don't round except ( ValueError, IndexError ): if options.verbosity: sys.stderr.write( "Skipping line %s" % ( line )) if ( len( labels )): labels[ -1 ] = labels[ -1 ][ :-1 ] + ')' # replace the last , with ) if options.normalize: if len( barIntensity ): max_ = max( barIntensity ) if max_: barIntensity /= max_ barNoise /= max_ if options.massLabels: for i in xrange( len( labels )): pylab.annotate( labels[ i ], ( barRt[ 3 * i + 1 ], barIntensity[ 3 * i + 1 ]), size=9) # calculate alpha based on which file this is in the list alpha = ( options.markerAlpha - options.markerAlpha * ( args.index( arg ) / float( len( args ))) * 0.75 ) if options.showPeaks: if not options.removeNoise: barIntensity += barNoise if options.normalize: label = label = ( "%s - intensity (%d peaks, normalized)" % ( filename, len( barIntensity )/3)) else: label = label = ( "%s - intensity (%d peaks)" % ( filename, len( barIntensity )/3)) pylab.plot( barRt, barIntensity, COLORS['intensity'][0] , linewidth = options.lineWidth*2, alpha = alpha, label = label ) if options.connectPeaks: pylab.plot( barRt[ 2::3 ], barIntensity[ 1::3 ], COLORS['intensity'][0], alpha = alpha, linewidth=options.lineWidth ) COLORS['intensity'] = COLORS['intensity'][1:] + [ COLORS['intensity'][0]] if options.showNoise: if options.normalize: label = ( "%s - noise (%d points, normalized)" % ( filename, len( barNoise )/3)) else: label = ( "%s - noise (%d points)" % ( filename, len( barNoise )/3)) pylab.plot( barRt[ 2::3 ], barNoise[ 1::3 ], COLORS['noise'][0], alpha = alpha, linewidth=options.lineWidth, label = label) COLORS['noise'] = COLORS['noise'][1:] + [ COLORS['noise'][0]] if len( barRt ): #draw a horizontal black line at 0 pylab.plot( [barRt[1], barRt[-2]], [0,0], 'k', linewidth=options.lineWidth ) f.close( ) # END READING DLTs if options.showLegend: legend = pylab.legend( loc="upper left", prop=FontProperties( size='small' )) pylab.grid( ) axes = thisFigure.get_axes( )[ 0 ] axes.set_xlabel( "Time (min)" ) axes.set_ylabel( "Intensity" ) axes.ticklabel_format( style="scientific", axis="y", scilimits=(3,3) ) if not len( rawFiles ): if ( options.bpc ): axes.set_title( "Base Peaks" ) else: axes.set_title( "Peaks" ) elif options.bpc: if options.mass: axes.set_title( "Selected Base Peak Chromatogram (M/Z: %f, Tolerance: %f)" % ( options.mass, options.massWindow )) else: axes.set_title( "Base Peak Chromatogram" ) else: if options.mass: axes.set_title( "Selected Ion Chromatogram (M/Z: %f, Tolerance: %f)" % ( options.mass, options.massWindow )) else: axes.set_title( "Total Ion Chromatogram" ) if options.outputFile: thisFigure.savefig( options.outputFile, dpi=options.dpi ) if not options.scriptMode: pylab.show( )
def init (): #x = array ([2*(gauss(0,1)-0.5) for i in range (N)]) u = zeros(N); s = zeros(N); v = array([]) beats = { "200": 120, "400": 125, "600": 119, "800": 130, "240": -80, "440": -75, "640": -70, "840": -85 } # генерируем сердцебиение for b in beats: u[int(b)] = beats[b] # генерируем синусоиду и дополняем ее нулями d = zeros (N) # d[:L] = [sin (2 * pi * f * t * 0.008) * exp (-alpha * t * 0.008) for t in range (L)] dt = 1; m = 64; n = 10 d = [ sin (2 * pi * 1 * t * 0.008 + 10*pi) # + sin (2 * pi * 17 * t * 0.008) # + sin (2 * pi * 150 * t * 0.008) for t in range (N)] a = array ([lfsr().next() for i in range (N)])*5 a_f = fourier (a) bpf_f = fourier (bpf (0.1, 0.2, L = N, m = m)) a_short_f = conv_freq (a_f, bpf_f) a_short = fourier_inv (a_short_f[:,comp]) #d_n = conv (d, a_short) # html.add_figure (d, u"Синусоида") # html.add_figure (add_spikes (-1, 1, 30, d), u"Синусоида с импульсами") # html.add_figure (add_trend (linear_trend (0.001, 0.01, 2, len(d)), d), u"Синусоида с линейным трендом") # html.add_figure (add_trend (exp_trend (0.001, 0.01, len(d)), d), u"Синусоида с экспоненциальным трендом") html.add_break () d = zeros (N) d[:L] = [sin (2 * pi * f * t * 0.008) * exp (-alpha * t * 0.008) for t in range (L)] v = conv (u,d) v_n = v + a html.add_figure (u, u"Кардиограмма") # html.add_figure (d, u"Синусоида") html.add_figure (v, u"Свертка") html.add_figure (v_n, u"Свертка с шумом") v_f = fourier (v) v_n_f = fourier (v_n) d_f = fourier (d) # g_i = reverse_filter (d, "ideal", a, v) g_r = reverse_filter (d, "regular", a, v) # g_v = reverse_filter (d+a, "viner", a, v) # html.add_figure (g_i[:,comp], u"Обратный идеальный фильтр\n(комплексный спектр)") html.add_figure (g_r[:,comp], u"Обратный регулярный фильтр\n(комплексный спектр)") # html.add_figure (g_v[:,comp], u"Обратный винеровский фильтр\n(комплексный спектр)") # # g_f = conv_freq (g_i, v_f) # d_est = fourier_inv (g_f[:,comp]) # html.add_figure (d_est, u"Деконволюция идеальным фильтром") g_f = conv_freq (g_r, v_f) d_est = fourier_inv (g_f[:,comp]) html.add_figure (d_est, u"Деконволюция регулярным фильтром")