Commit f4ec6cfe authored by Payno's avatar Payno

first step on treatment

parent 190b7397
ID06 workflow
-------------
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Bootstrap helps you to test scripts without installing them
by patching your PYTHONPATH on the fly
example: ./bootstrap.py ipython
"""
__authors__ = ["Frédéric-Emmanuel Picca", "Jérôme Kieffer"]
__contact__ = "jerome.kieffer@esrf.eu"
__license__ = "MIT"
__date__ = "02/03/2018"
import sys
import os
import distutils.util
import subprocess
import logging
logging.basicConfig()
logger = logging.getLogger("bootstrap")
def is_debug_python():
"""Returns true if the Python interpreter is in debug mode."""
try:
import sysconfig
except ImportError: # pragma nocover
# Python < 2.7
import distutils.sysconfig as sysconfig
if sysconfig.get_config_var("Py_DEBUG"):
return True
return hasattr(sys, "gettotalrefcount")
def _distutils_dir_name(dname="lib"):
"""
Returns the name of a distutils build directory
"""
platform = distutils.util.get_platform()
architecture = "%s.%s-%i.%i" % (dname, platform,
sys.version_info[0], sys.version_info[1])
if is_debug_python():
architecture += "-pydebug"
return architecture
def _distutils_scripts_name():
"""Return the name of the distrutils scripts sirectory"""
f = "scripts-{version[0]}.{version[1]}"
return f.format(version=sys.version_info)
def _get_available_scripts(path):
res = []
try:
res = " ".join([s.rstrip('.py') for s in os.listdir(path)])
except OSError:
res = ["no script available, did you ran "
"'python setup.py build' before bootstrapping ?"]
return res
if sys.version_info[0] >= 3: # Python3
def execfile(fullpath, globals=None, locals=None):
"Python3 implementation for execfile"
with open(fullpath) as f:
try:
data = f.read()
except UnicodeDecodeError:
raise SyntaxError("Not a Python script")
code = compile(data, fullpath, 'exec')
exec(code, globals, locals)
def run_file(filename, argv):
"""
Execute a script trying first to use execfile, then a subprocess
:param str filename: Script to execute
:param list[str] argv: Arguments passed to the filename
"""
full_args = [filename]
full_args.extend(argv)
try:
logger.info("Execute target using exec")
# execfile is considered as a local call.
# Providing globals() as locals will force to feed the file into
# globals() (for examples imports).
# Without this any function call from the executed file loses imports
try:
old_argv = sys.argv
sys.argv = full_args
logger.info("Patch the sys.argv: %s", sys.argv)
logger.info("Executing %s.main()", filename)
print("########### EXECFILE ###########")
module_globals = globals().copy()
module_globals['__file__'] = filename
execfile(filename, module_globals, module_globals)
finally:
sys.argv = old_argv
except SyntaxError as error:
logger.error(error)
logger.info("Execute target using subprocess")
env = os.environ.copy()
env.update({"PYTHONPATH": LIBPATH + os.pathsep + os.environ.get("PYTHONPATH", ""),
"PATH": os.environ.get("PATH", "")})
print("########### SUBPROCESS ###########")
run = subprocess.Popen(full_args, shell=False, env=env)
run.wait()
def run_entry_point(entry_point, argv):
"""
Execute an entry_point using the current python context
(http://setuptools.readthedocs.io/en/latest/setuptools.html#automatic-script-creation)
:param str entry_point: A string identifying a function from a module
(NAME = PACKAGE.MODULE:FUNCTION [EXTRA])
"""
import importlib
elements = entry_point.split("=")
target_name = elements[0].strip()
elements = elements[1].split(":")
module_name = elements[0].strip()
# Take care of entry_point optional "extra" requirements declaration
function_name = elements[1].split()[0].strip()
logger.info("Execute target %s (function %s from module %s) using importlib", target_name, function_name, module_name)
full_args = [target_name]
full_args.extend(argv)
try:
old_argv = sys.argv
sys.argv = full_args
print("########### IMPORTLIB ###########")
module = importlib.import_module(module_name)
if hasattr(module, function_name):
func = getattr(module, function_name)
func()
else:
logger.info("Function %s not found", function_name)
finally:
sys.argv = old_argv
def find_executable(target):
"""Find a filename from a script name.
- Check the script name as file path,
- Then checks if the name is a target of the setup.py
- Then search the script from the PATH environment variable.
:param str target: Name of the script
:returns: Returns a tuple: kind, name.
"""
if os.path.isfile(target):
return ("path", os.path.abspath(target))
# search the file from setup.py
import setup
config = setup.get_project_configuration(dry_run=True)
# scripts from project configuration
if "scripts" in config:
for script_name in config["scripts"]:
if os.path.basename(script) == target:
return ("path", os.path.abspath(script_name))
# entry-points from project configuration
if "entry_points" in config:
for kind in config["entry_points"]:
for entry_point in config["entry_points"][kind]:
elements = entry_point.split("=")
name = elements[0].strip()
if name == target:
return ("entry_point", entry_point)
# search the file from env PATH
for dirname in os.environ.get("PATH", "").split(os.pathsep):
path = os.path.join(dirname, target)
if os.path.isfile(path):
return ("path", path)
return None, None
home = os.path.dirname(os.path.abspath(__file__))
LIBPATH = os.path.join(home, 'build', _distutils_dir_name('lib'))
cwd = os.getcwd()
os.chdir(home)
build = subprocess.Popen([sys.executable, "setup.py", "build"],
shell=False, cwd=os.path.dirname(os.path.abspath(__file__)))
build_rc = build.wait()
if not os.path.exists(LIBPATH):
logger.warning("`lib` directory does not exist, trying common Python3 lib")
LIBPATH = os.path.join(os.path.split(LIBPATH)[0], "lib")
os.chdir(cwd)
if build_rc == 0:
logger.info("Build process ended.")
else:
logger.error("Build process ended with rc=%s", build_rc)
sys.exit(-1)
if __name__ == "__main__":
if len(sys.argv) < 2:
logger.warning("usage: ./bootstrap.py <script>\n")
script = None
else:
script = sys.argv[1]
if script:
logger.info("Executing %s from source checkout", script)
else:
logging.info("Running iPython by default")
sys.path.insert(0, LIBPATH)
logger.info("Patched sys.path with %s", LIBPATH)
if script:
argv = sys.argv[2:]
kind, target = find_executable(script)
if kind == "path":
run_file(target, argv)
elif kind == "entry_point":
run_entry_point(target, argv)
else:
logger.error("Script %s not found", script)
else:
logger.info("Patch the sys.argv: %s", sys.argv)
sys.path.insert(2, "")
try:
from IPython import embed
except Exception as err:
logger.error("Unable to execute iPython, using normal Python")
logger.error(err)
import code
code.interact()
else:
embed()
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2015-2017 European Synchrotron Radiation Facility
#
# 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.
#
# ###########################################################################*/
from __future__ import absolute_import, print_function, division
__authors__ = ["Jérôme Kieffer"]
__license__ = "MIT"
__date__ = "23/05/2016"
import os as _os
import logging as _logging
# Attach a do nothing logging handler for silx
_logging.getLogger(__name__).addHandler(_logging.NullHandler())
project = _os.path.basename(_os.path.dirname(_os.path.abspath(__file__)))
try:
from ._version import __date__ as date # noqa
from ._version import version, version_info, hexversion, strictversion # noqa
except ImportError:
raise RuntimeError("Do NOT use %s from its sources: build it and use the built version" % project)
#/*##########################################################################
# Copyright (C) 2016-2017 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__ = ["H. Payno"]
__license__ = "MIT"
__date__ = "07/08/2018"
import numpy
import os
from silx.io.url import DataUrl
from silx.io.utils import get_data
import fabio
class Data(object):
def __init__(self, data, thetaAngle, azimutAngle):
self.data = data
self.theta = thetaAngle
self.azimut = azimutAngle
def applyShift(self, shift):
"""shift should be given as x, y"""
assert type(shift) is tuple
self.data = numpy.roll(self.data, shift=shift[0], axis=1)
self.data = numpy.roll(self.data, shift=shift[1], axis=0)
class DataFrmUrl(Data):
def __init__(self, url, thetaAngle, azimutAngle, dark):
assert isinstance(url, DataUrl)
data = get_data(url)
# data = data - data.min()
# data = data / data.max()
# data = data * 4.e-4 - 2.e-4
Data.__init__(self, data, thetaAngle, azimutAngle)
self._file = url
self._pointNo = None
self._scanNo = None
class Dataset(object):
def __init__(self):
# Here the acquisition
self.name = 'id06dataset'
self._darks = {}
self._data = {}
self.loadDark()
self.loadData()
def loadDark(self):
folder_dark = "/nobackup/linazimov/payno/datasets/id06/bg_ff_5s_1x1"
for file_path in os.listdir(folder_dark):
with fabio.open(os.path.join(folder_dark, file_path)) as handler:
_pointNo = int(handler.header['point_no'])
self._darks[_pointNo] = handler.data
def loadData(self):
iFile = 0
folder_data = "/nobackup/linazimov/payno/datasets/id06/strain"
theta_angles = numpy.linspace(start=-0.15, stop=0.15, num=31)
self.theta_step = theta_angles[-1] - theta_angles[-2]
for theta in theta_angles:
self._data[theta] = {}
for azimuth in numpy.linspace(start=-0.3, stop=0.3, num=31):
file_name = "strain_" + str(iFile).zfill(4) + '.edf'
file_path = os.path.join(folder_data, file_name)
assert os.path.isfile(file_path)
dataFrmUrl = DataFrmUrl(DataUrl(file_path=file_path, scheme='fabio'),
thetaAngle=theta,
azimutAngle=azimuth,
dark=self._darks[0])
try:
with fabio.open(file_path) as handler:
dataFrmUrl._pointNo = int(handler.header['point_no'])
dataFrmUrl._scanNo = int(handler.header['scan_no'])
print('point No = %s, scan No = %s' % (dataFrmUrl._pointNo, dataFrmUrl._scanNo))
except:
pass
self._data[theta][azimuth] = dataFrmUrl
iFile = iFile + 1
def applyShift(self, shiftPerStep):
assert type(shiftPerStep) is tuple
for theta in self._data:
nSteps = theta / self.theta_step
shift = nSteps * shiftPerStep
print(shift)
for azimut in self._data[theta]:
self._data[theta][azimut].applyShift(shift)
This diff is collapsed.
This diff is collapsed.
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2017 European Synchrotron Radiation Facility
#
# 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.
#
# ###########################################################################*/
__authors__ = ["H. Payno"]
__license__ = "MIT"
__date__ = "04/01/2018"
from numpy.distutils.misc_util import Configuration
def configuration(parent_package='', top_path=None):
config = Configuration('id06workflow', parent_package, top_path)
config.add_subpackage('core')
return config
if __name__ == "__main__":
from numpy.distutils.core import setup
setup(configuration=configuration)
from silx.gui import qt
from Orange.widgets.widget import OWWidget
class CalibrationOW(OWWidget):
"""
"""
name = "Calibration"
icon = "icons/calibration.svg"
want_main_area = False
def __init__(self):
super().__init__()
label = qt.QLabel("Calibration")
self.controlArea.layout().addWidget(label)
from silx.gui import qt
from Orange.widgets.widget import OWWidget
import numpy
import os
class DataSelectionOW(OWWidget):
......@@ -63,3 +65,25 @@ class FolderWidget(qt.QWidget):
dialog.close()
return
self._qteFolderSelected.setText(dialog.selectedFiles()[0])
class Data(object):
def __init__(self, data, alphaAngle, betaAngle):
self.data = data
self.alpha = alphaAngle
self.beta = betaAngle
class Dataset(object):
def __init__(self):
# Here the acquisition
self._data = {}
self.name = 'id06dataset'
iFile = 0
folder_name = "/nobackup/linazimov/payno/datasets/id06workflow"
for theta in numpy.linspace(start=-0.15, stop=0.15, num=31):
for azimuth in numpy.linspace(start=-0.3, stop=0.3, num=31):
file_name = "strain_" + str(iFile).zfill(4) + '.edf'
self._data[(theta, azimuth)] = os.path.join(folder_name,
file_name)
iFile = iFile + 1
This diff is collapsed.
This diff is collapsed.
from silx.gui.plot import Plot2D
from id06workflow.core import data
from silx.gui import qt
import sys
import numpy
def _testFullTreatment():
d = data.Dataset()
_fullsum = None
_point_20_sum = None
_scan_100_sum = None
for theta in d._data:
_sum = None
for azimut in d._data[theta]:
file_data = d._data[theta][azimut].data
if _sum is None:
_sum = file_data.astype(numpy.float64)
else:
assert _sum.shape == file_data.shape
_sum = numpy.add(_sum, file_data)
if d._data[theta][azimut]._pointNo is 0:
if _point_20_sum is None:
_point_20_sum = file_data.astype(numpy.float64)
else:
_point_20_sum = numpy.add(_point_20_sum, file_data)
if d._data[theta][azimut]._scanNo is 100:
if _scan_100_sum is None:
_scan_100_sum = file_data.astype(numpy.float64)
else:
_scan_100_sum = numpy.add(_scan_100_sum, file_data)
if _fullsum is None:
_fullsum = _sum
else:
_fullsum = numpy.add(_fullsum, _sum)
plotSum = Plot2D()
plotSum.addImage(_sum)
plotSum.show()
sys.stdout.write('.')
print('\n')
plotFullSum = Plot2D()
plotFullSum.setWindowTitle('full sum')
plotFullSum.addImage(_fullsum)
plotFullSum.show()
plotPoint20 = Plot2D()
plotPoint20.setWindowTitle('_point_20_sum')
plotPoint20.addImage(_point_20_sum)
plotPoint20.show()
plotScan = Plot2D()
plotScan.setWindowTitle('_scan_100_sum')
plotScan.addImage(_scan_100_sum)
plotScan.show()
if __name__ == '__main__':
app = qt.QApplication([])
_testFullTreatment()
app.exec_()
#!/usr/bin/env python
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2015-2017 European Synchrotron Radiation Facility
#
# 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.
#
# ###########################################################################*/
"""Unique place where the version number is defined.
provides:
* version = "1.2.3" or "1.2.3-beta4"
* version_info = named tuple (1,2,3,"beta",4)
* hexversion: 0x010203B4
* strictversion = "1.2.3b4
* debianversion = "1.2.3~beta4"
* calc_hexversion: the function to transform a version_tuple into an integer
This is called hexversion since it only really looks meaningful when viewed as the
result of passing it to the built-in hex() function.
The version_info value may be used for a more human-friendly encoding of the same information.
The hexversion is a 32-bit number with the following layout:
Bits (big endian order) Meaning
1-8 PY_MAJOR_VERSION (the 2 in 2.1.0a3)
9-16 PY_MINOR_VERSION (the 1 in 2.1.0a3)
17-24 PY_MICRO_VERSION (the 0 in 2.1.0a3)
25-28 PY_RELEASE_LEVEL (0xA for alpha, 0xB for beta, 0xC for release candidate and 0xF for final)
29-32 PY_RELEASE_SERIAL (the 3 in 2.1.0a3, zero for final releases)
Thus 2.1.0a3 is hexversion 0x020100a3.
"""
from __future__ import absolute_import, print_function, division
__authors__ = ["Jérôme Kieffer"]
__license__ = "MIT"
__copyright__ = "European Synchrotron Radiation Facility, Grenoble, France"
__date__ = "28/02/2018"
__status__ = "production"
__docformat__ = 'restructuredtext'
__all__ = ["date", "version_info", "strictversion", "hexversion", "debianversion",
"calc_hexversion"]
RELEASE_LEVEL_VALUE = {"dev": 0,
"alpha": 10,
"beta": 11,
"gamma": 12,
"rc": 13,
"final": 15}
MAJOR = 0
MINOR = 9
MICRO = 0
RELEV = "dev" # <16
SERIAL = 0 # <16
date = __date__
from collections import namedtuple
_version_info = namedtuple("version_info", ["major", "minor", "micro", "releaselevel", "serial"])
version_info = _version_info(MAJOR, MINOR, MICRO, RELEV, SERIAL)
strictversion = version = debianversion = "%d.%d.%d" % version_info[:3]
if version_info.releaselevel != "final":
version += "-%s%s" % version_info[-2:]
debianversion += "~adev%i" % version_info[-1] if RELEV == "dev" else "~%s%i" % version_info[-2:]
prerel = "a" if RELEASE_LEVEL_VALUE.get(version_info[3], 0) < 10 else "b"
if prerel not in "ab":
prerel = "a"
strictversion += prerel + str(version_info[-1])
def calc_hexversion(major=0, minor=0, micro=0, releaselevel="dev", serial=0):
"""Calculate the hexadecimal version number from the tuple version_info:
:param major: integer
:param minor: integer
:param micro: integer
:param relev: integer or string
:param serial: integer
:return: integer always increasing with revision numbers
"""
try:
releaselevel = int(releaselevel)
except ValueError:
releaselevel = RELEASE_LEVEL_VALUE.get(releaselevel, 0)
hex_version = int(serial)
hex_version |= releaselevel * 1 << 4
hex_version |= int(micro) * 1 << 8