Ejemplo n.º 1
0
    def getForecast(self, tseries, units, algorithm, file, default="year"):
        highest = 0.0
        lowest = 0.0
        billdata = {"sum": 0.0, "avg": 0.0, "high": "", "low": ""}
        monthdata = {"sum": 0.0, "avg": 0.0, "high": "", "low": ""}
        dtlist = []
        list2 = []
        list1 = []
        list3 = []
        num = 0.0
        dtlist2 = []
        data = {}
        tserieslength = 0
        tseriesend = None
        hhold = ihousehold()
        series = iseries()
        quota = 18

        enddate = series.getfinaldate(tseries)  #end date of the time series
        #file = self.ts.getyearlyArff()

        if algorithm == "mlp":
            result = self.ts.getForecast(
                file, units, self.ts.getMLP(),
                "cost")  #execute forecast using MLP for 12 units in future
        elif algorithm == "lr":
            result = self.ts.getForecast(
                file, units, self.ts.getLinear(),
                "cost")  #execute forecast using MLP for 12 units in future
        elif algorithm == "gr":
            result = self.ts.getForecast(
                file, units, self.ts.getGaussian(),
                "cost")  #execute forecast using MLP for 12 units in future
        else:
            return None

        list2 = result.split(',')  #get result in list

        if default == "days":
            for x in range(0, units):
                dt = str(enddate + datetime.timedelta(days=(x + 1)))
                dtlist.append(dt)

        else:
            #get all future dates in list
            for x in range(0, units):
                dt = str(iutility.addMonths(enddate, x + 1))
                dtlist.append(dt)
                cost = round(hhold.tariff1(float(list2[x])), 2)
                list1.append(cost)
                billdata["sum"] = billdata["sum"] + cost
                #list1.append(iutility.getDate(dt,'%Y-%m-%d','%d-%m-%Y')) #covert format from YYY-mm-dd to dd-mm-YYYY

        highest = max(list1)
        lowest = min(list1)
        billdata["sum"] = round(billdata["sum"], 2)
        billdata["avg"] = round(billdata["sum"] / units, 2)
        billdata["high"] = {
            "date":
            iutility.convertdate(dtlist[list1.index(highest)], '%Y-%m-%d',
                                 '%B-%Y'),
            "max":
            highest
        }  #max per nonth
        billdata["low"] = {
            "date":
            iutility.convertdate(dtlist[list1.index(lowest)], '%Y-%m-%d',
                                 '%B-%Y'),
            "min":
            lowest
        }  #min per nonth

        data["data"] = series.getlistTojsoncost(
            dtlist, list1, "Date",
            "Cost")  #merge two list for passing to client for charting
        data["billdata"] = billdata
        data["title"] = "NEXT " + str(units) + " MONTHS BILL FORECAST"
        '''
        price break lines
        '''
        start = dtlist[0]
        end = dtlist[units - 1]
        data["price_break"] = [{
            "Price Break": "Highest",
            "Cost": highest,
            "Date": start
        }, {
            "Price Break": "Highest",
            "Cost": highest,
            "Date": end
        }, {
            "Price Break": "Average",
            "Cost": billdata["avg"],
            "Date": start
        }, {
            "Price Break": "Average",
            "Cost": billdata["avg"],
            "Date": end
        }, {
            "Price Break": "Lowest",
            "Cost": lowest,
            "Date": start
        }, {
            "Price Break": "Lowest",
            "Cost": lowest,
            "Date": end
        }]
        '''
        This is the data for the last "units" months to display with forecasting summary in use case 5.4
        '''
        tserieslength = len(tseries)
        if tserieslength >= units:
            tseriesend = tseries[tserieslength - units:]

        for d in tseriesend:
            dtlist2.append(str(d[0].date()))
            num = float(d[1])
            if math.isnan(num):
                num = 0.0

            num = iutility.percentage(num, quota)
            cost = round(hhold.tariff1(num), 2)
            list3.append(cost)
            monthdata["sum"] = monthdata["sum"] + cost

        highest = max(list3)
        lowest = min(list3)
        monthdata["sum"] = round(monthdata["sum"], 2)
        monthdata["avg"] = round(monthdata["sum"] / units, 2)
        monthdata["high"] = {
            "date":
            iutility.convertdate(dtlist2[list3.index(highest)], '%Y-%m-%d',
                                 '%B-%Y'),
            "max":
            highest
        }  #max per nonth
        monthdata["low"] = {
            "date":
            iutility.convertdate(dtlist2[list3.index(lowest)], '%Y-%m-%d',
                                 '%B-%Y'),
            "min":
            lowest
        }  #min per nonth

        data["monthdata"] = monthdata
        try:
            ""  #entry.shutGateway()
        except:
            pass

        #data=""
        return data
