Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
Live integration
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Koziej Lab
X-ray Diffraction
PDF
Live integration
Commits
9660f10c
Commit
9660f10c
authored
9 months ago
by
Gröne, Tjark Leon Raphael
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
a89d6ad2
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
maxwell_integrate_with_subdirs.py
+159
-0
159 additions, 0 deletions
maxwell_integrate_with_subdirs.py
with
159 additions
and
0 deletions
maxwell_integrate_with_subdirs.py
0 → 100644
+
159
−
0
View file @
9660f10c
#!/usr/bin/env python3
import
pyFAI
import
fabio
import
os
import
sys
from
time
import
sleep
from
glob
import
glob
import
logging
logger
=
logging
.
getLogger
()
logger
.
setLevel
(
logging
.
CRITICAL
)
import
threading
from
watchdog.observers.polling
import
PollingObserver
from
watchdog.events
import
PatternMatchingEventHandler
from
multiprocessing.pool
import
ThreadPool
as
Pool
global
NPROC
def
integrate_ims_in_dir
(
path_im
,
path_int
,
dtype_im
=
"
.tif
"
,
dtype_int
=
"
.dat
"
):
"""
Azimuthally integrate all images in directory <path> with ending <dtype_im>
to patterns of ending <dtype_int> if not already integrated.
:param
'
str
'
path: path to directory where to apply the azimuthal integration
:param
'
str
'
dtype_im: data type/filename ending of image file
:param
'
str
'
dtype_int: data type/filename ending of pattern file
"""
global
NPROC
fnames_ims
=
[]
#= glob(os.path.join(path_im, "*" + dtype_im))
path_int_list
=
[]
for
path
,
subdirs
,
files
in
os
.
walk
(
path_im
):
for
name
in
files
:
if
(
"
cu
"
not
in
name
)
or
(
"
Cu
"
not
in
name
)
or
(
"
np
"
not
in
name
):
if
"
sdd500
"
not
in
name
and
"
sdd750
"
not
in
name
and
"
sdd1000
"
not
in
name
:
fnames_ims
.
append
(
os
.
path
.
join
(
path
,
name
))
if
path_im
!=
str
(
path
):
path_new
=
str
(
path
).
replace
(
path_im
,
''
)
path_new
=
path_int
+
path_new
else
:
path_new
=
path_int
path_int_list
.
append
(
path_new
)
#fnames_ims.sort(key=str.lower)
def
integration_thread
(
fname_im
,
path_int
):
global
NPT
global
UNIT
global
POLARIZATION
if
not
os
.
path
.
isdir
(
path_int
):
os
.
mkdir
(
path_int
)
im
=
fabio
.
open
(
fname_im
).
data
basename_int
=
os
.
path
.
basename
(
fname_im
)[:
-
len
(
dtype_im
)]
+
dtype_int
fname_int
=
os
.
path
.
join
(
path_int
,
basename_int
)
if
not
os
.
path
.
isfile
(
fname_int
):
ai
.
integrate1d
(
data
=
im
,
npt
=
NPT
,
filename
=
fname_int
,
mask
=
mask
,
polarization_factor
=
POLARIZATION
,
correctSolidAngle
=
True
,
error_model
=
'
azimuthal
'
,
unit
=
UNIT
)
print
(
f
"
integrate:
{
fname_im
}
"
)
pool
=
Pool
(
int
(
NPROC
))
for
i
,
fname_im
in
enumerate
(
fnames_ims
):
pool
.
apply_async
(
integration_thread
,
(
fname_im
,
path_int_list
[
i
]))
pool
.
close
()
pool
.
join
()
def
integrate_on_created
(
event
,
path_int
,
dtype_im
=
"
.tif
"
,
dtype_int
=
"
.dat
"
):
"""
Azimuthally integrate all created images in directory <path>
with ending <dtype_im> to patterns of ending <dtype_int>.
:param
'
watchdog.events.FileCreatedEvent
'
event: watchdog object to check for created files
:param
'
str
'
dtype_im: data type/filename ending of image file
:param
'
str
'
dtype_int: data type/filename ending of pattern file
"""
if
not
os
.
path
.
isdir
(
path_int
):
os
.
mkdir
(
path_int
)
if
event
.
src_path
[
-
len
(
dtype_im
):]
==
dtype_im
:
im
=
fabio
.
open
(
event
.
src_path
).
data
basename_int
=
os
.
path
.
basename
(
event
.
src_path
)[:
-
len
(
dtype_im
)]
+
dtype_int
ai
.
integrate1d
(
data
=
im
,
npt
=
3000
,
filename
=
os
.
path
.
join
(
path_int
,
basename_int
),
mask
=
mask
,
polarization_factor
=
0.95
,
correctSolidAngle
=
True
,
error_model
=
'
azimuthal
'
,
unit
=
"
q_A^-1
"
)
print
(
f
"
integrate:
{
event
.
src_path
}
"
)
class
Handler
(
PatternMatchingEventHandler
):
patterns
=
[
"
*tif
"
,
"
*tiff
"
,
"
*TIF
"
,
"
*TIFF
"
]
ignore_patterns
=
[]
ignore_directories
=
True
case_sensitive
=
True
go_recursively
=
True
def
__init__
(
self
,
path_im
,
path_int
):
PatternMatchingEventHandler
.
__init__
(
self
)
self
.
path_im
=
path_im
self
.
path_int
=
path_int
def
on_created
(
self
,
event
):
#wait that the transfer of the file is finished before processing it
path_event
=
str
(
os
.
path
.
dirname
(
event
.
src_path
))
print
(
path_event
)
if
self
.
path_im
!=
path_event
:
if
self
.
path_im
in
path_event
:
path_event
=
path_event
.
replace
(
self
.
path_im
,
''
)
path_int
=
self
.
path_int
+
path_event
else
:
path_int
=
self
.
path_int
integrate_on_created
(
event
,
path_int
)
if
__name__
==
'
__main__
'
:
path_im
=
sys
.
argv
[
1
]
path_int
=
sys
.
argv
[
2
]
fname_poni
=
sys
.
argv
[
3
]
fname_mask
=
sys
.
argv
[
4
]
NPROC
=
sys
.
argv
[
5
]
POLARIZATION
=
sys
.
argv
[
6
]
NPT
=
sys
.
argv
[
7
]
UNIT
=
sys
.
argv
[
8
]
if
not
os
.
path
.
isdir
(
path_int
):
os
.
mkdir
(
path_int
)
ai
=
pyFAI
.
load
(
fname_poni
)
mask
=
fabio
.
open
(
fname_mask
).
data
if
fname_mask
else
None
integrate_ims_in_dir
(
path_im
,
path_int
)
print
(
"
Observing directory:
"
+
str
(
path_im
))
event_handler
=
Handler
(
path_im
,
path_int
)
observer
=
PollingObserver
()
observer
.
schedule
(
event_handler
,
path_im
,
recursive
=
True
)
print
(
"
About to start observer
"
)
observer
.
start
()
try
:
while
True
:
sleep
(
0.05
)
except
KeyboardInterrupt
:
observer
.
stop
()
observer
.
join
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment