Exemplo n.º 1
0
 def plot_fit_function(self, num_points=100):
     '''
     try:
         x_coords = np.linspace(self.x_vec[0], self.x_vec[-1], num_points)
     except Exception as message:
         print 'no x axis information specified', message
         return
     '''
     if self.landscape:
         for trace in self.landscape:
             try:
                 #plt.clear()
                 plt.plot(self.x_vec, trace)
                 plt.fill_between(self.x_vec,
                                  trace + float(self.span) / 2,
                                  trace - float(self.span) / 2,
                                  alpha=0.5)
             except Exception:
                 print 'invalid trace...skip'
         plt.axhspan(self.y_vec[0],
                     self.y_vec[-1],
                     facecolor='0.5',
                     alpha=0.5)
         plt.show()
     else:
         print 'No trace generated.'
    def plot_z_vs_obsangle_for_given_age(self, t1, t2, t3, t4, t5, t6, t7,
                                         obsanglemin, obsanglemax, step):

        obsangles = np.arange(obsanglemin, obsanglemax, step)

        plt.figure(figsize=(6, 6))
        plt.rc('axes', linewidth=2)
        plt.plot(obsangles,
                 self.get_SB_vs_obsangle_for_given_age(t1, obsanglemin,
                                                       obsanglemax, step)[5],
                 color='r',
                 label='%s yrs' % t1)
        plt.plot(obsangles,
                 self.get_SB_vs_obsangle_for_given_age(t2, obsanglemin,
                                                       obsanglemax, step)[5],
                 color='orange',
                 label='%s yrs' % t2)
        plt.plot(obsangles,
                 self.get_SB_vs_obsangle_for_given_age(t3, obsanglemin,
                                                       obsanglemax, step)[5],
                 color='y',
                 label='%s yrs' % t3)
        plt.plot(obsangles,
                 self.get_SB_vs_obsangle_for_given_age(t4, obsanglemin,
                                                       obsanglemax, step)[5],
                 color='g',
                 label='%s yrs' % t4)
        plt.plot(obsangles,
                 self.get_SB_vs_obsangle_for_given_age(t5, obsanglemin,
                                                       obsanglemax, step)[5],
                 color='b',
                 label='%s yrs' % t5)
        plt.plot(obsangles,
                 self.get_SB_vs_obsangle_for_given_age(t6, obsanglemin,
                                                       obsanglemax, step)[5],
                 color='purple',
                 label='%s yrs' % t6)
        plt.plot(obsangles,
                 self.get_SB_vs_obsangle_for_given_age(t7, obsanglemin,
                                                       obsanglemax, step)[5],
                 color='m',
                 label='%s yrs' % t7)
        #plt.axhline(y=1630,linestyle='--',color='k',label='Edge of LMC')
        #plt.axhline(y=-1630,linestyle='--',color='k')
        plt.axhspan(-1630,
                    1630,
                    color='skyblue',
                    alpha=0.5,
                    label='Thickness of LMC')
        #plt.axhline(y=2000,linestyle='-.',color='k',label='Edge of 30 Dor')
        plt.xlabel('Observed Angular Separation', fontsize=18)
        plt.ylabel('z (lyrs)', fontsize=18)
        plt.tight_layout()
        plt.legend(loc='upper left', prop={'size': 15})
def plot_episode(args):
    """Plot an episode plucked from the large h5 database"""
    print "plot_episode"
    # load the data file
    tblfilename = "bf_optimize_mavlink.h5"
    h5file = tb.open_file(tblfilename, mode = "a")
    # get the table handle
    table = h5file.root.v2.evaluations

    # selected episode
    episode_row = table.read_coordinates([int(args.epinum)])
    # compare episodes
    episode_row_1 = table.read_coordinates([2, 3, 22, 46]) # bad episodes
    print "row_1", episode_row_1.shape
    # episode_row = table.read_coordinates([3, 87])
    episode_target = episode_row["alt_target"]
    episode_target_1 = [row["alt_target"] for row in episode_row_1]
    print "episode_target_1.shape", episode_target_1
    episode_timeseries = episode_row["timeseries"][0]
    episode_timeseries_1 = [row["timeseries"] for row in episode_row_1]
    print "row", episode_timeseries.shape
    print "row_1", episode_timeseries_1

    sl_start = 0
    sl_end = 2500
    sl_len = sl_end - sl_start
    sl = slice(sl_start, sl_end)
    pl.plot(episode_timeseries[sl,1], "k-", label="alt", lw=2.)
    print np.array(episode_timeseries_1)[:,:,1]
    pl.plot(np.array(episode_timeseries_1)[:,:,1].T, "k-", alpha=0.2)
    # alt_hold = episode_timeseries[:,0] > 4
    alt_hold_act = np.where(episode_timeseries[sl,0] == 11)
    print "alt_hold_act", alt_hold_act[0].shape, sl_len
    alt_hold_act_min = np.min(alt_hold_act)
    alt_hold_act_max = np.max(alt_hold_act)
    print "min, max", alt_hold_act_min, alt_hold_act_max, alt_hold_act_min/float(sl_len), alt_hold_act_max/float(sl_len),

    # pl.plot(episode_timeseries[sl,0] * 10, label="mode")
    pl.axhspan(-100., 1000,
               alt_hold_act_min/float(sl_len),
               alt_hold_act_max/float(sl_len),
               facecolor='0.5', alpha=0.25)
    pl.axhline(episode_target, label="target")
    pl.xlim((0, sl_len))
    pl.xlabel("Time steps [1/50 s]")
    pl.ylabel("Alt [cm]")
    pl.legend()
    if args.plotsave:
        pl.gcf().set_size_inches((10, 3))
        pl.gcf().savefig("%s.pdf" % (sys.argv[0][:-3]), dpi=300, bbox_inches="tight")
    pl.show()
Exemplo n.º 4
0
 def plot_fit_function(self, num_points = 100):
     '''
     try:
         x_coords = np.linspace(self.x_vec[0], self.x_vec[-1], num_points)
     except Exception as message:
         print 'no x axis information specified', message
         return
     '''
     if self.landscape:
         for trace in self.landscape:
             try:
                 #plt.clear()
                 plt.plot(self.x_vec, trace)
                 plt.fill_between(self.x_vec, trace+float(self.span)/2, trace-float(self.span)/2, alpha=0.5)
             except Exception:
                 print 'invalid trace...skip'
         plt.axhspan(self.y_vec[0], self.y_vec[-1], facecolor='0.5', alpha=0.5)
         plt.show()
     else:
         print 'No trace generated.'
    def plot_z_vs_Ang_Sep_for_ages(self, obsanglemin, obsanglemax, step):

        obsangles = np.arange(obsanglemin, obsanglemax, step)
        ages = np.arange(200, 5000, 100)
        alphas = np.linspace(0, 1, len(ages))

        plt.figure(figsize=(6, 6))
        plt.rc('axes', linewidth=2)
        for i in range(0, len(ages)):
            plt.plot(obsangles,
                     self.get_SB_vs_obsangle_for_given_age(
                         ages[i], obsanglemin, obsanglemax, step)[5],
                     color='b',
                     alpha=alphas[i])

        plt.axhspan(-1630,
                    1630,
                    color='r',
                    alpha=0.5,
                    label='Thickness of LMC')
        plt.ylabel('z (ly)', fontsize=20, fontweight='bold')
        plt.xlabel('Angular Separation', fontsize=20, fontweight='bold')
        plt.legend(fontsize=18)
        plt.show()
    def make_area_prob_plot(self):

        #plt.axvspan(0, 0.49475, alpha=0.2, color='gray',label='400')
        plt.axvspan(0, 0.49475, alpha=0.2, color='gray', label='600')
        plt.axvspan(0, 0.4435, alpha=0.2, color='gray', label='800')
        #plt.axvspan(0, 0.4267, alpha=0.2, color='gray',label='200')
        plt.axvspan(0, 0.3665, alpha=0.2, color='gray', label='1000')
        plt.axvspan(0, 0.245, alpha=0.2, color='gray', label='1200')

        plt.axhspan(500, 1200, alpha=0.2, color='gray', label='Ages')
        plt.axhspan(500, 1000, alpha=0.2, color='gray', label='Ages')
        plt.axhspan(500, 800, alpha=0.2, color='gray', label='Ages')
        plt.axhspan(500, 600, alpha=0.2, color='gray', label='Ages')

        plt.xlim(-0.1, 1.1)
        plt.ylim(0, 2000)
        plt.xlabel('Observed Angular Separation')
        plt.ylabel('Age')
