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

Separate model view from view settings for more reliable reload #99

Merged
merged 32 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
e42ecd9
rough draft of only restoring view settings, not model settings - thi…
kkiesling Jun 9, 2022
a253599
remove commented lines and functions that are no longer needed
kkiesling Jun 9, 2022
0c6d89f
remove unnecessary import
kkiesling Jun 13, 2022
7ce9e4b
remove missed print statement
kkiesling Jun 22, 2022
5ee873b
more streamlined to use composition over inheritance
kkiesling Jun 22, 2022
1b187f7
trying to asses view equality and populating ids
kkiesling Jun 22, 2022
748baf8
trying to separate just the view parameters from the rest of view ind…
kkiesling Jun 23, 2022
b130f62
separate out only the _PlotBase parameters and check those
kkiesling Jun 23, 2022
95ef00e
only store necessary info for reload in pickle file
kkiesling Jun 24, 2022
4d371be
cleaned up docstrings
kkiesling Jun 29, 2022
e9e92a0
make view params attribute on plotview to simplify
kkiesling Jul 6, 2022
3e996c9
remove debugging print statements
kkiesling Jul 6, 2022
eb34f8b
need to copy view params from previous view
kkiesling Jul 7, 2022
ac39180
updated docstrings for new classes
kkiesling Jul 7, 2022
0ff3ed4
Apply suggestions from code review
kkiesling Jul 8, 2022
ed014f5
issue warning and ignore plot settings if load fails
kkiesling Jul 8, 2022
81b41bb
typo
kkiesling Jul 8, 2022
8051d2f
remove mistaken imports
kkiesling Jul 8, 2022
3ab3f4b
ids_map and prop_map don't need to be private
kkiesling Jul 8, 2022
7e3ecd3
wording changes
kkiesling Jul 11, 2022
ca2eab5
need to add view_params to list of attributes
kkiesling Jul 11, 2022
6ad6cde
increment plotmodel version
kkiesling Jul 11, 2022
08a9a38
simplify try/except statement
kkiesling Jul 11, 2022
635fbf9
use_settings_pkl in docstring
kkiesling Jul 11, 2022
9990c72
store full currentView so that view_ind and view_params are both saved
kkiesling Jul 11, 2022
6a5f909
list of attribures as class attribute
kkiesling Jul 11, 2022
05743d8
restore domain properties if file hashes are unchanged as well
kkiesling Jul 11, 2022
56e12dd
prop_map and properties attributes were redundant
kkiesling Jul 11, 2022
90bf435
adopt_plotbase must be in PlotView to be able to set params
kkiesling Jul 11, 2022
8825405
make cell_ids, instances, and mat_ids properties
kkiesling Jul 11, 2022
ebe5460
make initial view the currentview and always create a default view
kkiesling Jul 13, 2022
9ae7a76
compare active and current views before updating current view to chec…
kkiesling Jul 13, 2022
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
84 changes: 11 additions & 73 deletions openmc_plotter/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
except ImportError:
_HAVE_VTK = False

from .plotmodel import PlotModel, DomainTableModel
from .plotmodel import PlotModel, DomainTableModel, PlotView
from .plotgui import PlotImage, ColorDialog
from .docks import DomainDock, TallyDock
from .overlays import ShortcutsOverlay
Expand All @@ -41,17 +41,6 @@ def _openmcReload(threads=None):
openmc.lib.init(args)
openmc.lib.settings.verbosity = 1

def hash_file(filename):
# return the md5 hash of a file
h = hashlib.md5()
with open(filename,'rb') as file:
chunk = 0
while chunk != b'':
# read 32768 bytes at a time
chunk = file.read(32768)
h.update(chunk)
return h.hexdigest()

