Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ jobs:
# with:
# ssh-private-key: ${{ secrets.GH_WEBFACTORY_KEY }}

- name: Set up dependences
if: runner.os == 'Linux'
run: sudo apt-get install libgfortran4
# - name: Set up dependences
# if: runner.os == 'Linux'
# run: sudo apt-get install libgfortran4

- name: Set up Python environment
uses: actions/setup-python@v2
Expand All @@ -52,8 +52,8 @@ jobs:
- name: Create venv and install dependences
run: poetry update

- name: Relink CrysFML from default Python dylib
run: poetry run python tools/Scripts/RelinkCrysfml.py $pythonLocation
# - name: Relink CrysFML from default Python dylib
# run: poetry run python tools/Scripts/RelinkCrysfml.py $pythonLocation

- name: Run main.py
run: poetry run easyDiffractionLib
2 changes: 1 addition & 1 deletion easyDiffractionLib/Calculators/CFML.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def calculate(self, x_array: np.ndarray) -> np.ndarray:

start_time = timeit.default_timer()

hkltth = np.array([[*reflection_list[i].hkl, reflection_list[i].stl] for i in range(reflection_list.nrefs)])
hkltth = np.array([[*reflection_list[i].hkl, reflection_list[i].stl] for i in range(reflection_list.nref)])

self.hkl_dict['ttheta'] = np.rad2deg(np.arcsin(hkltth[:, 3] * job_info.lambdas[0])) * 2
self.hkl_dict['h'] = hkltth[:, 0]
Expand Down
4 changes: 2 additions & 2 deletions easyDiffractionLib/Calculators/GSASII.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import os, pathlib
from easyCore import borg
import GSASIIscriptable as G2sc
from GSASII import GSASIIscriptable as G2sc

from easyCore import np

Expand Down Expand Up @@ -161,4 +161,4 @@ def get_hkl(self, tth: np.array = None) -> dict:
hkl_dict = self.hkl_dict
if tth is not None:
pass
return hkl_dict
return hkl_dict
18 changes: 0 additions & 18 deletions easyDiffractionLib/Calculators/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
__author__ = 'github.com/wardsimon'
__version__ = '0.0.1'

import os, sys

if 'darwin' in sys.platform:
import libsDarwin
libs_path = list(libsDarwin.__path__)[0]
elif 'linux' in sys.platform:
import libsLinux
libs_path = list(libsLinux.__path__)[0]
elif 'win32' in sys.platform:
import libsWin32
libs_path = list(libsWin32.__path__)[0]
else:
raise NotImplementedError(f"Platform '{sys.platform}' is not supported")

gsasii_path = os.path.join(libs_path, "GSASII")

sys.path.append(libs_path)
sys.path.append(gsasii_path)
166 changes: 144 additions & 22 deletions easyDiffractionLib/Calculators/cryspy.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
__author__ = "github.com/wardsimon"
__version__ = "0.0.2"


import cryspy
import warnings
from easyCore import np, borg

warnings.filterwarnings('ignore')


Expand All @@ -22,16 +22,34 @@ def __init__(self):
}

}
self.conditions_TOF = {
'ttheta_bank': 0,
'dtt1': 0.1,
'dtt2': 0,
'resolution': {
'sigma0': 0,
'sigma1': 0,
'sigma2': 0,
'gamma0': 0,
'gamma1': 0,
'gamma2': 0,
'alpha0': 0,
'alpha1': 0,
'beta0': 0,
'beta1': 0}
}
self.background = None
self.hkl_dict = {
'ttheta': np.empty(0),
'h': np.empty(0),
'k': np.empty(0),
'l': np.empty(0)
'h': np.empty(0),
'k': np.empty(0),
'l': np.empty(0)
}
self.storage = {}
self.current_crystal = {}
self.model = None
self.phases = cryspy.PhaseL()
self.type = 'powder1DCW'

@property
def cif_str(self):
Expand All @@ -42,8 +60,17 @@ def cif_str(self):
def cif_str(self, value):
self.createCrystal_fromCifStr(value)

def createModel(self, model_id, model_type=None):
self.model = cryspy.Pd(background=cryspy.PdBackgroundL(), phase=cryspy.PhaseL())
def createModel(self, model_id, model_type='powder1DCW'):
model = {
'background': cryspy.PdBackgroundL(),
'phase': self.phases
}
cls = cryspy.Pd
if model_type == 'powder1DTOF':
cls = cryspy.TOF
model['background'] = cryspy.TOFBackground()
self.type = model_type
self.model = cls(**model)

def createPhase(self, crystal_name, key='phase'):
phase = cryspy.Phase(label=crystal_name, scale=1, igsize=0)
Expand All @@ -52,7 +79,7 @@ def createPhase(self, crystal_name, key='phase'):

def assignPhase(self, model_name, phase_name):
phase = self.storage[phase_name]
self.model.phase.items.append(phase)
self.phases.items.append(phase)

def removePhase(self, model_name, phase_name):
phase = self.storage[phase_name]
Expand Down Expand Up @@ -95,7 +122,7 @@ def createSpaceGroup(self, key='spacegroup', name_hm_alt='P 1'):
# sg = cryspy.SpaceGroup(**opts)
# except Exception as e:
sg = cryspy.SpaceGroup(**opts)
# print(e)
# print(e)
self.storage[key] = sg
return key

Expand Down Expand Up @@ -157,8 +184,18 @@ def createBackground(self, background_obj):
self.storage[key] = background_obj
return key

def createSetup(self, key='setup'):
setup = cryspy.Setup(wavelength=self.conditions['wavelength'], offset_ttheta=0)
def createSetup(self, key='setup', cls_type = None):

if cls_type is None:
cls_type = self.type

if cls_type == 'powder1DCW':
setup = cryspy.Setup(wavelength=self.conditions['wavelength'], offset_ttheta=0)
elif cls_type == 'powder1DTOF':
setup = cryspy.TOFParameters(zero=0, dtt1=self.conditions_TOF['dtt1'], dtt2=self.conditions_TOF['dtt2'],
ttheta_bank=self.conditions_TOF['ttheta_bank'])
else:
raise AttributeError('The experiment is of an unknown type')
self.storage[key] = setup
if self.model is not None:
setattr(self.model, 'setup', setup)
Expand All @@ -174,20 +211,31 @@ def genericReturn(self, item_key, value_key):
value = getattr(item, value_key)
return value

def createResolution(self):
key = 'pd_instr_resolution'
resolution = cryspy.PdInstrResolution(**self.conditions['resolution'])
def createResolution(self, cls_type = None):

if cls_type is None:
cls_type = self.type

if cls_type == 'powder1DCW':
key = 'pd_instr_resolution'
resolution = cryspy.PdInstrResolution(**self.conditions['resolution'])
elif cls_type == 'powder1DTOF':
key = 'tof_profile'
resolution = cryspy.TOFProfile(**self.conditions_TOF['resolution'])
resolution.peak_shape = 'Gauss'
else:
raise AttributeError('The experiment is of an unknown type')
self.storage[key] = resolution
if self.model is not None:
setattr(self.model, 'pd_instr_resolution', resolution)
setattr(self.model, key, resolution)
return key

def updateResolution(self, key, **kwargs):
resolution = self.storage[key]
for r_key in kwargs.keys():
setattr(resolution, r_key, kwargs[key])

def calculate(self, x_array: np.ndarray) -> np.ndarray:
def powder_1d_calculate(self, x_array: np.ndarray) -> np.ndarray:
"""
For a given x calculate the corresponding y
:param x_array: array of data points to be calculated
Expand Down Expand Up @@ -225,15 +273,89 @@ def calculate(self, x_array: np.ndarray) -> np.ndarray:
profile = self.model.calc_profile(this_x_array, [crystal], True, False)
self.hkl_dict = {
'ttheta': self.model.d_internal_val['peak_' + crystal.data_name].numpy_ttheta,
'h': self.model.d_internal_val['peak_'+crystal.data_name].numpy_index_h,
'k': self.model.d_internal_val['peak_'+crystal.data_name].numpy_index_k,
'l': self.model.d_internal_val['peak_'+crystal.data_name].numpy_index_l,
'h': self.model.d_internal_val['peak_' + crystal.data_name].numpy_index_h,
'k': self.model.d_internal_val['peak_' + crystal.data_name].numpy_index_k,
'l': self.model.d_internal_val['peak_' + crystal.data_name].numpy_index_l,
}
res = scale * np.array(profile.intensity_total) + bg
if borg.debug:
print(f"y_calc: {res}")
return res

def powder_1d_tof_calculate(self, x_array: np.ndarray) -> np.ndarray:
"""
For a given x calculate the corresponding y
:param x_array: array of data points to be calculated
:type x_array: np.ndarray
:return: points calculated at `x`
:rtype: np.ndarray
setup, tof_profile, phase, tof_background, tof_meas
"""

for key_inner in ['tof_profile', 'setup']:
if not hasattr(self.model, key_inner):
try:
setattr(self.model, key_inner, self.storage[key_inner])
except ValueError:
# Try to fix cryspy....
s = self.storage[key_inner]
cls = s.__class__
for idx, item in enumerate(self.model.items):
if isinstance(item, cls) and id(item) is not id(s):
self.model.items[idx] = s
break

if self.pattern is None:
scale = 1.0
offset = 0
else:
scale = self.pattern.scale.raw_value / 500.0
offset = self.pattern.zero_shift.raw_value

self.model['tof_parameters'].zero = offset
this_x_array = x_array

if borg.debug:
print('CALLING FROM Cryspy\n----------------------')
# USe the default for now
crystal = self.storage[list(self.current_crystal.keys())[-1]]

if len(self.pattern.backgrounds) == 0:
bg = np.zeros_like(this_x_array)
else:
bg = self.pattern.backgrounds[0].calculate(this_x_array)

if crystal is None:
return bg

profile = self.model.calc_profile(this_x_array, [crystal], True, False)
self.hkl_dict = {
'time': np.array(self.model.d_internal_val['peak_' + crystal.data_name].time),
'h': np.array(self.model.d_internal_val['peak_' + crystal.data_name].index_h),
'k': np.array(self.model.d_internal_val['peak_' + crystal.data_name].index_k),
'l': np.array(self.model.d_internal_val['peak_' + crystal.data_name].index_l),
}
res = scale * np.array(profile.intensity_total) + bg
if borg.debug:
print(f"y_calc: {res}")
return res


def calculate(self, x_array: np.ndarray) -> np.ndarray:
"""
For a given x calculate the corresponding y
:param x_array: array of data points to be calculated
:type x_array: np.ndarray
:return: points calculated at `x`
:rtype: np.ndarray
"""
res = np.zeros_like(x_array)
if self.type == 'powder1DCW':
return self.powder_1d_calculate(x_array)
if self.type == 'powder1DTOF':
return self.powder_1d_tof_calculate(x_array)
return res

def get_hkl(self, tth: np.array = None) -> dict:

hkl_dict = self.hkl_dict
Expand All @@ -252,9 +374,9 @@ def get_hkl(self, tth: np.array = None) -> dict:

hkl_dict = {
'ttheta': self.model.d_internal_val['peak_' + crystal.data_name].numpy_ttheta,
'h': self.model.d_internal_val['peak_' + crystal.data_name].numpy_index_h,
'k': self.model.d_internal_val['peak_' + crystal.data_name].numpy_index_k,
'l': self.model.d_internal_val['peak_' + crystal.data_name].numpy_index_l,
'h': self.model.d_internal_val['peak_' + crystal.data_name].numpy_index_h,
'k': self.model.d_internal_val['peak_' + crystal.data_name].numpy_index_k,
'l': self.model.d_internal_val['peak_' + crystal.data_name].numpy_index_l,
}

return hkl_dict
return hkl_dict
9 changes: 2 additions & 7 deletions easyDiffractionLib/Elements/Experiments/Experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,22 @@ class Pars1D(BaseObj):
'wavelength': {
'@module': 'easyCore.Objects.Base',
'@class': 'Parameter',
'@version': '0.0.1',
'name': 'wavelength',
'units': 'angstrom',
'value': 1.54056,
'fixed': True
'fixed': True,
'min': 0
},
'resolution_u': {
'@module': 'easyCore.Objects.Base',
'@class': 'Parameter',
'@version': '0.0.1',
'name': 'resolution_u',
'value': 0.0002,
'fixed': True
},
'resolution_v': {
'@module': 'easyCore.Objects.Base',
'@class': 'Parameter',
'@version': '0.0.1',
'name': 'resolution_v',
'value': -0.0002,
'fixed': True
Expand All @@ -40,7 +38,6 @@ class Pars1D(BaseObj):
'resolution_w': {
'@module': 'easyCore.Objects.Base',
'@class': 'Parameter',
'@version': '0.0.1',
'name': 'resolution_w',
'value': 0.012,
'fixed': True
Expand All @@ -49,15 +46,13 @@ class Pars1D(BaseObj):
'resolution_x': {
'@module': 'easyCore.Objects.Base',
'@class': 'Parameter',
'@version': '0.0.1',
'name': 'resolution_x',
'value': 0.0,
'fixed': True
},
'resolution_y': {
'@module': 'easyCore.Objects.Base',
'@class': 'Parameter',
'@version': '0.0.1',
'name': 'resolution_y',
'value': 0.0,
'fixed': True
Expand Down
Loading