modelvis module

Created on Fri May 12 11:07:02 2017

@author: hanseni

This module creates functions and classes for visualizing results.

modelvis.meltdim(df, dims=['dima', 'dimb'], source='Latest')[source]

Melts a wide dataframe the variable names are split to dimensions acording to the list of texts in dims. in variablenames the tall dataframe have a variable name for each dimensions also values and source are introduced ac column names in the dataframe

class modelvis.DummyVis(*args, **kwargs)[source]

Bases: object

class modelvis.vis(model=None, pat='', names=None, df=None)[source]

Bases: object

Visualization class. used as a method on a model instance.

The purpose is to select variables acording to a pattern, potential with wildcards

explain(**kwargs)[source]
draw(**kwargs)[source]
dekomp(**kwargs)[source]
heat(*args, **kwargs)[source]

Displays a heatmap of the resulting dataframe

plot(*args, **kwargs)[source]
Plots a subplot for each column in a datafra.

ppos determins which split by __ to use kind determins which kind of matplotlib chart to use

Args:

df (TYPE): Dataframe . name (TYPE, optional): title. Defaults to ‘’. ppos (TYPE, optional): # of position to use if split. Defaults to -1. kind (TYPE, optional): matplotlib kind . Defaults to ‘line’. colrow/ncol (TYPE, optional): columns per row . Defaults to 2. sharey (TYPE, optional): Share y axis between plots. Defaults to True. splitchar (TYPE, optional): if the name should be split . Defaults to ‘__’. savefig (TYPE, optional): save figure. Defaults to ‘’. xsize (TYPE, optional): x size default to 10 ysize (TYPE, optional): y size per row, defaults to 2

Returns:

a matplotlib fig.

note: ncol can be used instead of colrow to compatible with keep_plot

plot_alt(title='Title', *args, **kwargs)[source]

Displays a plot for each of the columns in the resulting dataframe

box()[source]

Displays a boxplot comparing basedf and lastdf

violin()[source]

Displays a violinplot comparing basedf and lastdf

swarm()[source]

Displays a swarmlot comparing basedf and lastdf

property df

Returns the result of this instance as a dataframe

property base

Returns basedf

property pct

Returns the pct change

property growth

Returns the pct growth

property year_pct

Returns the pct change over 4 periods (used for quarterly data)

property yoy_growth

Returns the pct change over 4 periods (used for quarterly data)

property qoq_ar

Returns the pct change over 4 periods (used for quarterly data)

property frml

Returns formulas

property des

Returns variable descriptions

property eviews

Returns variable descriptions

property dif

Returns the differens between the basedf and lastdf

property difpctlevel

Returns the differens between the basedf and lastdf in percent

property difpct

Returns the differens between the pct changes in basedf and lastdf

property difgrowth

Returns the differens between the pct changes in basedf and lastdf

property exo

Selects only exogenous variables

property print

prints the current result

property show
rename(other=None)[source]

rename columns

property endo

only endogennous variables. columns

mul(other)[source]

Multiply the curent result with other

property mul100

Multiply the current result with 1, used to be 100-

class modelvis.compvis(model=None, pat=None)[source]

Bases: object

Class to compare to runs in boxplots

Combines basedf and lastdf to one tall dataframe useful for the Seaborn library

box(*args, **kwargs)[source]

Displays a boxplot

swarm(*args, **kwargs)[source]

Displays a swarmplot

violin(*args, **kwargs)[source]

Displays a violinplot

class modelvis.container(lastdf, basedf)[source]

Bases: object

A container, used if to izualize dataframes without a model

smpl(start='', end='', df=None)[source]

Defines the model.current_per which is used for calculation period/index when no parameters are issues the current current period is returned

Either none or all parameters have to be provided

vlist(pat)[source]

returns a list of variable matching the pattern

class modelvis.varvis(model=None, var='')[source]

Bases: object

Visualization class. used as a method on a model instance.

The purpose is to select variables acording to a pattern, potential with wildcards

explain(**kwargs)[source]
draw(**kwargs)[source]
tracedep(down=1, **kwargs)[source]

Trace dependensies of name down to level down

