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

Regenerated the jobs unit tests according to recent changes in API.

Bug fixes related to those unit tests.
Improved the AtomsList dialog to keep track the order in which the atoms
are selected.
parent c140f806
......@@ -61,25 +61,30 @@ class Configurable(object):
Constructor
'''
self._configuration = self.build_configuration()
self._configuration = collections.OrderedDict()
self._configured=False
@classmethod
def build_configuration(cls):
self.build_configuration()
def build_configuration(self):
configuration = collections.OrderedDict()
self._configuration.clear()
for name,(typ,kwds) in cls.settings.items():
for name,(typ,kwds) in self.settings.items():
try:
configuration[name] = REGISTRY["configurator"][typ](name, **kwds)
self._configuration[name] = REGISTRY["configurator"][typ](name, **kwds)
# Any kind of error has to be caught
except:
raise ConfigurationError("Invalid type for %r configurator" % name)
def set_settings(self,settings):
return configuration
self.settings = settings
self.build_configuration()
def __getitem__(self, name):
"""
Returns a configuration item given its name.
......@@ -112,6 +117,8 @@ class Configurable(object):
'''
self._configured=False
self.build_configuration()
# If no configurator has to be configured, just return
if not self._configuration:
......
......@@ -87,11 +87,9 @@ class AtomsListConfigurator(IConfigurator):
molecule,atoms=value
self["value"] = value
indexes = [find_atoms_in_molecule(trajConfig['instance'].universe,molecule, at, True) for at in atoms]
self['atoms'] = zip(*indexes)
self['atoms'] = find_atoms_in_molecule(trajConfig['instance'].universe,molecule, atoms, True)
self['n_values'] = len(self['atoms'])
def get_information(self):
......
......@@ -49,7 +49,7 @@ class OutputFilesConfigurator(IConfigurator):
type = 'output_files'
_default = ('.', "output", ["netcdf"])
_default = ("output", ["netcdf"])
def __init__(self, name, formats=None, **kwargs):
'''
......
......@@ -107,7 +107,7 @@ class AngularCorrelation(IJob):
"""
e1, e2 = self.configuration['axis_selection']['atoms'][index]
e1 = read_atoms_trajectory(self.configuration["trajectory"]["instance"],
e1,
first=self.configuration['frames']['first'],
......
......@@ -315,7 +315,7 @@ class IJob(Configurable):
f.write('#!%s\n\n' % sys.executable)
f.write('import unittest\n')
f.write('from Tests.UnitTest import UnitTest\n')
f.write('from Tests.UnitTests.UnitTest import UnitTest\n')
f.write('from MDANSE import REGISTRY\n\n')
f.write('class Test%s(UnitTest):\n\n' % cls.type.upper())
......
......@@ -136,23 +136,20 @@ class SpatialDensity(IJob):
directCell = numpy.array(self.configuration['trajectory']['instance'].universe.basisVectors()).astype(numpy.float64)
reverseCell = numpy.array(self.configuration['trajectory']['instance'].universe.reciprocalBasisVectors()).astype(numpy.float64)
# The configuration is made contiguous.
conf = self.configuration['trajectory']['instance'].universe.contiguousObjectConfiguration()
origins = numpy.zeros((self.configuration['reference_basis']['n_values'],3), dtype = numpy.float64)
bases = numpy.zeros((self.configuration['reference_basis']['n_values'],3,3), dtype = numpy.float64)
indexes = numpy.array(self.configuration['target_molecule']['indexes']).astype(numpy.int32)
for i, basis in enumerate(self.configuration['reference_basis']['atoms']):
originIndexes, xIndexes, yIndexes = basis
origins[i,:] = center_of_mass(conf.array[originIndexes], None)
x = center_of_mass(conf.array[xIndexes,:], None)
y = center_of_mass(conf.array[yIndexes,:], None)
bases[i,:,:] = numpy.array(build_cartesian_axes(origins[i,:],x,y)).T
origin, x, y = basis
bases[i,:,:] = numpy.array(build_cartesian_axes(conf.array[origin,:],conf.array[x,:],conf.array[y,:])).T
hist = numpy.zeros_like(self.hist, dtype=numpy.int32)
......
......@@ -199,10 +199,8 @@ if __name__ == "__main__":
from MDANSE.GUI import DATA_CONTROLLER
from MDANSE.GUI.WorkingPanel import WorkingPanel
root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))))
filename = os.path.join(root,'Data','Trajectories','MMTK','protein_in_periodic_universe.nc')
filename = os.path.join(os.path.dirname(PLATFORM.package_directory()),'Data','Trajectories','MMTK','protein_in_periodic_universe.nc')
app = wx.App(False)
f = JobFrame(None,'msd',filename)
......
......@@ -38,6 +38,37 @@ from MDANSE import LOGGER
from MDANSE.Framework.Widgets.UserDefinitionWidget import UserDefinitionsDialog, UserDefinitionWidget
from MDANSE.MolecularDynamics.Trajectory import find_atoms_in_molecule, get_chemical_objects_dict
class AtomNameDropTarget(wx.TextDropTarget):
def __init__(self, molTree,atList):
wx.TextDropTarget.__init__(self)
self._molecules = molTree
self._atoms = atList
self._currentMolecule = None
def OnDropText(self, x, y, data):
listedAtoms = [self._atoms.GetItem(idx).GetText() for idx in range(self._atoms.GetItemCount())]
if data in listedAtoms:
return
atTreeItem = self._molecules.GetSelection()
molTreeItem = self._molecules.GetItemParent(atTreeItem)
molname = self._molecules.GetItemText(molTreeItem)
if self._atoms.GetItemCount() > 0:
if (molname != self._currentMolecule):
d = wx.MessageDialog(self._atoms, "The dragged atoms does not belong to the same molecule than the current selection.", style=wx.ICON_ERROR|wx.STAY_ON_TOP|wx.CENTRE)
d.ShowModal()
return
else:
self._currentMolecule = molname
self._atoms.Append([data])
class AtomsListDialog(UserDefinitionsDialog):
def __init__(self, parent, trajectory, nAtoms):
......@@ -57,44 +88,52 @@ class AtomsListDialog(UserDefinitionsDialog):
self.type = "%d_atoms_list" % self._nAtoms
UserDefinitionsDialog.__init__(self, parent, target, wx.ID_ANY, title="%d Atoms selection dialog" % nAtoms,style=wx.DEFAULT_DIALOG_STYLE|wx.RESIZE_BORDER|wx.MINIMIZE_BOX|wx.MAXIMIZE_BOX)
self.SetSize((400,400))
def build_dialog(self):
panel = wx.ScrolledWindow(self, wx.ID_ANY, size=self.GetSize())
panel.SetScrollbars(20,20,50,50)
sizer = wx.BoxSizer(wx.VERTICAL)
sb = wx.StaticBox(panel, wx.ID_ANY, label = "Atom selection")
sbSizer = wx.StaticBoxSizer(sb, wx.HORIZONTAL)
gbSizer = wx.GridBagSizer(5,5)
label1 = wx.StaticText(panel, wx.ID_ANY, label="Molecules")
label2 = wx.StaticText(panel, wx.ID_ANY, label="Atoms")
label2 = wx.StaticText(panel, wx.ID_ANY, label="Selected atoms")
gbSizer.Add(label1, (0,0), flag=wx.EXPAND)
gbSizer.Add(label2, (0,1), flag=wx.EXPAND)
self._molecules = wx.ListBox(panel, wx.ID_ANY, style = wx.LB_SINGLE|wx.LB_NEEDED_SB)
self._molecules = wx.TreeCtrl(panel, wx.ID_ANY, style=wx.TR_DEFAULT_STYLE|wx.LB_HSCROLL|wx.LB_NEEDED_SB^wx.TR_HIDE_ROOT)
self._molecularContents = get_chemical_objects_dict(self._trajectory.universe)
self._molecules.SetItems(sorted(self._molecularContents.keys()))
root = self._molecules.AddRoot('')
for mname in sorted(self._molecularContents.keys()):
molnode = self._molecules.AppendItem(root,mname)
atomsList = self._molecularContents[mname][0].atomList()
atomNames = sorted(set([at.name for at in atomsList]))
for aname in atomNames:
self._molecules.AppendItem(molnode,aname)
self._atoms = wx.ListBox(panel, wx.ID_ANY, style = wx.LB_MULTIPLE|wx.LB_NEEDED_SB)
self._atoms = wx.ListCtrl(panel, wx.ID_ANY, style=wx.LC_LIST)
dt = AtomNameDropTarget(self._molecules,self._atoms)
self._atoms.SetDropTarget(dt)
gbSizer.Add(self._molecules, (1,0), flag=wx.EXPAND)
gbSizer.Add(self._atoms , (1,1), flag=wx.EXPAND)
gbSizer.AddGrowableRow(1)
gbSizer.AddGrowableCol(0)
gbSizer.AddGrowableCol(1)
sbSizer.Add(gbSizer, 1, wx.EXPAND|wx.ALL, 5)
setButton = wx.Button(panel, wx.ID_ANY, label="Set")
self._resultsTextCtrl = wx.TextCtrl(panel, wx.ID_ANY, style=wx.TE_READONLY|wx.TE_MULTILINE)
sizer.Add(sbSizer,1,wx.EXPAND|wx.ALL,5)
sizer.Add(gbSizer, 1, wx.EXPAND|wx.ALL, 5)
sizer.Add(setButton,0,wx.EXPAND|wx.ALL,5)
sizer.Add(self._resultsTextCtrl,1,wx.EXPAND|wx.ALL,5)
......@@ -103,21 +142,55 @@ class AtomsListDialog(UserDefinitionsDialog):
self._mainSizer.Add(panel, 1, wx.EXPAND|wx.ALL, 5)
self.Bind(wx.EVT_BUTTON, self.on_set_user_definition, setButton)
self.Bind(wx.EVT_LISTBOX, self.on_select_molecule, self._molecules)
self.Bind(wx.EVT_LISTBOX, self.on_select_atom, self._atoms)
self.Bind(wx.EVT_TREE_BEGIN_DRAG,self.on_drag_atom_name,self._molecules)
self.Bind(wx.EVT_LIST_KEY_DOWN,self.on_delete_atom_name,self._atoms)
def on_delete_atom_name(self,event):
keycode = event.GetKeyCode()
if keycode != wx.WXK_DELETE:
return
item = self._atoms.GetFirstSelected()
selectedItems = []
while item != -1:
selectedItems.append(item)
item = self._atoms.GetNextSelected(item)
if not selectedItems:
return
selectedItems.reverse()
for it in selectedItems:
self._atoms.DeleteItem(it)
def on_drag_atom_name(self,event):
item = event.GetItem()
parentItem = self._molecules.GetItemParent(item)
if parentItem == self._molecules.GetRootItem():
return
text = self._molecules.GetItemText(item)
tdo = wx.TextDataObject(text)
tds = wx.DropSource(self._molecules)
tds.SetData(tdo)
tds.DoDragDrop(wx.Drag_CopyOnly)
def set_user_definition(self):
self._selection = []
if len(self._selectedAtoms) != self._nAtoms:
if self._atoms.GetItemCount() != self._nAtoms:
LOGGER('You must select %d atoms.' % self._nAtoms,'error',['dialog'])
return
molecule = str(self._molecules.GetStringSelection())
atoms = tuple(str(self._atoms.GetString(idx)) for idx in self._selectedAtoms)
self._selection = find_atoms_in_molecule(self._trajectory.universe,molecule, atoms, True)
atTreeItem = self._molecules.GetSelection()
molTreeItem = self._molecules.GetItemParent(atTreeItem)
molecule = self._molecules.GetItemText(molTreeItem)
self._selectedAtoms = [self._atoms.GetItem(idx).GetText() for idx in range(self._atoms.GetItemCount())]
self._selection = find_atoms_in_molecule(self._trajectory.universe,molecule, self._selectedAtoms, True)
def on_set_user_definition(self,event=None):
......@@ -129,28 +202,10 @@ class AtomsListDialog(UserDefinitionsDialog):
self._resultsTextCtrl.AppendText('Number of selected %d-tuplets: %d\n' % (self._nAtoms,len(self._selection)))
for idxs in self._selection:
line = " ; ".join(["Atom %d : %s" % (i,v) for i,v in enumerate(idxs)])
line = " ; ".join(["Atom %5d : %s" % (v,self._selectedAtoms[i]) for i,v in enumerate(idxs)])
self._resultsTextCtrl.AppendText(line)
self._resultsTextCtrl.AppendText('\n')
def on_select_atom(self, event):
selection = self._atoms.GetSelections()
if len(selection) > self._nAtoms:
self._atoms.DeselectAll()
for idx in self._selectedAtoms:
self._atoms.SetSelection(idx,True)
else:
self._selectedAtoms = selection
def on_select_molecule(self, event):
atomsList = self._molecularContents[event.GetString()][0].atomList()
atomNames = sorted(set([at.name for at in atomsList]))
self._atoms.SetItems(atomNames)
def validate(self):
if not self._selection:
......@@ -174,7 +229,7 @@ class AtomListWidget(UserDefinitionWidget):
def on_new_user_definition(self,event):
atomsListDlg = AtomsListDialog(self,self._trajectory,self._configurator._nAtoms)
atomsListDlg.ShowModal()
atomsListDlg.Destroy()
......@@ -183,11 +238,13 @@ if __name__ == "__main__":
from MMTK.Trajectory import Trajectory
t = Trajectory(None,"../../../../../Data/Trajectories/MMTK/waterbox_in_periodic_universe.nc","r")
from MDANSE import PLATFORM
t = Trajectory(None,os.path.join(os.path.dirname(PLATFORM.package_directory()),"Data","Trajectories","MMTK","nagma_in_periodic_universe.nc"),"r")
app = wx.App(False)
p = AtomsListDialog(None,t,2)
p = AtomsListDialog(None,t,3)
p.ShowModal()
......
......@@ -10,7 +10,7 @@ class ConfigurationPanel(wx.Panel):
wx.Panel.__init__(self, parent, wx.ID_ANY)
self._configurable = configurable
self._configurable = configurable()
self._widgets = {}
......@@ -20,15 +20,15 @@ class ConfigurationPanel(wx.Panel):
self.panelSizer = wx.BoxSizer(wx.VERTICAL)
configuration = self._configurable.build_configuration()
self._configurable.build_configuration()
for cfgName in self._configurable.settings.keys():
widget = configuration[cfgName].widget
widget = self._configurable.configuration[cfgName].widget
widgetClass = REGISTRY["widget"][widget]
self._widgets[cfgName] = widgetClass(self, cfgName, configuration[cfgName])
self._widgets[cfgName] = widgetClass(self, cfgName, self._configurable.configuration[cfgName])
self.panelSizer.Add(self._widgets[cfgName], 0, wx.ALL|wx.EXPAND, 5)
......
......@@ -172,4 +172,4 @@ class DataTreePanel(wx.Panel):
dropSource.SetData(data)
dropSource.DoDragDrop(wx.Drag_AllowMove)
dropSource.DoDragDrop(wx.Drag_CopyOnly)
......@@ -53,15 +53,14 @@ def build_cartesian_axes(origin, p1, p2, dtype = numpy.float64):
origin = numpy.array(origin, dtype = dtype)
p1 = numpy.array(p1, dtype = dtype)
p2 = numpy.array(p2, dtype = dtype)
p2 = numpy.array(p2, dtype = dtype)
v1 = p1 - origin
v2 = p2 - origin
n1 = (v1 + v2)
n1 /= numpy.linalg.norm(n1)
n3 = numpy.cross(v1,n1)
n3 /= numpy.linalg.norm(n3)
......
......@@ -132,6 +132,9 @@ def partition_universe(universe,groups):
return coll
def read_atoms_trajectory(trajectory, atoms, first, last=None, step=1, variable="configuration", dtype=numpy.float64):
if not isinstance(atoms,(list,tuple)):
atoms = [atoms]
if last is None:
last = len(trajectory)
......@@ -141,8 +144,6 @@ def read_atoms_trajectory(trajectory, atoms, first, last=None, step=1, variable=
serie = numpy.zeros((nFrames,3), dtype=dtype)
for at in atoms:
if not isinstance(at,Atom):
at = int(at)
serie += trajectory.readParticleTrajectory(at, first, last, step, variable).array
serie /= len(atoms)
......
#!/usr/bin/python
import unittest
from Tests.UnitTest import UnitTest
from Tests.UnitTests.UnitTest import UnitTest
from MDANSE import REGISTRY
class TestAC(UnitTest):
def test(self):
parameters = {}
parameters['axis_selection'] = {'endpoint1': ('OW',), 'endpoint2': ('HW',), 'molecule': 'Water'}
parameters['axis_selection'] = ('Water', ('OW', 'HW'))
parameters['frames'] = (0, 10, 1)
parameters['output_files'] = ('/users/pellegrini/workspace/MDANSE/Tests/FunctionalTests/Jobs', 'output', ['netcdf'])
parameters['output_files'] = ('output', ['netcdf'])
parameters['per_axis'] = False
parameters['running_mode'] = ('monoprocessor', 1)
parameters['trajectory'] = '../../../Data/Trajectories/MMTK/waterbox_in_periodic_universe.nc'
job = REGISTRY['job']['ac'](status=False)
self.assertNotRaises(job.run, parameters)
job = REGISTRY['job']['ac']()
job.run(parameters,False)
self.assertNotRaises(job.run, parameters, status=False)
def suite():
loader = unittest.TestLoader()
......
#!/usr/bin/python
import unittest
from Tests.UnitTest import UnitTest
from Tests.UnitTests.UnitTest import UnitTest
from MDANSE import REGISTRY
class TestAPM(UnitTest):
......@@ -11,11 +11,11 @@ class TestAPM(UnitTest):
parameters['axis'] = ['a', 'b']
parameters['frames'] = (0, 10, 1)
parameters['name'] = 'DMPC'
parameters['output_files'] = ('/users/pellegrini/workspace/MDANSE/Tests/FunctionalTests/Jobs', 'output', ['netcdf'])
parameters['output_files'] = ('output', ['netcdf'])
parameters['running_mode'] = ('monoprocessor', 1)
parameters['trajectory'] = '../../../Data/Trajectories/MMTK/dmpc_in_periodic_universe.nc'
job = REGISTRY['job']['apm'](status=False)
self.assertNotRaises(job.run, parameters)
job = REGISTRY['job']['apm']()
self.assertNotRaises(job.run, parameters, status=False)
def suite():
loader = unittest.TestLoader()
......
#!/usr/bin/python
import unittest
from Tests.UnitTest import UnitTest
from Tests.UnitTests.UnitTest import UnitTest
from MDANSE import REGISTRY
class TestBTT(UnitTest):
......@@ -10,10 +10,10 @@ class TestBTT(UnitTest):
parameters = {}
parameters['atom_selection'] = 'all'
parameters['frames'] = (0, 10, 1)
parameters['output_files'] = ('/users/pellegrini/workspace/MDANSE/Tests/FunctionalTests/Jobs', 'output', ['netcdf'])
parameters['output_files'] = ('output', ['netcdf'])
parameters['trajectory'] = '../../../Data/Trajectories/MMTK/waterbox_in_periodic_universe.nc'
job = REGISTRY['job']['btt'](status=False)
self.assertNotRaises(job.run, parameters)
job = REGISTRY['job']['btt']()
self.assertNotRaises(job.run, parameters, status=False)
def suite():
loader = unittest.TestLoader()
......
#!/usr/bin/python
import unittest
from Tests.UnitTest import UnitTest
from Tests.UnitTests.UnitTest import UnitTest
from MDANSE import REGISTRY
class TestCASTEP(UnitTest):
......@@ -9,9 +9,9 @@ class TestCASTEP(UnitTest):
def test(self):
parameters = {}
parameters['castep_file'] = '../../../Data/Trajectories/CASTEP/PBAnew.md'
parameters['output_file'] = ('/users/pellegrini/workspace/MDANSE/Tests/FunctionalTests/Jobs', 'output', ['netcdf'])
job = REGISTRY['job']['castep'](status=False)
self.assertNotRaises(job.run, parameters)
parameters['output_file'] = ('output', ['netcdf'])
job = REGISTRY['job']['castep']()
self.assertNotRaises(job.run, parameters, status=False)
def suite():
loader = unittest.TestLoader()
......
#!/usr/bin/python
import unittest
from Tests.UnitTest import UnitTest
from Tests.UnitTests.UnitTest import UnitTest
from MDANSE import REGISTRY
class TestCCF(UnitTest):
......@@ -12,14 +12,14 @@ class TestCCF(UnitTest):
parameters['frames'] = (0, 10, 1)
parameters['instrument_resolution'] = ('gaussian', {'mu': 0.0, 'sigma': 10.0})
parameters['normalize'] = False
parameters['output_files'] = ('/users/pellegrini/workspace/MDANSE/Tests/FunctionalTests/Jobs', 'output', ['netcdf'])
parameters['output_files'] = ('output', ['netcdf'])
parameters['q_vectors'] = ('spherical_lattice', {'width': 0.1, 'n_vectors': 50, 'shells': (0, 5, 0.1)})
parameters['running_mode'] = ('monoprocessor', 1)
parameters['trajectory'] = '../../../Data/Trajectories/MMTK/waterbox_in_periodic_universe.nc'
parameters['transmutated_atoms'] = None
parameters['weights'] = 'b_coherent'
job = REGISTRY['job']['ccf'](status=False)
self.assertNotRaises(job.run, parameters)
job = REGISTRY['job']['ccf']()
self.assertNotRaises(job.run, parameters, status=False)
def suite():
loader = unittest.TestLoader()
......
#!/usr/bin/python
import unittest
from Tests.UnitTest import UnitTest
from Tests.UnitTests.UnitTest import UnitTest
from MDANSE import REGISTRY
class TestCHARMM(UnitTest):
......@@ -10,10 +10,10 @@ class TestCHARMM(UnitTest):
parameters = {}
parameters['dcd_file'] = '../../../Data/Trajectories/CHARMM/2vb1.dcd'
parameters['fold'] = False
parameters['output_file'] = ('/users/pellegrini/workspace/MDANSE/Tests/FunctionalTests/Jobs', 'output', ['netcdf'])
parameters['output_file'] = ('output', ['netcdf'])
parameters['pdb_file'] = '../../../Data/Trajectories/CHARMM/2vb1.pdb'
job = REGISTRY['job']['charmm'](status=False)
self.assertNotRaises(job.run, parameters)
job = REGISTRY['job']['charmm']()
self.assertNotRaises(job.run, parameters, status=False)
def suite():
loader = unittest.TestLoader()
......