class MainWindow(QMainWindow):
def __init__(self,
font=QtGui.QFontMetrics(QtGui.QFont()),
Expand Down Expand Up @@ -463,10 +452,8 @@ def loadModel(self, reload=False, use_settings_pkl=True):
if reload:
self.resetModels()
else:
# create new plot model
self.model = PlotModel()
if use_settings_pkl:
self.restoreModelSettings()
self.model = PlotModel(use_settings_pkl)

# update plot and model settings
self.updateRelativeBases()

Expand Down Expand Up @@ -1074,51 +1061,6 @@ def restoreWindowSettings(self):

self.colorDialog.setVisible(is_visible)

def restoreModelSettings(self):
if os.path.isfile("plot_settings.pkl"):

with open('plot_settings.pkl', 'rb') as file:
model = pickle.load(file)

# check if loaded cell/mat ids hash match the pkl file:
current_mat_xml_hash = hash_file('materials.xml')
current_geom_xml_hash = hash_file('geometry.xml')
if (current_mat_xml_hash != model.mat_xml_hash) or \
(current_geom_xml_hash != model.geom_xml_hash):
# hashes do not match so ignore plot_settings.pkl file
msg_box = QMessageBox()
msg = "WARNING: Model has changed since storing plot " +\
"settings. Ignoring previous plot settings."
msg_box.setText(msg)
msg_box.setIcon(QMessageBox.Warning)
msg_box.setStandardButtons(QMessageBox.Ok)
msg_box.exec_()
return

# do not replace model if the version is out of date
if model.version != self.model.version:
print("WARNING: previous plot settings are for a different "
"version of the GUI. They will be ignored.")
wrn_msg = "Existing version: {}, Current GUI version: {}"
print(wrn_msg.format(model.version, self.model.version))
return

try:
self.model.statepoint = model.statepoint
except OSError:
msg_box = QMessageBox()
msg = "Could not open statepoint file: \n\n {} \n"
msg_box.setText(msg.format(self.model.statepoint.filename))
msg_box.setIcon(QMessageBox.Warning)
msg_box.setStandardButtons(QMessageBox.Ok)
msg_box.exec_()
self.model.statepoint = None

self.model.currentView = model.currentView
self.model.activeView = copy.deepcopy(model.currentView)
self.model.previousViews = model.previousViews
self.model.subsequentViews = model.subsequentViews

def resetModels(self):
self.cellsModel = DomainTableModel(self.model.activeView.cells)
self.materialsModel = DomainTableModel(self.model.activeView.materials)
Expand Down Expand Up @@ -1209,20 +1151,16 @@ def closeEvent(self, event):
self.saveSettings()

def saveSettings(self):
if self.model.statepoint:
self.model.statepoint.close()

if len(self.model.previousViews) > 10:
self.model.previousViews = self.model.previousViews[-10:]
if len(self.model.subsequentViews) > 10:
self.model.subsequentViews = self.model.subsequentViews[-10:]

# get hashes for geometry.xml and material.xml at close
self.model.mat_xml_hash = hash_file('materials.xml')
self.model.geom_xml_hash = hash_file('geometry.xml')

pickle_data = {
'version': self.model.version,
'currentView_ind': self.model.currentView.view_ind,
'statepoint': self.model.statepoint
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason we can't just save the entire currentView? As is, this misses other information. For example, if you change cell/material color assignments, close the plotter, and re-open, those assignments are gone.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The goal of this was to prevent having to store the large arrays with the cell/mat/property data to the pickle file smaller. Plus the goal of this PR was to separate the model-dependent properties from model-independent properties. Though I can see how this property is in the gray zone. If the material/cells IDs change, then the mapping of colors to materials/is no longer valid. I will play with it and see what can be done. Might have to go back to using hashes in that if the hashes are unchanged, the entire current view can be reused, but if the hashes differ, then use only the independent view settings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, and I am just remembering that we stored the full model in the past which is where that large amount of excess data came from. So storing the full currentView should be no issue.

with open('plot_settings.pkl', 'wb') as file:
if self.model.statepoint:
self.model.statepoint.close()
pickle.dump(self.model, file)
pickle.dump(pickle_data, file)

def exportTallyData(self):
# show export tool dialog
Expand Down
Loading