Skip to content

Commit

Permalink
Added ability to add finv table to report (#49)
Browse files Browse the repository at this point in the history
* chore: Fixed some comments; feature: Added new table method

* feature: Loading csv file and displaying as frame

* bugfix: Now limiting to 10 rows, styling changes and comments

* bugfix: Removed unnecessary flag

* bugfix: Swapped to using pandas dataframe for inventory table

* bugfix: Fixed issue with lost table features during export

* add label and notes

* Update dialog.py

use MOdel's native finv import

* Update reporter.py

replace layer generator with existing method

Co-authored-by: cefect <47092034+cefect@users.noreply.github.com>
  • Loading branch information
BenMaxIBI and cefect authored May 18, 2022
1 parent a2abb25 commit ab71f6e
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 45 deletions.
4 changes: 2 additions & 2 deletions canflood/build/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ def set_setup(self, set_cf_fp=True, set_finv=True, #attach parameters from setup
if set_finv:

#===================================================================
# get using sleection logic
# get using selection logic
#===================================================================
vlay_raw = self.comboBox_ivlay.currentLayer()
assert not vlay_raw is None, 'must select a finv vlay'
Expand Down Expand Up @@ -1132,7 +1132,7 @@ def run_dsamp(self): #sample dtm raster
def _get_rsamp_pars(self): #extract raster sampling paraeters from dialog

#=======================================================================
# get defautls
# get defaults
#=======================================================================
log = self.logger.getChild('_get_rsamp_pars')
finv = self.finv_vlay #set by set_setup()
Expand Down
2 changes: 1 addition & 1 deletion canflood/build/lisamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def run(self, #sample conditional probability polygon 'lfield' values with finv
#upper bound
):
#=======================================================================
# defults
# default
#=======================================================================

log = self.logger.getChild('run')
Expand Down
12 changes: 6 additions & 6 deletions canflood/hlpr/Q.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,12 +463,12 @@ def print_qt_version(self):
# LOAD/WRITE LAYERS-----------
#===========================================================================

def load_vlay(self,
def load_vlay(self,
fp,
logger=None,
providerLib='ogr',
aoi_vlay = None,
allow_none=True, #control check in saveselectedfeastures
allow_none=True, #control check in saveselectedfeatures
addSpatialIndex=True,
uriParams_d = {'encoding':'System',
'type':'csv',
Expand Down Expand Up @@ -525,7 +525,7 @@ def load_vlay(self,

#check if this is valid
if not vlay_raw.isValid():
raise Error('loaded vlay \'%s\' is not valid. \n \n did you initilize?'%vlay_raw.name())
raise Error('loaded vlay \'%s\' is not valid. \n \n did you initialize?'%vlay_raw.name())

#check if it has geometry
if not providerLib == 'delimitedtext':
Expand Down Expand Up @@ -3894,15 +3894,15 @@ def vlay_new_mlay(#create a new mlay
# prechecks
#=======================================================================
if not isinstance(layname, str):
raise Error('expected a string for layname, isntead got %s'%type(layname))
raise Error('expected a string for layname, instead got %s'%type(layname))

if gtype=='None':
log.warning('constructing mlay w/ \'None\' type')
#=======================================================================
# assemble into new layer
#=======================================================================
#initilzie the layer
EPSG_code=int(crs.authid().split(":")[1]) #get teh coordinate reference system of input_layer
#initialize the layer
EPSG_code=int(crs.authid().split(":")[1]) #get the coordinate reference system of input_layer
uri = gtype+'?crs=epsg:'+str(EPSG_code)+'&index=yes'

vlaym = QgsVectorLayer(uri, layname, "memory")
Expand Down
2 changes: 1 addition & 1 deletion canflood/misc/rfda/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def convert_rfda(self): #Other.Rfda tab
wrkr = RFDAconv(logger=self.logger, out_dir=out_dir, tag=tag, bsmt_ht = bsmt_ht)
self.feedback.setProgress(20)
#======================================================================
# invnentory convert
# inventory convert
#======================================================================
if isinstance(rinv_vlay, QgsVectorLayer):

Expand Down
16 changes: 8 additions & 8 deletions canflood/model/modcom.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def init_model(self, #load and attach control file parameters



def cf_attach_pars(self, #load parmaeteres from file
def cf_attach_pars(self, #load parametersrom file
cpars,
setAttr=True, #whether to save each attribute
):
Expand Down Expand Up @@ -486,7 +486,7 @@ def _cfFile_relative(self, #adding basedir to the control file
sections=['dmg_fps', 'risk_fps'], #parameter sections to manipulate
logger=None,
**kwargs):
"""wraper to work with the control file (rather than the configparser"""
"""wrapper to work with the control file (rather than the configparser"""

#=======================================================================
# defaults
Expand Down Expand Up @@ -537,7 +537,7 @@ def _cf_relative(self, #convert filepaths from relative to absolute
#assert os.path.exists(base_dir)
log.debug('w/ base_dir=%s'%base_dir)
#=======================================================================
# #loop through parser and retireve then convert
# #loop through parser and retrieve then convert
#=======================================================================
res_d = dict() #container for new values
for sectName in sections:
Expand All @@ -550,14 +550,14 @@ def _cf_relative(self, #convert filepaths from relative to absolute

if os.path.exists(valRaw):
"""switchged to warning... some tools may not use this fp"""
log.warning(('%s.%s passed aboslute_fp=False but fp exists \n %s'%(
log.warning(('%s.%s passed abosolute_fp=False but fp exists \n %s'%(
sectName, varName, valRaw)))
continue
else:

#get the absolute filepath
fp = os.path.join(base_dir, valRaw)
"""dont bother... some models may not use all the fps
"""don't bother... some models may not use all the fps
better to let the check with handles catch things
assert os.path.exists(fp), '%s.%s not found'%(sectName, varName)"""
if not os.path.exists(fp) and warn:
Expand Down Expand Up @@ -784,7 +784,7 @@ def set_finv(self, #set some special values from the finv
self.check_finv(df)

#=======================================================================
# resolve column gruops----
# resolve column groups----
#=======================================================================
cdf, prefix_l = self._get_finv_cnest(df)

Expand Down Expand Up @@ -2651,7 +2651,7 @@ def build(self,
self.pars_d = pars_d.copy()

#======================================================================
# extract depth-dmaage data
# extract depth-damage data
#======================================================================

#get just the dd rows
Expand Down Expand Up @@ -2913,7 +2913,7 @@ def _get_split(self,#split the raw df into function and metadata
return ddf.iloc[:,0].to_dict(), mdf.iloc[:,0].to_dict()


def _get_ddf(self): #return a formatted dataframe fo the dd_ar
def _get_ddf(self): #return a formatted dataframe of the dd_ar
return pd.DataFrame(self.dd_ar.T, columns=['exposure', 'impact'])


Expand Down
1 change: 0 additions & 1 deletion canflood/model/risk1.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ def run(self,
# defaults
#======================================================================
log = self.logger.getChild('run')
#ddf_raw, finv, = self.data_d['expos'],self.data_d['finv']
aep_ser = self.data_d['evals']
cid, bid = self.cid, self.bid
bdf ,ddf = self.bdf, self.ddf
Expand Down
74 changes: 53 additions & 21 deletions canflood/results/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@

import hlpr.plug


#from hlpr.Q import *
from hlpr.basic import force_open_dir
from hlpr.exceptions import QError as Error
from model.modcom import Model

import results.djoin
import results.riskPlot
import results.compare
import results.attribution
#import results.reporter


# This loads your .ui file so that PyQt can populate your plugin with the elements from Qt Designer
Expand Down Expand Up @@ -57,7 +55,6 @@ def __init__(self, iface, parent=None, **kwargs):
#custom setup

self.qproj_setup(iface=iface, **kwargs)
#self.connect_slots()

self.logger.debug('ResultsDialog init')

Expand Down Expand Up @@ -107,7 +104,7 @@ def connect_slots(self): #connect your slots


#=======================================================================
# Risk PLot-------------
# Risk Plot-------------
#=======================================================================
self.pushButton_RP_plot.clicked.connect(self.run_plotRisk)
self.pushButton_RP_pStacks.clicked.connect(self.run_pStack)
Expand Down Expand Up @@ -143,7 +140,7 @@ def connect_slots(self): #connect your slots
self.get_cf_par(self.lineEdit_cf_fp.text(), varName=x)
))

#also connect teh layer
#also connect the layer
self.comboBox_JGfinv.layerChanged.connect(
lambda x: self.lineEdit_JG_resfp.setText(
self.get_cf_par(self.lineEdit_cf_fp.text(),
Expand All @@ -160,7 +157,7 @@ def connect_slots(self): #connect your slots
self.setup_comboBox(self.comboBox_jg_relabel,['', 'ari', 'aep'], default='ari')

#styles
def set_style(): #set the style options based on the selecte dlayer
def set_style(): #set the style options based on the selected layer
vlay = self.comboBox_JGfinv.currentLayer()

if not isinstance(vlay, QgsVectorLayer):
Expand Down Expand Up @@ -333,7 +330,7 @@ def run_joinGeo(self):
wrkr = results.djoin.Djoiner(**kwargs).setup()

"""shortened setup... loading the data here"""
#wrkr.init_model() #load teh control file
#wrkr.init_model() #load the control file


#=======================================================================
Expand All @@ -347,7 +344,7 @@ def run_joinGeo(self):

self.feedback.setProgress(75)
#=======================================================================
# load and styleize
# load and stylize
#=======================================================================
self._load_toCanvas(res_vlay, logger=log, style_fn = self.comboBox_JG_style.currentText())

Expand Down Expand Up @@ -437,7 +434,7 @@ def run_pStack(self): #single risk plot of total results

self.feedback.setProgress(20)
#=======================================================================
# #execute
# execute
#=======================================================================
if self.checkBox_RP_aep.isChecked():
fig = wrkr.plot_stackdRCurves(stack_dxind, sEAD_ser, y1lab='AEP')
Expand Down Expand Up @@ -478,7 +475,7 @@ def run_pNoFail(self): #plot split between totals and no-fail
si_ttl = wrkr.get_slice_noFail()
self.feedback.setProgress(20)
#=======================================================================
# #execute
# execute
#=======================================================================
if self.checkBox_RP_aep.isChecked():
fig = wrkr.plot_slice(si_ttl, y1lab='AEP')
Expand All @@ -497,7 +494,7 @@ def run_pNoFail(self): #plot split between totals and no-fail
log.push('pNoFail finished')


def _set_fps(self, logger=None): #retrieve control file paths on Cmopare/Combine tab from user
def _set_fps(self, logger=None): #retrieve control file paths on Compare/Combine tab from user
if logger is None: logger=self.logger
log=logger.getChild('_set_fps')

Expand Down Expand Up @@ -538,8 +535,37 @@ def _set_fps(self, logger=None): #retrieve control file paths on Cmopare/Combine
log.debug('pars w/ %i keys'%(len(fps_d)))

return fps_d

# Retrieve inventory table from results
def get_finv(self):
self._set_setup(set_cf_fp=True)

#=======================================================================
# get finv from control file
#=======================================================================
kwargs = {attn:getattr(self, attn) for attn in self.inherit_fieldNames}
#wrkr = results.riskPlot.RiskPlotr(**kwargs).setup()

with Model(**kwargs) as wrkr:
#load the control file
wrkr.init_model(check_pars=False)

#load data from teh control file
dtag_d = {k:d for k,d in wrkr.dtag_d.items() if k=='finv'}
wrkr.load_df_ctrl(dtag_d=dtag_d)
df_raw = wrkr.raw_d.pop('finv')


#===============================================================================
# finv_fp = wrkr.finv
#
# assert os.path.exists(finv_fp), 'passed invalid finv file path: %s'%finv_fp
# df_raw = pd.read_csv(finv_fp, index_col=0)
#===============================================================================

#df = df_raw.head(10)

return df_raw

def run_compare(self):
log = self.logger.getChild('run_compare')
Expand Down Expand Up @@ -572,7 +598,7 @@ def run_compare(self):

self.feedback.setProgress(70)
#=======================================================================
# #plot curves
# plot curves
#=======================================================================
if self.checkBox_C_ari.isChecked():
fig = wrkr.riskCurves(y1lab='impacts')
Expand Down Expand Up @@ -615,7 +641,7 @@ def run_combine(self):
#===========================================================================
# get data
#===========================================================================
cdxind, cWrkr = wrkr.build_composite()
cWrkr = wrkr.build_composite()


self.feedback.setProgress(70)
Expand Down Expand Up @@ -679,7 +705,7 @@ def run_cba_copy(self):
#=======================================================================
# update gui
#=======================================================================
"""populate for conveience of the cba_plot tool"""
"""populate for convenience of the cba_plot tool"""
self.lineEdit_cba_cf.setText(ofp)

#=======================================================================
Expand Down Expand Up @@ -750,11 +776,14 @@ def run_reporter(self):
# retrieve from other functions
#=======================================================================
geo_vlay = self.comboBox_rpt_vlay.currentLayer()

plots_d = self.run_plotRisk(plt_window=False)

finv_df = self.get_finv() #retrieve the inventory frame
assert isinstance(finv_df, pd.DataFrame), 'failed to load finv'
#=======================================================================
# init
#=======================================================================
#=======================================================================
from results.reporter import ReportGenerator

kwargs = {attn:getattr(self, attn) for attn in ['logger', 'tag', 'cf_fp',
Expand All @@ -766,20 +795,23 @@ def run_reporter(self):

#start the report
report = wrkr.add_report()
self.feedback.setProgress(30)
self.feedback.setProgress(20)

wrkr.add_header()
self.feedback.setProgress(35)
self.feedback.setProgress(30)

#add the map section
if isinstance(geo_vlay, QgsVectorLayer):
wrkr.add_map(vlay=geo_vlay)
self.feedback.setProgress(40)
self.feedback.setProgress(35)

wrkr.add_finv_smry(finv_df) #making this mandatory
self.feedback.setProgress(45)

# add the total plots
for name, fp in plots_d.items():
wrkr.add_picture(fp)
self.feedback.setProgress(45)
self.feedback.setProgress(50)


# add the control file report
Expand Down
Loading

0 comments on commit ab71f6e

Please sign in to comment.