Exemplo n.º 7
0
    print
t0 = time.time()
subprocess.call(ffmpegcommand, stdout=DEVNULL, stderr=subprocess.STDOUT,
                shell=True)
t1 = time.time()
print "in", str(round(t1 - t0, 3)), "seconds (" +\
    str(round(options.images / (t1-t0), 3)) + " images per second)"

filename = os.path.join(FileSavePath,
    "snapshot_%03d" % (int(round(options.images / 2.0))) + ".jpg")

image = plt.imread(filename)
plt.imshow(image, origin="lower")
figuretitle = "Snapshot", str(int(round(options.images / 2.0))), "of",\
    str(options.images), "from", FileSavePath, "\nwith an exposure time of",\
    str(options.exposuretime / 10), "ms",
if options.preview:
    plt.axhspan(ymin=CMOSheight-previewheight, ymax=CMOSheight,
                xmin=0, xmax=float(previewwidth)/CMOSwidth,
                facecolor='r', alpha=0.5)
    plt.xlim([0, CMOSwidth])
    plt.ylim([0, CMOSheight])
    figuretitle += "\nred=preview area",
plt.title(' '.join(figuretitle))
plt.show()

print 'Images saved to',
print os.path.abspath(os.path.join(FileSavePath, 'snapshot*.jpg'))
print 80 * "-"
print "done"
Exemplo n.º 8
0
    def plot(self,
             title,
             xlim=None,
             ylim=None,
             plot_type='amplitude',
             show_limits=False,
             show_ssa_limits=False,
             beam_on=False,
             annotate=False,
             beam_start=20,
             beam_end=22,
             hide_fwd=False):
        """ Plot signals of interest in RF Station time-series simulation.
        Supports amplitude, phase and cartesian plots with a number of options.
        """
        fund_k_probe = self.fund_mode_dict['k_probe']
        fund_k_drive = self.fund_mode_dict['k_drive']
        fund_k_em = self.fund_mode_dict['k_em']
        fund_k_beam = self.fund_mode_dict['k_beam']

        # Amplitude
        if plot_type == 'amplitude':
            if hide_fwd == False:
                plt.plot(self.trang * 1e3,
                         np.abs(self.E_fwd) * fund_k_drive,
                         '-',
                         label=r'Forward $\left(\vec E_{\rm fwd}\right)$',
                         linewidth=2)
                plt.plot(self.trang * 1e3,
                         np.abs(self.E_reverse / fund_k_em),
                         '-',
                         label=r'Reverse $\left(\vec E_{\rm reverse}\right)$',
                         linewidth=2)
            plt.plot(self.trang * 1e3,
                     np.abs(self.cav_v),
                     '-',
                     label=r'Cavity Field',
                     linewidth=2,
                     color='r')
            plt.plot(self.trang * 1e3,
                     np.abs(self.set_point / fund_k_probe),
                     '-',
                     label=r'Set-point $\left(\vec E_{\rm sp}\right)$',
                     linewidth=2,
                     color='c')
            # Y label
            plt.ylabel('Amplitude [V]', fontsize=30)

            if show_limits == True:
                plt.plot(self.trang * 1e3,
                         np.abs(self.set_point / fund_k_probe) * (1 + 1e-4),
                         label='Upper limit',
                         linewidth=2,
                         color='m')
                plt.plot(self.trang * 1e3,
                         np.abs(self.set_point / fund_k_probe) * (1 - 1e-4),
                         label='Lower limit',
                         linewidth=2,
                         color='y')
                plt.axhspan(16e6 / 1.00005,
                            16e6 * 1.00005,
                            color='blue',
                            alpha=0.2)

            if show_ssa_limits == True:
                # If SSA noise were a sine wave, this would be the amplitude limit
                ssa_limit = 4e-2 * fund_k_drive * np.sqrt(3.8e3)
                plt.plot(self.trang * 1e3,
                         np.abs(self.set_point / fund_k_probe) +
                         ssa_limit / np.sqrt(2) / 2,
                         label='SSA Upper (RMS) limit',
                         linewidth=2,
                         color='m')
                plt.plot(self.trang * 1e3,
                         np.abs(self.set_point / fund_k_probe) -
                         ssa_limit / np.sqrt(2) / 2,
                         label='SSA Lower (RMS) limit',
                         linewidth=2,
                         color='y')

                low_index = np.where(self.trang == 20e-3)[0][0]
                high_index = np.where(self.trang == 49.9e-3)[0][0]

                ssa_std = np.std(self.E_fwd[low_index:high_index] *
                                 fund_k_drive)
                ssa_std_percent = 100 * ssa_std / (fund_k_drive *
                                                   np.sqrt(3.8e3))
                ssa_std_text = r'$\sigma_{\rm SSA}\,=\,$' + '%.2f %% RMS' % (
                    np.abs(ssa_std_percent))
                plt.text(35,
                         13e6,
                         ssa_std_text,
                         verticalalignment='top',
                         fontsize=30)

        # Phase
        if plot_type == 'phase':
            plt.plot(self.trang * 1e3,
                     np.angle(self.cav_v, deg=True),
                     '-',
                     label=r'Cavity Field',
                     linewidth=2,
                     color='r')
            plt.plot(self.trang * 1e3,
                     np.angle(self.set_point / fund_k_probe, deg=True),
                     label=r'Set-point $\left(\vec E_{\rm sp}\right)$',
                     linewidth=2,
                     color='c')
            if show_limits:
                plt.plot(self.trang * 1e3,
                         1e-2 * np.ones(len(self.trang)),
                         label='Upper limit',
                         linewidth=2,
                         color='m')
                plt.plot(self.trang * 1e3,
                         -1e-2 * np.ones(len(self.trang)),
                         label='Lower limit',
                         linewidth=2,
                         color='y')
                plt.axhspan(-4e-3, 4e-3, color='blue', alpha=0.2)

            # Y label
            plt.ylabel('Phase [degrees]', fontsize=30)

        # Cartesian coordinates
        if plot_type == 'cartesian':
            plt.plot(self.trang * 1e3,
                     np.real(self.E_fwd) * fund_k_drive,
                     '-',
                     label=r'Forward $\Re \left(\vec E_{\rm fwd}\right)$',
                     linewidth=2)
            plt.plot(self.trang * 1e3,
                     np.imag(self.E_fwd) * fund_k_drive,
                     '-',
                     label=r'Forward $\Im \left(\vec E_{\rm fwd}\right)$',
                     linewidth=2)
            plt.plot(self.trang * 1e3,
                     np.real(self.cav_v),
                     '-',
                     label=r'$\Re$ Cavity Field',
                     linewidth=2)
            plt.plot(self.trang * 1e3,
                     np.imag(self.cav_v),
                     '-',
                     label=r'$\Im$ Cavity Field',
                     linewidth=2)

        # Add annotation for a very specific plot
        if annotate:
            delta_E_fwd_text = r'$\Delta \left| \vec E_{\rm fwd} \right| \approx \,$' + '%.d MV' % (
                round(np.abs(fund_k_beam) * 100e-6 * 1e-6))
            plt.annotate(delta_E_fwd_text, xy=(21.05, 18e6), fontsize=25)
            index_of_21 = np.where(self.trang == 21e-3)
            plt.annotate(s='',
                         xy=(21,
                             np.abs(self.E_fwd[index_of_21]) * fund_k_drive),
                         xytext=(21, np.abs(self.cav_v[index_of_21])),
                         arrowprops=dict(arrowstyle='<->'))

        # Add clear red shade if beam current is on to highlight the location of the beam train
        if beam_on:
            plt.axvspan(beam_start, beam_end, color='red', alpha=0.2)

        plt.xticks(fontsize=20)
        plt.yticks(fontsize=20)
        plt.title(title, fontsize=40, y=1.02)
        plt.xlabel('Time [ms]', fontsize=30)

        if xlim:
            plt.xlim(xlim)

        if ylim:
            plt.ylim(ylim)

        plt.rc('font', **{'size': 20})
        plt.legend(loc='upper right')

        plt.show()
