Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
pycalibration
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
calibration
pycalibration
Commits
7daba04f
Commit
7daba04f
authored
1 year ago
by
Egor Sobolev
Browse files
Options
Downloads
Patches
Plain Diff
Support multiple module detector in Correct_DynamicFF_NBC.ipynb
parent
91e05cb3
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
notebooks/DynamicFF/Correct_DynamicFF_NBC.ipynb
+153
-170
153 additions, 170 deletions
notebooks/DynamicFF/Correct_DynamicFF_NBC.ipynb
with
153 additions
and
170 deletions
notebooks/DynamicFF/Correct_DynamicFF_NBC.ipynb
+
153
−
170
View file @
7daba04f
...
...
@@ -4,11 +4,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"#
Shimadzu HPVX2
Offline Correction\n",
"#
Dynamic Flat-field
Offline Correction\n",
"\n",
"Author: Egor Sobolev\n",
"\n",
"Offline dynamic flat-field correction
for Shimadzu HPVX2 cameras
"
"Offline dynamic flat-field correction"
]
},
{
...
...
@@ -17,17 +17,17 @@
"metadata": {},
"outputs": [],
"source": [
"in_folder = \"/gpfs/exfel/exp/SPB/202
121
/p00
2919
/raw
/
\" # input folder, required\n",
"in_folder = \"/gpfs/exfel/exp/SPB/202
430
/p
9
00
425
/raw\" # input folder, required\n",
"out_folder = '/gpfs/exfel/data/scratch/esobolev/test/shimadzu' # output folder, required\n",
"metadata_folder = \"\" # Directory containing calibration_metadata.yml when run by xfel-calibrate\n",
"run = 3
0
# which run to read data from, required\n",
"run = 3 # which run to read data from, required\n",
"\n",
"# Data files parameters.\n",
"karabo_da = ['HPVX01'] # data aggregators\n",
"karabo_id = \"SPB_EHD_
HPVX2_2
\" # karabo prefix of Shimadzu HPV-X2 devices\n",
"karabo_da = ['HPVX01
/1', 'HPVX01/2
'] # data aggregators\n",
"karabo_id = \"SPB_EHD_
MIC
\" # karabo prefix of Shimadzu HPV-X2 devices\n",
"#receiver_id = \"PNCCD_FMT-0\" # inset for receiver devices\n",
"#path_template = 'RAW-R{:04d}-{}-S{{:05d}}.h5' # the template to use to access data\n",
"instrument_source_template = '
{}/CAM/CAMERA
:daqOutput' # data source path in h5file.
Template filled with karabo_id
\n",
"instrument_source_template = '
SPB_EHD_MIC/CAM/HPVX2_{module}
:daqOutput' # data source path in h5file.\n",
"image_key = \"data.image.pixels\" # image data key in Karabo or exdf notation\n",
"\n",
"# Database access parameters.\n",
...
...
@@ -42,11 +42,11 @@
"downsample_factors = [1, 1] # list of downsample factors for each image dimention (y, x)\n",
"\n",
"constants_folder = \"/gpfs/exfel/data/scratch/esobolev/test/shimadzu\"\n",
"db_module
= \"SHIMADZU
_HPVX2_
M001
\"\n",
"db_module
_template = \"Shimadzu
_HPVX2_
{}
\"\n",
"\n",
"num_proc = 32 # number of processes running correction in parallel\n",
"\n",
"corrected_source_template = '
{}/CORR/CAMERA:daqO
utput' # data source path in h5file.
Template filled with karabo_id
"
"corrected_source_template = '
SPB_EHD_MIC/CORR/HPVX2_{module}:o
utput' # data source path in h5file."
]
},
{
...
...
@@ -59,8 +59,9 @@
"import h5py\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from IPython.display import display, Markdown\n",
"\n",
"from extra_data import RunDirectory\n",
"from extra_data import RunDirectory
, by_id
\n",
"\n",
"%matplotlib inline\n",
"from cal_tools.step_timing import StepTimer\n",
...
...
@@ -77,8 +78,29 @@
"metadata": {},
"outputs": [],
"source": [
"instrument = karabo_id.split(\"_\")[0]\n",
"source = instrument_source_template.format(karabo_id)\n",
"index_group = image_key.partition('.')[0]\n",
"instrument, part, component = karabo_id.split('_')\n",
"\n",
"aggregators = {}\n",
"sources = {}\n",
"source_to_db = {}\n",
"print(\"Sources:\")\n",
"for da in karabo_da:\n",
" aggr, _, module = da.partition('/')\n",
" instrument_source_name = instrument_source_template.format(\n",
" instrument=instrument, part=part, component=component,\n",
" module=module\n",
" )\n",
" corrected_source_name = corrected_source_template.format(\n",
" instrument=instrument, part=part, component=component,\n",
" module=module\n",
" )\n",
" aggregators.setdefault(aggr, []).append(\n",
" (instrument_source_name, corrected_source_name))\n",
" sources[instrument_source_name] = aggr\n",
" source_to_db[instrument_source_name] = db_module_template.format(module)\n",
" print('-', instrument_source_name)\n",
"print()\n",
"\n",
"print(f\"Detector in use is {karabo_id}\")\n",
"print(f\"Instrument {instrument}\")\n",
...
...
@@ -99,62 +121,42 @@
"metadata": {},
"outputs": [],
"source": [
"step_timer.start()\n",
"\n",
"# Offsets\n",
"constant_name = \"Offset\"\n",
"const_file = f\"{constants_folder}/const_{constant_name}_{db_module}.h5\"\n",
"if not os.path.isfile(const_file):\n",
" raise FileNotFoundError(f\"{constant_name} constants are not found for {karabo_id}.\")\n",
"\n",
"with h5py.File(const_file, 'r') as f:\n",
" dark_conditions = dict(\n",
" num_frames=int(f[\"condition/Memory cells/value\"][()]),\n",
" nx=int(f[\"condition/Pixels X/value\"][()]),\n",
" ny=int(f[\"condition/Pixels Y/value\"][()]),\n",
" n_components=int(f[\"condition/FF components/value\"][()]),\n",
" )\n",
" dark = f[\"data\"][:]\n",
" dark_creation_time = f[\"creation_time\"][()].decode()\n",
"\n",
"print(f\"{constant_name}: {dark_creation_time}\")\n",
"\n",
"# Flat-field components\n",
"constant_name = \"ComponentsFF\"\n",
"const_file = f\"{constants_folder}/const_{constant_name}_{db_module}.h5\"\n",
"if not os.path.isfile(const_file):\n",
" raise FileNotFoundError(f\"{constant_name} constants are not found for {karabo_id}.\")\n",
"\n",
"with h5py.File(const_file, 'r') as f:\n",
" flat_conditions = dict(\n",
" num_frames=int(f[\"condition/Memory cells/value\"][()]),\n",
" nx=int(f[\"condition/Pixels X/value\"][()]),\n",
" ny=int(f[\"condition/Pixels Y/value\"][()]),\n",
" n_components=int(f[\"condition/FF components/value\"][()]),\n",
" )\n",
" flat = f[\"data\"][:]\n",
" components = flat[1:]\n",
" flat = flat[0]\n",
" flat_creation_time = f[\"creation_time\"][()].decode()\n",
"\n",
"print(f\"{constant_name}: {dark_creation_time}\")\n",
"\n",
"if not all(flat_conditions[key] == value for key, value in dark_conditions.items()):\n",
" raise ValueError(\"Conditions for offsets and flat-field components are different\")\n",
"requested_conditions = {\n",
" \"frame_size\": 1.0,\n",
"}\n",
"\n",
"conditions = type(\"Conditions\", (), flat_conditions)\n",
"\n",
"print(f\"Image size: {conditions.nx} x {conditions.ny} px\")\n",
"print(f\"Number of flat-field components: {conditions.n_components}\")\n",
"step_timer.start()\n",
"\n",
"if conditions.n_components < n_components:\n",
" warnings.warn(\n",
" f\"The correction set to use {n_components} flat-field components, \"\n",
" f\"but constants contains only {conditions.n_components}.\"\n",
" \"The settings adjusted to the number of available components.\"\n",
" )\n",
"else:\n",
" components = components[:n_components]\n",
"corrections = {}\n",
"constant_types = [\"Offset\", \"DynamicFF\"]\n",
"for source, db_module in source_to_db.items():\n",
" constants = {}\n",
" for constant_name in constant_types:\n",
" const_file = f\"{constants_folder}/const_{constant_name}_{db_module}.h5\"\n",
" if not os.path.isfile(const_file):\n",
" raise FileNotFoundError(f\"{constant_name} constants are not found for {karabo_id}.\")\n",
"\n",
" with h5py.File(const_file, 'r') as f:\n",
" conditions = dict(\n",
" frame_size=int(f[\"condition/Frame Size/value\"][()])\n",
" )\n",
" data = f[\"data\"][:]\n",
" data_creation_time = f[\"creation_time\"][()].decode()\n",
" \n",
" if not all(conditions[key] == value for key, value in requested_conditions.items()):\n",
" raise ValueError(\"Conditions for {constant_name} are not match\")\n",
"\n",
" print(f\"{source} {db_module} {constant_name}: {data_creation_time}\")\n",
" constants[constant_name] = data\n",
"\n",
" dark = constants[\"Offset\"]\n",
" flat = constants[\"DynamicFF\"][0]\n",
" components = constants[\"DynamicFF\"][1:][:n_components]\n",
"\n",
" dffc = DynamicFlatFieldCorrection.from_constants(\n",
" dark, flat, components, downsample_factors)\n",
"\n",
" corrections[source] = dffc\n",
"\n",
"step_timer.done_step(\"Load calibration constants\")"
]
...
...
@@ -172,121 +174,102 @@
"metadata": {},
"outputs": [],
"source": [
"step_timer.start()\n",
"dc = RunDirectory(f\"{in_folder}/r{run:04d}\")\n",
"\n",
"num_trains, num_cells = dc[source][image_key].shape[:2]\n",
"num_images = num_trains * num_cells\n",
"print(\"Number of trains:\", num_trains)\n",
"print(\"Number of images:\", num_images)\n",
"\n",
"dffc = DynamicFlatFieldCorrection.from_constants(\n",
" dark, flat, components, downsample_factors)\n",
"\n",
"proc = FlatFieldCorrectionFileProcessor(dffc, num_proc, source, image_key)\n",
"\n",
"proc.start_workers()\n",
"proc.run(dc)\n",
"proc.join_workers()\n",
"\n",
"train_ids = proc.rdr.trains\n",
"corrected_images = np.stack(proc.rdr.results, 0)\n",
"step_timer.done_step(\"Correct images\")"
"report = []\n",
"for aggr, sources in aggregators.items():\n",
" dc = RunDirectory(f\"{in_folder}/r{run:04d}\", f\"RAW-R{run:04d}-{aggr}-S*.h5\")\n",
"\n",
" train_ids = set()\n",
" keydata_cache = {}\n",
" for instrument_source, corrected_source in sources:\n",
" keydata = dc[instrument_source][image_key].drop_empty_trains()\n",
" train_ids.update(keydata.train_ids)\n",
" keydata_cache[instrument_source] = keydata\n",
" train_ids = np.array(sorted(train_ids))\n",
" ts = dc.select_trains(by_id[train_ids]).train_timestamps().astype(np.uint64)\n",
"\n",
" for seq_id, train_mask in sequence_trains(train_ids, 200):\n",
" step_timer.start()\n",
" print('* sequience', seq_id)\n",
" seq_train_ids = train_ids[train_mask]\n",
" seq_timestamps = ts[train_mask]\n",
" dc_seq = dc.select_trains(by_id[seq_train_ids])\n",
" ntrains = len(seq_train_ids)\n",
"\n",
" # create output file\n",
" channels = [f\"{s[1]}/{index_group}\" for s in sources]\n",
"\n",
" f = DataFile.from_details(out_folder, aggr, run, seq_id)\n",
" f.create_metadata(like=dc, instrument_channels=channels)\n",
" f.create_index(seq_train_ids, timestamps=seq_timestamps)\n",
"\n",
" seq_report = {}\n",
" image_datasets = {}\n",
" for instrument_source, corrected_source in sources:\n",
" keydata = dc_seq[instrument_source][image_key].drop_empty_trains()\n",
" count = keydata.data_counts()\n",
" i = np.flatnonzero(count.values)\n",
" raw_images = keydata.select_trains(np.s_[i]).ndarray()\n",
"\n",
" # not pulse resolved\n",
" shape = keydata.shape\n",
" count = np.in1d(seq_train_ids, keydata.train_ids).astype(int)\n",
"\n",
" src = f.create_instrument_source(corrected_source)\n",
" src.create_index(index_group=count)\n",
"\n",
" ds_data = src.create_key(image_key, shape=shape, dtype=np.float32)\n",
" image_datasets[corrected_source] = ds_data\n",
"\n",
" step_timer.done_step(\"Create output file\")\n",
"\n",
" for instrument_source, corrected_source in sources:\n",
" step_timer.start()\n",
" dc_seq = dc.select_trains(by_id[seq_train_ids])\n",
"\n",
" dffc = corrections[instrument_source]\n",
" proc = FlatFieldCorrectionFileProcessor(dffc, num_proc, instrument_source, image_key)\n",
"\n",
" proc.start_workers()\n",
" proc.run(dc_seq)\n",
" proc.join_workers()\n",
"\n",
" # not pulse resolved\n",
" corrected_images = np.stack(proc.rdr.results, 0)\n",
" image_datasets[corrected_source][:] = corrected_images\n",
"\n",
" seq_report[instrument_source] = (raw_images[0, 0], corrected_images[:20, 0])\n",
" step_timer.done_step(\"Correct flat-field\")\n",
"\n",
" f.close()\n",
" report.append(seq_report)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"step_timer.start()\n",
"\n",
"corr_source = corrected_source_template.format(karabo_id)\n",
"channel = image_key.partition('.')[0]\n",
"data_source_id = corr_source + '/' + channel\n",
"\n",
"ts = dc.train_timestamps().astype(np.uint64)\n",
"ts = ts[np.in1d(dc.train_ids, train_ids)]\n",
"\n",
"for seq_id, train_mask in sequence_trains(train_ids):\n",
" seq_train_ids = train_ids[train_mask]\n",
" seq_timestamps = ts[train_mask]\n",
" ntrains = len(seq_train_ids)\n",
" \n",
" f = DataFile.from_details(out_folder, karabo_da[0], run, seq_id)\n",
" src = f.create_instrument_source(corr_source)\n",
" \n",
" f.create_metadata(like=dc, instrument_channels=(data_source_id,))\n",
" f.create_index(seq_train_ids, timestamps=seq_timestamps)\n",
" \n",
" channels = {\n",
" image_key.partition('.')[0]: np.ones(ntrains, int)\n",
" }\n",
" src.create_index(**channels)\n",
" src.create_key(image_key, corrected_images[train_mask])\n",
"\n",
" f.close()\n",
" \n",
"step_timer.done_step(\"Save corrected images\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The first raw image"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"step_timer.start()\n",
"for source, (raw_image, corrected_images) in report[0].items():\n",
" display(Markdown(f\"# {source}\"))\n",
"\n",
"counts = dc[source][image_key].data_counts()\n",
"i = np.flatnonzero(counts.values)\n",
" display(Markdown(\"## The first raw image\"))\n",
" plot_camera_image(raw_images[0, 0])\n",
" plt.show()\n",
"\n",
"raw_images = dc[source][image_key].select_trains(np.s_[i]).ndarray()\n",
"plot_camera_image(raw_images[0, 0])\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The first corrected image"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plot_camera_image(corrected_images[0, 0])\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## The first corrected images in the trains (up to 20)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plot_images(corrected_images[:20, 0], figsize=(13, 8))\n",
"plt.show()\n",
"step_timer.done_step(\"Draw examples of corrected images\")"
" display(Markdown(\"## The first corrected image\"))\n",
" plot_camera_image(corrected_images[0])\n",
" plt.show()\n",
"\n",
" display(Markdown(\"## The first corrected images in the trains (up to 20)\"))\n",
" plot_images(corrected_images, figsize=(13, 8))\n",
" plt.show()\n",
"\n",
"step_timer.done_step(\"Draw images\")"
]
},
{
...
...
%% Cell type:markdown id: tags:
#
Shimadzu HPVX2
Offline Correction
#
Dynamic Flat-field
Offline Correction
Author: Egor Sobolev
Offline dynamic flat-field correction
for Shimadzu HPVX2 cameras
Offline dynamic flat-field correction
%% Cell type:code id: tags:
```
python
in_folder
=
"
/gpfs/exfel/exp/SPB/202
121
/p00
2919
/raw
/
"
# input folder, required
in_folder
=
"
/gpfs/exfel/exp/SPB/202
430
/p
9
00
425
/raw
"
# input folder, required
out_folder
=
'
/gpfs/exfel/data/scratch/esobolev/test/shimadzu
'
# output folder, required
metadata_folder
=
""
# Directory containing calibration_metadata.yml when run by xfel-calibrate
run
=
3
0
# which run to read data from, required
run
=
3
# which run to read data from, required
# Data files parameters.
karabo_da
=
[
'
HPVX01
'
]
# data aggregators
karabo_id
=
"
SPB_EHD_
HPVX2_2
"
# karabo prefix of Shimadzu HPV-X2 devices
karabo_da
=
[
'
HPVX01
/1
'
,
'
HPVX01/2
'
]
# data aggregators
karabo_id
=
"
SPB_EHD_
MIC
"
# karabo prefix of Shimadzu HPV-X2 devices
#receiver_id = "PNCCD_FMT-0" # inset for receiver devices
#path_template = 'RAW-R{:04d}-{}-S{{:05d}}.h5' # the template to use to access data
instrument_source_template
=
'
{}/CAM/CAMERA
:daqOutput
'
# data source path in h5file.
Template filled with karabo_id
instrument_source_template
=
'
SPB_EHD_MIC/CAM/HPVX2_{module}
:daqOutput
'
# data source path in h5file.
image_key
=
"
data.image.pixels
"
# image data key in Karabo or exdf notation
# Database access parameters.
use_dir_creation_date
=
True
# use dir creation date as data production reference date
cal_db_interface
=
"
tcp://max-exfl-cal001:8021
"
# calibration DB interface to use
cal_db_timeout
=
300000
# timeout on caldb requests
db_output
=
False
# if True, the notebook sends dark constants to the calibration database
local_output
=
True
# if True, the notebook saves dark constants locally
creation_time
=
""
# To overwrite the measured creation_time. Required Format: YYYY-MM-DD HR:MN:SC.00 e.g. 2019-07-04 11:02:41.00
n_components
=
20
# number of principal components of flat-field to use in correction
downsample_factors
=
[
1
,
1
]
# list of downsample factors for each image dimention (y, x)
constants_folder
=
"
/gpfs/exfel/data/scratch/esobolev/test/shimadzu
"
db_module
=
"
SHIMADZU
_HPVX2_
M001
"
db_module
_template
=
"
Shimadzu
_HPVX2_
{}
"
num_proc
=
32
# number of processes running correction in parallel
corrected_source_template
=
'
{}/CORR/CAMERA:daqO
utput
'
# data source path in h5file.
Template filled with karabo_id
corrected_source_template
=
'
SPB_EHD_MIC/CORR/HPVX2_{module}:o
utput
'
# data source path in h5file.
```
%% Cell type:code id: tags:
```
python
import
os
import
h5py
import
numpy
as
np
import
matplotlib.pyplot
as
plt
from
IPython.display
import
display
,
Markdown
from
extra_data
import
RunDirectory
from
extra_data
import
RunDirectory
,
by_id
%
matplotlib
inline
from
cal_tools.step_timing
import
StepTimer
from
cal_tools.files
import
sequence_trains
,
DataFile
from
dffc.correction
import
DynamicFlatFieldCorrectionCython
as
DynamicFlatFieldCorrection
from
dffc.offline
import
FlatFieldCorrectionFileProcessor
from
dffc.draw
import
plot_images
,
plot_camera_image
```
%% Cell type:code id: tags:
```
python
instrument
=
karabo_id
.
split
(
"
_
"
)[
0
]
source
=
instrument_source_template
.
format
(
karabo_id
)
index_group
=
image_key
.
partition
(
'
.
'
)[
0
]
instrument
,
part
,
component
=
karabo_id
.
split
(
'
_
'
)
aggregators
=
{}
sources
=
{}
source_to_db
=
{}
print
(
"
Sources:
"
)
for
da
in
karabo_da
:
aggr
,
_
,
module
=
da
.
partition
(
'
/
'
)
instrument_source_name
=
instrument_source_template
.
format
(
instrument
=
instrument
,
part
=
part
,
component
=
component
,
module
=
module
)
corrected_source_name
=
corrected_source_template
.
format
(
instrument
=
instrument
,
part
=
part
,
component
=
component
,
module
=
module
)
aggregators
.
setdefault
(
aggr
,
[]).
append
(
(
instrument_source_name
,
corrected_source_name
))
sources
[
instrument_source_name
]
=
aggr
source_to_db
[
instrument_source_name
]
=
db_module_template
.
format
(
module
)
print
(
'
-
'
,
instrument_source_name
)
print
()
print
(
f
"
Detector in use is
{
karabo_id
}
"
)
print
(
f
"
Instrument
{
instrument
}
"
)
step_timer
=
StepTimer
()
```
%% Cell type:markdown id: tags:
# Calibration constants
%% Cell type:code id: tags:
```
python
requested_conditions
=
{
"
frame_size
"
:
1.0
,
}
step_timer
.
start
()
# Offsets
constant_name
=
"
Offset
"
const_file
=
f
"
{
constants_folder
}
/const_
{
constant_name
}
_
{
db_module
}
.h5
"
if
not
os
.
path
.
isfile
(
const_file
):
raise
FileNotFoundError
(
f
"
{
constant_name
}
constants are not found for
{
karabo_id
}
.
"
)
with
h5py
.
File
(
const_file
,
'
r
'
)
as
f
:
dark_conditions
=
dict
(
num_frames
=
int
(
f
[
"
condition/Memory cells/value
"
][()]),
nx
=
int
(
f
[
"
condition/Pixels X/value
"
][()]),
ny
=
int
(
f
[
"
condition/Pixels Y/value
"
][()]),
n_components
=
int
(
f
[
"
condition/FF components/value
"
][()]),
)
dark
=
f
[
"
data
"
][:]
dark_creation_time
=
f
[
"
creation_time
"
][()].
decode
()
corrections
=
{}
constant_types
=
[
"
Offset
"
,
"
DynamicFF
"
]
for
source
,
db_module
in
source_to_db
.
items
():
constants
=
{}
for
constant_name
in
constant_types
:
const_file
=
f
"
{
constants_folder
}
/const_
{
constant_name
}
_
{
db_module
}
.h5
"
if
not
os
.
path
.
isfile
(
const_file
):
raise
FileNotFoundError
(
f
"
{
constant_name
}
constants are not found for
{
karabo_id
}
.
"
)
with
h5py
.
File
(
const_file
,
'
r
'
)
as
f
:
conditions
=
dict
(
frame_size
=
int
(
f
[
"
condition/Frame Size/value
"
][()])
)
data
=
f
[
"
data
"
][:]
data_creation_time
=
f
[
"
creation_time
"
][()].
decode
()
if
not
all
(
conditions
[
key
]
==
value
for
key
,
value
in
requested_conditions
.
items
()):
raise
ValueError
(
"
Conditions for {constant_name} are not match
"
)
print
(
f
"
{
source
}
{
db_module
}
{
constant_name
}
:
{
data_creation_time
}
"
)
constants
[
constant_name
]
=
data
dark
=
constants
[
"
Offset
"
]
flat
=
constants
[
"
DynamicFF
"
][
0
]
components
=
constants
[
"
DynamicFF
"
][
1
:][:
n_components
]
print
(
f
"
{
constant_name
}
:
{
dark_creation_time
}
"
)
dffc
=
DynamicFlatFieldCorrection
.
from_constants
(
dark
,
flat
,
components
,
downsample_factors
)
# Flat-field components
constant_name
=
"
ComponentsFF
"
const_file
=
f
"
{
constants_folder
}
/const_
{
constant_name
}
_
{
db_module
}
.h5
"
if
not
os
.
path
.
isfile
(
const_file
):
raise
FileNotFoundError
(
f
"
{
constant_name
}
constants are not found for
{
karabo_id
}
.
"
)
with
h5py
.
File
(
const_file
,
'
r
'
)
as
f
:
flat_conditions
=
dict
(
num_frames
=
int
(
f
[
"
condition/Memory cells/value
"
][()]),
nx
=
int
(
f
[
"
condition/Pixels X/value
"
][()]),
ny
=
int
(
f
[
"
condition/Pixels Y/value
"
][()]),
n_components
=
int
(
f
[
"
condition/FF components/value
"
][()]),
)
flat
=
f
[
"
data
"
][:]
components
=
flat
[
1
:]
flat
=
flat
[
0
]
flat_creation_time
=
f
[
"
creation_time
"
][()].
decode
()
print
(
f
"
{
constant_name
}
:
{
dark_creation_time
}
"
)
if
not
all
(
flat_conditions
[
key
]
==
value
for
key
,
value
in
dark_conditions
.
items
()):
raise
ValueError
(
"
Conditions for offsets and flat-field components are different
"
)
conditions
=
type
(
"
Conditions
"
,
(),
flat_conditions
)
print
(
f
"
Image size:
{
conditions
.
nx
}
x
{
conditions
.
ny
}
px
"
)
print
(
f
"
Number of flat-field components:
{
conditions
.
n_components
}
"
)
if
conditions
.
n_components
<
n_components
:
warnings
.
warn
(
f
"
The correction set to use
{
n_components
}
flat-field components,
"
f
"
but constants contains only
{
conditions
.
n_components
}
.
"
"
The settings adjusted to the number of available components.
"
)
else
:
components
=
components
[:
n_components
]
corrections
[
source
]
=
dffc
step_timer
.
done_step
(
"
Load calibration constants
"
)
```
%% Cell type:markdown id: tags:
# Correction
%% Cell type:code id: tags:
```
python
step_timer
.
start
()
dc
=
RunDirectory
(
f
"
{
in_folder
}
/r
{
run
:
04
d
}
"
)
num_trains
,
num_cells
=
dc
[
source
][
image_key
].
shape
[:
2
]
num_images
=
num_trains
*
num_cells
print
(
"
Number of trains:
"
,
num_trains
)
print
(
"
Number of images:
"
,
num_images
)
dffc
=
DynamicFlatFieldCorrection
.
from_constants
(
dark
,
flat
,
components
,
downsample_factors
)
proc
=
FlatFieldCorrectionFileProcessor
(
dffc
,
num_proc
,
source
,
image_key
)
report
=
[]
for
aggr
,
sources
in
aggregators
.
items
():
dc
=
RunDirectory
(
f
"
{
in_folder
}
/r
{
run
:
04
d
}
"
,
f
"
RAW-R
{
run
:
04
d
}
-
{
aggr
}
-S*.h5
"
)
train_ids
=
set
()
keydata_cache
=
{}
for
instrument_source
,
corrected_source
in
sources
:
keydata
=
dc
[
instrument_source
][
image_key
].
drop_empty_trains
()
train_ids
.
update
(
keydata
.
train_ids
)
keydata_cache
[
instrument_source
]
=
keydata
train_ids
=
np
.
array
(
sorted
(
train_ids
))
ts
=
dc
.
select_trains
(
by_id
[
train_ids
]).
train_timestamps
().
astype
(
np
.
uint64
)
for
seq_id
,
train_mask
in
sequence_trains
(
train_ids
,
200
):
step_timer
.
start
()
print
(
'
* sequience
'
,
seq_id
)
seq_train_ids
=
train_ids
[
train_mask
]
seq_timestamps
=
ts
[
train_mask
]
dc_seq
=
dc
.
select_trains
(
by_id
[
seq_train_ids
])
ntrains
=
len
(
seq_train_ids
)
# create output file
channels
=
[
f
"
{
s
[
1
]
}
/
{
index_group
}
"
for
s
in
sources
]
f
=
DataFile
.
from_details
(
out_folder
,
aggr
,
run
,
seq_id
)
f
.
create_metadata
(
like
=
dc
,
instrument_channels
=
channels
)
f
.
create_index
(
seq_train_ids
,
timestamps
=
seq_timestamps
)
seq_report
=
{}
image_datasets
=
{}
for
instrument_source
,
corrected_source
in
sources
:
keydata
=
dc_seq
[
instrument_source
][
image_key
].
drop_empty_trains
()
count
=
keydata
.
data_counts
()
i
=
np
.
flatnonzero
(
count
.
values
)
raw_images
=
keydata
.
select_trains
(
np
.
s_
[
i
]).
ndarray
()
# not pulse resolved
shape
=
keydata
.
shape
count
=
np
.
in1d
(
seq_train_ids
,
keydata
.
train_ids
).
astype
(
int
)
src
=
f
.
create_instrument_source
(
corrected_source
)
src
.
create_index
(
index_group
=
count
)
ds_data
=
src
.
create_key
(
image_key
,
shape
=
shape
,
dtype
=
np
.
float32
)
image_datasets
[
corrected_source
]
=
ds_data
step_timer
.
done_step
(
"
Create output file
"
)
for
instrument_source
,
corrected_source
in
sources
:
step_timer
.
start
()
dc_seq
=
dc
.
select_trains
(
by_id
[
seq_train_ids
])
dffc
=
corrections
[
instrument_source
]
proc
=
FlatFieldCorrectionFileProcessor
(
dffc
,
num_proc
,
instrument_source
,
image_key
)
proc
.
start_workers
()
proc
.
run
(
dc_seq
)
proc
.
join_workers
()
# not pulse resolved
corrected_images
=
np
.
stack
(
proc
.
rdr
.
results
,
0
)
image_datasets
[
corrected_source
][:]
=
corrected_images
proc
.
start_workers
()
proc
.
run
(
dc
)
proc
.
join_workers
()
seq_report
[
instrument_source
]
=
(
raw_images
[
0
,
0
],
corrected_images
[:
20
,
0
])
step_timer
.
done_step
(
"
Correct flat-field
"
)
train_ids
=
proc
.
rdr
.
trains
corrected_images
=
np
.
stack
(
proc
.
rdr
.
results
,
0
)
step_timer
.
done_step
(
"
Correct images
"
)
f
.
close
()
report
.
append
(
seq_report
)
```
%% Cell type:code id: tags:
```
python
step_timer
.
start
()
corr_source
=
corrected_source_template
.
format
(
karabo_id
)
channel
=
image_key
.
partition
(
'
.
'
)[
0
]
data_source_id
=
corr_source
+
'
/
'
+
channel
for
source
,
(
raw_image
,
corrected_images
)
in
report
[
0
].
items
():
display
(
Markdown
(
f
"
#
{
source
}
"
))
ts
=
dc
.
train_timestamps
().
astype
(
np
.
uint64
)
ts
=
ts
[
np
.
in1d
(
dc
.
train_ids
,
train_ids
)]
display
(
Markdown
(
"
## The first raw image
"
))
plot_camera_image
(
raw_images
[
0
,
0
])
plt
.
show
()
display
(
Markdown
(
"
## The first corrected image
"
))
plot_camera_image
(
corrected_images
[
0
])
plt
.
show
()
display
(
Markdown
(
"
## The first corrected images in the trains (up to 20)
"
))
plot_images
(
corrected_images
,
figsize
=
(
13
,
8
))
plt
.
show
()
for
seq_id
,
train_mask
in
sequence_trains
(
train_ids
):
seq_train_ids
=
train_ids
[
train_mask
]
seq_timestamps
=
ts
[
train_mask
]
ntrains
=
len
(
seq_train_ids
)
f
=
DataFile
.
from_details
(
out_folder
,
karabo_da
[
0
],
run
,
seq_id
)
src
=
f
.
create_instrument_source
(
corr_source
)
f
.
create_metadata
(
like
=
dc
,
instrument_channels
=
(
data_source_id
,))
f
.
create_index
(
seq_train_ids
,
timestamps
=
seq_timestamps
)
channels
=
{
image_key
.
partition
(
'
.
'
)[
0
]:
np
.
ones
(
ntrains
,
int
)
}
src
.
create_index
(
**
channels
)
src
.
create_key
(
image_key
,
corrected_images
[
train_mask
])
f
.
close
()
step_timer
.
done_step
(
"
Save corrected images
"
)
```
%% Cell type:markdown id: tags:
## The first raw image
%% Cell type:code id: tags:
```
python
step_timer
.
start
()
counts
=
dc
[
source
][
image_key
].
data_counts
()
i
=
np
.
flatnonzero
(
counts
.
values
)
raw_images
=
dc
[
source
][
image_key
].
select_trains
(
np
.
s_
[
i
]).
ndarray
()
plot_camera_image
(
raw_images
[
0
,
0
])
plt
.
show
()
```
%% Cell type:markdown id: tags:
## The first corrected image
%% Cell type:code id: tags:
```
python
plot_camera_image
(
corrected_images
[
0
,
0
])
plt
.
show
()
```
%% Cell type:markdown id: tags:
## The first corrected images in the trains (up to 20)
%% Cell type:code id: tags:
```
python
plot_images
(
corrected_images
[:
20
,
0
],
figsize
=
(
13
,
8
))
plt
.
show
()
step_timer
.
done_step
(
"
Draw examples of corrected images
"
)
step_timer
.
done_step
(
"
Draw images
"
)
```
%% Cell type:code id: tags:
```
python
print
(
f
"
Total processing time
{
step_timer
.
timespan
()
:
.
01
f
}
s
"
)
step_timer
.
print_summary
()
```
...
...
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