From 0444a30cb5acda056c21b0697ed49444f89ceca0 Mon Sep 17 00:00:00 2001 From: chenzheng06 Date: Thu, 13 Apr 2017 22:17:38 -0400 Subject: [PATCH 1/7] why can't I commit? --- bpfin/lib/date.py | 0 bpfin/lib/math.py | 0 bpfin/lib/other.py | 0 .../{ => test_financials}/test_prelim.py | 0 bpfin/tests/test_utilbills.py | 182 ---------- bpfin/tests/test_utilbills/test_bill_lib.py | 12 + .../test_utilbills/test_bill_normalize.py | 0 .../test_utilbills/test_bill_proj_charge.py | 0 .../test_utilbills/test_bill_proj_reg.py | 0 bpfin/utilbills/bill_lib.py | 175 ++++++++++ bpfin/utilbills/bill_normalize.py | 110 ++++++ bpfin/utilbills/bill_proj_charge.py | 166 +++++++++ bpfin/utilbills/bill_proj_reg.py | 98 ++++++ bpfin/utilbills/billprojections.py | 323 ------------------ bpfin/utilbills/calcbill.py | 199 ----------- 15 files changed, 561 insertions(+), 704 deletions(-) create mode 100644 bpfin/lib/date.py create mode 100644 bpfin/lib/math.py create mode 100644 bpfin/lib/other.py rename bpfin/tests/{ => test_financials}/test_prelim.py (100%) delete mode 100644 bpfin/tests/test_utilbills.py create mode 100644 bpfin/tests/test_utilbills/test_bill_lib.py create mode 100644 bpfin/tests/test_utilbills/test_bill_normalize.py create mode 100644 bpfin/tests/test_utilbills/test_bill_proj_charge.py create mode 100644 bpfin/tests/test_utilbills/test_bill_proj_reg.py create mode 100644 bpfin/utilbills/bill_lib.py create mode 100644 bpfin/utilbills/bill_normalize.py create mode 100644 bpfin/utilbills/bill_proj_charge.py create mode 100644 bpfin/utilbills/bill_proj_reg.py delete mode 100644 bpfin/utilbills/billprojections.py delete mode 100644 bpfin/utilbills/calcbill.py diff --git a/bpfin/lib/date.py b/bpfin/lib/date.py new file mode 100644 index 0000000..e69de29 diff --git a/bpfin/lib/math.py b/bpfin/lib/math.py new file mode 100644 index 0000000..e69de29 diff --git a/bpfin/lib/other.py b/bpfin/lib/other.py new file mode 100644 index 0000000..e69de29 diff --git a/bpfin/tests/test_prelim.py b/bpfin/tests/test_financials/test_prelim.py similarity index 100% rename from bpfin/tests/test_prelim.py rename to bpfin/tests/test_financials/test_prelim.py diff --git a/bpfin/tests/test_utilbills.py b/bpfin/tests/test_utilbills.py deleted file mode 100644 index 60b360f..0000000 --- a/bpfin/tests/test_utilbills.py +++ /dev/null @@ -1,182 +0,0 @@ -import os -import pandas as pd -import datetime -from bpfin.utilbills.calcbill import calculate_utility_bill - -BASE_DIR = os.path.dirname(os.path.abspath(__file__)) - -excel_master = pd.ExcelFile(os.path.join(BASE_DIR, 'testdata/Energy_Bill_Model_Python_WorkSheet_v1.2.xlsx')) - -customer_financials = excel_master.parse("Customer_Financials") -cash_flow = excel_master.parse("Project_Cash_Flow") -loan_information = excel_master.parse("Loan_Information") -utility_bill_w_savings = excel_master.parse("Utility_Bills_w_Savings") -utility_bill_no_savings = excel_master.parse("Utility_Bill_No_Saving") -project_economics = excel_master.parse("Project_Economics") -ecm_savings = excel_master.parse("ECM_Savings") -hist_el = excel_master.parse("Historical_Electricity_Bill") -split_el = excel_master.parse("Split_Electricity_Bill") -hist_gas = excel_master.parse("Historical_Gas_Bill") -split_gas = excel_master.parse("Split_Gas_Bill") -hist_oil = excel_master.parse("Historical_Oil_Bill") -split_oil = excel_master.parse("Split_Oil_Bill") -gas_rate_plan = excel_master.parse("Gas_Rate_Plans") -el_rate_plan = excel_master.parse("Electricity_Rate_Plans") -ecm_eng_info = excel_master.parse("Sheet1") -cdd_data = excel_master.parse("CDD") -hdd_data = excel_master.parse("HDD") -inflation_sheet = excel_master.parse("Inflation") - -# Engineering Input: Utility Use w/ Savings -bill_period_start_w_savings = utility_bill_w_savings["From Date"] -bill_period_end_w_savings = utility_bill_w_savings["End Date"] - -# Engineering Input: Utility Use w/o Savings -bd_start_wo_savings = utility_bill_no_savings["From Date"] -bd_end_wo_savings = utility_bill_no_savings["End Date"] - -# Project Economics - -# ECMSavings -ecm_electricity = ecm_savings["Savings on Electricity, kwh"] -ecm_gas = ecm_savings["Savings on Gas, mmBTU"] -ecm_oil = ecm_savings["Savings on Oil, mmBTU"] -ecm_id = ecm_savings["ECM ID"] - -# Historical Electricity Bill -bd_start_el = hist_el['From Date'] -bd_end_el = hist_el['To Date'] -el_usage = hist_el['Electric Usage (kWh)'] -el_charge = hist_el['Total Electric Charge'] - -# Split Electricity Bill -split_start_date_el = split_el["From Date"] -split_end_date_el = split_el["To Date"] -heat_el_use = split_el["Heat Electricity Use"] -cool_el_use = split_el["Cooling Electricity Use"] -light_el_use = split_el["Lighting Electricity Use"] -other_el_use = split_el["Other Use"] - -# Historical Gas Bill -bd_start_gas = hist_gas['From Date'] -bd_end_gas = hist_gas['To Date'] -gas_usage = hist_gas['Gas Usage (mmBTU)'] -gas_charge = hist_gas['Total Gas Charge'] - -# Split Gas Bill -split_start_date_gas = split_gas["From Date"] -split_end_date_gas = split_gas["To Date"] -heat_gas_use = split_gas["Heat Gas Use"] -cool_gas_use = split_gas["Cooling Gas Use"] -dhw_gas_use = split_gas["DHW Gas Use"] -other_gas_use = split_gas["Other Use"] - -# Historical Oil Bill -bd_start_oil = hist_oil["From Date"] -bd_end_oil = hist_oil["To Date"] -oil_charge = hist_oil["Total oil charge"] -oil_purchase = hist_oil["Oil Purchase (mmBTU)"] - -# Split Oil Bill -split_start_date_oil = split_oil["From Date"] -split_end_date_oil = split_oil["To Date"] -heat_oil_use = split_oil["Heat Oil Usage"] -dhw_oil_use = split_oil["DHW Oil Use"] -other_oil_use = split_oil["Other Oil Use"] - -# ECM Engineering Data -el_eng_from_date = ecm_eng_info["El From date"] -el_eng_to_date = ecm_eng_info["El To date"] -el_lighting_ecm_a_kwh = ecm_eng_info["Lighting Electricity ECM A kWh Savings"] -el_lighting_ecm_a_dollar = ecm_eng_info["Lighting Electricity ECM A Dollar Savings"] -ecm_el_total_kwh = ecm_eng_info["Total kWh Savings Electricity"] -ecm_el_total_dollar = ecm_eng_info["Total Dollar Savings Electricity"] -gas_eng_from_date = ecm_eng_info["Gas From date"] -gas_eng_to_date = ecm_eng_info["Gas To date"] -gas_dhw_ecm_a_mmbtu = ecm_eng_info["DHW Gas ECM A mmBTU Savings"] -gas_dhw_ecm_a_dollar = ecm_eng_info["DHW Gas ECM A Dollar Savings"] -gas_heating_ecm_b_mmbtu = ecm_eng_info["Heating Gas ECM B mmBTU Savings"] -gas_heating_ecm_b_dollar = ecm_eng_info["Heating Gas ECM B Dollar Savings"] -gas_heating_ecm_c_mmbtu = ecm_eng_info["Heating Gas ECM C mmBTU Savings"] -gas_heating_ecm_c_dollar = ecm_eng_info["Heating Gas ECM C Dollar Savings"] -gas_heating_ecm_d_mmbtu = ecm_eng_info["Heating Gas ECM D mmBTU Savings"] -gas_heating_ecm_d_dollar = ecm_eng_info["Heating Gas ECM D Dollar Savings"] -ecm_gas_total_mmbtu = ecm_eng_info["Total mmBTU Savings Gas"] -ecm_gas_total_dollar = ecm_eng_info["Total Dollar Savings Gas"] -oil_eng_from_date = ecm_eng_info["Oil From date"] -oil_eng_to_date = ecm_eng_info["Oil To date"] -oil_heating_ecm_a_mmbtu = ecm_eng_info["Heating Oil ECM A mmBTU Savings"] -oil_heating_ecm_a_dollar = ecm_eng_info["Heating Oil ECM A Dollar Savings"] -oil_dhw_ecm_b_mmbtu = ecm_eng_info["DHW Oil ECM B mmBTU Savings"] -oil_dhw_ecm_b_dollar = ecm_eng_info["DHW Oil ECM B Dollar Savings"] -ecm_oil_total_mmbtu = ecm_eng_info["Total mmBTU Savings Oil"] -ecm_oil_total_dollar = ecm_eng_info["Total Dollar Savings Oil"] - -# Months -year = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] - -# RatePlans -service_classification_el = el_rate_plan["Service Classification"] -service_classification_gas = gas_rate_plan["Service Classification"] - -basic_charge_el = el_rate_plan["Delivery basic charge"] -uplimit_1_el = el_rate_plan["Upper Limit 1"] -leap_1_el = el_rate_plan["Leap 1"] -uplimit_2_el = el_rate_plan["Upper Limit 2"] -leap_2_el = el_rate_plan["Leap 2"] -uplimit_3_el = el_rate_plan["Upper Limit 3"] -leap_3_el = el_rate_plan["Leap 3"] -uplimit_4_el = el_rate_plan["Upper Limit 4"] -leap_4_el = el_rate_plan["Leap 4"] -supply_charge_el = el_rate_plan["Supply Charge"] -tax_rate_el = el_rate_plan["Tax rate"] -adjustments_el = el_rate_plan["Other/Adjustment"] - -limit_el = [uplimit_1_el, uplimit_2_el, uplimit_3_el, uplimit_4_el] -leap_el = [leap_1_el, leap_2_el, leap_3_el, leap_4_el] -SC_1_el = [limit_el, leap_el] - -basic_charge_gas = gas_rate_plan["Delivery basic charge"] -uplimit_1_gas = gas_rate_plan["Upper Limit 1"] -leap_1_gas = gas_rate_plan["Leap 1"] -uplimit_2_gas = gas_rate_plan["Upper Limit 2"] -leap_2_gas = gas_rate_plan["Leap 2"] -uplimit_3_gas = gas_rate_plan["Upper Limit 3"] -leap_3_gas = gas_rate_plan["Leap 3"] -supply_charge_gas = gas_rate_plan["Supply charge"] -tax_rate_gas = gas_rate_plan["Tax rate"] -adjustments_gas = gas_rate_plan["Other/Adjustment"] - -limit_gas = [uplimit_1_gas, uplimit_2_gas, uplimit_3_gas] -leap_gas = [leap_1_gas, leap_2_gas, leap_3_gas] -SC_1_gas = [limit_gas, leap_gas] - -# HDD and CDD -hdd_date = hdd_data["Date"] -hdd_projected = hdd_data["Projected HDD"] -cdd_date = cdd_data["Date"] -cdd_projected = cdd_data["Projected CDD"] - -# Inflation -inflation_rate_info = inflation_sheet["inflation"] -inflation_year_info = inflation_sheet["inflationyear"] -current_date = datetime.date(2015, 1, 31) - -if __name__ == "__main__": - solve = calculate_utility_bill(inflation_rate_info, inflation_year_info, current_date, - bd_start_el, bd_end_el, el_charge, - bd_start_gas, bd_end_gas, gas_charge, - el_usage, gas_usage, - oil_charge, oil_purchase, - hdd_date, hdd_projected, - cdd_date, cdd_projected, - split_start_date_oil, split_end_date_oil, - basic_charge_el, limit_el, leap_el, - tax_rate_el, adjustments_el, supply_charge_el, - basic_charge_gas, limit_gas, leap_gas, - tax_rate_gas, adjustments_gas, supply_charge_gas, - heat_el_use, cool_el_use, light_el_use, other_el_use, - heat_gas_use, cool_gas_use, dhw_gas_use, other_gas_use, - heat_oil_use, dhw_oil_use, other_oil_use - ) - print(solve) diff --git a/bpfin/tests/test_utilbills/test_bill_lib.py b/bpfin/tests/test_utilbills/test_bill_lib.py new file mode 100644 index 0000000..a5c6aec --- /dev/null +++ b/bpfin/tests/test_utilbills/test_bill_lib.py @@ -0,0 +1,12 @@ +from bpfin.utilbills.0bill_lib import add_list + + +def test_add_list(): + input = [1, 2] + output = [3, 4] + result = add_list(input, 2) + + for i in result: + assert result[i] == output[i] + + diff --git a/bpfin/tests/test_utilbills/test_bill_normalize.py b/bpfin/tests/test_utilbills/test_bill_normalize.py new file mode 100644 index 0000000..e69de29 diff --git a/bpfin/tests/test_utilbills/test_bill_proj_charge.py b/bpfin/tests/test_utilbills/test_bill_proj_charge.py new file mode 100644 index 0000000..e69de29 diff --git a/bpfin/tests/test_utilbills/test_bill_proj_reg.py b/bpfin/tests/test_utilbills/test_bill_proj_reg.py new file mode 100644 index 0000000..e69de29 diff --git a/bpfin/utilbills/bill_lib.py b/bpfin/utilbills/bill_lib.py new file mode 100644 index 0000000..8aa7522 --- /dev/null +++ b/bpfin/utilbills/bill_lib.py @@ -0,0 +1,175 @@ +import statsmodels.api as sm +import datetime +import calendar + + +def add_list(obj_list, number): + """Add a number to each value in a list. + + Args: + obj_list (list): list of numbers + number (int): number value + Returns: + list: numbers + number to each value + """ + return [x + number for x in obj_list] + + +def cal_last_day(obj_year, obj_month): + """Return last day of month given year,month. + + Args: + obj_year (int): year + obj_month (int): month + Returns: + int: last day of month + """ + _, last_day_num = calendar.monthrange(obj_year, obj_month) + return last_day_num + + +def form_bill_calendar(date_start, year_term): + """Return single list value of first date in bill period and last date in bill period. + + Args: + date_start (datetime): date xx/yy/abcd + year_term (int): number of years in bill + Returns: + list: first date in bill period, last date in bill period + + """ + bdstart = [] + bdend = [] + day = date_start + for term in range(12 * year_term): + first = datetime.date(day=1, month=day.month, year=day.year) + bdstart.append(first) + last = datetime.date(day.year, day.month, cal_last_day(day.year, day.month)) + bdend.append(last) + day = last + datetime.timedelta(days=1) + return [bdstart, bdend] + + +def form_date_calendar(date_start, date_end): + """Return list of all calendar dates. + + Args: + date_start (datetime): date xx/yy/abcd + date_end (datetime): date xx/yy/abcd + Returns: + list: all calendar dates, first date is first day of calendar + """ + bdstart = [] + bdend = [] + day = date_start + i = 1 + while day <= date_end: + if i > 9999: + return False + first = datetime.date(day=1, month=day.month, year=day.year) + bdstart.append(first) + last = datetime.date(day.year, day.month, cal_last_day(day.year, day.month)) + bdend.append(last) + day = last + datetime.timedelta(days=1) + i += 1 + return [bdstart, bdend] + + +def form_year_month(target_terms): + """Return list of month,year of datetimes in a list. + + Args: + target_terms (list of datetimes): date xx/yy/abcd + Returns: + list: list of tuples containing year and month + """ + obj_list = [] + for term in target_terms: + obj_list.append((term.year, term.month)) + return obj_list + + +def cal_inflation_product(inflation_rate_dict, present_date): + """Return the inflation value for the current date. + + Args: + inflation_rate_dict (dictionary): dictionary of {date,inflation value} + present_date (datetime): present date + Returns: + integer: inflation rate + + """ + date_start = sorted(inflation_rate_dict)[0] + product = 1 + for term in form_date_calendar(date_start, present_date)[1]: + product = product * inflation_rate_dict[term] + return product + + +def cal_inflated_item(base_list, target_terms, inflation_coeff_dict): + # Deleted "present_day" from inputs. + """Create dictionary of inflated terms. + + Args: + base_list (integer): base value + target_terms (list): list of inflation values + inflation_coeff_dict (dictionary): dictioanry of inflation coefficients with date + Returns: + dictionary: coefficients and dates + """ + new_dict = dict(zip([], [])) + for term in target_terms: + new_dict[term] = base_list[term.month - 1] * inflation_coeff_dict[term] + return [new_dict[term] for term in sorted(new_dict)] + + +def form_inflation_ratedict(inflationyears, inflationhist): + """Create dictionary of {date,inflation}. + + Args: + inflationyears (list): list of years + inflationhist (list): list of inflation values + Returns: + dictionary: {datetime, adjusted inflation} + """ + inflationterms = form_bill_calendar(datetime.date(int(inflationyears[0]), 1, 31), len(inflationyears))[1] + inflation_rate = [] + for year, rate in zip(inflationyears, inflationhist): + for number in range(12): + inflation_rate.append(rate / 12) + inflation_rate = add_list(inflation_rate, 1) + dict1 = dict(zip(inflationterms, inflation_rate)) # return (2016,2,29 : 100.67%) + return dict1 + + +def form_inflation_coeffdict(inflationyears, inflationhist, present_date): + """Create dictionary of date:inflation relative to present date. + + Args: + inflationyears (list): list of years + inflationhist (list): list of inflation values + present_date (datetime): present date + Returns: + dictionary: date:inflation relative to present date + """ + inflation_rate_dict = form_inflation_ratedict(inflationyears, inflationhist) + inflationterms = sorted(inflation_rate_dict) + new_dict = dict(zip([], [])) + for term in inflationterms: + new_dict[term] = cal_inflation_product(inflation_rate_dict, term) / cal_inflation_product(inflation_rate_dict, + present_date) + return new_dict + + +def occupancy_final_list(occupancy_rate_list): + """Return list of occupancy values. + + Args: + occupancy_rate_list (list): list of occupancy integers + Returns: + list: list of occupancy integers * 24, representing occupancy in hours + """ + occupancy_list = [] + for occ in occupancy_rate_list: + occupancy_list.append(occ * 24) + return occupancy_list diff --git a/bpfin/utilbills/bill_normalize.py b/bpfin/utilbills/bill_normalize.py new file mode 100644 index 0000000..e7d365e --- /dev/null +++ b/bpfin/utilbills/bill_normalize.py @@ -0,0 +1,110 @@ +import statsmodels.api as sm +import datetime +import calendar + +def daily_average_use_or_charge(from_date, to_date, total_utility_use_or_charge): + """Calculate average daily use or charge for a utility. + + Args: + from_date (list): list of datetimes + to_date (list): list of datetimes + total_utility_use_or_charge (list): list of charges or usage, integers + Returns: + list: list of average values + """ + fromdate = [] + todate = [] + for date in from_date: + fromdate.append(date) + for date in to_date: + todate.append(date) + interstore = [list(x) for x in zip(fromdate, todate)] + finalstore = [] + for value in interstore: + difference = (value[1] - value[0]) + finalstore.append(difference.days) + + bill_daily_average = [x / y for x, y in zip(total_utility_use_or_charge, finalstore)] + perbill_useorcharge = [] + for number in bill_daily_average: + perbill_useorcharge.append(number) + return perbill_useorcharge + + +def firstmatrix(from_date, to_date): + """Create 2D matrix, list of lists. + + Args: + from_date (list): list of datetimes of "from dates" for bills + to_date (list): list of datetimes of "end dates" for bills + Returns: + list: matrix (list of lists) of # of days in bill periods + """ + range_id = 0 + for date in from_date: + range_id += 1 + monthmatrix = [[0 for i in range(12)] for j in range(range_id)] + for (fromdate, enddate, range_id) in zip(from_date, to_date, range(range_id)): + date = fromdate + datetime.timedelta(1) + while date <= enddate: + for month in range(1, 13): # range(1,13)=[1:12] + if date.month == month: + monthmatrix[range_id][month - 1] += 1 + date = date + datetime.timedelta(1) + return monthmatrix + + +def finalmatrix(matrixchoice): + """Return final normalized matrix. + + Args: + matrixchoice (list): list of lists (matrix) + Returns: + list: list of lists of normalized # of days in each bill + """ + newmatrix = [] + for i in range(len(matrixchoice[0])): + bill = [] + for item in matrixchoice: + bill.append(item[i]) + newmatrix.append(bill) + return newmatrix + + +def normalize_average(matrix_choice): + """Provide ratios necessary for bill averages. + + Args: + matrix_choice (list): list of lists of normalized # of days in each bill + Returns: + list: ratios necessary for normalizing usage/charge per month + """ + newlist = [] + for i in matrix_choice: + newlist.append((sum(list(i)))) + divider = [x / y for x, y in zip(newlist, [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31])] + return divider + + +def monthly_info(billing_info, matrix, divider): + """Generate normalized use/charge based on ratios. + + Args: + billing_info (list): list of usage or charge + matrix (list): list of numbers of day + divider (list): list of ratios + Returns: + list: normalized use/charge based on ratios + """ + monthly_average = [] + for month in range(1, 13): + matrixcolumn = [] + for range_id in range(len(billing_info)): + matrixcolumn.append(matrix[range_id][month - 1]) + value = [daily_avg * (days) for (daily_avg, days) in zip(billing_info, matrixcolumn)] + total = 0 + for number in range(len(value)): + total += value[number] + monthly_average.append(total) + monthly_values = [x / y for x, y in zip(monthly_average, divider)] + return monthly_values diff --git a/bpfin/utilbills/bill_proj_charge.py b/bpfin/utilbills/bill_proj_charge.py new file mode 100644 index 0000000..18b2046 --- /dev/null +++ b/bpfin/utilbills/bill_proj_charge.py @@ -0,0 +1,166 @@ + + +def rate_plan_cons(date, con, limit, leap, supply_charge, basic_charge, tax_rate, adjustments): + """Generate correct charge per consumption. + + Args: + date (datetime): datetime aa/bb/xyzw + con (int): consumption value of utility use + limit (list): list of integer limits + leap (list): list of integer limits + supply_charge (list): supply charge of utility use + basic_charge (list): supply charge of utility use + tax_rate (list): tax rate of utility use + adjustments (list): value adjustments + Returns: + int: utility charge given consumption + """ + temp_list = [] + y = date.month - 1 + for i in range(len(limit)): + if (i + 1) < (len(limit)) and con > limit[i + 1][y]: + first = ((limit[i + 1][y] - limit[i][y]) * leap[i][y]) + temp_list.append(first) + if (i + 1) < (len(limit)) and con < limit[i + 1][y] and con > limit[i][y]: + second = ((con - limit[i][y]) * leap[i][y]) + temp_list.append(second) + if (i + 1) == len(limit): + third = max(((con - limit[len(leap) - 1][y]) * leap[i][y]), 0) + temp_list.append(third) + else: + temp_list.append(0) + result = sum(temp_list) + con * (supply_charge[y]) + basic_charge[y] + final_result = result * (1 + tax_rate[y]) * (1 + adjustments[y]) + + return final_result + + +# This function needs to be worked on +def calc_charge_list(conlist, datelist, limit, leap, supply_charge, basic_charge, tax_rate, adjustments): + """Return charge results of rate plan calculator. + + Args: + conlist (list): list of utiltiy consumption + datelist (list): list of datetimes + limit (list): list of integer limits + leap (list): list of integer limits + supply_charge (list): supply charge of utility use + basic_charge (list): supply charge of utility use + tax_rate (list): tax rate of utility use + adjustments (list): value adjustments + Returns: + list: list of utility charges based on rate plan consumption + """ + charge = [] + for con, date in zip(conlist, datelist): + temp_charge = rate_plan_cons(date, con, limit, leap, supply_charge, basic_charge, tax_rate, adjustments) + charge.append(temp_charge) + return charge + + +def price_x_consumption(price, con_list): + """Return list of charges. + + Args: + price (list): list of prices + con_list (list): list of consumption + Returns: + list: list of charges + """ + return [x * price for x in con_list] + + +def utility_charge_sum(list1, list2, list3): + """Return sum of utility lists. + + Args: + list1 (list): list of utility charges + list2 (list): list of utility charges + list3 (list): list of utility charges + Returns: + list: sum of the three lists + """ + return [x + y + z for x, y, z in zip(list1, list2, list3)] + + +def charge_dictionary(billperiod, charge_list): + """Create dictioanry of date and charge. + + Args: + billperiod (datetime): datetime + charge_list (list): list of charges + Returns: + dictionary: ({month, year}, charge) + """ + bdmonth = [] + bdyear = [] + + for i in billperiod: + bdmonth.append(i.month) + for i in billperiod: + bdyear.append(i.year) + + dicthelper = {key: value for key, value in zip(billperiod, charge_list)} + + return dicthelper + + +def charge_with_inflation(billperiod, bill_dictionary, inf_dictionary): + """Create dictionary of inflation adjusted bills with date. + + Args: + billperiod (list): list of datetimes + bill_dictionary (dictionary): dictionary of bills (bill, {month, year} + inf_dictionary (dictionary): dictioanry of infs (inflation, {month, year}) + Returns: + dictionary: inflation adjusted dictionary of bills (bill, {month, year}) + """ + bdmonth = [] + bdyear = [] + for i in billperiod: + bdmonth.append(i.month) + for i in billperiod: + bdyear.append(i.year) + bdzipper = [(x, y) for x, y in zip(bdyear, bdmonth)] + list_helper = [] + for first, second in bill_dictionary.items(): + for third, fourth in inf_dictionary.items(): + if first == third: + list_helper.append(second * fourth) + dict_final = {key: value for key, value in zip(bdzipper, list_helper)} + + # return dict_final + return [(x, dict_final[x]) for x in sorted(dict_final)] + + +def hist_oil_price(oilcharge, oiluse): + """Return list of historical price for oil. + + Args: + oilcharge (list): list of oil charges per bill + oiluse (list): list of oil usage per bill + Returns: + list: oil price per billing period, 12 times - + for oil, we calculate one average price, and apply that across all months + """ + oilchargesum = sum(oilcharge) + oilusesum = sum(oiluse) + oprice = oilchargesum / oilusesum + newlist = [] + for number in range(1, 13): + newlist.append(oprice) + return newlist + + +def monthly_price(charge, use): + """Return Monthly prices per month. + + Args: + charge (list): list of charges + use (list): list of uses + Returns: + list: list of prices per month + """ + finalmonthprice = [x / y for x, y in zip(charge, use)] + return finalmonthprice + diff --git a/bpfin/utilbills/bill_proj_reg.py b/bpfin/utilbills/bill_proj_reg.py new file mode 100644 index 0000000..b6165de --- /dev/null +++ b/bpfin/utilbills/bill_proj_reg.py @@ -0,0 +1,98 @@ +import statsmodels.api as sm +import datetime +import calendar + + +def regression_base_sum(bill_start_utility, bill_end_utility, x_value_calendar, x_values): + """Return x values for regression. + + Args: + bill_start_utility (list): list of datetimes (start-date) from bills + bill_end_utility (list): list of datetimes (end-date) from bills + x_value_calendar (list): list of datetimes + x_values (list): x values corresponding to datetimes + + Returns: + list: x_values of a regression + """ + day = [] + month = [] + year = [] + for i in bill_start_utility: + day.append(i.day) + month.append(i.month) + year.append(i.year) + + day2 = [] + month2 = [] + year2 = [] + for i in x_value_calendar: + day2.append(i.day) + month2.append(i.month) + year2.append(i.year) + + datezipper = [(x, y, z) for x, y, z in zip(year, month, day)] + datezipper2 = [(x, y, z) for x, y, z in zip(year2, month2, day2)] + + list_subtractor = [x - y for x, y in zip(bill_end_utility, bill_start_utility)] + day_counter = [] + for number in list_subtractor: + if number is not None: + day_counter.append(abs(number.days)) + else: + day_counter.append(0) + + index_finder = [] + for i in datezipper: + for x in datezipper2: + if x == i: + index_finder.append(datezipper2.index(x)) + + list_creator = [] + for i in range(len(index_finder)): + list_creator.append(x_values[index_finder[i]:index_finder[i] + day_counter[i]]) + + x_totals = [] + for i in list_creator: + x_totals.append(sum(i)) + + return x_totals + + +def regression_coefficients(x_list, y_list, is_intercept): + """Regress two lists of values. + + Args: + x_list: list of base x values for regression + y_list: list of base y values for regression + is_intercept (boolean): it is true that we want an intercept value in our regression + Returns: + tuple: x and y coefficients from regression + """ + x_values = x_list + y_values = y_list + if is_intercept is True: + x_values = sm.add_constant(x_values) + model = sm.OLS(y_values, x_values) + results = model.fit() + coefficients = results.params + return coefficients + + +def regression_predicting_y(reg_history, periodstart, periodend, x_value_calendar, x_values): + """Return results of regression. + + Args: + reg_history (list): tuple of x and y coefficient based on historical regression + periodstart (list): bill + periodend (list): + x_value_calendar (list): + x_values (list) : x values that we want for projection + Returns: + list: progression of future y values + """ + x_list = regression_base_sum(periodstart, periodend, x_value_calendar, x_values) + future_y = [] + for i in x_list: + future_y.append(reg_history[0] + reg_history[1] * i) + return future_y diff --git a/bpfin/utilbills/billprojections.py b/bpfin/utilbills/billprojections.py deleted file mode 100644 index 02119f7..0000000 --- a/bpfin/utilbills/billprojections.py +++ /dev/null @@ -1,323 +0,0 @@ -import statsmodels.api as sm -import datetime -import calendar - -year = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] - - -def add_list(obj_list, number): - return [x + number for x in obj_list] - - -def cal_last_day(obj_year, obj_month): - _, last_day_num = calendar.monthrange(obj_year, obj_month) - return last_day_num - - -def form_bill_calendar(date_start, year_term): - bdstart = [] - bdend = [] - day = date_start - for term in range(12 * year_term): - first = datetime.date(day=1, month=day.month, year=day.year) - bdstart.append(first) - last = datetime.date(day.year, day.month, cal_last_day(day.year, day.month)) - bdend.append(last) - day = last + datetime.timedelta(days=1) - return [bdstart, bdend] - - -def form_date_calendar(date_start, date_end): - bdstart = [] - bdend = [] - day = date_start - i = 1 - while day <= date_end: - if i > 9999: - return False - first = datetime.date(day=1, month=day.month, year=day.year) - bdstart.append(first) - last = datetime.date(day.year, day.month, cal_last_day(day.year, day.month)) - bdend.append(last) - day = last + datetime.timedelta(days=1) - i += 1 - return [bdstart, bdend] - - -def form_year_month(target_terms): - obj_list = [] - for term in target_terms: - obj_list.append((term.year, term.month)) - return obj_list - - -def cal_inflation_product(inflation_rate_dict, present_date): - date_start = sorted(inflation_rate_dict)[0] - product = 1 - for term in form_date_calendar(date_start, present_date)[1]: - product = product * inflation_rate_dict[term] - return product - - -def cal_inflated_item(base_list, target_terms, inflation_coeff_dict, - present_date): # base_list is the base value near the present month - new_dict = dict(zip([], [])) - for term in target_terms: - new_dict[term] = base_list[term.month - 1] * inflation_coeff_dict[term] - return [new_dict[term] for term in sorted(new_dict)] - - -def form_inflation_ratedict(inflationyears, inflationhist): - inflationterms = form_bill_calendar(datetime.date(int(inflationyears[0]), 1, 31), len(inflationyears))[1] - inflation_rate = [] - for year, rate in zip(inflationyears, inflationhist): - for i in range(12): - inflation_rate.append(rate / 12) - inflation_rate = add_list(inflation_rate, 1) - dict1 = dict(zip(inflationterms, inflation_rate)) # return (2016,2,29 : 100.67%) - return dict1 - - -def form_inflation_coeffdict(inflationyears, inflationhist, present_date): - inflation_rate_dict = form_inflation_ratedict(inflationyears, inflationhist) - inflationterms = sorted(inflation_rate_dict) - new_dict = dict(zip([], [])) - for term in inflationterms: - new_dict[term] = cal_inflation_product(inflation_rate_dict, term) / cal_inflation_product(inflation_rate_dict, - present_date) - return new_dict - - -def daily_average_use_or_charge(from_date, to_date, total_utility_use_or_charge): - fromdate = [] - todate = [] - for x in from_date: - fromdate.append(x) - for y in to_date: - todate.append(y) - interstore = [list(x) for x in zip(fromdate, todate)] - finalstore = [] - for x in interstore: - f = (x[1] - x[0]) - finalstore.append(f.days) - - bill_daily_average = [x / y for x, y in zip(total_utility_use_or_charge, finalstore)] - perbill_useorcharge = [] - for x in bill_daily_average: - perbill_useorcharge.append(x) - return perbill_useorcharge - - -# returns 2d list -# [0, 4, 20...] means first bill with 4 days in feb and 20 days in march -def firstmatrix(from_date, to_date): - rangeID = 0 - for x in from_date: - rangeID += 1 - monthmatrix = [[0 for i in range(12)] for j in range(rangeID)] - for (fromdate, enddate, rangeID) in zip(from_date, to_date, range(rangeID)): - date = fromdate + datetime.timedelta(1) - while date <= enddate: - for month in range(1, 13): # range(1,13)=[1:12] - if date.month == month: - monthmatrix[rangeID][month - 1] += 1 - date = date + datetime.timedelta(1) - return monthmatrix - - -# returns 2d list -# [0, 4, 20...] means for jan, bill 1 has 0 days, bill 2 has 4 days -def finalmatrix(matrixchoice): - newmatrix = [] - for i in range(len(matrixchoice[0])): - bill = [] - for item in matrixchoice: - bill.append(item[i]) - newmatrix.append(bill) - return newmatrix - - -def normalize_average(matrix_choice): - newlist = [] - for i in matrix_choice: - newlist.append((sum(list(i)))) - divider = [x / y for x, y in zip(newlist, year)] - return divider - - -# calculate charge and usage as one array with 12 values representing the month -def monthly_info(billing_info, matrix, divider): - monthly_average = [] - for month in range(1, 13): - matrixcolumn = [] - for rangeID in range(len(billing_info)): - matrixcolumn.append(matrix[rangeID][month - 1]) - z = [daily_avg * (days) for (daily_avg, days) in zip(billing_info, matrixcolumn)] - total = 0 - for i in range(len(z)): - total += z[i] - monthly_average.append(total) - monthly_values = [x / y for x, y in zip(monthly_average, divider)] - return monthly_values - - -# 12 prices which are the same -def hist_oil_price(oilcharge, oiluse): - a = sum(oilcharge) - b = sum(oiluse) - oprice = a / b - newlist = [] - for i in range(1, 13): - newlist.append(oprice) - return newlist - - -def monthly_price(charge, use): - finalmonthprice = [x / y for x, y in zip(charge, use)] - return finalmonthprice - - -def occupancy_final_list(occupancy_rate_list): - hours_in_day = 24 - occupancy_list = [] - for x in occupancy_rate_list: - occupancy_list.append(x * 24) - return occupancy_list - - -def regression_base_sum(bill_start_utility, bill_end_utility, x_value_calendar, x_values): - day = [] - month = [] - year = [] - for i in bill_start_utility: - day.append(i.day) - month.append(i.month) - year.append(i.year) - - day2 = [] - month2 = [] - year2 = [] - for i in x_value_calendar: - day2.append(i.day) - month2.append(i.month) - year2.append(i.year) - - datezipper = [(x, y, z) for x, y, z in zip(year, month, day)] - datezipper2 = [(x, y, z) for x, y, z in zip(year2, month2, day2)] - - list_subtractor = [x - y for x, y in zip(bill_end_utility, bill_start_utility)] - day_counter = [] - for number in list_subtractor: - if number is not None: - day_counter.append(abs(number.days)) - else: - day_counter.append(0) - - index_finder = [] - for i in datezipper: - for x in datezipper2: - if x == i: - index_finder.append(datezipper2.index(x)) - - list_creator = [] - for i in range(len(index_finder)): - list_creator.append(x_values[index_finder[i]:index_finder[i] + day_counter[i]]) - - x_totals = [] - for i in list_creator: - x_totals.append(sum(i)) - - return x_totals - - -def regression_coefficients(x_list, y_list, is_intercept): - X = x_list - Y = y_list - if is_intercept == True: - X = sm.add_constant(X) - model = sm.OLS(Y, X) - results = model.fit() - coefficients = results.params - return coefficients - - -def regression_predicting_y(reg_history, periodstart, periodend, x_value_calendar, x_values): - x_list = regression_base_sum(periodstart, periodend, x_value_calendar, x_values) - future_y = [] - for i in x_list: - future_y.append(reg_history[0] + reg_history[1] * i) - return future_y - - -def rate_plan_cons(date, con, limit, leap, supply_charge, basic_charge, tax_rate, adjustments): - temp_list = [] - y = date.month - 1 - for i in range(len(limit)): - if (i + 1) < (len(limit)) and con > limit[i + 1][y]: - first = ((limit[i + 1][y] - limit[i][y]) * leap[i][y]) - temp_list.append(first) - if (i + 1) < (len(limit)) and con < limit[i + 1][y] and con > limit[i][y]: - second = ((con - limit[i][y]) * leap[i][y]) - temp_list.append(second) - if (i + 1) == len(limit): - third = max(((con - limit[len(leap) - 1][y]) * leap[i][y]), 0) - temp_list.append(third) - else: - temp_list.append(0) - result = sum(temp_list) + con * (supply_charge[y]) + basic_charge[y] - final_result = result * (1 + tax_rate[y]) * (1 + adjustments[y]) - - return final_result - - -# This function needs to be worked on -def calc_charge_list(conlist, datelist, limit, leap, supply_charge, basic_charge, tax_rate, adjustments): - charge = [] - for con, date in zip(conlist, datelist): - temp_charge = rate_plan_cons(date, con, limit, leap, supply_charge, basic_charge, tax_rate, adjustments) - charge.append(temp_charge) - return charge - - -def price_x_consumption(price, con_list): - return [x * price for x in con_list] - - -def utility_charge_sum(list1, list2, list3): - return [x + y + z for x, y, z in zip(list1, list2, list3)] - - -def charge_dictionary(billperiod, charge_list): - bdday = [] - bdmonth = [] - bdyear = [] - - for i in billperiod: - bdmonth.append(i.month) - for i in billperiod: - bdyear.append(i.year) - - bdzipper = [(x, y) for x, y in zip(bdyear, bdmonth)] - - dicthelper = {key: value for key, value in zip(billperiod, charge_list)} - - return dicthelper - - -def charge_with_inflation(billperiod, bill_dictionary, inf_dictionary): - bdmonth = [] - bdyear = [] - for i in billperiod: - bdmonth.append(i.month) - for i in billperiod: - bdyear.append(i.year) - bdzipper = [(x, y) for x, y in zip(bdyear, bdmonth)] - list_helper = [] - for a, b in bill_dictionary.items(): - for c, d in inf_dictionary.items(): - if a == c: - list_helper.append(b * d) - dict_final = {key: value for key, value in zip(bdzipper, list_helper)} - - # return dict_final - return [(x, dict_final[x]) for x in sorted(dict_final)] diff --git a/bpfin/utilbills/calcbill.py b/bpfin/utilbills/calcbill.py deleted file mode 100644 index c25e723..0000000 --- a/bpfin/utilbills/calcbill.py +++ /dev/null @@ -1,199 +0,0 @@ -from datetime import timedelta, datetime -# from buildingdata.models import Building, UtilityBill, RatePlan, RatePlanMonth -# from reference_data.models import Inflation, HddWeather, CddWeather -from .billprojections import * - - -def calculate_utility_bill(inflation_rate_info, inflation_year_info, current_date, - bd_start_el, bd_end_el, el_charge, - bd_start_gas, bd_end_gas, gas_charge, - el_usage, gas_usage, - oil_charge, oil_purchase, - hdd_date, hdd_projected, - cdd_date, cdd_projected, - split_start_date_oil, split_end_date_oil, - basic_charge_el, limit_el, leap_el, - tax_rate_el, adjustments_el, supply_charge_el, - basic_charge_gas, limit_gas, leap_gas, - tax_rate_gas, adjustments_gas, supply_charge_gas, - heat_el_use, cool_el_use, light_el_use, other_el_use, - heat_gas_use, cool_gas_use, dhw_gas_use, other_gas_use, - heat_oil_use, dhw_oil_use, other_oil_use - ): - - - inflation_dictionary = form_inflation_coeffdict(inflation_year_info, inflation_rate_info, current_date) - - daily_el_charge = daily_average_use_or_charge(bd_start_el, bd_end_el, el_charge) - daily_gas_charge = daily_average_use_or_charge(bd_start_gas, bd_end_gas, gas_charge) - - daily_el_use = daily_average_use_or_charge(bd_start_el, bd_end_el, el_usage) - daily_gas_use = daily_average_use_or_charge(bd_start_gas, bd_end_gas, gas_usage) - - month_matrix_el = firstmatrix(bd_start_el, bd_end_el) - month_matrix_gas = firstmatrix(bd_start_gas, bd_end_gas) - - new_matrix_el = finalmatrix(month_matrix_el) - new_matrix_gas = finalmatrix(month_matrix_gas) - - divider_el = normalize_average(new_matrix_el) - divider_gas = normalize_average(new_matrix_gas) - - monthly_use_el = monthly_info(daily_el_use, month_matrix_el, divider_el) - monthly_charge_el = monthly_info(daily_el_charge, month_matrix_el, divider_el) - - monthly_use_gas = monthly_info(daily_gas_use, month_matrix_gas, divider_gas) - monthly_charge_gas = monthly_info(daily_gas_charge, month_matrix_gas, divider_gas) - - hist_oil_pricing = hist_oil_price(oil_charge, oil_purchase) - - hist_monthly_el_price = monthly_price(monthly_charge_el, monthly_use_el) - - hist_monthly_gas_price = monthly_price(monthly_charge_gas, monthly_use_gas) - - starting_d0 = datetime.date(2012, 12, 1) - starting_d1 = datetime.date(2031, 1, 31) - - ending_d0 = datetime.date(2012, 12, 31) - ending_d1 = datetime.date(2031, 2, 1) - - occupancy_date = [] - starting_months = [] - ending_months = [] - - delta = starting_d1 - starting_d0 - for i in range(delta.days + 1): - my_date = starting_d0 + timedelta(days=i) - occupancy_date.append(my_date) - - if my_date.day is 1: - starting_months.append(my_date) - - delta = ending_d1 - ending_d0 - for i in range(delta.days + 1): - my_date = ending_d0 + timedelta(days=i) - - if my_date.day is 1: - ending_months.append(my_date - timedelta(days=1)) - - occupancy_rate = [0.95] * (delta.days + 3) - - # I added 3 to match the Jupyter Notebook length - # This might be an issue of leap year day count - - occupancy_list = occupancy_final_list(occupancy_rate) - - base_el_heat_hdd = regression_base_sum(bd_start_el, bd_end_el, hdd_date, hdd_projected) - base_el_cool_cdd = regression_base_sum(bd_start_el, bd_end_el, cdd_date, cdd_projected) - base_el_light_occ = regression_base_sum(bd_start_el, bd_end_el, occupancy_date, occupancy_list) - base_el_other_occ = regression_base_sum(bd_start_el, bd_end_el, occupancy_date, occupancy_list) - - base_gas_heat_hdd = regression_base_sum(bd_start_gas, bd_end_gas, hdd_date, hdd_projected) - base_gas_cool_cdd = regression_base_sum(bd_start_gas, bd_end_gas, cdd_date, cdd_projected) - base_gas_dhw_occ = regression_base_sum(bd_start_gas, bd_end_gas, occupancy_date, occupancy_list) - base_gas_other_occ = regression_base_sum(bd_start_gas, bd_end_gas, occupancy_date, occupancy_list) - - base_oil_heat_hdd = regression_base_sum(split_start_date_oil, split_end_date_oil, hdd_date, hdd_projected) - base_oil_dhw_occ = regression_base_sum(split_start_date_oil, split_end_date_oil, occupancy_date, occupancy_list) - base_oil_other_occ = regression_base_sum(split_start_date_oil, split_end_date_oil, occupancy_date, occupancy_list) - - reg_el_heat_hdd = regression_coefficients(base_el_heat_hdd, heat_el_use, True) - reg_el_cool_cdd = regression_coefficients(base_el_cool_cdd, cool_el_use, True) - reg_el_light_occ = regression_coefficients(base_el_light_occ, light_el_use, True) - reg_el_other_occ = regression_coefficients(base_el_other_occ, other_el_use, True) - - reg_gas_heat_hdd = regression_coefficients(base_gas_heat_hdd, heat_gas_use, True) - reg_gas_cool_cdd = regression_coefficients(base_gas_cool_cdd, cool_gas_use, True) - reg_gas_dhw_occ = regression_coefficients(base_gas_dhw_occ, dhw_gas_use, True) - reg_gas_other_occ = regression_coefficients(base_gas_other_occ, other_gas_use, True) - - reg_oil_heat_hdd = regression_coefficients(base_oil_heat_hdd, heat_oil_use, True) - reg_oil_dhw_occ = regression_coefficients(base_oil_dhw_occ, dhw_oil_use, True) - reg_oil_other_occ = regression_coefficients(base_oil_other_occ, other_oil_use, True) - - reg_output_el_heat = regression_predicting_y(reg_el_heat_hdd, starting_months, - ending_months, hdd_date, hdd_projected) - reg_output_el_cool = regression_predicting_y(reg_el_cool_cdd, starting_months, - ending_months, cdd_date, cdd_projected) - reg_output_el_light = regression_predicting_y(reg_el_light_occ, starting_months, - ending_months, occupancy_date, occupancy_list) - reg_output_el_other = regression_predicting_y(reg_el_other_occ, starting_months, - ending_months, occupancy_date, occupancy_list) - - reg_output_gas_heat = regression_predicting_y(reg_gas_heat_hdd, - starting_months, - ending_months, - hdd_date, - hdd_projected) - - reg_output_gas_cool = regression_predicting_y(reg_gas_cool_cdd, - starting_months, - ending_months, - cdd_date, - cdd_projected) - - reg_output_gas_dhw = regression_predicting_y(reg_gas_dhw_occ, - starting_months, - ending_months, - occupancy_date, - occupancy_list) - - reg_output_gas_other = regression_predicting_y(reg_gas_other_occ, - starting_months, - ending_months, - occupancy_date, - occupancy_list) - - reg_output_oil_heat = regression_predicting_y(reg_oil_heat_hdd, starting_months, ending_months, - hdd_date, hdd_projected) - reg_output_oil_dhw = regression_predicting_y(reg_oil_dhw_occ, starting_months, ending_months, - occupancy_date, occupancy_list) - reg_output_oil_other = regression_predicting_y(reg_oil_other_occ, starting_months, ending_months, - occupancy_date, occupancy_list) - - # In limit_gas and leap_gas, I only appended three limits and leaps. - - # The correct number of limits and leaps must be noted!! - - calc_charge_el_heat = calc_charge_list(reg_output_el_heat, ending_months, limit_el, leap_el, - supply_charge_el, basic_charge_el, tax_rate_el, adjustments_el) - calc_charge_el_cool = calc_charge_list(reg_output_el_cool, ending_months, limit_el, leap_el, - supply_charge_el, basic_charge_el, tax_rate_el, adjustments_el) - calc_charge_el_light = calc_charge_list(reg_output_el_light, ending_months, limit_el, leap_el, - supply_charge_el, basic_charge_el, tax_rate_el, adjustments_el) - calc_charge_el_other = calc_charge_list(reg_output_el_other, ending_months, limit_el, leap_el, - supply_charge_el, basic_charge_el, tax_rate_el, adjustments_el) - - calc_charge_gas_heat = calc_charge_list(reg_output_gas_heat, ending_months, limit_gas, leap_gas, - supply_charge_gas, basic_charge_gas, tax_rate_gas, adjustments_gas) - calc_charge_gas_cool = calc_charge_list(reg_output_gas_cool, ending_months, limit_gas, leap_gas, - supply_charge_gas, basic_charge_gas, tax_rate_gas, adjustments_gas) - calc_charge_gas_dhw = calc_charge_list(reg_output_gas_dhw, ending_months, limit_gas, leap_gas, - supply_charge_gas, basic_charge_gas, tax_rate_gas, adjustments_gas) - - # gas_other is 0 right now - - calc_charge_gas_other = calc_charge_list(reg_output_gas_other, ending_months, limit_gas, leap_gas, - supply_charge_gas, basic_charge_gas, tax_rate_gas, adjustments_gas) - - calc_charge_oil_heat = price_x_consumption(hist_oil_pricing[1], reg_output_oil_heat) - calc_charge_oil_dhw = price_x_consumption(hist_oil_pricing[1], reg_output_oil_dhw) - calc_charge_oil_other = price_x_consumption(hist_oil_pricing[1], reg_output_oil_other) - - el_charge_no_savings = [w + x + y + z for w, x, y, z in - zip(calc_charge_el_heat, calc_charge_el_cool, calc_charge_el_light, - calc_charge_el_other)] - - # gas_other is not included - - gas_charge_no_savings = [w + x + y for w, x, y in - zip(calc_charge_gas_heat, calc_charge_gas_cool, calc_charge_gas_dhw)] - - oil_charge_no_savings = [x + y + z for x, y, z in - zip(calc_charge_oil_heat, calc_charge_oil_dhw, calc_charge_oil_other)] - - bill_no_savings = utility_charge_sum(el_charge_no_savings, gas_charge_no_savings, oil_charge_no_savings) - bill_dict_no_savings = charge_dictionary(ending_months, bill_no_savings) - bill_dict_no_savings_inf = charge_with_inflation(ending_months, bill_dict_no_savings, inflation_dictionary) - - return bill_dict_no_savings_inf -- GitLab From 5685eac967ae8f3323a3fb548295c41f2c50da70 Mon Sep 17 00:00:00 2001 From: chenzheng06 Date: Thu, 13 Apr 2017 22:23:45 -0400 Subject: [PATCH 2/7] try file structure modify --- bpfin/utilbills/bill_month_normalize_rough.py | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 bpfin/utilbills/bill_month_normalize_rough.py diff --git a/bpfin/utilbills/bill_month_normalize_rough.py b/bpfin/utilbills/bill_month_normalize_rough.py new file mode 100644 index 0000000..afa6afd --- /dev/null +++ b/bpfin/utilbills/bill_month_normalize_rough.py @@ -0,0 +1,2 @@ +def bill_month_normalize_rough(): + pass -- GitLab From 5f3a6f6a406f54cc9b4997bbf6f175f68f0af028 Mon Sep 17 00:00:00 2001 From: chenzheng06 Date: Fri, 14 Apr 2017 13:54:24 -0400 Subject: [PATCH 3/7] clean prelimfuncs, created normalize_rough. moved functions from prelimfuncs to lib --- bpfin/prelim/prelimfuncs.py | 11 --- .../test_month_normalize_rough.py | 0 bpfin/utilbills/bill_month_normalize_rough.py | 81 ++++++++++++++++++- 3 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 bpfin/tests/test_utilbills/test_month_normalize_rough.py diff --git a/bpfin/prelim/prelimfuncs.py b/bpfin/prelim/prelimfuncs.py index dadda30..8e5ff98 100644 --- a/bpfin/prelim/prelimfuncs.py +++ b/bpfin/prelim/prelimfuncs.py @@ -9,17 +9,6 @@ import xlrd from scipy.optimize import linprog import matplotlib -import statsmodels.api as sm -from dateutil.parser import * -import calendar -import numpy as np -import pandas as pd -from datetime import * -import datetime, time -import xlrd -from scipy.optimize import linprog -import matplotlib - matplotlib.use('TkAgg') diff --git a/bpfin/tests/test_utilbills/test_month_normalize_rough.py b/bpfin/tests/test_utilbills/test_month_normalize_rough.py new file mode 100644 index 0000000..e69de29 diff --git a/bpfin/utilbills/bill_month_normalize_rough.py b/bpfin/utilbills/bill_month_normalize_rough.py index afa6afd..4eb77ee 100644 --- a/bpfin/utilbills/bill_month_normalize_rough.py +++ b/bpfin/utilbills/bill_month_normalize_rough.py @@ -1,2 +1,79 @@ -def bill_month_normalize_rough(): - pass +import datetime + + +class Bill(): + def __init__(self, raw_bill): + self.utility_type=raw_bill['utility_type'] + # self.billstart = convert_timestamp_date(Hist_Bill['From Date']) + # self.billend = convert_timestamp_date(Hist_Bill['To Date']) + # self.usage = Hist_Bill['Total Usage'] + # self.charge = Hist_Bill['Total Charge'] + # self.utility_type = utility_type + + # def hist(self): + # if self.billstart != []: + # monthmatrix = form_month_matrix(self.billstart, self.billend) + # self.hist_daily_usage = division(self.usage, date_diff(self.billstart, self.billend)) + # self.hist_daily_charge = division(self.charge, date_diff(self.billstart, self.billend)) + # self.hist_monthly_usage = monthly_average(self.hist_daily_usage, monthmatrix) + # self.hist_monthly_charge = monthly_average(self.hist_daily_charge, monthmatrix) + # self.hist_monthly_price = division(self.hist_monthly_charge, self.hist_monthly_usage) + # else: + # self.hist_monthly_usage = [0] * 12 + # self.hist_monthly_charge = [0] * 12 + # self.hist_monthly_price = [0] * 12 + # return self + + # def prior(self, FinancialDict): + # [self.prior_billstart, self.prior_billend] = form_bill_calendar(FinancialDict['ProForma_Start'], + # FinancialDict['ProForma_Duration']) + # self.prior_monthly_usage = duplicate2terms(self.hist_monthly_usage, self.prior_billend) + # self.prior_monthly_price = form_inflated_item(self.hist_monthly_price, self.prior_billend, + # FinancialDict['inflation_coeff_dict'], + # FinancialDict['present_date']) + # self.prior_monthly_charge = product2list(self.prior_monthly_usage, self.prior_monthly_price) + + +def bill_month_normalize_rough(raw_bill): + hist_bill = Bill(raw_bill) + month_normalized_bill = {} + month_normalized_bill['utility_type'] = we_want_type = hist_bill.utility_type + print("\n It goes ok") + return month_normalized_bill + +# the following are tests ###################### +test_raw_bill = {} + +test_raw_bill['date_from'] = [ + datetime.date(2014, 4, 17), datetime.date(2014, 5, 16), datetime.date(2014, 6, 17), + datetime.date(2014, 7, 17), datetime.date(2014, 8, 15), datetime.date(2014, 9, 16), + datetime.date(2014, 10, 16), datetime.date(2014, 11, 14), datetime.date(2014, 12, 17), + datetime.date(2015, 1, 16), datetime.date(2015, 2, 18), datetime.date(2015, 3, 19), + datetime.date(2015, 4, 17), datetime.date(2015, 5, 18), datetime.date(2015, 6, 17), + datetime.date(2015, 7, 17), datetime.date(2015, 8, 17), datetime.date(2015, 9, 16), + datetime.date(2015, 10, 16), datetime.date(2015, 11, 16), datetime.date(2015, 12, 17), + datetime.date(2016, 1, 19), datetime.date(2016, 2, 18), datetime.date(2016, 3, 18)] + +test_raw_bill['date_to'] = [ + datetime.date(2014, 5, 16), datetime.date(2014, 6, 17), datetime.date(2014, 7, 17), + datetime.date(2014, 8, 15), datetime.date(2014, 9, 16), datetime.date(2014, 10, 16), + datetime.date(2014, 11, 14), datetime.date(2014, 12, 17), datetime.date(2015, 1, 16), + datetime.date(2015, 2, 18), datetime.date(2015, 3, 19), datetime.date(2015, 4, 17), + datetime.date(2015, 5, 18), datetime.date(2015, 6, 17), datetime.date(2015, 7, 17), + datetime.date(2015, 8, 17), datetime.date(2015, 9, 16), datetime.date(2015, 10, 16), + datetime.date(2015, 11, 16), datetime.date(2015, 12, 17), datetime.date(2016, 1, 19), + datetime.date(2016, 2, 18), datetime.date(2016, 3, 18), datetime.date(2016, 4, 18)] + +test_raw_bill['charge'] = [ + 2601.6289696969693, 2642.1740261508003, 3437.551946607342, 2930.5070588449826, + 2514.1242835094554, 2804.4448749999997, 3237.7359972191325, 3289.4155027808674, + 2796.5700000000002, 2463.5113047830923, 2370.0007392051775, 2479.081440860215] + +test_raw_bill['usage'] = [ + 0.094265231101663627, 0.10175446346513953, 0.12248981352003527, 0.11229655777596657, + 0.092170964664193167, 0.094173672324921492, 0.095187070309577237, 0.097177529967719486, + 0.095676831493476874, 0.091583582602530869, 0.089122740090923164, 0.091516956021057888] + +test_raw_bill['utility_type'] = 'electricity' + +print(bill_month_normalize_rough(test_raw_bill)) -- GitLab From ce199caf849755714b9ff6dad3b200361a1e3ca2 Mon Sep 17 00:00:00 2001 From: chenzheng06 Date: Fri, 14 Apr 2017 14:09:04 -0400 Subject: [PATCH 4/7] moved math functions to lib-other.py, cleared some duplication --- bpfin/lib/other.py | 476 +++++++++++++++++++++++++++++++++ bpfin/prelim/prelimfuncs.py | 510 ++++++++++++++++++------------------ bpfin/utilbills/bill_lib.py | 1 + 3 files changed, 732 insertions(+), 255 deletions(-) diff --git a/bpfin/lib/other.py b/bpfin/lib/other.py index e69de29..b9a1588 100644 --- a/bpfin/lib/other.py +++ b/bpfin/lib/other.py @@ -0,0 +1,476 @@ +import statsmodels.api as sm +import datetime +import calendar +from dateutil.parser import * +import numpy as np +import pandas +import xlrd +from scipy.optimize import linprog +import matplotlib +matplotlib.use('TkAgg') + +def add_list(obj_list, number): + """Add a number to each value in a list. + + Args: + obj_list (list): list of numbers + number (int): number value + Returns: + list: numbers + number to each value + """ + return [x + number for x in obj_list] + + +def cal_last_day(obj_year, obj_month): + """Return last day of month given year,month. + + Args: + obj_year (int): year + obj_month (int): month + Returns: + int: last day of month + """ + _, last_day_num = calendar.monthrange(obj_year, obj_month) + return last_day_num + + +def form_bill_calendar(date_start, year_term): + """Return single list value of first date in bill period and last date in bill period. + + Args: + date_start (datetime): date xx/yy/abcd + year_term (int): number of years in bill + Returns: + list: first date in bill period, last date in bill period + + """ + bdstart = [] + bdend = [] + day = date_start + for term in range(12 * year_term): + first = datetime.date(day=1, month=day.month, year=day.year) + bdstart.append(first) + last = datetime.date(day.year, day.month, cal_last_day(day.year, day.month)) + bdend.append(last) + day = last + datetime.timedelta(days=1) + return [bdstart, bdend] + + +def form_date_calendar(date_start, date_end): + """Return list of all calendar dates. + + Args: + date_start (datetime): date xx/yy/abcd + date_end (datetime): date xx/yy/abcd + Returns: + list: all calendar dates, first date is first day of calendar + """ + bdstart = [] + bdend = [] + day = date_start + i = 1 + while day <= date_end: + if i > 9999: + return False + first = datetime.date(day=1, month=day.month, year=day.year) + bdstart.append(first) + last = datetime.date(day.year, day.month, cal_last_day(day.year, day.month)) + bdend.append(last) + day = last + datetime.timedelta(days=1) + i += 1 + return [bdstart, bdend] + + +def form_year_month(target_terms): + """Return list of month,year of datetimes in a list. + + Args: + target_terms (list of datetimes): date xx/yy/abcd + Returns: + list: list of tuples containing year and month + """ + obj_list = [] + for term in target_terms: + obj_list.append((term.year, term.month)) + return obj_list + + +def cal_inflation_product(inflation_rate_dict, present_date): + """Return the inflation value for the current date. + + Args: + inflation_rate_dict (dictionary): dictionary of {date,inflation value} + present_date (datetime): present date + Returns: + integer: inflation rate + + """ + date_start = sorted(inflation_rate_dict)[0] + product = 1 + for term in form_date_calendar(date_start, present_date)[1]: + product = product * inflation_rate_dict[term] + return product + + +def cal_inflated_item(base_list, target_terms, inflation_coeff_dict): + # Deleted "present_day" from inputs. + """Create dictionary of inflated terms. + + Args: + base_list (integer): base value + target_terms (list): list of inflation values + inflation_coeff_dict (dictionary): dictioanry of inflation coefficients with date + Returns: + dictionary: coefficients and dates + """ + new_dict = dict(zip([], [])) + for term in target_terms: + new_dict[term] = base_list[term.month - 1] * inflation_coeff_dict[term] + return [new_dict[term] for term in sorted(new_dict)] + + +def form_inflation_ratedict(inflationyears, inflationhist): + """Create dictionary of {date,inflation}. + + Args: + inflationyears (list): list of years + inflationhist (list): list of inflation values + Returns: + dictionary: {datetime, adjusted inflation} + """ + inflationterms = form_bill_calendar(datetime.date(int(inflationyears[0]), 1, 31), len(inflationyears))[1] + inflation_rate = [] + for year, rate in zip(inflationyears, inflationhist): + for number in range(12): + inflation_rate.append(rate / 12) + inflation_rate = add_list(inflation_rate, 1) + dict1 = dict(zip(inflationterms, inflation_rate)) # return (2016,2,29 : 100.67%) + return dict1 + + +def form_inflation_coeffdict(inflationyears, inflationhist, present_date): + """Create dictionary of date:inflation relative to present date. + + Args: + inflationyears (list): list of years + inflationhist (list): list of inflation values + present_date (datetime): present date + Returns: + dictionary: date:inflation relative to present date + """ + inflation_rate_dict = form_inflation_ratedict(inflationyears, inflationhist) + inflationterms = sorted(inflation_rate_dict) + new_dict = dict(zip([], [])) + for term in inflationterms: + new_dict[term] = cal_inflation_product(inflation_rate_dict, term) / cal_inflation_product(inflation_rate_dict, + present_date) + return new_dict + + +def occupancy_final_list(occupancy_rate_list): + """Return list of occupancy values. + + Args: + occupancy_rate_list (list): list of occupancy integers + Returns: + list: list of occupancy integers * 24, representing occupancy in hours + """ + occupancy_list = [] + for occ in occupancy_rate_list: + occupancy_list.append(occ * 24) + return occupancy_list + + +########################################### need to check duplication ################### + +##math calculations +# divide list x by list y, return list z +def division(x, y): + z = [] + for (i, j) in zip(x, y): + if j == 0: + d = 0 + else: + d = i / j + z.append(d) + return z + + +def product2list(list1, list2): + return [x * y for x, y in zip(list1, list2)] + + +def product(list1): + a = 1 + for i in list1: + a = a * i + return a + + +def sum2list(list1, list2): + return [x + y for x, y in zip(list1, list2)] + + +def sum3list(list1, list2, list3): + return [x + y + z for x, y, z in zip(list1, list2, list3)] + + +def sublist(list1, list2): + return [x - y for x, y in zip(list1, list2)] + + +# def add_list(obj_list, number): +# return [x + number for x in obj_list] + + +def multiply_list(obj_list, number): + return [x * number for x in obj_list] + + +def form_dict(terms, obj): + obj_dict = dict(zip(terms, obj)) + return obj_dict + + +# calculate number of days in given month +# this fun is used for matrix +def days_in_month(month): + if month in {1, 3, 5, 7, 8, 10, 12}: return 31 + if month in {4, 6, 9, 11}: return 30 + if month in {2}: + return 28.25 + else: + return 0 + + +# calculate days in a range, for two list of dates +def date_diff(startdate, enddate): + return list(map(lambda x, y: (y - x).days, startdate, enddate)) + + +# def cal_last_day(obj_year, obj_month): +# _, last_day_num = calendar.monthrange(obj_year, obj_month) +# return last_day_num + + +# def form_bill_calendar(date_start, year_term): +# # year_term=1 +# bdstart = [] +# bdend = [] +# day = date_start +# for term in range(int(12 * year_term)): +# first = datetime.date(day=1, month=day.month, year=day.year) +# bdstart.append(first) +# last = datetime.date(day.year, day.month, cal_last_day(day.year, day.month)) +# bdend.append(last) +# day = last + datetime.timedelta(days=1) +# return [bdstart, bdend] + + +# def form_date_calendar(date_start, date_end): +# bdstart = [] +# bdend = [] +# day = date_start +# i = 1 +# while day <= date_end: +# if i > 9999: +# return False +# first = datetime.date(day=1, month=day.month, year=day.year) +# bdstart.append(first) +# last = datetime.date(day.year, day.month, cal_last_day(day.year, day.month)) +# bdend.append(last) +# day = last + datetime.timedelta(days=1) +# i += 1 +# return [bdstart, bdend] + + +def form_year_terms(year): + start_date = datetime.date(year, 1, 31) + terms = form_bill_calendar(start_date, 1) + return terms[1] + + +# print(form_year_terms(2013)) + +def convert_timestamp_date(list1): + return list(map(lambda x: datetime.date(x.year, x.month, x.day), list1)) + + +def cal_latest_day(terms_list): + latestday = datetime.date(1900, 1, 1) + for term_i in terms_list: + for term in term_i: + latestday = (term if term > latestday else latestday) + return latestday + + +def average(list1): + if len(list1) == 0: + return 0 + else: + return sum(list1) / len(list1) + + +def cal_CAGR(start_value, end_value, n): + if start_value != 0: + CAGR = (end_value / start_value) ** (1 / n) - 1 + else: + CAGR = 0 + return CAGR + + +# build the matrix to reflect #of days in each month +def form_month_matrix(bdstart, bdend): + monthmatrix = [[0 for i in range(12)] for j in range(len(bdstart))] + # rangeID=0 + for (fromdate, enddate, rangeID) in zip(bdstart, bdend, range(len(bdstart))): + date = fromdate + datetime.timedelta(1) + while date <= enddate: + for month in range(1, 13): # range(1,13)=[1:12] + if date.month == month: + monthmatrix[rangeID][month - 1] += 1 + date = date + datetime.timedelta(1) + return monthmatrix + + +def sumproduct(list1, list2): + return np.sum(m for m in np.multiply(np.array(list1), np.array(list2))) + + +# calculate monthly usage/charge with matrix, return a list +def monthly_average(daily, matrix): + month_average = [] + for month in range(1, 13): + matrixcolumn = list(map(lambda rangeID: matrix[rangeID][month - 1], range(len(daily)))) + total = sumproduct(daily, matrixcolumn) * days_in_month(month) / sum(matrixcolumn) + month_average.append(total) + return month_average + + +# oil price calculation +def cal_oil_price(oilcharge, oiluse): + if sum(oilcharge) == 0 or pd.isnull(sum(oilcharge)) == 1: + overall_price = 0 + else: + overall_price = sum(oilcharge) / sum(oiluse) + oil_price = [] + for i in range(1, 13): + oil_price.append(overall_price) + return oil_price + + +# list1=12 month values, target_terms = multiple years. Copy the values to all months +def duplicate2terms(list1, target_terms): + monthlist = range(1, 13) + dict1 = dict(zip(monthlist, list1)) + new_dict = dict(zip([], [])) + for term in target_terms: + new_dict[term] = dict1[term.month] + return [new_dict[term] for term in sorted(new_dict)] + + +# calculate max loan term and assign to total energy loan +def cal_max_term(loan_list): + max_length = 0 + max_ID = 0 + for loanID in range(len(loan_list)): + if len(loan_list[loanID].terms) >= max_length: + max_length = len(loan_list[loanID].terms) + max_ID = loanID + return loan_list[max_ID].terms + + +# calculate and assign cash flow for loans and for total energy loan +def align2term(terms1, list1, target_terms): + dict1 = dict(zip(terms1, list1)) + new_dict = dict(zip([], [])) + for term in target_terms: + if term in terms1: + new_dict[term] = dict1[term] + else: + new_dict[term] = 0 + return [new_dict[term] for term in sorted(new_dict)] + + +# calculate loan's (loan amount/debt service), just like a payback +def cal_loan_payback(interest, duration): + return ((1 + interest) ** duration - 1) / (interest * (1 + interest) ** duration) + + +# cauclate loan amount should be borrowed from each bank, given assumed total cost and loan info +def loan_allocate(total_cost, loan_list): + sum_loan_max = 0 + bound_list = [] + c_base = [] + for loan in loan_list: + sum_loan_max += loan.max_amount + c_base.append( + 1 / loan.payback) # loan amount / ratio = DS, targeting minimum total DS, equivalent to longest payback + bound_list.append((0, loan.max_amount)) + c = c_base # c_base is target function + A = [-1] * len(loan_list) + b = [max(0 - total_cost, 0 - sum_loan_max)] + bounds = bound_list + if total_cost > sum_loan_max: + print('alert: upfront cost > available loan amount') + res = linprog(c, A_ub=A, b_ub=b, bounds=bounds, options={'disp': False}) + for i in range(len(loan_list)): + loan_list[i].amount = res.x[i] + + +# return res.x + +##maybe we can use this, maybe not +# def transpose_matrix(matrix1): +# return [[r[col] for r in matrix1] for col in range(len(matrix1[0]))] + +def month_shift(month): + month_shift_dict = dict(zip([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1])) + return month_shift_dict[month] + + +####inflation calculation + +#product inflation rate from starting date to target date +# def cal_inflation_product(inflation_rate_dict,target_date): +# date_start=sorted(inflation_rate_dict)[0] +# # product=1 +# # for term in form_date_calendar(date_start,target_date)[1]: +# # product = product * inflation_rate_dict[term] +# product = np.prod(np.array([inflation_rate_dict[term] for term in form_date_calendar(date_start,target_date)[1]])) +# return product + +# def cal_previous_term(date): +# first_date=datetime.date(date.year,date.month,1) +# previous_term=first_date+datetime.timedelta(days=-1) +# return previous_term + +# ##calculate inflation_coeff_dict. Inflation=0.02, inflation rate =1.02, inflation coeff=1.02 or 0.98, or 1.00... +# # form inflation_rate_current_year +# def form_inflation_ratedict(inflationyears,inflationhist): +# hist_dict=dict(zip(inflationyears,inflationhist)) +# inflationterms=form_bill_calendar(datetime.date(inflationyears[0],1,31),len(inflationyears))[1] +# dict1={} +# for term in inflationterms: +# dict1[term]=hist_dict[term.year]/12+1 +# return dict1 #return monthly inflation rates (2016,2,29 : 100.67%) + +# def form_inflation_coeffdict(inflationyears,inflationhist,present_date): +# inflation_rate_dict=form_inflation_ratedict(inflationyears,inflationhist) +# inflationterms=sorted(inflation_rate_dict) +# new_dict=dict(zip([],[])) +# for term in inflationterms: +# new_dict[term]=cal_inflation_product(inflation_rate_dict,term)/cal_inflation_product(inflation_rate_dict,present_date) +# return new_dict #return (2016,2,29 : 99.86%; present_date: 100%) + +# form inflated items, based on a 12 month list, present time is the last month of 12 months list +def form_inflated_item(base_list,target_terms,inflation_coeff_dict,present_date): + pre_year_date=datetime.date(present_date.year-1,present_date.month+1,cal_last_day(present_date.year-1,present_date.month+1)) + base_terms=form_date_calendar(pre_year_date,present_date)[1] + for term in base_terms: + base_list[term.month-1]=base_list[term.month-1]/inflation_coeff_dict[term] + new_dict=dict(zip([],[])) + for term in target_terms: + new_dict[term]=base_list[term.month-1]*inflation_coeff_dict[term] + return [new_dict[term] for term in sorted(new_dict)] + diff --git a/bpfin/prelim/prelimfuncs.py b/bpfin/prelim/prelimfuncs.py index 8e5ff98..f4e31c0 100644 --- a/bpfin/prelim/prelimfuncs.py +++ b/bpfin/prelim/prelimfuncs.py @@ -12,296 +12,296 @@ import matplotlib matplotlib.use('TkAgg') -##math calculations -# divide list x by list y, return list z -def division(x, y): - z = [] - for (i, j) in zip(x, y): - if j == 0: - d = 0 - else: - d = i / j - z.append(d) - return z - +# ##math calculations +# # divide list x by list y, return list z +# def division(x, y): +# z = [] +# for (i, j) in zip(x, y): +# if j == 0: +# d = 0 +# else: +# d = i / j +# z.append(d) +# return z -def product2list(list1, list2): - return [x * y for x, y in zip(list1, list2)] +# def product2list(list1, list2): +# return [x * y for x, y in zip(list1, list2)] -def product(list1): - a = 1 - for i in list1: - a = a * i - return a +# def product(list1): +# a = 1 +# for i in list1: +# a = a * i +# return a -def sum2list(list1, list2): - return [x + y for x, y in zip(list1, list2)] +# def sum2list(list1, list2): +# return [x + y for x, y in zip(list1, list2)] -def sum3list(list1, list2, list3): - return [x + y + z for x, y, z in zip(list1, list2, list3)] +# def sum3list(list1, list2, list3): +# return [x + y + z for x, y, z in zip(list1, list2, list3)] -def sublist(list1, list2): - return [x - y for x, y in zip(list1, list2)] +# def sublist(list1, list2): +# return [x - y for x, y in zip(list1, list2)] -def add_list(obj_list, number): - return [x + number for x in obj_list] +# def add_list(obj_list, number): +# return [x + number for x in obj_list] -def multiply_list(obj_list, number): - return [x * number for x in obj_list] +# def multiply_list(obj_list, number): +# return [x * number for x in obj_list] -def form_dict(terms, obj): - obj_dict = dict(zip(terms, obj)) - return obj_dict +# def form_dict(terms, obj): +# obj_dict = dict(zip(terms, obj)) +# return obj_dict -# calculate number of days in given month -# this fun is used for matrix -def days_in_month(month): - if month in {1, 3, 5, 7, 8, 10, 12}: return 31 - if month in {4, 6, 9, 11}: return 30 - if month in {2}: - return 28.25 - else: - return 0 +# # calculate number of days in given month +# # this fun is used for matrix +# def days_in_month(month): +# if month in {1, 3, 5, 7, 8, 10, 12}: return 31 +# if month in {4, 6, 9, 11}: return 30 +# if month in {2}: +# return 28.25 +# else: +# return 0 -# calculate days in a range, for two list of dates -def date_diff(startdate, enddate): - return list(map(lambda x, y: (y - x).days, startdate, enddate)) +# # calculate days in a range, for two list of dates +# def date_diff(startdate, enddate): +# return list(map(lambda x, y: (y - x).days, startdate, enddate)) -def cal_last_day(obj_year, obj_month): - _, last_day_num = calendar.monthrange(obj_year, obj_month) - return last_day_num +# def cal_last_day(obj_year, obj_month): +# _, last_day_num = calendar.monthrange(obj_year, obj_month) +# return last_day_num -def form_bill_calendar(date_start, year_term): - # year_term=1 - bdstart = [] - bdend = [] - day = date_start - for term in range(int(12 * year_term)): - first = datetime.date(day=1, month=day.month, year=day.year) - bdstart.append(first) - last = datetime.date(day.year, day.month, cal_last_day(day.year, day.month)) - bdend.append(last) - day = last + datetime.timedelta(days=1) - return [bdstart, bdend] +# def form_bill_calendar(date_start, year_term): +# # year_term=1 +# bdstart = [] +# bdend = [] +# day = date_start +# for term in range(int(12 * year_term)): +# first = datetime.date(day=1, month=day.month, year=day.year) +# bdstart.append(first) +# last = datetime.date(day.year, day.month, cal_last_day(day.year, day.month)) +# bdend.append(last) +# day = last + datetime.timedelta(days=1) +# return [bdstart, bdend] -def form_date_calendar(date_start, date_end): - bdstart = [] - bdend = [] - day = date_start - i = 1 - while day <= date_end: - if i > 9999: - return False - first = datetime.date(day=1, month=day.month, year=day.year) - bdstart.append(first) - last = datetime.date(day.year, day.month, cal_last_day(day.year, day.month)) - bdend.append(last) - day = last + datetime.timedelta(days=1) - i += 1 - return [bdstart, bdend] +# def form_date_calendar(date_start, date_end): +# bdstart = [] +# bdend = [] +# day = date_start +# i = 1 +# while day <= date_end: +# if i > 9999: +# return False +# first = datetime.date(day=1, month=day.month, year=day.year) +# bdstart.append(first) +# last = datetime.date(day.year, day.month, cal_last_day(day.year, day.month)) +# bdend.append(last) +# day = last + datetime.timedelta(days=1) +# i += 1 +# return [bdstart, bdend] -def form_year_terms(year): - start_date = datetime.date(year, 1, 31) - terms = form_bill_calendar(start_date, 1) - return terms[1] +# def form_year_terms(year): +# start_date = datetime.date(year, 1, 31) +# terms = form_bill_calendar(start_date, 1) +# return terms[1] -# print(form_year_terms(2013)) -def convert_timestamp_date(list1): - return list(map(lambda x: datetime.date(x.year, x.month, x.day), list1)) +# # print(form_year_terms(2013)) +# def convert_timestamp_date(list1): +# return list(map(lambda x: datetime.date(x.year, x.month, x.day), list1)) -def cal_latest_day(terms_list): - latestday = datetime.date(1900, 1, 1) - for term_i in terms_list: - for term in term_i: - latestday = (term if term > latestday else latestday) - return latestday +# def cal_latest_day(terms_list): +# latestday = datetime.date(1900, 1, 1) +# for term_i in terms_list: +# for term in term_i: +# latestday = (term if term > latestday else latestday) +# return latestday -def average(list1): - if len(list1) == 0: - return 0 - else: - return sum(list1) / len(list1) - -def cal_CAGR(start_value, end_value, n): - if start_value != 0: - CAGR = (end_value / start_value) ** (1 / n) - 1 - else: - CAGR = 0 - return CAGR - - -# build the matrix to reflect #of days in each month -def form_month_matrix(bdstart, bdend): - monthmatrix = [[0 for i in range(12)] for j in range(len(bdstart))] - # rangeID=0 - for (fromdate, enddate, rangeID) in zip(bdstart, bdend, range(len(bdstart))): - date = fromdate + datetime.timedelta(1) - while date <= enddate: - for month in range(1, 13): # range(1,13)=[1:12] - if date.month == month: - monthmatrix[rangeID][month - 1] += 1 - date = date + datetime.timedelta(1) - return monthmatrix - - -def sumproduct(list1, list2): - return np.sum(m for m in np.multiply(np.array(list1), np.array(list2))) - - -# calculate monthly usage/charge with matrix, return a list -def monthly_average(daily, matrix): - month_average = [] - for month in range(1, 13): - matrixcolumn = list(map(lambda rangeID: matrix[rangeID][month - 1], range(len(daily)))) - total = sumproduct(daily, matrixcolumn) * days_in_month(month) / sum(matrixcolumn) - month_average.append(total) - return month_average - - -# oil price calculation -def cal_oil_price(oilcharge, oiluse): - if sum(oilcharge) == 0 or pd.isnull(sum(oilcharge)) == 1: - overall_price = 0 - else: - overall_price = sum(oilcharge) / sum(oiluse) - oil_price = [] - for i in range(1, 13): - oil_price.append(overall_price) - return oil_price - - -# list1=12 month values, target_terms = multiple years. Copy the values to all months -def duplicate2terms(list1, target_terms): - monthlist = range(1, 13) - dict1 = dict(zip(monthlist, list1)) - new_dict = dict(zip([], [])) - for term in target_terms: - new_dict[term] = dict1[term.month] - return [new_dict[term] for term in sorted(new_dict)] - - -# calculate max loan term and assign to total energy loan -def cal_max_term(loan_list): - max_length = 0 - max_ID = 0 - for loanID in range(len(loan_list)): - if len(loan_list[loanID].terms) >= max_length: - max_length = len(loan_list[loanID].terms) - max_ID = loanID - return loan_list[max_ID].terms - - -# calculate and assign cash flow for loans and for total energy loan -def align2term(terms1, list1, target_terms): - dict1 = dict(zip(terms1, list1)) - new_dict = dict(zip([], [])) - for term in target_terms: - if term in terms1: - new_dict[term] = dict1[term] - else: - new_dict[term] = 0 - return [new_dict[term] for term in sorted(new_dict)] - - -# calculate loan's (loan amount/debt service), just like a payback -def cal_loan_payback(interest, duration): - return ((1 + interest) ** duration - 1) / (interest * (1 + interest) ** duration) - - -# cauclate loan amount should be borrowed from each bank, given assumed total cost and loan info -def loan_allocate(total_cost, loan_list): - sum_loan_max = 0 - bound_list = [] - c_base = [] - for loan in loan_list: - sum_loan_max += loan.max_amount - c_base.append( - 1 / loan.payback) # loan amount / ratio = DS, targeting minimum total DS, equivalent to longest payback - bound_list.append((0, loan.max_amount)) - c = c_base # c_base is target function - A = [-1] * len(loan_list) - b = [max(0 - total_cost, 0 - sum_loan_max)] - bounds = bound_list - if total_cost > sum_loan_max: - print('alert: upfront cost > available loan amount') - res = linprog(c, A_ub=A, b_ub=b, bounds=bounds, options={'disp': False}) - for i in range(len(loan_list)): - loan_list[i].amount = res.x[i] - - -# return res.x - -##maybe we can use this, maybe not -# def transpose_matrix(matrix1): -# return [[r[col] for r in matrix1] for col in range(len(matrix1[0]))] - -def month_shift(month): - month_shift_dict = dict(zip([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1])) - return month_shift_dict[month] - - -####inflation calculation - -#product inflation rate from starting date to target date -def cal_inflation_product(inflation_rate_dict,target_date): - date_start=sorted(inflation_rate_dict)[0] -# product=1 -# for term in form_date_calendar(date_start,target_date)[1]: -# product = product * inflation_rate_dict[term] - product = np.prod(np.array([inflation_rate_dict[term] for term in form_date_calendar(date_start,target_date)[1]])) - return product - -def cal_previous_term(date): - first_date=datetime.date(date.year,date.month,1) - previous_term=first_date+datetime.timedelta(days=-1) - return previous_term - -##calculate inflation_coeff_dict. Inflation=0.02, inflation rate =1.02, inflation coeff=1.02 or 0.98, or 1.00... -# form inflation_rate_current_year -def form_inflation_ratedict(inflationyears,inflationhist): - hist_dict=dict(zip(inflationyears,inflationhist)) - inflationterms=form_bill_calendar(datetime.date(inflationyears[0],1,31),len(inflationyears))[1] - dict1={} - for term in inflationterms: - dict1[term]=hist_dict[term.year]/12+1 - return dict1 #return monthly inflation rates (2016,2,29 : 100.67%) - -def form_inflation_coeffdict(inflationyears,inflationhist,present_date): - inflation_rate_dict=form_inflation_ratedict(inflationyears,inflationhist) - inflationterms=sorted(inflation_rate_dict) - new_dict=dict(zip([],[])) - for term in inflationterms: - new_dict[term]=cal_inflation_product(inflation_rate_dict,term)/cal_inflation_product(inflation_rate_dict,present_date) - return new_dict #return (2016,2,29 : 99.86%; present_date: 100%) - -# form inflated items, based on a 12 month list, present time is the last month of 12 months list -def form_inflated_item(base_list,target_terms,inflation_coeff_dict,present_date): - pre_year_date=datetime.date(present_date.year-1,present_date.month+1,cal_last_day(present_date.year-1,present_date.month+1)) - base_terms=form_date_calendar(pre_year_date,present_date)[1] - for term in base_terms: - base_list[term.month-1]=base_list[term.month-1]/inflation_coeff_dict[term] - new_dict=dict(zip([],[])) - for term in target_terms: - new_dict[term]=base_list[term.month-1]*inflation_coeff_dict[term] - return [new_dict[term] for term in sorted(new_dict)] +# def average(list1): +# if len(list1) == 0: +# return 0 +# else: +# return sum(list1) / len(list1) + + +# def cal_CAGR(start_value, end_value, n): +# if start_value != 0: +# CAGR = (end_value / start_value) ** (1 / n) - 1 +# else: +# CAGR = 0 +# return CAGR + + +# # build the matrix to reflect #of days in each month +# def form_month_matrix(bdstart, bdend): +# monthmatrix = [[0 for i in range(12)] for j in range(len(bdstart))] +# # rangeID=0 +# for (fromdate, enddate, rangeID) in zip(bdstart, bdend, range(len(bdstart))): +# date = fromdate + datetime.timedelta(1) +# while date <= enddate: +# for month in range(1, 13): # range(1,13)=[1:12] +# if date.month == month: +# monthmatrix[rangeID][month - 1] += 1 +# date = date + datetime.timedelta(1) +# return monthmatrix + + +# def sumproduct(list1, list2): +# return np.sum(m for m in np.multiply(np.array(list1), np.array(list2))) + + +# # calculate monthly usage/charge with matrix, return a list +# def monthly_average(daily, matrix): +# month_average = [] +# for month in range(1, 13): +# matrixcolumn = list(map(lambda rangeID: matrix[rangeID][month - 1], range(len(daily)))) +# total = sumproduct(daily, matrixcolumn) * days_in_month(month) / sum(matrixcolumn) +# month_average.append(total) +# return month_average + + +# # oil price calculation +# def cal_oil_price(oilcharge, oiluse): +# if sum(oilcharge) == 0 or pd.isnull(sum(oilcharge)) == 1: +# overall_price = 0 +# else: +# overall_price = sum(oilcharge) / sum(oiluse) +# oil_price = [] +# for i in range(1, 13): +# oil_price.append(overall_price) +# return oil_price + + +# # list1=12 month values, target_terms = multiple years. Copy the values to all months +# def duplicate2terms(list1, target_terms): +# monthlist = range(1, 13) +# dict1 = dict(zip(monthlist, list1)) +# new_dict = dict(zip([], [])) +# for term in target_terms: +# new_dict[term] = dict1[term.month] +# return [new_dict[term] for term in sorted(new_dict)] + + +# # calculate max loan term and assign to total energy loan +# def cal_max_term(loan_list): +# max_length = 0 +# max_ID = 0 +# for loanID in range(len(loan_list)): +# if len(loan_list[loanID].terms) >= max_length: +# max_length = len(loan_list[loanID].terms) +# max_ID = loanID +# return loan_list[max_ID].terms + + +# # calculate and assign cash flow for loans and for total energy loan +# def align2term(terms1, list1, target_terms): +# dict1 = dict(zip(terms1, list1)) +# new_dict = dict(zip([], [])) +# for term in target_terms: +# if term in terms1: +# new_dict[term] = dict1[term] +# else: +# new_dict[term] = 0 +# return [new_dict[term] for term in sorted(new_dict)] + + +# # calculate loan's (loan amount/debt service), just like a payback +# def cal_loan_payback(interest, duration): +# return ((1 + interest) ** duration - 1) / (interest * (1 + interest) ** duration) + + +# # cauclate loan amount should be borrowed from each bank, given assumed total cost and loan info +# def loan_allocate(total_cost, loan_list): +# sum_loan_max = 0 +# bound_list = [] +# c_base = [] +# for loan in loan_list: +# sum_loan_max += loan.max_amount +# c_base.append( +# 1 / loan.payback) # loan amount / ratio = DS, targeting minimum total DS, equivalent to longest payback +# bound_list.append((0, loan.max_amount)) +# c = c_base # c_base is target function +# A = [-1] * len(loan_list) +# b = [max(0 - total_cost, 0 - sum_loan_max)] +# bounds = bound_list +# if total_cost > sum_loan_max: +# print('alert: upfront cost > available loan amount') +# res = linprog(c, A_ub=A, b_ub=b, bounds=bounds, options={'disp': False}) +# for i in range(len(loan_list)): +# loan_list[i].amount = res.x[i] + + +# # return res.x + +# ##maybe we can use this, maybe not +# # def transpose_matrix(matrix1): +# # return [[r[col] for r in matrix1] for col in range(len(matrix1[0]))] + +# def month_shift(month): +# month_shift_dict = dict(zip([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1])) +# return month_shift_dict[month] + + +# ####inflation calculation + +# #product inflation rate from starting date to target date +# def cal_inflation_product(inflation_rate_dict,target_date): +# date_start=sorted(inflation_rate_dict)[0] +# # product=1 +# # for term in form_date_calendar(date_start,target_date)[1]: +# # product = product * inflation_rate_dict[term] +# product = np.prod(np.array([inflation_rate_dict[term] for term in form_date_calendar(date_start,target_date)[1]])) +# return product + +# def cal_previous_term(date): +# first_date=datetime.date(date.year,date.month,1) +# previous_term=first_date+datetime.timedelta(days=-1) +# return previous_term + +# ##calculate inflation_coeff_dict. Inflation=0.02, inflation rate =1.02, inflation coeff=1.02 or 0.98, or 1.00... +# # form inflation_rate_current_year +# def form_inflation_ratedict(inflationyears,inflationhist): +# hist_dict=dict(zip(inflationyears,inflationhist)) +# inflationterms=form_bill_calendar(datetime.date(inflationyears[0],1,31),len(inflationyears))[1] +# dict1={} +# for term in inflationterms: +# dict1[term]=hist_dict[term.year]/12+1 +# return dict1 #return monthly inflation rates (2016,2,29 : 100.67%) + +# def form_inflation_coeffdict(inflationyears,inflationhist,present_date): +# inflation_rate_dict=form_inflation_ratedict(inflationyears,inflationhist) +# inflationterms=sorted(inflation_rate_dict) +# new_dict=dict(zip([],[])) +# for term in inflationterms: +# new_dict[term]=cal_inflation_product(inflation_rate_dict,term)/cal_inflation_product(inflation_rate_dict,present_date) +# return new_dict #return (2016,2,29 : 99.86%; present_date: 100%) + +# # form inflated items, based on a 12 month list, present time is the last month of 12 months list +# def form_inflated_item(base_list,target_terms,inflation_coeff_dict,present_date): +# pre_year_date=datetime.date(present_date.year-1,present_date.month+1,cal_last_day(present_date.year-1,present_date.month+1)) +# base_terms=form_date_calendar(pre_year_date,present_date)[1] +# for term in base_terms: +# base_list[term.month-1]=base_list[term.month-1]/inflation_coeff_dict[term] +# new_dict=dict(zip([],[])) +# for term in target_terms: +# new_dict[term]=base_list[term.month-1]*inflation_coeff_dict[term] +# return [new_dict[term] for term in sorted(new_dict)] ## define all classes # energy bill, including historical data, normalized bill on monthly basis diff --git a/bpfin/utilbills/bill_lib.py b/bpfin/utilbills/bill_lib.py index 8aa7522..d872fbe 100644 --- a/bpfin/utilbills/bill_lib.py +++ b/bpfin/utilbills/bill_lib.py @@ -3,6 +3,7 @@ import datetime import calendar + def add_list(obj_list, number): """Add a number to each value in a list. -- GitLab From 6eb9fa1bc23554e568dbc430071a86d0c9a6c6db Mon Sep 17 00:00:00 2001 From: chenzheng06 Date: Fri, 14 Apr 2017 15:00:14 -0400 Subject: [PATCH 5/7] added BPFINPATH, and imported other.py --- bpfin/lib/__init__.py | 0 bpfin/lib/other.py | 5 +++++ bpfin/utilbills/BPFINPATH | 0 bpfin/utilbills/bill_month_normalize_rough.py | 20 ++++++++++++------- 4 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 bpfin/lib/__init__.py create mode 100644 bpfin/utilbills/BPFINPATH diff --git a/bpfin/lib/__init__.py b/bpfin/lib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bpfin/lib/other.py b/bpfin/lib/other.py index b9a1588..6c42a9b 100644 --- a/bpfin/lib/other.py +++ b/bpfin/lib/other.py @@ -1,3 +1,6 @@ +import bpfin.lib.date +import bpfin.lib.math + import statsmodels.api as sm import datetime import calendar @@ -9,6 +12,8 @@ from scipy.optimize import linprog import matplotlib matplotlib.use('TkAgg') + + def add_list(obj_list, number): """Add a number to each value in a list. diff --git a/bpfin/utilbills/BPFINPATH b/bpfin/utilbills/BPFINPATH new file mode 100644 index 0000000..e69de29 diff --git a/bpfin/utilbills/bill_month_normalize_rough.py b/bpfin/utilbills/bill_month_normalize_rough.py index 4eb77ee..4649123 100644 --- a/bpfin/utilbills/bill_month_normalize_rough.py +++ b/bpfin/utilbills/bill_month_normalize_rough.py @@ -1,14 +1,18 @@ import datetime +# import sys +import bpfin.lib.other as lib + +# sys.path.append('C:/Users/zheng/Documents/BlocPower_Work_File/7. Python/') class Bill(): def __init__(self, raw_bill): - self.utility_type=raw_bill['utility_type'] - # self.billstart = convert_timestamp_date(Hist_Bill['From Date']) - # self.billend = convert_timestamp_date(Hist_Bill['To Date']) - # self.usage = Hist_Bill['Total Usage'] - # self.charge = Hist_Bill['Total Charge'] - # self.utility_type = utility_type + # self.utility_type = raw_bill['utility_type'] + self.billstart = lib.convert_timestamp_date(raw_bill['date_from']) + self.billend = lib.convert_timestamp_date(raw_bill['date_to']) + self.usage = raw_bill['usage'] + self.charge = raw_bill['charge'] + self.utility_type = raw_bill['utility_type'] # def hist(self): # if self.billstart != []: @@ -37,7 +41,9 @@ class Bill(): def bill_month_normalize_rough(raw_bill): hist_bill = Bill(raw_bill) month_normalized_bill = {} - month_normalized_bill['utility_type'] = we_want_type = hist_bill.utility_type + month_normalized_bill['utility_type'] = hist_bill.utility_type + print(bill_month_normalize_rough['utility_type']) + month_normalized_bill['usage'] = hist_bill.usage print("\n It goes ok") return month_normalized_bill -- GitLab From 3e41bc20aa957c0b9dbc56bddbeebb24382dde09 Mon Sep 17 00:00:00 2001 From: chenzheng06 Date: Fri, 14 Apr 2017 17:43:03 -0400 Subject: [PATCH 6/7] month_norm_rough developing --- bpfin/utilbills/bill_month_normalize_rough.py | 42 ++++++++++++------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/bpfin/utilbills/bill_month_normalize_rough.py b/bpfin/utilbills/bill_month_normalize_rough.py index 4649123..1b7e223 100644 --- a/bpfin/utilbills/bill_month_normalize_rough.py +++ b/bpfin/utilbills/bill_month_normalize_rough.py @@ -7,26 +7,36 @@ import bpfin.lib.other as lib class Bill(): def __init__(self, raw_bill): - # self.utility_type = raw_bill['utility_type'] self.billstart = lib.convert_timestamp_date(raw_bill['date_from']) self.billend = lib.convert_timestamp_date(raw_bill['date_to']) self.usage = raw_bill['usage'] self.charge = raw_bill['charge'] self.utility_type = raw_bill['utility_type'] - # def hist(self): - # if self.billstart != []: - # monthmatrix = form_month_matrix(self.billstart, self.billend) - # self.hist_daily_usage = division(self.usage, date_diff(self.billstart, self.billend)) - # self.hist_daily_charge = division(self.charge, date_diff(self.billstart, self.billend)) - # self.hist_monthly_usage = monthly_average(self.hist_daily_usage, monthmatrix) - # self.hist_monthly_charge = monthly_average(self.hist_daily_charge, monthmatrix) - # self.hist_monthly_price = division(self.hist_monthly_charge, self.hist_monthly_usage) - # else: - # self.hist_monthly_usage = [0] * 12 - # self.hist_monthly_charge = [0] * 12 - # self.hist_monthly_price = [0] * 12 - # return self + def hist(self): + if self.billstart != []: + monthmatrix = lib.form_month_matrix(self.billstart, self.billend) + # self.hist_daily_usage = lib.division(self.usage, lib.date_diff(self.billstart, self.billend)) + # self.hist_daily_charge = lib.division(self.charge, lib.date_diff(self.billstart, self.billend)) + # self.hist_monthly_usage = lib.monthly_average(self.hist_daily_usage, monthmatrix) + # self.hist_monthly_charge = lib.monthly_average(self.hist_daily_charge, monthmatrix) + # self.hist_monthly_price = lib.division(self.hist_monthly_charge, self.hist_monthly_usage) + + hist_daily_usage = lib.division(self.usage, lib.date_diff(self.billstart, self.billend)) + hist_daily_charge = lib.division(self.charge, lib.date_diff(self.billstart, self.billend)) + hist_monthly_usage = lib.monthly_average(hist_daily_usage, monthmatrix) + hist_monthly_charge = lib.monthly_average(hist_daily_charge, monthmatrix) + hist_monthly_price = lib.division(hist_monthly_charge, hist_monthly_usage) + else: + hist_monthly_usage = [0] * 12 + hist_monthly_charge = [0] * 12 + hist_monthly_price = [0] * 12 + + result_dict = { + 'monthly_usage': hist_monthly_usage, + 'monthly_charge': hist_monthly_charge, + 'monthly_price': hist_monthly_price} + return result_dict # def prior(self, FinancialDict): # [self.prior_billstart, self.prior_billend] = form_bill_calendar(FinancialDict['ProForma_Start'], @@ -39,13 +49,15 @@ class Bill(): def bill_month_normalize_rough(raw_bill): + print('\n let the normalization starts') hist_bill = Bill(raw_bill) + # print(hist_bill.utility_type) month_normalized_bill = {} month_normalized_bill['utility_type'] = hist_bill.utility_type - print(bill_month_normalize_rough['utility_type']) month_normalized_bill['usage'] = hist_bill.usage print("\n It goes ok") return month_normalized_bill + # return 1 # the following are tests ###################### test_raw_bill = {} -- GitLab From 35eab3b12a9f6943649de8c5c8cf8b3790f9c601 Mon Sep 17 00:00:00 2001 From: chenzheng06 Date: Sun, 16 Apr 2017 12:57:03 -0400 Subject: [PATCH 7/7] finished month_normalization_rough feature, created a test_month_normalize_rough, created test case (date) in test file, need to run test properly for next step --- bpfin/lib/other.py | 1 - bpfin/tests/test_utilbills/__init__.py | 0 .../test_month_normalize_rough.py | 39 ++++++ bpfin/utilbills/bill_month_normalize_rough.py | 132 ++++++++---------- 4 files changed, 95 insertions(+), 77 deletions(-) create mode 100644 bpfin/tests/test_utilbills/__init__.py diff --git a/bpfin/lib/other.py b/bpfin/lib/other.py index 6c42a9b..a2f081b 100644 --- a/bpfin/lib/other.py +++ b/bpfin/lib/other.py @@ -13,7 +13,6 @@ import matplotlib matplotlib.use('TkAgg') - def add_list(obj_list, number): """Add a number to each value in a list. diff --git a/bpfin/tests/test_utilbills/__init__.py b/bpfin/tests/test_utilbills/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bpfin/tests/test_utilbills/test_month_normalize_rough.py b/bpfin/tests/test_utilbills/test_month_normalize_rough.py index e69de29..63d7ee1 100644 --- a/bpfin/tests/test_utilbills/test_month_normalize_rough.py +++ b/bpfin/tests/test_utilbills/test_month_normalize_rough.py @@ -0,0 +1,39 @@ +import bpfin.utilbills.bill_month_normalize_rough as ub +import datetime + + +test_raw_bill = {} + +test_raw_bill['date_from'] = [ + datetime.date(2014, 4, 17), datetime.date(2014, 5, 16), datetime.date(2014, 6, 17), + datetime.date(2014, 7, 17), datetime.date(2014, 8, 15), datetime.date(2014, 9, 16), + datetime.date(2014, 10, 16), datetime.date(2014, 11, 14), datetime.date(2014, 12, 17), + datetime.date(2015, 1, 16), datetime.date(2015, 2, 18), datetime.date(2015, 3, 19), + datetime.date(2015, 4, 17), datetime.date(2015, 5, 18), datetime.date(2015, 6, 17), + datetime.date(2015, 7, 17), datetime.date(2015, 8, 17), datetime.date(2015, 9, 16), + datetime.date(2015, 10, 16), datetime.date(2015, 11, 16), datetime.date(2015, 12, 17), + datetime.date(2016, 1, 19), datetime.date(2016, 2, 18), datetime.date(2016, 3, 18)] + +test_raw_bill['date_to'] = [ + datetime.date(2014, 5, 16), datetime.date(2014, 6, 17), datetime.date(2014, 7, 17), + datetime.date(2014, 8, 15), datetime.date(2014, 9, 16), datetime.date(2014, 10, 16), + datetime.date(2014, 11, 14), datetime.date(2014, 12, 17), datetime.date(2015, 1, 16), + datetime.date(2015, 2, 18), datetime.date(2015, 3, 19), datetime.date(2015, 4, 17), + datetime.date(2015, 5, 18), datetime.date(2015, 6, 17), datetime.date(2015, 7, 17), + datetime.date(2015, 8, 17), datetime.date(2015, 9, 16), datetime.date(2015, 10, 16), + datetime.date(2015, 11, 16), datetime.date(2015, 12, 17), datetime.date(2016, 1, 19), + datetime.date(2016, 2, 18), datetime.date(2016, 3, 18), datetime.date(2016, 4, 18)] + +test_raw_bill['charge'] = [ + 2601.6289696969693, 2642.1740261508003, 3437.551946607342, 2930.5070588449826, + 2514.1242835094554, 2804.4448749999997, 3237.7359972191325, 3289.4155027808674, + 2796.5700000000002, 2463.5113047830923, 2370.0007392051775, 2479.081440860215] + +test_raw_bill['usage'] = [ + 0.094265231101663627, 0.10175446346513953, 0.12248981352003527, 0.11229655777596657, + 0.092170964664193167, 0.094173672324921492, 0.095187070309577237, 0.097177529967719486, + 0.095676831493476874, 0.091583582602530869, 0.089122740090923164, 0.091516956021057888] + +test_raw_bill['utility_type'] = 'electricity' + +print(ub.bill_month_normalize_rough(test_raw_bill)) diff --git a/bpfin/utilbills/bill_month_normalize_rough.py b/bpfin/utilbills/bill_month_normalize_rough.py index 1b7e223..07d0772 100644 --- a/bpfin/utilbills/bill_month_normalize_rough.py +++ b/bpfin/utilbills/bill_month_normalize_rough.py @@ -5,38 +5,38 @@ import bpfin.lib.other as lib # sys.path.append('C:/Users/zheng/Documents/BlocPower_Work_File/7. Python/') -class Bill(): - def __init__(self, raw_bill): - self.billstart = lib.convert_timestamp_date(raw_bill['date_from']) - self.billend = lib.convert_timestamp_date(raw_bill['date_to']) - self.usage = raw_bill['usage'] - self.charge = raw_bill['charge'] - self.utility_type = raw_bill['utility_type'] - - def hist(self): - if self.billstart != []: - monthmatrix = lib.form_month_matrix(self.billstart, self.billend) - # self.hist_daily_usage = lib.division(self.usage, lib.date_diff(self.billstart, self.billend)) - # self.hist_daily_charge = lib.division(self.charge, lib.date_diff(self.billstart, self.billend)) - # self.hist_monthly_usage = lib.monthly_average(self.hist_daily_usage, monthmatrix) - # self.hist_monthly_charge = lib.monthly_average(self.hist_daily_charge, monthmatrix) - # self.hist_monthly_price = lib.division(self.hist_monthly_charge, self.hist_monthly_usage) - - hist_daily_usage = lib.division(self.usage, lib.date_diff(self.billstart, self.billend)) - hist_daily_charge = lib.division(self.charge, lib.date_diff(self.billstart, self.billend)) - hist_monthly_usage = lib.monthly_average(hist_daily_usage, monthmatrix) - hist_monthly_charge = lib.monthly_average(hist_daily_charge, monthmatrix) - hist_monthly_price = lib.division(hist_monthly_charge, hist_monthly_usage) - else: - hist_monthly_usage = [0] * 12 - hist_monthly_charge = [0] * 12 - hist_monthly_price = [0] * 12 - - result_dict = { - 'monthly_usage': hist_monthly_usage, - 'monthly_charge': hist_monthly_charge, - 'monthly_price': hist_monthly_price} - return result_dict +# class Bill(): +# def __init__(self, raw_bill): +# self.billstart = lib.convert_timestamp_date(raw_bill['date_from']) +# self.billend = lib.convert_timestamp_date(raw_bill['date_to']) +# self.usage = raw_bill['usage'] +# self.charge = raw_bill['charge'] +# self.utility_type = raw_bill['utility_type'] + +# def normalize_rough(self): +# if self.billstart != []: +# monthmatrix = lib.form_month_matrix(self.billstart, self.billend) +# # self.hist_daily_usage = lib.division(self.usage, lib.date_diff(self.billstart, self.billend)) +# # self.hist_daily_charge = lib.division(self.charge, lib.date_diff(self.billstart, self.billend)) +# # self.hist_monthly_usage = lib.monthly_average(self.hist_daily_usage, monthmatrix) +# # self.hist_monthly_charge = lib.monthly_average(self.hist_daily_charge, monthmatrix) +# # self.hist_monthly_price = lib.division(self.hist_monthly_charge, self.hist_monthly_usage) + +# daily_usage = lib.division(self.usage, lib.date_diff(self.billstart, self.billend)) +# daily_charge = lib.division(self.charge, lib.date_diff(self.billstart, self.billend)) +# month_norm_usage = lib.monthly_average(daily_usage, monthmatrix) +# month_norm_charge = lib.monthly_average(daily_charge, monthmatrix) +# month_norm_price = lib.division(month_norm_charge, month_norm_usage) +# else: +# month_norm_usage = [0] * 12 +# month_norm_charge = [0] * 12 +# month_norm_price = [0] * 12 + +# result_dict = { +# 'monthly_usage': month_norm_usage, +# 'monthly_charge': month_norm_charge, +# 'monthly_price': month_norm_price} +# return result_dict # def prior(self, FinancialDict): # [self.prior_billstart, self.prior_billend] = form_bill_calendar(FinancialDict['ProForma_Start'], @@ -50,48 +50,28 @@ class Bill(): def bill_month_normalize_rough(raw_bill): print('\n let the normalization starts') - hist_bill = Bill(raw_bill) - # print(hist_bill.utility_type) - month_normalized_bill = {} - month_normalized_bill['utility_type'] = hist_bill.utility_type - month_normalized_bill['usage'] = hist_bill.usage - print("\n It goes ok") - return month_normalized_bill - # return 1 - -# the following are tests ###################### -test_raw_bill = {} - -test_raw_bill['date_from'] = [ - datetime.date(2014, 4, 17), datetime.date(2014, 5, 16), datetime.date(2014, 6, 17), - datetime.date(2014, 7, 17), datetime.date(2014, 8, 15), datetime.date(2014, 9, 16), - datetime.date(2014, 10, 16), datetime.date(2014, 11, 14), datetime.date(2014, 12, 17), - datetime.date(2015, 1, 16), datetime.date(2015, 2, 18), datetime.date(2015, 3, 19), - datetime.date(2015, 4, 17), datetime.date(2015, 5, 18), datetime.date(2015, 6, 17), - datetime.date(2015, 7, 17), datetime.date(2015, 8, 17), datetime.date(2015, 9, 16), - datetime.date(2015, 10, 16), datetime.date(2015, 11, 16), datetime.date(2015, 12, 17), - datetime.date(2016, 1, 19), datetime.date(2016, 2, 18), datetime.date(2016, 3, 18)] + print(raw_bill) + + billstart = lib.convert_timestamp_date(raw_bill['date_from']) + billend = lib.convert_timestamp_date(raw_bill['date_to']) + usage = raw_bill['usage'] + charge = raw_bill['charge'] + utility_type = raw_bill['utility_type'] + + if billstart != []: + monthmatrix = lib.form_month_matrix(billstart, billend) + daily_usage = lib.division(usage, lib.date_diff(billstart, billend)) + daily_charge = lib.division(charge, lib.date_diff(billstart, billend)) + + month_norm_usage = lib.monthly_average(daily_usage, monthmatrix) + month_norm_charge = lib.monthly_average(daily_charge, monthmatrix) + month_norm_price = lib.division(month_norm_charge, month_norm_usage) + else: + month_norm_usage = [0] * 12 + month_norm_charge = [0] * 12 + month_norm_price = [0] * 12 -test_raw_bill['date_to'] = [ - datetime.date(2014, 5, 16), datetime.date(2014, 6, 17), datetime.date(2014, 7, 17), - datetime.date(2014, 8, 15), datetime.date(2014, 9, 16), datetime.date(2014, 10, 16), - datetime.date(2014, 11, 14), datetime.date(2014, 12, 17), datetime.date(2015, 1, 16), - datetime.date(2015, 2, 18), datetime.date(2015, 3, 19), datetime.date(2015, 4, 17), - datetime.date(2015, 5, 18), datetime.date(2015, 6, 17), datetime.date(2015, 7, 17), - datetime.date(2015, 8, 17), datetime.date(2015, 9, 16), datetime.date(2015, 10, 16), - datetime.date(2015, 11, 16), datetime.date(2015, 12, 17), datetime.date(2016, 1, 19), - datetime.date(2016, 2, 18), datetime.date(2016, 3, 18), datetime.date(2016, 4, 18)] - -test_raw_bill['charge'] = [ - 2601.6289696969693, 2642.1740261508003, 3437.551946607342, 2930.5070588449826, - 2514.1242835094554, 2804.4448749999997, 3237.7359972191325, 3289.4155027808674, - 2796.5700000000002, 2463.5113047830923, 2370.0007392051775, 2479.081440860215] - -test_raw_bill['usage'] = [ - 0.094265231101663627, 0.10175446346513953, 0.12248981352003527, 0.11229655777596657, - 0.092170964664193167, 0.094173672324921492, 0.095187070309577237, 0.097177529967719486, - 0.095676831493476874, 0.091583582602530869, 0.089122740090923164, 0.091516956021057888] - -test_raw_bill['utility_type'] = 'electricity' - -print(bill_month_normalize_rough(test_raw_bill)) + print("\n It goes ok") + return {'monthly_usage': month_norm_usage, + 'monthly_charge': month_norm_charge, + 'monthly_price': month_norm_price} -- GitLab