ReportFunctions module¶
This module contains function which are used for three main jobs:
General jobs including error checking and reading and writing output files.
‘within trial’ calculations. These functions manipulate the output variables from the LP and the values from the precalcs. These functions all return a data array.
‘between trial’ calculations. These functions summarise the data array from the ‘within trial’ functions for all the trials in an experiment. These functions can return table or figures.
Note
This module should not import inputs (in case the inputs are adjusted during the exp so they will not be correct for r_vals)
Tip
There are cases where the report requires more detail than the decision variable returned from the lp contains. For example some livestock reports (e.g. NV) need an e b and p axis but the clustering has removed this level of detail in the lp variables. To handle this we make the corresponding r_vals array with both the detailed and clustered axis.
author: young
- AfoLogic.ReportFunctions.f_add_axis(prod, na_prod, prod_weights, na_prodweights, weights, na_weights, den_weights, na_denweights)¶
Adds new axis if required.
- Parameters:
weights – array
na_weights – list: position to add new axis
prod – array
na_prod – list: position to add new axis
- Returns:
expanded array
- AfoLogic.ReportFunctions.f_append_dfs(stacked_df, additional_df)¶
- AfoLogic.ReportFunctions.f_area_summary(lp_vars, r_vals, option)¶
Rotation & landuse area summary. With multiple output levels. return options:
- Parameters:
lp_vars – dict
r_vals – dict
- Key option:
table all rotations by lmu
total pasture area each season in p7[-1]
total crop area each season in p7[-1]
table crop and pasture area by lmu and season
landuse area in p7[-1]
float pasture %, max, min & stdev in p7[-1]
float cereal %, max, min & stdev in p7[-1]
float canola %, max, min & stdev in p7[-1]
table all rotations by lmu with disagregated land uses as index
- AfoLogic.ReportFunctions.f_arith(prod, prod_weights, weight, den_weights, arith, axis)¶
option 0: return production param averaged across all axis that are not reported. option 1: return weighted average of production param (using denominator weight returns production per day the animal is on hand) option 2: weighted total production summed across all axis that are not reported. option 3: weighted total production for each axis (axis not reported are disregarded) option 4: return weighted average of production param using prod>0 as the weights option 5: return the maximum value across all axis that are not reported.
- Parameters:
prod – array: production param
prod_weight – array: weights the production param
weight – array: weights (typically the variable associated with the prod param)
den_weight – array: weights the denominator in the weighted average calculation
arith – int: arith option
axis – list: axes to perform arith along
- Returns:
array
- AfoLogic.ReportFunctions.f_asset_cost_summary(lp_vars, r_vals)¶
- AfoLogic.ReportFunctions.f_available_cropgrazing(r_vals)¶
Calculates the total crop that CAN be grazed based on seeding timing (the actual amount consumed is optimised).
- AfoLogic.ReportFunctions.f_biomass_penalty(lp_vars, r_vals)¶
- AfoLogic.ReportFunctions.f_crop_summary(lp_vars, r_vals, option=0)¶
Crop summary. Includes pasture inputs. :param option:
Return - tuple: fert cost, chem cost, miscellaneous costs and grain revenue for each landuse
- AfoLogic.ReportFunctions.f_dep_summary(lp_vars, r_vals)¶
- AfoLogic.ReportFunctions.f_df2xl(writer, df, sheet, df_settings=None, rowstart=0, colstart=0, option=0)¶
Pandas to excel. https://xlsxwriter.readthedocs.io/working_with_pandas.html
You can simply stick a dataframe from pandas into Excel using df.to_excel() function. for this you can specify the workbook the sheet and the start row or col (so you can put multiple dfs in one sheet)
The next level involves interacting with xlsxwriter. This allows you to do custom things like creating graphs, hiding rows/cols, filtering or grouping.
- Parameters:
writer – writer used. controls the workbook being writen to.
df – dataframe going to excel
sheet – str: sheet name.
df_settings – df: df to store number of row and col indexes.
rowstart – start row in Excel
colstart – start col in Excel
option – int: specifying the writing option 0: df straight into Excel 1: df into Excel collapsing empty rows and cols (using the group function is xl - ie the rows/cols are still there they are just minimised) 2: df into Excel removing empty rows and cols (the rows/cols are completely removed)
- AfoLogic.ReportFunctions.f_dse(lp_vars, r_vals, method, per_ha, summary1=False, summary2=False, summary3=False)¶
DSE calculation.
- Parameters:
lp_vars – dict: results from pyomo
r_vals – dict: report variable
method –
int
dse by normal weight
dse by mei
per_ha – Bool if true it returns DSE/ha else it returns total dse
summary1 – Bool if true it returns the total expected DSE/ha in winter. Used in the summary report
summary2 – Bool if true it returns the total numbers at the start and end of the season with qsz axis. Used in numbers summary report.
summary1 – Bool if true it returns the total DSE/ha in winter for each season.
- Returns:
DSE per pasture hectare for each sheep group.
- AfoLogic.ReportFunctions.f_errors(trial_outdated, trials)¶
The report module conducts three error checks before commencing:
Have you run the main model for each trial you are trying to report. If not a warning will be printed and the given trial will be removed from the list of trials to report (allowing the remaining trials to still be reported).
Are any trials out of date E.g. have you run the main model since updating the inputs or the code. If trials are out of date a warning message will be printed but the report code will continue to execute.
Did all the trials you are reporting solve optimally. Infeasible trials are still reported the reports are just filled with 0’s. A list of infeasible trials will be printed to the console and reported in the final Report.xlsx document.
- Parameters:
trial_outdated – boolean list of all trials stating if trial is out of date.
trials – list of trials being run
- Returns:
None.
- AfoLogic.ReportFunctions.f_feed_budget(lp_vars, r_vals, option=0, nv_option=0, dams_cols=[], offs_cols=[])¶
Feed budget: stock mei requirement and feed mei supply.
Reported axes for stock can vary.
- Parameters:
lp_vars – dict: results from pyomo
r_vals – dict: report variable
option – (optional, default = 0): int: option 0: mei/hd/day & propn of mei from each feed source option 1: total mei / day
nv_option – (optional, default = 0): int: option 0: Active NV pool option 1: NV pool summed (not active)
dams_cols – (optional, default = []): list: axis you want as the cols of pandas df (order of list is the col level order).
offs_cols – (optional, default = []): list: axis you want as the cols of pandas df (order of list is the col level order).
- Returns:
pandas df
- AfoLogic.ReportFunctions.f_grain_sup_summary(lp_vars, r_vals, option=0)¶
Summary of grain, supplement and their costs
- Parameters:
option –
int:
return dict with sup cost
return total supplement fed in each feed period
return total of each grain supplement fed in each feed period in each season
return total of each grain supplement fed in each feed period for each feed pool in each season
return total sup fed (weighted by season prob)
- AfoLogic.ReportFunctions.f_grazing_summary(lp_vars, r_vals)¶
Green pasture grazing summary :return: data table with zp6lo as index and g and column. Containing the hectares of each activity, average foo and starting foo.
- AfoLogic.ReportFunctions.f_labour_summary(lp_vars, r_vals, option=0)¶
- Parameters:
option –
return total labour cost
return amount for each enterprise
- AfoLogic.ReportFunctions.f_lambing_status(lp_vars, r_vals, option=0, keys=None, index=[], cols=[], axis_slice={}, lp_vars_inc=True)¶
- Depending on the option selected this function can calc:
Lamb survival (per ewe at start of dvp when lambing occurs - e.g. mort is included) Weaning % (per dam at the start of the dvp when mating occurs - e.g. mort is included) Scanning % Proportion of dry ewes
- Parameters:
lp_vars – dict: results from pyomo
r_vals – dict: report variable
option – (optional, default = 0): int: option 0: survival % option 1: wean % option 2: scan % option 3: Proportion of dry ewes
keys –
index – (optional, default = []): list: axis you want as the index of pandas df (order of list is the index level order).
cols – (optional, default = []): list: axis you want as the cols of pandas df (order of list is the col level order).
axis_slice – (optional, default = {}): dict: keys (int) is the axis. value (list) is the start, stop and step of the slice
lp_vars_inc – weight the report using the results stored in lp_vars. If false, the result is as if single animal in all slices
- Returns:
pandas df
- AfoLogic.ReportFunctions.f_mach_summary(lp_vars, r_vals, option=0)¶
Machine summary. :param option:
table: total machine cost for each crop in each cash period
table: total seeding biomass penalty for untimely sowing.
- AfoLogic.ReportFunctions.f_minroe_summary(lp_vars, r_vals)¶
- AfoLogic.ReportFunctions.f_mvf_summary(lp_vars)¶
- AfoLogic.ReportFunctions.f_numpy2df(prod, keys, index, cols)¶
- AfoLogic.ReportFunctions.f_numpy2df_error(prod, weights, arith_axis, index, cols)¶
- AfoLogic.ReportFunctions.f_overhead_summary(r_vals)¶
- AfoLogic.ReportFunctions.f_price_summary(lp_vars, r_vals, option, grid, weight, score)¶
Returns price summaries
- Parameters:
r_vals –
option –
farmgate grain price
wool price STB price for FNF (free or nearly free of fault)
sale price for specified grid at given weight and fat score (sale yard price)
grid – list - sale grids to report. Has to be int between 0 and 7 inclusive.
weight – float/int - stock weight to report price for.
score – int - fat score to report price for. Has to be number between 1-5 inclusive.
- Returns:
price summary df
- AfoLogic.ReportFunctions.f_profit(lp_vars, r_vals, option=0)¶
returns profit 0- Profit = rev - (exp + dep) 1- Risk neutral objective = rev - (exp + minroe + asset_cost +dep). 2- Utility - this is the same as risk neutral obj if risk aversion is not included 3- range and stdev of profit 4- profit by zqs
- AfoLogic.ReportFunctions.f_profitloss_table(lp_vars, r_vals, option=1)¶
Returns profit and loss statement for selected trials. Multiple trials result in a stacked pnl table.
- Parameters:
lp_vars – dict - results from pyomo
r_vals – dict - report variable
option – int - controls how q, s and z are reported. Default is to report them. Option 2 does a weighted average.
- Returns:
dataframe
- AfoLogic.ReportFunctions.f_rotation(lp_vars, r_vals)¶
manipulates the rotation solution into usable format. This is used in many function.
- AfoLogic.ReportFunctions.f_slice(prod, prod_weights, weights, den_weights, keys, arith, axis_slice)¶
Slices the prod, weights and key arrays
- Parameters:
prod – array: production param
prod_weights – array: production param weights
weights – array: weights (typically the variable associated with the prod param)
keys – list: keys for axes
axis_slice – dict: containing list of with slice params (start, stop, step)
- Returns:
prod array
- AfoLogic.ReportFunctions.f_stock_cash_summary(lp_vars, r_vals)¶
Returns:
expense and revenue items
- AfoLogic.ReportFunctions.f_stock_pasture_summary(r_vals, build_df=True, keys=None, type=None, index=[], cols=[], arith=0, prod=array([1]), na_prod=[], weights=None, na_weights=[], axis_slice={}, na_denweights=[], den_weights=1, na_prodweights=[], prod_weights=1)¶
..Note:
#. prod and weights must be broadcastable. #. Specify axes to broadcasted/expanded version.
- Parameters:
lp_vars – dict: results from pyomo
r_vals – dict: report variable
build_df – bool: return df
- Key type:
str: either ‘stock’, ‘pas’ or ‘stub’ to indicate which r_vals
- Key key:
str: dict key for the axis keys
- Key index (optional, default = []):
list: axis you want as the index of pandas df (order of list is the index level order).
- Key cols (optional, default = []):
list: axis you want as the cols of pandas df (order of list is the col level order).
- Key arith (optional, default = 0):
int: arithmetic operation used.
option 0: return production param averaged across all axis that are not reported.
option 1: return weighted average of production param (using denominator weight returns production per day the animal is on hand)
option 2: weighted total production summed across all axis that are not reported.
option 3: weighted total production for each (axis not reported are disregarded)
option 4: return weighted average of production param using prod>0 as the weights
option 5: return the maximum value across all axis that are not reported.
- Key prod (optional, default = 1):
str/int/float: if it is a string then it is used as a key for stock_vars, if it is a number that number is used as the prod value
- Key na_prod (optional, default = []):
list: position to add new axis
- Key weights (optional, default = None):
str: weights to be used in arith (typically a lp variable e.g. numbers). Only required when arith>0
- Key na_weights (optional, default = []):
list: position to add new axis
- Key den_weights (optional, default = 1):
str: key to variable used to weight the denominator in the weighted average (required p6 reporting)
- Key na_denweights (optional, default = []):
list: position to add new axis
- Key prod_weights (optional, default = 1):
str: keys to r_vals referencing array used to weight production.
- Key na_prodweights (optional, default = []):
list: position to add new axis
- Key axis_slice (optional, default = {}):
dict: keys (int) is the axis. value (list) is the start, stop and step of the slice
- Returns:
summary of a numpy array in a pandas table.
- AfoLogic.ReportFunctions.f_summary(lp_vars, r_vals, trial)¶
Returns a simple 1 row summary of the trial (season results are averaged)
- AfoLogic.ReportFunctions.f_var_reshape(lp_vars, r_vals)¶
Reshapes variables and creates a qsz weighted version.
This function gets called once per trial at the start of the reporting process (doesnt get called for each report). The resulting dictionary is stored as a global variable so it can be access by all reports.
Use the base version when z is being reported. Use the qsz weighted version when z is being averaged (i.e. not reported).
Returns a dictionary with params.
- AfoLogic.ReportFunctions.f_vars2df(lp_vars, var_key, maskz8=None, z_pos=-1)¶
converts lp_vars to pandas series. :param lp_vars: dict of variables. :param var_key: string - name of variable to convert to series. :param maskz8: z8 mask. Must be broadcastable to lp_vars once lp_vars if numpy :param z_pos: position (level) of z axis
- Returns:
series with season as index level 0
- AfoLogic.ReportFunctions.f_vars2np(lp_vars, var_key, shape, maskz8=None, z_pos=-1)¶
Converts lp_vars to numpy.
- Parameters:
lp_vars – dict of lp variables
var_key – string - name of variable to convert to numpy
shape – shape of desired numpy array
maskz8 – z8 mask. Must be broadcastable to lp_vars
z_pos – position of z axis
- Returns:
numpy array with un-clustered season axis.
- AfoLogic.ReportFunctions.f_wc_summary(lp_vars, r_vals)¶
- AfoLogic.ReportFunctions.f_xy_graph(data)¶
Generic x-y line graphing function.
- Parameters:
data – df with data to plot. First col contains x values and second col contains y values
- Returns:
x-y plot
- AfoLogic.ReportFunctions.load_pkl(trial_name)¶
load in lp_vars and r_vals output file.