Ejemplo n.º 2
0
    def usecase3_4(self):
        '''
        Calculating household monthly cost and other statistics
        '''
        hhold = ihousehold()
        hholdstats = hhold.getUsage(self.User)
        household = self.User.households.all()[0]
        dma = household.dma  # dma to calculate user with highest cost
        household_dma = dma.households.filter(
            num_of_occupants=household.num_of_occupants,
            property_type=household.property_type)
        d = idma()
        series = iseries()
        obj = {}
        list1 = []
        data = {}
        effuser = None
        #effdata = {"year":"","half":"","quarter":"","month":""}
        yourdata = {"year": "", "half": "", "quarter": "", "month": ""}
        effuser = d.getMostefficient(household_dma, 12)
        if effuser:
            effdata = effuser["stats"]
            for x in range(0, len(effuser["chartdata"])):
                list1.append(effuser["chartdata"][x])

            for x in range(0, 4):
                if x == 0:
                    period = 12
                    cal = "year"
                elif x == 1:
                    period = 6
                    cal = "half"
                elif x == 2:
                    period = 3
                    cal = "quarter"
                elif x == 3:
                    period = 1
                    cal = "month"

                obj = {}
                dic = {}
                dic = hholdstats[cal]
                yourdata[cal] = dic
                #print dic
                obj["Units"] = str(dic["sum_units"])
                obj["Cost"] = round(hhold.tariff1(dic["sum_units"]), 2)
                #obj["Cost"]  = str(series.getCost(dic["sum_units"]))
                obj["Period"] = str(period) + " Month"
                obj["Data"] = "You"
                dic = {}
                list1.append(obj)
                obj = {}

            data["chart"] = list1
            data["effdata"] = effdata
            data["yourdata"] = yourdata
            # Added by Chris Pantazis to send the currency to the graphs
            # see: uc32barplot1 javascript function in base.js
            if self.User.username.startswith("GB"):
                data["currency"] = u"£"
            else:
                data["currency"] = u"€"
        else:
            data = None

        return data
Ejemplo n.º 3
0
    def usecase5_4(self):
        series = iseries()
        hhold = ihousehold()
        household = self.User.households.all()[0]
        tsmonthly = series.readseries(
            series.getmonthlyseries(household))  #get timeseries
        quota = 18
        num = 0.0
        try:
            forecast = ElectricForecast.objects.get(
                user=self.User)  #get the current user forecast model object
            if not forecast.yearfile:
                if len(tsmonthly) > 0:
                    gateway = JavaGateway()  #connect to JVM
                    entry = gateway.entry_point
                    ts = entry.getTimeSeries(
                        str(self.User.id
                            ))  #get JAVA timeseries Object for this user
                    path = ts.getelectricArff()
                    '''
                    The method runs in a separate JAVA thread so it will not block the application, however if the thread runs for very long time  
                    for file writing and Iforecast class request the forecasting athen code needs to be block until the thread completes writing the file. 
                    '''
                    ts.writelectricArff(
                        json.dumps(
                            series.getseriesToelectricjson(
                                tsmonthly, quota, "cost")), path)
                    '''
                    End of thread
                    '''
                    #write path to db
                    forecast.yearfile = path
                    forecast.yeardate = series.getfinaldate(tsmonthly)
                    forecast.save()
            else:
                #if newer data then append that month to file
                seriesdate = series.getfinaldate(tsmonthly)
                dbdate = forecast.yeardate
                if seriesdate > dbdate:
                    diff = iutility.diffmonth(
                        seriesdate, dbdate
                    )  #get the mumber of months to append series to file
                    list = tsmonthly[len(tsmonthly) - diff:]
                    line = ""
                    for i in list:
                        num = float(i[1])
                        if math.isnan(num):
                            num = 0.0

                        if num > 0.0:
                            num = iutility.percentage(num, quota)

                        #print (tseries[len(tseries)-1:][0][0]).date()
                        line = line + str(i[0].date()) + "," + str(num) + "\n"

                    f = ifile(forecast.yearfile, "a")
                    f.write(line)
                    f.close()
                    forecast.yeardate = seriesdate
                    forecast.save()

        except:  #exception result if no record exists
            gateway = JavaGateway()  #connect to JVM
            entry = gateway.entry_point
            ts = entry.getTimeSeries(str(
                self.User.id))  #get JAVA timeseries Object for this user
            # get_or_create: Added by Chris Pantazis because sometimes
            # two forecast keys are made for a user
            forecast, created = ElectricForecast.objects.get_or_create(
                user=self.User)
            #ts.safeThread();
            #entry.shutGateway()
            if len(tsmonthly) > 0:
                path = ts.getelectricArff()
                '''
                The method runs in a separate JAVA thread so it will not block the application, however if the thread runs for very long time  
                for file writing and Iforecast class request the forecasting athen code needs to be block until the thread completes writing the file. 
                '''
                ts.writelectricArff(
                    json.dumps(
                        series.getseriesToelectricjson(tsmonthly, quota,
                                                       "cost")), path)
                '''
                End of thread
                '''
                #write path to db
                forecast.yearfile = path
                forecast.yeardate = series.getfinaldate(tsmonthly)
                forecast.save()

        return
Ejemplo n.º 4
0
    def usecase3_2(self):
        data = {}
        household = self.User.households.all()[0]
        dma = household.dma
        series = iseries()
        d = idma()
        '''
        start of all user statistics in DMA
        '''
        #dict = {"sum_households":"","avg_occupants":""}
        #dict["sum_households"] = Household.objects.filter(dma__pk=dma.pk).count() #total household
        #dict["avg_occupants"]  = Household.objects.filter(dma__pk=dma.pk).aggregate(Sum("num_of_occupants"))
        #dict["avg_occupants"]  = dict["avg_occupants"]["num_of_occupants__sum"]/dict["sum_households"]
        #print dict

        household_dma = dma.households.all(
        )  #get all households in logged user DMA
        '''#start
        dmastats = DMAstats.objects.filter(dma__pk=dma.pk)            
        
        #dmasummary = d.getStats(household_dma,0)
        #print dict
        if not dmastats: #if no statistics ever computed before for this DMA
            self.__savedbdmastats(household_dma,dma,12)
            self.__savedbdmastats(household_dma,dma,6)
            self.__savedbdmastats(household_dma,dma,4)
            self.__savedbdmastats(household_dma,dma,1)           
        else: #if stats exists then check it is updated in the last 24 hours
            for st in dmastats:
                if datetime.date.today() > st.statsdate:
                    stats = d.getStats(household_dma,st.statsperiod)
                    st.statsdate = datetime.date.today()
                    st.sumhouseholds=stats["sum_households"]
                    st.sumoccupants=stats["sum_occupants"]
                    st.sumunits=stats["sum_units"]
                    st.maxoccupants=stats["max_occupants"]
                    st.avgoccupants=stats["avg_occupants"]
                    st.minoccupants=stats["min_occupants"]
                    st.maxunits=stats["max_units"]
                    st.avgunits=stats["avg_units"]
                    st.minunits=stats["min_units"]
                    st.dma=dma
                    st.save();
        '''#end
        '''
        End of all user statistics in DMA
        '''
        '''
        Start of DMA user statistics based on filter information
        1) Based on number of occupants 
        '''
        '''#start                    
        dmastats  = userDMAstats.objects.filter(household=household)
        #household_dma = dma.households.exclude(user=self.User) #exclude logged user from DMA
                 
        if not dmastats:
            household_dma = dma.households.exclude(user=self.User) #exclude logged user from DMA
            household_dma = household_dma.filter(num_of_occupants=household.num_of_occupants) #get households with the same of household as like logged user            
            self.__savedbuserdmastats(household_dma,12,1)
            self.__savedbuserdmastats(household_dma,6,1)
            self.__savedbuserdmastats(household_dma,4,1)
            self.__savedbuserdmastats(household_dma,1,1)

            household_dma = dma.households.exclude(user=self.User) #exclude logged user from DMA
            household_dma = household_dma.filter(property_type=household.property_type) #get households with the same of household as like logged user            
            self.__savedbuserdmastats(household_dma,12,2)
            self.__savedbuserdmastats(household_dma,6,2)
            self.__savedbuserdmastats(household_dma,4,2)
            self.__savedbuserdmastats(household_dma,1,2)

            household_dma = dma.households.exclude(user=self.User) #exclude logged user from DMA
            household_dma = household_dma.filter(num_of_occupants=household.num_of_occupants,property_type=household.property_type) #get households with the same of household as like logged user            
            self.__savedbuserdmastats(household_dma,12,3)
            self.__savedbuserdmastats(household_dma,6,3)
            self.__savedbuserdmastats(household_dma,4,3)
            self.__savedbuserdmastats(household_dma,1,3)
        else: #if stats exists then check it is updated in the last 24 hours
            for st in dmastats:
                if datetime.date.today() > st.statsdate:
                    stats = d.gethouseholdstats(household_dma,st.statsperiod)
                    st.statsdate = datetime.date.today()
                    st.sumhouseholds=stats["sum_households"]
                    st.sumoccupants=stats["sum_occupants"]
                    st.sumunits=stats["sum_units"]
                    st.maxunits=stats["max_units"]
                    st.avgunits=stats["avg_units"]
                    st.minunits=stats["min_units"]
                    st.household=household
                    st.save();
        '''#end
        hhold = ihousehold()
        hholdstats = hhold.getUsage(self.User)
        #print hholdstats
        #household = self.User.households.all()[0]
        #dma = DMA.objects.get(pk=10) #chosing other DMA for comparison, we will change this when we will get the different demographic database
        #household_dma = dma.households.all()
        #d = idma()
        #series = iseries()
        obj = {}
        list1 = []
        areadata = {"year": "", "half": "", "quarter": "", "month": ""}
        yourdata = {"year": "", "half": "", "quarter": "", "month": ""}
        #getting consumer with same socio-demographic status for the average unit/bill
        for x in range(0, 4):
            if x == 0:
                period = 12
                cal = "year"
            elif x == 1:
                period = 6
                cal = "half"
            elif x == 2:
                period = 3
                cal = "quarter"
            elif x == 3:
                period = 1
                cal = "month"

            dic = d.getStats(household_dma, period)
            areadata[cal] = dic
            obj["Units"] = dic["avg_units"]
            obj["Cost"] = round(hhold.tariff1(dic["avg_units"]), 2)
            #obj["Cost"]  = str(series.getCost(dic["avg_units"]))
            obj["Period"] = str(period) + " Month"
            obj["Data"] = "Area"
            list1.append(obj)
            obj = {}
            dic = {}
            dic = hholdstats[cal]
            yourdata[cal] = dic
            #print dic
            obj["Units"] = str(dic["sum_units"])
            obj["Cost"] = round(hhold.tariff1(dic["sum_units"]), 2)
            #obj["Cost"]  = str(series.getCost(dic["sum_units"]))
            obj["Period"] = str(period) + " Month"
            obj["Data"] = "You"

            dic = {}
            list1.append(obj)
            obj = {}

        data = {}
        data["chart"] = list1
        data["areadata"] = areadata
        data["yourdata"] = yourdata
        # Added by Chris Pantazis to send the currency to the graphs
        # see: uc32barplot1 javascript function in base.js
        if self.User.username.startswith("GB"):
            data["currency"] = u"£"
        else:
            data["currency"] = u"€"

        return data
