Commit 11660543 authored by eric pellegrini's avatar eric pellegrini
Browse files

refactored the QVectorsPlugin

Bug fix in UD_STORE
parent a154add2
import wx
import wx.lib.agw.pygauge as pygauge
from MDANSE.Framework.Status import StatusHandler
from MDANSE.Framework.Status import Status
from MDANSE.App.GUI.Icons import ICONS
class ProgressBar(wx.Panel,StatusHandler):
class ProgressBar(wx.Panel,Status):
def __init__(self, *args, **kwargs):
wx.Panel.__init__(self,*args, **kwargs)
StatusHandler.__init__(self)
Status.__init__(self)
sizer = wx.BoxSizer(wx.HORIZONTAL)
......@@ -29,7 +29,7 @@ class ProgressBar(wx.Panel,StatusHandler):
status = message
if status != self._status:
if status != self:
return
self._progress.SetBarColor(wx.BLUE)
......@@ -44,25 +44,25 @@ class ProgressBar(wx.Panel,StatusHandler):
return
self._stop.Disable()
self._status.stop()
self.stop()
def start_status(self, message):
status = message
if status != self._status:
if status != self:
return
self._stop.Enable()
self._progress.SetValue(self._status.currentStep)
self._progress.SetRange(self._status.nSteps)
self._progress.SetValue(self.currentStep)
self._progress.SetRange(self.nSteps)
self._progress.SetBarColor(wx.GREEN)
def stop_status(self, message):
status = message
if status != self._status:
if status != self:
return
self._progress.SetBarColor(wx.RED)
......@@ -72,8 +72,8 @@ class ProgressBar(wx.Panel,StatusHandler):
status = message
if status != self._status:
if status != self:
return
self._progress.SetValue(self._status.currentStep)
self._progress.SetValue(self.currentStep)
self._progress.Refresh()
......@@ -413,15 +413,15 @@ class AtomSelectionPlugin(ComponentPlugin):
LOGGER("You must enter a value for the user definition", "error", ["dialog"])
return
path = os.path.basename(self.parent.trajectory.filename)
target = os.path.basename(self.parent.trajectory.filename)
ud = REGISTRY['user_definition']['atom_selection'](path,expression=self._expression,indexes=self._selection)
ud = REGISTRY['user_definition']['atom_selection'](target,expression=self._expression,indexes=self._selection)
UD_STORE[name] = ud
UD_STORE.set_definition(target,ud.type,name,ud)
UD_STORE.save()
pub.sendMessage("new_selection", message = (path, name))
pub.sendMessage("new_selection", message = (target, name))
def on_select_atoms(self, message):
......
......@@ -79,13 +79,13 @@ class AtomTransmutationPlugin(AtomSelectionPlugin):
LOGGER("You must enter a value for the user definition", "error", ["dialog"])
return
path = os.path.basename(self.parent.trajectory.filename)
target = os.path.basename(self.parent.trajectory.filename)
element = self._elements.GetStringSelection()
ud = REGISTRY['user_definition']['atom_transmutation'](path,expression=self._expression,indexes=self._selection,element=element)
UD_STORE[name] = ud
ud = REGISTRY['user_definition']['atom_transmutation'](target,expression=self._expression,indexes=self._selection,element=element)
UD_STORE.set_definition(target,ud.type,name,ud)
UD_STORE.save()
Publisher.sendMessage("new_transmutation", message = (path, name))
Publisher.sendMessage("new_transmutation", message = (target, name))
\ No newline at end of file
......@@ -139,7 +139,7 @@ class AxisSelectionPlugin(ComponentPlugin):
LOGGER("An axis can not be set from identical endpoints selections.", "error", ["dialog"])
return
path = os.path.basename(self._trajectory.filename)
target = os.path.basename(self._trajectory.filename)
name = self._udPanel.get_selection_name()
......@@ -151,12 +151,12 @@ class AxisSelectionPlugin(ComponentPlugin):
atomNames1 = tuple([self._atomNames1.GetString(idx) for idx in atomIndexes1])
atomNames2 = tuple([self._atomNames2.GetString(idx) for idx in atomIndexes2])
ud = REGISTRY['user_definition']['axis_selection'](path,molecule=molName,endpoint1=atomNames1,endpoint2=atomNames2)
ud = REGISTRY['user_definition']['axis_selection'](target,molecule=molName,endpoint1=atomNames1,endpoint2=atomNames2)
UD_STORE[name] = ud
UD_STORE.set_definition(target,ud.type,name,ud)
UD_STORE.save()
pub.sendMessage("new_axis", message = (path, name))
pub.sendMessage("new_axis", message = (target, name))
def on_select_atom(self, event):
......
......@@ -151,19 +151,19 @@ class BasisSelectionPlugin(ComponentPlugin):
LOGGER("You must enter a value for the user definition", "error", ["dialog"])
return
path = os.path.basename(self._trajectory.filename)
target = os.path.basename(self._trajectory.filename)
molName = self._moleculeNames.GetStringSelection()
origin = tuple([self._origin.GetString(idx) for idx in origin])
xAxis = tuple([self._xAxis.GetString(idx) for idx in xAxis])
yAxis = tuple([self._yAxis.GetString(idx) for idx in yAxis])
ud = REGISTRY['user_definition']['basis_selection'](path,molecule=molName,origin=origin,x_axis=xAxis,y_axis=yAxis)
ud = REGISTRY['user_definition']['basis_selection'](target,molecule=molName,origin=origin,x_axis=xAxis,y_axis=yAxis)
UD_STORE[name] = ud
UD_STORE.set_definition(target,ud.type,name,ud)
UD_STORE.save()
pub.sendMessage("new_basis", message = (path, name))
pub.sendMessage("new_basis", message = (target, name))
def on_select_atom(self, event):
......
......@@ -179,7 +179,7 @@ class QVectorsPanel(wx.Panel):
q_vectors=self._grid.GetTable().data,
is_lattice=self._generator.is_lattice)
UD_STORE[name] = ud
UD_STORE.set_definition(target,ud.type,name,ud)
UD_STORE.save()
pub.sendMessage("new_q_vectors", message = (target, name))
......@@ -276,17 +276,17 @@ class QVectorsPlugin(ComponentPlugin):
event.Veto()
else:
qPanel = event.GetEventObject().GetPage(event.GetSelection())
if qPanel.progress.status.is_running():
if qPanel.progress.is_running():
d = wx.MessageDialog(None, 'The background task is still running. You must cancel it before closing this tab.', 'Warning', wx.OK|wx.ICON_WARNING)
d.ShowModal()
event.Veto()
return
def generate_q_vectors(self,generator,status):
def generate_q_vectors(self,generator):
data = generator.run(status)
return data
generator.generate()
return generator['q_vectors']
def generate_q_vectors_done(self,result,qPanel):
......@@ -301,17 +301,19 @@ class QVectorsPlugin(ComponentPlugin):
def on_generate_q_vectors(self, event):
generator = self._generatorsChoice.GetStringSelection()
generator = REGISTRY["qvectors"][generator](self._trajectory.universe)
generator = REGISTRY["q_vectors"][generator](self._trajectory.universe)
parameters = self._configurationPanel.get_value()
generator.configure(parameters)
generator.setup(parameters)
qPanel = QVectorsPanel(generator,parameters,self._mainPanel,wx.ID_ANY)
self._notebook.AddPage(qPanel, "Q Vectors")
generator.setStatus(qPanel.progress)
qPanel.udName.Disable()
qPanel.saveButton.Disable()
startWorker(self.generate_q_vectors_done, self.generate_q_vectors, cargs=(qPanel,),wargs=(generator,qPanel.progress.status))
startWorker(self.generate_q_vectors_done, self.generate_q_vectors, cargs=(qPanel,),wargs=(generator,))
def on_select_generator(self, event):
......@@ -320,9 +322,9 @@ class QVectorsPlugin(ComponentPlugin):
def set_generators(self):
if self._trajectory.universe.is_periodic:
choices = REGISTRY["qvectors"].keys()
choices = REGISTRY["q_vectors"].keys()
else:
choices = [k for k,v in REGISTRY["qvectors"].items() if not v.is_lattice]
choices = [k for k,v in REGISTRY["q_vectors"].items() if not v.is_lattice]
self._generatorsChoice.SetItems(choices)
self._generatorsChoice.SetSelection(0)
......@@ -343,13 +345,13 @@ class QVectorsPlugin(ComponentPlugin):
if generatorName == self._currentGenerator:
return
self._currentGenerator = REGISTRY["qvectors"][generatorName](self._trajectory.universe)
self._currentGenerator = REGISTRY["q_vectors"][generatorName](self._trajectory.universe)
self._parametersSizer.Clear(deleteWindows=True)
self.Freeze()
self._configurationPanel = ConfigurationPanel(self._parametersPanel, self._currentGenerator.configuration)
self._configurationPanel = ConfigurationPanel(self._parametersPanel, self._currentGenerator)
self._parametersSizer.Add(self._configurationPanel, 1, wx.ALL|wx.EXPAND, 5)
......@@ -365,7 +367,7 @@ if __name__ == "__main__":
from MMTK.Trajectory import Trajectory
t = Trajectory(None,"../../../UserData/Trajectories/mmtk/protein_in_periodic_universe.nc","r")
t = Trajectory(None,"../../../../../Data/Trajectories/MMTK/protein_in_periodic_universe.nc","r")
app = wx.App(False)
......
......@@ -27,7 +27,7 @@
'''
Created on Apr 14, 2015
@author: Gael Goret
@author: Gael Goret and Eric C. Pellegrini
'''
import wx
......@@ -90,32 +90,22 @@ class UserDefinitionViewerPlugin(ComponentPlugin):
self.Bind(wx.EVT_TREE_BEGIN_LABEL_EDIT, self.on_try_rename, self._tree)
def build_plugins_tree(self):
for k,v in UD_STORE.iteritems():
self._udTree.setdefault(v.target,{}).setdefault(v.type,{})[k] = v
self._root = self._tree.AddRoot("root")
self.set_plugins_tree(self._root, self._udTree)
self.set_plugins_tree(self._root, UD_STORE)
def set_plugins_tree(self, node, data):
for k, v in data.items():
dataItem = wx.TreeItemData(v)
subnode = self._tree.AppendItem(node, str(k), data=dataItem)
self._tree.SetItemTextColour(subnode, 'blue')
if isinstance(v, IUserDefinition):
self.set_plugins_tree(subnode, v)
self._tree.SetItemTextColour(subnode, 'red')
elif isinstance(v, dict):
if isinstance(v, dict):
self.set_plugins_tree(subnode, v)
self._tree.SetItemTextColour(subnode, 'blue')
else:
self._tree.SetItemTextColour(subnode, 'green')
def on_show_info(self, event):
ItemData = self._tree.GetItemData(event.GetItem())
......@@ -144,7 +134,7 @@ class UserDefinitionViewerPlugin(ComponentPlugin):
if isinstance(data, IUserDefinition):
d = wx.MessageDialog(None, 'Do you really want to delete this definition ?', 'Question', wx.YES_NO|wx.YES_DEFAULT|wx.ICON_QUESTION)
if d.ShowModal() == wx.ID_YES:
del UD_STORE[itemText]
UD_STORE.remove_definition(data.target,data.type,itemText)
UD_STORE.save()
self._tree.DeleteAllItems()
self._udTree.clear()
......@@ -173,7 +163,8 @@ class UserDefinitionViewerPlugin(ComponentPlugin):
data = itemData.GetData()
if isinstance(data, IUserDefinition):
UD_STORE[newtext] = UD_STORE.pop(oldtext)
UD_STORE.set_definition(data.target,data.type,newtext,data)
UD_STORE.remove_definition(data.target,data.type,oldtext)
UD_STORE.save()
self.build_plugins_tree()
......
......@@ -88,15 +88,13 @@ class AtomSelectionConfigurator(IConfigurator):
self["value"] = value
try:
ud = UD_STORE[trajConfig["basename"],"atom_selection",value]
# The input value is an atom selection string: parse it and update the configuration
except UserDefinitionsStoreError:
if UD_STORE.has_definition(trajConfig["basename"],"atom_selection",value):
ud = UD_STORE.get_definition(trajConfig["basename"],"atom_selection",value)
self.update(ud)
else:
parser = AtomSelectionParser(trajConfig["instance"].universe)
self["indexes"] = parser.parse(value)
self["expression"] = value
else:
self.update(ud)
self["n_selected_atoms"] = len(self["indexes"])
atoms = sorted(trajConfig["universe"].atomList(), key = operator.attrgetter('index'))
......
......@@ -102,8 +102,8 @@ class AtomTransmutationConfigurator(IConfigurator):
elif isinstance(value,(list,tuple)):
for definition in value:
ud = UD_STORE[trajConfig["basename"],"atom_transmutation",definition]
if ud is not None:
if UD_STORE.has_definition(trajConfig["basename"],"atom_transmutation",value):
ud = UD_STORE.get_definition(trajConfig["basename"],"atom_transmutation",definition)
self.transmutate(configuration, ud["indexes"], ud["element"])
else:
raise ConfiguratorError("wrong parameters type: must be either a dictionary whose keys are an atom selection string and values are the target element \
......
......@@ -73,12 +73,11 @@ class AxisSelection(IConfigurator):
trajConfig = configuration[self._dependencies['trajectory']]
try:
ud = UD_STORE[trajConfig["basename"],"axis_selection",value]
except UserDefinitionsStoreError:
self.update(value)
else:
if UD_STORE.has_definition(trajConfig["basename"],"axis_selection",value):
ud = UD_STORE.get_definition(trajConfig["basename"],"axis_selection",value)
self.update(ud)
else:
self.update(value)
e1 = find_atoms_in_molecule(trajConfig['instance'].universe,self['molecule'], self['endpoint1'], True)
e2 = find_atoms_in_molecule(trajConfig['instance'].universe,self['molecule'], self['endpoint2'], True)
......
......@@ -75,13 +75,11 @@ class BasisSelection(IConfigurator):
trajConfig = configuration[self._dependencies['trajectory']]
try:
ud = UD_STORE[trajConfig["basename"],"basis_selection",value]
except UserDefinitionsStoreError:
self.update(value)
else:
if UD_STORE.has_definition(trajConfig["basename"],"basis_selection",value):
ud = UD_STORE.get_definition(trajConfig["basename"],"basis_selection",value)
self.update(ud)
else:
self.update(value)
e1 = find_atoms_in_molecule(trajConfig['instance'].universe,self['molecule'], self['origin'], True)
e2 = find_atoms_in_molecule(trajConfig['instance'].universe,self['molecule'], self['x_axis'], True)
......
......@@ -57,7 +57,7 @@ class PartialChargeConfigurator(IConfigurator):
trajConfig = configuration[self._dependencies['trajectory']]
if UD_STORE.has_definition(trajConfig["basename"],'partial_charges',value):
self['charges'] = UD_STORE[trajConfig["basename"],'partial_charges',value]
self['charges'] = UD_STORE.get_definition(trajConfig["basename"],'partial_charges',value)
else:
if isinstance(value,basestring):
# Case of a python script
......
......@@ -70,13 +70,17 @@ class QVectorsConfigurator(IConfigurator):
trajConfig = configuration[self._dependencies['trajectory']]
try:
ud = UD_STORE[trajConfig["basename"],"q_vectors",value]
if UD_STORE.has_definition(trajConfig["basename"],"q_vectors",value):
ud = UD_STORE.get_definition(trajConfig["basename"],"q_vectors",value)
self["parameters"] = ud['parameters']
self["type"] = ud['generator']
self["is_lattice"] = ud['is_lattice']
self["q_vectors"] = ud['q_vectors']
except UserDefinitionsStoreError:
else:
generator, parameters = value
generator = REGISTRY["q_vectors"][generator](trajConfig["instance"])
generator = REGISTRY["q_vectors"][generator](trajConfig["universe"])
generator.setup(parameters)
generator.generate()
......@@ -87,13 +91,7 @@ class QVectorsConfigurator(IConfigurator):
self["type"] = generator.type
self["is_lattice"] = generator.is_lattice
self["q_vectors"] = generator.configuration['q_vectors']
else:
self["parameters"] = ud['parameters']
self["type"] = ud['generator']
self["is_lattice"] = ud['is_lattice']
self["q_vectors"] = ud['q_vectors']
self["shells"] = self["q_vectors"].keys()
self["n_shells"] = len(self["q_vectors"])
self["value"] = self["q_vectors"]
......
......@@ -27,7 +27,7 @@
'''
Created on Mar 30, 2015
@author: pellegrini
@author: Eric C. Pellegrini
'''
import collections
......
......@@ -27,7 +27,7 @@
'''
Created on Mar 30, 2015
@author: pellegrini
@author: Eric C. Pellegrini
'''
import collections
......
......@@ -27,7 +27,7 @@
'''
Created on Mar 30, 2015
@author: pellegrini
@author: Eric C. Pellegrini
'''
import collections
......
......@@ -27,7 +27,7 @@
'''
Created on Mar 30, 2015
@author: pellegrini
@author: Eric C. Pellegrini
'''
import collections
......
......@@ -27,7 +27,7 @@
'''
Created on Mar 30, 2015
@author: pellegrini
@author: Eric C. Pellegrini
'''
import collections
......
......@@ -35,7 +35,6 @@ import abc
from MDANSE import REGISTRY
from MDANSE.Core.Error import Error
from MDANSE.Framework.Configurable import Configurable
from MDANSE.Framework.Status import Status
class QVectorsError(Error):
pass
......@@ -48,16 +47,13 @@ class IQVectors(Configurable):
is_lattice = False
def __init__(self, trajectory, status=False):
def __init__(self, universe, status=None):
Configurable.__init__(self)
self._universe = trajectory.universe
self._universe = universe
if status:
self._status = Status()
else:
self._status = None
self._status = status
@abc.abstractmethod
def _generate(self):
......@@ -68,6 +64,10 @@ class IQVectors(Configurable):
self._generate()
if self._status is not None:
self._status.finish()
self._status.finish()
def setStatus(self,status):
self._status = status
\ No newline at end of file
......@@ -27,7 +27,7 @@
'''
Created on Mar 30, 2015
@author: pellegrini
@author: Eric C. Pellegrini
'''
import numpy
......@@ -40,10 +40,10 @@ class LatticeQVectors(IQVectors):
type = None
def __init__(self, universe):
super(LatticeQVectors,self).__init__(universe)
def __init__(self, universe,status=None):
super(LatticeQVectors,self).__init__(universe,status)
if not self._universe.is_periodic:
raise QVectorsError("The universe must be periodic for building lattice-based Q vectors")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment