Skip to content

Commit 96dc69b

Browse files
authored
Merge pull request #827 from HEXRD/wppf-sanitize-material-names
Sanitize material names in WPPF
2 parents 3060f50 + af38fbf commit 96dc69b

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

hexrd/wppf/phase.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def _readHDF(self, fhdf, xtal):
8686
confusion later on
8787
"""
8888
self.lparms = list(gid.get('LatticeParameters'))
89-
self.name = name
89+
self.name = name.replace('-', '_')
9090
fid.close()
9191

9292
def _init_from_materials(self, material_obj):
@@ -98,7 +98,7 @@ def _init_from_materials(self, material_obj):
9898
O7/01/2021 SS ADDED DIRECT AND RECIPROCAL STRUCTURE MATRIX AS
9999
FIELDS IN THE CLASS
100100
"""
101-
self.name = material_obj.name
101+
self.name = material_obj.name.replace('-', '_')
102102
self.dmin = material_obj.dmin.getVal('nm')
103103
self.sgnum = material_obj.unitcell.sgnum
104104
self.sgsetting = material_obj.sgsetting
@@ -346,7 +346,7 @@ def CalcStar(self, v, space, applyLaue=False):
346346

347347
def removeinversion(self, ksym):
348348
"""
349-
this function chooses a subset from a list
349+
this function chooses a subset from a list
350350
of symmetrically equivalent reflections such
351351
that there are no g and -g present.
352352
"""
@@ -541,12 +541,17 @@ def __str__(self):
541541
return resstr
542542

543543
def __getitem__(self, key):
544+
# Always sanitize the material name since lmfit won't accept '-'
545+
key = key.replace('-', '_')
546+
544547
if(key in self.phase_dict.keys()):
545548
return self.phase_dict[key]
546549
else:
547550
raise ValueError('phase with name not found')
548551

549552
def __setitem__(self, key, mat_cls):
553+
# Always sanitize the material name since lmfit won't accept '-'
554+
key = key.replace('-', '_')
550555

551556
if(key in self.phase_dict.keys()):
552557
warnings.warn('phase already in parameter \
@@ -589,7 +594,7 @@ def add_many(self, material_file, material_keys):
589594
self[k].pf = 1.0/len(self)
590595

591596
self.material_file = material_file
592-
self.material_keys = material_keys
597+
self.material_keys = [k.replace('-', '_') for k in material_keys]
593598

594599
def load(self, fname):
595600
"""
@@ -737,7 +742,7 @@ def _init_from_materials(self, material_obj):
737742
738743
"""
739744
# name
740-
self.name = material_obj.name
745+
self.name = material_obj.name.replace('-', '_')
741746

742747
# inverse of absorption length
743748
self.abs_fact = 1e-4 * (1./material_obj.absorption_length)
@@ -860,7 +865,7 @@ def _readHDF(self, fhdf, xtal):
860865
# read atom types (by atomic number, Z)
861866
self.atom_type = np.array(gid.get('Atomtypes'), dtype=np.int32)
862867
self.atom_ntype = self.atom_type.shape[0]
863-
self.name = name
868+
self.name = name.replace('-', '_')
864869

865870
fid.close()
866871

@@ -1461,12 +1466,17 @@ def __str__(self):
14611466
return resstr
14621467

14631468
def __getitem__(self, key):
1469+
# Always sanitize the material name since lmfit won't accept '-'
1470+
key = key.replace('-', '_')
1471+
14641472
if(key in self.phase_dict.keys()):
14651473
return self.phase_dict[key]
14661474
else:
14671475
raise ValueError('phase with name not found')
14681476

14691477
def __setitem__(self, key, mat_cls):
1478+
# Always sanitize the material name since lmfit won't accept '-'
1479+
key = key.replace('-', '_')
14701480

14711481
if(key in self.phase_dict.keys()):
14721482
warnings.warn('phase already in parameter list. overwriting ...')
@@ -1524,7 +1534,7 @@ def add_many(self, material_file, material_keys):
15241534
self[k][l].pf = 1.0/self.num_phases
15251535

15261536
self.material_file = material_file
1527-
self.material_keys = material_keys
1537+
self.material_keys = [k.replace('-', '_') for k in material_keys]
15281538

15291539
def load(self, fname):
15301540
"""

hexrd/wppf/wppfsupport.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
classes are put here to minimize code duplication. Some examples
3333
include initialize background, generate_default_parameter list etc.
3434
"""
35+
import copy
36+
import warnings
37+
3538
from hexrd.material.symbols import pstr_spacegroup
3639
from hexrd.wppf.parameters import Parameters
3740
from lmfit import Parameters as Parameters_lmfit
@@ -41,7 +44,6 @@
4144
import hexrd
4245
import numpy as np
4346
from hexrd import constants
44-
import warnings
4547

4648
def _generate_default_parameters_pseudovoight(params):
4749
"""
@@ -461,6 +463,9 @@ def _generate_default_parameters_LeBail(mat,
461463
@details: generate a default parameter class given a list/dict/
462464
single instance of material class
463465
"""
466+
# Sanitize the material names
467+
mat = _sanitize_material_names(mat)
468+
464469
if ptype == "wppf":
465470
params = Parameters()
466471
elif ptype == "lmfit":
@@ -659,6 +664,8 @@ def _generate_default_parameters_Rietveld(mat,
659664
@details: generate a default parameter class given a list/dict/
660665
single instance of material class
661666
"""
667+
mat = _sanitize_material_names(mat)
668+
662669
params = _generate_default_parameters_LeBail(mat,
663670
peakshape,
664671
bkgmethod,
@@ -932,6 +939,27 @@ def _add_intensity_parameters(params,hkls,Icalc,prefix):
932939
[params.add(name=pname[i],
933940
value=Icalc[p][k][i],
934941
min=0.0) for i in range(shape)]
942+
943+
944+
def _sanitize_material_names(mats):
945+
if isinstance(mats, Material):
946+
if '-' in mats.name:
947+
mats = copy.deepcopy(mats)
948+
mats.name = mats.name.replace('-', '_')
949+
elif isinstance(mats, (list, dict)):
950+
mats = copy.deepcopy(mats)
951+
if isinstance(mats, list):
952+
mats_iter = mats
953+
else:
954+
mats_iter = mats.values()
955+
956+
for mat in mats_iter:
957+
if '-' in mat.name:
958+
mat.name = mat.name.replace('-', '_')
959+
960+
return mats
961+
962+
935963
background_methods = {
936964
'spline': None,
937965

0 commit comments

Comments
 (0)