Exemplo n.º 9
0
def run(appfolder, stationName, data):
    try:
        Data={}
        for k, v in data.iteritems():
            Data[int(k)] = {}
            for a, b in v.iteritems():
                Data[int(k)][int(a)] = map(lambda x: x if x!=None else 0,b)

        print "DATA TO RUN...", Data
        Years = Data.keys() # My Index with Years
        #-------------------------------------------------------------------------------
        # Calculation for Thronwaite Potential Evapotranspiration Formula
        # FinalData:  Data = {Year:Month:[Rain,Temp,i_index,PET}
        #-------------------------------------------------------------------------------
        #Calculation i_index
        for Year in Years:
            for i in [1,2,3,4,5,6,7,8,9,10,11,12]:
                if Data[Year][i][1]>0:
                    i_index = 0.09*(Data[Year][i][1])**1.514
                else: #if Data[Year][i][1]<=0:
                    i_index = 0
                Data[Year][i].append(i_index)
        #Daylight Hour  (Just for Now) Constant =12 hour <----- THIS WILL CHANGE!!!
        N=12
        for Year in Years:
            sum_I=0
            for i in [1,2,3,4,5,6,7,8,9,10,11,12]:
                sum_I = sum_I + Data[Year][i][2]
            for i in [1,2,3,4,5,6,7,8,9,10,11,12]:
                DaysPerMonth = [31,28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
                if calendar.isleap(Year) == True:
                    DaysPerMonth[1]=29
                N=12
                if Data[Year][i][1] == 0:
                    PET = 0
                else:
                    if sum_I != 0: #prevent division by zero
                        PET  = 16 * (10 * (Data[Year][i][1])/sum_I)* ( DaysPerMonth[i-1] * N/360)
                    else:
                        print stationName, Data
                        PET = 0
                if PET <= 0 :
                    Data[Year][i].append(0)
                else:
                    Data[Year][i].append(PET)
        #-------------------------------------------------------------------------------
        # FINAL DATASET IS READY FOR CALC SPI AND RDI
        #-------------------------------------------------------------------------------
        # Get Special Rain and Rain/PET  DataSet :
        rain1_data = []
        rain3_data = []
        rain6_data = []
        rain12_data = []
        rainpet1 = []
        rainpet3 = []
        rainpet6 = []
        rainpet12 = []

        for Year in Years:
            #Rain1
            for i in [1,2,3,4,5,6,7,8,9,10,11,12]:
                rain1_data.append(Data[Year][i][0])
                if Data[Year][i][3]==0:
                    rainpet1.append(0)
                else:
                    rainpet1.append(float(Data[Year][i][0])/Data[Year][i][3])
            #Rain3 Selected Months
            Months= [[1,2,3],[4,5,6],[7,8,9],[10,11,12]] # Every 3 Months Selections
            for month in Months:
                sum_rain3 = 0 # Loop for Summing
                sum_RP3 = 0
                for i in [0,1,2]:
                    sum_rain3 = sum_rain3+Data[Year][month[i]][0] # Selected Values
                    if Data[Year][month[i]][3] != 0:
                        sum_RP3 = sum_RP3 +float(Data[Year][month[i]][0])/(Data[Year][month[i]][3])
                    else:
                        sum_RP3 = sum_RP3 + 0
                rain3_data.append(sum_rain3)
                rainpet3.append(sum_RP3)
            #Rain 6 Selected
            Months= [[1,2,3,4,5,6],[7,8,9,10,11,12]]
            for month in Months:
                sum_rain6 = 0
                sum_RP6 = 0
                for i in [0,1,2,3,4,5]:
                    sum_rain6 = sum_rain6+Data[Year][month[i]][0]
                    if Data[Year][month[i]][3] !=0:
                        sum_RP6 = sum_RP6 +float(Data[Year][month[i]][0])/(Data[Year][month[i]][3])
                    else:
                        sum_RP6 = sum_RP6 + 0
                rain6_data.append(sum_rain6)
                rainpet6.append(sum_RP6)
            #Rain12 Selected not so pythonic way
            Months= [[1,2,3,4,5,6,7,8,9,10,11,12]]
            for month in Months:
                sum_rain12 = 0
                sum_RP12 = 0
                for i in [0,1,2,3,4,5,6,7,8,9,10,11]:
                    sum_rain12 = sum_rain12+Data[Year][month[i]][0]
                    if Data[Year][month[i]][3] !=0:
                        sum_RP12 = sum_RP12 +float(Data[Year][month[i]][0])/(Data[Year][month[i]][3])
                    else:
                       sum_RP12 = sum_RP12 + 0
                rain12_data.append(sum_rain12)
                rainpet12.append(sum_RP12)
        #-------------------------------------------------------------------------------------------
        # Datasets For Spi Analusis  - YOU NEED TO TEST FOR RANDOM NULL VALUES!!!
        dataspi = {"Spi1":rain1_data,"Spi3":rain3_data,"Spi6":rain6_data,"Spi12":rain12_data}
        datardi = {"Rdi1":rainpet1,"Rdi3":rainpet3,"Rdi6":rainpet6,"Rdi12":rainpet12}

        #-------------------------------------------------------------------------------------------
        #         RESULTS
        # Note : Problem with RDI (Cant figure out yet)
        #-------------------------------------------------------------------------------------------
        #THIS THE OUTPUT YOU NEED FOR THE WEB APP -- Spi diffferent time Span Resulsv
        spiresults = {"Spi1": spi_calc(dataspi["Spi1"]),"Spi3": spi_calc(dataspi["Spi3"]),"Spi6": spi_calc(dataspi["Spi6"]),"Spi12": spi_calc(dataspi["Spi12"])}
        rdiresults = {"Rdi1": rdi_calc(datardi["Rdi1"]),"Rdi3": rdi_calc(datardi["Rdi3"]),"Rdi6": rdi_calc(datardi["Rdi6"]),"Rdi12": rdi_calc(datardi["Rdi12"])}

        #-------------------------------------------------------------------------------------------
        #          Charts
        #-------------------------------------------------------------------------------------------
        stationname = stationName
        minyear=min(Years)
        maxyear=max(Years)
        keysspi = spiresults.keys()
        keysrdi = rdiresults.keys()
        # Spi1 and Rdi1 Chart-------------------------------------------------------------------------------------------
        plt.figure(figsize=(6*3.13,4*3.13))
        plt.subplot(211)
        plt.axhspan(0, -0.8, facecolor='0.5', alpha=0.5,color ='yellow',linewidth = True)
        plt.axhspan(-0.8, -1.3, facecolor='0.5', alpha=0.5,color ='orange',linewidth = True)
        plt.axhspan(-1.3, -1.6, facecolor='0.5', alpha=0.5,color ='orangered',linewidth = True)
        plt.axhspan(-1.6, -2, facecolor='0.5', alpha=0.5,color ='r',linewidth = True)
        plt.axhspan(-2, -3, facecolor='0.5', alpha=0.5,color ='maroon',linewidth = True)
        Spi1= spiresults["Spi1"]['Spi_result']
        Rdi1= rdiresults["Rdi1"]['Rdi_result']
        n=len(Spi1)
        ind = np.arange(n)
        width = 0.40
        rects1 = plt.bar(ind, Spi1, width, color='b', label='SPI ')
        rects2 = plt.bar(ind+width, Rdi1, width, color='lightblue', label='RDI ')
        plt.ylabel('Drought Index - std units')
        plt.title('{} {} Drought Propagation Chart for The Period {} - {}'.format('[1 month]', stationname,minyear,maxyear))
        #Need to search in net , convert list to list of str
        xYears = []
        for Year in Years:
            xYears.append(str(Year))
        myTicks = range(0,n,12)
        plt.xticks(myTicks,xYears,rotation ="vertical")
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlim(xmax = n)
        plt.ylim([-3,3])
        plt.text( len(Spi1), 2.5, 'Wet Conditions', rotation=90)
        plt.text( len(Spi1), -0.5, 'Dry Conditions', rotation=90)
        plt.subplot(212)
        plt.scatter(Spi1,rain1_data,label='Spi ')
        plt.scatter(Rdi1,rain1_data,c="lightblue",label='Rdi ')
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlabel("Drought Index")
        plt.ylabel("Rainfall (mm)")
        plt.grid()
        #plt.show()
        #return [os.path.join(os.getcwd(), f) for f in os.listdir(os.getcwd())]

        plt.savefig(appfolder+'/static/charts'
                            '/'+stationName+'_1month.png')
        # <--------------------------------------------PIC ANTONIS
        plt.close()
        # Note some info inside the chart will be added later
        # Spi3 and Rdi3 Chart-------------------------------------------------------------------------------------------
        Spi3= spiresults["Spi3"]['Spi_result']
        Rdi3= rdiresults["Rdi3"]['Rdi_result']
        plt.figure(figsize=(6*3.13,4*3.13))
        plt.subplot(211)
        plt.axhspan(0, -0.8, facecolor='0.5', alpha=0.5,color ='yellow',linewidth = True)
        plt.axhspan(-0.8, -1.3, facecolor='0.5', alpha=0.5,color ='orange',linewidth = True)
        plt.axhspan(-1.3, -1.6, facecolor='0.5', alpha=0.5,color ='orangered',linewidth = True)
        plt.axhspan(-1.6, -2, facecolor='0.5', alpha=0.5,color ='r',linewidth = True)
        plt.axhspan(-2, -3, facecolor='0.5', alpha=0.5,color ='maroon',linewidth = True)
        n=len(Spi3)
        ind = np.arange(n)
        width = 0.40
        rects1 = plt.bar(ind, Spi3, width, color='b', label='SPI ')
        rects2 = plt.bar(ind + width, Rdi3, width, color='lightblue', label='RDI ')
        plt.ylabel('Drought Index - std units')
        plt.title('{} {} Drought Propagation Chart for The Period {} - {}'.format('[3 Month]', stationname,minyear,maxyear))
        #Need to search in net , convert list to list of str
        xYears = []
        for Year in Years:
            xYears.append(str(Year))
        myTicks = range(0,n,4)
        plt.xticks(myTicks,xYears,rotation='vertical')
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlim(xmax = n)
        plt.ylim([-3,3])
        plt.text( len(Spi3), 2.5, 'Wet Conditions', rotation=90)
        plt.text( len(Spi3), -0.5, 'Dry Conditions', rotation=90)
        plt.grid()
        #plt.show()
        plt.subplot(212)
        plt.scatter(Spi3,rain3_data,label='SPI ')
        plt.scatter(Rdi3,rain3_data,c="lightblue",label='RDI ')
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlabel("Drought Index")
        plt.ylabel("Rainfall (mm)")
        plt.grid()
        plt.savefig(appfolder+'static/charts'
                            '/'+stationName+'_3month.png')
        plt.close()

        # Spi6 and Rdi6 Chart-------------------------------------------------------------------------------------------
        plt.figure(figsize=(6*3.13,4*3.13))
        plt.subplot(211)
        Spi6= spiresults["Spi6"]['Spi_result']
        Rdi6= rdiresults["Rdi6"]['Rdi_result']
        plt.axhspan(0, -0.8, facecolor='0.5', alpha=0.5,color ='yellow',linewidth = True)
        plt.axhspan(-0.8, -1.3, facecolor='0.5', alpha=0.5,color ='orange',linewidth = True)
        plt.axhspan(-1.3, -1.6, facecolor='0.5', alpha=0.5,color ='orangered',linewidth = True)
        plt.axhspan(-1.6, -2, facecolor='0.5', alpha=0.5,color ='r',linewidth = True)
        plt.axhspan(-2, -3, facecolor='0.5', alpha=0.5,color ='maroon',linewidth = True)
        n=len(Spi6)
        ind = np.arange(n)
        width = 0.40
        rects1 = plt.bar(ind, Spi6, width, color='b', label='SPI')
        rects2 = plt.bar(ind + width, Rdi6, width, color='lightblue', label='RDI')
        plt.ylabel('Drought Index - std units')
        plt.title('{} {} Drought Propagation Chart for The Period {} - {}'.format('[6 Month]', stationname,minyear,maxyear))
        #Need to search in net , convert list to list of str
        xYears = []
        for Year in Years:
            xYears.append(str(Year))
        myTicks = range(0,n,2)
        plt.xticks(myTicks,xYears,rotation='vertical')
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlim(xmax = n)
        plt.ylim([-3,3])
        plt.grid()
        plt.text( len(Spi6) , 2.5, 'Wet Conditions', rotation=90)
        plt.text( len(Spi6) , -0.5, 'Dry Conditions', rotation=90)
        #plt.show()
        plt.subplot(212)
        plt.scatter(Spi6,rain6_data,label='SPI')
        plt.scatter(Rdi6,rain6_data,c="r",label='RDI ')
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlabel("Drought Index")
        plt.ylabel("Rainfall (mm)")
        plt.grid()
        plt.savefig(appfolder+'static/charts'
                            '/'+stationName+'_6month.png')
        plt.close()
        # Spi12 and Rdi12 Chart-------------------------------------------------------------------------------------------
        Spi12= spiresults["Spi12"]['Spi_result']
        Rdi12= rdiresults["Rdi12"]['Rdi_result']
        plt.figure(figsize=(6*3.13,4*3.13))
        plt.subplot(211)
        n=len(Spi12)
        ind = np.arange(n)
        width = 0.40
        plt.axhspan(0, -0.8, facecolor='0.5', alpha=0.5,color ='yellow',linewidth = True)
        plt.axhspan(-0.8, -1.3, facecolor='0.5', alpha=0.5,color ='orange',linewidth = True)
        plt.axhspan(-1.3, -1.6, facecolor='0.5', alpha=0.5,color ='orangered',linewidth = True)
        plt.axhspan(-1.6, -2, facecolor='0.5', alpha=0.5,color ='r',linewidth = True)
        plt.axhspan(-2, -3, facecolor='0.5', alpha=0.5,color ='maroon',linewidth = True)
        rects1 = plt.bar(ind, Spi12, width, color='b', label='Spi ')
        rects2 = plt.bar(ind + width, Rdi12, width, color='lightblue', label='Rdi ')
        plt.ylabel('Drought Index - std units')
        plt.title('{} {} Drought Propagation Chart for The Period {} - {}'.format('[12 Month]', stationname,minyear,maxyear))
        #Need to search in net , convert list to list of str
        xYears = []
        for Year in Years:
            xYears.append(str(Year))
        myTicks=range(0,n)
        plt.xticks(myTicks,xYears,rotation='vertical')
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlim(xmax = n)
        plt.ylim([-3,3])
        plt.grid()
        plt.text( len(Spi12), 2.5, 'Wet Conditions', rotation=90)
        plt.text( len(Spi12), -0.5, 'Dry Conditions', rotation=90)
        #plt.show()
        plt.subplot(212)
        plt.scatter(Spi12,rain12_data,label='SPI ')
        plt.scatter(Rdi12,rain12_data,c="lightblue",label='RDI')
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlabel("Drought Index")
        plt.ylabel("Rainfall (mm)")
        plt.grid()
        plt.savefig(appfolder+'static/charts'
                            '/'+stationName+'_12month.png')
        plt.close()
        print stationName, "Charts Update run completed... OK"
    except Exception, e:
        print "Drought Error for", stationName,":", e.message, e.args
def pid_pca(args):
    #  import modular data processing toolkit
    import mdp
    # load data file
    tblfilename = "bf_optimize_mavlink.h5"
    h5file = tb.open_file(tblfilename, mode = "a")
    # table = h5file.root.v1.evaluations
    #  get tabke handle
    table = h5file.root.v2.evaluations

    # sort rows
    if not table.cols.mse.is_indexed:
        table.cols.mse.createCSIndex()
        
    if not args.sorted:
        pids = [ [x["alt_p"], x["alt_i"], x["alt_d"], x["vel_p"], x["vel_i"], x["vel_d"]]
             for x in table.iterrows() ]
        mses = [ [x["mse"]] for x in table.iterrows() ]
    else:
        pids = [ [x["alt_p"], x["alt_i"], x["alt_d"], x["vel_p"], x["vel_i"], x["vel_d"]] for x in table.itersorted("mse")]
        mses = [ [x["mse"]] for x in table.itersorted("mse")]
    print "best two", pids
    mses_a = np.log(np.clip(np.array(mses), 0, 200000.))
    mses_a /= np.max(mses_a)
    # FIXME: try kernel pca on this
    from sklearn.decomposition import PCA, KernelPCA, SparsePCA
    kpca = KernelPCA(n_components = None,
                     kernel="rbf", degree=6, fit_inverse_transform=True,
                     gamma=1/6., alpha=1.)
    # kpca = SparsePCA(alpha=2., ridge_alpha=0.1)
    X_kpca = kpca.fit_transform(np.asarray(pids).astype(float))
    # X_back = kpca.inverse_transform(X_kpca)

    Z_kpca = kpca.transform(np.asarray(pids).astype(float))

    print Z_kpca.shape, X_kpca.shape
    print "|Z_kpca|", np.linalg.norm(Z_kpca, 2, axis=1)
    # for i in range(8):
    #     pl.subplot(8,1,i+1)
    #     pl.plot(Z_kpca[:,i])
    #     pl.legend()
    # pl.show()

    
    # fast PCA
    # pid_p = mdp.pca(np.array(pids).astype(float))
    pid_array = np.array(pids).astype(float)
    print "pid_array.shape", pid_array.shape
    pcanode = mdp.nodes.PCANode(output_dim = 6)
    # pcanode.desired_variance = 0.75
    pcanode.train(np.array(pids).astype(float))
    pcanode.stop_training()
    print "out dim", pcanode.output_dim

    pid_p = pcanode.execute(np.array(pids).astype(float))

    # pid_array_mse = np.hstack((np.array(pids).astype(float), mses_a))
    pid_ica = mdp.fastica(np.array(pids).astype(float))
    print "ica.shape", pid_ica.shape
    # pid_p = np.asarray(pids)[:,[0, 3]]
    # pid_p = pids[:,0:2]
    # [:,0:2]
    sl_start = 0
    sl_end = 100
    sl = slice(sl_start, sl_end)

    print "expl var", pcanode.explained_variance
    pl.subplot(111)
    colors = np.zeros((100, 3))
    # colors = np.hstack((colors, 1-(0.5*mses_a)))
    colors = np.hstack((colors, 1-(0.8*mses_a)))
    # print colors.shape
    # pl.scatter(pid_p[sl,0], pid_p[sl,1], color=colors)

    # ica spektrum
    pid_ica_sum = np.sum(np.square(pid_ica), axis=0)
    # pid_ica_sum_sort = np.sort(pid_ica_sum)
    pid_ica_sum_0 = np.argmax(pid_ica_sum)
    pid_ica_sum[pid_ica_sum_0] = 0
    pid_ica_sum_1 = np.argmax(pid_ica_sum)
    
    # pl.scatter(pid_p[sl,0], pid_p[sl,1], color=colors)
    pl.scatter(pid_ica[sl,pid_ica_sum_0], pid_ica[sl,pid_ica_sum_1], color=colors)
    # pl.scatter(X_kpca[:,0], X_kpca[:,1], color=colors)
    pl.gca().set_aspect(1)
    # pl.scatter(pid_p[:,0], pid_p[:,1], alpha=1.)
    # pl.show()

    # plot raw pid values     
    pl.subplot(411)
    pl.plot(pid_array[sl,[0,3]], "o")
    pl.xlim((sl_start - 0.2, sl_end + 0.2))
    pl.subplot(412)
    pl.plot(pid_array[sl,[1,4]], "o")
    pl.xlim((sl_start - 0.2, sl_end + 0.2))
    pl.subplot(413)
    pl.plot(pid_array[sl,[2,5]], "o")

    # plot compressed pid values: pca, ica, ...
    # pl.subplot(211)
    # pl.plot(pid_p, ".")
    # pl.plot(pid_p[sl], "o")
    # pl.plot(pid_ica[sl] + np.random.uniform(-0.01, 0.01, size=pid_ica[sl].shape), "o")
    pl.xlim((sl_start - 0.2, sl_end + 0.2))
    # pl.plot(Z_kpca[:,:], "-o", label="kpca")
    # pl.plot(Z_kpca[:,:], ".", label="kpca")
    # pl.legend()
        
    # pl.subplot(212)
    pl.subplot(414)
    pl.plot(mses_a[sl], "ko")
    # pl.gca().set_yscale("log")
    pl.xlim((sl_start - 0.2, sl_end + 0.2))
    pl.show()

    # gp fit
    x = mses_a[sl]
    x_sup = np.atleast_2d(np.arange(0, x.shape[0])).T
    x_ones = x != 1.
    x_ones[0:20] = False
    print x, x_sup, x_ones, x_ones.shape
    print "x[x_ones]", x[x_ones].shape
    print "x_sup[x_ones]", x_sup[x_ones].shape

    from sklearn.gaussian_process import GaussianProcess
    # gp = GaussianProcess(regr='constant', corr='absolute_exponential',
    #                  theta0=[1e-4] * 1, thetaL=[1e-12] * 1,
    #                  thetaU=[1e-2] * 1, nugget=1e-2, optimizer='Welch')
    gp = GaussianProcess(corr="squared_exponential",
                         theta0=1e-2, thetaL=1e-4, thetaU=1e-1,
                         nugget=1e-1/x[x_ones])
    gp.fit(x_sup[x_ones,np.newaxis], x[x_ones,np.newaxis])
    x_pred, sigma2_pred = gp.predict(x_sup, eval_MSE=True)
    print x_pred, sigma2_pred

    from sklearn import linear_model
    clf = linear_model.Ridge (alpha = .5)
    clf.fit(x_sup[x_ones,np.newaxis], x[x_ones,np.newaxis])
    x_pred = clf.predict(x_sup[20:100])
        
    pl.subplot(111)
    pl.plot(mses_a[sl], "ko")
    x_mean = np.mean(x[0:20])
    pl.plot(np.arange(0, 20), np.ones((20, )) * x_mean, "k-", alpha=0.5)
    pl.plot(np.arange(20, 100), x_pred, "k-", alpha=0.5)
    pl.axhspan(0.5, 1.1, 0, 0.19, facecolor="0.5", alpha=0.25)
    # pl.plot(x_pred + sigma2_pred, "k-", alpha=0.5)
    # pl.plot(x_pred - sigma2_pred, "k-", alpha=0.5)
    # pl.gca().set_yscale("log")
    pl.xlim((sl_start - 0.2, sl_end + 0.2))
    pl.ylim((0.5, 1.1))
    pl.text(5, 0.6, "Random\ninitialization")
    pl.text(40, 0.6, "Optimizer\nsuggestions")
    pl.xlabel("Episode #")
    pl.ylabel("MSE")
    if args.plotsave:
        pl.gcf().set_size_inches((10, 3))
        pl.gcf().savefig("%s-mse.pdf" % (sys.argv[0][:-3]), dpi=300,
                        bbox_inches="tight")
    pl.show()
Exemplo n.º 11
0
def run(appfolder, stationName, data, userid):
    try:
        userid = str(userid)
        print appfolder + 'static/usercharts/' + userid + '.png'
        Data = {}
        for k, v in data.iteritems():
            Data[int(k)] = {}
            for a, b in v.iteritems():
                Data[int(k)][int(a)] = map(lambda x: x if x != None else 0, b)

        Years = Data.keys()  # My Index with Years
        im = Image.open(appfolder + 'static/images/logo.jpg')
        height = im.size[1]
        # We need a float array between 0-1, rather than
        # a uint8 array between 0-255
        im = np.array(im).astype(np.float) / 255
        #Exaple how to get Rain or Temp for my Data
        #print "Example:  01/1956 Rain And Temp"
        #print "Data[2008][1][0] : {}".format( Data[1956][1][0])
        #print "Data[2009][1][1] : {}" .format( Data[1956][1][1])

        #-------------------------------------------------------------------------------
        # Calculation for Thronwaite Potential Evapotranspiration Formula
        # FinalData:  Data = {Year:Month:[Rain,Temp,i_index,PET}
        #-------------------------------------------------------------------------------
        #-------------------------------------------------------------------------------
        # FINAL DATASET IS READY FOR CALC SPI AND RDI
        #-------------------------------------------------------------------------------
        # Get Special Rain and Rain/PET  DataSet :
        Rain1_data = []
        Rain3_data = []
        Rain6_data = []
        Rain12_data = []

        for Year in Years:
            #Rain1
            for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]:
                Rain1_data.append(Data[Year][i][0])
            #Rain3 Selected Months
            Months = [[1, 2, 3], [4, 5, 6], [7, 8, 9],
                      [10, 11, 12]]  # Every 3 Months Selections

            for month in Months:
                sum_rain3 = 0  # Loop for Summing
                for i in [0, 1, 2]:
                    sum_rain3 = sum_rain3 + Data[Year][month[i]][
                        0]  # Selected Values
                    #print Year,month[i],sum_rain3 #Debug
                Rain3_data.append(sum_rain3)
            #Rain 6 Selected
            Months = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]
            for month in Months:
                sum_rain6 = 0
                for i in [0, 1, 2, 3, 4, 5]:
                    sum_rain6 = sum_rain6 + Data[Year][month[i]][0]
                Rain6_data.append(sum_rain6)
            #Rain12 Selected not so pythonic way
            Months = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]]
            for month in Months:
                sum_rain12 = 0
                for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]:
                    sum_rain12 = sum_rain12 + Data[Year][month[i]][0]
                Rain12_data.append(sum_rain12)
        #-------------------------------------------------------------------------------------------
        # Datasets For Spi Analusis  - YOU NEED TO TEST FOR RANDOM NULL VALUES!!!
        DataSPi = {
            "Spi1": Rain1_data,
            "Spi3": Rain3_data,
            "Spi6": Rain6_data,
            "Spi12": Rain12_data
        }

        #-------------------------------------------------------------------------------------------
        #         RESULTS
        # Note : Problem with RDI (Cant figure out yet)
        #-------------------------------------------------------------------------------------------
        #THIS THE OUTPUT YOU NEED FOR THE WEB APP -- Spi diffferent time Span Resulsv
        SPIresults = {
            "Spi1": spi_calc(DataSPi["Spi1"]),
            "Spi3": spi_calc(DataSPi["Spi3"]),
            "Spi6": spi_calc(DataSPi["Spi6"]),
            "Spi12": spi_calc(DataSPi["Spi12"])
        }

        #-------------------------------------------------------------------------------------------
        #          Charts
        #-------------------------------------------------------------------------------------------
        StationName = stationName
        MinYear = min(Years)
        MaxYear = max(Years)
        KeysSpi = SPIresults.keys()
        # Spi1 and Rdi1 Chart-------------------------------------------------------------------------------------------
        fig = plt.figure(figsize=(6 * 3.13, 4 * 3.13))
        plt.subplot(211)
        plt.axhspan(0,
                    -0.8,
                    facecolor='0.5',
                    alpha=0.5,
                    color='yellowgreen',
                    linewidth=True)
        plt.axhspan(-0.8,
                    -1.3,
                    facecolor='0.5',
                    alpha=0.5,
                    color='yellow',
                    linewidth=True)
        plt.axhspan(-1.3,
                    -1.6,
                    facecolor='0.5',
                    alpha=0.5,
                    color='goldenrod',
                    linewidth=True)
        plt.axhspan(-1.6,
                    -2,
                    facecolor='0.5',
                    alpha=0.5,
                    color='r',
                    linewidth=True)
        plt.axhspan(-2,
                    -3,
                    facecolor='0.5',
                    alpha=0.5,
                    color='darkred',
                    linewidth=True)
        Spi1 = SPIresults["Spi1"]['Spi_result']
        n = len(Spi1)
        ind = np.arange(n)
        width = 0.40
        rects1 = plt.bar(ind, Spi1, width, color='b', label='Spi ')
        plt.ylabel('Drought Index - std units')
        plt.title(
            '{} {} Drought Propagation Chart for The Period {} - {}'.format(
                '[1 month]', StationName, MinYear, MaxYear))
        #Need to search in net , convert list to list of str
        xYears = []
        for Year in Years:
            xYears.append(str(Year))
        myTicks = range(0, n, 12)
        plt.xticks(myTicks, xYears, rotation="vertical")
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlim(xmax=n)
        plt.ylim([-3, 3])
        plt.text(len(Spi1), 2.5, 'Wet Conditions', rotation=90)
        plt.text(len(Spi1), -0.5, 'Dry Conditions', rotation=90)
        plt.subplot(212)
        plt.scatter(Spi1, Rain1_data, label='Spi ')
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlabel("Drought Index")
        plt.ylabel("Rainfall (mm)")
        plt.grid()
        fig.figimage(im, 0)
        #plt.show()
        #return [os.path.join(os.getcwd(), f) for f in os.listdir(os.getcwd())]

        plt.savefig(appfolder + 'static/usercharts/' + stationName +
                    'SPI_1month_' + userid + '.png')
        # <--------------------------------------------PIC ANTONIS
        plt.close()
        # Note some info inside the chart will be added later
        # Spi3 and Rdi3 Chart-------------------------------------------------------------------------------------------
        Spi3 = SPIresults["Spi3"]['Spi_result']
        plt.figure(figsize=(6 * 3.13, 4 * 3.13))
        plt.subplot(211)
        plt.axhspan(0,
                    -0.8,
                    facecolor='0.5',
                    alpha=0.5,
                    color='yellowgreen',
                    linewidth=True)
        plt.axhspan(-0.8,
                    -1.3,
                    facecolor='0.5',
                    alpha=0.5,
                    color='yellow',
                    linewidth=True)
        plt.axhspan(-1.3,
                    -1.6,
                    facecolor='0.5',
                    alpha=0.5,
                    color='goldenrod',
                    linewidth=True)
        plt.axhspan(-1.6,
                    -2,
                    facecolor='0.5',
                    alpha=0.5,
                    color='r',
                    linewidth=True)
        plt.axhspan(-2,
                    -3,
                    facecolor='0.5',
                    alpha=0.5,
                    color='darkred',
                    linewidth=True)
        n = len(Spi3)
        ind = np.arange(n)
        width = 0.40
        rects1 = plt.bar(ind, Spi3, width, color='b', label='Spi ')
        plt.ylabel('Drought Index - std units')
        plt.title(
            '{} {} Drought Propagation Chart for The Period {} - {}'.format(
                '[3 Month]', StationName, MinYear, MaxYear))
        #Need to search in net , convert list to list of str
        xYears = []
        for Year in Years:
            xYears.append(str(Year))
        myTicks = range(0, n, 4)
        plt.xticks(myTicks, xYears, rotation='vertical')
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlim(xmax=n)
        plt.ylim([-3, 3])
        plt.text(len(Spi3), 2.5, 'Wet Conditions', rotation=90)
        plt.text(len(Spi3), -0.5, 'Dry Conditions', rotation=90)
        plt.grid()
        #plt.show()
        plt.subplot(212)
        plt.scatter(Spi3, Rain3_data, label='Spi ')
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlabel("Drought Index")
        plt.ylabel("Rainfall (mm)")
        plt.savefig(appfolder + 'static/usercharts/' + stationName +
                    'SPI_3month_' + userid + '.png')
        plt.close()

        # Spi6 and Rdi6 Chart-------------------------------------------------------------------------------------------
        plt.figure(figsize=(6 * 3.13, 4 * 3.13))
        plt.subplot(211)
        Spi6 = SPIresults["Spi6"]['Spi_result']
        plt.axhspan(0,
                    -0.8,
                    facecolor='0.5',
                    alpha=0.5,
                    color='yellowgreen',
                    linewidth=True)
        plt.axhspan(-0.8,
                    -1.3,
                    facecolor='0.5',
                    alpha=0.5,
                    color='yellow',
                    linewidth=True)
        plt.axhspan(-1.3,
                    -1.6,
                    facecolor='0.5',
                    alpha=0.5,
                    color='goldenrod',
                    linewidth=True)
        plt.axhspan(-1.6,
                    -2,
                    facecolor='0.5',
                    alpha=0.5,
                    color='r',
                    linewidth=True)
        plt.axhspan(-2,
                    -3,
                    facecolor='0.5',
                    alpha=0.5,
                    color='darkred',
                    linewidth=True)
        n = len(Spi6)
        ind = np.arange(n)
        width = 0.40
        rects1 = plt.bar(ind, Spi6, width, color='b', label='Spi')
        plt.ylabel('Drought Index - std units')
        plt.title(
            '{} {} Drought Propagation Chart for The Period {} - {}'.format(
                '[6 Month]', StationName, MinYear, MaxYear))
        #Need to search in net , convert list to list of str
        xYears = []
        for Year in Years:
            xYears.append(str(Year))
        myTicks = range(0, n, 2)
        plt.xticks(myTicks, xYears, rotation='vertical')
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlim(xmax=n)
        plt.ylim([-3, 3])
        plt.grid()
        plt.text(len(Spi6), 2.5, 'Wet Conditions', rotation=90)
        plt.text(len(Spi6), -0.5, 'Dry Conditions', rotation=90)
        #plt.show()
        plt.subplot(212)
        plt.scatter(Spi6, Rain6_data, label='Spi ')
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlabel("Drought Index")
        plt.ylabel("Rainfall (mm)")
        plt.grid()
        plt.savefig(appfolder + 'static/usercharts/' + stationName +
                    'SPI_6month_' + userid + '.png')
        plt.close()
        # Spi12 and Rdi12 Chart-------------------------------------------------------------------------------------------
        Spi12 = SPIresults["Spi12"]['Spi_result']
        plt.figure(figsize=(6 * 3.13, 4 * 3.13))
        plt.subplot(211)
        n = len(Spi12)
        ind = np.arange(n)
        width = 0.40
        plt.axhspan(0,
                    -0.8,
                    facecolor='0.5',
                    alpha=0.5,
                    color='yellowgreen',
                    linewidth=True)
        plt.axhspan(-0.8,
                    -1.3,
                    facecolor='0.5',
                    alpha=0.5,
                    color='yellow',
                    linewidth=True)
        plt.axhspan(-1.3,
                    -1.6,
                    facecolor='0.5',
                    alpha=0.5,
                    color='goldenrod',
                    linewidth=True)
        plt.axhspan(-1.6,
                    -2,
                    facecolor='0.5',
                    alpha=0.5,
                    color='r',
                    linewidth=True)
        plt.axhspan(-2,
                    -3,
                    facecolor='0.5',
                    alpha=0.5,
                    color='darkred',
                    linewidth=True)
        rects1 = plt.bar(ind, Spi12, width, color='b', label='Spi ')
        plt.ylabel('Drought Index - std units')
        plt.title(
            '{} {} Drought Propagation Chart for The Period {} - {}'.format(
                '[12 Month]', StationName, MinYear, MaxYear))
        #Need to search in net , convert list to list of str
        xYears = []
        for Year in Years:
            xYears.append(str(Year))
        myTicks = range(0, n)
        plt.xticks(myTicks, xYears, rotation='vertical')
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlim(xmax=n)
        plt.ylim([-3, 3])
        plt.grid()
        plt.text(len(Spi12), 2.5, 'Wet Conditions', rotation=90)
        plt.text(len(Spi12), -0.5, 'Dry Conditions', rotation=90)
        #plt.show()
        plt.subplot(212)
        plt.scatter(Spi12, Rain12_data, label='Spi ')
        plt.legend(bbox_to_anchor=(1.025, 1), loc=2, borderaxespad=0.)
        plt.xlabel("Drought Index")
        plt.ylabel("Rainfall (mm)")
        plt.grid()
        plt.savefig(appfolder + 'static/usercharts/' + stationName +
                    'SPI_12month_' + userid + '.png')
        plt.close()
        print "Charts Update Ending"
    except Exception, e:
        print "Drought Error:", e.message, e.args
