Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ixstools
xrstools
Commits
a01cf976
Commit
a01cf976
authored
Dec 01, 2020
by
myron
Browse files
Merge branch 'alessandro_develop' into cdi
parents
522702f5
00233a02
Changes
44
Expand all
Hide whitespace changes
Inline
Side-by-side
.gitlab-ci.yml
View file @
a01cf976
...
@@ -6,7 +6,8 @@ variables:
...
@@ -6,7 +6,8 @@ variables:
PROJECT_NAME
:
"
xrstools"
PROJECT_NAME
:
"
xrstools"
LOCAL_REPO
:
"
/dev/shm/gitlab_ci/xrstools"
LOCAL_REPO
:
"
/dev/shm/gitlab_ci/xrstools"
VENV_DIR
:
/dev/shm/gitlab_ci/xrstools/venv
VENV_DIR
:
/dev/shm/gitlab_ci/xrstools/venv
USE_CLEAN_VENV
:
"
true"
USE_CLEAN_VENV
:
"
false"
CLEAN_RESULTS
:
"
true"
test
:
test
:
stage
:
test
stage
:
test
...
@@ -32,11 +33,13 @@ test:
...
@@ -32,11 +33,13 @@ test:
script
:
script
:
-
pwd
-
pwd
-
cd nonregressions/xes
-
cd nonregressions/xes
-
-
/usr/bin/xvfb-run --server-args="-screen 0 1024x768x24" -a python xes_analysis.py > output
-
/usr/bin/xvfb-run --server-args="-screen 0 1024x768x24" -a python xes_analysis.py >
|
output
-
cd ../xrs_raman
-
cd ../xrs_raman
-
/usr/bin/xvfb-run --server-args="-screen 0 1024x768x24" -a python non_reg_testing_XRS.py > output1
-
/usr/bin/xvfb-run --server-args="-screen 0 1024x768x24" -a python non_reg_testing_XRS.py >| output1
-
/usr/bin/xvfb-run --server-args="-screen 0 1024x768x24" -a python non_reg_testing_XRS_small.py > output2
-
/usr/bin/xvfb-run --server-args="-screen 0 1024x768x24" -a python non_reg_testing_XRS_small.py >| output2
-
/usr/bin/xvfb-run --server-args="-screen 0 1024x768x24" -a python non_reg_testing_XRS_raman_extraction.py > output3
-
/usr/bin/xvfb-run --server-args="-screen 0 1024x768x24" -a python non_reg_testing_XRS_raman_extraction.py >| output3
-
cd ../volumes/test1
-
/usr/bin/xvfb-run --server-args="-screen 0 1024x768x24" -a python test.py >| output
-
deactivate
-
deactivate
after_script
:
after_script
:
-
pwd
-
pwd
...
...
XRStools/XRS_swissknife.py
View file @
a01cf976
This diff is collapsed.
Click to expand it.
XRStools/reponse_percussionelle.py
View file @
a01cf976
...
@@ -1433,7 +1433,9 @@ def DOROIS(filename = "../nonregressions/demo_imaging.hdf5" , groupname = "ROI_B
...
@@ -1433,7 +1433,9 @@ def DOROIS(filename = "../nonregressions/demo_imaging.hdf5" , groupname = "ROI_B
for
i
in
range
(
len
(
newspots
)):
for
i
in
range
(
len
(
newspots
)):
newspots
[
i
]
=
newspots
[
i
]
/
newspots
[
i
].
sum
()
spot_sum
=
newspots
[
i
].
sum
()
if
spot_sum
:
newspots
[
i
]
=
newspots
[
i
]
/
spot_sum
h5
[
"matrix"
]
=
newspots
h5
[
"matrix"
]
=
newspots
...
...
XRStools/roifinder_and_gui.py
View file @
a01cf976
This diff is collapsed.
Click to expand it.
XRStools/xrs_calctools.py
View file @
a01cf976
...
@@ -567,6 +567,24 @@ class xyzAtom:
...
@@ -567,6 +567,24 @@ class xyzAtom:
norm
=
np
.
trapz
(
self
.
spectrum
[
inds
,
1
],
self
.
spectrum
[
inds
,
0
])
norm
=
np
.
trapz
(
self
.
spectrum
[
inds
,
1
],
self
.
spectrum
[
inds
,
0
])
self
.
spectrum
[:,
1
]
/=
norm
self
.
spectrum
[:,
1
]
/=
norm
def
getDist
(
self
,
atom
):
return
np
.
linalg
.
norm
(
self
.
coordinates
-
atom
.
coordinates
)
def
getDistPBCarb
(
self
,
atom
,
lattice
,
lattice_inv
):
return
getDistancePBC_arb
(
self
,
atom
,
lattice
,
lattice_inv
)
def
getAnglePBCarb
(
self
,
atom2
,
atom3
,
lattice
,
lattice_inv
,
degrees
=
True
):
""" **get_angle**
Return angle between the three given atoms (as seen from atom2).
"""
vec1
=
getDistVectorPBC_arb
(
atom2
,
self
,
lattice
,
lattice_inv
)
vec2
=
getDistVectorPBC_arb
(
atom3
,
self
,
lattice
,
lattice_inv
)
dotp
=
np
.
dot
(
vec1
/
np
.
linalg
.
norm
(
vec1
),
vec2
/
np
.
linalg
.
norm
(
vec2
))
if
degrees
:
return
np
.
degrees
(
np
.
arccos
(
np
.
clip
(
dotp
,
-
1.0
,
1.0
)
)
)
else
:
return
np
.
arccos
(
np
.
clip
(
dotp
,
-
1.0
,
1.0
)
)
class
xyzMolecule
:
class
xyzMolecule
:
""" **xyzMolecule**
""" **xyzMolecule**
...
...
XRStools/xrs_read.py
View file @
a01cf976
...
@@ -434,7 +434,7 @@ class Hydra:
...
@@ -434,7 +434,7 @@ class Hydra:
print
(
"Integrating pixelwise "
+
scan
)
print
(
"Integrating pixelwise "
+
scan
)
self
.
scans
[
scan
].
apply_rois_pw
(
self
.
roi_obj
)
self
.
scans
[
scan
].
apply_rois_pw
(
self
.
roi_obj
)
def
SumDirect
(
self
,
scan_numbers
):
def
SumDirect
(
self
,
scan_numbers
,
index
=
None
):
""" **SumDirect**
""" **SumDirect**
Creates a summed 2D image of a given scan or list of scans.
Creates a summed 2D image of a given scan or list of scans.
...
@@ -463,9 +463,18 @@ class Hydra:
...
@@ -463,9 +463,18 @@ class Hydra:
en_column
=
en_column
,
moni_column
=
self
.
moni_column
)
en_column
=
en_column
,
moni_column
=
self
.
moni_column
)
if
im_sum
is
None
:
if
im_sum
is
None
:
im_sum
=
np
.
zeros
(
scan
.
edfmats
[
0
].
shape
,
"f"
)
im_sum1
=
np
.
zeros
(
scan
.
edfmats
[
0
].
shape
,
"f"
)
im_sum
[:]
+=
scan
.
edfmats
.
sum
(
axis
=
0
)
im_sum2
=
np
.
zeros
(
scan
.
edfmats
[
0
].
shape
,
"f"
)
return
im_sum
if
not
index
:
im_sum1
[:]
+=
scan
.
edfmats
.
sum
(
axis
=
0
)
else
:
im_sum1
[:]
+=
scan
.
edfmats
[
0
:
index
,:,:].
sum
(
axis
=
0
)
im_sum2
[:]
+=
scan
.
edfmats
[
index
:,:,:].
sum
(
axis
=
0
)
if
not
index
:
return
im_sum1
else
:
return
im_sum2
-
im_sum1
def
get_eloss_new
(
self
,
method
=
'sum'
):
def
get_eloss_new
(
self
,
method
=
'sum'
):
""" **get_eloss_new**
""" **get_eloss_new**
...
@@ -528,8 +537,8 @@ class Hydra:
...
@@ -528,8 +537,8 @@ class Hydra:
# master eloss scale in eV is the one of central pixel in first ROI
# master eloss scale in eV is the one of central pixel in first ROI
self
.
signals
=
np
.
zeros
((
len
(
self
.
energy
),
len
(
self
.
cenom_dict
)))
self
.
signals
=
np
.
zeros
((
len
(
self
.
energy
),
len
(
self
.
cenom_dict
)))
self
.
errors
=
np
.
zeros
((
len
(
self
.
energy
),
len
(
self
.
cenom_dict
)))
self
.
errors
=
np
.
zeros
((
len
(
self
.
energy
),
len
(
self
.
cenom_dict
)))
master_eloss
=
(
self
.
energy
-
np
.
median
([
np
.
median
(
self
.
cenom_dict
[
key
])
for
key
in
self
.
cenom_dict
])
)
*
1.0e3
master_eloss
=
(
self
.
energy
-
np
.
median
(
self
.
cenom_dict
[
first_key
][
self
.
cenom_dict
[
first_key
]
>
0.0
])
)
*
1.0e3
self
.
E0
=
np
.
mean
(
self
.
cenom_dict
[
first_key
][
self
.
cenom_dict
[
first_key
]
>
0.0
])
self
.
E0
=
np
.
me
di
an
(
self
.
cenom_dict
[
first_key
][
self
.
cenom_dict
[
first_key
]
>
0.0
])
for
key
,
ii
in
zip
(
sorted
(
self
.
cenom_dict
),
range
(
len
(
self
.
cenom_dict
))):
for
key
,
ii
in
zip
(
sorted
(
self
.
cenom_dict
),
range
(
len
(
self
.
cenom_dict
))):
print
(
'Pixel-by-pixel compensation for '
+
key
+
'.'
)
print
(
'Pixel-by-pixel compensation for '
+
key
+
'.'
)
signal
=
np
.
zeros
(
len
(
master_eloss
))
signal
=
np
.
zeros
(
len
(
master_eloss
))
...
@@ -539,17 +548,18 @@ class Hydra:
...
@@ -539,17 +548,18 @@ class Hydra:
x
=
(
self
.
energy
-
self
.
cenom_dict
[
key
][
dim1
,
dim2
]
)
*
1.0e3
x
=
(
self
.
energy
-
self
.
cenom_dict
[
key
][
dim1
,
dim2
]
)
*
1.0e3
# signals
# signals
y
=
self
.
raw_signals
[
key
][:,
dim1
,
dim2
]
y
=
self
.
raw_signals
[
key
][:,
dim1
,
dim2
]
#print "Y AMAX", signal.max()
if
np
.
any
(
y
)
>
0.0
:
#rbfi = Rbf( x, y, function='linear' )
#print "Y AMAX", signal.max()
rbfi
=
interp1d
(
x
,
y
,
bounds_error
=
False
,
fill_value
=
0.0
)
#rbfi = Rbf( x, y, function='linear' )
#print "rbf AMAX", Rbf( x, y, function='linear' )
rbfi
=
interp1d
(
x
,
y
,
bounds_error
=
False
,
fill_value
=
0.0
)
#signal += rbfi( master_eloss )
#print "rbf AMAX", Rbf( x, y, function='linear' )
signal
+=
rbfi
(
master_eloss
)
#signal += rbfi( master_eloss )
#print "SIGNAL AMAX", signal.max()
signal
+=
rbfi
(
master_eloss
)
# errors
#print "SIGNAL AMAX", signal.max()
y
=
self
.
raw_errors
[
key
][:,
dim1
,
dim2
]
# errors
rbfi
=
Rbf
(
x
,
y
,
function
=
'linear'
)
y
=
self
.
raw_errors
[
key
][:,
dim1
,
dim2
]
error
+=
rbfi
(
master_eloss
)
**
2
rbfi
=
Rbf
(
x
,
y
,
function
=
'linear'
)
error
+=
rbfi
(
master_eloss
)
**
2
self
.
signals
[:,
ii
]
=
signal
self
.
signals
[:,
ii
]
=
signal
self
.
errors
[:,
ii
]
=
np
.
sqrt
(
error
)
self
.
errors
[:,
ii
]
=
np
.
sqrt
(
error
)
self
.
eloss
=
master_eloss
self
.
eloss
=
master_eloss
...
@@ -2154,7 +2164,7 @@ class read_id20:
...
@@ -2154,7 +2164,7 @@ class read_id20:
self
.
scans
=
{}
# was a dictionary before
self
.
scans
=
{}
# was a dictionary before
if
absfilename
is
not
None
:
if
absfilename
is
not
None
:
if
not
os
.
path
.
isfile
(
absfilename
):
if
not
os
.
path
.
isfile
(
absfilename
):
raise
Exception
(
'IOError! No such file, please check filename.'
)
raise
Exception
(
'IOError! No such file
%s
, please check filename.'
%
absfilename
)
self
.
path
=
os
.
path
.
split
(
absfilename
)[
0
]
+
'/'
self
.
path
=
os
.
path
.
split
(
absfilename
)[
0
]
+
'/'
self
.
filename
=
os
.
path
.
split
(
absfilename
)[
1
]
self
.
filename
=
os
.
path
.
split
(
absfilename
)[
1
]
if
not
edfName
:
if
not
edfName
:
...
...
XRStools/xrs_rois.py
View file @
a01cf976
...
@@ -44,6 +44,7 @@ import copy
...
@@ -44,6 +44,7 @@ import copy
import
h5py
import
h5py
import
os
import
os
import
matplotlib.pyplot
as
plt
import
matplotlib.pyplot
as
plt
from
collections
import
Iterable
# commented the *import because otherwise sphinx documents all the symbol of other packages
# commented the *import because otherwise sphinx documents all the symbol of other packages
# from xrs_utilities import *
# from xrs_utilities import *
...
@@ -168,32 +169,82 @@ class roi_object:
...
@@ -168,32 +169,82 @@ class roi_object:
"""
"""
Container class to hold all relevant information about given ROIs.
Container class to hold all relevant information about given ROIs.
"""
"""
def
__init__
(
self
):
def
__init__
(
self
):
self
.
roi_matrix
=
np
.
array
([])
# single matrix of zeros, ones, twos, ... , n's (where n is the number of ROIs defined)
self
.
roi_matrix
=
np
.
array
([])
# single matrix of zeros, ones, twos, ... ,
self
.
red_rois
=
{}
# dictionary, one entry for each ROI, each ROI has an origin and a rectangular box of ones and zeros defining the ROI
# n's (where n is the number of ROIs defined)
self
.
indices
=
[]
# list of list of tuples (one list of tuples for each ROI)
self
.
red_rois
=
{}
# dictionary, one entry for each ROI, each ROI
self
.
number_of_rois
=
0
# number of ROIs defined
# has an origin and a rectangular box of ones
self
.
kind
=
[]
# keyword (e.g. 'zoom', 'line', 'auto', etc.), certain features (esp. in imaging) are only available for certain kinds of ROIs
# and zeros defining the ROI
self
.
indices
=
[]
# list of list of tuples (one list of tuples
# for each ROI)
self
.
number_of_rois
=
0
# number of ROIs defined
self
.
kind
=
[]
# keyword (e.g. 'zoom', 'line', 'auto', etc.),
# certain features (esp. in imaging) are only
# available for certain kinds of ROIs
self
.
x_indices
=
[]
# list of numpy arrays of x-indices (for each ROI)
self
.
x_indices
=
[]
# list of numpy arrays of x-indices (for each ROI)
self
.
y_indices
=
[]
# list of numpy arrays of y-indices (for each ROI)
self
.
y_indices
=
[]
# list of numpy arrays of y-indices (for each ROI)
self
.
masks
=
[]
# 3D numpy array with slices of zeros and ones (same size as detector image) for each roi
self
.
masks
=
[]
# 3D numpy array with slices of zeros and ones (same
# size as detector image) for each roi
self
.
input_image
=
[]
# 2D imput image that was used to define the ROIs
self
.
input_image
=
[]
# 2D imput image that was used to define the ROIs
def
__add__
(
self
,
roi_obj
):
""" **__add__**
Allows appending two ROI objects by using the + operator.
"""
assert
(
type
(
roi_obj
)
==
type
(
self
)
)
# create a new instance
new_obj
=
roi_object
()
# copy the ROIs
new_obj
.
red_rois
=
copy
.
deepcopy
(
self
.
red_rois
)
# append the other ROIs
self_len
=
len
(
new_obj
.
red_rois
)
for
ii
,
key
in
enumerate
(
sorted
(
roi_obj
.
red_rois
)
):
new_key
=
'ROI%02d'
%
(
ii
+
self_len
)
if
not
new_key
in
list
(
new_obj
.
red_rois
.
keys
()
):
new_obj
.
red_rois
[
new_key
]
=
roi_obj
.
red_rois
[
key
]
new_obj
.
red_rois
[
new_key
][
1
][
new_obj
.
red_rois
[
new_key
][
1
]
>
0
]
+=
self_len
else
:
'something fishy happened, skipping %s.'
%
(
key
)
return
self
# add the input images
new_obj
.
input_image
=
self
.
input_image
+
roi_obj
.
input_image
# convert summed ROIs to other ROI formats
new_obj
.
roi_matrix
=
convert_redmatrix_to_matrix
(
new_obj
.
red_rois
,
np
.
zeros_like
(
new_obj
.
input_image
),
offsetX
=
0
,
offsetY
=
0
)
new_obj
.
masks
=
convert_roi_matrix_to_masks
(
new_obj
.
roi_matrix
)
new_obj
.
indices
=
convert_matrix_rois_to_inds
(
new_obj
.
roi_matrix
)
new_obj
.
number_of_rois
=
int
(
np
.
amax
(
new_obj
.
roi_matrix
)
)
new_obj
.
x_indices
=
convert_inds_to_xinds
(
new_obj
.
indices
)
new_obj
.
y_indices
=
convert_inds_to_yinds
(
new_obj
.
indices
)
def
load_rois_fromMasksDict
(
self
,
masksDict
,
newshape
=
None
,
kind
=
"zoom"
):
return
new_obj
def
load_rois_fromMasksDict
(
self
,
masksDict
,
newshape
=
None
,
kind
=
"zoom"
):
""" **load_rois_fromMasksDict**
"""
self
.
kind
=
kind
self
.
kind
=
kind
self
.
red_rois
=
masksDict
self
.
red_rois
=
masksDict
if
newshape
is
not
None
:
if
newshape
is
not
None
:
self
.
roi_matrix
=
np
.
zeros
(
newshape
)
self
.
roi_matrix
=
np
.
zeros
(
newshape
)
self
.
roi_matrix
=
convert_redmatrix_to_matrix
(
masksDict
,
self
.
roi_matrix
,
offsetX
=
0
,
offsetY
=
0
)
self
.
roi_matrix
=
convert_redmatrix_to_matrix
(
masksDict
,
self
.
roi_matrix
,
offsetX
=
0
,
offsetY
=
0
)
self
.
masks
=
convert_roi_matrix_to_masks
(
self
.
roi_matrix
)
self
.
masks
=
convert_roi_matrix_to_masks
(
self
.
roi_matrix
)
self
.
indices
=
convert_matrix_rois_to_inds
(
self
.
roi_matrix
)
self
.
indices
=
convert_matrix_rois_to_inds
(
self
.
roi_matrix
)
self
.
number_of_rois
=
int
(
np
.
amax
(
self
.
roi_matrix
))
self
.
number_of_rois
=
int
(
np
.
amax
(
self
.
roi_matrix
))
self
.
x_indices
=
convert_inds_to_xinds
(
self
.
indices
)
self
.
x_indices
=
convert_inds_to_xinds
(
self
.
indices
)
self
.
y_indices
=
convert_inds_to_yinds
(
self
.
indices
)
self
.
y_indices
=
convert_inds_to_yinds
(
self
.
indices
)
def
writeH5
(
self
,
fname
):
def
writeH5
(
self
,
fname
):
""" **writeH5**
""" **writeH5**
Creates an HDF5 file and writes the ROIs into it.
Creates an HDF5 file and writes the ROIs into it.
Args:
Args:
...
@@ -215,6 +266,7 @@ class roi_object:
...
@@ -215,6 +266,7 @@ class roi_object:
def
loadH5
(
self
,
fname
):
def
loadH5
(
self
,
fname
):
""" **loadH5**
""" **loadH5**
Loads ROIs from an HDF5 file written by the self.writeH5() method.
Loads ROIs from an HDF5 file written by the self.writeH5() method.
Args:
Args:
...
@@ -237,42 +289,65 @@ class roi_object:
...
@@ -237,42 +289,65 @@ class roi_object:
self
.
load_rois_fromMasksDict
(
self
.
red_rois
,
newshape
=
shape
,
kind
=
"zoom"
)
self
.
load_rois_fromMasksDict
(
self
.
red_rois
,
newshape
=
shape
,
kind
=
"zoom"
)
else
:
else
:
self
.
roi_matrix
=
convert_redmatrix_to_matrix
(
self
.
red_rois
,
np
.
zeros_like
(
self
.
input_image
),
offsetX
=
0
,
offsetY
=
0
)
self
.
roi_matrix
=
convert_redmatrix_to_matrix
(
self
.
red_rois
,
np
.
zeros_like
(
self
.
input_image
),
offsetX
=
0
,
offsetY
=
0
)
self
.
indices
=
convert_matrix_rois_to_inds
(
self
.
roi_matrix
)
self
.
indices
=
convert_matrix_rois_to_inds
(
self
.
roi_matrix
)
self
.
number_of_rois
=
int
(
np
.
amax
(
self
.
roi_matrix
))
self
.
number_of_rois
=
int
(
np
.
amax
(
self
.
roi_matrix
))
self
.
x_indices
=
convert_inds_to_xinds
(
self
.
indices
)
self
.
x_indices
=
convert_inds_to_xinds
(
self
.
indices
)
self
.
y_indices
=
convert_inds_to_yinds
(
self
.
indices
)
self
.
y_indices
=
convert_inds_to_yinds
(
self
.
indices
)
self
.
masks
=
convert_roi_matrix_to_masks
(
self
.
roi_matrix
)
self
.
masks
=
convert_roi_matrix_to_masks
(
self
.
roi_matrix
)
def
load_shadok_h5
(
self
,
fname
,
group_name1
,
group_name2
=
'ROI_AS_SELECTED'
):
def
load_shadok_h5
(
self
,
fname
,
group_name1
,
group_name2
=
'ROI_AS_SELECTED'
):
""" **load_shadok_h5**
Load ROIs from a HDF5-file created by the Shadok/XRS_Swissknife.
"""
f
=
h5py
.
File
(
fname
,
"r"
)
f
=
h5py
.
File
(
fname
,
"r"
)
self
.
input_image
=
f
[
group_name1
][
group_name2
][
"rois_definition"
][
"image"
][:]
self
.
input_image
=
f
[
group_name1
][
group_name2
][
"rois_definition"
][
"image"
][:]
self
.
red_rois
=
{}
self
.
red_rois
=
{}
load_rois_fromh5
(
f
[
group_name1
][
group_name2
],
self
.
red_rois
)
load_rois_fromh5
(
f
[
group_name1
][
group_name2
],
self
.
red_rois
)
self
.
roi_matrix
=
convert_redmatrix_to_matrix
(
self
.
red_rois
,
np
.
zeros_like
(
self
.
input_image
),
offsetX
=
0
,
offsetY
=
0
)
self
.
roi_matrix
=
convert_redmatrix_to_matrix
(
self
.
red_rois
,
np
.
zeros_like
(
self
.
input_image
),
offsetX
=
0
,
offsetY
=
0
)
self
.
indices
=
convert_matrix_rois_to_inds
(
self
.
roi_matrix
)
self
.
indices
=
convert_matrix_rois_to_inds
(
self
.
roi_matrix
)
self
.
number_of_rois
=
int
(
np
.
amax
(
self
.
roi_matrix
))
self
.
number_of_rois
=
int
(
np
.
amax
(
self
.
roi_matrix
))
self
.
x_indices
=
convert_inds_to_xinds
(
self
.
indices
)
self
.
x_indices
=
convert_inds_to_xinds
(
self
.
indices
)
self
.
y_indices
=
convert_inds_to_yinds
(
self
.
indices
)
self
.
y_indices
=
convert_inds_to_yinds
(
self
.
indices
)
self
.
masks
=
convert_roi_matrix_to_masks
(
self
.
roi_matrix
)
self
.
masks
=
convert_roi_matrix_to_masks
(
self
.
roi_matrix
)
def
append
(
self
,
roi_object
):
def
append
(
self
,
roi_object
):
orig_length
=
len
(
self
.
red_rois
)
""" **append**
self
.
indices
.
extend
(
roi_object
.
indices
)
# list of list of tuples (one list of tuples for each ROI)
self
.
number_of_rois
=+
roi_object
.
number_of_rois
# number of ROIs defined
Append other ROI definitions.
self
.
x_indices
.
extend
(
roi_object
.
x_indices
)
# list of numpy arrays of x-indices (for each ROI)
self
.
y_indices
.
extend
(
roi_object
.
y_indices
)
# list of numpy arrays of y-indices (for each ROI)
Args:
#self.masks = [] # 3D numpy array with slices of zeros and ones (same size as detector image) for each roi
* roi_object (roi_obj) : Instance of the roi_object class.
#self.input_image += [] # 2D imput image that was used to define the ROIs
roi_object
.
roi_matrix
[
roi_object
.
roi_matrix
>
0
]
+=
orig_length
self
.
roi_matrix
+=
roi_object
.
roi_matrix
# single matrix of zeros, ones, twos, ... , n's (where n is the number of ROIs defined)
"""
assert
(
type
(
roi_object
)
==
type
(
self
)
)
orig_length
=
len
(
self
.
red_rois
)
for
ii
,
key
in
enumerate
(
sorted
(
roi_object
.
red_rois
)):
for
ii
,
key
in
enumerate
(
sorted
(
roi_object
.
red_rois
)):
new_key
=
'ROI%02d'
%
(
ii
+
orig_length
)
new_key
=
'ROI%02d'
%
(
ii
+
orig_length
)
self
.
red_rois
[
new_key
]
=
roi_object
.
red_rois
[
key
]
self
.
red_rois
[
new_key
]
=
roi_object
.
red_rois
[
key
]
self
.
red_rois
[
new_key
][
1
][
self
.
red_rois
[
new_key
][
1
]
>
0
]
+=
orig_length
self
.
red_rois
[
new_key
][
1
][
self
.
red_rois
[
new_key
][
1
]
>
0
]
+=
orig_length
def
get_number_of_rois
(
self
):
# convert summed ROIs to other ROI formats
self
.
roi_matrix
=
convert_redmatrix_to_matrix
(
self
.
red_rois
,
np
.
zeros_like
(
self
.
input_image
),
offsetX
=
0
,
offsetY
=
0
)
self
.
masks
=
convert_roi_matrix_to_masks
(
self
.
roi_matrix
)
self
.
indices
=
convert_matrix_rois_to_inds
(
self
.
roi_matrix
)
self
.
number_of_rois
=
int
(
np
.
amax
(
self
.
roi_matrix
)
)
self
.
x_indices
=
convert_inds_to_xinds
(
self
.
indices
)
self
.
y_indices
=
convert_inds_to_yinds
(
self
.
indices
)
def
get_number_of_rois
(
self
):
""" **get_number_of_rois**
Returns the number of currently defined ROIs.
"""
return
self
.
number_of_rois
return
self
.
number_of_rois
def
get_indices
(
self
):
def
get_indices
(
self
):
...
@@ -297,63 +372,113 @@ class roi_object:
...
@@ -297,63 +372,113 @@ class roi_object:
"""
"""
return
copy
.
deepcopy
(
self
)
return
copy
.
deepcopy
(
self
)
def
strip
_rois
(
self
):
def
strip
(
self
):
""" **strip
_rois
**
""" **strip**
Strips extra zeros
out of
ROIs.
Strips extra zeros
from border of the
ROIs.
"""
"""
pass
for
key
in
self
.
red_rois
:
num
=
int
(
""
.
join
([
c
for
c
in
key
if
c
.
isdigit
()]))
origin
=
self
.
red_rois
[
key
][
0
]
data
=
self
.
red_rois
[
key
][
1
]
inds1
,
inds2
=
np
.
where
(
data
>
0
)
new_data
=
np
.
zeros
((
inds1
.
max
()
-
inds1
.
min
()
+
1
,
inds2
.
max
()
-
inds2
.
min
()
+
1
))
new_data
=
data
[
inds1
,
inds2
].
reshape
(
new_data
)
new_origin
=
(
origin
[
0
]
+
inds1
.
min
(),
origin
[
1
]
+
inds2
.
min
())
self
.
red_rois
[
key
][
0
]
=
new_origin
self
.
red_rois
[
key
][
1
]
=
new_data
def
delete_empty_rois
(
self
):
def
delete_empty_rois
(
self
):
""" **delete_empty_rois**
""" **delete_empty_rois**
Deletes ROI entries that are completely empty.
"""
"""
pass
for
key
in
self
.
red_rois
:
if
not
np
.
any
(
self
.
red_rois
[
key
][
1
])
>
0
:
self
.
pop
(
key
)
def
shift
_rois
(
self
,
shiftVal
,
direction
=
'horiz'
,
whichroi
=
None
):
def
shift
(
self
,
shiftVal
,
direction
=
'horiz'
,
roi_inds
=
None
):
"""
"""
**shift**
**shift_rois**
Displaces the defined ROIs by the provided value.
Displaces the defined ROIs by the provided value.
Args
Args
* shiftVal : int
* shiftVal (int) : Value by which the ROIs should be shifted.
Value by which the ROIs should be shifted.
* direction : string
* direction (str) : Description of which direction to shift
Description of which direction to shit by.
by (can be 'horiz' or 'vert'), default
* whichroi : sequence
is 'horiz'.
Sequence (iterable) for which ROIs should be shifted.
* roi_inds (int) or (sequence) : Index or Sequence (iterable)
for which ROIs should be shifted.
If None, all ROIs defined are
shifted (default.)
"""
"""
the_indices
=
[]
if
not
whichroi
:
if
not
roi_inds
:
inds
=
list
(
range
(
len
(
self
.
indice
s
)))
inds
=
list
(
range
(
len
(
self
.
red_roi
s
)))
else
:
else
:
inds
=
whichroi
inds
=
roi_inds
if
direction
==
'vert'
:
if
not
isinstance
(
inds
,
Iterable
):
for
roi
in
self
.
indices
:
inds
=
list
([
inds
])
oneroi
=
[]
for
pixel
in
roi
:
for
ind
in
inds
:
oneroi
.
append
(
(
pixel
[
0
]
+
shiftVal
,
pixel
[
1
])
)
key
=
'ROI%02d'
%
ind
the_indices
.
append
(
oneroi
)
if
direction
==
'horiz'
:
self
.
red_rois
[
key
][
0
][
1
]
+=
shiftVal
if
direction
==
'horiz'
:
for
roi
in
self
.
indices
:
elif
direction
==
'vert'
:
oneroi
=
[]
self
.
red_rois
[
key
][
0
][
0
]
+=
shiftVal
for
pixel
in
roi
:
oneroi
.
append
(
(
pixel
[
0
],
pixel
[
1
]
+
shiftVal
)
)
# convert summed ROIs to other ROI formats
the_indices
.
append
(
oneroi
)
self
.
roi_matrix
=
convert_redmatrix_to_matrix
(
self
.
red_rois
,
np
.
zeros_like
(
self
.
input_image
),
offsetX
=
0
,
self
.
indices
=
the_indices
offsetY
=
0
)
self
.
masks
=
convert_roi_matrix_to_masks
(
self
.
roi_matrix
)
self
.
roi_matrix
=
convert_inds_to_matrix
(
self
.
indices
,
self
.
input_image
.
shape
)
self
.
indices
=
convert_matrix_rois_to_inds
(
self
.
roi_matrix
)
self
.
red_rois
=
convert_matrix_to_redmatrix
(
self
.
roi_matrix
)
self
.
number_of_rois
=
int
(
np
.
amax
(
self
.
roi_matrix
)
)
self
.
x_indices
=
convert_inds_to_xinds
(
self
.
indices
)
self
.
x_indices
=
convert_inds_to_xinds
(
self
.
indices
)
self
.
y_indices
=
convert_inds_to_yinds
(
self
.
indices
)
self
.
y_indices
=
convert_inds_to_yinds
(
self
.
indices
)
self
.
masks
=
convert_roi_matrix_to_masks
(
self
.
roi_matrix
)
def
pop
(
self
,
roi_key
=
None
):
""" **pop**
Discards a ROI.
def
show_rois
(
self
,
cmap
=
'Blues'
,
interpolation
=
'nearest'
,
logscaling
=
True
):
Args
""" **show_rois**
* roi_key (str) : Dict key for ROI to delete. If None, the ROI with
highest index (defined last) will be discarded (defalt).
"""
# delete last ROI if no key is specified
if
not
roi_key
:
roi_key
=
sorted
(
list
(
self
.
red_rois
.
keys
()))[
-
1
]
# make sure the ROI exists
assert
(
roi_key
in
list
(
self
.
red_rois
.
keys
())
)
# delete the ROI from the maskDict
self
.
red_rois
.
pop
(
roi_key
)
# convert summed ROIs to other ROI formats
self
.
roi_matrix
=
convert_redmatrix_to_matrix
(
self
.
red_rois
,
np
.
zeros_like
(
self
.
input_image
),
offsetX
=
0
,
offsetY
=
0
)
self
.
masks
=
convert_roi_matrix_to_masks
(
self
.
roi_matrix
)
self
.
indices
=
convert_matrix_rois_to_inds
(
self
.
roi_matrix
)
self
.
number_of_rois
=
int
(
np
.
amax
(
self
.
roi_matrix
)
)
self
.
x_indices
=
convert_inds_to_xinds
(
self
.
indices
)
self
.
y_indices
=
convert_inds_to_yinds
(
self
.
indices
)
def
show
(
self
,
cmap
=
'Blues'
,
interpolation
=
'nearest'
,
logscaling
=
True
):
""" **show**
Creates a figure showing the existing ROIs.
Creates a figure showing the existing ROIs.
Args:
Args:
...
@@ -411,9 +536,9 @@ class roi_object:
...
@@ -411,9 +536,9 @@ class roi_object:
plt
.
text
(
xcenter
,
ycenter
,
string
)
plt
.
text
(
xcenter
,
ycenter
,
string
)
plt
.
show
()
plt
.
show
()