Skip to content

Commit

Permalink
allow to prevent batch modification of parameters (close #107)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Dec 8, 2015
1 parent f67ed39 commit 3f49a10
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 13 deletions.
10 changes: 2 additions & 8 deletions pycorrfit/fcs_data_set.py
Expand Up @@ -188,8 +188,6 @@ def __init__(self, backgrounds=[], correlation=None, corr_type="AC",
self.fit_weight_type = fit_weight_type
self.fit_weight_parameters = fit_weight_data

# lock prevents any changes to the parameters
self.lock_parameters = False
self.normparm = normparm
self.title = title
self.traces = traces
Expand Down Expand Up @@ -452,17 +450,13 @@ def fit_weight_data(self, value):
@property
def fit_parameters(self):
"""parameters that were fitted/will be used for fitting"""
return self._fit_parameters
return self._fit_parameters.copy()

@fit_parameters.setter
def fit_parameters(self, value):
# must unlock parameters, if change is required
value = np.array(value)
if self.lock_parameters == False:
self._fit_parameters = self.check_parms(value)
else:
warnings.warn("Correlation {}: fixed parameters unchanged.".
format(self.uid))
self._fit_parameters = self.check_parms(value)

@property
def fit_parameters_range(self):
Expand Down
18 changes: 17 additions & 1 deletion pycorrfit/frontend.py
Expand Up @@ -1429,9 +1429,17 @@ def OnOpenSession(self, e=None, sessionfile=None):
except:
pass
try:
Infodict["Preferences"] # not used yet
prefdict = Infodict["Preferences"]
except:
pass
else:
for key in prefdict:
if key == "Pages prevent batch modification":
for pid in prefdict[key]:
for i in np.arange(N):
page = self.notebook.GetPage(i)
if page.counter.strip("#: ") == pid:
page.prevent_batch_modification = True
if self.notebook.GetPageCount() > 0:
# Enable the "Current" Menu
self.EnableToolCurrent(True)
Expand Down Expand Up @@ -1591,6 +1599,14 @@ def OnSaveSession(self,e=None):
Infodict["External Weights"][counter] = external_weights
# Append Session Comment:
Infodict["Comments"]["Session"] = self.SessionComment
# Protected pages:
protpage = []
for i in np.arange(N):
# Set Page
Page = self.notebook.GetPage(i)
if Page.prevent_batch_modification:
protpage.append(Page.counter.strip("#: "))
Infodict["Preferences"]["Pages prevent batch modification"] = protpage
# File dialog
dlg = wx.FileDialog(self, "Save session file", self.dirname, "",
"PyCorrFit session (*.pcfs)|*.pcfs|All files (*.*)|*.*",
Expand Down
36 changes: 35 additions & 1 deletion pycorrfit/openfile.py
Expand Up @@ -258,6 +258,28 @@ def LoadSessionData(sessionfile, parameters_only=False):
Wdata.append(np.float(row[0]))
Weightsdict[pageid][Nkey] = np.array(Wdata)
Infodict["External Weights"] = Weightsdict
## Preferences
preferencesname = "preferences.cfg"
try:
# Raises KeyError, if file is not present:
Arc.getinfo(preferencesname)
except:
pass
else:
prefdict = {}
with Arc.open(preferencesname) as fd:
data = fd.readlines()
for line in data:
line = line.strip()
if len(line) == 0 or line.startswith("#"):
continue
key, value = line.split("=")
key = key.strip()
value = value.strip()
if value.count(","):
value = [ v.strip() for v in value.split(",")]
prefdict[key] = value
Infodict["Preferences"] = prefdict
Arc.close()
return Infodict

Expand All @@ -278,7 +300,7 @@ def SaveSessionData(sessionfile, Infodict):
"External Functions, dict": modelids to external model functions
"External Weights", dict: page numbers, external weights for fitting
"Parameters", dict: page numbers, all parameters of the pages
"Preferences", dict: not used yet
"Preferences", dict: fixed page parameters
"Traces", dict: page numbers, all traces of the pages
Expand Down Expand Up @@ -493,6 +515,18 @@ def SaveSessionData(sessionfile, Infodict):
WeightFile.close()
Arc.write(WeightFilename)
os.remove(os.path.join(tempdir, WeightFilename))
## Preferences
preferencesname = "preferences.cfg"
with open(preferencesname, 'w') as fd:
for key in Infodict["Preferences"]:
value = Infodict["Preferences"][key]
if isinstance(value, list):
value = " ".join("{}".format(it) for it in value)
else:
value = "{}".format(value)
fd.write("{} = {}\n".format(key, value))
Arc.write(preferencesname)
os.remove(os.path.join(tempdir, preferencesname))
## Readme
rmfilename = "Readme.txt"
rmfile = open(rmfilename, 'wb')
Expand Down
15 changes: 14 additions & 1 deletion pycorrfit/page.py
Expand Up @@ -132,6 +132,14 @@ def IsCrossCorrelation(self):
def modelid(self):
return self.corr.fit_model.id

@property
def prevent_batch_modification(self):
return self.wxCBPreventBatchParms.GetValue()

@prevent_batch_modification.setter
def prevent_batch_modification(self, value):
self.wxCBPreventBatchParms.SetValue(value)

@property
def title(self):
return self.tabtitle.GetValue()
Expand Down Expand Up @@ -740,7 +748,12 @@ def settings(self):
self.panelsettings.sizer = wx.BoxSizer(wx.VERTICAL)
self.panelsettings.sizer.Add(sizerti)
self.panelsettings.sizer.Add(box1)
# Add button "Apply" and "Set range"
# checkbox "Protect from batch control"
self.wxCBPreventBatchParms = wx.CheckBox(self.panelsettings,
-1,
"prevent batch modification")
box1.Add(self.wxCBPreventBatchParms)
# buttons "Apply" and "Set range"
horzs = wx.BoxSizer(wx.HORIZONTAL)
buttonapply = wx.Button(self.panelsettings, label="Apply")
self.Bind(wx.EVT_BUTTON, self.PlotAll, buttonapply)
Expand Down
31 changes: 29 additions & 2 deletions pycorrfit/tools/batchcontrol.py
Expand Up @@ -74,8 +74,10 @@ def __init__(self, parent):
wx.Frame.SetIcon(self, parent.MainIcon)
self.Show(True)


def OnApply(self, event):

def GetParameters(self):
""" The parameters
"""
# Get the item from the dropdown list
item = self.dropdown.GetSelection()
if self.rbtnhere.Value == True:
Expand All @@ -91,12 +93,37 @@ def OnApply(self, event):
else:
# Get Parameters from different session
Parms = self.YamlParms[item]
return Parms


def GetProtectedParameterIDs(self):
""" The model parameters that are protected from batch control
"""
Parms = self.GetParameters()
pbool = np.ones(len(Parms[2]), dtype=bool)
return pbool


def OnApply(self, event):
Parms = self.GetParameters()
modelid = Parms[1]
# Set all parameters for all pages
for i in np.arange(self.parent.notebook.GetPageCount()):
OtherPage = self.parent.notebook.GetPage(i)
if OtherPage.corr.fit_model.id == modelid and OtherPage.corr.correlation is not None:
# create a copy of the fitting parameters in
# case we want to protect them
proparms = OtherPage.corr.fit_parameters
self.parent.UnpackParameters(Parms, OtherPage)
if OtherPage.wxCBPreventBatchParms.GetValue():
# write back protected parameters
OtherPage.corr.fit_parameters = proparms
OtherPage.apply_parameters_reverse()
else:
# write back only selected parameters
pbool = self.GetProtectedParameterIDs()
OtherPage.corr.fit_parameters[pbool] = proparms[pbool]
OtherPage.PlotAll(trigger="parm_batch")
# Update all other tools fit the finalize trigger.
self.parent.OnFNBPageChanged(trigger="parm_finalize")
Expand Down

0 comments on commit 3f49a10

Please sign in to comment.