ElementsDatabase.py 14.7 KB
Newer Older
eric pellegrini's avatar
eric pellegrini committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
'''
MDANSE : Molecular Dynamics Analysis for Neutron Scattering Experiments
------------------------------------------------------------------------------------------
Copyright (C)
2015- Eric C. Pellegrini Institut Laue-Langevin
BP 156
6, rue Jules Horowitz
38042 Grenoble Cedex 9
France
pellegrini[at]ill.fr

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
Created on Mar 23, 2015

@author: pellegrini
'''

eric pellegrini's avatar
eric pellegrini committed
31
32
33
34
35
36
37
import _abcoll
import copy
import csv
import numbers
import os
import xml.etree.ElementTree as etree

eric pellegrini's avatar
eric pellegrini committed
38
39
40
from MDANSE.Core.Platform import PLATFORM
from MDANSE.Core.Error import Error
from MDANSE.Core.Singleton import Singleton
eric pellegrini's avatar
eric pellegrini committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

class ElementsDatabaseError(Error):
    pass

def create_mmtk_database_entry(directory,name, symbol=None,mass=0.0):
    
    if symbol is None:
        symbol = name

    filename = os.path.join(PLATFORM.local_mmtk_database_directory(),directory, name)
    f = open(filename, 'w')
    f.write('name = "%s"\n\n' % name)
    f.write('symbol = "%s"\n\n' % name)
    f.write('mass = %s' % mass)
    f.close()
    
    return filename

def indent(elem, level=0):
    """
    Produces a pretty indented XML tree.
    
    @param elem: the element tree to indent.
    @type elem: Element instance.
    
    @param level: the level of indentation.
    @type level: int.
    """
    
    i = "\n" + level*"  "
    if len(elem):
        if not elem.text or not elem.text.strip():
            elem.text = i + "  "
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
        for elem in elem:
            indent(elem, level+1)
        if not elem.tail or not elem.tail.strip():
            elem.tail = i
    else:
        if level and (not elem.tail or not elem.tail.strip()):
            elem.tail = i

class SortedCaselessDict(dict):

    def __init__(self, *args, **kwds):

        self._keys = []

        self.__update(*args, **kwds)

    def __contains__(self,item):

        return self.has_key(item.lower())

    def __deepcopy__(self,memo):

	return SortedCaselessDict(copy.deepcopy(dict(self)))
    
    def __getitem__(self,item):
                
        return dict.__getitem__(self,item.lower())

    def __setitem__(self,item,value):

        item = item.lower()

        dict.__setitem__(self,item,value)

        if not item in self._keys:
            self._keys.append(item)
            self._keys.sort()
            
    def __update(*args, **kwds):
        if len(args) > 2:
            raise TypeError("update() takes at most 2 positional arguments ({} given)".format(len(args)))
        elif not args:
            raise TypeError("update() takes at least 1 argument (0 given)")
        
        self = args[0]

        other = args[1] if len(args) >= 2 else ()

        if isinstance(other, _abcoll.Mapping):
            for key in other:
                self[key.lower()] = other[key]
        elif hasattr(other, "keys"):
            for key in other.keys():
                self[key.lower()] = other[key]
        else:
            for key, value in other:
                self[key] = value
        for key, value in kwds.items():
            self[key.lower()] = value
            
    def clear(self):
        
        dict.clear(self)
        
        self._keys = []

    def has_key(self, item):
        
        return dict.has_key(self,item.lower())

    def items(self):
        return [(key, self[key]) for key in self._keys]

    def keys(self):
        return self._keys

    def iteritems(self):
        for k in self._keys:
            yield (k, self[k])

    def iterkeys(self):
        return iter(self._keys)

    def itervalues(self):
        for k in self._keys:
            yield self[k]        

    def values(self):
        return [self[key] for key in self._keys]

class ElementsDatabase(object):
    
eric pellegrini's avatar
eric pellegrini committed
168
169
    __metaclass__ = Singleton
    
