From edd05a0f5816d65233e5f87c045a19941003b6cc Mon Sep 17 00:00:00 2001 From: Sarey Hamarneh Date: Mon, 17 Apr 2017 15:41:35 -0400 Subject: [PATCH 1/2] Create tests for individual functions in utilbills files --- .ipynb_checkpoints/Untitled-checkpoint.ipynb | 6 + Untitled.ipynb | 170 ++++++++++++++++++ .../test_financials/__init__.py} | 0 bpfin/tests/test_utilbills/test_bill_lib.py | 10 +- .../test_utilbills/test_bill_normalize.py | 88 +++++++++ .../test_utilbills/test_bill_proj_charge.py | 86 +++++++++ .../test_utilbills/test_bill_proj_reg.py | 13 ++ bpfin/utilbills/bill_normalize.py | 1 + 8 files changed, 368 insertions(+), 6 deletions(-) create mode 100644 .ipynb_checkpoints/Untitled-checkpoint.ipynb create mode 100644 Untitled.ipynb rename bpfin/{utilbills/BPFINPATH => tests/test_financials/__init__.py} (100%) diff --git a/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/.ipynb_checkpoints/Untitled-checkpoint.ipynb new file mode 100644 index 0000000..2fd6442 --- /dev/null +++ b/.ipynb_checkpoints/Untitled-checkpoint.ipynb @@ -0,0 +1,6 @@ +{ + "cells": [], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Untitled.ipynb b/Untitled.ipynb new file mode 100644 index 0000000..d6402ec --- /dev/null +++ b/Untitled.ipynb @@ -0,0 +1,170 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 66, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "from datetime import date\n", + "import numpy as np\n", + "import statsmodels.api as sm\n", + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "def daily_average_use_or_charge(from_date, to_date, total_utility_use_or_charge):\n", + " fromdate = []\n", + " todate = []\n", + " for date in from_date:\n", + " fromdate.append(date)\n", + " for date in to_date:\n", + " todate.append(date)\n", + " interstore = [list(x) for x in zip(fromdate, todate)]\n", + " finalstore = []\n", + " for value in interstore:\n", + " difference = (value[1] - value[0])\n", + " finalstore.append(difference.days)\n", + "\n", + " bill_daily_average = [x / y for x, y in zip(total_utility_use_or_charge, finalstore)]\n", + " perbill_useorcharge = []\n", + " for number in bill_daily_average:\n", + " perbill_useorcharge.append(number)\n", + " return perbill_useorcharge" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "input_from_date = [date(1995, 10, 1), date(1996, 11, 1), date(1997, 12, 1)]\n", + "input_to_date = [date(1996, 10, 1), date(1997, 11, 1), date(1998, 12, 1)]\n", + "input_total_utility_use_or_charge = [10, 10, 10]" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[0.0273224043715847, 0.0273972602739726, 0.0273972602739726]" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "daily_average_use_or_charge(input_from_date, input_to_date, input_total_utility_use_or_charge)" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def regression_coefficients(x_list, y_list, is_intercept):\n", + "# \"\"\"Regress two lists of values.\n", + "\n", + "# Args:\n", + "# x_list: list of base x values for regression\n", + "# y_list: list of base y values for regression\n", + "# is_intercept (boolean): it is true that we want an intercept value in our regression\n", + "# Returns:\n", + "# tuple: x and y coefficients from regression\n", + "# \"\"\"\n", + " x_values = x_list\n", + " y_values = y_list\n", + " if is_intercept is True:\n", + " x_values = sm.add_constant(x_values)\n", + " model = sm.OLS(y_values, x_values)\n", + " results = model.fit()\n", + " coefficients = results.params\n", + " return coefficients" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "array([ -3.77475828e-15, 2.00000000e+00])" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "regression_coefficients([15,30,45],[30,60,90],True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/bpfin/utilbills/BPFINPATH b/bpfin/tests/test_financials/__init__.py similarity index 100% rename from bpfin/utilbills/BPFINPATH rename to bpfin/tests/test_financials/__init__.py diff --git a/bpfin/tests/test_utilbills/test_bill_lib.py b/bpfin/tests/test_utilbills/test_bill_lib.py index a5c6aec..8535bdc 100644 --- a/bpfin/tests/test_utilbills/test_bill_lib.py +++ b/bpfin/tests/test_utilbills/test_bill_lib.py @@ -1,12 +1,10 @@ -from bpfin.utilbills.0bill_lib import add_list +from bpfin.utilbills.bill_lib import add_list, form_inflation_ratedict def test_add_list(): - input = [1, 2] + input_list = [1, 2] output = [3, 4] - result = add_list(input, 2) + result = add_list(input_list, 2) - for i in result: + for i in range(len(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 index e69de29..2fc53db 100644 --- a/bpfin/tests/test_utilbills/test_bill_normalize.py +++ b/bpfin/tests/test_utilbills/test_bill_normalize.py @@ -0,0 +1,88 @@ +from datetime import date +from bpfin.utilbills.bill_normalize import daily_average_use_or_charge + + +def test_daily_average_use_or_charge(): + input_from_date = [date(1995, 10, 1), date(1996, 11, 1), date(1997, 12, 1)] + input_to_date = [date(1996, 10, 1), date(1997, 11, 1), date(1998, 12, 1)] + input_total_utility_use_or_charge = [10, 10, 10] + output = [0.027, 0.027, 0.027] + result = daily_average_use_or_charge(input_from_date, input_to_date, input_total_utility_use_or_charge) + +# 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/tests/test_utilbills/test_bill_proj_charge.py b/bpfin/tests/test_utilbills/test_bill_proj_charge.py index e69de29..b2fbac5 100644 --- a/bpfin/tests/test_utilbills/test_bill_proj_charge.py +++ b/bpfin/tests/test_utilbills/test_bill_proj_charge.py @@ -0,0 +1,86 @@ +from datetime import date +from bpfin.utilbills.bill_proj_charge import price_x_consumption, utility_charge_sum, charge_dictionary +from bpfin.utilbills.bill_proj_charge import hist_oil_price, monthly_price + + +def test_price_x_consumption(): + input_price = [1, 2, 3] + input_con_list = [10, 20, 30] + output = [10, 40, 90] + result = price_x_consumption(input_price, input_con_list) + + for value in output: + for res in result: + value = res + + +def test_utility_charge_sum(): + input_list1 = [1, 2, 3] + input_list2 = [2, 3, 4] + input_list3 = [3, 4, 5] + output = [6, 9, 12] + result = utility_charge_sum(input_list1, input_list2, input_list3) + + for value in output: + for res in result: + value = res + + +def test_charge_dictionary(): + input_billperiod = [date(1995, 10, 1), date(1996, 11, 1), date(1997, 12, 1)] + input_charge_list = [20, 30, 40] + output = [(date(1995, 10, 1), 20), (date(1996, 11, 1), 30), (date(1997, 12, 1), 40)] + result = charge_dictionary(input_billperiod, input_charge_list) + + for value in output: + for res in result: + value = res + + +# 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): dictionary 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 test_hist_oil_price(): + input_oilcharge = [2, 2, 2] + input_oiluse = [10, 10, 10] + output = [5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5] + result = hist_oil_price(input_oilcharge, input_oiluse) + + for value in output: + for res in result: + value = res + + +def test_monthly_price(): + input_charge = [10, 4, 2] + input_use = [5, 2, 2] + output = [2, 2, 1] + result = monthly_price(input_charge, input_use) + + for value in output: + for res in result: + value = res diff --git a/bpfin/tests/test_utilbills/test_bill_proj_reg.py b/bpfin/tests/test_utilbills/test_bill_proj_reg.py index e69de29..7301e9e 100644 --- a/bpfin/tests/test_utilbills/test_bill_proj_reg.py +++ b/bpfin/tests/test_utilbills/test_bill_proj_reg.py @@ -0,0 +1,13 @@ +from bpfin.utilbills.bill_proj_reg import regression_coefficients, regression_predicting_y + + +def test_regression_coefficients(): + input_x = [15, 30, 45] + input_y = [30, 60, 90] + is_true = True + output = [0, 2] + result = regression_coefficients(input_x, input_y, is_true) + + for number in output: + for coef in result: + number = coef diff --git a/bpfin/utilbills/bill_normalize.py b/bpfin/utilbills/bill_normalize.py index e7d365e..537f8a7 100644 --- a/bpfin/utilbills/bill_normalize.py +++ b/bpfin/utilbills/bill_normalize.py @@ -2,6 +2,7 @@ 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. -- GitLab From ce8ae545b5da9a1707e7667114d557428adf41b0 Mon Sep 17 00:00:00 2001 From: Sarey Hamarneh Date: Wed, 19 Apr 2017 18:34:55 -0400 Subject: [PATCH 2/2] Util bill test corrections --- .ipynb_checkpoints/Untitled-checkpoint.ipynb | 6 - Untitled.ipynb | 170 ------------------- bpfin/tests/test_utilbills/test_bill_lib.py | 4 - 3 files changed, 180 deletions(-) delete mode 100644 .ipynb_checkpoints/Untitled-checkpoint.ipynb delete mode 100644 Untitled.ipynb diff --git a/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/.ipynb_checkpoints/Untitled-checkpoint.ipynb deleted file mode 100644 index 2fd6442..0000000 --- a/.ipynb_checkpoints/Untitled-checkpoint.ipynb +++ /dev/null @@ -1,6 +0,0 @@ -{ - "cells": [], - "metadata": {}, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/Untitled.ipynb b/Untitled.ipynb deleted file mode 100644 index d6402ec..0000000 --- a/Untitled.ipynb +++ /dev/null @@ -1,170 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 66, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "from datetime import date\n", - "import numpy as np\n", - "import statsmodels.api as sm\n", - "import pandas as pd" - ] - }, - { - "cell_type": "code", - "execution_count": 69, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "def daily_average_use_or_charge(from_date, to_date, total_utility_use_or_charge):\n", - " fromdate = []\n", - " todate = []\n", - " for date in from_date:\n", - " fromdate.append(date)\n", - " for date in to_date:\n", - " todate.append(date)\n", - " interstore = [list(x) for x in zip(fromdate, todate)]\n", - " finalstore = []\n", - " for value in interstore:\n", - " difference = (value[1] - value[0])\n", - " finalstore.append(difference.days)\n", - "\n", - " bill_daily_average = [x / y for x, y in zip(total_utility_use_or_charge, finalstore)]\n", - " perbill_useorcharge = []\n", - " for number in bill_daily_average:\n", - " perbill_useorcharge.append(number)\n", - " return perbill_useorcharge" - ] - }, - { - "cell_type": "code", - "execution_count": 73, - "metadata": { - "collapsed": false - }, - "outputs": [], - "source": [ - "input_from_date = [date(1995, 10, 1), date(1996, 11, 1), date(1997, 12, 1)]\n", - "input_to_date = [date(1996, 10, 1), date(1997, 11, 1), date(1998, 12, 1)]\n", - "input_total_utility_use_or_charge = [10, 10, 10]" - ] - }, - { - "cell_type": "code", - "execution_count": 74, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[0.0273224043715847, 0.0273972602739726, 0.0273972602739726]" - ] - }, - "execution_count": 74, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "daily_average_use_or_charge(input_from_date, input_to_date, input_total_utility_use_or_charge)" - ] - }, - { - "cell_type": "code", - "execution_count": 70, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "def regression_coefficients(x_list, y_list, is_intercept):\n", - "# \"\"\"Regress two lists of values.\n", - "\n", - "# Args:\n", - "# x_list: list of base x values for regression\n", - "# y_list: list of base y values for regression\n", - "# is_intercept (boolean): it is true that we want an intercept value in our regression\n", - "# Returns:\n", - "# tuple: x and y coefficients from regression\n", - "# \"\"\"\n", - " x_values = x_list\n", - " y_values = y_list\n", - " if is_intercept is True:\n", - " x_values = sm.add_constant(x_values)\n", - " model = sm.OLS(y_values, x_values)\n", - " results = model.fit()\n", - " coefficients = results.params\n", - " return coefficients" - ] - }, - { - "cell_type": "code", - "execution_count": 65, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "data": { - "text/plain": [ - "array([ -3.77475828e-15, 2.00000000e+00])" - ] - }, - "execution_count": 65, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "regression_coefficients([15,30,45],[30,60,90],True)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.6.0" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} diff --git a/bpfin/tests/test_utilbills/test_bill_lib.py b/bpfin/tests/test_utilbills/test_bill_lib.py index f56dfb0..32670f1 100644 --- a/bpfin/tests/test_utilbills/test_bill_lib.py +++ b/bpfin/tests/test_utilbills/test_bill_lib.py @@ -1,8 +1,4 @@ -<<<<<<< HEAD -from bpfin.utilbills.bill_lib import add_list, form_inflation_ratedict -======= from bpfin.utilbills.bill_lib import add_list ->>>>>>> 351e9e58b4c42c3145f55e5ae023ec0f23b45018 def test_add_list(): -- GitLab