File: SingleLayerStrategy.py

package info (click to toggle)
pymca 5.1.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 40,004 kB
  • ctags: 17,800
  • sloc: python: 132,302; ansic: 20,016; cpp: 827; makefile: 48; sh: 30; xml: 24
file content (159 lines) | stat: -rw-r--r-- 7,310 bytes parent folder | download
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
#/*##########################################################################
#
# The PyMca X-Ray Fluorescence Toolkit
#
# Copyright (c) 2004-2016 European Synchrotron Radiation Facility
#
# This file is part of the PyMca X-ray Fluorescence Toolkit developed at
# the ESRF by the Software group.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#############################################################################*/
__author__ = "V.A. Sole - ESRF Data Analysis"
__contact__ = "sole@esrf.fr"
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
import copy
from . import Elements
from . import ConcentrationsTool

DEBUG = 0

class SingleLayerStrategy(object):
    def __init__(self):
        self._tool = ConcentrationsTool.ConcentrationsTool()

    def applyStrategy(self, fitResult, fluorescenceRates, currentIteration=None):
        """
        Provided a fit result, it returns an new fit configuration and
        a positive integer to indicate the strategy procedure has not finished.

        Returning an empty fit configuration, or a number of iterations equal 0
        will indicate the process is over.
        """
        if DEBUG:
            print("SingleLayerStrategy called with iteration ", currentIteration)
        newConfiguration = copy.deepcopy(fitResult['config'])
        strategyConfiguration = newConfiguration['SingleLayerStrategy']
        if currentIteration is None:
            currentIteration = strategyConfiguration['iterations']
        if currentIteration < 1:
            # enough for the journey
            return {}, 0

        # calculate concentrations with current configuration
        ddict = {}
        ddict.update(newConfiguration['concentrations'])
        ddict, addInfo = self._tool.processFitResult( \
                                config=ddict,
                                fitresult={"result":fitResult},
                                elementsfrommatrix=False,
                                fluorates = fluorescenceRates,
                                addinfo=True)

        # find the layer to be updated the matrix
        matrixKey = None
        for attenuator in list(newConfiguration['attenuators'].keys()):
            if not newConfiguration['attenuators'][attenuator][0]:
                continue
            if attenuator.upper() == "MATRIX":
                if newConfiguration['attenuators'][attenuator][1].upper() != \
                                    "MULTILAYER":
                    matrixKey = attenuator
                else:
                    matrixKey = "MULTILAYER"
            if matrixKey:
                break
        if matrixKey != "MULTILAYER":
            parentKey = 'attenuators'
            daughterKey = matrixKey
        else:
            parentKey = "multilayer"
            daughterKey = None
            if newConfiguration["SingleLayerStrategy"]["layer"].upper() == \
                                                       ["AUTO"]:
                # we have to find the layer where we should work
                firstLayer = None
                for layer in newConfiguration[parentKey]:
                    if newConfiguration[parentKey][layer][0]:
                        material = newConfiguration[parentKey][layer][1]
                        composition = Elements.getMaterialMassFractions( \
                                    [material], [1.0])
                        for group in strategyConfiguration["peaks"]:
                            if "-" in group:
                                continue
                            ele = group.split()[0]
                            if ele in composition:
                                daughterLayer = layer
                                break
                        if firstLayer is None:
                            firstLayer = layer
                if daughterKey is None:
                    daughterKey = firstLayer
            else:
                daughterKey = newConfiguration["SingleLayerStrategy"]["layer"]
            if daughterKey is None:
                raise ValueError("Cannot find appropriate sample layer")
        # newConfiguration[parentKey][daughterKey] composition is to be updated
        # get the new composition
        total = 0.0
        CompoundList = []
        CompoundFraction = []
        materialCounter = -1
        for group in strategyConfiguration["peaks"]:
            materialCounter += 1
            if "-" in group:
                continue
            ele = group.split()[0]
            material = strategyConfiguration["materials"][materialCounter]
            if material in ["-", ele, ele + "1"]:
                CompoundList.append(ele)
                CompoundFraction.append(\
                                ddict["mass fraction"][group])
            else:
                massFractions = Elements.getMaterialMassFractions( \
                            [material], [1.0])
                CompoundFraction.append(massFractions[ele] / \
                                       ddict["mass fraction"][group])
                CompoundList.append(material)
            total += CompoundFraction[-1]
        if strategyConfiguration["completer"] not in ["-"]:
            if total < 1.0:
                CompoundList.append(strategyConfiguration["completer"])
                CompoundFraction.append(1.0 - total)
            else:
                for i in range(len(CompoundFraction)):
                    CompoundFraction[i] /= total;
        else:
            for i in range(len(CompoundFraction)):
                CompoundFraction[i] /= total;
        materialName = "SingleLayerStrategyMaterial"
        newConfiguration["materials"][materialName] = \
            {"Density": newConfiguration['attenuators'][attenuator][2],
             "Thickness":newConfiguration['attenuators'][attenuator][3],
             "CompoundList":CompoundList,
             "CompoundFraction":CompoundFraction,
             "Comment":"Last Single Layer Strategy iteration"}
        # and update it
        newConfiguration[parentKey][daughterKey][1] = materialName
        if DEBUG:
            print("Updated sample material: ", \
                  newConfiguration["materials"][materialName])
        return newConfiguration, currentIteration - 1