Ejemplo n.º 5
0
    def usecase5_2(self):
        '''
        Declare data
        '''
        dtlist = []
        list1 = []
        tariff1data = {"sum": 0.0, "avg": 0.0, "high": 0.0, "low": 0.0}
        tariff2data = {"sum": 0.0, "avg": 0.0, "high": 0.0, "low": 0.0}
        comparechart = [{
            "Cost": "",
            "Data": "Tariff1"
        }, {
            "Cost": "",
            "Data": "Tariff2"
        }]
        stdate = ""
        endate = ""
        data = {}

        household = self.User.households.all()[0]
        hhold = ihousehold()
        series = iseries()
        ts_monthly = series.readseries(
            series.getmonthlyseries(household))  #get timeseries

        if len(ts_monthly) >= 2:  #at least 2 months data
            dates, units = IT.izip(
                *ts_monthly)  #much better for longer data, returning tuples
            pdf = pd.DataFrame(
                list(units), index=list(dates), columns=["units"]
            )  #create pandas Series (time series using two different list for timeseries data analysis
            pdf.index.name = "dates"

            #list of dates (chosen the latest two months of the time series)
            stdate = str(dates[len(dates) - 2].date())
            endate = str(dates[len(dates) - 1].date())

            dtlist.append(stdate)
            dtlist.append(endate)

            list1.append(round(hhold.tariff1(units[len(units) - 2]), 2))
            list1.append(round(hhold.tariff1(units[len(units) - 1]), 2))
            data["tariff1"] = series.getlistTojson(dtlist, list1, "Date",
                                                   "Cost")

            tariff1data["sum"] = list1[0] + list1[
                1]  # sum over month for this tariff
            tariff1data["avg"] = round(tariff1data["sum"] / len(list1),
                                       2)  #average per nonth
            tariff1data["high"] = {
                "date":
                iutility.convertdate(dtlist[list1.index(max(list1))],
                                     '%Y-%m-%d', '%B-%Y'),
                "max":
                max(list1)
            }  #max per nonth
            tariff1data["low"] = {
                "date":
                iutility.convertdate(dtlist[list1.index(min(list1))],
                                     '%Y-%m-%d', '%B-%Y'),
                "min":
                min(list1)
            }  #min per nonth
            data["tariff1data"] = tariff1data
            comparechart[0]["Cost"] = tariff1data["sum"]

            list1 = []
            list1.append(round(hhold.tariff2(units[len(units) - 2]), 2))
            list1.append(round(hhold.tariff2(units[len(units) - 1]), 2))
            data["tariff2"] = series.getlistTojson(dtlist, list1, "Date",
                                                   "Cost")

            tariff2data["sum"] = list1[0] + list1[
                1]  # sum over month for this tariff
            tariff2data["avg"] = round(tariff2data["sum"] / len(list1),
                                       2)  #average per nonth
            tariff2data["high"] = {
                "date":
                iutility.convertdate(dtlist[list1.index(max(list1))],
                                     '%Y-%m-%d', '%B-%Y'),
                "max":
                max(list1)
            }  #max per nonth
            tariff2data["low"] = {
                "date":
                iutility.convertdate(dtlist[list1.index(min(list1))],
                                     '%Y-%m-%d', '%B-%Y'),
                "min":
                min(list1)
            }  #min per nonth
            data["tariff2data"] = tariff2data
            comparechart[1]["Cost"] = tariff2data["sum"]
            data["comparechart"] = comparechart
            '''
            add title to be displayed on top of graph
            '''
            #convert date from to easily read at client side (eg. December 2009
            stdate = iutility.convertdate(str(stdate), '%Y-%m-%d', '%B-%Y')
            endate = iutility.convertdate(str(endate), '%Y-%m-%d', '%B-%Y')
            data["title"] = _("TARIFF COMPARISON FROM ") + stdate + _(
                " TO ") + endate

            if self.User.username.startswith("GB"):
                data["currency"] = u"£"
            else:
                data["currency"] = u"€"
        else:
            data = None

        #print data
        return data
        '''
Ejemplo n.º 6
0
    def usecase3_3(self):
        '''
        Calculating household monthly cost and other statistics
        '''
        hhold = ihousehold()
        hholdstats = hhold.getUsage(self.User)
        #print hholdstats
        household = self.User.households.all()[0]
        # dma_id added by Chris Pantazis. Used to be int(3). Don't know why.
        dma_id = household.dma.id
        dma = DMA.objects.get(
            pk=dma_id
        )  #chosing other DMA for comparison, we will change this when we will get the different demographic database
        household_dma = dma.households.all()
        d = idma()
        series = iseries()
        obj = {}
        list1 = []
        areadata = {"year": "", "half": "", "quarter": "", "month": ""}
        yourdata = {"year": "", "half": "", "quarter": "", "month": ""}
        #getting consumer with same socio-demographic status for the average unit/bill
        for x in range(0, 4):
            if x == 0:
                period = 12
                cal = "year"
            elif x == 1:
                period = 6
                cal = "half"
            elif x == 2:
                period = 3
                cal = "quarter"
            elif x == 3:
                period = 1
                cal = "month"

            dic = d.getStats(household_dma, period)
            areadata[cal] = dic
            obj["Units"] = dic["avg_units"]
            obj["Cost"] = round(hhold.tariff1(dic["avg_units"]), 2)
            #obj["Cost"]  = str(series.getCost(dic["avg_units"]))
            obj["Period"] = str(period) + " Month"
            obj["Data"] = "Area"
            list1.append(obj)
            obj = {}
            dic = {}
            dic = hholdstats[cal]
            yourdata[cal] = dic
            #print dic
            obj["Units"] = str(dic["sum_units"])
            obj["Cost"] = round(hhold.tariff1(dic["sum_units"]), 2)
            #obj["Cost"]  = str(series.getCost(dic["sum_units"]))
            obj["Period"] = str(period) + " Month"
            obj["Data"] = "You"
            dic = {}
            list1.append(obj)
            obj = {}

        data = {}
        data["chart"] = list1
        data["areadata"] = areadata
        data["yourdata"] = yourdata
        # Added by Chris Pantazis to send the currency to the graphs
        # see: uc32barplot1 javascript function in base.js
        if self.User.username.startswith("GB"):
            data["currency"] = u"£"
        else:
            data["currency"] = u"€"

        return data