def scatter_suspects_probabilities(scatter_dicts,
                                   x_ticks,
                                   net_right_prediction,
                                   subjective_right_prediction,
                                   out,
                                   title='Training Curve',
                                   x_label="Iterations",
                                   legend_box_pos=(1, 1)):

    fig, ax1 = plt.subplots()

    fig.set_size_inches(10, 10)

    # ax1.xaxis.grid(True)

    # plt.grid()

    # else:
    #     ax1.xaxis.set_major_formatter(FuncFormatter(ord_to_char))
    #     ax1.xaxis.set_major_locator(MultipleLocator(1))

    ax1.set_ylim(-0.5, 1.5)
    ax1.set_xlabel(x_label, fontsize=15)
    ax1.set_ylabel("KC Probability", fontsize=15)
    ax1.tick_params(labelsize=10)

    legends_plots = []
    legends_strs = []
    for plot_dict in scatter_dicts:

        tmp = ax1.scatter(plot_dict.x,
                          plot_dict.y,
                          color=plot_dict.color,
                          marker=plot_dict.marker,
                          s=plot_dict.markersize,
                          label=plot_dict.legend)

        legends_plots.append(tmp)
        legends_strs.append(plot_dict.legend)

    ax1.yaxis.set_ticks(np.arange(0, 1.1, 0.1))

    ax1.set_xticks(range(len(x_ticks)))
    ax1.set_xticklabels(x_ticks, rotation='vertical', fontsize=10)

    plt.axhline(y=0, xmin=0, xmax=3, linewidth=1, color='grey')
    plt.axhline(y=1, xmin=0, xmax=3, linewidth=1, color='grey')

    plt.axhline(y=0.5,
                xmin=0,
                xmax=3,
                linewidth=1.5,
                zorder=0,
                color='green',
                linestyle='dashed',
                label="0.5 propability. above: KC. below: Healthy")
    plt.axhspan(0.5, 1, facecolor='0.2', alpha=0.2)
    plt.axhspan(0, 0.5, facecolor='0.5', alpha=0.2)

    for i, right_prediction in enumerate(net_right_prediction):
        if right_prediction:
            if subjective_right_prediction[i]:
                ax1.axvspan(i - 0.5, i + 0.5, alpha=0.1, color='green')
            else:
                ax1.axvspan(i - 0.5, i + 0.5, alpha=0.2, color='green')

        else:

            if subjective_right_prediction[i]:
                ax1.axvspan(i - 0.5, i + 0.5, alpha=0.2, color='red')
            else:
                ax1.axvspan(i - 0.5, i + 0.5, alpha=0.1, color='red')

    # Adding legend

    a = mpatches.Patch(alpha=0.1, color='green', label='both predicted kc')
    b = mpatches.Patch(alpha=0.2,
                       color='green',
                       label='net: kc. subjective: healthy')
    c = mpatches.Patch(alpha=0.1, color='red', label='both predicted healthy')
    d = mpatches.Patch(alpha=0.2,
                       color='red',
                       label='subjective: kc. net: healthy')

    handles, labels = ax1.get_legend_handles_labels()
    handles += [a, b, c, d]
    lgd = plt.legend(handles=handles, bbox_to_anchor=legend_box_pos)

    plt.title(wrap_title(title), fontsize=18)

    if out.endswith(".png"):
        out = out.replace(".png", "")

    fig.set_size_inches(15, 10)

    plt.savefig(out,
                bbox_extra_artists=(lgd, ),
                bbox_inches='tight',
                pad_inches=0.5)

    plt.close()
Exemplo n.º 13
0
print '    * and', NumberOfFrames, 'frames of the video (frame*.tif), thus',\
    'a (calculated)', str(round(NumberOfFrames / options.videotime, 2)), 'fps.'
print 'Have fun with this!'

if options.display:
    # Get middle frame
    filename = os.path.join(
        FileSavePath,
        "frame_%05d" % (int(round(NumberOfFrames / 2.0))) + ".tif")
    # Make image
    image = plt.imread(filename)
    plt.imshow(image, cmap='bone')
    figuretitle = "Snapshot", str(int(round(NumberOfFrames / 2.0))), "of", \
                  str(NumberOfFrames), "from", FileSavePath,\
                  "\nwith an exposure time of", str(options.exposuretime), "ms"
    if options.preview:
        plt.axhspan(ymin=CMOSheight - previewheight,
                    ymax=CMOSheight,
                    xmin=0,
                    xmax=float(previewwidth) / CMOSwidth,
                    facecolor='r',
                    alpha=0.5)
        plt.xlim([0, CMOSwidth])
        plt.ylim([0, CMOSheight])
        figuretitle += "| red=preview area",
    plt.title(' '.join(figuretitle))
    plt.show()

print 80 * "-"
print "done"