def plot_timeseries_comparison(masked, inf):
    fig = plt.figure(figsize=(16,4))

    times = np.arange(len(masked))*inf.dt
    warnings.warn("Only plotting every 10th point of time series.")
    plt.plot(times[::10], masked.data[::10], 'k-', drawstyle='steps-post', 
             label='Time series', zorder=1)

    inverted = invert_mask(masked) 
    slices = np.ma.flatnotmasked_contiguous(inverted)
    if slices:
        for ii, badslice in enumerate(slices):
            if ii == 0:
                label='Radar indentified'
            else:
                label="_nolabel"
            tstart = inf.dt*(badslice.start)
            tstop = inf.dt*(badslice.stop-1)
            plt.axvspan(tstart, tstop, alpha=0.5, 
                        fc='r', ec='none', zorder=0, label=label)
    
    plt.figtext(0.02, 0.02,
                "Frac. of data masked: %.2f %%" % ((len(masked)-masked.count())/float(len(masked))*100), 
                size='x-small')
    plt.figtext(0.02, 0.05, inf.basenm, size='x-small')

    plt.xlabel("Time (s)")
    plt.ylabel("Intensity")
    plt.xlim(0, times.max()+inf.dt)

    plt.subplots_adjust(bottom=0.15, left=0.075, right=0.98)
Example #2
0
def snap1(vector):
	fig = plt.figure()
	plt.plot(vector)
	plt.axvspan(75, 125, facecolor='b', alpha=0.3)
	plt.xlabel('$x$')
	plt.ylabel('$Ez$')
	plt.show()
Example #3
0
def plotRegion(start,end,area,bpScores,centers):
	if len(centers) < 6 or len(area) < 2000:
		return
	
	plt.clf()
	
	left=len(area)/2-1000
	right=len(area)/2+1000
	
	smooth=[]
	for i in range(len(area)):
		smooth.append(sum(area[max(0,i-10):i+10])/20.)
	print smooth
	print centers
	for center in centers:
		#plt.axvline(center[0]-start-74, color="gray")
		#plt.axvline(center[0]-start+74, color="gray")
		plt.axvspan(center[0]-73, center[0]+73, color='gray', alpha=0.3)
		plt.axvline(center[0], color="black")
	plt.plot(smooth)
	plt.plot(bpScores,"red")
	plt.title("Smoothed Read Start Count (chr21:"+str(start+left)+"-"+str(start+right)+")")
	plt.xlabel("Relative BP position")
	plt.ylabel("Count or Score")
	plt.xlim([left,right])
	plt.ylim([0,10])
	fig = matplotlib.pyplot.gcf()
	fig.set_size_inches(16,2)
	plt.savefig(sys.argv[3]+"_"+str(start)+"-"+str(end)+'.plot.pdf', dpi=100)
Example #4
0
def plot_y_dist(y_train, y_test):

    """
    Plotting the counts of inhibition scores
    :param y_train: Target values used for training
    :param y_test: Target values used for testing
    :return: None
    """

    y_train = pd.read_csv('https://s3-us-west-2.amazonaws.com/pphilip-usp-inhibition/'
                          'data/y_train_postprocessing.csv')
    y_test = pd.read_csv('https://s3-us-west-2.amazonaws.com/pphilip-usp-inhibition/'
                         'data/y_test_postprocessing.csv')
    y_train.columns = ['ID', 'Activity_Score']
    df_train = y_train.groupby('Activity_Score')['ID'].nunique().to_frame()
    df_train['score'] = df_train.index
    df_train.columns = ['score_counts', 'score']
    df_train = df_train.reset_index(drop=True)

    y_test.columns = ['ID', 'Activity_Score']
    df_test = y_test.groupby('Activity_Score')['ID'].nunique().to_frame()
    df_test['score'] = df_test.index
    df_test.columns = ['score_counts', 'score']
    df_test = df_test.reset_index(drop=True)
    plt.plot(df_train['score'], df_train['score_counts'])
    plt.plot(df_test['score'], df_test['score_counts'])
    plt.title('Sample counts of unique inhibition scores')
    plt.xlabel('Inhibition score')
    plt.ylabel('Number of molecule samples')
    plt.axvspan(0, 40, facecolor='orange', alpha=0.2)
    plt.axvspan(40, 100, facecolor='violet', alpha=0.2)
    plt.savefig('./plots/score_counts.png', bbox_inches='tight')
Example #5
0
def colorize_phases(STOP_TIME,durrationOfStep,ev_foot_const):
    for phase in range(int(1+STOP_TIME/durrationOfStep)):
        t0_phase = phase*durrationOfStep
        t1_phase = (phase+ev_foot_const)*durrationOfStep
        t2_phase = (phase+1)*durrationOfStep
        plt.axvspan(t0_phase, t1_phase, color='g', alpha=0.3, lw=2) #adaptative part (foot goal can change)
        plt.axvspan(t1_phase, t2_phase, color='r', alpha=0.3, lw=2) #non adaptative part
Example #6
0
def plot_gen(ping, now, t, nans, host, interactive=False, size="1280x640"):
    ''' Generates ping vs time plot '''
    if not interactive:
        import matplotlib
        matplotlib.use("Agg") # no need to load gui toolkit, can run headless
    import matplotlib.pyplot as plt
    
    size      = [int(dim) for dim in size.split('x')]
    datestr   = now[0].ctime().split()
    datestr   = datestr[0] + " " + datestr[1] + " " + datestr[2] + " " + datestr[-1]
    plt.figure(figsize=(size[0]/80.,size[1]/80.)) # dpi is 80
    plt.plot(now[~nans], ping[~nans], drawstyle='steps', marker='+')
    plt.title("Ping Results for {0}".format(host))
    plt.ylabel("Latency [ms]")
    plt.xlabel("Time, {0} [GMT -{1} hrs]".format(datestr, time.timezone/3600))
    plt.xticks(size=10)
    plt.yticks(size=10)
    plt.ylim(ping[~nans].min()-5, ping[~nans].max()+5)
    
    # plot packet losses
    start  = []
    finish = []
    for i in range(len(nans)):
        if nans[i] == True:
            if i == 0:
                start.append(i)
            elif nans[i] != nans[i-1]:
                start.append(i)
            #if i != len(nans) and nans[i+1] != nans[i]:
            #    finish.append(i)
                
    # add the red bars for bad pings
    for i in range(len(start)):
        plt.axvspan(now[start[i]], now[finish[i]+1], color='red')
    return plt
def Plot_Background(m_wavelengths,F_bgd,xs,ys):

   plt.scatter(m_wavelengths,F_bgd)
   plt.plot(xs,ys,'-',label='Integrated sky brightness')

   plt.title("Sky background as a function of wavelength")
   plt.xlabel("Wavelength / A"), plt.ylabel("log flux (ergs/s/cm2/A/arcsec2)") 
   x_min,x_max = (m_wavelengths[0] - FWHM[0]), (m_wavelengths[-1] + FWHM[-1])
   plt.xlim(x_min,x_max)
   plt.annotate

   # Shade in filters (optional)
   
   previous = 0
   for i in range(0,len(band_colours)-1):
      if m_wavelengths[i]-FWHM[i] < previous:
         min,max = previous, m_wavelengths[i]+FWHM[i]
      else: 
         min,max = m_wavelengths[i]-FWHM[i],m_wavelengths[i]+FWHM[i]
      plt.axvspan(min,max,facecolor=band_colours[i],alpha=0.35)
      previous = m_wavelengths[i]+FWHM[i]
   
   
   labels=['U','B','V','R','I','J','H','K']
   
   for label, x, y in zip(labels, m_wavelengths,F_bgd):
      plt.annotate(
         label, 
         xy = (x, y), xytext = (10, 12),
         textcoords = 'offset points', ha = 'right', va = 'bottom',
         bbox = dict(boxstyle = 'round,pad=0.5', fc = 'white', alpha = 0.5))

   plt.show()
Example #8
0
def plot_gen(ping, now, t, nans, host):
    ''' Generates ping vs time plot '''
    datestr   = now[0].ctime().split()
    datestr   = datestr[0] + " " + datestr[1] + " " + datestr[2] + " " + datestr[-1]
    plt.figure(figsize=(25,8))
    plt.plot(now[~nans], ping[~nans], drawstyle='steps')
    plt.title("Ping Results for {0}".format(host))
    plt.ylabel("Latency [ms]")
    plt.xlabel("Time, {0} [GMT -{1} hrs]".format(datestr, time.timezone/3600))
    plt.xticks(size=10)
    plt.yticks(size=10)
    plt.ylim(ping[~nans].min()-5, ping[~nans].max()+5)
    
    # plot packet losses
    start  = []
    finish = []
    for i in range(len(nans)):
        if nans[i] == True:
            if i == 0:
                start.append(i)
            elif nans[i] != nans[i-1]:
                start.append(i)
            if i != len(nans) and nans[i+1] != nans[i]:
                finish.append(i)
                
    # add the red bars for bad pings
    for i in range(len(start)):
        plt.axvspan(now[start[i]], now[finish[i]+1], color='red')
    return
Example #9
0
def calc_ola(window, M, R):
    window = scipy.signal.get_window(window, M, fftbins = True)
    
    f1, ax = plt.subplots(1)
    R0 = M - M%R
    #ax.axvline(R0,linewidth=3.0,color='black')
    plt.axvspan(R0, R0+M-1, facecolor='grey', alpha=0.5)
    #ax.axvline(R0+M-1,linewidth=3.0,color='black')
    
    ola = np.zeros(3*M)
    w_i = []
    for shift in np.arange(0,2*M,R):
        win = np.zeros(3*M)
        win[shift:shift+M] += window
        w_i.append(win)
        if shift == R0:
            ax.plot(win, '-o', linewidth=2.5)
        else:
            ax.plot(win)
        ola += win
    ax.plot(ola, '-or')    
    ola = ola[R0:R0+M]
    normCola = np.unique(np.round(ola,10))
    print(normCola)
    if len(normCola)==1:
        return(True, normCola)
    else:
        return(False, ola)
Example #10
0
def output_spectrum(wl, vec, interval=None):
  order = np.argsort(wl)
  p = plt.plot(wl[order], vec[order], color='red')
  ax = plt.gca()
  ax.set_xlabel('Wavelength')
  ax.set_ylabel('Magnitude')
  ax.grid(True)
  ax.spines['bottom'].set_color('white')
  ax.spines['top'].set_color('white')
  ax.spines['left'].set_color('white')
  ax.spines['right'].set_color('white')
  ax.xaxis.label.set_color('white')
  ax.yaxis.label.set_color('white')
  ax.tick_params(axis='x', colors='white')
  ax.tick_params(axis='y', colors='white')
  if interval is not None:
      plt.axvspan(interval[0], interval[1], facecolor='white', alpha=0.25)
  plt.savefig('spectrum_out.png',transparent=True)
  sys.stdout.write('%i'%len(wl))
  for w in wl:
    sys.stdout.write(' %f' % w)
  for v in vec:
    sys.stdout.write(' %f' % v)
  sys.stdout.write('\n')
  os.system('convert -resize 100x100 spectrum_out.png thumb_out.png')
def profile_plot(self, bbox=None,
        start_freq=None, end_freq=None,
        start_time=None, end_time=None,
        t = None,
    ):  
    """
    generate plot of all of or bbox of spectrogram
    self ... Fingerprinter object
    """
    spc = self.spectrum
    if bbox is None:
        bbox = Bbox(spc, start_freq, end_freq, start_time, end_time)
    freq_slice, time_slice = bbox.ix()
    x2 = spc.db_arr[freq_slice, time_slice]
    mn, mx = np.percentile(x2,[25,99.9])
    Z = np.clip(x2,mn,mx)
    X,Y = np.meshgrid(spc.times[time_slice], spc.freqs[freq_slice],)
    # --build plot--
    plt.clf()
    # spectrograph heatmap
    plt.pcolormesh(X,Y,Z, cmap='gist_heat_r', shading='gouraud')
    # place dots on detected harmonic peaks
    if hasattr(self, 'harmonic_intvl') and self.harmonic_intvl is not None:
        for i in self.harmonics:
            plt.plot(spc.times, self.harmonic_intvl*i, '.', color='g', alpha=.25)
    # shade areas of interest
    for start, stop in self.interest_areas:
        plt.axvspan(start, stop, facecolor='y', alpha=.5, edgecolor='k')        
    if t is not None:
        plt.axvline(t, color='b')
    plt.grid(b=True, which='major',linestyle='-', alpha=.5)
    plt.xlim(bbox.start_time, bbox.end_time)
    plt.xlabel('Time (s)')
    plt.ylabel('Frequency (Hz)')
    return plt.gcf()
Example #12
0
def plot_frag_arrays(conditions, transcript_arrays, outputdir, transcript_coords, transcripts_dict, paired):
	#Plot sererately per transcript
	for transcript in sorted(transcripts_dict): #Key is transcript, values are dict of positions and then fragments 
		a = 1
		for frag_pos in sorted(transcripts_dict[transcript]):
			c = 1
			for sample in sorted(transcript_arrays[transcript]):
				length_of_transcript = len(transcript_arrays[transcript][sample])
				base_label = np.array(xrange(length_of_transcript))

				c += 1 #Count number of samples for legend size	
				plt.plot(base_label, transcript_arrays[transcript][sample], label="{}".format(sample)) #Same as transcripts
			
			start_pos = int(frag_pos[1]) - int(transcript_coords[transcript][1])
			end_pos = int(frag_pos[2]) - int(transcript_coords[transcript][1])
			plt.axvspan(start_pos, end_pos, color='red', alpha=0.2)
			if paired:
				start_pos = int(frag_pos[3]) - int(transcript_coords[transcript][1])
				end_pos = int(frag_pos[4]) - int(transcript_coords[transcript][1])
				plt.axvspan(start_pos, end_pos, color='red', alpha=0.2)
		#Plot labels
			if c <= 4: #Control size of legend
				plt.legend(bbox_to_anchor=(1.05, 1), loc=1, borderaxespad=0., prop={'size':5})
			else:
				plt.legend(bbox_to_anchor=(1.05, 1), loc=1, borderaxespad=0., prop={'size':7})
			plt.ylabel('Read Count')
			plt.savefig(outputdir+'/plots/{}_{}.png'.format(transcript, a))
			plt.close()
			a += 1
def plot_ks_analysis(lower_y, upper_y, pval, name):
	plt.figure(figsize=(5,10))
	plt.subplot(311)
	ax = plt.gca()
	ax.set_axis_bgcolor('white')
	plt.title('smaller embryos, N={0}'.format(lower_y.shape[0]))
	for i,p in enumerate(pval):
		if p < 0.01:
			plt.axvspan(i, i+1, facecolor='0.5', alpha=0.5)
		elif p < 0.05:
			plt.axvspan(i, i+1, facecolor='0.5', alpha=0.2)
	plt.plot(np.exp(lower_y.T[:,:100]))
	plt.subplot(312)
	plt.title('bigger embryos, N={0}'.format(upper_y.shape[0]))
	plt.plot(np.exp(upper_y.T[:,:100]))
	plt.subplot(313)
	plt.plot(pval)
	plt.plot(np.ones(shape=len(pval))*0.05, "g--", label=r"5\% confidence")
	plt.plot(np.ones(shape=len(pval))*0.01, "r--", label=r"1\% confidence")
	plt.yscale('log')
	plt.ylabel('pvalue', fontsize=10)
	plt.xlabel('x/L')
	plt.suptitle(name)
	plt.savefig(ensure_dir(os.path.join(config.plots_path,
							"full_mutant", "ks_{0}.pdf".format(name))))
	plt.clf()
Example #14
0
def _periodogram_plot(title, column, data, trend, peaks):
    """display periodogram results using matplotlib"""

    periods, power = periodogram(data)
    plt.figure(1)
    plt.subplot(311)
    plt.title(title)
    plt.plot(data, label=column)
    if trend is not None:
        plt.plot(trend, linewidth=3, label="broad trend")
        plt.legend()
        plt.subplot(312)
        plt.title("detrended")
        plt.plot(data - trend)
    else:
        plt.legend()
        plt.subplot(312)
        plt.title("(no detrending specified)")
    plt.subplot(313)
    plt.title("periodogram")
    plt.stem(periods, power)
    for peak in peaks:
        period, score, pmin, pmax = peak
        plt.axvline(period, linestyle='dashed', linewidth=2)
        plt.axvspan(pmin, pmax, alpha=0.2, color='b')
        plt.annotate("{}".format(period), (period, score * 0.8))
        plt.annotate("{}...{}".format(pmin, pmax), (pmin, score * 0.5))
    plt.tight_layout()
    plt.show()
    def PlotConcentrations(self, params, figure=None):
        concentrations = params['concentrations']

        if figure is None:
            figure = plt.figure()
        plt.xscale('log', figure=figure)
        plt.ylabel('Compound KEGG ID', figure=figure)
        plt.xlabel('Concentration [M]', figure=figure)
        plt.yticks(range(self.Nc, 0, -1), self.cids,
                   fontproperties=FontProperties(size=8))
        plt.plot(concentrations.T, range(self.Nc, 0, -1), '*b', figure=figure)

        x_min = concentrations.min() / 10
        x_max = concentrations.max() * 10
        y_min = 0
        y_max = self.Nc + 1
       
        for c, cid in enumerate(self.cids):
            plt.text(concentrations[0, c] * 1.1, self.Nc - c, cid, \
                       figure=figure, fontsize=6, rotation=0)
            b_low, b_up = self.GetConcentrationBounds(cid)
            plt.plot([b_low, b_up], [self.Nc - c, self.Nc - c], '-k', linewidth=0.4)

        if self.c_range is not None:
            plt.axvspan(self.c_range[0], self.c_range[1],
                          facecolor='r', alpha=0.3, figure=figure)
        plt.axis([x_min, x_max, y_min, y_max], figure=figure)
        return figure
Example #16
0
def plot(rr_file, tag_file):
    """
    Paint results of acquisition
    @param rr_file: Path to file that contains rr values
    @param tag_file: Path to file that contains tag values
    """

    import matplotlib.pyplot as plt
    plt.switch_backend("WXAgg")

    colors = ['orange', 'green', 'lightblue', 'grey', 'brown', 'red', 'yellow', 'black', 'magenta', 'purple']
    shuffle(colors)
    rr_values = parse_rr_file(rr_file)
    hr_values = map(lambda rr: 60 / (float(rr) / 1000), rr_values)
    tag_values = parse_tag_file(tag_file)
    x = [x / 1000 for x in cumsum(rr_values)]
    y = hr_values
    plt.plot(x, y)

    for tag in tag_values:
        c = colors.pop()
        plt.axvspan(tag[0], tag[0] + tag[2], facecolor=c, alpha=.8, label=tag[1])

    plt.ylabel('Heart rate (bpm)')
    plt.xlabel('Time (s)')
    plt.title('Acquisition results')
    plt.ylim(ymin=min(min(y) - 10, 40), ymax=max(max(y) + 10, 150))
    plt.legend()
    plt.show()
Example #17
0
 def _ref_single_cell_plot(self):
     seg = neo.PickleIO(self.cell_signal_path).read()[0]
     signal = seg.analogsignals[0]
     plt.figure()
     v_line, = plt.plot(signal.times, signal)
     for label, start, duration in zip(seg.epochs[0].labels,
                                       seg.epochs[0].times,
                                       seg.epochs[0].durations):
         if label == 'subthreshold':
             end = start + duration
             plt.axvspan(start, end, facecolor=self.subthresh_colour,
                         alpha=0.05)
             plt.axvline(start, linestyle=':', color='gray', linewidth=0.5)
             plt.axvline(end, linestyle=':', color='gray', linewidth=0.5)
     fig = plt.gcf()
     fig.set_figheight(5)
     fig.set_figwidth(5)
     fig.suptitle('PyPe9 Simulation Output')
     plt.xlim((seg.analogsignals[0].t_start, seg.analogsignals[0].t_stop))
     plt.xlabel('Time (ms)')
     plt.ylabel('Analog signals (mV)')
     plt.title("Analog Signals", fontsize=12)
     plt.legend(handles=[
         v_line,
         mp.Patch(facecolor='white', edgecolor='grey',
                  label='subVb regime', linewidth=0.5, linestyle=':'),
         mp.Patch(facecolor=self.subthresh_colour, edgecolor='grey',
                  label='subthreshold regime', linewidth=0.5,
                  linestyle=':')])
     plt.savefig(self.ref_single_cell_path, dpi=100.0)
Example #18
0
def draw_shannon_distrib(neut_h, obs_h, outfile=None, filetype=None,
                         size=(15, 15)):
    '''
    draws distribution of Shannon values for random neutral

    :argument neut_h: list of Shannon entropies corresponding to simulation under neutral model
    :argument obs_h: Shannon entropy of observed distribution of abundance
    :argument None outfile: path were image will be saved, if none, plot
       will be shown using matplotlib GUI
    :argument None filetype: pdf or png
    :argument (15,15) size: size in inches of the drawing
    
    '''
    neut_h = np.array ([float (x) for x in neut_h])
    obs_h = float (obs_h)
    pyplot.hist(neut_h, 40, color='green', histtype='bar', fill=True)
    pyplot.axvline(float(obs_h), 0, color='r', linestyle='dashed')
    pyplot.axvspan (float(obs_h) - neut_h.std(), float(obs_h) + neut_h.std(),
                    facecolor='orange', alpha=0.3)
    pyplot.xlabel('Shannon entropy (H)')
    pyplot.ylabel('Number of observations over %s simulations' % (len (neut_h)))
    pyplot.title("Histogram of entropies from %s simulations compared to \nobserved entropy (red), deviation computed from simulation" % (len (neut_h)))
    fig = pyplot.gcf()
    dpi = fig.get_dpi()
    fig.set_size_inches (size)
    if outfile:
        fig.savefig(outfile, dpi=dpi+30, filetype=filetype)
        pyplot.close()
    else:
        pyplot.show()
Example #19
0
    def mousepress(self, event):
        if event.inaxes and event.button==1:
            self.eventpress = event
            if self.region_start is None:
                # Starting a new on-pulse region
                xx =  int(event.xdata+0.5)
                self.region_start = xx
                self.region_start_line = plt.axvline(xx, c='k', ls='--')
            else:
                # Selected an on-pulse region
                if self.region_start > event.xdata:
                    xlo = 0
                    xhi = int(event.xdata+0.5)
                    self.regions.append(plt.axvspan(xlo, xhi, picker=True,
                                                    facecolor='b', alpha=0.5))
                    xlo = int(self.region_start+0.5)
                    xhi = self.nbin
                    self.regions.append(plt.axvspan(xlo, xhi, picker=True,
                                                    facecolor='b', alpha=0.5))
                else:
                    xlo = int(self.region_start+0.5)
                    xhi = int(event.xdata+0.5)
                    self.regions.append(plt.axvspan(xlo, xhi, picker=True,
                                                    facecolor='b', alpha=0.5))
                
                # Remove line
                self.region_start_line.remove()
                self.region_start = None
                self.region_start_line = None

            self.fig.canvas.draw()
def showdata(f, info):
    print(repr(info))
    offA, offB = info
    offMin, offMax = min(info), max(info)
    
    offMax += 11*EODSamples*BytesPerSample
    offMin -= 10*EODSamples*BytesPerSample
    offMin = max(0, offMin)
    rlen = offMax - offMin
    
    posA = (offA-offMin) / BytesPerSample
    posB = (offB-offMin) / BytesPerSample    
    
    f.seek(offMin)
    data = f.read(rlen)
    arr = np.frombuffer(data, dtype=np.float32)
   
    plt.figure(1,figsize=(24,14))
    plt.clf()
    ax = None
    for ch in xrange(NumChannels):
        charr = arr[ch::NumChannels]
        if ch == 0:
            ax = plt.subplot(NumChannels, 1, 1)
        else:
            plt.subplot(NumChannels, 1, ch+1, sharex=ax)
        plt.plot(charr,'k')
        plt.axvspan(posA, posA + EODSamples, fc='r', ec='r', alpha=.5)
        plt.axvspan(posB, posB + EODSamples, fc='g', ec='g', alpha=.5)
        plt.axis([0, len(charr), -10, 10])
        plt.ylabel('ch%d'%ch)
        
    plt.show()
Example #21
0
def pointing_spectra(basename, savename, axlims):  # Require axlims a priori
    """Plot four raw (but, averaged) spectra for single pnting"""
    left = np.load(basename + '_left0.npy')
    #leftn = np.load(basename + '_left_cal0.npy')
    right = np.load(basename + '_right0.npy')
    #rightn = np.load(basename + '_right_cal0.npy')

    fig = plt.figure(figsize=(4,3))
    plt.axis(axlims)
    plt.plot(right, '-r', rasterized=True)
    #plt.plot(rightn, ':r', rasterized=True)
    plt.plot(left, '-b', rasterized=True)
    #plt.plot(leftn, ':b', rasterized=True)

    # Highlight where 1420 MHz is for each spectrum
    # Sent to 148.5, 151.5 MHz for left, right (range: 144-156 MHz)
    # Center channels: 3072, 5120 \pm 341 channels (covers 1 MHz)
    plt.axvspan(3072-341, 3072+341, facecolor='b', alpha=0.3)
    plt.axvspan(5120-341, 5120+341, facecolor='r', alpha=0.3)

    plt.legend(('$f_{\mathrm{LO}} = 1268.9$ MHz',
                #'$f_{\mathrm{LO}} = 1268.9$ MHz, noise',
                '$f_{\mathrm{LO}} = 1271.9$ MHz'))#,
                #'$f_{\mathrm{LO}} = 1271.9$ MHz, noise'))
    plt.xlabel('Channel number')
    plt.ylabel('Power (unknown units)')
    fig.set_tight_layout(True)  # plt.tight_layout()
    plt.savefig(os.path.join('..','plots',savename+'.pdf'), dpi=400)
    plt.clf()
def plotSpectrum(template, conditions, outname):
    # Plot the template spectrum we are passed with frequency on the x-axis.
    # Initialise the plot.
    plt.clf()
    fig = plt.figure()
    ax = fig.add_subplot(111)

    # These are the colours we can use for the different lines.
    colours = [ "blue", "green", "red", "black", "yellow" ]

    # Go through the conditions (usually weather conditions) and plot a line for each.
    for i, c in enumerate(conditions):
        ax.plot(template[c]['centreFrequency'], template[c]['value'], colours[i], label=c)

    # Set the x-axis limits to be tight on the actual frequency range.
    plt.xlim(template[c]['centreFrequency'][0], template[c]['centreFrequency'][-1])
    plt.xlabel("Frequency [MHz]")
    plt.ylabel("RMS noise level [mJy/beam]")

    # Ensure that the x- and y-axes don't have an offset value.
    ax.get_yaxis().get_major_formatter().set_useOffset(False)
    ax.get_xaxis().get_major_formatter().set_useOffset(False)

    # Put the legend with the condition names at the top of the plot outside the border.
    plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
               ncol=len(c), mode="expand", borderaxespad=0.)

    # Highlight the regions that are flagged in the template.
    for i in xrange(0, len(template[conditions[0]]['flags'])):
        if (template[conditions[0]]['flags'][i] == True):
            x1 = template[conditions[0]]['centreFrequency'][i] - template[c]['channelWidth'] / 2.0
            x2 = template[conditions[0]]['centreFrequency'][i] + template[c]['channelWidth'] / 2.0
            plt.axvspan(x1, x2, alpha=0.2, edgecolor='none', facecolor='red')
    plt.savefig(outname)
def renderFullSignal(data, deltaT, labels, input_data, window_size, window_shift, ignore_list=[0], name="full.png"):
    fig = plt.figure()
    #Render the image huge
    fig.set_size_inches(300,25)
    plt.plot(data)
    
    #Generate a set of unique colors for each label
    colors = genColors(labels)
    
    #for color in colors.keys():
    #    print color, colors[color]
    
    #Flip through all the labels    
    #The length of each box drawn is the length of the sample
    for index in range(len(labels)):
        label = labels[index]
        #Label 0 should be ignored, it is the "no other cluster" cluster
        if label not in ignore_list:
            color = colors[label]
            #Draw a box. X coords are in data units, y coords are in the range 0-1
            #X start of the box is the size of the sample offset times the index of the sample
            xmin = index * window_shift
            #X end of the box is the start of the box plus the sample length
            xmax = xmin + window_size
            plt.axvspan(xmin, xmax, ymin=0.25, ymax=0.75, alpha=0.4, ec="none", fc=colors[label])
     
     
    fig.savefig(name)
    plt.close()
Example #24
0
 def fig_nucleotide_gene(self):
     print '# Plotting gene in nucleotide resolution.'
     fig = plt.figure(figsize=(15, 10), dpi=80, facecolor='w', edgecolor='k')
     if len(self.experiments) > 1:
         print 'More than one experiment. Preprocess concat file to get only one experiment:' \
               "cat your.concat | awk '$7 == \"experiment\" {print $0}' > experiment.concat"
         exit()
     else:
         e = self.experiments[0]
         fig_no, plot_no = 0, 0
         for i_gene_id in self.genes_id_list:
             gene_name = self.id_to_names[i_gene_id]
             plot_no += 1
             ax = fig.add_subplot(5, 1, plot_no)
             fig.tight_layout()
             plt.title(e)
             ax.set_ylabel("no. of reads")
             ax.set_xlabel('ID: '+i_gene_id+', Name: '+gene_name)
             try:
                 self.data[gene_name][e] = self.data[gene_name][e][self.three_prime_flank:-self.three_prime_flank:] # plot only gene
                 bar = ax.bar(self.data[gene_name][e]['position'], self.data[gene_name][e]['hits'], width=0.5)
                 for i in self.genes[gene_name]['exons']:
                     plt.axvspan(i[0], i[1], alpha=0.2, color='orange')
                 plt.xticks(list(self.data[gene_name][e]['position']), list(self.data[gene_name][e]['nucleotides']), fontsize=8)
             except KeyError:
                 plt.text(0.5,0.5,"NO READS")
             if plot_no == 5:
                 fig_no += 1
                 plt.savefig(self.prefix+'nuc_gene'+'_l'+str(self.lookahead)+'_t'+str(self.hits_threshold)+'_'+'_fig_'+str(fig_no)+'.png')
                 plt.clf()
                 plot_no = 0
         if plot_no > 0:
             plt.savefig(self.prefix+'nuc_gene'+'_l'+str(self.lookahead)+'_t'+str(self.hits_threshold)+'_'+'_fig_'+str(fig_no+1)+'.png')
             plt.clf()
     return True
