Skip to content

Commit

Permalink
rename things so no cryspy dependence shows up.
Browse files Browse the repository at this point in the history
code reshuffling to enable calculator independce, etc.
  • Loading branch information
rozyczko committed Nov 21, 2023
1 parent 2f510ee commit 995d56c
Show file tree
Hide file tree
Showing 8 changed files with 2,162 additions and 2,225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ EaElements.ComboBox {
EaComponents.TableViewParameter {
enabled: false
text: typeof comboBox.model[currentIndex] !== 'undefined' ?
comboBox.model[currentIndex].name :
comboBox.model[currentIndex]['name'].value :
''
}
}
Expand Down
16 changes: 8 additions & 8 deletions easyDiffractionApp/Logic/Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
from PySide6.QtCore import QObject, Slot

from EasyApp.Logic.Logging import console
from easyDiffractionLib.calculators.cryspy import Data as CryspyData # TODO: make non-cryspy specific
from easyDiffractionLib.calculators.cryspy import Data as CalcData # TODO: make non-cryspy specific

class Data(QObject):
def __init__(self, parent, interface=None):
super().__init__(parent)
self.interface = interface
self._data = self.interface.data()
self._cryspyDict = self._data._cryspyDict
self._cryspyObj = self._data._cryspyObj
self._cryspyInOutDict = self._data._cryspyInOutDict
self._calcDict = self._data._cryspyDict
self._calcObj = self._data._cryspyObj
self._calcInOutDict = self._data._cryspyInOutDict

@Slot()
def resetAll(self):
self._data.reset()

def cryspyDictParamPathToStr(p):
return CryspyData.cryspyDictParamPathToStr(p)
def calcDictParamPathToStr(p):
return CalcData.cryspyDictParamPathToStr(p)

def strToCryspyDictParamPath(s):
return CryspyData.strToCryspyDictParamPath(s)
def strToCalcDictParamPath(s):
return CalcData.strToCryspyDictParamPath(s)
94 changes: 43 additions & 51 deletions easyDiffractionApp/Logic/Experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import cryspy
from cryspy.H_functions_global.function_1_cryspy_objects import \
str_to_globaln
from cryspy.procedure_rhochi.rhochi_by_dictionary import \
rhochi_calc_chi_sq_by_dictionary
console.debug('CrysPy module imported')
except ImportError:
console.debug('No CrysPy module found')
Expand Down Expand Up @@ -662,13 +660,13 @@ def loadExperimentsFromEdCif(self, edCif):

@Slot(str)
def replaceExperiment(self, edCifNoMeas=''):
console.debug("Cryspy obj and dict need to be replaced")
console.debug("Calculator obj and dict need to be replaced")

currentDataBlock = self.dataBlocksNoMeas[self.currentIndex]
currentExperimentName = currentDataBlock['name']['value']

cryspyObjBlockNames = [item.data_name for item in self._proxy.data._cryspyObj.items]
cryspyObjBlockIdx = cryspyObjBlockNames.index(currentExperimentName)
calcObjBlockNames = [item.data_name for item in self._proxy.data._calcObj.items]
calcObjBlockIdx = calcObjBlockNames.index(currentExperimentName)

if not edCifNoMeas:
edCifNoMeas = dataBlockToCif(currentDataBlock)
Expand All @@ -683,20 +681,21 @@ def replaceExperiment(self, edCifNoMeas=''):

edCif = edCifNoMeas + '\n\n' + edCifMeasOnly

cryspyCif = cifV2ToV1(edCif)
cryspyExperimentsObj = str_to_globaln(cryspyCif)
cryspyExperimentsDict = cryspyExperimentsObj.get_dictionary()
blocks = self._interface.replaceExpCif(edCif, currentExperimentName)
self._dataBlocksNoMeas[self.currentIndex] = blocks

# Powder diffraction experiment always has 'pd_' prefix if recognized by CrysPy
# Had to add edCifMeasOnly loop to the input CIF in order to allow CrysPy to do this
cryspyDictBlockName = f'pd_{currentExperimentName}'
# calcCif = cifV2ToV1(edCif)
# calcExperimentsObj = str_to_globaln(calcCif)
# calcExperimentsDict = calcExperimentsObj.get_dictionary()