tracepre(up=1, **kwargs)[source]
Trace dependensies of name down to level down
  • showdata|sd=True will include a table of values for each variable

  • showdata|sd=’pattern of variable names’ will include a table of values for each variable matching the pattern (including wildcharts

  • attshow|ats = True will include a table of attributions for each variable

  • growthshow|gs = True will include a table of growth for each variable

  • HR = True will reorient the dependency graph

  • up = <integer> will determine how many levels of parents to include

  • png = True will display as a png picture

  • svg = True will display as a svg picture which can be zoomed

  • pdf = True will display as a pdf picture

  • eps = True will create a eps file

  • browser = True will open a browser with the resulting dependency graph - useful for zooming on a big graph

  • saveas = <a file name without extension> will save the picture wit the filename with an added extension reflection the picture type

To allow the use of the display in presentations or publications The resulting file(s) are placed in the graph/subfolder

property dash

Trace dependensies of name down to level down

property dash2

Trace dependensies of name down to level down poer = 5002

dashport(port=5002)[source]

Trace dependensies of name down to level down

get_att(start='', end='', dec=None, bare=True, **kwargs)[source]

Retrieve and display the attribution for a variable within a specified period.

Parameters:
  • start (str) – Start date of the period (default: ‘’).

  • end (str) – End date of the period (default: ‘’).

  • dec (int) – Number of decimal places for formatting (default: None).

  • bare (bool) – If True, display only the attribution result; if False, display both the difference and attribution results (default: True).

  • type (str) – One of ‘pct’, ‘growth’, ‘level’ (default: ‘pct’)

  • **kwargs – Additional keyword arguments for specifying attribution type and other options.

Returns:

None (displays the attribution result)

Note

  • The method retrieves the difference and attribution results using the specified period and attribution type.

  • The dec parameter controls the number of decimal places for formatting. If not provided, the default number of decimal places is determined based on the attribution type.

  • The bare parameter determines whether to display only the attribution result or both the difference and attribution results.

  • The type parameter wether to display the level, pct or growth attribution determines whether to display level, growth or pct attribution

dekomp(**kwargs)[source]
var_des(var)[source]
property show
property showdif
property frml
property eviews
modelvis.vis_alt(grund, mul, title='Show variables', ttop=None)[source]

Graph of one of more variables each variable is displayed for 3 banks

modelvis.plotshow(df, name='', ppos=-1, kind='line', colrow=2, sharey=False, top=None, splitchar='__', savefig='', *args, **kwargs)[source]
Plots a subplot for each column in a datafra.

ppos determins which split by __ to use kind determins which kind of matplotlib chart to use

Args:

df (TYPE): Dataframe . name (TYPE, optional): title. Defaults to ‘’. ppos (TYPE, optional): # of position to use if split. Defaults to -1. kind (TYPE, optional): matplotlib kind . Defaults to ‘line’. colrow/ncol (TYPE, optional): columns per row . Defaults to 2. sharey (TYPE, optional): Share y axis between plots. Defaults to True. splitchar (TYPE, optional): if the name should be split . Defaults to ‘__’. savefig (TYPE, optional): save figure. Defaults to ‘’. xsize (TYPE, optional): x size default to 10 ysize (TYPE, optional): y size per row, defaults to 2

Returns:

a matplotlib fig.

note: ncol can be used instead of colrow to compatible with keep_plot

modelvis.melt(df, source='Latest')[source]

melts a wide dataframe to a tall dataframe , appends a soruce column

modelvis.heatshow(df, name='', cmap='Reds', mul=1.0, annot=False, size=(11.69, 8.27), dec=0, cbar=True, linewidths=0.5)[source]

A heatmap of a dataframe problems in 3.12

modelvis.attshow(df, treshold=False, head=5000, tail=0, t=True, annot=False, showsum=False, sort=True, size=(11.69, 8.27), title='', tshow=True, dec=0, cbar=True, cmap='jet', savefig='')[source]

Shows heatmap of impacts of exogeneous variables :df: Dataframe with impact :treshold: Take exogeneous variables with max impact of treshold or larger :numhigh: take the numhigh largest impacts :t: transpose the heatmap :annot: Annotate the heatmap :head: take the head largest .tail: take the tail smalest :showsum: Add a column with the sum :sort: Sort the data .tshow: Show a longer title :cbar: if a colorbar shoud be displayes :cmap: the colormap :save: Save the chart (in png format)

modelvis.attshowone(df, name, pre='', head=5, tail=5)[source]

shows the contribution to row=name from each column the coulumns can optional be selected as starting with pre

modelvis.water(serxinput, sort=False, ascending=True, autosum=False, allsort=False, threshold=0.0)[source]

Creates a dataframe with information for a watrfall diagram

Serx:

the input serie of values

Sort:

True if the bars except the first and last should be sorted (default = False)

Allsort:

True if all bars should be sorted (default = False)

Autosum:

True if a Total bar are added in the end

Ascending:

True if sortorder = ascending

Returns a dataframe with theese columns:

Hbegin:

Height of the first bar

Hend:

Height of the last bar

Hpos:

Height of positive bars

Hneg:

Height of negative bars

Start:

Ofset at which each bar starts

Height:

Height of each bar (just for information)

modelvis.waterplot(basis, sort=True, ascending=True, autosum=False, bartype='bar', threshold=0.0, allsort=False, title='Attribution ', top=0.9, desdic={}, zero=True, ysize=5, **kwarg)[source]