ModelFlowIb 1.46__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
modelvis.py ADDED
@@ -0,0 +1,677 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Fri May 12 11:07:02 2017
4
+
5
+ @author: hanseni
6
+
7
+ This module creates functions and classes for visualizing results.
8
+
9
+
10
+ """
11
+
12
+
13
+ import pandas as pd
14
+ import matplotlib.pyplot as plt
15
+ import matplotlib as mpl
16
+ import seaborn as sns
17
+ import fnmatch
18
+ from matplotlib import dates
19
+ import matplotlib.ticker as ticker
20
+ from IPython.display import display
21
+
22
+ import numpy
23
+
24
+ from modelhelp import cutout,finddec
25
+
26
+ ##%%
27
+ def meltdim(df,dims=['dima','dimb'],source='Latest'):
28
+ ''' Melts a wide dataframe the variable names are split to dimensions acording
29
+ to the list of texts in dims. in variablenames
30
+ the tall dataframe have a variable name for each dimensions
31
+ also values and source are introduced ac column names in the dataframe '''
32
+ splitstring = (r'\|').join(['(?P<'+d+'>[A-Z0-9_]*)' for d in dims])
33
+ melted = pd.melt(df.reset_index().rename(columns={'index':'quarter'}),id_vars='quarter')
34
+ vardf = (melted
35
+ .assign(source=source)
36
+ .assign(varname = lambda df_ :df_.variable.str.replace('__','|',len(dims)-1)) # just to make the next step more easy
37
+ .pipe(lambda df_ : pd.concat([df_ ,df_.varname.str.extract(splitstring,expand=True)],axis=1)))
38
+ return vardf
39
+
40
+
41
+ class vis():
42
+ ''' Visualization class. used as a method on a model instance.
43
+
44
+ The purpose is to select variables acording to a pattern, potential with wildcards
45
+ '''
46
+ def __init__(self, model=None, pat='',names=None,df=None):
47
+ self.model = model
48
+ self.__pat__ = pat
49
+ self.names = self.model.vlist(self.__pat__)
50
+ if isinstance(df,pd.DataFrame):
51
+ self.thisdf = df
52
+ else:
53
+ self.thisdf = self.model.lastdf.loc[:,self.names]
54
+ return
55
+
56
+ def explain(self,**kwargs):
57
+ for var in self.names:
58
+ x = self.model.explain(var,**kwargs)
59
+ return
60
+
61
+ def draw(self,**kwargs):
62
+ for var in self.names:
63
+ x = self.model.draw(var,**kwargs)
64
+
65
+ def dekomp(self,**kwargs):
66
+ self.model.dekomp.cache_clear()
67
+ for var in self.names:
68
+ x = self.model.dekomp(var,**kwargs)
69
+
70
+ def heat(self,*args, **kwargs):
71
+ ''' Displays a heatmap of the resulting dataframe'''
72
+
73
+ name = kwargs.pop('title',self.__pat__)
74
+ a = heatshow(self.thisdf.loc[self.model.current_per,:].T,
75
+ name=name,*args, **kwargs)
76
+ return a
77
+
78
+ def plot(self,*args, **kwargs):
79
+ ''' Displays a plot for each of the columns in the resulting dataframe '''
80
+
81
+ name = kwargs.get('title','Title')
82
+ a = plotshow(self.thisdf.loc[self.model.current_per,:],
83
+ name=name,*args,**kwargs)
84
+ return a
85
+
86
+
87
+ def plot_alt(self,title='Title',*args, **kwargs):
88
+ ''' Displays a plot for each of the columns in the resulting dataframe '''
89
+
90
+ if hasattr(self.model,'var_description'):
91
+ vtrans = self.model.var_description
92
+ else:
93
+ vtrans = {}
94
+ a = vis_alt(self.model.basedf.loc[self.model.current_per,self.names].rename(columns=vtrans) ,
95
+ self.model.lastdf.loc[self.model.current_per,self.names].rename(columns=vtrans) ,
96
+ title=title,*args,**kwargs)
97
+ return a
98
+
99
+ def box(self):
100
+ ''' Displays a boxplot comparing basedf and lastdf '''
101
+ return compvis(model=self.model,pat=self.__pat__).box()
102
+ def violin(self):
103
+ ''' Displays a violinplot comparing basedf and lastdf '''
104
+ return compvis(model=self.model,pat=self.__pat__).violin()
105
+ def swarm(self):
106
+ ''' Displays a swarmlot comparing basedf and lastdf '''
107
+ return compvis(model=self.model,pat=self.__pat__).swarm()
108
+
109
+
110
+ @property
111
+ def df(self):
112
+ ''' Returns the result of this instance as a dataframe'''
113
+ return self.thisdf.loc[self.model.current_per,:]
114
+
115
+ @property
116
+ def base(self):
117
+ ''' Returns basedf '''
118
+ return vis(model=self.model,df=self.model.basedf.loc[:,self.names],pat=self.__pat__)
119
+
120
+ @property
121
+ def pct(self):
122
+ '''Returns the pct change'''
123
+ return vis(model=self.model,df=self.thisdf.loc[:,self.names].pct_change(),pat=self.__pat__)
124
+
125
+ @property
126
+ def year_pct(self):
127
+ '''Returns the pct change over 4 periods (used for quarterly data) '''
128
+ return vis(model=self.model,df=self.thisdf.loc[:,self.names].pct_change(periods=4).loc[:,self.names],pat=self.__pat__)
129
+
130
+ @property
131
+ def frml(self):
132
+ '''Returns formulas '''
133
+ def getfrml(var,l):
134
+ if var in self.model.endogene:
135
+ t=self.model.allvar[var]['frml'].replace('\n',' ').replace(' ',' ')
136
+ return f'{var:<{l}} : {t}'
137
+ else:
138
+ return f'{var:{l}} : Exogenous'
139
+ mlength = max([len(v) for v in self.names])
140
+ out = '\n'.join(getfrml(var,mlength) for var in self.names)
141
+ print(out)
142
+
143
+ @property
144
+ def des(self):
145
+ '''Returns variable descriptions '''
146
+ def getdes(var,l):
147
+ return f'{var:<{l}} : {self.model.var_description[var]}'
148
+ mlength = max([len(v) for v in self.names])
149
+
150
+ out = '\n'.join(getdes(var,mlength) for var in self.names)
151
+ print(out)
152
+
153
+
154
+ @property
155
+ def dif(self):
156
+ ''' Returns the differens between the basedf and lastdf'''
157
+ difdf = self.thisdf-self.model.basedf.loc[:,self.names]
158
+ return vis(model=self.model,df=difdf,pat=self.__pat__)
159
+
160
+ @property
161
+ def difpctlevel(self):
162
+ ''' Returns the differens between the basedf and lastdf'''
163
+ difdf = (self.thisdf-self.model.basedf.loc[:,self.names])/ self.model.basedf.loc[:,self.names]
164
+ return vis(model=self.model,df=difdf,pat=self.__pat__)
165
+ @property
166
+ def difpct(self):
167
+ ''' Returns the differens between the pct changes in basedf and lastdf'''
168
+ difdf = self.thisdf.pct_change()-self.model.basedf.loc[:,self.names].pct_change()
169
+ return vis(model=self.model,df=difdf,pat=self.__pat__)
170
+ @property
171
+ def print(self):
172
+ ''' prints the current result'''
173
+ print('\n',self.thisdf.loc[self.model.current_per,:].to_string())
174
+ return
175
+
176
+ def __repr__(self):
177
+
178
+ # return self.thisdf.loc[self.model.current_per,:].to_string()
179
+
180
+ if self.model.in_notebook():
181
+ # from modelwidget import visshow
182
+ # visshow(self.model,self.__pat__)
183
+ return ''
184
+ else:
185
+ return self.thisdf.loc[self.model.current_per,:].to_string()
186
+
187
+
188
+
189
+ def _repr_html_(self):
190
+ '''Displays a nice summary of the results when called in a Jupyter enviorement'''
191
+
192
+ from modelwidget import visshow
193
+ visshow(self.model,self.__pat__)
194
+ return ''
195
+
196
+ # return self.model.ibsstyle(self.thisdf.loc[self.model.current_per,:]).to_html(doctype_html=True)
197
+
198
+
199
+ # @property
200
+ # def show(self):
201
+ # if self.model.in_notebook():
202
+ # display(self.model.ibsstyle(self.thisdf.loc[self.model.current_per,:],transpose=True))
203
+ # else:
204
+ # print(self.thisdf.loc[self.model.current_per,:].to_string())
205
+ @property
206
+ def show(self):
207
+ if self.model.in_notebook():
208
+ from modelwidget import visshow
209
+ visshow(self.model,self.__pat__)
210
+ else:
211
+ print(self.thisdf.loc[self.model.current_per,:].to_string())
212
+
213
+
214
+ def __mul__(self,other):
215
+ ''' Multiply the curent result with other '''
216
+ muldf = self.thisdf * other
217
+ return vis(model=self.model,df=muldf,pat=self.__pat__)
218
+
219
+ def rename(self,other=None):
220
+ ''' rename columns '''
221
+ if type(other) == type(None):
222
+ if hasattr(self.model,'var_description'):
223
+ vtrans = self.model.var_description
224
+ else:
225
+ vtrans = {}
226
+ else:
227
+ vtrans = other
228
+ muldf = self.thisdf.rename(columns=vtrans)
229
+ return vis(model=self.model,df=muldf,pat=self.__pat__)
230
+
231
+ def mul(self,other):
232
+ ''' Multiply the curent result with other '''
233
+ return self.__mul__(other)
234
+
235
+ @property
236
+ def mul100(self):
237
+ '''Multiply the current result with 100 '''
238
+ return self.__mul__(100.0)
239
+
240
+ class compvis() :
241
+ ''' Class to compare to runs in boxplots'''
242
+ def __init__(self, model=None, pat=None):
243
+ ''' Combines basedf and lastdf to one tall dataframe useful for the Seaborn library'''
244
+ self.model = model
245
+ self.__pat__ = pat
246
+ self.names = self.model.vlist(self.__pat__)
247
+ self.lastdf = self.model.lastdf.loc[self.model.current_per,self.names]
248
+ self.basedf = self.model.basedf.loc[self.model.current_per,self.names]
249
+ self.lastmelt = melt(self.lastdf,source='Scenario')
250
+ self.basemelt = melt(self.basedf ,source='Base')
251
+ self.melted = pd.concat([self.lastmelt,self.basemelt])
252
+ return
253
+
254
+ def box(self,*args, **kwargs):
255
+ '''Displays a boxplot'''
256
+ fig, ax = plt.subplots(figsize=(12,6))
257
+ ax = sns.boxplot(x='time',y='value',data=self.melted,hue='source',ax=ax)
258
+ ax.set_title(self.__pat__)
259
+ def swarm(self,*args, **kwargs):
260
+ '''Displays a swarmplot '''
261
+ fig, ax = plt.subplots(figsize=(12,6))
262
+ ax = sns.swarmplot(x='time',y='value',data=self.melted,hue='source',ax=ax)
263
+ ax.set_title(self.__pat__)
264
+ def violin(self,*args, **kwargs):
265
+ '''Displays a violinplot'''
266
+ fig, ax = plt.subplots(figsize=(12,6))
267
+ ax = sns.violinplot(x='time',y='value',data=self.melted,hue='source',ax=ax)
268
+ ax.set_title(self.__pat__)
269
+
270
+
271
+ class container():
272
+ '''A container, used if to izualize dataframes without a model'''
273
+
274
+ def __init__(self,lastdf,basedf):
275
+ self.lastdf = lastdf
276
+ self.basedf = basedf
277
+
278
+ def smpl(self,start='',slut='',df=None):
279
+ ''' Defines the model.current_per which is used for calculation period/index
280
+ when no parameters are issues the current current period is returned \n
281
+ Either none or all parameters have to be provided '''
282
+ if start =='' and slut == '':
283
+ pass
284
+ else:
285
+ istart,islut= self.lastdf.index.slice_locs(start,slut)
286
+ per=self.lastdf.index[istart:islut]
287
+ self.current_per = per
288
+ return self.current_per
289
+ def vlist(self,pat):
290
+ '''returns a list of variable matching the pattern'''
291
+ if isinstance(pat,list):
292
+ ipat=pat
293
+ else:
294
+ ipat = [pat]
295
+ out = [v for p in ipat for v in sorted(fnmatch.filter(self.lastdf.columns,p.upper()))]
296
+ return out
297
+
298
+
299
+ ##%%
300
+ class varvis():
301
+ ''' Visualization class. used as a method on a model instance.
302
+
303
+ The purpose is to select variables acording to a pattern, potential with wildcards
304
+ '''
305
+ def __init__(self, model=None, var=''):
306
+ self.model = model
307
+ self.var = var
308
+ self.endo = self.model.allvar[var]['endo']
309
+
310
+ def explain(self,**kwargs):
311
+ x = self.model.explain(self.var,**kwargs)
312
+ return x
313
+
314
+ def draw(self,**kwargs):
315
+ x = self.model.draw(self.var,**kwargs)
316
+
317
+ def tracedep(self,down=1,**kwargs):
318
+ '''Trace dependensies of name down to level down'''
319
+ self.model.draw(self.var,down=down,up=0,source=self.var,**kwargs)
320
+
321
+ def tracepre(self,up=1,**kwargs):
322
+ '''Trace dependensies of name down to level down'''
323
+ self.model.draw(self.var,down=0,up=up,sink=self.var,**kwargs)
324
+
325
+
326
+
327
+ def dekomp(self,**kwargs):
328
+ if kwargs.get('lprint','False'):
329
+ self.model.dekomp.cache_clear()
330
+ x = self.model.dekomp(self.var,**kwargs)
331
+ return x
332
+
333
+ def var_des(self,var):
334
+ des = self.model.var_description[var]
335
+ return des if des != var else ''
336
+
337
+ def _showall_old(self,all=1,dif=0,last=0,show_all=True):
338
+ if self.endo:
339
+ des_string = self.model.get_eq_des(self.var,show_all)
340
+ out1,out2 = '',''
341
+ out0 = f'Endogeneous: {self.var}: {self.var_des(self.var)} \nFormular: {self.model.allvar[self.var]["frml"]}\n\n{des_string}\n'
342
+ try:
343
+ if dif:
344
+ out0 = out0+f'\nValues : \n{self.model.get_values(self.var)}\n'
345
+
346
+ if all:
347
+ out0 = out0+f'\nValues : \n{self.model.get_values(self.var)}\n'
348
+ out1 = f'\nInput last run: \n {self.model.get_eq_values(self.var)}\n\nInput base run: \n {self.model.get_eq_values(self.var,last=False)}\n'
349
+ elif last:
350
+ out0 = out0+f'\nValues : \n{self.model.get_values(self.var)}\n'
351
+ out1 = f'\nInput last run: \n {self.model.get_eq_values(self.var)}\n'
352
+ if all or dif:
353
+ out2 = f'\nDifference for input variables: \n {self.model.get_eq_dif(self.var,filter=False)}'
354
+ except Exception as e:
355
+ print(e)
356
+ pass
357
+ out=out0+out1+out2
358
+ else:
359
+ out = f'Exogeneous : {self.var}: {self.var_des(self.var)} \n Values : \n{self.model.get_values(self.var)}\n'
360
+ return out
361
+
362
+ # def ibsstyle(self,df,dec=2):
363
+ # ''' display a dataframe with tooltip'''
364
+
365
+ # tt = pd.DataFrame([[self.model.var_description[v] for c in df.columns] for v in df.index ],index=df.index,columns=df.columns)
366
+ # xdec = f'{dec}'
367
+ # result = df.style.format('{:.'+xdec+'f}').\
368
+ # set_tooltips(tt, props='visibility: hidden; position: absolute; z-index: 1; border: 1px solid #000066;'
369
+ # 'background-color: white; color: #000066; font-size: 0.8em;'
370
+ # 'transform: translate(0px, -24px); padding: 0.6em; border-radius: 0.5em;')
371
+ # return result
372
+
373
+ def _showall(self,all=1,dif=0,last=0,show_all=True):
374
+ from IPython.display import SVG, display, Image, IFrame, HTML, Markdown
375
+
376
+ if self.endo:
377
+ des_string = self.model.get_eq_des(self.var,show_all)
378
+ out0,out1,out2 = '','',''
379
+ print(f'Endogeneous: {self.var}: {self.var_des(self.var)}')
380
+ print(f'Formular: {self.model.allvar[self.var]["frml"]}\n\n{des_string}\n')
381
+ try:
382
+ if dif or all or last: print('Values :')
383
+ if dif or all or last: display(HTML(self.model.ibsstyle(self.model.get_values(self.var)).to_html() ))
384
+ if all or last: print('Input last run:')
385
+ if all or last: display(self.model.ibsstyle(self.model.get_eq_values(self.var)))
386
+ if all : print('Input base run:')
387
+ if all : display(self.model.ibsstyle(self.model.get_eq_values(self.var,last=False)))
388
+ if all or dif: print('Difference for input variables')
389
+ if all or dif: display(self.model.ibsstyle(self.model.get_eq_dif(self.var,filter=False)))
390
+ except Exception as e:
391
+ print(e)
392
+ pass
393
+ out=out0+out1+out2
394
+ else:
395
+ out = f'Exogeneous : {self.var}: {self.var_des(self.var)} \n Values : \n{self.model.get_values(self.var)}\n'
396
+ return out
397
+
398
+
399
+ @property
400
+ def show(self):
401
+ out = self._showall(all=1)
402
+ print(out)
403
+ return
404
+
405
+ @property
406
+ def showdif(self):
407
+ out = self._showall(all=0,dif=1)
408
+ print(out)
409
+ return
410
+ @property
411
+ def frml(self):
412
+ out = self._showall(all=0,dif=0)
413
+ print(out)
414
+ return
415
+
416
+
417
+ def __repr__(self):
418
+
419
+ out = self._showall(all=0,last=1)
420
+ return out
421
+
422
+
423
+
424
+ def vis_alt(grund,mul,title='Show variables',top=0.9):
425
+ ''' Graph of one of more variables each variable is displayed for 3 banks'''
426
+ avar = grund.columns
427
+ antal=len(avar)
428
+ fig, axes = plt.subplots(nrows=antal, ncols=1,figsize=(15,antal*6)) #,sharex='col' ,sharey='row')
429
+ fig.suptitle(title, fontsize=20)
430
+ ax2 = [axes] if antal == 1 else axes
431
+ for i,(var,ax) in enumerate(zip(avar,ax2)):
432
+ grunddata = grund.loc[:,var]
433
+ muldata = mul.loc[:,var]
434
+ # breakpoint()
435
+ grunddata.plot(ax=ax,legend=False,fontsize=14)
436
+ muldata.plot (ax=ax,legend=False,fontsize=14)
437
+ ax.set_title(var,fontsize=14)
438
+ x_pos = grunddata.index[-1]
439
+ ax.text(x_pos, grunddata.values[-1],'Baseline',fontsize=14)
440
+ ax.text(x_pos, muldata.values[-1] ,'Alternative',fontsize=14)
441
+ ax.yaxis.set_major_formatter(ticker.FuncFormatter(lambda value,number: f'{value:,}'))
442
+
443
+ ax.xaxis.set_minor_locator(plt.NullLocator())
444
+ ax.tick_params(axis='x', labelleft=True)
445
+ fig.subplots_adjust(top=top)
446
+
447
+ return fig
448
+
449
+
450
+ def plotshow(df,name='',ppos=-1,kind='line',colrow=2,sharey=False,top=0.90,
451
+ splitchar='__',savefig='',*args,**kwargs):
452
+ '''
453
+
454
+
455
+ Args:
456
+ df (TYPE): Dataframe .
457
+ name (TYPE, optional): title. Defaults to ''.
458
+ ppos (TYPE, optional): # of position to use if split. Defaults to -1.
459
+ kind (TYPE, optional): matplotlib kind . Defaults to 'line'.
460
+ colrow (TYPE, optional): columns per row . Defaults to 6.
461
+ sharey (TYPE, optional): Share y axis between plots. Defaults to True.
462
+ top (TYPE, optional): relative position of the title. Defaults to 0.90.
463
+ splitchar (TYPE, optional): if the name should be split . Defaults to '__'.
464
+ savefig (TYPE, optional): save figure. Defaults to ''.
465
+ xsize (TYPE, optional): x size default to 10
466
+ ysize (TYPE, optional): y size per row, defaults to 2
467
+
468
+ Returns:
469
+ a matplotlib fig.
470
+
471
+ '''
472
+
473
+ ''' Plots a subplot for each column in a datafra.
474
+ ppos determins which split by __ to use
475
+ kind determins which kind of matplotlib chart to use
476
+
477
+
478
+ '''
479
+ if splitchar:
480
+ out=df.pipe(lambda df_: df_.rename(columns={v: v.split(splitchar)[ppos] for v in df_.columns}))
481
+ else:
482
+ out=df
483
+ number = out.shape[1]
484
+ row=-((-number)//colrow)
485
+ # breakpoint()
486
+
487
+ axes=out.plot(kind=kind,subplots=True,layout=(row,colrow),figsize = (kwargs.get('xsize',10), row*kwargs.get('ysize',2)),
488
+ use_index=True,title=name,sharey=sharey)
489
+ for ax in axes.flatten():
490
+ pass
491
+ # dec=finddec(dfatt)
492
+ # ax.yaxis.set_major_formatter(ticker.FuncFormatter(lambda value,number: f'{value:,}'))
493
+
494
+ ax.xaxis.set_minor_locator(plt.NullLocator())
495
+ ax.tick_params(axis='x', labelleft=True)
496
+ fig = axes.flatten()[0].get_figure()
497
+ fig.suptitle(name,fontsize=20)
498
+ fig.tight_layout()
499
+ # top = (row*(2-0.1)-0.2)/(row*(2-0.1))
500
+ # print(top)
501
+ fig.subplots_adjust(top=top)
502
+ if savefig:
503
+ fig.savefig(savefig)
504
+ # plt.subplot_tool()
505
+ return fig
506
+
507
+ def melt(df,source='Latest'):
508
+ ''' melts a wide dataframe to a tall dataframe , appends a soruce column '''
509
+ melted = pd.melt(df.reset_index().rename(columns={'index':'time'}),id_vars='time').assign(source=source)
510
+ return melted
511
+
512
+ def heatshow(df,name='',cmap="Reds",mul=1.,annot=False,size=(11.69,8.27),dec=0,cbar=True,linewidths=.5):
513
+ ''' A heatmap of a dataframe '''
514
+ xx=(df.astype('float'))*mul
515
+ # fig, ax = plt.subplots(figsize=(11,8))
516
+ fig, ax = plt.subplots(figsize=size) #A4
517
+ sns.heatmap(xx,cmap=cmap,ax=ax,fmt="."+str(dec)+"f",annot=annot,annot_kws ={"ha": 'center'},linewidths=linewidths,cbar=cbar)
518
+ ax.set_title(name, fontsize=20)
519
+
520
+ ax.set_yticklabels(ax.yaxis.get_majorticklabels(), ha = 'left',rotation=0)
521
+ yax = ax.get_yaxis()
522
+ pad = max(T.label.get_window_extent().width for T in yax.majorTicks)
523
+ yax.set_tick_params(pad=pad)
524
+
525
+ ax.set_xticklabels(ax.xaxis.get_majorticklabels(), va = 'top' ,rotation=70.)
526
+ fig.subplots_adjust(bottom=0.15)
527
+ #ax.tick_paraOms(axis='y',direction='out', length=3, width=2, colors='b',labelleft=True)
528
+ return fig
529
+ ##%%
530
+ def attshow(df,treshold=False,head=5000,tail=0,t=True,annot=False,showsum=False,sort=True,size=(11.69,8.27),title='',
531
+ tshow=True,dec=0,cbar=True,cmap='jet',savefig=''):
532
+ '''Shows heatmap of impacts of exogeneous variables
533
+ :df: Dataframe with impact
534
+ :treshold: Take exogeneous variables with max impact of treshold or larger
535
+ :numhigh: take the numhigh largest impacts
536
+ :t: transpose the heatmap
537
+ :annot: Annotate the heatmap
538
+ :head: take the head largest
539
+ .tail: take the tail smalest
540
+ :showsum: Add a column with the sum
541
+ :sort: Sort the data
542
+ .tshow: Show a longer title
543
+ :cbar: if a colorbar shoud be displayes
544
+ :cmap: the colormap
545
+ :save: Save the chart (in png format) '''
546
+
547
+
548
+ selectmin = df.min().sort_values(ascending=False).tail(tail).index.tolist()
549
+ selectmax = df.max().sort_values(ascending=False).head(head).index.tolist()
550
+ select=selectmax+selectmin
551
+ yy = df[select].pipe(
552
+ lambda df_ : df_[select] if sort else df_[sorted(list(df_.columns))])
553
+ if showsum:
554
+ asum= yy.sum(axis=1)
555
+ asum.name = '_Sum'
556
+ yy = pd.concat([yy,asum],axis=1)
557
+
558
+ yy2 = yy.T if t else yy
559
+ if sort and tshow:
560
+ txt= ' Impact from exogeneous variables. '+(
561
+ str(head)+' highest. ' if head >= 1 else '')+( str(tail)+' smallest. ' if tail >= 1 else '')
562
+ else:
563
+ txt= ''
564
+ f=heatshow(yy2,cmap=cmap,name=title+txt ,annot=annot,mul=1.,size=size,dec=dec,cbar=cbar)
565
+ f.subplots_adjust(bottom=0.16)
566
+ if savefig:
567
+ f.savefig(savefig)
568
+ return yy2
569
+
570
+
571
+
572
+ def attshowone(df,name,pre='',head=5,tail=5):
573
+ ''' shows the contribution to row=name from each column
574
+ the coulumns can optional be selected as starting with pre'''
575
+
576
+ res = df.loc[name,[n for n in df.columns if n.startswith(pre)]].sort_values(ascending=False).pipe(
577
+ lambda df_: df_.head(head).append(df_.tail(tail)))
578
+ ax = res.plot(kind='bar')
579
+ txt= ( str(head)+' highest. ' if head >= 1 else '')+( str(tail)+' smallest. ' if tail >= 1 else '')
580
+ ax.set_title('Contributions to '+name+'. '+txt)
581
+ return ax
582
+
583
+
584
+ def water(serxinput,sort=False,ascending =True,autosum=False,allsort=False,threshold=0.0):
585
+ ''' Creates a dataframe with information for a watrfall diagram
586
+
587
+ :serx: the input serie of values
588
+ :sort: True if the bars except the first and last should be sorted (default = False)
589
+ :allsort: True if all bars should be sorted (default = False)
590
+ :autosum: True if a Total bar are added in the end
591
+ :ascending: True if sortorder = ascending
592
+
593
+ Returns a dataframe with theese columns:
594
+
595
+ :hbegin: Height of the first bar
596
+ :hend: Height of the last bar
597
+ :hpos: Height of positive bars
598
+ :hneg: Height of negative bars
599
+ :start: Ofset at which each bar starts
600
+ :height: Height of each bar (just for information)
601
+ '''
602
+ # get the height of first and last column
603
+
604
+
605
+ total=serxinput.sum()
606
+ serx = cutout(serxinput,threshold)
607
+ if sort or allsort : # sort rows except the first and last
608
+ endslice = None if allsort else -1
609
+ startslice = None if allsort else 1
610
+ i = serx[startslice:endslice].sort_values(ascending =ascending ).index
611
+ if allsort:
612
+ newi =i.tolist()
613
+ else:
614
+ newi =[serx.index.tolist()[0]] + i.tolist() + [serx.index.tolist()[-1]]# Get the head and tail
615
+ ser=serx[newi]
616
+ else:
617
+ ser=serx.copy()
618
+
619
+ if autosum:
620
+ ser['Total'] = total
621
+
622
+ hbegin = ser.copy()
623
+ hbegin[1:]=0.0
624
+ hend = ser.copy()
625
+ hend[:-1] = 0.0
626
+
627
+
628
+ height = ser
629
+ start = ser.cumsum().shift().fillna(0.0) # the starting point for each bar
630
+ start[-1] = start[-1] if allsort and not autosum else 0 # the last bar should start at 0
631
+ end = start + ser
632
+
633
+ hpos= height*(height>=0.0)
634
+ hneg= height*(height<=0.0)
635
+ dfatt = pd.DataFrame({'start':start,'hbegin':hbegin,'hpos':hpos,'hneg':hneg,'hend':hend,'height':height}).loc[ser.index,:]
636
+
637
+ return dfatt
638
+
639
+ def waterplot(basis,sort=True,ascending =True,autosum=False,bartype='bar',threshold=0.0,
640
+ allsort=False,title=f'Attribution ',top=0.9, desdic = {},zero=True, ysize=5,**kwarg):
641
+ att = [(name,water(ser,sort=sort,autosum=autosum,allsort=allsort,threshold=threshold))
642
+ for name,ser in basis.transpose().iterrows()]
643
+ # print(att[0][1])
644
+ fig, axis = plt.subplots(nrows=len(att),ncols=1,figsize=(10,ysize*len(att)),constrained_layout=False)
645
+ width = 0.5 # the width of the barsser
646
+ laxis = axis if isinstance(axis,numpy.ndarray) else [axis]
647
+ for i,((name,dfatt),ax) in enumerate(zip(att,laxis)):
648
+ _ = dfatt.hpos.plot(ax=ax,kind=bartype,bottom=dfatt.start,stacked=True,
649
+ color='green',width=width)
650
+ _ = dfatt.hneg.plot(ax=ax,kind=bartype,bottom=dfatt.start,stacked=True,
651
+ color='red',width=width)
652
+ _ = None if allsort else dfatt.hbegin.plot(ax=ax,kind=bartype,bottom=dfatt.start,
653
+ stacked=True,color=('green' if dfatt.hbegin[0] > 0 else 'red') if zero else 'blue',width=width)
654
+ _ = None if allsort and not autosum else dfatt.hend.plot(ax=ax,kind=bartype,bottom=dfatt.start,stacked=True,color='blue',width=width)
655
+ ax.set_ylabel(name,fontsize='x-large')
656
+ dec=finddec(dfatt)
657
+ ax.yaxis.set_major_formatter(ticker.FuncFormatter(lambda value,number: f'{value:,.{dec}f}'))
658
+
659
+ ax.set_title(desdic.get(name,name))
660
+ ax.set_xticklabels(dfatt.index.tolist(), rotation = 70,fontsize='x-large')
661
+ # plt.xticks(rotation=45, horizontalalignment='right',
662
+ # fontweight='light', fontsize='x-large' )
663
+ fig.suptitle(title,fontsize=20)
664
+ if 1:
665
+ ...
666
+ # plt.tight_layout()
667
+ # fig.subplots_adjust(top=top)
668
+
669
+ # plt.show()
670
+ return fig
671
+
672
+ if __name__ == '__main__' and 1:
673
+ basis = pd.DataFrame([[100,100.],[-10.0,-12], [12,-10],[-10,10]],index=['nii','cost','credit','fee'],columns=['ex','ex2'])
674
+ basis.loc['total'] = basis.sum()
675
+ waterplot(basis)
676
+
677
+