_, edExperimentsNoMeas = CryspyParser.cryspyObjAndDictToEdExperiments(cryspyExperimentsObj,
cryspyExperimentsDict)
# calcDictBlockName = f'pd_{currentExperimentName}'

self._proxy.data._cryspyObj.items[cryspyObjBlockIdx] = cryspyExperimentsObj.items[0]
self._proxy.data._cryspyDict[cryspyDictBlockName] = cryspyExperimentsDict[cryspyDictBlockName]
self._dataBlocksNoMeas[self.currentIndex] = edExperimentsNoMeas[0]
# _, edExperimentsNoMeas = CryspyParser.calcObjAndDictToEdExperiments(calcExperimentsObj,
# calcExperimentsDict)

# self._proxy.data._calcObj.items[calcObjBlockIdx] = calcExperimentsObj.items[0]
# self._proxy.data._calcDict[calcDictBlockName] = calcExperimentsDict[calcDictBlockName]
# self._dataBlocksNoMeas[self.currentIndex] = edExperimentsNoMeas[0]

console.debug(f"Experiment data block '{currentExperimentName}' (no. {self.currentIndex + 1}) (without measured data) has been replaced")
self.dataBlocksNoMeasChanged.emit() # self.dataBlocksNoMeasChanged.emit(blockIdx)
Expand Down Expand Up @@ -749,8 +748,8 @@ def setMainParamWithFullUpdate(self, blockIdx, category, name, field, value):
@Slot(int, str, str, str, 'QVariant')
def setMainParam(self, blockIdx, category, name, field, value):
changedIntern = self.editDataBlockMainParam(blockIdx, category, name, field, value)
changedCryspy = self.editCryspyDictByMainParam(blockIdx, category, name, field, value)
if changedIntern and changedCryspy:
changedCalc = self.editCalcDictByMainParam(blockIdx, category, name, field, value)
if changedIntern and changedCalc:
self.dataBlocksNoMeasChanged.emit()

