Exemple #1
0
 def on_epoch_begin(self, epoch, logs={}):
     self.target = self.params['samples']
     self.progbar = ProgBar.Progbar(target=self.target,
                                    newline_on_end=False)
     self.seen = 0
     self.progbar.last_run = 0
     return
def BasicGraphs(GraphPath, Data):
    """Creates basic graphs
    
    Creates graphs of the energy production
    Only creates the graphs if they don't already exist
    
    Creates:
        Production graph of entire period
        Monthly and Daily graphs of production
    
    Parameters
    ----------
    GraphPath : str
        Path to save the graphs.
    Data : array-like
        The data to graph.
    """

    pcount = 0
    toCreate = 0
    if not os.path.isfile(GraphPath + 'Monthly/Share2018-3.png'):
        MonthGraphs = 60
    else:
        MonthGraphs = 0
    if not os.path.isdir(GraphPath + 'Daily/'):
        os.makedirs(GraphPath + 'Daily')
    if not os.listdir(GraphPath + 'Daily'):
        DayGraphs = 150
    else:
        DayGraphs = 0
    if not os.path.isfile(GraphPath + 'Diesel.png'):
        toCreate += 1
    if not os.path.isfile(GraphPath + 'Wind.png'):
        toCreate += 1
    if not os.path.isfile(GraphPath + 'Water.png'):
        toCreate += 1
    if not os.path.isfile(GraphPath + 'Total.png'):
        toCreate += 1
    toCreate += MonthGraphs
    toCreate += DayGraphs
    #toCreate += 1

    progbar = ProgBar.Progbar(target=toCreate,
                              newline_on_end=False,
                              text_description='Creating overview graphs: ')

    years = mdates.YearLocator()  # every year
    months = mdates.MonthLocator()  # every month
    yearsFmt = mdates.DateFormatter('%Y')
    plt.rcParams["figure.figsize"] = [16, 9]

    if not os.path.isfile(GraphPath + 'Diesel.png'):
        pcount += 1
        #print('Creating diesel overview graph')
        fig, ax = plt.subplots()
        ax.plot(Data.index, Data.Diesel, color=Color1)
        # format the ticks
        ax.xaxis.set_major_locator(years)
        ax.xaxis.set_major_formatter(yearsFmt)
        ax.xaxis.set_minor_locator(months)
        # round to nearest years...
        datemin = np.datetime64(Data.index[0], 'Y')
        datemax = np.datetime64(Data.index[-1], 'Y') + np.timedelta64(1, 'Y')
        ax.set_xlim(datemin, datemax)
        plt.title('Timeseries')
        plt.xlabel('Time [date]')
        plt.ylabel('Diesel production [% of total]')
        ax.grid(which='major', alpha=1, color='black')
        ax.grid(which='minor', alpha=0.05, color='black')
        fig.autofmt_xdate()
        plt.savefig(GraphPath + 'Diesel.png')
        plt.close(fig)
        progbar.update(pcount)

    if not os.path.isfile(GraphPath + 'Wind.png'):
        pcount += 1
        #print('Creating Wind overview graph')
        fig, ax = plt.subplots()
        ax.plot(Data.index, Data.Wind, color=Color1)
        # format the ticks
        ax.xaxis.set_major_locator(years)
        ax.xaxis.set_major_formatter(yearsFmt)
        ax.xaxis.set_minor_locator(months)
        # round to nearest years...
        datemin = np.datetime64(Data.index[0], 'Y')
        datemax = np.datetime64(Data.index[-1], 'Y')\
                                       + np.timedelta64(1, 'Y')
        ax.set_xlim(datemin, datemax)
        plt.title('Timeseries')
        plt.xlabel('Time [date]')
        plt.ylabel('Wind production [% of total]')
        ax.grid(which='major', alpha=1, color='black')
        ax.grid(which='minor', alpha=0.05, color='black')
        fig.autofmt_xdate()
        plt.savefig(GraphPath + 'Wind.png')
        plt.close(fig)
        progbar.update(pcount)

    if not os.path.isfile(GraphPath + 'Water.png'):
        pcount += 1
        #print('Creating Water overview graph')
        fig, ax = plt.subplots()
        ax.plot(Data.index, Data.Water, color=Color1)
        # format the ticks
        ax.xaxis.set_major_locator(years)
        ax.xaxis.set_major_formatter(yearsFmt)
        ax.xaxis.set_minor_locator(months)
        # round to nearest years...
        datemin = np.datetime64(Data.index[0], 'Y')
        datemax = np.datetime64(Data.index[-1], 'Y')\
                                       + np.timedelta64(1, 'Y')
        ax.set_xlim(datemin, datemax)
        plt.title('Timeseries')
        plt.xlabel('Time [date]')
        plt.ylabel('Water production [% of total]')
        ax.grid(which='major', alpha=1, color='black')
        ax.grid(which='minor', alpha=0.05, color='black')
        fig.autofmt_xdate()
        plt.savefig(GraphPath + 'Water.png')
        plt.close(fig)
        progbar.update(pcount)

    if not os.path.isfile(GraphPath + 'Total.png'):
        pcount += 1
        #print('Creating Total overview graph')
        fig, ax = plt.subplots()
        ax.plot(Data.index, Data.Total, color=Color1)
        # format the ticks
        ax.xaxis.set_major_locator(years)
        ax.xaxis.set_major_formatter(yearsFmt)
        ax.xaxis.set_minor_locator(months)
        # round to nearest years...
        datemin = np.datetime64(Data.index[0], 'Y')
        datemax = np.datetime64(Data.index[-1], 'Y')\
                                       + np.timedelta64(1, 'Y')
        ax.set_xlim(datemin, datemax)
        plt.title('Timeseries')
        plt.xlabel('Time [date]')
        plt.ylabel('Total production [%]')
        ax.grid(which='major', alpha=1, color='black')
        ax.grid(which='minor', alpha=0.05, color='black')
        fig.autofmt_xdate()
        plt.savefig(GraphPath + 'Total.png')
        plt.close(fig)
        progbar.update(pcount)

    majorTick = mdates.DayLocator(interval=7)  # every month
    majorFmt = mdates.DateFormatter('%d/%m')
    minorTick = mdates.DayLocator()  # every day
    if not os.path.isdir(GraphPath + 'Monthly'):
        #print('Monthly folder not found, creating it')
        os.makedirs(GraphPath + 'Monthly')
    if not os.path.isfile(GraphPath + 'Monthly/Share2018-3.png'):
        progbar = ProgBar.Progbar(target=toCreate,
                                  newline_on_end=False,
                                  text_description='Creating monthly graphs: ')
        month = 3
        year = 3
        done = False
        count = 0
        #print('Creating Monthly share graph [%]')
        while not done:

            pcount += 1
            #if count % 6 == 0:
            #    print('\u220E', end='', flush=True)
            StartPoint = Data.index.get_loc\
                ('201'+str(year)+'-'+str(month)+'-'+'01 00:00',method='nearest')
            EndPoint = Data['201' + str(year) + '-' + str(month)].shape[0]
            fig, ax = plt.subplots()
            ax.plot(Data.index[StartPoint:StartPoint + EndPoint],
                    Data.Diesel['201' + str(year) + '-' + str(month)])
            # format the ticks
            ax.xaxis.set_major_locator(majorTick)
            ax.xaxis.set_major_formatter(majorFmt)
            ax.xaxis.set_minor_locator(minorTick)
            datemin = np.datetime64(Data.index[StartPoint],
                                    'm')  # round to nearest years...
            datemax = np.datetime64(Data.index[StartPoint + EndPoint - 1], 'm')
            ax.set_xlim(datemin, datemax)
            ax.grid(which='major', alpha=1, color='black')
            ax.grid(which='minor', alpha=0.05, color='black')
            fig.autofmt_xdate()
            Diesel = Data.Diesel['201' + str(year) + '-' + str(month)]
            Water = Data.Water['201' + str(year) + '-' + str(month)] + Diesel
            Wind = Data.Wind['201' + str(year) + '-' + str(month)] + Water
            plt.plot(Water, color=Color2)
            plt.plot(Wind, color=Color3)
            plt.fill_between(Wind.index,
                             Wind,
                             Water,
                             facecolor=Color3,
                             alpha=1)
            plt.fill_between(Water.index,
                             Water,
                             Diesel,
                             facecolor=Color2,
                             alpha=1)
            plt.fill_between(Diesel.index,
                             Diesel,
                             0,
                             facecolor=Color1,
                             alpha=1)
            plt.title('201' + str(year) + ' - ' + monthDict[month])
            plt.ylim([-1, 105])
            plt.xlabel('Time [date]')
            plt.ylabel('Total production [%]')
            plt.legend(['Diesel', 'Water', 'Wind'])
            plt.savefig(GraphPath + 'Monthly/' + 'Share201' + str(year) + '-' +
                        str(month) + '.png')
            plt.close(fig)
            progbar.update(pcount)
            if month == 12:
                month = 1
                year = year + 1
            else:
                month = month + 1
            if year == 8 and month == 4:
                done = True
                #print('')
    """
    If not created, create "random" graphs of single days
    This can help see the flow of a day
    """

    years = mdates.YearLocator()  # every year
    months = mdates.MonthLocator()  # every month
    hours = mdates.HourLocator()  # every hour
    hoursM = mdates.HourLocator(interval=3)  # every 3rd hour
    yearsFmt = mdates.DateFormatter('%d')
    hoursFmt = mdates.DateFormatter('%H:%M')

    if not os.listdir(GraphPath + 'Daily'):
        progbar = ProgBar.Progbar(target=toCreate,
                                  newline_on_end=False,
                                  text_description='Creating daily graphs: ')
        #widgets = ['Creating daily share graph: ', progressbar.Percentage(),
        #           ' ',progressbar.Bar(marker='∎',left='[',right=']'),
        #           ' ', progressbar.AdaptiveETA()]
        #pbar = progressbar.ProgressBar(widgets=widgets, maxval=150)
        #pbar.start()
        #print('')
        #print('Creating Daily share graph [%]')
        for count in range(0, DayGraphs):
            #if count % 5 == 0:
            #    print('\u220E', end='', flush=True)

            #pbar.update(count+1)
            pcount += 1
            year = np.random.randint(3, 7 + 1)
            if year == 3:
                month = np.random.randint(3, 12 + 1)
            elif year == 8:
                month = np.random.randint(1, 3 + 1)
            else:
                month = np.random.randint(1, 12 + 1)
            if year == 3 and month == 3:
                day = np.random.randint(5, 31 + 1)
            elif month == 2:
                day = np.random.randint(1, 28 + 1)
            elif month == 4 or month == 6 or month == 9 or month == 11:
                day = np.random.randint(1, 30 + 1)
            else:
                day = np.random.randint(1, 31 + 1)

            StartPoint = Data.index.get_loc('201' + str(year) + '-' +
                                            str(month) + '-' + str(day) +
                                            ' 00:00',
                                            method='nearest')
            EndPoint = Data['201' + str(year) + '-' + str(month) + '-' +
                            str(day)].shape[0]
            fig, ax = plt.subplots()
            ax.plot(
                Data.index[StartPoint:StartPoint + EndPoint],
                Data.Diesel['201' + str(year) + '-' + str(month) + '-' +
                            str(day)])
            # format the ticks
            ax.xaxis.set_major_locator(hoursM)
            ax.xaxis.set_major_formatter(hoursFmt)
            ax.xaxis.set_minor_locator(hours)
            # round to nearest years...
            datemin = np.datetime64(Data.index[StartPoint], 'm')
            datemax = np.datetime64(Data.index[StartPoint + EndPoint - 1], 'm')
            # + np.timedelta64(1, 'D')
            ax.set_xlim(datemin, datemax)
            ax.grid(which='major', alpha=1, color='black')
            ax.grid(which='minor', alpha=0.05, color='black')
            fig.autofmt_xdate()
            Diesel = Data.Diesel['201' + str(year) + '-' + str(month) + ' - ' +
                                 str(day)]
            Water = Data.Water['201' + str(year) + '-' + str(month) + ' - ' +
                               str(day)] + Diesel
            Wind = Data.Wind['201' + str(year) + '-' + str(month) + ' - ' +
                             str(day)] + Water
            #plt.plot(Diesel,color=Color1)
            plt.plot(Water, color=Color2)
            plt.plot(Wind, color=Color3)
            plt.fill_between(Wind.index,
                             Wind,
                             Water,
                             facecolor=Color3,
                             alpha=1)
            plt.fill_between(Water.index,
                             Water,
                             Diesel,
                             facecolor=Color2,
                             alpha=1)
            plt.fill_between(Diesel.index,
                             Diesel,
                             0,
                             facecolor=Color1,
                             alpha=1)
            plt.title('201' + str(year) + ' - ' + monthDict[month] + ' - ' +
                      str(day))
            plt.ylim([-1, 105])
            plt.xlabel('Time [date]')
            plt.ylabel('Total production [%]')
            plt.legend(['Diesel', 'Water', 'Wind'])

            plt.savefig(GraphPath + 'Daily/' + 'Share201' + str(year) + '-' +
                        str(month) + '-' + str(day) + '.png')
            plt.close(fig)
            progbar.update(pcount)
        #pbar.finish()
    progbar = ProgBar.Progbar(target=toCreate,
                              newline_on_end=False,
                              text_description='Done: ')
    if toCreate > 0:
        progbar.update(pcount)