Plotting Charts
Akumen can return charts, plotted through Python libraries such as MatPlotLib. By adding the following code to your Python coffee shop application you will be able to view the results of the model once the scenarios have been run. Add the following code to the bottom of the Python application, just before the return{}.
# Images can be viewed through the Research Grid, if they are saved to outputs.
plt = monthly_incomes.plot(x='date', y='monthly_incomes')
fig = plt.get_figure()
fig.savefig('outputs/monthly_income.png')
plt = monthly_balances.plot(x='date', y='monthly_balances')
fig = plt.get_figure()
fig.savefig('outputs/monthly_balances.png')
Once you have added the code and saved your changes, rerun your model to produce new data. As soon as the scenarios have run and returned complete you can view the charts in a few different ways:
- Access the build tab, and click on the Images icon located above the log; or
- Access the research tab, right click on an executed scenario and select Images from the options list.
The completed main.py file should appear as follows:
from pandas import DataFrame
from dateutil import parser, relativedelta
from random import gauss
def akumen(start_day, periods,
cost_per_cup, price_per_cup, demand_noise, fract_of_month_operating,
shop_rent, jacks_salary, start_capital,
**kwargs):
"""
Parameters:
- Input: start_day [datetime]
- Input: periods [float]
Operational Income:
- Input: cost_per_cup [float]
- Input: price_per_cup [float]
- Input: demand_noise [float]
- Input: fract_of_month_operating [tabular] (xlsx)
Fix costs and Accounting:
- Input: shop_rent [float]
- Input: jacks_salary [float]
- Input: start_capital [float]
Output data:
- Output: monthly_incomes [file] (monthly_incomes.csv)
- Output: monthly_balances [file] (monthly_balances.csv)
"""
# Initialize model run variables
fract_of_month_operating = fract_of_month_operating.get('Sheet1')
date = parser.parse(start_day)
time_step = relativedelta.relativedelta(months=1)
account_balance = start_capital
monthly_incomes = []
monthly_balances = []
# Iterate over time periods
for i in range(int(periods)):
# Calculate operational income
profit_per_cup = price_per_cup - cost_per_cup
demand = 3500 - 100 * price_per_cup**2 - gauss(0, demand_noise)
income = demand * profit_per_cup * fract_of_month_operating.iloc[0, i%12]
# Calculate operational expenses
fix_costs = jacks_salary + shop_rent
# Calculate cash flow and account balance
cash_flow = income - fix_costs
account_balance = account_balance + cash_flow
# Store values for output reporting
monthly_incomes.append([date, income])
monthly_balances.append([date, account_balance])
# Advance model time
date = date + time_step
# Prepare output data
monthly_incomes = DataFrame(monthly_incomes, columns=['date', 'monthly_incomes'])
monthly_balances = DataFrame(monthly_balances, columns=['date', 'monthly_balances'])
# Images can be viewed through the Research Grid, if they are saved to outputs.
plt = monthly_incomes.plot(x='date', y='monthly_incomes')
fig = plt.get_figure()
fig.savefig('outputs/monthly_income.png')
plt = monthly_balances.plot(x='date', y='monthly_balances')
fig = plt.get_figure()
fig.savefig('outputs/monthly_balances.png')
return {
'monthly_incomes': monthly_incomes,
'monthly_balances': monthly_balances
}