eric pellegrini's avatar
eric pellegrini committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
    _DEFAULT_DATABASE = os.path.join(os.path.dirname(__file__), "elements_database.csv")

    _USER_DATABASE = os.path.join(PLATFORM.application_directory(), "elements_database.csv")
    
    _TYPES = {"str" : str, "int" : int, "float" : float}

    def __init__(self):

        self._data = SortedCaselessDict()

        self._properties = SortedCaselessDict()

        self.reset()

        # Load the user database. If any problem occurs while loading it, loads the default one
        self._load(ElementsDatabase._USER_DATABASE)
                
    def __contains__(self,ename):

        return self._data.has_key(ename)

    def __getitem__(self,item):

        ename,pname = item
                
        try:
            element = self._data[ename]
        except KeyError:
            raise ElementsDatabaseError("The element %r is not registered in the database." % ename)
        else:
            try:
                return element[pname]
            except KeyError:
                try:
                    return self._properties[pname]["default"]
                except KeyError:
                    raise ElementsDatabaseError("The property %r is not registered in the database." % pname)

    def __iter__(self):
                        
        for v in self._data.itervalues():
            yield copy.deepcopy(v)
                
    def __setitem__(self,item,value):

        ename,pname = item
                
        self._data[ename][pname] = self._properties[pname]['type'](value)

        # If one of those properties is changed then the corresponding mmtk entry has to be updated
        if pname in ["name","symbol","atomic_weight"]:
            create_mmtk_database_entry("Atoms",ename,symbol=self._data[ename]["symbol"],mass=self._data[ename]["atomic_weight"])

    def _export_csv(self, filename, delimiter=',', lineterminator='\n', restval='undefined'):

        f = open(filename, 'w')

        propNames = ["id"] + self._properties.keys()
                
        # Return a csv writer object.            
        databaseWriter = csv.DictWriter(f, propNames, delimiter=delimiter, lineterminator=lineterminator, restval=restval)
        databaseWriter.writeheader()
        default = {"id":"''"}
        default.update(dict([(pname,repr(prop['default'])) for pname,prop in self._properties.items()]))
        databaseWriter.writerow(default)
        for ename, props in self._data.items():
            p = {"id":ename}
            p.update(dict([(pname,props.get(pname,prop["default"])) for pname,prop in self._properties.items()]))
            databaseWriter.writerow(p)
                                                 
        # Closes the csv file. 
        f.close()

    def _export_xml(self, filename):

        f = open(filename, 'w')      
                                
        # Create the ElementTree root node.                         
        root = etree.Element('elements_database')
    
        # Write all the elements.
        for ename,props in self._data.iteritems():
                        
            # Create a XML node for the element.
            node = etree.Element('element')
            
            node.attrib["id"] = ename
                        
            for pname,prop in self._properties.iteritems():
                        
                # Creates a XML node for the property.      
                subnode = etree.Element(pname)
                # Set its value with the element current property.
                subnode.text = str(props.get(pname,prop["default"]))
                # And append the property node to the current element node.
                node.append(subnode)
                
            # Append the element node to the root node.
            root.append(node)
            
        # Produce a nice-looking indented output in the XML file.
        indent(root)
        
        etree.ElementTree(root).write(f)

        f.close()

    exporters = {"csv" : _export_csv, "xml" : _export_xml}
            
    def _load(self, filename):

        # Try to open the input file
        try:
            f = open(filename, "r")
        
            # Open a CSV reader object from the input file object
            reader = csv.reader(f, delimiter=',')
            
            line = ["#"]
            while line[0].strip().startswith("#"):
                line = reader.next()
                
            properties = line        
            if properties.pop(0).lower() != "id":
                return
            
            for pname,ptype in zip(properties,reader.next()[1:]):
                
                if self._properties.has_key(pname):
                    continue
                
                ptype=self._TYPES[ptype]
                                
                self._properties[pname] = {"type":ptype,"default":ptype()}
                                        
            # Reads the next lines of the CSV that correspond to the database entries
            for r in reader:
                ename = r.pop(0)
                props = SortedCaselessDict()
                # Loop over the contents of the current line
                for i,v in enumerate(r):
                    pname = properties[i]
                    try:
                        props[pname] = self._properties[pname]["type"](v)
                    except ValueError:
                        props[pname] = self._properties[pname]["default"]
                    
                self._data[ename] = props
                        
            f.close()

        # If any error occurs, load the default database
        except:
            self._load(ElementsDatabase._DEFAULT_DATABASE)
        
    def add_element(self, ename, save=False):

        if not self._data.has_key(ename):
            self._data[ename] = SortedCaselessDict([(pname,prop["default"]) for pname,prop in self._properties.iteritems()])
        
        # Create the corresponding MMTK entry
        create_mmtk_database_entry("Atoms",ename,symbol=self._data[ename]["symbol"],mass=self._data[ename]["atomic_weight"])

        if save:
            self.save()

    def add_property(self, pname, default, save=False):

        if self._properties.has_key(pname):
            raise ElementsDatabaseError("The property %r is already registered in the database." % pname)

        self._properties[pname] = {"type":type(default),"default":default}

        if save:
            self.save()
                
    @property
    def elements(self):
        
        return self._data.keys()

    def export(self, fmt, filename, *args, **kwargs):

        try:
            exporter = ElementsDatabase.exporters[fmt]
            filename = os.path.splitext(filename)[0] + "." + fmt
            exporter(self, filename, *args, **kwargs)
        except KeyError:
            raise ElementsDatabaseError("%r format is not a valid export format." % fmt)
            
    def get_element(self,ename):
        
        return copy.deepcopy(self._data[ename])

    def get_elements(self):
        
        return self._data.keys()

    def get_isotopes(self,ename):

        symbol = self._data[ename]["symbol"]

        return [ename for ename,props in self._data.iteritems() if props["symbol"] == symbol]

    def get_properties(self):

        return self._properties.keys()
        
    def get_property(self,pname):
        
        return SortedCaselessDict([(k,self[k,pname]) for k in self._data.iterkeys()])

    def get_property_settings(self,pname):

        return copy.deepcopy(self._properties[pname])
                
    def get_numeric_properties(self):

        return [pname for pname,prop in self._properties.iteritems() if isinstance(prop['default'],numbers.Number)]

    def has_element(self,ename):

        return self._data.has_key(ename)

    def has_property(self,pname):

        return self._properties.has_key(pname)

    def info(self, ename):
        '''
        Return a formatted string that contains all the informations about a given element.
        
        @param element: the element symbol.
        @type element: string
        '''
        
        # A delimiter line.
        delimiter = "-"*70

        # The list that will contain the informations.
        info = []

        # Append a delimiter.                
        info.append(delimiter)

        # The name of the entry is centered in a centered-string.
        info.append("%s" % ename.center(70))
                        
        # The 'property' and 'value' columns names.
        info.append("%s" % " {0:<20}{1:>50}".format('property', 'value'))
                                
        # Append a delimiter.                
        info.append(delimiter)
                        
        # The values for all element's properties
        for pname in self._properties.keys():
            info.append("%s" % " {0:<20}{1:>50}".format(pname,self[ename,pname]))

        # Append a delimiter.                
        info.append(delimiter)
                
        # The list is joined to a string.
        info = "\n".join(info)
                            
        # And returned.        
        return info

    def match_numeric_property(self, pname, value, tolerance=0.0):

        tolerance = abs(tolerance)

        pvalues = self.get_property(pname)

        return [ename for ename,pval in pvalues.items() if abs(pval-value)<tolerance]
           
    @property
    def nElements(self):
        return len(self._data)

    @property
    def nProperties(self):
        return len(self._properties)

    def number_of_elements(self):
        return len(self._data)

    def number_of_properties(self):
        return len(self._properties)
    
    @property
    def numericProperties(self):

        return self.get_numeric_properties()    
        
    @property
    def properties(self):
        
        return self._properties.keys()
    
    def reset(self):
        
        self._data.clear()

        self._properties.clear()
        
    def save(self):
        
        self.export("csv",ElementsDatabase._USER_DATABASE)
        
    def regenerate_mmtk_database(self):
        
        for name,props in self._data.items():
            filename = os.path.join(PLATFORM.local_mmtk_database_directory(),"Atoms", name)
            f = open(filename, 'w')
            f.write('name = "%s"\n\n' % props["name"])
            f.write('symbol = "%s"\n\n' % name)
            f.write('mass = %s' % props["atomic_weight"])
            f.close()
            
eric pellegrini's avatar
eric pellegrini committed
489
ELEMENTS = ElementsDatabase()