Commit a2010048 authored by payno's avatar payno

[io] create read_pymca_xas_from_file function

This is replacing the xas_input._interpreteInput function.
The prupose of this function is to pick the default data location for creating DataUrl and calling `read_pymca_xas_from_file`
parent cbb32885
......@@ -325,10 +325,11 @@
"import xas.core.process.k_weight\n",
"import xas.core.process.exafs\n",
"import xas.core.process.ft\n",
"from xas.core.types import XASObject\n",
"import xas.io\n",
"\n",
"\n",
"read_task = Node(callback=xas.io.read_pymca_xas)\n",
"read_task = Node(callback=xas.io.read_pymca_xas_from_file)\n",
"normalization_task = Node(callback=xas.core.process.normalization.pymca_normalization)\n",
"k_weight_task = Node(callback=xas.core.process.k_weight.pymca_k_weight)\n",
"exafs_task = Node(callback=xas.core.process.exafs.pymca_exafs)\n",
......@@ -370,7 +371,8 @@
"data_file = os.path.join(PYMCA_DATA_DIR, \"EXAFS_Cu.dat\")\n",
"out = exec_workflow(scheme=scheme, input_=data_file)\n",
"assert isinstance(out, dict)\n",
"assert 'FTRadius' in out['spectrum']['FT']"
"xas_obj_out = XASObject.from_dict(out)\n",
"assert 'FTRadius' in xas_obj_out.spectra[0].ft"
]
},
{
......
......@@ -31,10 +31,8 @@ __date__ = "06/11/2019"
from Orange.widgets import gui
from Orange.widgets.widget import OWWidget
from silx.gui import qt
from xas.core.types import XASObject, PyMcaSpectrum
from xas.core.types import XASObject
from Orange.widgets.settings import Setting
from silx.io.url import DataUrl
import h5py
import os
import logging
import xas.io
......@@ -60,7 +58,7 @@ class XASInputOW(OWWidget):
_input_file_setting = Setting(str())
process_function = xas.io.read_pymca_xas
process_function = xas.io.read_pymca_xas_from_file
def __init__(self):
super().__init__()
......@@ -126,23 +124,8 @@ class XASInputOW(OWWidget):
return
else:
try:
xas_obj = self._interpreteInput(file_path=self._input_file_setting)
xas_obj = XASInputOW.read_pymca_xas_from_file(file_path=self._input_file_setting)
except ... as e:
_logger.error(e)
else:
self.send("spectra", xas_obj)
def _interpreteInput(self, file_path):
if file_path.endswith('.dat'):
return xas.io.read_pymca_xas(DataUrl(file_path=self._input_file_setting,
scheme='PyMca'))
elif h5py.is_hdf5(file_path):
return xas.io.read_pymca_xas(
spectra_url=DataUrl(file_path=file_path,
scheme='PyMca',
data_path='spectra'),
config_url=DataUrl(file_path=file_path,
scheme='silx',
data_path='configuration'))
else:
raise ValueError('file type not managed, unable to load')
......@@ -167,25 +167,15 @@ class XASObject(object):
self.spectrum = ddict
return self
<<<<<<< HEAD
<<<<<<< HEAD
=======
=======
>>>>>>> 5b3e965... [core] remove the XASFactory
@staticmethod
def from_dict(ddict):
return XASObject().load_frm_dict(ddict=ddict)
<<<<<<< HEAD
@staticmethod
def from_file(h5_file):
ddict = h5todict(h5file=h5_file)
return XASObject.from_dict(ddict=ddict)
>>>>>>> 854819f... fix b81fd6b08587923138a5156441340508914830ac
=======
>>>>>>> 5b3e965... [core] remove the XASFactory
def dump(self, h5_file):
"""dump the XAS object to a file_path"""
dicttoh5(treedict=self.to_dict(), h5file=h5_file)
......
......@@ -37,6 +37,41 @@ from xas.core.types import XASObject
_logger = logging.getLogger(__name__)
DEFAULT_SPECTRA_PATH = '/data/NXdata/data'
DEFAULT_CHANNEL_PATH = '/data/NXdata/Channel'
DEFAULT_CONF_PATH = '/configuration'
def read_pymca_xas_from_file(file_path):
"""
:param str file_path:
:return: `.XASObject`
"""
if file_path.endswith('.dat'):
return read_pymca_xas(
spectra_url=DataUrl(file_path=file_path,
scheme='PyMca'),
channel_url=DataUrl(file_path=file_path,
scheme='PyMca'),
)
elif h5py.is_hdf5(file_path):
return read_pymca_xas(
spectra_url=DataUrl(file_path=file_path,
scheme='silx',
data_path=DEFAULT_SPECTRA_PATH),
channel_url=DataUrl(file_path=file_path,
scheme='silx',
data_path=DEFAULT_CHANNEL_PATH),
config_url=DataUrl(file_path=file_path,
scheme='silx',
data_path='configuration'))
else:
raise ValueError('file type not managed, unable to load')
def read_pymca_xas(spectra_url, channel_url, config_url=None):
"""
Read the given spectra url and the config url if any
......
......@@ -37,6 +37,7 @@ import xas.core.process.normalization
import xas.core.process.k_weight
import xas.core.process.exafs
import xas.core.process.ft
from xas.core.types import XASObject
import xas.io
import os
......@@ -46,7 +47,7 @@ class TestWorkflowFunctions(unittest.TestCase):
def setUp(self):
unittest.TestCase.setUp(self)
read_task = Node(callback=xas.io.read_pymca_xas)
read_task = Node(callback=xas.io.read_pymca_xas_from_file)
normalization_task = Node(
callback=xas.core.process.normalization.pymca_normalization)
k_weight_task = Node(callback=xas.core.process.k_weight.pymca_k_weight)
......@@ -74,8 +75,9 @@ class TestWorkflowFunctions(unittest.TestCase):
def testProcess(self):
out = exec_workflow(scheme=self.scheme, input_=self.data_file)
assert isinstance(out, dict)
assert 'FT' in out['spectra']
assert 'FTRadius' in out['spectra']['FT']
xas_obj_out = XASObject.from_dict(out)
assert 'FT' in xas_obj_out.spectra[0]
assert 'FTRadius' in xas_obj_out.spectra[0].ft
class TestWorkflowCallableClasses(unittest.TestCase):
......@@ -83,7 +85,7 @@ class TestWorkflowCallableClasses(unittest.TestCase):
def setUp(self):
unittest.TestCase.setUp(self)
read_task = Node(callback=xas.io.read_pymca_xas)
read_task = Node(callback=xas.io.read_pymca_xas_from_file)
normalization_task = Node(
callback=xas.core.process.normalization.PyMca_normalization)
k_weight_task = Node(callback=xas.core.process.k_weight.PyMca_k_weight)
......@@ -111,10 +113,11 @@ class TestWorkflowCallableClasses(unittest.TestCase):
def testProcess(self):
out = exec_workflow(scheme=self.scheme, input_=self.data_file)
assert isinstance(out, dict)
assert 'FT' in out['spectra']
assert 'FTRadius' in out['spectra']['FT']
assert 'FT' in out['configuration']
assert 'Normalization' in out['configuration']
xas_obj_out = XASObject.from_dict(out)
assert 'FT' in xas_obj_out.spectra[0]
assert 'FTRadius' in xas_obj_out.spectra[0].ft
assert 'FT' in xas_obj_out.configuration
assert 'Normalization' in xas_obj_out.configuration
def suite():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment