Skip to content

Commit

Permalink
Merge branch 'xraypy:master' into struct2xasV2407
Browse files Browse the repository at this point in the history
  • Loading branch information
maurov authored Oct 25, 2024
2 parents bddd35c + 3e27376 commit 60d6792
Show file tree
Hide file tree
Showing 20 changed files with 477 additions and 314 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test with Ubuntu, Mamba
name: Test with Ubuntu, Conda

on: [push]

Expand All @@ -19,15 +19,14 @@ jobs:
uses: conda-incubator/[email protected]
with:
miniconda-version: latest
mamba-version: "*"
channels: conda-forge,defaults
channel-priority: true
activate-environment: anaconda-client-env
python-version: ${{ matrix.python-version }}
- name: Install conda dependencies
run: |
printenv
mamba install -y -c conda-forge python=${{ matrix.python-version }} "numpy>=1.23" "scipy>=1.8" "matplotlib>=3.6" "h5py>=3.5" "wxpython>=4.2" scikit-image scikit-learn pycifrw pandas jupyter plotly pyparsing pytest pytest-cov coverage
conda install -y -c conda-forge python=${{ matrix.python-version }} "numpy>=1.23" "scipy>=1.8" "matplotlib>=3.6" "h5py>=3.5" "wxpython>=4.2" scikit-image scikit-learn pandas jupyter plotly pyparsing pytest pytest-cov coverage
- name: Install xraylarch and other dependencies with pip
run: |
pip install ".[all]"
Expand Down
35 changes: 0 additions & 35 deletions .github/workflows/test-windows-mamba.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/test-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Install conda dependencies
run: |
printenv
conda install -y -c conda-forge python=${{ matrix.python-version }} "numpy>=1.23" "scipy>=1.8" "matplotlib>=3.6" "h5py>=3.5" "wxpython>=4.2" scikit-image scikit-learn pycifrw pandas jupyter plotly pyparsing pytest pytest-cov coverage
conda install -y -c conda-forge python=${{ matrix.python-version }} "numpy>=1.23" "scipy>=1.8" "matplotlib>=3.6" "h5py>=3.5" "wxpython>=4.2" scikit-image scikit-learn pandas jupyter plotly pyparsing pytest pytest-cov coverage
- name: Install xraylarch and other dependencies with pip
run: |
printenv
Expand Down
6 changes: 2 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
Larch: Data Analysis Tools for X-ray Spectroscopy and More
============================================================

.. image:: https://github.com/xraypy/xraylarch/actions/workflows/test-ubuntu-mamba.yml/badge.svg
:target: https://github.com/xraypy/xraylarch/actions/workflows/test-ubuntu-mamba.yml
.. image:: https://github.com/xraypy/xraylarch/actions/workflows/test-windows-mamba.yml/badge.svg
:target: https://github.com/xraypy/xraylarch/actions/workflows/test-ubuntu-mamba.yml
.. image:: https://github.com/xraypy/xraylarch/actions/workflows/test-ubuntu.yml/badge.svg
:target: https://github.com/xraypy/xraylarch/actions/workflows/test-ubuntu.yml
.. image:: https://github.com/xraypy/xraylarch/actions/workflows/test-windows.yml/badge.svg
:target: https://github.com/xraypy/xraylarch/actions/workflows/test-windows.yml

Expand Down
2 changes: 1 addition & 1 deletion installers/GetLarch.bat
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ echo ## basic mamba installed, running updates
set PATH=%prefix%;%prefix%\bin;%prefix%\condabin;%prefix%\Scripts;%PATH%

echo ## Installing basic python scipy packages
call %prefix%\Scripts\mamba install -yc conda-forge python==3.11.5 numpy scipy matplotlib h5py scikit-image scikit-learn pycifrw pandas jupyter plotly wxpython fabio pyfai pymatgen mkl_fft tomopy
call %prefix%\Scripts\mamba install -yc conda-forge python==3.11.5 numpy scipy matplotlib h5py scikit-image scikit-learn pandas jupyter plotly wxpython fabio pyfai pymatgen mkl_fft tomopy

echo ## Installing xraylarch and dependencies from PyPI
call %prefix%\Scripts\pip install xraylarch[larix]
Expand Down
2 changes: 1 addition & 1 deletion installers/GetLarch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ condafile="Mambaforge-$uname-x86_64.sh"
logfile=GetLarch.log

## set list of conda packages to install from conda-forge
cforge_pkgs="python=>3.12.3 numpy scipy matplotlib h5py scikit-image scikit-learn pycifrw pandas jupyter plotly wxpython fabio pyfai pymatgen mkl_fft tomopy"
cforge_pkgs="python=>3.12.3 numpy scipy matplotlib h5py scikit-image scikit-learn pandas jupyter plotly wxpython fabio pyfai pymatgen mkl_fft tomopy"

unset CONDA_EXE CONDA_PYTHON_EXE CONDA_PREFIX PROJ_LIB

Expand Down
1 change: 0 additions & 1 deletion installers/conda_constructor/construct.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,4 @@ specs:
- wxpython>=4.2.0
- tomopy
- pymatgen
- pycifrw
- numdifftools
8 changes: 4 additions & 4 deletions larch/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import numpy
from pathlib import Path
from copy import deepcopy

from functools import partial
from inspect import signature
from . import site_config
from asteval import valid_symbol_name
from .symboltable import SymbolTable, Group, isgroup
Expand Down Expand Up @@ -154,9 +155,8 @@ def __init__(self, symtable=None, input=None, writer=None,
group = self.symtable.set_symbol(groupname,
value=Group(__name__=groupname))
for fname, fcn in list(entries.items()):
if callable(fcn):
setattr(group, fname,
Closure(func=fcn, _larch=self, _name=fname))
if callable(fcn) and '_larch' in signature(fcn).parameters:
setattr(group, fname, Closure(func=fcn, _larch=self))
else:
setattr(group, fname, fcn)

Expand Down
21 changes: 19 additions & 2 deletions larch/io/athena_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from larch import Group, repr_value
from larch import __version__ as larch_version
from larch.utils import bytes2str, str2bytes, fix_varname, asfloat, unixpath
from larch.math import remove_dups

from xraydb import guess_edge
import asteval
Expand Down Expand Up @@ -760,6 +761,8 @@ def read(self, filename=None, match=None, do_preedge=True, do_bkg=False,
raise IOError("%s '%s': cannot find file" % (ERR_MSG, self.filename))

from larch.xafs import pre_edge, autobk, xftf
from larch.xafs.xafsutils import TINY_ENERGY


if not Path(filename).exists():
raise IOError("file '%s' not found" % filename)
Expand Down Expand Up @@ -797,6 +800,17 @@ def read(self, filename=None, match=None, do_preedge=True, do_bkg=False,
if not fnmatch(gname.lower(), match):
continue
this = getattr(data, gname)
this.energy = remove_dups(this.energy, tiny=TINY_ENERGY)
eorder = np.argsort(this.energy)
has_nan = False
for aname in dir(this):
obj = getattr(this, aname)
if isinstance(obj, np.ndarray) and len(eorder) == len(obj):
setattr(this, aname, obj[eorder])
has_nan = has_nan or any(np.isnan(obj))
this.energy = remove_dups(this.energy, tiny=TINY_ENERGY)
if has_nan:
print("NOTE: nans seen!!")

this.athena_id = this.athena_params.id
if use_hashkey:
Expand Down Expand Up @@ -835,15 +849,18 @@ def read(self, filename=None, match=None, do_preedge=True, do_bkg=False,
if is_chi:
this.k = this.energy*1.0
this.chi = this.mu*1.0
this.xdat = this.energy*1.0
this.ydat = this.mu*1.0
del this.energy
del this.mu
else:
this.xdat = 1.0*this.energy
this.ydat = 1.0*this.mu

# add a selection flag and XAS datatypes, as used by Larix
this.sel = 1
this.datatype = 'xas'
this.filename = getattr(this, 'label', 'unknown')
this.xdat = 1.0*this.energy
this.ydat = 1.0*this.mu
this.yerr = 1.0
this.plot_xlabel = 'energy'
this.plot_ylabel = 'mu'
Expand Down
14 changes: 10 additions & 4 deletions larch/io/mergegroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
import numpy as np
from larch import Group
from larch.math import interp, interp1d, index_of, remove_dups
from larch.math import interp, interp1d, index_of, remove_dups, remove_nans
from larch.utils.logging import getLogger

_logger = getLogger("larch.io.mergegroups")
Expand All @@ -32,14 +32,20 @@ def merge_groups(grouplist, master=None, xarray='energy', yarray='mu',
if master is None:
master = grouplist[0]

xout = remove_dups(getattr(master, xarray))
xarr = getattr(master, xarray)
dxperc = np.percentile(abs(np.diff(xarr)), [1, 2, 25])
dxmin = min(0.01*dxperc[1], 1.e-4*dxperc[2])


xout = remove_dups(xarr, dxmin)
xout = remove_nans(xout, interp=True)
xmins = [min(xout)]
xmaxs = [max(xout)]
yvals = []

for g in grouplist:
x = getattr(g, xarray)
y = getattr(g, yarray)
x = remove_nans(remove_dups(getattr(g, xarray), dxmin), interp=True)
y = remove_nans(getattr(g, yarray), interp=True)
yvals.append(interp(x, y, xout, kind=kind))
xmins.append(min(x))
xmaxs.append(max(x))
Expand Down
80 changes: 54 additions & 26 deletions larch/wxlib/cif_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
from xraydb import atomic_number

import larch

from larch import Group
from larch.xafs import feff8l, feff6l
from larch.xrd.cif2feff import cif_sites
from larch.xrd.cif2feff import cif2feffinp, cif_cluster, site_label, fcompact
from larch.utils import read_textfile, mkdir
from larch.utils.paths import unixpath
from larch.utils.strutils import fix_filename, unique_name, strict_ascii
Expand Down Expand Up @@ -227,8 +228,8 @@ def createMainPanel(self):
wids['feffvers'] = Choice(panel, choices=['6', '8'], default=1,
size=(80, -1),
action=self.onGetFeff)
wids['feff_site'] = Choice(panel, choices=['1', '2', '3', '4'],
size=(80, -1),
wids['feff_site'] = Choice(panel, choices=[' ', ' ', ' ', ' '],
size=(200, -1),
action=self.onGetFeff)
wids['feff_cluster_size'] = FloatSpin(panel, value=7.0, digits=2,
increment=0.1, max_val=10,
Expand All @@ -244,10 +245,10 @@ def createMainPanel(self):

sizer.Add(SimpleText(panel, ' Absorbing Atom: '), (ir, 0), (1, 1), LEFT, 3)
sizer.Add(wids['feff_central_atom'], (ir, 1), (1, 1), LEFT, 3)
sizer.Add(SimpleText(panel, ' Crystal Site: '), (ir, 2), (1, 1), LEFT, 3)
sizer.Add(wids['feff_site'], (ir, 3), (1, 1), LEFT, 3)
sizer.Add(SimpleText(panel, ' Edge: '), (ir, 4), (1, 1), LEFT, 3)
sizer.Add(wids['feff_edge'], (ir, 5), (1, 1), LEFT, 3)
sizer.Add(SimpleText(panel, ' Edge: '), (ir, 2), (1, 1), LEFT, 3)
sizer.Add(wids['feff_edge'], (ir, 3), (1, 1), LEFT, 3)
sizer.Add(SimpleText(panel, ' Crystal Site: '), (ir, 4), (1, 1), LEFT, 3)
sizer.Add(wids['feff_site'], (ir, 5), (1, 1), LEFT, 3)

ir += 1
sizer.Add(SimpleText(panel, ' Cluster Size (\u212B): '), (ir, 0), (1, 1), LEFT, 3)
Expand Down Expand Up @@ -292,16 +293,17 @@ def _swallow_plot_messages(s, panel=0):
self.plotpanel.SetMaxSize((675, 400))
self.plotpanel.onPanelExposed = self.showXRD1D

fw_font = wx.Font(FONTSIZE, wx.MODERN, wx.NORMAL, wx.BOLD)

cif_panel = wx.Panel(rightpanel)
wids['cif_text'] = wx.TextCtrl(cif_panel, value='<CIF TEXT>',
style=wx.TE_MULTILINE|wx.TE_READONLY,
size=(700, 450))
wids['cif_text'].SetFont(Font(FONTSIZE+1))
size=(725, 450))
wids['cif_text'].SetFont(fw_font)
cif_sizer = wx.BoxSizer(wx.VERTICAL)
cif_sizer.Add(wids['cif_text'], 0, LEFT, 1)
pack(cif_panel, cif_sizer)


self.nbpages = []
for label, page in (('CIF Text', cif_panel),
('1-D XRD Pattern', self.plotpanel),
Expand All @@ -318,11 +320,12 @@ def _swallow_plot_messages(s, panel=0):
wids['feff_text'] = wx.TextCtrl(feffinp_panel,
value='<Feff Input Text>',
style=wx.TE_MULTILINE,
size=(700, 450))
size=(725, 450))
wids['feff_text'].SetFont(fw_font)
wids['feff_text'].CanCopy()

feffinp_panel.onPanelExposed = self.onGetFeff
wids['feff_text'].SetFont(Font(FONTSIZE+1))
# wids['feff_text'].SetFont(Font(FONTSIZE+1))
feff_sizer = wx.BoxSizer(wx.VERTICAL)
feff_sizer.Add(wids['feff_text'], 0, LEFT, 1)
pack(feffinp_panel, feff_sizer)
Expand All @@ -331,9 +334,10 @@ def _swallow_plot_messages(s, panel=0):
wids['feffout_text'] = wx.TextCtrl(feffout_panel,
value='<Feff Output>',
style=wx.TE_MULTILINE,
size=(700, 450))
size=(725, 450))
wids['feffout_text'].CanCopy()
wids['feffout_text'].SetFont(Font(FONTSIZE+1))
wids['feffout_text'].SetFont(fw_font)
# wids['feffout_text'].SetFont(Font(FONTSIZE+1))
feffout_sizer = wx.BoxSizer(wx.VERTICAL)
feffout_sizer.Add(wids['feffout_text'], 0, LEFT, 1)
pack(feffout_panel, feffout_sizer)
Expand Down Expand Up @@ -448,13 +452,14 @@ def onShowCIF(self, event=None, cif_id=None):
self.wids['feff_central_atom'].AppendItems(list(elems.keys()))
self.wids['feff_central_atom'].Select(0)

el0 = list(elems.keys())[0]
edge_val = 'K' if atomic_number(el0) < 60 else 'L3'
catom = list(elems.keys())[0]
edge_val = 'K' if atomic_number(catom) < 60 else 'L3'
self.wids['feff_edge'].SetStringSelection(edge_val)

sites = cif_sites(cif.ciftext, absorber=el0)
cluster = cif_cluster(ciftext=cif.ciftext, absorber=catom)
self.absorber_sites = cluster.all_sites[catom]
try:
sites = ['%d' % (i+1) for i in range(len(sites))]
sites = list(self.absorber_sites.keys())
except:
title = "Could not make sense of atomic sites"
message = [f"Elements: {list(elems.keys())}",
Expand Down Expand Up @@ -482,11 +487,14 @@ def onFeffCentralAtom(self, event=None):
return
catom = event.GetString()
try:
sites = cif_sites(cif.ciftext, absorber=catom)
sites = ['%d' % (i+1) for i in range(len(sites))]
cluster = cif_cluster(ciftext=cif.ciftext, absorber=catom)
self.absorber_sites = cluster.all_sites[catom]

sites = list(self.absorber_sites.keys())
self.wids['feff_site'].Clear()
self.wids['feff_site'].AppendItems(sites)
self.wids['feff_site'].Select(0)

except:
self.write_message(f"could not get sites for central atom '{catom}'")
title = f"Could not get sites for central atom '{catom}'"
Expand All @@ -505,16 +513,37 @@ def onGetFeff(self, event=None):
edge = self.wids['feff_edge'].GetStringSelection()
version8 = '8' == self.wids['feffvers'].GetStringSelection()
catom = self.wids['feff_central_atom'].GetStringSelection()
asite = int(self.wids['feff_site'].GetStringSelection())
label = self.wids['feff_site'].GetStringSelection()
site_index = self.absorber_sites[label]
csize = self.wids['feff_cluster_size'].GetValue()
with_h = not self.wids['feff_without_h'].IsChecked()
mineral = cif.get_mineralname()
folder = f'{catom:s}{asite:d}_{edge:s}_{mineral}_cif{cif.ams_id:d}'

folder = f'{catom:s}{site_index:d}_{edge:s}_{mineral}_cif{cif.ams_id:d}'
folder = unique_name(fix_filename(folder), self.feffruns_list)

fefftext = cif.get_feffinp(catom, edge=edge, cluster_size=csize,
absorber_site=asite, version8=version8,
with_h=with_h)
# extra titles from CIF
etitles = [f'CIF Source: Am Min Crystal Structure Database ID {cif.ams_id}',
f'Mineral Name: {cif.mineral.name.lower()}']
pub = getattr(cif, 'publication', None)
if pub is not None:
authors = ', '.join(pub.authors)
ptext = f'{pub.journalname} {pub.volume} [{pub.page_first}:{pub.page_last}] ({pub.year})'
etitles.append(f'Publication: {ptext}')
etitles.append(f'Authors: {authors}')

cell = [f'a={fcompact(cif.a)}', f'b={fcompact(cif.b)}', f'c={fcompact(cif.c)}',
f'alpha={fcompact(cif.alpha)}', f'beta={fcompact(cif.beta)}',
f'gamma={fcompact(cif.gamma)}']

etitles.append(f'Cell Parameters (A, degrees): {", ".join(cell)}')
etitles.append(f'Cell Volume (A^3): {cif.cell_volume:.5f}')
etitles.append(f'Crystal Density (gr/cm^3): {cif.crystal_density:.5f}')
etitles.append(f'Compound: {cif.compound}')

fefftext = cif2feffinp(cif.ciftext, catom, edge=edge, cluster_size=csize,
absorber_site=site_index, version8=version8,
with_h=with_h, extra_titles=etitles)

self.wids['feff_runfolder'].SetValue(folder)
self.wids['feff_text'].SetValue(fefftext)
Expand Down Expand Up @@ -570,7 +599,6 @@ def onRunFeff(self, event=None):
wx.CallAfter(self.run_feff, folder, version8=version8)

def run_feff(self, folder=None, version8=True):
# print("RUN FEFF ", folder)
dname = Path(folder).name
prog, cmd = feff8l, 'feff8l'
if not version8:
Expand Down
Loading

0 comments on commit 60d6792

Please sign in to comment.