Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Adding Event summary table underneath diagrams #51

Merged
merged 4 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions canflood/results/dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,23 @@ def get_finv(self):
wrkr.load_df_ctrl(dtag_d=dtag_d)
df_raw = wrkr.raw_d.pop('finv')


return df_raw

def get_r_ttl(self):
self._set_setup(set_cf_fp=True)

#=======================================================================
# get r_ttl from control file
#=======================================================================
kwargs = {attn:getattr(self, attn) for attn in self.inherit_fieldNames}

with Model(**kwargs) as wrkr:
wrkr.init_model(check_pars=False)


df_raw = pd.read_csv(wrkr.r_ttl)

return df_raw

def run_compare(self):
Expand Down Expand Up @@ -809,7 +826,11 @@ def run_reporter(self):

finv_df = self.get_finv() #retrieve the inventory frame
assert isinstance(finv_df, pd.DataFrame), 'failed to load finv'



r_ttl_df = self.get_r_ttl() #retrieve the event summary table
assert isinstance(r_ttl_df, pd.DataFrame), 'failed to load r_ttl'

#=======================================================================
# init
#=======================================================================
Expand Down Expand Up @@ -839,7 +860,7 @@ def run_reporter(self):

# add the total plots
for name, fp in plots_d.items():
wrkr.add_picture(fp)
wrkr.add_picture(fp=fp, df=r_ttl_df.iloc[:, 0:2])
self.feedback.setProgress(50)

#add the damage function library plot
Expand Down
128 changes: 71 additions & 57 deletions canflood/results/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ def add_label(self,
qrect=QRectF(5, 20, 200, 10),
text_size=8,
text_bold=True,
text_underline=False,
**kwargs):

#=======================================================================
Expand All @@ -369,6 +370,7 @@ def add_label(self,
font = QtGui.QFont()
font.setPointSize(text_size)
font.setBold(text_bold)
font.setUnderline(text_underline)
label.setFont(font)

qlayout.addLayoutItem(label)
Expand Down Expand Up @@ -427,6 +429,7 @@ def add_html(self, #add content from an html file

def add_picture(self,
fp, #path to svg file to load
df=None, #dataframe to use for the event summary table
qlayout=None,
report=None,
):
Expand All @@ -447,13 +450,29 @@ def add_picture(self,
layItem_pic.setPicturePath(fp)

qlayout.addLayoutItem(layItem_pic)

# Check that event summary table is present
if df is not None:
#format table
df.columns = map(str.upper, df.columns)
df.iloc[:, 1] = df.iloc[:, 1].map(lambda impact_val: self.impactFmtFunc(impact_val))

#convert to layer
df_layer = self.vlay_new_df2(df, layname='event_summary_table',
logger=log)
# Add table header
self.add_label(qlayout=qlayout, text='Event Summary Table', qrect=QRectF(5, 145, 100, 100),
text_size=16, text_bold=False, text_underline=True)
# Add the table under the picture
self.add_table(df_layer, qlayout=qlayout,
qrect=QRectF(25, 160, 160, 49.050), column_width=34)

log.debug('added item from %s'%fp)

return layItem_pic

#===========================================================================
# Param 'finv': Inventory file DataFrame object
# Param 'finv_df_raw': Inventory file DataFrame object
#===========================================================================
def add_finv_smry(self, finv_df_raw, qlayout=None, report=None):
#=======================================================================
Expand All @@ -462,7 +481,7 @@ def add_finv_smry(self, finv_df_raw, qlayout=None, report=None):
if qlayout is None:
qlayout = self.add_section(report=report)

log = self.logger.getChild('add_table')
log = self.logger.getChild('add_finv_smry')

project = self.qproj

Expand All @@ -474,76 +493,71 @@ def add_finv_smry(self, finv_df_raw, qlayout=None, report=None):
#=======================================================================
# # Create layout and vector layer with finv file path
#=======================================================================


finv_layer = self.vlay_new_df2(finv_df.reset_index(), layname='finv_summary_table',
logger=log)

#===============================================================================
# finv_layer = QgsVectorLayer('none', 'finv_summary_table', 'memory')
#
# finv_layer.startEditing()
#
# # Add fields
# index_field = QgsField(finv_df.index.name, QVariant.Int)
# finv_layer.addAttribute(index_field)
#
# for head in finv_df:
# field = QgsField(head, QVariant.String)
# finv_layer.addAttribute(field)
#
# finv_layer.updateFields()
#
# # Add features
# for row in finv_df.itertuples():
# feature = QgsFeature()
# feature.setAttributes([row[0], row[1], row[2]])
# finv_layer.addFeature(feature)
#
# finv_layer.commitChanges()
#===============================================================================

# Needs to be added to project instance as a temporary file to prevent data loss
project.addMapLayer(finv_layer)
self.add_table(df_layer=finv_layer, qrect=QRectF(25, 40, 160, 67.050),
qlayout=qlayout, report=report)

#=======================================================================
# add title
#=======================================================================
label = self.add_label(qlayout=qlayout, text='Inventory Summary', text_size=20)

#=======================================================================
# wrap
#=======================================================================

log.debug('added table from %s'%str(finv_df.shape))

#===========================================================================
# Param 'df_layer': QgsVectorLayer object derived from DataFrame
# Param 'qrect': QRectF object to specify size and placement of table
# Param 'qlayout': QgsLayout object to add table to
# Param 'report': QgsReport object to add layout to
#===========================================================================
def add_table(self, df_layer, qrect=None, column_width=50, qlayout=None, report=None):
#=======================================================================
# # Set table layer
# defaults
#=======================================================================
finv_table = QgsLayoutItemAttributeTable.create(qlayout)
finv_table.setVectorLayer(finv_layer)
if qlayout is None:
qlayout = self.add_section(report=report)

if qrect is None:
qrect = QRectF(25, 20, 160, 67.050)

log = self.logger.getChild('add_table')

project = self.qproj

# Needs to be added to project instance as a temporary file to prevent data loss
project.addMapLayer(df_layer)

#=======================================================================
# Set table layer
#=======================================================================
table = QgsLayoutItemAttributeTable.create(qlayout)
table.setVectorLayer(df_layer)

# Resize columns and set alignment to centre
columns = finv_table.columns()
# Resize columns and set alignment to centre
columns = table.columns()
for column in columns:
column.setHAlignment(Qt.AlignHCenter)
column.setWidth(50)
column.setWidth(column_width)

# Set table column styling and refresh table to display with new styling
finv_table.setColumns(columns)
finv_table.refresh()
table.setColumns(columns)
table.refresh()

#=======================================================================
# # Add the frame
#=======================================================================
finv_frame = QgsLayoutFrame(qlayout, finv_table)
finv_frame.attemptSetSceneRect(QRectF(25, 40, 160, 67.050))
finv_frame.setFrameEnabled(True)
finv_table.addFrame(finv_frame)

# Add the frame to the layout
qlayout.addMultiFrame(finv_table)

#=======================================================================
# add title
#=======================================================================
label = self.add_label(qlayout=qlayout, text='Inventory Summary', text_size=20)

#=======================================================================
# wrap
# Add the frame
#=======================================================================
frame = QgsLayoutFrame(qlayout, table)
frame.attemptSetSceneRect(qrect)
frame.setFrameEnabled(True)
table.addFrame(frame)

log.debug('added table from %s'%str(finv_df.shape))
# Add the frame to the layout
qlayout.addMultiFrame(table)

return finv_table
return table