@Slot(int, str, str, int, str, 'QVariant')
Expand All @@ -763,8 +762,8 @@ def setLoopParamWithFullUpdate(self, blockIdx, category, name, rowIndex, field,
@Slot(int, str, str, int, str, 'QVariant')
def setLoopParam(self, blockIdx, category, name, rowIndex, field, value):
changedIntern = self.editDataBlockLoopParam(blockIdx, category, name, rowIndex, field, value)
changedCryspy = self.editCryspyDictByLoopParam(blockIdx, category, name, rowIndex, field, value)
if changedIntern and changedCryspy:
changedCalc = self.editCalcDictByLoopParam(blockIdx, category, name, rowIndex, field, value)
if changedIntern and changedCalc:
self.dataBlocksNoMeasChanged.emit()

@Slot(str, int)
Expand All @@ -783,13 +782,6 @@ def resetBkgToDefault(self):
self.replaceExperiment()

# Private methods

def cryspyObjExperiments(self):
cryspyObj = self._proxy.data._cryspyObj
cryspyExperimentType = cryspy.E_data_classes.cl_2_pd.Pd
experiments = [block for block in cryspyObj.items if type(block) == cryspyExperimentType]
return experiments

def removeDataBlockLoopRow(self, category, rowIndex):
block = 'experiment'
blockIdx = self._currentIndex
Expand Down Expand Up @@ -903,39 +895,39 @@ def blocksToLoopJob(self, blockIdx, category, name, rowIndex, field, value):
job_with_item.fixed = not value
pass

def editCryspyDictByMainParam(self, blockIdx, category, name, field, value):
def editCalcDictByMainParam(self, blockIdx, category, name, field, value):
if field != 'value' and field != 'fit':
return True

path, value = self.cryspyDictPathByMainParam(blockIdx, category, name, value)
path, value = self.calcDictPathByMainParam(blockIdx, category, name, value)
if field == 'fit':
path[1] = f'flags_{path[1]}'

oldValue = self._proxy.data._cryspyDict[path[0]][path[1]][path[2]]
oldValue = self._proxy.data._calcDict[path[0]][path[1]][path[2]]
if oldValue == value:
return False
self._proxy.data._cryspyDict[path[0]][path[1]][path[2]] = value
self._proxy.data._calcDict[path[0]][path[1]][path[2]] = value

console.debug(formatMsg('sub', 'Cryspy dict', f'{oldValue}{value}', f'{path}'))
console.debug(formatMsg('sub', 'Calc dict', f'{oldValue}{value}', f'{path}'))
return True

def editCryspyDictByLoopParam(self, blockIdx, category, name, rowIndex, field, value):
def editCalcDictByLoopParam(self, blockIdx, category, name, rowIndex, field, value):
if field != 'value' and field != 'fit':
return True

path, value = self.cryspyDictPathByLoopParam(blockIdx, category, name, rowIndex, value)
path, value = self.calcDictPathByLoopParam(blockIdx, category, name, rowIndex, value)
if field == 'fit':
path[1] = f'flags_{path[1]}'

oldValue = self._proxy.data._cryspyDict[path[0]][path[1]][path[2]]
oldValue = self._proxy.data._calcDict[path[0]][path[1]][path[2]]
if oldValue == value:
return False
self._proxy.data._cryspyDict[path[0]][path[1]][path[2]] = value
self._proxy.data._calcDict[path[0]][path[1]][path[2]] = value

console.debug(formatMsg('sub', 'Cryspy dict', f'{oldValue}{value}', f'{path}'))
return True

def cryspyDictPathByMainParam(self, blockIdx, category, name, value):
def calcDictPathByMainParam(self, blockIdx, category, name, value):
blockName = self._dataBlocksNoMeas[blockIdx]['name']['value']
path = ['','','']
path[0] = f"pd_{blockName}"
Expand Down Expand Up @@ -993,7 +985,7 @@ def cryspyDictPathByMainParam(self, blockIdx, category, name, value):

return path, value

def cryspyDictPathByLoopParam(self, blockIdx, category, name, rowIndex, value):
def calcDictPathByLoopParam(self, blockIdx, category, name, rowIndex, value):
blockName = self._dataBlocksNoMeas[blockIdx]['name']['value']
path = ['','','']
path[0] = f"pd_{blockName}"
Expand Down Expand Up @@ -1093,7 +1085,7 @@ def paramValueByFieldAndCrypyParamPath(self, field, path): # NEED FIX: code dup

def editDataBlockByLmfitParams(self, params):
for param in params.values():
block, group, idx = Data.strToCryspyDictParamPath(param.name)
block, group, idx = Data.strToCalcDictParamPath(param.name)

# pd (powder diffraction) block
if block.startswith('pd_'):
Expand Down Expand Up @@ -1199,34 +1191,34 @@ def runProfileCalculations(self):

def setMeasuredArraysForSingleExperiment(self, idx):
ed_name = self._proxy.experiment.dataBlocksNoMeas[idx]['name']['value']
cryspy_block_name = f'pd_{ed_name}'
cryspyInOutDict = self._proxy.data._cryspyInOutDict
calc_block_name = f'pd_{ed_name}'
calcInOutDict = self._proxy.data._calcInOutDict

# X data
x_array = cryspyInOutDict[cryspy_block_name]['ttheta']
x_array = calcInOutDict[calc_block_name]['ttheta']
x_array = np.rad2deg(x_array)
self.setXArray(x_array, idx)

# Measured Y data
y_meas_array = cryspyInOutDict[cryspy_block_name]['signal_exp'][0]
y_meas_array = calcInOutDict[calc_block_name]['signal_exp'][0]
self.setYMeasArray(y_meas_array, idx)

# Standard deviation of the measured Y data
sy_meas_array = cryspyInOutDict[cryspy_block_name]['signal_exp'][1]
sy_meas_array = calcInOutDict[calc_block_name]['signal_exp'][1]
self.setSYMeasArray(sy_meas_array, idx)

def setCalculatedArraysForSingleExperiment(self, idx):
ed_name = self._proxy.experiment.dataBlocksNoMeas[idx]['name']['value']
cryspy_block_name = f'pd_{ed_name}'
cryspyInOutDict = self._proxy.data._cryspyInOutDict
calc_block_name = f'pd_{ed_name}'
calcInOutDict = self._proxy.data._calcInOutDict

# Background Y data # NED FIX: use calculatedYBkgArray()
y_bkg_array = cryspyInOutDict[cryspy_block_name]['signal_background']
y_bkg_array = calcInOutDict[calc_block_name]['signal_background']
self.setYBkgArray(y_bkg_array, idx)

# Total calculated Y data (sum of all phases up and down polarisation plus background)
y_calc_total_array = cryspyInOutDict[cryspy_block_name]['signal_plus'] + \
cryspyInOutDict[cryspy_block_name]['signal_minus'] + \
y_calc_total_array = calcInOutDict[calc_block_name]['signal_plus'] + \
calcInOutDict[calc_block_name]['signal_minus'] + \
y_bkg_array
self.setYCalcTotalArray(y_calc_total_array, idx)

Expand All @@ -1238,10 +1230,10 @@ def setCalculatedArraysForSingleExperiment(self, idx):
# Bragg peaks data
#cryspyInOutDict[cryspy_name]['dict_in_out_co2sio4']['index_hkl'] # [0] - h array, [1] - k array, [2] - l array
#cryspyInOutDict[cryspy_name]['dict_in_out_co2sio4']['ttheta_hkl'] # need rad2deg
modelNames = [key[12:] for key in cryspyInOutDict[cryspy_block_name].keys() if 'dict_in_out' in key]
modelNames = [key[12:] for key in calcInOutDict[calc_block_name].keys() if 'dict_in_out' in key]
xBraggDict = {}
for modelName in modelNames:
x_bragg_array = cryspyInOutDict[cryspy_block_name][f'dict_in_out_{modelName}']['ttheta_hkl']
x_bragg_array = calcInOutDict[calc_block_name][f'dict_in_out_{modelName}']['ttheta_hkl']
x_bragg_array = np.rad2deg(x_bragg_array)
xBraggDict[modelName] = x_bragg_array
self.setXBraggDict(xBraggDict, idx)
Expand Down
8 changes: 4 additions & 4 deletions easyDiffractionApp/Logic/Fittables.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,24 +141,24 @@ def editSilently(self, blockType, blockIdx, category, rowIndex, name, field, val
self._proxy.experiment.editDataBlockMainParam(blockIdx, category, name, 'error', 0) # NEED FIX. Temp solution to reset su
changedIntern = self._proxy.experiment.editDataBlockMainParam(blockIdx, category, name, field, value)
# update cryspy model
changedCryspy = self._proxy.experiment.editCryspyDictByMainParam(blockIdx, category, name, field, value)
changedCryspy = self._proxy.experiment.editCalcDictByMainParam(blockIdx, category, name, field, value)
elif blockType == 'model':
self._proxy.model.editDataBlockMainParam(blockIdx, category, name, 'error', 0) # NEED FIX. Temp solution to reset su
changedIntern = self._proxy.model.editDataBlockMainParam(blockIdx, category, name, field, value)
self._proxy.model.blocksToPhase(blockIdx, category, name, field, value)
# update cryspy model
changedCryspy = self._proxy.model.editCryspyDictByMainParam(blockIdx, category, name, field, value)
changedCryspy = self._proxy.model.editCalcDictByMainParam(blockIdx, category, name, field, value)
else:
console.debug(formatMsg('main', 'Changing fittable', f'{blockType}[{blockIdx}].{category}[{rowIndex}].{name}.{field} to {value}'))
if blockType == 'experiment':
self._proxy.experiment.editDataBlockLoopParam(blockIdx, category, name, rowIndex, 'error', 0) # NEED FIX. Temp solution to reset su
changedIntern = self._proxy.experiment.editDataBlockLoopParam(blockIdx, category, name, rowIndex, field, value)
changedCryspy = self._proxy.experiment.editCryspyDictByLoopParam(blockIdx, category, name, rowIndex, field, value)
changedCryspy = self._proxy.experiment.editCalcDictByLoopParam(blockIdx, category, name, rowIndex, field, value)
elif blockType == 'model':
self._proxy.model.editDataBlockLoopParam(blockIdx, category, name, rowIndex, 'error', 0) # NEED FIX. Temp solution to reset su
changedIntern = self._proxy.model.editDataBlockLoopParam(blockIdx, category, name, rowIndex, field, value)
self._proxy.model.blocksToLoopPhase(blockIdx, category, name, rowIndex, field, value)
changedCryspy = self._proxy.model.editCryspyDictByLoopParam(blockIdx, category, name, rowIndex, field, value)
changedCryspy = self._proxy.model.editCalcDictByLoopParam(blockIdx, category, name, rowIndex, field, value)
if changedIntern and changedCryspy:
if blockType == 'model':
self.modelChangedSilently.emit()
Expand Down
Loading

0 comments on commit 995d56c

Please sign in to comment.