Example #25
0
 def plot_trade_windows(self, dt_s, dt_e, f_width):
     ts_signal = self.getBollingerValue(dt_s, dt_e)
     dt_previous = ''
     s_color_previous = ''
     
     for i in range(len(ts_signal)):
         # get values for the current date
         dt = ts_signal.index[i]
         s = ts_signal[dt]
         s_color = 'r' if s >= f_width else 'g' if s <= -f_width else ''
                         
         # update the figure: on change and in last day
         if s_color != s_color_previous \
         or (i == len(ts_signal)-1):
             
             # if we are ending a trade opportunity window
             if s_color_previous != '':
                 # shade the trade opportunity window
                 plt.axvspan(dt_previous, dt, color=s_color_previous, alpha=0.25)
                 
                 # draw the end line
                 plt.axvline(x=dt, color=s_color_previous, alpha=0.5)
             
             # if we are starting a new trade opportunity window
             if s_color != '':
                 # draw the start line
                 plt.axvline(x=dt, color=s_color, alpha=0.5)
             
             # save the last event
             s_color_previous = s_color
             dt_previous = dt
def day_e_plot(results, plot_date, save):

    import matplotlib.pyplot as plt

    fig = plt.figure()

    results['USAGE'].ix[plot_date].plot(linestyle='--', linewidth=5, color=darkgray)

    results['grid_demand_peak'].ix[plot_date].plot(marker='', linestyle='-', linewidth=2, color=redorange)
    results['grid_demand_offpeak'].ix[plot_date].plot(marker='', linestyle='-', linewidth=2, color=purple)
    results['grid_store'].ix[plot_date].plot(marker='', linestyle='-', linewidth=2, color=mint)
    results['storage_available'].ix[plot_date].plot(marker='', linestyle='-', linewidth=2, color=apple)
    results['storage_send'].ix[plot_date].plot(marker='', linestyle='-', linewidth=2, color=navy, grid='off')

    plt.axvspan(plot_date+' 10:00:00',plot_date+' 19:00:00', facecolor=lightgray, alpha=0.5)

    plt.legend(['Demand',
                'Peak Grid for Demand',
                'Off-Peak Grid for Demand',
                'Grid to Battery',
                'Storage Available',
                'Storage to Demand'],
               labelspacing=.2,
               prop={'size':10})

    plt.title('Demand-Side Storage Model, Hourly Energy State \n %s' %plot_date)
    plt.ylabel('Hourly Electricity State (kWh)', fontsize=14)

    if save == True:

        filename = 'Daily_Energy_State_'+plot_date+'.png'

        plt.savefig(filename)

    plt.show()
def day_purchase(results, plot_date, save):

    import matplotlib.pyplot as plt

    fig = plt.figure()

    results['USAGE'].ix[plot_date].plot(linestyle='--', linewidth=3, color='gray')

    #results['purchase_peak'].ix[plot_date].plot(marker='.', linestyle='', markersize=13, color='red')
    #results['purchase_offpeak'].ix[plot_date].plot(marker='.', linestyle='', markersize=13, color='orange', grid='off')

    results['purchase_peak'].ix[plot_date].plot(marker='', linestyle='-', linewidth=2, color='red')
    results['purchase_offpeak'].ix[plot_date].plot(marker='', linestyle='-', linewidth=2, color='orange', grid='off')

    plt.axvspan(plot_date+' 10:00:00',plot_date+' 19:00:00', facecolor=lightgray, alpha=0.5)

    plt.legend(['Demand','Purchased During Peak Hours', 'Purchased During Off-Peak Hours'], labelspacing=.2, prop={'size':10})
    plt.ylabel('Electricity (kWh)', fontsize=14)
    plt.title('Demand-Side Storage Model, Hourly Electricity From Grid \n %s' %plot_date)

    if save == True:

        filename = 'Daily_Energy_Purchased_'+plot_date+'.png'

        plt.savefig(filename)

    plt.show()
Example #28
0
def ScatterPlot(TransitionForces,ListOfSepAndFits,ExpectedContourLength,
                OutDir):
    """
    Makes a scatter plot of the contour length and transition forces

    Args:
        TransitionForces: array, each element the transition region for curve i
        ListOfSepAndFits: array, each element the output of GetWLCFits
        ExpectedContourLength: how long we expect the construct to be
        OutDir: base directory, for saving stuff
    """
    L0Arr = []
    TxArr = []
    for (SepNear,FitObj),TransitionFoces in zip(ListOfSepAndFits,
                                                TransitionForces):
        MedianTx = np.median(TransitionFoces)
        L0,Lp,_,_ = FitObj.Params()
        L0Arr.append(L0)
        TxArr.append(MedianTx)
    # go ahead an throw out ridiculous data from the WLC, where transition
    # normalize the contour length to L0
    L0Arr = np.array(L0Arr)/ExpectedContourLength
    # convert to useful units
    L0Plot = np.array(L0Arr)
    TxPlot =  np.array(TxArr) * 1e12
    fig = pPlotUtil.figure(figsize=(12,12))
    plt.subplot(2,2,1)
    plt.plot(L0Plot,TxPlot,'go',label="Data")
    alpha = 0.3
    ColorForce = 'r'
    ColorLength = 'b'
    plt.axhspan(62,68,color=ColorForce,label=r"$F_{\rm tx}$ $\pm$ 5%",
                alpha=alpha)
    L0BoxMin = 0.9
    L0BoxMax = 1.1
    plt.axvspan(L0BoxMin,L0BoxMax,color=ColorLength,
                label=r"L$_{\rm 0}$ $\pm$ 10%",alpha=alpha)
    fudge = 1.05
    # make the plot boundaries OK
    MaxX = max(L0BoxMax,max(L0Plot))*fudge
    MaxY = 90
    plt.xlim([0,MaxX])
    plt.ylim([0,MaxY])
    pPlotUtil.lazyLabel("",r"F$_{\rm overstretch}$ (pN)",
                        "DNA Characterization Histograms ",frameon=True)
    ## now make 1-D histograms of everything
    # subplot of histogram of transition force
    HistOpts = dict(alpha=alpha,linewidth=0)
    plt.subplot(2,2,2)
    TransitionForceBins = np.linspace(0,MaxY)
    plt.hist(TxPlot,bins=TransitionForceBins,orientation="horizontal",
             color=ColorForce,**HistOpts)
    pPlotUtil.lazyLabel("Count","","")
    plt.ylim([0,MaxY])
    plt.subplot(2,2,3)
    ContourBins = np.linspace(0,MaxX)
    plt.hist(L0Plot,bins=ContourBins,color=ColorLength,**HistOpts)
    pPlotUtil.lazyLabel(r"$\frac{L_{\rm WLC}}{L_0}$","Count","")
    plt.xlim([0,MaxX])
    pPlotUtil.savefig(fig,"{:s}Out/ScatterL0vsFTx.png".format(OutDir))
Example #29
0
 def plot(self, plot = None, ybound = None, fig = None, title = None):
     if fig == None:
         fig = plt.figure()
     if self.log == False:
         plt.plot(self.Follows[:,0], self.Follows[:,1], 'r-', label='read')
         plt.plot(self.Follows[:,0], self.Follows[:,2], 'b-', label='write')
     else:
         plt.semilogy(self.Follows[:,0], self.Follows[:,1], 'r-', label='read')
         plt.semilogy(self.Follows[:,0], self.Follows[:,2], 'b-', label='write')
     ax, = fig.get_axes()
     if title == None:
         plt.title(r'Distribution of "follows" observations')
     else:
         plt.title(title)
     if ybound != None:
         ax.set_ybound(upper = ybound)
     plt.axvspan(self.low, self.high, facecolor='g', alpha=0.5)
     plt.legend()
     plt.ylabel(r'$p_f(x)$')
     plt.xlabel('observation size (MB)')
     if plot == None:
         plt.show()
     else:
         plt.savefig(plot)
     return(fig)
def main():
    tests = 100000
    net_scores_1 = [trial(SCORES_1) for _ in range(tests)]
    net_scores_2 = [trial(SCORES_2) for _ in range(tests)]

    h1 = plt.hist(net_scores_1, 16, color='darkblue', alpha=0.4,
                  label=r'$scoring_1$')
    h2 = plt.hist(net_scores_2, 16, color='red', alpha=0.7,
                  label=r'$scoring_2$')

    actual_difference = 73.5 - 66.9
    plt.axvline(actual_difference, lw=2, color='black',
                label=r'$Actual\ score\ difference$')
    plt.axvspan(significance_bound(h1, 0.05),
                significance_bound(h1, 0.95),
                color='skyblue',
                alpha=0.3,
                label=r'$2\sigma_1$')
    plt.axvspan(significance_bound(h2, 0.05),
                significance_bound(h2, 0.95),
                color='salmon',
                alpha=0.4,
                label=r'$2\sigma_2$')
    plt.legend()
    plt.show()
        eventData = loader.get_session_events(
            cellInfo['ephysDirs'][cellInfo['laserIndex'][-1]])
        spikeData = loader.get_session_spikes(
            cellInfo['ephysDirs'][cellInfo['laserIndex'][-1]],
            cellInfo['tetrode'],
            cluster=cellInfo['cluster'])
        eventOnsetTimes = loader.get_event_onset_times(eventData)
        spikeTimestamps = spikeData.timestamps
        timeRange = [-0.1, 0.4]
        spikeTimesFromEventOnset, trialIndexForEachSpike, indexLimitsEachTrial = spikesanalysis.eventlocked_spiketimes(
            spikeTimestamps, eventOnsetTimes, timeRange)
        pRaster, hcond, zline = extraplots.raster_plot(
            spikeTimesFromEventOnset, indexLimitsEachTrial, timeRange)
        plt.setp(pRaster, ms=4)
        plt.xlabel('Time from laser onset (sec)')
        plt.axvspan(0, 0.1, alpha=0.2, color='blue')

        plt.subplot(gs[indCell, 1])
        bandIndex = cellInfo['bandIndex'][0]
        eventData = loader.get_session_events(cellInfo['ephysDirs'][bandIndex])
        spikeData = loader.get_session_spikes(cellInfo['ephysDirs'][bandIndex],
                                              cellInfo['tetrode'],
                                              cluster=cellInfo['cluster'])
        eventOnsetTimes = loader.get_event_onset_times(eventData)
        spikeTimestamps = spikeData.timestamps
        timeRange = [-0.2, 1.5]
        bandBData = loader.get_session_behavior(
            cellInfo['behavDirs'][bandIndex])
        bandEachTrial = bandBData['currentBand']
        ampEachTrial = bandBData['currentAmp']
        numAmps = len(np.unique(ampEachTrial))
Example #32
0
        c[i:i + buffer_size] = signal[i:i + buffer_size]

c = norm(c) * makeup_gain

#draw
plt.plot(range(len(signal)), signal, "black")

plt.plot(range(len(signal)), c, "pink")
plt.plot(range(len(compression)),
         compression * -1 + 1 - threshold,
         "red",
         dashes=[6, 2])
plt.plot(range(len(signal)), gr * -1 + 1, "blue")
plt.axhline(y=threshold, color="green")

for i in range(len(keyframes)):
    plt.axvline(x=keyframes[i], color="yellow")

for v in segments(keyframes):
    plt.axvspan(v[0], v[1], facecolor='0.2', alpha=0.5)

for v in segments(keyframes, False):
    plt.axvspan(v[0], v[1], facecolor='0.5', alpha=0.2)

plt.show()

#write file
c *= 32767
c = np.int16(c)
wavfile.write("file.wav", 44100, c)
Example #33
0
    def __init__(self,
                 obsfilename,
                 paramfilename,
                 starnum,
                 wvlcorr,
                 galaxyname,
                 slitmaskname,
                 globular,
                 lines,
                 RA,
                 Dec,
                 obsspecial=None,
                 plot=False,
                 hires=None,
                 smooth=None,
                 specialparams=None):

        # Observed star
        self.obsfilename = obsfilename  # File with observed spectra
        self.paramfilename = paramfilename  # File with parameters of observed spectra
        self.starnum = starnum  # Star number
        self.galaxyname = galaxyname  # Name of galaxy
        self.slitmaskname = slitmaskname  # Name of slitmask
        self.globular = globular  # Parameter marking if globular cluster
        self.lines = lines  # Parameter marking whether or not to use revised or original linelist

        # If observed spectrum comes from moogify file (default), open observed file and continuum normalize as usual
        if obsspecial is None:

            # Output filename
            if self.globular:
                self.outputname = '/raid/madlr/glob/' + galaxyname + '/' + slitmaskname
            else:
                self.outputname = '/raid/madlr/dsph/' + galaxyname + '/' + slitmaskname

            # Open observed spectrum
            self.specname, self.obswvl, self.obsflux, self.ivar, self.dlam, self.zrest = open_obs_file(
                self.obsfilename, retrievespec=self.starnum)

            # Get measured parameters from observed spectrum
            self.temp, self.logg, self.fe, self.alpha, self.fe_err = open_obs_file(
                self.paramfilename,
                self.starnum,
                specparams=True,
                objname=self.specname,
                inputcoords=[RA, Dec])
            if specialparams is not None:
                self.temp = specialparams[0]
                self.logg = specialparams[1]
                self.fe = specialparams[2]
                self.alpha = specialparams[3]

            if plot:
                # Plot observed spectrum
                plt.figure()
                plt.plot(self.obswvl, self.obsflux, 'k-')
                plt.axvspan(4749, 4759, alpha=0.5, color='blue')
                plt.axvspan(4778, 4788, alpha=0.5, color='blue')
                plt.axvspan(4818, 4828, alpha=0.5, color='blue')
                plt.axvspan(5389, 5399, alpha=0.5, color='blue')
                plt.axvspan(5532, 5542, alpha=0.5, color='blue')
                plt.axvspan(6008, 6018, alpha=0.5, color='blue')
                plt.axvspan(6016, 6026, alpha=0.5, color='blue')
                plt.axvspan(4335, 4345, alpha=0.5, color='red')
                plt.axvspan(4856, 4866, alpha=0.5, color='red')
                plt.axvspan(6558, 6568, alpha=0.5, color='red')
                plt.savefig(self.outputname + '/' + self.specname + '_obs.png')
                plt.close()

            # Get synthetic spectrum, split both obs and synth spectra into red and blue parts
            synthfluxmask, obsfluxmask, obswvlmask, ivarmask, mask = mask_obs_for_division(
                self.obswvl,
                self.obsflux,
                self.ivar,
                temp=self.temp,
                logg=self.logg,
                fe=self.fe,
                alpha=self.alpha,
                dlam=self.dlam,
                lines=self.lines)

            if plot:
                # Plot spliced synthetic spectrum
                plt.figure()
                plt.plot(obswvlmask[0], synthfluxmask[0], 'b-')
                plt.plot(obswvlmask[1], synthfluxmask[1], 'r-')
                plt.savefig(self.outputname + '/' + self.specname +
                            '_synth.png')
                plt.close()

            # Compute continuum-normalized observed spectrum
            self.obsflux_norm, self.ivar_norm = divide_spec(
                synthfluxmask,
                obsfluxmask,
                obswvlmask,
                ivarmask,
                mask,
                sigmaclip=True,
                specname=self.specname,
                outputname=self.outputname)

            if plot:
                # Plot continuum-normalized observed spectrum
                plt.figure()
                plt.plot(self.obswvl, self.obsflux_norm, 'k-')
                plt.axvspan(4749, 4759, alpha=0.5, color='blue')
                plt.axvspan(4778, 4788, alpha=0.5, color='blue')
                plt.axvspan(4818, 4828, alpha=0.5, color='blue')
                plt.axvspan(5389, 5399, alpha=0.5, color='blue')
                plt.axvspan(5532, 5542, alpha=0.5, color='blue')
                plt.axvspan(6008, 6018, alpha=0.5, color='blue')
                plt.axvspan(6016, 6026, alpha=0.5, color='blue')
                plt.axvspan(4335, 4345, alpha=0.5, color='red')
                plt.axvspan(4856, 4866, alpha=0.5, color='red')
                plt.axvspan(6558, 6568, alpha=0.5, color='red')
                plt.ylim((0, 5))
                plt.savefig(self.outputname + '/' + self.specname +
                            '_obsnormalized.png')
                plt.close()
                np.savetxt(
                    self.outputname + '/' + self.specname +
                    '_obsnormalized.txt',
                    np.asarray((self.obswvl, self.obsflux_norm)).T)

            if wvlcorr:
                print('Doing wavelength correction...')

                try:
                    # Compute standard deviation
                    contdivstd = np.zeros(len(self.ivar_norm)) + np.inf
                    contdivstd[self.ivar_norm > 0] = np.sqrt(
                        np.reciprocal(self.ivar_norm[self.ivar_norm > 0]))

                    # Wavelength correction
                    self.obswvl = fit_wvl(self.obswvl, self.obsflux_norm,
                                          contdivstd, self.dlam, self.temp,
                                          self.logg, self.fe, self.alpha,
                                          self.specname, self.outputname + '/')

                    print('Done with wavelength correction!')

                except Exception as e:
                    print(repr(e))
                    print(
                        'Couldn\'t complete wavelength correction for some reason.'
                    )

            # Crop observed spectrum into regions around Mn lines
            self.obsflux_fit, self.obswvl_fit, self.ivar_fit, self.dlam_fit, self.skip = mask_obs_for_abundance(
                self.obswvl,
                self.obsflux_norm,
                self.ivar_norm,
                self.dlam,
                lines=self.lines)

        # Else, check if we need to open a hi-res file to get the spectrum
        elif hires is not None:

            # Output filename
            if self.globular:
                self.outputname = '/raid/madlr/glob/' + galaxyname + '/' + 'hires'
            else:
                self.outputname = '/raid/madlr/dsph/' + galaxyname + '/' + 'hires'

            # Open observed spectrum
            self.specname = hires
            self.obswvl, self.obsflux, self.dlam = open_obs_file(
                self.obsfilename, hires=True)

            # Get measured parameters from obsspecial keyword
            self.temp = obsspecial[0]
            self.logg = obsspecial[1]
            self.fe = obsspecial[2]
            self.alpha = obsspecial[3]
            self.fe_err = obsspecial[4]
            self.zrest = obsspecial[5]

            self.ivar = np.ones(len(self.obsflux))

            # Correct for wavelength
            self.obswvl = self.obswvl / (1. + self.zrest)
            print('Redshift: ', self.zrest)

            # Get synthetic spectrum, split both obs and synth spectra into red and blue parts
            synthfluxmask, obsfluxmask, obswvlmask, ivarmask, mask = mask_obs_for_division(
                self.obswvl,
                self.obsflux,
                self.ivar,
                temp=self.temp,
                logg=self.logg,
                fe=self.fe,
                alpha=self.alpha,
                dlam=self.dlam,
                lines=self.lines,
                hires=True)

            # Compute continuum-normalized observed spectrum
            self.obsflux_norm, self.ivar_norm = divide_spec(
                synthfluxmask,
                obsfluxmask,
                obswvlmask,
                ivarmask,
                mask,
                specname=self.specname,
                outputname=self.outputname,
                hires=True)

            if smooth is not None:

                # Crop med-res wavelength range to match hi-res spectrum
                smooth = smooth[np.where((smooth > self.obswvl[0])
                                         & (smooth < self.obswvl[-1]))]

                # Interpolate and smooth the synthetic spectrum onto the observed wavelength array
                self.dlam = np.ones(len(smooth)) * 0.7086
                self.obsflux_norm = smooth_gauss_wrapper(
                    self.obswvl, self.obsflux_norm, smooth, self.dlam)
                self.obswvl = smooth
                self.ivar_norm = np.ones(len(self.obswvl)) * 1.e4

            if plot:
                # Plot continuum-normalized observed spectrum
                plt.figure()
                plt.plot(self.obswvl, self.obsflux_norm, 'k-')
                plt.savefig(self.outputname + '/' + self.specname +
                            '_obsnormalized.png')
                plt.close()

            # Crop observed spectrum into regions around Mn lines
            self.obsflux_fit, self.obswvl_fit, self.ivar_fit, self.dlam_fit, self.skip = mask_obs_for_abundance(
                self.obswvl,
                self.obsflux_norm,
                self.ivar_norm,
                self.dlam,
                lines=self.lines,
                hires=True)

        # Else, take both spectrum and observed parameters from obsspecial keyword
        else:

            # Output filename
            self.outputname = '/raid/madlr/test/' + slitmaskname

            self.obsflux_fit = obsspecial[0]
            self.obswvl_fit = obsspecial[1]
            self.ivar_fit = obsspecial[2]
            self.dlam_fit = obsspecial[3]
            self.skip = obsspecial[4]
            self.temp = obsspecial[5]
            self.logg = obsspecial[6]
            self.fe = obsspecial[7]
            self.alpha = obsspecial[8]
            self.fe_err = obsspecial[9]

            self.specname = self.slitmaskname

        # Splice together Mn line regions of observed spectra
        print('Skip: ', self.skip)

        self.obsflux_final = np.hstack((self.obsflux_fit[self.skip]))
        self.obswvl_final = np.hstack((self.obswvl_fit[self.skip]))
        self.ivar_final = np.hstack((self.ivar_fit[self.skip]))
Example #34
0
colors = ['#D9853B', '#d62728', '#2ca02c']
for j in raw_motion_data.iterkeys():
    fish_id = j
    motion = raw_motion_data[fish_id][1]
    bouts, bout_indices = bout_detect(motion)
    all_bouts[fish_id] = bouts
    all_bout_indices[fish_id] = bout_indices

    # Plot the motion and highlight the detected bouts.
    # Try cycling through colours to see if there is patchy detection.
    if bout_detection_plot:
        plt.figure()
        plt.plot(motion)
        for ind, i in enumerate(bout_indices):
            color = colors[ind % len(colors)]
            plt.axvspan(i[0], i[-1], facecolor=color, alpha=0.5)
        plt.title("Bout detection for Fish %d" % (fish_id + 1))
        plt.xlabel("Time (frames)")
        plt.ylabel("Raw Motion (a.u.)")

    # Extract bout parameters from a list of bouts.
    bout_number, bout_durations, interbout_intervals = bout_params(
        bouts, bout_indices)
    all_bout_numbers[fish_id] = bout_number
    all_bout_durations[fish_id] = bout_durations
    all_interbout_intervals[fish_id] = interbout_intervals
    #print bout_number

# Plot 1 - Total Bout Number
# Plot 2 - Bout Durations PER FISH
# Plot 3 - Interbout Intervals PER FISH
def plotSaturation(title,
                   variant,
                   sample_names,
                   data_values,
                   types,
                   verbose=False,
                   hide=False,
                   outputdir='./pictures/'):
    width = variant.end - variant.start
    array = variant.members
    rep = variant.signals
    original_seed = variant.seed
    parameters = len(variant.members)

    fig = plt.figure(figsize=(4, 1))
    ax = fig.add_subplot(111)
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.xaxis.set_ticks_position('bottom')
    ax.yaxis.set_ticks_position('left')

    title_font = {
        'fontname': 'Arial',
        'size': '15',
        'color': 'black',
        'weight': 'bold',
        'verticalalignment': 'bottom'
    }  # Bottom vertical alignment for more space
    axis_font = {'fontname': 'Arial', 'size': '15', 'weight': 'bold'}

    chromosome, start, end, cluster_number = title.split("_")

    xvalues = np.arange(array.shape[1]) * 10

    # ax.set_prop_cycle(color=['grey'])
    # for i in range(parameters):
    #     ax.plot(xvalues, array[i, :], linewidth=0.1, alpha=0.5)

    ax.set_prop_cycle(color=['red'])
    # ax.plot(xvalues, rep, linewidth=0.5, label='representative', alpha=0.5)
    ax.fill_between(xvalues, 0, rep, alpha=0.5, color='red')

    peaks = variant.units

    #######

    # if original_seed != []:
    #     ax.set_prop_cycle(color=['blue'])
    #     ax.plot(xvalues, original_seed, linewidth=1, label='seed')

    # legend = ax.legend(loc='upper right', prop={'size': 2})

    # # The frame is matplotlib.patches.Rectangle instance surrounding the legend.
    # frame = legend.get_frame()
    # frame.set_facecolor('0.90')

    # Set the fontsize
    # for label in legend.get_texts():
    #     label.set_fontsize(5)
    #
    # for label in legend.get_lines():
    #     label.set_linewidth(0.75)  # the legend line width

    fig.set_tight_layout(True)

    variant_id = title.replace("_", ".")[3:]

    if hide:
        plt.axis('off')
    else:
        ax.set_title(
            chromosome + " " + start + "~" + end + " " + "variant " +
            str(int(cluster_number[-1]) + 1), **title_font)
        ax.set_xlabel("bp", **axis_font)
        ax.set_ylabel("Signal", **axis_font)
        for p in peaks:
            plt.axvspan(xmin=p.start - int(start) + width / 100.0,
                        xmax=p.end - int(start) - width / 100.0,
                        ymin=0,
                        ymax=0.05,
                        facecolor='blue',
                        alpha=0.5,
                        edgecolor='black')

    for type in types:
        fig.savefig(outputdir + type + '/' + variant_id + '.png',
                    dpi=600,
                    facecolor='w',
                    edgecolor='w',
                    orientation='portrait',
                    bbox_inches='tight')
    plt.close('all')

    if verbose:
        best_sample_index = variant.best_representative_sample()
        for i in range(len(best_sample_index)):
            index = best_sample_index[i]
            sample_name = sample_names[index]
            output_name = variant_id + " " + sample_name
            cur_data = data_values[index]
            plotBestSample(sample_name, output_name, cur_data, xvalues,
                           outputdir, hide)
    return peaks
Example #36
0
plt.loglog(np.asarray(height), H_ions, label='H_ions')
plt.loglog(np.asarray(height), He_ions, label='He_ions')
plt.loglog(np.asarray(height), O2_ions, label='O2_ions')
plt.loglog(np.asarray(height), O_ions, label='O_ions')
plt.loglog(np.asarray(height), NO_ions, label='NO_ions')
plt.loglog(np.asarray(height), N_ions, label='N_ions')
plt.plot(np.asarray(height),
         neY,
         label=r'n$_e$ (Yabroff, 1961)',
         linestyle='--')
#print(height,ne)

perigeum = 450.
apogeum = 720.

plt.axvspan(perigeum, apogeum, color='y', alpha=0.5, lw=0)

plt.xlabel(r"r (km)")
plt.ylabel(r"plasma density (m$^{-3}$)")
plt.legend(loc=1, prop={'size': 16})
#plt.title(fname)

name = 'ionosphere_' + fname
dir = '../IRI/'
plt.savefig(dir + name + '.png')

plt.show()

#f = open('height.dat', 'w')
#json.dump(height, f)
#f.close()
Example #37
0
theta1k10  = phievo[1500:2999,4]
theta1k30  = phievo[3000:4499,4]
theta1k50  = phievo[4500:5999,4]
theta1k80  = phievo[6000:7499,4]
theta1k100 = phievo[7500:8999,4]

x_tick = [-20,-15,-10,-7.9,-5,0]

plt.semilogy(x,deltk1,label=r"$kc/H_0 = 0.1$")
plt.semilogy(x,deltk10,label=r"$kc/H_0 = 8.36$")
plt.semilogy(x,deltk30,label=r"$kc/H_0 = 85.90$")
plt.semilogy(x,deltk50,label=r"$kc/H_0 = 245.1$")
plt.semilogy(x,deltk80,label=r"$kc/H_0 = 636.8$")
plt.semilogy(x,deltk100,label=r"$kc/H_0 = 1000.0$")
plt.text(-7.9, 10000, "Recombination", size=10)
plt.axvspan(-7.397193821,-6.421947418, color='red', alpha=0.5)
ax.set_xticks(x_tick)
ax.set_xticklabels([-20,-15,-10,r"x$_{eq}$",-5,0])
plt.grid()
plt.title("Dark Matter Perturbations")
plt.xlabel(r'$x$',size=20)
plt.ylabel(r"$\delta$",size=20)
plt.legend(loc="best")
plt.show()

plt.plot(x,deltbk1,label=r"$kc/H_0 = 0.1$")
plt.plot(x,deltbk10,label=r"$kc/H_0 = 8.36$")
plt.plot(x,deltbk30,label=r"$kc/H_0 = 85.90$")
plt.plot(x,deltbk50,label=r"$kc/H_0 = 245.1$")
plt.plot(x,deltbk80,label=r"$kc/H_0 = 636.8$")
plt.plot(x,deltbk100,label=r"$kc/H_0 = 1000.0$")
Example #38
0
plt.text(27, ymax - 0.2, "30", color='black', va='top', alpha=0.5)
plt.text(42, ymax - 0.2, "44", color='black', va='top', alpha=0.5)
plt.text(64, ymax - 0.2, "70", color='black', va='top', alpha=0.5)
plt.text(90, ymax - 0.2, "100", color='black', va='top', alpha=0.5)
plt.text(135, ymax - 0.2, "143", color='black', va='top', alpha=0.5)
plt.text(200, ymax - 0.2, "217", color='black', va='top', alpha=0.5)
plt.text(330, ymax - 0.2, "353", color='black', va='top', alpha=0.5)
plt.text(490, ymax - 0.2, "545", color='black', va='top', alpha=0.5)
plt.text(770, ymax - 0.2, "857", color='black', va='top', alpha=0.5)
#plt.text(1100,ymax-0-2,r"240$\mu$m",size=15,color='#CC0000', va='top')
#plt.text(1800,ymax-0-2,r"140$\mu$m",size=15,color='#CC0000', va='top')
#plt.text(2700,ymax-0-2,r"100$\mu$m",size=15,color='#CC0000', va='top')
#plt.axvspan(band_range1[0],band_range1[1],color='red',hatch='+',alpha=0.3,label='DIRBE')
#plt.axvspan(band_range2[0],band_range2[1],color='red',hatch='+',alpha=0.3)
#plt.axvspan(band_range3[0],band_range3[1],color='red',hatch='+',alpha=0.3)
plt.axvspan(band_range4[0], band_range4[1], color='C2', hatch='//', alpha=0.3)
plt.axvspan(band_range5[0], band_range5[1], color='C2', hatch='//', alpha=0.3)
plt.axvspan(band_range6[0], band_range6[1], color='C2', hatch='//', alpha=0.3)
plt.axvspan(band_range7[0], band_range7[1], color='C2', hatch='//', alpha=0.3)
plt.axvspan(band_range8[0], band_range8[1], color='C2', hatch='//', alpha=0.3)
plt.axvspan(band_range9[0], band_range9[1], color='C2', hatch='//', alpha=0.3)
plt.axvspan(band_range10[0],
            band_range10[1],
            color='C2',
            hatch='//',
            alpha=0.3)
plt.axvspan(band_range11[0],
            band_range11[1],
            color='C2',
            hatch='//',
            alpha=0.3)
Example #39
0
import matplotlib.pyplot as plt

plt.axvspan(76, 76, facecolor='g', alpha=1)
plt.annotate('This is awesome!',
             xy=(76, 0.75))
             #xycoords='data'
             #textcoords='offset points')
             #arrowprops=dict(arrowstyle="->"))
