/
database.py
468 lines (420 loc) · 16.2 KB
/
database.py
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
31
32
33
34
35
36
37
38
39
40
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
168
169
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
"""
The database module provides support for reading and writing data types
associated with structured thermodynamic/kinetic data.
"""
from tinydb import TinyDB
from tinydb.storages import MemoryStorage
from datetime import datetime
from collections import namedtuple
import os
from pycalphad.core.cache import fhash
try:
# Python 2
from StringIO import StringIO
except ImportError:
# Python 3
from io import StringIO
class DatabaseExportError(Exception):
"""Raised when a database cannot be written."""
pass
def _to_tuple(lst):
"Convert nested list to nested tuple. Source: Martijn Pieters on StackOverflow"
return tuple(_to_tuple(i) if isinstance(i, list) else i for i in lst)
class Phase(object): #pylint: disable=R0903
"""
Phase in the database.
Attributes
----------
name : string
System-local name of the phase.
constituents : tuple of frozenset
Possible sublattice constituents (elements and/or species).
sublattices : list
Site ratios of sublattices.
model_hints : dict
Structured "hints" for a Model trying to read this phase.
Hints for major constituents and typedefs (Thermo-Calc) go here.
"""
def __init__(self):
self.name = None
self.constituents = None
self.sublattices = []
self.model_hints = {}
def __eq__(self, other):
if type(self) == type(other):
return self.__dict__ == other.__dict__
else:
return False
def __ne__(self, other):
return not self.__eq__(other)
def __repr__(self):
return 'Phase({0!r})'.format(self.__dict__)
def __hash__(self):
return hash((self.name, self.constituents, tuple(self.sublattices),
tuple(sorted(_to_tuple(self.model_hints.items())))))
DatabaseFormat = namedtuple('DatabaseFormat', ['read', 'write'])
format_registry = {}
class Database(object): #pylint: disable=R0902
"""
Structured thermodynamic and/or kinetic data.
Attributes
----------
elements : list
List of elements in database.
species : list
List of species in database.
phases : dict
Phase objects indexed by their system-local name.
symbols : dict
SymPy objects indexed by their name (FUNCTIONs in Thermo-Calc).
references : dict
Reference objects indexed by their system-local identifier.
Examples
--------
>>> mydb = Database(open('crfeni_mie.tdb'))
>>> mydb = Database('crfeni_mie.tdb')
>>> f = StringIO(u'$a complete TDB file as a string\n')
>>> mydb = Database(f)
"""
def __new__(cls, *args):
if len(args) == 0:
obj = super(Database, cls).__new__(cls, *args)
# Should elements be rolled into a special case of species?
obj.elements = set()
obj.species = set()
obj.phases = {}
obj._structure_dict = {} # System-local phase names to global IDs
obj._parameters = TinyDB(storage=MemoryStorage)
obj.symbols = {}
obj.references = {}
# Note: No public typedefs here (from TDB files)
# Instead we put that information in the model_hint for phases
return obj
elif len(args) == 1:
fname = args[0]
# Backwards compatibility: assume TDB by default
fmt = 'tdb'
# Attempt to auto-detect the correct format based on the file extension
try:
path, ext = os.path.splitext(fname)
if '.' in ext and ext[1:].lower() in format_registry:
fmt = ext[1:].lower()
except (AttributeError, TypeError):
pass
if hasattr(fname, 'read'):
# File descriptor
return cls.from_file(fname, fmt=fmt)
elif fname.find('\n') == -1:
# Single-line string; it's probably a filename
return cls.from_file(fname, fmt=fmt)
else:
# Newlines found: probably a full database string
return cls.from_string(fname, fmt=fmt)
else:
raise ValueError('Invalid number of parameters: '+len(args))
def __hash__(self):
return fhash(self.__dict__)
def __getstate__(self):
pickle_dict = {}
for key, value in self.__dict__.items():
if key == '_parameters':
pickle_dict[key] = value.all()
else:
pickle_dict[key] = value
return pickle_dict
def __setstate__(self, state):
for key, value in state.items():
if key == '_parameters':
self._parameters = TinyDB(storage=MemoryStorage)
self._parameters.insert_multiple(value)
else:
setattr(self, key, value)
@staticmethod
def register_format(fmt, read=None, write=None):
"""
Add support for reading and/or writing the specified format.
Parameters
----------
fmt: str
Format.
read : callable, optional
Read function with arguments (Database, file_descriptor)
write : callable, optional
Write function with arguments (Database, file_descriptor)
Examples
--------
None yet.
"""
format_registry[fmt.lower()] = DatabaseFormat(read=read, write=write)
@staticmethod
def from_file(fname, fmt=None):
"""
Create a Database from a file.
Parameters
----------
fname: str or file-like
File name/descriptor to read.
fmt : str, optional
File format. If not specified, an attempt at auto-detection is made.
Returns
-------
dbf : Database
Database from file.
Examples
--------
None yet.
"""
if fmt is None:
# Attempt to auto-detect the correct format based on the file extension
try:
path, ext = os.path.splitext(fname)
except (AttributeError, TypeError):
# fname isn't actually a path, so we don't know the correct format
raise ValueError('\'fmt\' keyword argument must be specified when passing a file descriptor.')
if '.' in ext and ext[1:].lower() in format_registry:
fmt = ext[1:].lower()
else:
fmt = fmt.lower()
if fmt not in format_registry or format_registry[fmt].read is None:
supported_reads = [key for key, value in format_registry.items() if value.read is not None]
raise NotImplementedError('Unsupported read format \'{0}\'. Supported formats: {1}'.format(fmt,
supported_reads))
# Is it a file descriptor?
if hasattr(fname, 'read'):
fd = fname
need_to_close = False
else:
# It's not file-like, so it's probably a filename
need_to_close = True
fd = open(fname, mode='r')
try:
dbf = Database()
format_registry[fmt.lower()].read(dbf, fd)
finally:
# Close file descriptors created in this routine
# Otherwise that's left up to the calling function
if need_to_close:
fd.close()
return dbf
@classmethod
def from_string(cls, data, **kwargs):
"""
Returns Database from a string in the specified format.
This function is a wrapper for calling `from_file` with StringIO.
Parameters
----------
data : str
Raw database string in the specified format.
kwargs : optional
See keyword arguments for `from_file`.
Returns
-------
dbf : Database
"""
return cls.from_file(StringIO(data), **kwargs)
def to_file(self, fname, fmt=None, if_exists='raise', **write_kwargs):
"""
Write the Database to a file.
Parameters
----------
fname: str or file-like
File name/descriptor to write.
fmt : str, optional
File format. If not specified, an attempt at auto-detection is made.
if_exists : string, optional ['raise', 'rename', 'overwrite']
Strategy if 'fname' already exists.
The 'raise' option (default) will raise a FileExistsError.
The 'rename' option will append the date/time to the filename.
The 'overwrite' option will overwrite the file.
This argument is ignored if 'fname' is file-like.
write_kwargs : optional
Keyword arguments to pass to write function.
Examples
--------
None yet.
"""
if fmt is None:
# Attempt to auto-detect the correct format based on the file extension
try:
path, ext = os.path.splitext(fname)
except (AttributeError, TypeError):
# fname isn't actually a path, so we don't know the correct format
raise ValueError('\'fmt\' keyword argument must be specified when passing a file descriptor.')
if '.' in ext and ext[1:].lower() in format_registry:
fmt = ext[1:].lower()
else:
fmt = fmt.lower()
if fmt not in format_registry or format_registry[fmt].write is None:
supported_writes = [key for key, value in format_registry.items() if value.write is not None]
raise NotImplementedError('Unsupported write format \'{0}\'. Supported formats: {1}'.format(fmt,
supported_writes))
# Is this a file descriptor?
if hasattr(fname, 'write'):
format_registry[fmt].write(self, fname, **write_kwargs)
else:
if os.path.exists(fname) and if_exists != 'overwrite':
if if_exists == 'raise':
raise FileExistsError('File {} already exists'.format(fname))
elif if_exists == 'rename':
writetime = datetime.now()
fname = os.path.splitext(fname)
fname = fname[0] + "." + writetime.strftime("%Y-%m-%d-%H-%M") + fname[1]
with open(fname, mode='w') as fd:
format_registry[fmt].write(self, fd, **write_kwargs)
def to_string(self, **kwargs):
"""
Returns Database as a string.
This function is a wrapper for calling `to_file` with StringIO.
Parameters
----------
kwargs : optional
See keyword arguments for `to_file`.
Returns
-------
result : str
"""
result = StringIO()
self.to_file(result, **kwargs)
return result.getvalue()
def __str__(self):
result = 'Elements: {0}\n'.format(sorted(self.elements))
result += 'Species: {0}\n'.format(sorted(self.species))
for name, phase in sorted(self.phases.items()):
result += str(phase)+'\n'
result += '{0} symbols in database\n'.format(len(self.symbols))
result += '{0} parameters in database\n'.format(len(self._parameters))
return result
def __eq__(self, other):
if self is other:
return True
elif type(self) != type(other):
return False
elif sorted(self.__dict__.keys()) != sorted(other.__dict__.keys()):
return False
else:
def param_sort_key(x):
return x['phase_name'], x['parameter_type'], x['constituent_array'], \
x['parameter_order'], x['diffusing_species']
for key in self.__dict__.keys():
if key == '_parameters':
# Special handling for TinyDB objects
if len(self._parameters.all()) != len(other._parameters.all()):
return False
self_params = sorted(self._parameters.all(), key=param_sort_key)
other_params = sorted(other._parameters.all(), key=param_sort_key)
if self_params != other_params:
return False
elif self.__dict__[key] != other.__dict__[key]:
return False
return True
def __ne__(self, other):
return not self.__eq__(other)
def add_structure_entry(self, local_name, global_name):
"""
Define a relation between the system-local name of a phase and a
"global" identifier. This is used to link crystallographically
similar phases known by different colloquial names.
Parameters
----------
local_name : string
System-local name of the phase.
global_name : object
Abstract representation of symbol, e.g., in SymPy format.
Examples
--------
None yet.
"""
self._structure_dict[local_name] = global_name
def add_parameter(self, param_type, phase_name, #pylint: disable=R0913
constituent_array, param_order,
param, ref=None, diffusing_species=None):
"""
Add a parameter.
Parameters
----------
param_type : str
Type name of the parameter, e.g., G, L, BMAGN.
phase_name : str
Name of the phase.
constituent_array : list
Configuration of the sublattices (elements and/or species).
param_order : int
Polynomial order of the parameter.
param : object
Abstract representation of the parameter, e.g., in SymPy format.
ref : str, optional
Reference for the parameter.
diffusing_species : str, optional
(If kinetic parameter) Diffusing species for this parameter.
Examples
--------
None yet.
"""
new_parameter = {
'phase_name': phase_name,
'constituent_array': _to_tuple(constituent_array), # must be hashable type
'parameter_type': param_type,
'parameter_order': param_order,
'parameter': param,
'diffusing_species': diffusing_species,
'reference': ref
}
param_id = self._parameters.insert(new_parameter)
return param_id
def add_phase(self, phase_name, model_hints, sublattices):
"""
Add a phase.
Parameters
----------
phase_name : string
System-local name of the phase.
model_hints : list
Structured "hints" for a Model trying to read this phase.
Hints for major constituents and typedefs (Thermo-Calc) go here.
sublattices : list
Site ratios of sublattices.
Examples
--------
None yet.
"""
new_phase = Phase()
new_phase.name = phase_name
# Need to convert from ParseResults or else equality testing will break
new_phase.sublattices = tuple(sublattices)
new_phase.model_hints = model_hints
self.phases[phase_name] = new_phase
def add_phase_constituents(self, phase_name, constituents):
"""
Add a phase.
Parameters
----------
phase_name : string
System-local name of the phase.
constituents : list
Possible phase constituents (elements and/or species).
Examples
--------
None yet.
"""
try:
# Need to convert constituents from ParseResults
# Otherwise equality testing will be broken
self.phases[phase_name].constituents = tuple(map(frozenset, constituents))
except KeyError:
print("Undefined phase "+phase_name)
raise
def search(self, query):
"""
Search for parameters matching the specified query.
Parameters
----------
query : object
Structured database query in TinyDB format.
Examples
--------
>>>> from tinydb import where
>>>> db = Database()
>>>> eid = db.add_parameter(...) #TODO
>>>> db.search(where('eid') == eid)
"""
return self._parameters.search(query)