plt.show()
Example #40
0
    def _plot_comparison(self, start_idx, length=100, train=True):
        """Plot the predicted and true output-signals.

        Args:
            start_idx: Start-index for the time-series.
            length: Sequence-length to process and plot.
            train: Boolean whether to use training- or test-set.

        Returns:
            None

        """
        # Initialize key variables
        datetimes = {}
        num_train = self.training_rows

        # End-index for the sequences.
        end_idx = start_idx + length

        # Variables for date formatting
        days = mdates.DayLocator()  # Every day
        months = mdates.MonthLocator()  # Every month
        months_format = mdates.DateFormatter('%b %Y')
        days_format = mdates.DateFormatter('%d')

        # Assign other variables dependent on the type of data we are plotting
        if train is True:
            # Use training-data.
            x_values = self._x_train_scaled[start_idx:end_idx]
            y_true = self._y_train[start_idx:end_idx]
            shim = 'Train'

            # Datetimes to use for training
            datetimes[shim] = self._data.datetime(
            )[:num_train][start_idx:end_idx]

        else:
            # Scale the data
            x_test_scaled = self._x_scaler.transform(
                self._data.vectors_test_all())

            # Use test-data.
            x_values = x_test_scaled[start_idx:end_idx]
            y_true = self._yv_test[start_idx:end_idx]
            shim = 'Test'

            # Datetimes to use for testing
            datetimes[shim] = self._data.datetime(
            )[num_train:][start_idx:end_idx]

        # Input-signals for the model.
        x_values = np.expand_dims(x_values, axis=0)

        # Use the model to predict the output-signals.
        y_pred = self._model.predict(x_values)

        # The output of the model is between 0 and 1.
        # Do an inverse map to get it back to the scale
        # of the original data-set.
        y_pred_rescaled = self._y_scaler.inverse_transform(y_pred[0])

        # For each output-signal.
        for signal in range(len(self._data.labels())):
            # Assign other variables dependent on the type of data plot
            if train is True:
                # Only get current values that are a part of the training data
                current = self._y_current[:num_train][start_idx:end_idx]

                # The number of datetimes for the 'actual' plot must match
                # that of current values
                datetimes['actual'] = self._data.datetime(
                )[:num_train][start_idx:end_idx]

            else:
                # Only get current values that are a part of the test data.
                current = self._y_current[num_train:][start_idx:]

                # The number of datetimes for the 'actual' plot must match
                # that of current values
                datetimes['actual'] = self._data.datetime(
                )[num_train:][start_idx:]

            # Create a filename
            filename = (
                '/tmp/batch_{}_epochs_{}_training_{}_{}_{}_{}.png').format(
                    self._data.batch_size(), self._data.epochs(), num_train,
                    signal, int(time.time()), shim)

            # Get the output-signal predicted by the model.
            signal_pred = y_pred_rescaled[:, signal]

            # Get the true output-signal from the data-set.
            signal_true = y_true[:, signal]

            # Create a new chart
            (fig, axis) = plt.subplots(figsize=(15, 5))

            # Plot and compare the two signals.
            axis.plot(datetimes[shim][:len(signal_true)],
                      signal_true,
                      label='Current +{}'.format(self._data.labels()[signal]))
            axis.plot(datetimes[shim][:len(signal_pred)],
                      signal_pred,
                      label='Prediction')
            axis.plot(datetimes['actual'], current, label='Current')

            # Set plot labels and titles
            axis.set_title('{1}ing Forecast ({0} Future Intervals)'.format(
                self._data.labels()[signal], shim))
            axis.set_ylabel('Values')
            axis.legend(bbox_to_anchor=(1.04, 0.5),
                        loc='center left',
                        borderaxespad=0)

            # Add gridlines and ticks
            ax = plt.gca()
            ax.grid(True)

            # Add major gridlines
            ax.xaxis.grid(which='major', color='black', alpha=0.2)
            ax.yaxis.grid(which='major', color='black', alpha=0.2)

            # Add minor ticks (They must be turned on first)
            ax.minorticks_on()
            ax.xaxis.grid(which='minor', color='black', alpha=0.1)
            ax.yaxis.grid(which='minor', color='black', alpha=0.1)

            # Format the tick labels
            ax.xaxis.set_major_locator(months)
            ax.xaxis.set_major_formatter(months_format)
            ax.xaxis.set_minor_locator(days)

            # Remove tick marks
            ax.tick_params(axis='both', which='both', length=0)

            # Print day numbers on xaxis for Test data only
            if train is False:
                ax.xaxis.set_minor_formatter(days_format)
                plt.setp(ax.xaxis.get_minorticklabels(), rotation=90)

            # Rotates and right aligns the x labels, and moves the bottom of
            # the axes up to make room for them
            fig.autofmt_xdate()

            # Plot grey box for warmup-period if we are working with training
            # data and the start is within the warmup-period
            if (0 < start_idx < self._warmup_steps):
                if train is True:
                    plt.axvspan(datetimes[shim][start_idx],
                                datetimes[shim][self._warmup_steps],
                                facecolor='black',
                                alpha=0.15)

            # Show and save the image
            if self.display is True:
                fig.savefig(filename, bbox_inches='tight')
                plt.show()
            else:
                fig.savefig(filename, bbox_inches='tight')
            print('> Saving file: {}'.format(filename))

            # Close figure
            plt.close(fig=fig)
Example #41
0
def plot_neutrinos_by_year_ara(fluxes,
                               energies,
                               veff_additions,
                               styles=None,
                               years=[
                                   2012, 2013, 2014, 2015, 2016, 2017, 2018,
                                   2019, 2020, 2021, 2022, 2023
                               ],
                               stations=[
                                   "ARA 1", "ARA 1-3", "ARA 1-3", "ARA 1-3",
                                   "ARA 1-3", "ARA 1-3", "ARA 1-5"
                               ],
                               xlim=None,
                               save_name=None,
                               color_damping=0):
    plt.figure(figsize=(7, 6))
    #     plt.axhline(5, ls=':', c='k')
    #     plt.grid(ls='--', c='dimgray', axis='y')
    detections = {}
    for flux in fluxes:
        detections[flux] = [0]
        for i, year in enumerate(years[1:]):
            nus = count_neutrinos(
                fluxes[flux], energies, veff_additions[i], stations=1,
                years=1) * 1e4
            detections[flux].append(np.sum(nus))

    uniq = []
    for name in stations:
        if name not in uniq:
            uniq.append(name)
    i = 0
    c = 0
    while i < min(len(stations), len(years)):
        xmin = years[i]
        st = stations[i]
        j = i + 1
        while j < len(stations) and stations[j] == st:
            j += 1
        if j == len(stations):
            xmax = years[-1]
        else:
            xmax = years[j]
        cval = 1 - c / (len(uniq) + color_damping)
        plt.axvspan(xmin, xmax, color=(cval, cval, cval, 0.15))
        plt.annotate(str(stations[i]),
                     xy=(i / (len(years) - 1) + 0.1 / (len(years) - 1), 0.4),
                     xycoords='axes fraction',
                     color='k',
                     rotation=90,
                     verticalalignment='bottom',
                     fontsize=12)
        i = j
        c += 1

    for flux, counts in detections.items():
        if styles is None:
            style = '.-'
            color = None
            ls = None
        else:
            style = styles[flux]
            color = style.rstrip('.-:&')
            style = style[len(color):]
            ls = None
            if style.endswith('&'):
                style = style[:-1]
                ls = (0, (5, 10))
        line, = plt.plot(years,
                         np.cumsum(counts),
                         style,
                         color=color,
                         label=flux)
        if ls is not None:
            line.set_linestyle(ls)
    plt.xlabel("Year", fontsize=16)
    plt.ylabel("Expected Number of Neutrinos", fontsize=16)
    if xlim is None:
        plt.xlim(years[0], years[-1])
    else:
        plt.xlim(*xlim)


#     plt.ylim(0, plt.ylim()[1])
    plt.ylim(0, 10)
    plt.axvline(2019, c='k', ls='--')
    #     ticks = plt.xticks()[0]
    #   plt.xticks(ticks[ticks%0==0], [str(int(tick)) for tick in ticks[ticks%1==0]])
    plt.xticks([2013, 2015, 2017, 2019, 2021, 2023])
    plt.legend(loc=2, fontsize=12)
    ax1 = plt.gca()
    ax2 = ax1.twinx()
    ax2.set_ylim(ax1.get_ylim())
    #     ax2.set_yticklabels([])
    for item in (ax1.get_xticklabels() + ax1.get_yticklabels() +
                 ax2.get_yticklabels()):
        item.set_fontsize(14)
    plt.tight_layout()
    if save_name is not None:
        plt.savefig(save_name, dpi=300)
    plt.show()
Example #42
0
                y = [i] * len(x)
                if entry[0] == m_start and entry[1] == m_end:
                    plt.plot(x, y, color=darkdarkgreen, linewidth=lw)
                    mat_count += 1
                elif entry[0] == s_start and entry[1] == s_end:
                    plt.plot(x, y, color='darkred', linewidth=lw)
                else:
                    plt.plot(x, y, color='black', linewidth=lw)

#%%
    alpha = 0.25
    ymin = 0
    if m_start > s_start:
        plt.axvspan(0.5,
                    s_start - 0.5,
                    color='black',
                    alpha=alpha,
                    zorder=1,
                    ymin=ymin)
        plt.axvspan(s_start - 0.5,
                    s_end + 0.5,
                    color='firebrick',
                    alpha=alpha,
                    zorder=1,
                    ymin=ymin)
        plt.axvspan(s_end + 0.5,
                    m_start - 0.5,
                    color='gold',
                    alpha=alpha,
                    zorder=1,
                    ymin=ymin)
        plt.axvspan(m_start - 0.5,
Example #43
0
# In[11]:

Df['P/Z']

# In[21]:

Initial_gas_in_place = (0 - 2070.38439919) / -148.2874553
Initial_gas_in_place
G = Initial_gas_in_place
G

# In[31]:

plt.style.use('seaborn-whitegrid')
plt.figure(figsize=(10, 7))
plt.plot(Df['Cumulative Gas Produced Gp(MMMSCF)'][1:700], Df['P/Z'][1:700])
plt.plot(Df['Cumulative Gas Produced Gp(MMMSCF)'][700:2071],
         Df['P/Z'][700:2071],
         ls='-.')
plt.scatter(x, y, marker='o', color='green', label="Gas Produced Gp")
plt.scatter(G, 5, label="Initial Gas in Place G")
plt.xlim(0, 18)
plt.ylim(1, 2200)
plt.axvspan(0, 4, alpha=0.5, label='Production History', color='aquamarine')
plt.xlabel("Cumulative Gas Production 'Gp' (MMMSCF)")
plt.ylabel("P/Z")
plt.title("Gas Material Balance")
plt.legend(loc='upper right')
plt.show()
print("The Gas Initially in place is", G, "MMMSCF")
Example #44
0
axPlot = plt.subplot(111)
axPlot.set_xlim(-.001, 3)

# add titles, labels, and legend
plt.title('Velocity Error', fontdict=font)
plt.xlabel('Time', fontdict=font)
plt.ylabel('Velocity Error', fontdict=font)
#plt.legend(loc=4, shadow=True)

# annotate rise time (interval)
plt.axhspan(set_point - error_band_delta,
            set_point + error_band_delta,
            facecolor='0.5',
            alpha=0.25)
plt.axhline(y=set_point - error_band_delta, color='k', linestyle='--')
plt.axvspan(0.0, rise_time, facecolor='0.5', alpha=0.25)
#plt.axvline(x=rise_time, color='k',linestyle='--')
#plt.annotate('rise time', xy=(rise_time-.5, set_point-eps))

# annotate peak_time
#plt.axvline(x=peak_time, color='k',linestyle='--')
#plt.annotate('peak time', xy=(peak_time+.1, set_point-2*eps))

# annotate overshoot
#plt.axhline(y=overshoot, xmin=0, xmax=peak_time,color='k',linestyle='--')
#plt.annotate('overshoot', xy=(peak_time, overshoot+eps))

# annotate settling time
#plt.axvline(x=settling_time,color='k',linestyle='--')
#plt.annotate('settling time', xy=(settling_time, set_point+eps))
Example #45
0
    EIGENVALUES = np.linalg.eigvalsh(K)
    if EIGENVALUES.min() <= 0:
        logging.warning('nonpositive eigenvalue of kernel spotted!')

    LOG_START = np.floor(np.log10(abs(EIGENVALUES).min())) - 2
    LOG_END = np.ceil(np.log10(abs(EIGENVALUES).max())) + 4
    REGULARIZATION_PARAMETERS = [0] + list(np.logspace(LOG_START, LOG_END,
                                                       100))
    CV_ERROR = cv(reconstructor, DF.V, REGULARIZATION_PARAMETERS)
    idx = np.argmin(CV_ERROR)
    regularization_parameter = REGULARIZATION_PARAMETERS[idx]
    plt.figure()
    plt.title('CV')
    plt.xscale('symlog', linthreshx=10**LOG_START)
    plt.axvspan(abs(EIGENVALUES).min(),
                abs(EIGENVALUES).max(),
                color=cbf.YELLOW)
    for x in EIGENVALUES:
        if x > 0:
            plt.axvline(x, ls='--', color=cbf.SKY_BLUE)
        else:
            plt.axvline(-x, ls='-', color=cbf.VERMILION)

    plt.plot(REGULARIZATION_PARAMETERS, CV_ERROR, color=cbf.BLUE)
    plt.axvline(regularization_parameter, ls=':', color=cbf.BLACK)

    for rp in [0, regularization_parameter]:
        approximator = reconstructor(DF.V, regularization_parameter=rp)
        DIPOLES['W'] = approximator.dipole_moments()
        DIPOLES['WPX'] = DIPOLES.PX * DIPOLES.W
        DIPOLES['WPY'] = DIPOLES.PY * DIPOLES.W
t1 = np.arange(0.0, 9.0, 0.1)

plt.figure(figsize=(16, 4))

sp = plt.subplot(1, 3, 1)

plt.xlabel(u"KLOC, тыс. строк/компонент", fontsize=18)
plt.ylabel(u"Трудоемкость, чел-мес", fontsize=18)
plt.tick_params(axis='x', labelsize=14)
plt.tick_params(axis='y', labelsize=14)
plt.plot(t1, f1(t1), 'k')
plt.ylim((0, y_lim))
plt.title(u"Минимальное", fontsize=18)
plt.grid()
plt.axvspan(x_begin, x_end, color='grey', alpha=0.3)
plt.axhspan(f1(x_begin), f1(x_end), color='grey', alpha=0.3)

plt.axhspan(391, 391, color='black', alpha=1)

sp1 = plt.subplot(1, 3, 2)
plt.xlabel(u"KLOC, тыс. строк/компонент", fontsize=18)
plt.ylabel(u"Трудоемкость, чел-мес", fontsize=18)
plt.tick_params(axis='x', labelsize=14)
plt.tick_params(axis='y', labelsize=14)
plt.plot(t1, f2(t1), 'k')
plt.ylim((0, y_lim))
plt.title(u"Усредненное", fontsize=18)

plt.axvspan(x_begin, x_end, color='grey', alpha=0.3)
plt.axhspan(f2(x_begin), f2(x_end), color='grey', alpha=0.3)
    def plot_effects(self, sub=None, bottom=0, top=None):
        # plots variance ratio (variance first group/variance second group) and Cohen's d ((mean first group - mean second group)/pooled standard deviation) across epochs
        if not sub:
            sub = range(self.masks)
        assert len(
            sub
        ) == 2, "Number of groups must be 2, got %.0f. Specify sub parameter." % len(
            sub)

        if not top:
            top = self.epochs
        assert [type(x) for x in [bottom, top]
                ] == [int,
                      int], 'bottom and top must be of type int, got %s' % str(
                          [type(x) for x in [bottom, top]])

        vr = self.get_vr(sub=sub, bottom=bottom, top=top)
        d = self.get_d(sub=sub, bottom=bottom, top=top)

        fig = plt.figure()
        plt.suptitle(self.name, fontsize=30)
        ax1 = fig.add_subplot(111)
        line1 = ax1.plot(range(bottom, top),
                         vr,
                         color='black',
                         linestyle='solid',
                         label='Variance ratio')
        ax1.set_xlabel('Epoch', fontsize=28)
        ax1.set_ylabel('Variance ratio', fontsize=28)

        ax2 = ax1.twinx()
        line2 = ax2.plot(range(bottom, top),
                         d,
                         color='black',
                         linestyle='dotted',
                         label='Cohen\'s d')
        ax2.set_ylabel('Cohen\'s d', fontsize=28)

        fig = plt.gca()
        fig.tick_params(axis='x',
                        which='major',
                        width=1,
                        length=7,
                        labelsize=24)
        lns = line1 + line2
        labs = [l.get_label() for l in lns]
        plt.legend(lns, labs, loc=4, prop={'size': 28})
        leg = fig.get_legend()
        llines = leg.get_lines()
        plt.setp(llines, linewidth=2.0)

        for bin in self.get_conditional_bins(sub=sub, bottom=bottom, top=top):
            plt.axvspan(bin[0],
                        bin[1],
                        alpha=0.3,
                        facecolor='gray',
                        edgecolor='none')

        if len(sub) == 2:
            for bin in self.get_conditional_bins(sub=sub[::-1],
                                                 bottom=bottom,
                                                 top=top):
                plt.axvspan(bin[0],
                            bin[1],
                            alpha=0.3,
                            facecolor='red',
                            edgecolor='none')
Example #48
0
time = np.linspace(0,0.5, 50)

pts = np.zeros(50, dtype=np.float64)
pts[10:40] = 0.5
pts[40:] = 1

spline_interpolation_(pts)

plt.plot(time, pts*100)
plt.xticks([0.1, 0.4], [r'$t_{HS}$', r'$t_{TO}$'])
plt.axvline(0.1, ls=':', color='black')
plt.axvline(0.4, ls=':', color='black')
plt.title('Percentage of force applied on one foot')
plt.xlabel('Time [s]')
plt.ylabel('Distribution of force on one foot [%]')
plt.axvspan(0.1, 0.4, facecolor='gray', alpha=0.2)
plt.text(0.05, 15, 'Swinging\nfoot', horizontalalignment='center')
plt.text(0.25, 15, 'Double stance\nphase', horizontalalignment='center')
plt.text(0.45, 15, 'Stance\nfoot', horizontalalignment='center')

x = np.linspace(0,1.2,100)
y = 0.5 * (np.tanh(-np.pi*(2*(x-0.15)-0.85)/0.85)+1)

plt.figure()
plt.plot(x, y)
plt.xticks([0.15, 1], [r'$u_{th}$', '1'])
plt.axvline(0.15, ls=':', color='black')
plt.axvline(1, ls=':', color='black')
plt.title('Smoothing function')
plt.xlabel('Input ratio')
plt.ylabel('Smoothed ratio')
    def plot_line_chart(self,
                        sub=None,
                        bottom=0,
                        top=None,
                        yaxis=[None, None],
                        is_subplot=False):
        """ plot mean error over epochs for each mask, show variance as error bars

        Args:
            sub: list of integers, specifies a subsample of the masks

        Returns:
            None
        """
        if not sub:
            sub = range(self.masks)

        if not top:
            top = self.epochs
        assert [type(x) for x in [bottom, top]
                ] == [int,
                      int], 'bottom and top must be of type int, got %s' % str(
                          [type(x) for x in [bottom, top]])

        if not is_subplot:
            plt.figure()
            plt.xlabel('Epoch', fontsize=28, labelpad=15)
            plt.ylabel('Mean squared error', fontsize=28, labelpad=15)
            plt.suptitle(self.name, fontsize=30)
        for i in sub:
            mean = self.mean[i, :]
            var = self.std[i, :]
            plt.plot(range(bottom, top),
                     mean[bottom:top],
                     color=self.colors[i],
                     label=self.labels[i],
                     linewidth=5.0)
            plt.fill_between(range(bottom, top),
                             mean[bottom:top] - var[bottom:top],
                             mean[bottom:top] + var[bottom:top],
                             alpha=0.2,
                             facecolor=self.colors[i])
        plt.axis([bottom, top, yaxis[0], yaxis[1]])
        fig = plt.gca()
        fig.tick_params(axis='both',
                        which='major',
                        width=1,
                        length=7,
                        labelsize=24)

        plt.legend(prop={'size': 28})
        leg = fig.get_legend()
        llines = leg.get_lines()
        plt.setp(llines, linewidth=5.0)

        for bin in self.get_conditional_bins(sub=sub, bottom=bottom, top=top):
            plt.axvspan(bin[0],
                        bin[1],
                        alpha=0.2,
                        facecolor='grey',
                        edgecolor='none')
    def plot_d(self,
               sub=None,
               bottom=0,
               top=None,
               add_lines=None,
               add_lables=[None],
               linestyles=['solid'],
               long_title=False):

        if not sub:
            sub = range(self.masks)
        assert len(
            sub
        ) == 2, "Number of groups must be 2, got %.0f. Specify sub parameter." % len(
            sub)

        if not top:
            top = self.epochs
        assert [type(x) for x in [bottom, top]
                ] == [int,
                      int], 'bottom and top must be of type int, got %s' % str(
                          [type(x) for x in [bottom, top]])

        d = self.get_d(sub=sub, bottom=bottom, top=top)
        plt.figure()
        plt.xlabel('Epoch', fontsize=28, labelpad=15)
        plt.ylabel('Cohen\'s d', fontsize=28, labelpad=15)
        if long_title:
            plt.suptitle("%s\n%s vs. %s" %
                         (self.name, self.labels[sub[0]], self.labels[sub[1]]),
                         fontsize=30)
        else:
            plt.suptitle(self.name, fontsize=30)
        plt.subplots_adjust(top=0.9)
        plt.plot(range(bottom, top),
                 d,
                 c='black',
                 linewidth=2.0,
                 label=add_lables[0],
                 linestyle=linestyles[0])

        fig = plt.gca()

        if add_lines:
            for idx, line in enumerate(add_lines):
                plt.plot(range(bottom, top),
                         line,
                         c='black',
                         linewidth=2.0,
                         label=add_lables[idx + 1],
                         linestyle=linestyles[idx + 1])
            plt.legend(prop={'size': 28})
            leg = fig.get_legend()
            llines = leg.get_lines()
            plt.setp(llines, linewidth=2.0)

        fig.tick_params(axis='both',
                        which='major',
                        width=1,
                        length=7,
                        labelsize=24)

        for bin in self.get_conditional_bins(sub=sub):
            plt.axvspan(bin[0],
                        bin[1],
                        alpha=0.3,
                        facecolor='gray',
                        edgecolor='none')

        if len(sub) == 2:
            for bin in self.get_conditional_bins(sub=sub[::-1],
                                                 bottom=bottom,
                                                 top=top):
                plt.axvspan(bin[0],
                            bin[1],
                            alpha=0.3,
                            facecolor='red',
                            edgecolor='none')
Example #51
0
test_y = test_y.reshape((len(test_y), 1))
inv_y = concatenate((test_y, test_X[:, -(len(series.columns) - 2):]), axis=1)
inv_y = scaler.inverse_transform(inv_y)
inv_y = inv_y[:, 0]

pyplot.clf()

pyplot.plot(inv_y, label='actual')
pyplot.plot(inv_yhat, label='predicted')
pyplot.xlabel('Test Samples', color='#1C2833')
pyplot.ylabel('Values', color='#1C2833')
pyplot.title('Actual vs Predicted Values (' + str(originalTestSamples) +
             ' Test Samples):' + currentFeatureName)
pyplot.legend()

pyplot.axvspan(originalTestSamples, len(test_y), alpha=0.5, color='yellow')
pyplot.savefig(outfolder + "Out\\ActualVsPredictedValues.png",
               bbox_inches='tight',
               dpi=100)
pyplot.show()

inv_yhat_df = pd.DataFrame(inv_yhat)
inv_y_df = pd.DataFrame(inv_y)

# print out all actual vs predicted values (inclusive of horizon steps into future)
inv_yhat_df.to_excel(outfolder + r'Out\\Predicted.xlsx', index=False)
inv_y_df.to_excel(outfolder + r'Out\\Actual.xlsx', index=False)

rmse = sqrt(mean_squared_error(inv_y, inv_yhat))

writeAccuracyFile = open(outfolder + "Out\\TESTRMSE.txt", "w")
import numpy as np
import matplotlib.pyplot as ploty
from scipy.signal import medfilt
import matplotlib.lines as mlines
import matplotlib.patches as mpatches

#plot entire signal x and night time
ploty.figure(10)
ploty.plot(range(0, len(x_axis)), x_axis, 'b-')
ploty.ylabel('g')
ploty.xlabel('samples')
#ploty.axis([0,len(x_axis),-6,6])
ploty.title('neki' + ' x axis')
ploty.xticks(range(0, len(x_axis), 10), [str(x) + " h" for x in range(0, 10)])
ploty.axvspan(first_night_begin_index,
              first_night_end_index,
              facecolor='b',
              alpha=0.5)
ploty.axvspan(second_night_begin_index,
              second_night_end_index,
              facecolor='b',
              alpha=0.5)
blue_shade = mpatches.Patch(color='blue',
                            alpha=0.5,
                            label='night (21.00:7.00)')
ploty.legend(handles=[blue_shade])
red_mark = mlines.Line2D([], [],
                         color='red',
                         linewidth=1,
                         linestyle='-',
                         label='no wear')
ploty.legend(handles=[red_mark])
Example #53
0
def test_axvspan():
    ax = plt.subplot(projection="polar")
    span = plt.axvspan(0, np.pi / 4)
    assert span.get_path()._interpolation_steps > 1
Example #54
0
def main():
    first_second, last_second = start_end_second()
    intervals = [
    ]  #used to separate packet loss between congested and uncongested periods
    raw_ts_far = []
    raw_ts_near = []

    path = ''
    x_label = 'UTC Time'
    y_label = 'RTT 15-min \nminimums (ms)'
    yaxislow = 15
    yaxishigh = 45
    in_files = str(sys.argv[1])
    number_files = in_files.count(' ') + 1
    packet_high = 40
    packet_low = -0.1
    msize = 12
    alfa = 1
    #first_second = 0
    #    last_second = 1494201600 + int(5.75* 86400) #end time x axis
    if (number_files == 0):
        sys.stderr.write('No files were provided\n')
    elif (number_files > 5):
        sys.stderr.write(
            'more than 5 files provided. Will plot first 2. files provided:')
        sys.stderr.write(str(sys.argv[1]))
        number_files = 5

    #Read all filenames provided
    for j in range(number_files):
        #print "j = " + str(j)
        plotx = []
        ploty = []
        #Get information about file from inputs
        filename = in_files.split(' ')[j]

        #series labels
        if j == 0:
            s_label = 'Far RTT'
            month = filename.split('/')[-1].split('.')[2]
            monitor = filename.split('/')[-1].split('.')[0]
            #create output directory
            output_dir = "/project/comcast-ping/kabir-plots/loss_data/supplemental_data/ddc/ndt"
            #dir_creation = "mkdir -p " + output_dir
            #raw_plot_data = output_dir + '/' + in_files.split(' ')[0].split('/')[-1] + '.txt'
            #os.system(dir_creation)
            #raw = open (raw_plot_data, 'w+')
            #sys.stderr.write('opened ' +  + ' for writing \n')
        elif j == 1:
            s_label = 'congestion periods'
        elif j == 2:
            s_label = 'Near RTT'
        elif j == 3:
            s_label = 'Download Throughput'
        else:
            s_label = 'Upload Throughput'

        #file_path = path + monitor + '/' + month + '/'
        file_path = ''
        filename = file_path + in_files.split(' ')[j]

        if j == 1 or j == 3 or j == 4:
            f = open(filename, 'rb')  #import file
            g = open(filename, 'rb')  #import fi

        if j == 0 or j == 2:  #The time series files
            command = "gunzip -f " + filename
            os.system(command)
            f = open(filename, 'rb')  #import file1
            g = open(filename, 'rb')  #import fi
            data = g.readlines()
            ploty, plotx = filter_minimums(data, first_second, last_second)
            #print plotx[0]
            #print plotx[-1]
            #print "y values"
            #print ploty[0]
            #print ploty[-1]
            command = "gzip -f " + filename
            os.system(command)
        elif j > 2:
            plotx = []
            ploty = []

        sys.stderr.write('reading time series file %s\n' % filename)

        if j == 0:  #space-separated file
            reader = csv.reader(f,
                                delimiter=' ')  #read file into variable reader
            fig = plt.figure(1, figsize=(12, 8))
        else:
            reader = csv.reader(f, delimiter=' ')
        #Read values from file
        for row in reader:
            if float(row[0]) < first_second:
                continue
            if float(row[0]) > last_second:
                break
            secs = mdate.epoch2num(float(row[0]))

            if j == 1:
                secs2 = mdate.epoch2num(float(row[1]))
                #plt.axvline(x=secs, color='g')
                #plt.axvline(x=secs2, color='r')
                plt.axvspan(secs, secs2, facecolor='gray', alpha=0.5)
                intervals.append([int(row[0]), int(row[1])
                                  ])  #append intervals to compute loss

            elif j == 2:  #near ts
                secs3 = mdate.epoch2num(float(row[0]))
                y = float(row[1])
                #plotx.append(secs3)
                #ploty.append(y)
            elif j == 3:  #far loss

                secs3 = mdate.epoch2num(float(row[0]))
                y = float(row[1])
                raw_ts_far.append(int(row[0]))
                plotx.append(secs3)
                ploty.append(y)
            elif j == 4:  #near loss
                secs3 = mdate.epoch2num(float(row[0]))
                y = float(row[2])  #upload throughput
                raw_ts_near.append(int(row[0]))
                plotx.append(secs3)
                ploty.append(y)
        if j == 0:
            fig = plt.figure(1, figsize=(9, 6))
            ax = fig.add_subplot(211)
            title = in_files.split(' ')[0].split("/")[-1]
            #ax.set_title(title)
            s_color = 'b-'
            #bigger marker for levelshift
            ax.plot_date(plotx, ploty, s_color, alpha = alfa, \
            marker = ".", markersize = msize,  label = s_label, lw = 0)
            ax.set_xlabel(x_label)
            ax.set_ylabel(y_label)
            ax.set_ylim([yaxislow, yaxishigh])
            ax.autoscale_view()
            #artificial limit below
            ax.set_xlim([
                datetime.date(year, first_month, first_day),
                datetime.datetime(year, second_month, first_day + 4, 6)
            ])
            date_fmt = '%m-%d-%y'

            # Use a DateFormatter to set the data to the correct format
            date_formatter = mdate.DateFormatter(date_fmt)
            ax.xaxis.set_major_formatter(date_formatter)

        if j == 2:
            s_color = 'r-'
            ax.plot_date(plotx, ploty, s_color, alpha = alfa, \
                marker = ".", markersize = msize,  label = s_label, lw=0)
            ax.set_xlim([
                datetime.date(year, first_month, first_day),
                datetime.datetime(year, second_month, first_day + 4, 6)
            ])
            ax.legend(fontsize=14, framealpha=0.8)
        if j == 3:

            ax = fig.add_subplot(212)
            s_color = 'k-'
            ax.plot_date(plotx, ploty, s_color, alpha = alfa, \
                marker = ".", markersize = msize,  label = s_label, lw=0)
            ax.set_xlabel(x_label)
            ax.set_ylabel("Throughput \n (Mbps)")
            yaxishigh = packet_high
            yaxislow = packet_low
            ax.set_ylim([yaxislow, yaxishigh])
            ax.autoscale_view()
            # Choose your xtick format string
            date_fmt = '%H:%M'
            #date_fmt = '%m-%d-%y'
            # Use a DateFormatter to set the data to the correct format
            date_formatter = mdate.DateFormatter(date_fmt)
            ax.xaxis.set_major_formatter(date_formatter)
            ax.set_xlim([
                datetime.date(year, first_month, first_day),
                datetime.datetime(year, second_month, first_day + 4, 6)
            ])

            for k in range(len(intervals)):
                secs = mdate.epoch2num(float(intervals[k][0]))
                secs2 = mdate.epoch2num(float(intervals[k][1]))
                #plt.axvline(x=secs, color='g')
                #plt.axvline(x=secs2, color='r')
                plt.axvspan(secs, secs2, facecolor='gray', alpha=0.5)
            '''
                        string = 'far'
                        plt.axvline(x=mdate.epoch2num(intervals[k][0]), color='g')
                        plt.axvline(x=mdate.epoch2num(intervals[k][1]), color='r')
                        this_period_lost = 0
                        this_period_sent = 0
                        this_period_loss = 0
                        next_period_lost = 0
                        next_period_sent = 0
                        next_period_loss = 0
                        next_interval = []
                        
                        for l in range(len(raw_ts_far)):
                                        
                            if is_in_window(raw_ts_far[l], intervals[k][0], intervals[k][1]):
                                this_period_lost = this_period_lost + ploty[l][0]
                                this_period_sent = this_period_sent + ploty[l][1]
                            if k < (len(intervals)-1): #all but last congestion interval
                                if is_in_window(raw_ts_far[l], intervals[k][1], intervals[k+1][0]):
                                        next_period_lost = next_period_lost + ploty[l][0]
                                        next_period_sent = next_period_sent + ploty[l][1]

                        string = string + ',' + convert_to_ht(intervals[k][0])#save interv begin/end
                        string = string + ',' + convert_to_ht(intervals[k][1])
                        string = string + ',' + str(this_period_lost)
                        string = string + ',' + str(this_period_sent)
                        if this_period_sent > 0 and this_period_lost > 0:
                            this_period_loss = 100*float(this_period_lost) / float(this_period_sent)
                            this_central_interval = central_limit(this_period_lost, this_period_sent)
                            this_interval = proportion_confint(this_period_lost, this_period_sent)
                        else: 
                            this_period_loss = 0.0
                            this_central_interval = [0.0, 0.0]
                            this_interval = [0.0, 0.0]
                        string = string + ',' + str(round(this_period_loss,3))
                        
                        upper = 100 * this_interval[1] + this_period_loss
                        lower = -100. * this_interval[0] + this_period_loss
                        #print this_interval
                        string = string + ',' + str(round(upper,3))
                        string = string + ',' + str(round(lower,3))
                         #save central limit intervals for forensics
                        string = string + ',' + \
                                str(round( 100*(this_central_interval[1]) + this_period_loss ,3))
                        string = string + ',' + \
                                str(round(( -100.*this_central_interval[0] + this_period_loss ),3))
                        this_x = mdate.epoch2num(float(intervals[k][0]+ 0.5*(intervals[k][1]-intervals[k][0])))
                        raw.write(string + '\n')
                        if k < (len(intervals)-1):
                                #Save next interval info
                                string = 'far'
                                string = string + ',' + convert_to_ht(intervals[k][1])
                                string = string + ',' + convert_to_ht(intervals[k+1][0])
                                string = string + ',' + str(next_period_lost)
                                string = string + ',' + str(next_period_sent)
                                if next_period_sent > 0 and next_period_lost > 0:
                                    next_period_loss = 100*float(next_period_lost) \
                                        / float(next_period_sent)
                                    next_central = central_limit(next_period_lost, next_period_sent)
                                    next_interval = proportion_confint(next_period_lost, next_period_sent)
                                else:
                                    next_period_loss = 0.0
                                    next_central = [0.0, 0.0]
                                    next_interval = [0.0, 0.0]
                                string = string + ',' + str(round(next_period_loss,3))
                                upper_n = 100 * next_interval[1] + next_period_loss
                                lower_n = -100. * next_interval[0] + next_period_loss
                                string = string + ',' + str(round(upper_n,3))
                                string = string + ',' + str(round(lower_n,3))
                                upper_n_central = 100 * next_central[1] + next_period_loss
                                lower_n_central = -100. * next_central[0] + next_period_loss
                                string = string + ',' + str(round(upper_n_central,3))
                                string = string + ',' + str(round(lower_n_central,3))
                                raw.write(string + '\n')
                        #print this_interval
                                next_x = mdate.epoch2num(float(intervals[k][1]+ 0.5*(intervals[k+1][0] - intervals[k][1])))
                        if k == 0:
                                ax.plot_date(this_x, this_period_loss, \
                                        color = s_color, alpha = 1, \
                                        marker = "^", markersize = msize,  label = s_label)
                                label_s = 'far conf. interval'
                                ax.plot_date([this_x,this_x], [lower, upper], \
                                color = s_color, alpha = 1, \
                                marker = "+", markersize = msize, label = label_s)
                        else:
                                ax.plot_date(this_x, this_period_loss, \
                                        color = s_color, alpha = 1, \
                                        marker = "^", markersize = msize)                     
                                ax.plot_date([this_x,this_x], [lower, upper], \
                                color = s_color, alpha = 1, \
                                marker = "+", markersize = msize)
                        if k < len(intervals)-1:
                                ax.plot_date([next_x,next_x], [lower_n, upper_n], \
                                        color = s_color, alpha = 1, \
                                        marker = "+", markersize = msize)
                                ax.plot_date(next_x, next_period_loss, \
                                        color = s_color, alpha = 1, \
                                        marker = "^", markersize = msize)

                    #plot triangle and bars at 1/3 of distance between beginning and end of interval
                    ax.set_xlim([datetime.date(year, month_p, first_day), datetime.date(year, month_p, last_day)])
                    '''
        if j == 4:
            s_color = 'g'
            #ax.plot_date(plotx, ploty, color = s_color, alpha = alfa, \
            #                                marker = ".", markersize = msize,  label = s_label)
            '''
                    for k in range(len(intervals)):
                        this_period_lost = 0
                        this_period_sent = 0
                        this_period_loss = 0
                        next_period_lost = 0
                        next_period_sent = 0
                        next_period_loss = 0
                        next_interval = []
                        for l in range(len(raw_ts_near)):
                                        
                            if is_in_window(raw_ts_near[l], intervals[k][0], intervals[k][1]):
                                this_period_lost = this_period_lost + ploty[l][0]
                                this_period_sent = this_period_sent + ploty[l][1]
                            if k < (len(intervals)-1):
                                if is_in_window(raw_ts_near[l], intervals[k][1], intervals[k+1][0]):
                                        next_period_lost = next_period_lost + ploty[l][0]
                                        next_period_sent = next_period_sent + ploty[l][1]

                        string = 'near'
                        if this_period_sent > 0 and this_period_lost > 0:
                            this_period_loss = 100*float(this_period_lost) / float(this_period_sent)
                            this_central_interval = central_limit(this_period_lost, this_period_sent)
                            this_interval = proportion_confint(this_period_lost, this_period_sent)
                        else:
                            this_period_loss = 0.0
                            this_central_interval = [0.0, 0.0]
                            this_interval = [0.0, 0.0]
                        upper = 100 * (this_interval[1]) + this_period_loss
                        lower =  (-100. * this_interval[0]) + this_period_loss
                        string = string + ',' + convert_to_ht(intervals[k][0])#save interv begin/end
                        string = string + ',' + convert_to_ht(intervals[k][1])
                        string = string + ',' + str(this_period_lost)
                        string = string + ',' + str(this_period_sent)
                        string = string + ',' + str(round(this_period_loss,3))
                        string = string + ',' + str(round(upper,3))
                        string = string + ',' + str(round(lower,3))
                        string = string + ',' + \
                            str(round( 100*(this_central_interval[1] + this_period_loss) ,3))
                        string = string + ',' + \
                            str(round((-100.*this_central_interval[0] + this_period_loss),3))
                        raw.write(string + '\n')
                        #print this_interval
                        this_x = mdate.epoch2num(float(intervals[k][0]+ 0.5*(intervals[k][1]-intervals[k][0])))
                        if k < (len(intervals)-1):   
                                string = 'near'
                                if next_period_sent > 0 and next_period_lost > 0:
                                    next_period_loss = 100*float(next_period_lost) / float(next_period_sent)
                                    next_interval = proportion_confint(next_period_lost, next_period_sent)
                                    next_central_interval = central_limit(next_period_lost, next_period_sent)
                                else:
                                    next_period_loss = 0.0
                                    next_central_interval = [0.0, 0.0]
                                    next_interval = [0.0, 0.0]
                                upper_n = 100 * next_interval[1] + next_period_loss
                                lower_n = -100. * next_interval[0] + next_period_loss
                                upper_central_n = 100 * next_central_interval[1] + next_period_loss
                                lower_central_n = -100. * next_central_interval[0] + next_period_loss

                                string = string + ',' + convert_to_ht(intervals[k][1])#save interv begin/end
                                string = string + ',' + convert_to_ht(intervals[k+1][0])
                                string = string + ',' + str(next_period_lost)
                                string = string + ',' + str(next_period_sent)
                                string = string + ',' + str(round(next_period_loss,3))
                                string = string + ',' + str(round(upper_n,3))
                                string = string + ',' + str(round(lower_n,3))
                                string = string + ',' + str(round(upper_central_n,3))
                                string = string + ',' + str(round(lower_central_n,3))
                                raw.write(string + '\n')
                                #print this_interval
                                next_x = mdate.epoch2num(float(intervals[k][1]+ 0.5*(intervals[k+1][0] - intervals[k][1])))
                        if k == 0:
                                ax.plot_date(this_x, this_period_loss, \
                                        color = s_color, alpha = 1, \
                                        marker = "^", markersize = msize,  label = s_label)
                                label_s = 'near conf. interval'
                                ax.plot_date([this_x,this_x], [lower, upper], \
                                color = s_color, alpha = 1, \
                                marker = "+", markersize = msize, label = label_s)
                        else:
                                ax.plot_date(this_x, this_period_loss, \
                                        color = s_color, alpha = 1, \
                                        marker = "^", markersize = msize)                     
                                ax.plot_date([this_x,this_x], [lower, upper], \
                                color = s_color, alpha = 1, \
                                marker = "+", markersize = msize)
                        if k < (len(intervals)-1):
                                ax.plot_date([next_x,next_x], [lower_n, upper_n], \
                                        color = s_color, alpha = 1, \
                                        marker = "+", markersize = msize)
                                ax.plot_date(next_x, next_period_loss, \
                                        color = s_color, alpha = 1, \
                                        marker = "^", markersize = msize)
                    '''
            #plot triangle and bars at 1/3 of distance between beginning and end of interval
            #ARTIFICIAL LIMIT BELOW
            ax.set_xlim([
                datetime.date(year, first_month, first_day),
                datetime.datetime(year, second_month, first_day + 4, 6)
            ])
            plt.axhline(y=0, color='gray')
            ax.legend(fontsize=14, framealpha=0.8)
    fig.autofmt_xdate()
    fig.tight_layout()
    output_dir = "/project/comcast-ping/kabir-plots/loss_data/supplemental_data/ddc/ndt"  #+ monitor + '/' + month
    dir_creation = "mkdir -p " + output_dir
    os.system(dir_creation)

    output = output_dir + '/' + filename + '.pdf'
    sys.stderr.write("saving " + output)
    fig.savefig(output, transparent=True)
Example #55
0
# Let's plot the histogram as well as some standard values such as mean
# upper, and lower value and the one-sigma range.

plt.figure()
# Note that we have to use ``.ravel()`` here to avoid matplotlib interpreting each
# row in the array as a different dataset to histogram.
plt.hist(aia_smap.data.ravel(), bins=bins, label='Histogram', histtype='step')
plt.xlabel('Intensity')
plt.axvline(aia_smap.min(), label='Data min={:.2f}'.format(aia_smap.min()), color='black')
plt.axvline(aia_smap.max(), label='Data max={:.2f}'.format(aia_smap.max()), color='black')
plt.axvline(aia_smap.data.mean(),
            label='mean={:.2f}'.format(aia_smap.data.mean()), color='green')
one_sigma = np.array([aia_smap.data.mean() - aia_smap.data.std(),
                      aia_smap.data.mean() + aia_smap.data.std()])
plt.axvspan(one_sigma[0], one_sigma[1], alpha=0.3, color='green',
            label='mean +/- std = [{:.2f}, {:.2f}]'.format(
            one_sigma[0], one_sigma[1]))
plt.axvline(one_sigma[0], color='green')
plt.axvline(one_sigma[1], color='red')
plt.yscale('log')
plt.legend(loc=9)

###############################################################################
# Finally let's overplot the one-sigma contours.

plt.figure()
aia_smap.plot()
levels = one_sigma / aia_smap.max() * u.percent * 100
aia_smap.draw_contours(levels=levels, colors=['blue'])
plt.show()
Example #56
0
plt.grid(True)
ax.set_ylim([0, 1])
plt.plot(history.history["val_loss"], label="val_loss", linewidth=2.0)
plt.plot(history.history["loss"], label="loss", linewidth=2.0)
plt.plot(history.history["val_acc"], label="val_acc", linewidth=2.0)
plt.plot(history.history["acc"], label="acc", linewidth=2.0)
plt.legend(bbox_to_anchor=(1.0, 1), loc=2, borderaxespad=0.)
plt.text(
    0,
    0.5,
    "acc:       %.2f       loss:    %.2f\nval_acc: %.2f    val_loss: %.2f" %
    (history.history["acc"][-1], history.history["loss"][-1],
     history.history["val_acc"][-1], history.history["val_loss"][-1]),
    fontsize=20)
plt.text(0, 0.1, "at_%s: %.2f (epoch-%d)" % (idk, va, index[0]), fontsize=20)
plt.axvspan(index[0], index[0], color='red', alpha=1)
plt.savefig("training.png")

# In[ ]:

md.load_weights("../working/weights-improvement-top.hdf5")

# In[ ]:

rst = (md.predict(data_cleaner[1]))
rta = []
for t in rst:
    rta.append(int(round(t[0])))
test_dat = pd.read_csv("../input/test.csv")
a = pd.Series(test_dat["PassengerId"], name='PassengerId')
b = pd.Series(rta, name='Survived')
Example #57
0
     facecolor(fc):mpl颜色规则
     linestyle(ls):[‘solid’ | ‘dashed’, ‘dashdot’, ‘dotted’ | (offset, on-off-dash-seq) | '-' | '--' | '-.' | ':' | 'None' | ' ' | '']
     linewidth(lw):float or None for default
'''
#例
import numpy as np
import matplotlib.pyplot as plt

t = np.arange(-1, 2, .01)
s = np.sin(2 * np.pi * t)

plt.plot(t, s)
l = plt.axhline(linewidth=8, color='#d62728')  #在y=0处画一条粗红横线,它跨越了xrange

l = plt.axhline(y=1)  #在y=1处画一条默认的水平线,横跨xrange

l = plt.axvline(x=1)  #在x=1处画一条默认的垂直线,横跨yrange
#在x = 0处画一条粗蓝色的垂直线,它跨越了yrange的上象限
l = plt.axvline(x=0, ymin=0.75, linewidth=8, color='#1f77b4')

#在y =0.5上画一个默认的hline。它跨越了axes中间的一半
l = plt.axhline(y=.5, xmin=0.25, xmax=0.75)

p = plt.axhspan(0.25, 0.75, facecolor='0.5', alpha=0.5)

p = plt.axvspan(1.25, 1.55, facecolor='#2ca02c', alpha=0.5)

plt.axis([-1, 2, -1, 2])

plt.show()
Example #58
0
File: mcz.py Project: zpace/pyMCZ
def savehist(data,
             snname,
             Zs,
             nsample,
             i,
             path,
             nmeas,
             measnames,
             verbose=False,
             fs=24):
    global BINMODE
    #global NOPLOT

    name = '%s_n%d_%s_%d' % ((snname, nsample, Zs, i + 1))
    outdir = os.path.join(path, 'hist')
    outfile = os.path.join(outdir, name + ".pdf")
    if not NOPLOT:
        fig = plt.figure(figsize=(11, 8))
        plt.clf()

    ####kill outliers, infinities, and bad distributions###
    data = data[np.isfinite(data)]

    n = data.shape[0]
    kde = None

    if not n > 0:
        if verbose:
            print "data must be an actual distribution (n>0 elements!, %s)" % Zs
        return "-1,-1,_1", [], kde

    if data.shape[0] <= 0 or np.sum(data) <= 0:
        print '{0:15} {1:20} {2:>13d}   {3:>7d}   {4:>7d} '.format(
            snname, Zs, -1, -1, -1)
        return "-1, -1, -1", [], kde
    try:
        ###find C.I.###
        median, pc16, pc84 = np.percentile(data, [50, 16, 84])
        std = np.std(data)
        left = pc16
        right = pc84
        maxleft = median - std * 5
        maxright = median + std * 5
        if "%2f" % maxright == "%2f" % maxleft:
            maxleft = median - 1
            maxright = median + 1
        if round(right, 6) == round(left, 6) and round(left, 6) == round(
                median, 6):
            print '{0:15} {1:20} {2:>13.3f}   -{3:>7.3f}   +{4:>7.3f} (no distribution)'.format(
                snname, Zs, median, 0, 0)

            return "%f\t %f\t %f" % (round(median, 3), round(
                median - left, 3), round(right - median,
                                         3)), data, kde  #"-1,-1,-1",[]
        ###print out the confidence interval###
        print '{0:15} {1:20} {2:>13.3f}   -{3:>7.3f}   +{4:>7.3f}'.format(
            snname, Zs, round(median, 3), round(median - left, 3),
            round(right - median, 3))
        alpha = 1.0

        ######histogram######
        if BINMODE == 'kd':
            ##if sklearn is available use it to get Kernel Density
            try:
                from sklearn.neighbors import KernelDensity
            except ImportError:
                print '''sklearn is not available,
                thus we cannot compute kernel density.
                switching to bayesian blocks'''
                BINMODE = 'bb'
        if BINMODE == 'kd':
            ##bw is chosen according to Silverman 1986
            bw = 1.06 * std * n**(-0.2)
            numbin, bm = getbinsize(data.shape[0], data)
            distrib = np.histogram(data, bins=int(numbin), density=True)
            ###make hist###
            counts, bins = distrib[0], distrib[1]
            widths = np.diff(bins)
            countsnorm = counts / np.max(counts)

            if bw > 0:
                kde = KernelDensity(kernel='gaussian',
                                    bandwidth=bw).fit(data[:, np.newaxis])
                kdebins = np.linspace(maxleft, maxright, 1000)[:, np.newaxis]
                log_dens = kde.score_samples(kdebins)
                dens = np.exp(log_dens)
                norm = countsnorm.sum() * (bins[1] - bins[0]) / dens.sum() / (
                    kdebins[1] - kdebins[0])
                if not NOPLOT:
                    plt.fill(kdebins[:, 0],
                             dens * norm,
                             fc='#7570b3',
                             alpha=0.8)
                alpha = 0.5

        ###find appropriate bin size###
        else:
            if BINMODE == 'bb':
                ##if astroML is available use it to get Bayesian blocks
                bm = 0
                try:
                    from astroML.plotting import hist as amlhist
                    if BINMODE == 'bb':
                        distrib = amlhist(data, bins='blocks', normed=True)
                    if not NOPLOT: plt.clf()
                except ImportError:
                    print "bayesian blocks for histogram requires astroML to be installed"
                    print "defaulting to Knuth's rule "
                    ##otherwise
                    numbin, bm = getbinsize(data.shape[0], data)
                    distrib = np.histogram(data,
                                           bins=int(numbin),
                                           density=True)
            else:
                numbin, bm = getbinsize(data.shape[0], data)
                distrib = np.histogram(data, numbin, density=True)

            ###make hist###
            counts, bins = distrib[0], distrib[1]
            widths = np.diff(bins)
            countsnorm = counts / np.max(counts)

        ###plot hist###
        if NOPLOT:
            return "%f\t %f\t %f" % (round(median, 3), round(
                median - left, 3), round(right - median, 3)), data, kde

        plt.bar(bins[:-1], countsnorm, widths, color=['gray'], alpha=alpha)
        plt.minorticks_on()
        plt.gca().xaxis.set_major_formatter(FormatStrFormatter('%.2f'))
        plt.xlim(maxleft, maxright)

        #the following lines assure the x tick label is
        #within the length of the x axis
        xticks = plt.xticks()[0]
        dx = xticks[-1] - xticks[-2]
        xticks = xticks[(xticks < maxright) * (xticks > maxleft)]
        if (maxright - xticks[-1]) < 0.25 * dx:
            maxright = maxright + 0.25 * dx
            maxleft = maxleft - 0.25 * dx
        plt.xlim(maxleft, maxright)
        plt.xticks(xticks, ['%.2f' % s for s in xticks])

        plt.ylim(0, 1.15)
        plt.yticks(np.arange(0.2, 1.3, 0.2),
                   ["%.1f" % x for x in np.arange(0.2, 1.1, 0.2)])
        plt.axvspan(left, right, color='DarkOrange', alpha=0.4)
        plt.axvline(x=median, linewidth=2, color='white', ls='--')

        #labels and legends
        st = '%s ' % (snname)
        plt.annotate(st,
                     xy=(0.13, 0.6),
                     xycoords='axes fraction',
                     size=fs,
                     fontweight='bold')
        st = '%s ' % (Zs.replace('_', ' '))
        plt.annotate(st,
                     xy=(0.61, 0.93),
                     xycoords='axes fraction',
                     fontsize=fs,
                     fontweight='bold')
        st = 'measurement %d of %d\n   %s\nmedian: %.3f\n16th Percentile: %.3f\n84th Percentile: %.3f' % (
            i + 1, nmeas, measnames[i], round(median, 3), round(
                left, 3), round(right, 3))
        plt.annotate(st,
                     xy=(0.61, 0.65),
                     xycoords='axes fraction',
                     fontsize=fs)
        effectiven = len(data[~np.isnan(data)])
        if effectiven:
            st = 'MC sample size %d (%d)\nhistogram rule: %s' % (
                effectiven, nsample, binning[BINMODE])
        if bm:
            if effectiven < nsample:
                st = 'MC sample size %d (%d)\nhistogram rule: %s' % (
                    effectiven, nsample, binning[bm])
            else:
                st = 'MC sample size %d\nhistogram rule: %s' % (nsample,
                                                                binning[bm])
        plt.annotate(st,
                     xy=(0.61, 0.55),
                     xycoords='axes fraction',
                     fontsize=fs - 5)
        if "E(B-V)" in Zs:
            plt.xlabel('E(B-V) [mag]')
            outfile = outfile.replace('(', '').replace(')', '')
        elif "logR23" in Zs:
            plt.xlabel('logR23')
        else:
            plt.xlabel('12+log(O/H)')
        plt.ylabel('relative counts')
        plt.savefig(outfile, format='pdf')
        plt.close(fig)

        return "%f\t %f\t %f" % (round(median, 3), round(
            median - left, 3), round(right - median, 3)), data, kde

    except (OverflowError, AttributeError, ValueError):
        if VERBOSE: print data
        print name, 'had infinities (or something in plotting went wrong)'
        return "-1, -1,-1", [], None
Example #59
0
#plt.hist(hist) #Plot values on histogram
#plt.scatter(x,y) #Plot points on scatter graph

fig = plt.figure(figsize=(25, 10))  #Create 25 x 10 figure
ax = fig.add_subplot(111)  #
ax.bar(x, y)  #Create a bar graph in the subplot
ax.bar(y, x, color='red')  #Append an additional bar graph in the subplot
ax2 = ax.twinx()  #Create a twin axes sharing the xaxis
ax2.plot(hist, color='green')  #Plot aditional data

ax.set_xlabel('Test X-axis')  #Set the x-axis label
ax.set_ylabel('Test Y-axis')  #Set the y-axis label

ax.set_xticks([1, 3, 5, 7, 9, 11])  #Sets the x-axis tick marks
ax.set_yticks([1, 3, 5, 7, 9, 11])  #Sets the y-axis tick marks

ax.set_xticklabels(['a', 'b', 'c', 'd', 'e',
                    'f'])  #For the x-axis ticks, set a label
ax.set_yticklabels(['g', 'h', 'i', 'j', 'k',
                    'l'])  #For the y-axis ticks, set a label

ax.legend(loc="Upper right", fontsize=18)

plt.title("Test graph")  #Add title to the graph

plt.axvspan(2, 10, 0.4, 0.5)  #Add demarkation regions to the graph
plt.axvline(1, 0, 1)  #Adds demarkation line to the graph

plt.gca().invert_xaxis()  #Flip graph on x-axis
plt.savefig("result.png")  #Save the picture to a file
plt.show()  #Display plot
Example #60
0
def show_test_statistics(
        bi_corr_array,
        mean_correlations,  # shutdown if sparse
        eigenvalues,  # shutdown if sparse
        selector,
        test_bi_corr_array,
        test_mean_corr,  # shutdown if sparse
        eigenvalue,  # shutdown fi sparse
        re_samples,
        sparse=False):
    """
    A general function that performs demonstration of an example of random samples of
     the same size as our sample and of our sample and conducts the statistical tests
     on wherther any of nodes or functional groups in our sample are non-random

    :param bi_corr_array: [[current, informativity, confusion_potential], ...] -
    characteristics of the random samples
    :param mean_correlations: [[cluster size, average internode connection], ...] -
    characteristics of clustering random samples with the same parameters
    :param eigenvalues: eigenvalues associated to the interconnection matrix of random samples
    :param selector: range on which we would like to visually zoom and plot a histogram
    :param test_bi_corr_array: [[current, informativity, confusion_potential], ...] -
    characteristics of the true sample. If none, nothing happens
    :param test_mean_corr: [[cluster size, average internode connection], ...] -
    characteristics of clustering the true sample
    :param eigenvalue: eigenvalues associated to the interconnection matrix of the true sample
    :param re_samples: how many random samples we analyzed for the default model
    :param sparse: True if we are showing test statistics of a sparse kernel run
    :return:
    """
    fig = plt.figure()
    fig.set_size_inches(30, 20)

    plt.subplot(331)
    plt.title('current through nodes')
    bins = np.linspace(bi_corr_array[0, :].min(), bi_corr_array[0, :].max(),
                       100)

    if test_bi_corr_array is not None:
        bins = np.linspace(
            min(bi_corr_array[0, :].min(), test_bi_corr_array[0, :].min()),
            max(bi_corr_array[0, :].max(), test_bi_corr_array[0, :].max()),
            100)
    plt.hist(bi_corr_array[0, :],
             bins=bins,
             histtype='step',
             log=True,
             color='b')
    if test_bi_corr_array is not None:
        plt.hist(test_bi_corr_array[0, :],
                 bins=bins,
                 histtype='step',
                 log=True,
                 color='r')

    plt.subplot(332)
    plt.title('test current vs degree')
    plt.scatter(bi_corr_array[1, :], bi_corr_array[0, :])
    if test_bi_corr_array is not None:
        plt.scatter(test_bi_corr_array[1, :],
                    test_bi_corr_array[0, :],
                    color='r',
                    alpha=0.5)
    plt.axvspan(selector[0], selector[1], facecolor='0.5', alpha=0.3)

    plt.subplot(333)
    plt.title('Currently empty')

    plt.subplot(334)
    plt.title('Gaussian KDE current_info')
    estimator_function = kde_compute(bi_corr_array[(1, 0), :], 50, re_samples)
    current_info_rel = None
    if test_bi_corr_array is not None:
        current_info_rel = estimator_function(test_bi_corr_array[(1, 0), :])

    plt.subplot(335)
    plt.title('Node degree distribution')
    bins = np.linspace(bi_corr_array[1, :].min(), bi_corr_array[1, :].max(),
                       100)
    if test_bi_corr_array is not None:
        bins = np.linspace(
            min(bi_corr_array[1, :].min(), test_bi_corr_array[1, :].min()),
            max(bi_corr_array[1, :].max(), test_bi_corr_array[1, :].max()),
            100)
    plt.hist(bi_corr_array[1, :],
             bins=100,
             histtype='step',
             log=True,
             color='b')
    if test_bi_corr_array is not None:
        plt.hist(test_bi_corr_array[1, :],
                 bins=100,
                 histtype='step',
                 log=True,
                 color='r')

    plt.subplot(336)
    plt.title('Density of current in the highlighted area')
    if test_bi_corr_array is not None:
        bins = np.linspace(
            min(
                local_indexed_select(bi_corr_array, 1, selector)[0, :].min(),
                local_indexed_select(test_bi_corr_array, 1,
                                     selector)[0, :].min()),
            max(
                local_indexed_select(bi_corr_array, 1, selector)[0, :].max(),
                local_indexed_select(test_bi_corr_array, 1,
                                     selector)[0, :].max()), 100)
    plt.hist(local_indexed_select(bi_corr_array, 1, selector)[0, :],
             bins=bins,
             histtype='step',
             log=True,
             color='b')
    if test_bi_corr_array is not None:
        plt.hist(local_indexed_select(test_bi_corr_array, 1, selector)[0, :],
                 bins=100,
                 histtype='step',
                 log=True,
                 color='r')

    # this property is better off viewed as a scatterplot of true points and
    # default points

    cluster_props = None

    plt.subplot(337)
    plt.title('Clustering correlation')
    if not sparse:
        # plt.scatter(mean_correlations[0, :], mean_correlations[1, :], color = 'b')
        estimator_function = kde_compute(mean_correlations[(0, 1), :], 50,
                                         re_samples)
        cluster_props = None
        if test_mean_corr is not None:
            plt.scatter(test_mean_corr[0, :],
                        test_mean_corr[1, :],
                        color='k',
                        alpha=0.8)
            cluster_props = estimator_function(test_mean_corr[(0, 1), :])

    plt.subplot(338)
    plt.title('Eigvals_hist')
    if not sparse:
        bins = np.linspace(eigenvalues.min(), eigenvalues.max(), 100)
        if test_bi_corr_array is not None:
            bins = np.linspace(min(eigenvalues.min(), eigenvalue.min()),
                               max(eigenvalues.max(), eigenvalue.max()), 100)
        plt.hist(eigenvalues, bins=bins, histtype='step', color='b')
        if eigenvalue is not None:
            plt.hist(eigenvalue.tolist() * 3,
                     bins=bins,
                     histtype='step',
                     color='r')

    plt.subplot(339)
    plt.title('Currently empty')

    # plt.show()
    plt.savefig(Outputs.interactome_network_stats)

    # pull the groups corresponding to non-random associations.
    return current_info_rel, cluster_props