Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
INSIGHT_HZB
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Lennart Reb
INSIGHT_HZB
Commits
fbc6ca18
Commit
fbc6ca18
authored
1 year ago
by
RLennart
Browse files
Options
Downloads
Patches
Plain Diff
adding file structure and jupyter demo notebook
parent
06b6b87e
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
demo/demo_short.ipynb
+824
-0
824 additions, 0 deletions
demo/demo_short.ipynb
demo/parameters/params_Lambda9M.txt
+36
-0
36 additions, 0 deletions
demo/parameters/params_Lambda9M.txt
with
860 additions
and
0 deletions
demo/demo_short.ipynb
0 → 100644
+
824
−
0
View file @
fbc6ca18
{
"cells": [
{
"cell_type": "markdown",
"source": [
"# Demo Short\n",
"## Short demo script demonstrating basic functionality of INSIGHT-SingleImage.\n",
"For a more complex example see the `demo.py` file.\n",
"\n",
"We start defining the paths, loading the geometry parameters, preprocess a lambda image a bit\n",
"do the reshaping process including intensity corrections and do some exemplary plots and cuts.\n",
"Then we will optimize the sdd by fitting a given reflex q position and do more plotting."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"### import python modules, and the *INSIGHT* package"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 1,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"##############################\n",
"##### Welcome to INSIGHT #####\n",
"##############################\n",
"\n",
"No version could be determined.\n",
"Set plot style: insight\n",
"Set plotting backend to:\n",
"TkAgg\n",
"\n",
"##############################\n",
"##### Start of script ########\n",
"##############################\n",
"\n"
]
}
],
"source": [
"import glob\n",
"import os\n",
"\n",
"# import the INSIGHT package as ins\n",
"import insight_scatter as ins"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"start_time": "2023-04-19T15:15:40.227817Z",
"end_time": "2023-04-19T15:15:42.087141Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"## A general remark for help option\n",
"Type `help(OBJECT)` to get more information about the corresponding class.\n",
"E.g. for the *Params* class you could type into the console `help(MyParams)`.\n"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"Download a test detector image"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 7,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"File exists.\n"
]
}
],
"source": [
"import gdown\n",
"\n",
"url_lambda = 'https://drive.google.com/file/d/11LvS5UmImslth9Qm458_MihVJezMXwEf/view?usp=sharing'\n",
"if not os.path.exists('data'):\n",
" os.mkdir('data')\n",
"output = 'data/lambda4.cbf'\n",
"if not os.path.isfile(output):\n",
" gdown.download(url_lambda, output, fuzzy=True)\n",
"else:\n",
" print(\"File exists.\")"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "markdown",
"source": [
"\n",
"\n",
"## Load lambda or pilatus files\n",
"specify folder-path to the images"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 2,
"outputs": [],
"source": [
"folderpath = os.path.abspath(r'data')"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"start_time": "2023-04-19T15:15:48.160543Z",
"end_time": "2023-04-19T15:15:48.176544Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"specify identifier for your image, this can be the exact name or part of name(s) using the wildcard symbol *\n",
"here we import all files that contain the name lambda and end in .cbf"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 3,
"outputs": [],
"source": [
"img_identifier = 'lambda*.cbf'"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"start_time": "2023-04-19T15:15:49.957520Z",
"end_time": "2023-04-19T15:15:49.973290Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"now `folderpath` and `img_identifier` are used to load all images that match the requirements\n",
"all those image paths are stored in `raw_path_list`"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 4,
"outputs": [],
"source": [
"raw_path_list = ins.sort_natural(glob.glob(os.path.join(folderpath, img_identifier)))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"start_time": "2023-04-19T15:15:52.348915Z",
"end_time": "2023-04-19T15:15:52.367945Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"let's check if all images are loaded correctly ( there should be a none-empty list)"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 5,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['C:\\\\Users\\\\ge46bak\\\\PycharmProjects\\\\INSIGHT\\\\insight-scatter\\\\demo\\\\data\\\\lambda_01.cbf', 'C:\\\\Users\\\\ge46bak\\\\PycharmProjects\\\\INSIGHT\\\\insight-scatter\\\\demo\\\\data\\\\lambda_02.cbf', 'C:\\\\Users\\\\ge46bak\\\\PycharmProjects\\\\INSIGHT\\\\insight-scatter\\\\demo\\\\data\\\\lambda_03.cbf', 'C:\\\\Users\\\\ge46bak\\\\PycharmProjects\\\\INSIGHT\\\\insight-scatter\\\\demo\\\\data\\\\lambda_04.cbf']\n"
]
}
],
"source": [
"print(raw_path_list)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"start_time": "2023-04-19T15:15:53.993573Z",
"end_time": "2023-04-19T15:15:54.013832Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"## Load reshape parameters\n",
"to load all necessary parameters from a txt file we use the insight-scatter.Params class\n",
"the text-file containing all needed parameters is already included in this demo\n",
"check out the demo file by navigating to the demo/parameters folder and open the txt file\n",
"this file is loaded using the Params class using the init function\n",
"we have to give the filepath as argument\n",
"os.path.join will join the path of the current directory with our subfolder and txt file name"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 6,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Imported the following static parameters from\n",
"C:\\Users\\ge46bak\\PycharmProjects\\INSIGHT\\insight-scatter\\demo\\parameters\\params_Lambda9M.txt:\n",
"db_x = 5.0\n",
"db_y = 1578.5\n",
"sdd = 240.0\n",
"inca = 0.4\n",
"px_size = 0.055\n",
"wl = 1.0507\n",
"det_rot_x = 0.9669255716443013\n",
"det_rot_y = 0.0\n",
"det_rot_z = 0.0\n",
"air_attenuation_coeff = 0.0011839\n",
"si_attenuation_coeff = 2.33\n",
"det_thickness = 0.3\n",
"horizontal_polarization_fraction = 0.98\n",
"det_rot_x was calculated from provided specular beam coordinates.\n"
]
}
],
"source": [
"MyParams = ins.Params(filepath=os.path.join(os.getcwd(), r'parameters/params_Lambda9M.txt'))"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"start_time": "2023-04-19T15:15:56.461002Z",
"end_time": "2023-04-19T15:15:56.505685Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"As you can see, the output shows some errors - go check the parameter input file and correct the wrong lines. In this\n",
"case the program can continue, but if you ignore such errors, the code might fail later!\n",
"You can always check the loaded parameters by typing `MyParams.params_dict` into the console or `MyParams.print_dict()`"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 7,
"outputs": [
{
"data": {
"text/plain": "{'db_x': 5.0,\n 'db_y': 1578.5,\n 'sdd': 240.0,\n 'inca': 0.4,\n 'px_size': 0.055,\n 'wl': 1.0507,\n 'det_rot_x': 0.9669255716443013,\n 'det_rot_y': 0.0,\n 'det_rot_z': 0.0,\n 'air_attenuation_coeff': 0.0011839,\n 'si_attenuation_coeff': 2.33,\n 'det_thickness': 0.3,\n 'horizontal_polarization_fraction': 0.98}"
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"MyParams.get_params()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"start_time": "2023-04-19T15:15:58.194020Z",
"end_time": "2023-04-19T15:15:58.215022Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"# Load and plot raw image\n",
"Now we load the image, for this we use the central class of INSIGHT: the SingleImage class\n",
"This class will store the image and call other classes that might manipulate the image\n",
"use the init function to load the image\n",
"here we load the first (and only) image from the list of files in raw_path_list\n",
"We also specify the set of parameters for this image."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 8,
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"WARNING:fabio.cbfimage:Defaulting type to int32\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Loaded image C:\\Users\\ge46bak\\PycharmProjects\\INSIGHT\\insight-scatter\\demo\\data\\lambda_01.cbf\n"
]
}
],
"source": [
"MySingleImage = ins.SingleImage(MyParams, raw_path_list[0])"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"start_time": "2023-04-19T15:16:00.531726Z",
"end_time": "2023-04-19T15:16:00.663117Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"To check the image we can plot it\n",
"If you need to rotate, flip etc your image check out the commands in the SingleImage class documentation\n",
"or via help(MySingleImage)"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 9,
"outputs": [],
"source": [
"MySingleImage.plot_raw_image(pretty=True)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"start_time": "2023-04-19T15:16:02.328170Z",
"end_time": "2023-04-19T15:16:03.369474Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"Before we further proceed we might want to delete hot pixels\n",
"For this we set a maximum intensity of allowed values"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 10,
"outputs": [],
"source": [
"MySingleImage.delete_hot_pixels(max_int=2e4)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"start_time": "2023-04-19T15:16:04.194586Z",
"end_time": "2023-04-19T15:16:04.261091Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"which you should control afterwards (not to delete good data as well!) by"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 11,
"outputs": [],
"source": [
"MySingleImage.plot_raw_image(pretty=True)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"start_time": "2023-04-19T15:16:07.922606Z",
"end_time": "2023-04-19T15:16:08.913272Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"Now we are set up and can get to the real stuff.\n",
"Let's transform the image to q-space vie the calculate_geometry() function.\n",
"No further input is required, since SingleImage already got the image and the corresponding parameters."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 12,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[Geometry] boost=2 performed in 0.5 s!\n"
]
}
],
"source": [
"MySingleImage.calculate_geometry()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"start_time": "2023-04-19T15:16:09.640581Z",
"end_time": "2023-04-19T15:16:10.145007Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"And now we can continue to calculate various corrections to the intensity values.\n",
"For more details check out the documentation.\n",
"This class handles all intensity corrections: angular sensitivity, solid angle, air attenuation,\n",
"polarization correction and a user defined arbitrary correction map."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 13,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[Intensity] boost=2 performed in 0.16 s!\n"
]
}
],
"source": [
"MySingleImage.calculate_corrected_intensity()"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"start_time": "2023-04-19T15:16:12.575295Z",
"end_time": "2023-04-19T15:16:12.794238Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"We can check the result by plotting the reshaped image"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 15,
"outputs": [],
"source": [
"MySingleImage.plot_reshaped_image(pretty=True, show=True)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"start_time": "2023-04-19T15:16:30.194490Z",
"end_time": "2023-04-19T15:16:31.418206Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"To create a WAXS cut use the create_cut_waxs function and provide a name and the cut limits\n",
"to look up more details about a function hit Str+B to jump to the source code of the function at your cursor"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 16,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Creating MAPI with q_min = 0.96, q_max = 1.04, chi_min = -10, chi_max = 90\n"
]
}
],
"source": [
"MySingleImage.create_cut_waxs('MAPI', q_min=0.96, q_max=1.04, chi_min=-10, chi_max=90)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"start_time": "2023-04-19T15:16:41.768110Z",
"end_time": "2023-04-19T15:16:42.363430Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"the following line to see the raw values of each pixel sorted along `q` in blue.\n",
"We can also bin the data and add it to the plot in yellos."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 20,
"outputs": [],
"source": [
"MySingleImage.MAPI.plot_raw(along='q', show=True)\n",
"MySingleImage.MAPI.plot_binned(along='q', show=True)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"start_time": "2023-04-19T15:17:09.044021Z",
"end_time": "2023-04-19T15:17:09.434526Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"You can use this peak to optimize or correct the sdd by giving reference value\n",
"the cut should only contain the reference Bragg peak - so make it quite narrow"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 21,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Using binned data for sdd optimization fit.\n",
"[[Model]]\n",
" (Model(gaussian, prefix='g1_') + Model(constant, prefix='c1_'))\n",
"[[Fit Statistics]]\n",
" # fitting method = least_squares\n",
" # function evals = 85\n",
" # data points = 1000\n",
" # variables = 4\n",
" chi-square = 31456498.7\n",
" reduced chi-square = 31582.8300\n",
" Akaike info crit = 10364.3609\n",
" Bayesian info crit = 10383.9919\n",
" R-squared = 0.97544076\n",
"[[Variables]]\n",
" g1_amplitude: 42.9443062 +/- 0.28115310 (0.65%) (init = 10)\n",
" g1_center: 0.98370979 +/- 2.7111e-05 (0.00%) (init = 1.008)\n",
" g1_sigma: 0.00422982 +/- 2.8824e-05 (0.68%) (init = 0.01)\n",
" c1_c: 237.294388 +/- 6.62849585 (2.79%) (init = 10)\n",
" g1_fwhm: 0.00996047 +/- 6.7875e-05 (0.68%) == '2.3548200*g1_sigma'\n",
" g1_height: 4050.35974 +/- 22.9657174 (0.57%) == '0.3989423*g1_amplitude/max(1e-15, g1_sigma)'\n",
"[[Correlations]] (unreported correlations are < 0.100)\n",
" C(g1_amplitude, g1_sigma) = +0.6405\n",
" C(g1_amplitude, c1_c) = -0.5303\n",
" C(g1_sigma, c1_c) = -0.3396\n",
"[[Model]]\n",
" (Model(gaussian, prefix='g1_') + Model(constant, prefix='c1_'))\n",
"[[Fit Statistics]]\n",
" # fitting method = least_squares\n",
" # function evals = 85\n",
" # data points = 1000\n",
" # variables = 4\n",
" chi-square = 31456498.7\n",
" reduced chi-square = 31582.8300\n",
" Akaike info crit = 10364.3609\n",
" Bayesian info crit = 10383.9919\n",
" R-squared = 0.97544076\n",
"[[Variables]]\n",
" g1_amplitude: 42.9443062 +/- 0.28115310 (0.65%) (init = 10)\n",
" g1_center: 0.98370979 +/- 2.7111e-05 (0.00%) (init = 1.008)\n",
" g1_sigma: 0.00422982 +/- 2.8824e-05 (0.68%) (init = 0.01)\n",
" c1_c: 237.294388 +/- 6.62849585 (2.79%) (init = 10)\n",
" g1_fwhm: 0.00996047 +/- 6.7875e-05 (0.68%) == '2.3548200*g1_sigma'\n",
" g1_height: 4050.35974 +/- 22.9657174 (0.57%) == '0.3989423*g1_amplitude/max(1e-15, g1_sigma)'\n",
"[[Correlations]] (unreported correlations are < 0.100)\n",
" C(g1_amplitude, g1_sigma) = +0.6405\n",
" C(g1_amplitude, c1_c) = -0.5303\n",
" C(g1_sigma, c1_c) = -0.3396\n",
"\n",
"Reshaping with new sdd=234.17673603092985 mm. Old was sdd = 240.0 mm.\n",
"[Geometry] boost=2 performed in 0.46 s!\n",
"[Intensity] boost=2 performed in 0.17 s!\n",
"Updating all cuts.\n",
"Creating MAPI with q_min = 0.96, q_max = 1.04, chi_min = -10, chi_max = 90\n",
"The desired name of your cut MAPI existed already - overwriting!\n"
]
}
],
"source": [
"MySingleImage.optimize_sdd('MAPI', 1.008, plot=True)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"start_time": "2023-04-19T15:17:21.172128Z",
"end_time": "2023-04-19T15:17:22.740523Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"The SDD changed - let's see if the peak position now matches better to the reference value `1.008`"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 24,
"outputs": [],
"source": [
"MySingleImage.MAPI.plot_raw(along='q', show=True)\n",
"MySingleImage.MAPI.plot_binned(along='q', show=True)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"start_time": "2023-04-19T15:17:41.388337Z",
"end_time": "2023-04-19T15:17:41.821376Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"Let's now do a pseudo XRD cut of the complete image"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 26,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Creating psXRD with q_min = 0, q_max = 5, chi_min = -10, chi_max = 90\n",
"The desired name of your cut psXRD existed already - overwriting!\n"
]
}
],
"source": [
"MySingleImage.create_cut_waxs('psXRD', 0, 5, -10, 90)\n",
"MySingleImage.psXRD.plot_binned(along='q', show=True)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"start_time": "2023-04-19T15:17:49.498170Z",
"end_time": "2023-04-19T15:17:51.941467Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"Now we want to make an azimuthal tube cut"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 27,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Creating MAPItube with q_min = 0.96, q_max = 1.04, chi_min = -10, chi_max = 90\n"
]
}
],
"source": [
"MySingleImage.create_cut_waxs('MAPItube', 0.96, 1.04, -10, 90)\n",
"MySingleImage.MAPItube.plot_binned(along='chi', show=True)"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"start_time": "2023-04-19T15:18:01.509559Z",
"end_time": "2023-04-19T15:18:02.302095Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"To visualize the cuts we can draw them on the reshaped image\n",
"if you closed it just copy and past the line above into the console to plot the image again.\n",
"You can do this also with the other cuts by providing the corresponding name"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 28,
"outputs": [],
"source": [
"MySingleImage.plot_reshaped_image(pretty=True, show=True)\n",
"MySingleImage.plot_cut_outline_waxs('MAPItube', color='red')"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"start_time": "2023-04-19T15:18:14.049795Z",
"end_time": "2023-04-19T15:18:15.773584Z"
}
}
},
{
"cell_type": "markdown",
"source": [
"Thank you for working you way through this tutorial. This is the same content as in `demo_short.py`, so now it is time\n",
"for you to move to the other demo."
],
"metadata": {
"collapsed": false
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
\ No newline at end of file
%% Cell type:markdown id: tags:
# Demo Short
## Short demo script demonstrating basic functionality of INSIGHT-SingleImage.
For a more complex example see the
`demo.py`
file.
We start defining the paths, loading the geometry parameters, preprocess a lambda image a bit
do the reshaping process including intensity corrections and do some exemplary plots and cuts.
Then we will optimize the sdd by fitting a given reflex q position and do more plotting.
%% Cell type:markdown id: tags:
### import python modules, and the *INSIGHT* package
%% Cell type:code id: tags:
```
python
import
glob
import
os
# import the INSIGHT package as ins
import
insight_scatter
as
ins
```
%% Output
##############################
##### Welcome to INSIGHT #####
##############################
No version could be determined.
Set plot style: insight
Set plotting backend to:
TkAgg
##############################
##### Start of script ########
##############################
%% Cell type:markdown id: tags:
## A general remark for help option
Type
`help(OBJECT)`
to get more information about the corresponding class.
E.g. for the
*Params*
class you could type into the console
`help(MyParams)`
.
%% Cell type:markdown id: tags:
Download a test detector image
%% Cell type:code id: tags:
```
python
import
gdown
url_lambda
=
'
https://drive.google.com/file/d/11LvS5UmImslth9Qm458_MihVJezMXwEf/view?usp=sharing
'
if
not
os
.
path
.
exists
(
'
data
'
):
os
.
mkdir
(
'
data
'
)
output
=
'
data/lambda4.cbf
'
if
not
os
.
path
.
isfile
(
output
):
gdown
.
download
(
url_lambda
,
output
,
fuzzy
=
True
)
else
:
print
(
"
File exists.
"
)
```
%% Output
File exists.
%% Cell type:markdown id: tags:
## Load lambda or pilatus files
specify folder-path to the images
%% Cell type:code id: tags:
```
python
folderpath
=
os
.
path
.
abspath
(
r
'
data
'
)
```
%% Cell type:markdown id: tags:
specify identifier for your image, this can be the exact name or part of name(s) using the wildcard symbol
*
here we import all files that contain the name lambda and end in .cbf
%% Cell type:code id: tags:
```
python
img_identifier
=
'
lambda*.cbf
'
```
%% Cell type:markdown id: tags:
now
`folderpath`
and
`img_identifier`
are used to load all images that match the requirements
all those image paths are stored in
`raw_path_list`
%% Cell type:code id: tags:
```
python
raw_path_list
=
ins
.
sort_natural
(
glob
.
glob
(
os
.
path
.
join
(
folderpath
,
img_identifier
)))
```
%% Cell type:markdown id: tags:
let's check if all images are loaded correctly ( there should be a none-empty list)
%% Cell type:code id: tags:
```
python
print
(
raw_path_list
)
```
%% Output
['C:\\Users\\ge46bak\\PycharmProjects\\INSIGHT\\insight-scatter\\demo\\data\\lambda_01.cbf', 'C:\\Users\\ge46bak\\PycharmProjects\\INSIGHT\\insight-scatter\\demo\\data\\lambda_02.cbf', 'C:\\Users\\ge46bak\\PycharmProjects\\INSIGHT\\insight-scatter\\demo\\data\\lambda_03.cbf', 'C:\\Users\\ge46bak\\PycharmProjects\\INSIGHT\\insight-scatter\\demo\\data\\lambda_04.cbf']
%% Cell type:markdown id: tags:
## Load reshape parameters
to load all necessary parameters from a txt file we use the insight-scatter.Params class
the text-file containing all needed parameters is already included in this demo
check out the demo file by navigating to the demo/parameters folder and open the txt file
this file is loaded using the Params class using the init function
we have to give the filepath as argument
os.path.join will join the path of the current directory with our subfolder and txt file name
%% Cell type:code id: tags:
```
python
MyParams
=
ins
.
Params
(
filepath
=
os
.
path
.
join
(
os
.
getcwd
(),
r
'
parameters/params_Lambda9M.txt
'
))
```
%% Output
Imported the following static parameters from
C:\Users\ge46bak\PycharmProjects\INSIGHT\insight-scatter\demo\parameters\params_Lambda9M.txt:
db_x = 5.0
db_y = 1578.5
sdd = 240.0
inca = 0.4
px_size = 0.055
wl = 1.0507
det_rot_x = 0.9669255716443013
det_rot_y = 0.0
det_rot_z = 0.0
air_attenuation_coeff = 0.0011839
si_attenuation_coeff = 2.33
det_thickness = 0.3
horizontal_polarization_fraction = 0.98
det_rot_x was calculated from provided specular beam coordinates.
%% Cell type:markdown id: tags:
As you can see, the output shows some errors - go check the parameter input file and correct the wrong lines. In this
case the program can continue, but if you ignore such errors, the code might fail later!
You can always check the loaded parameters by typing
`MyParams.params_dict`
into the console or
`MyParams.print_dict()`
%% Cell type:code id: tags:
```
python
MyParams
.
get_params
()
```
%% Output
{'db_x': 5.0,
'db_y': 1578.5,
'sdd': 240.0,
'inca': 0.4,
'px_size': 0.055,
'wl': 1.0507,
'det_rot_x': 0.9669255716443013,
'det_rot_y': 0.0,
'det_rot_z': 0.0,
'air_attenuation_coeff': 0.0011839,
'si_attenuation_coeff': 2.33,
'det_thickness': 0.3,
'horizontal_polarization_fraction': 0.98}
%% Cell type:markdown id: tags:
# Load and plot raw image
Now we load the image, for this we use the central class of INSIGHT: the SingleImage class
This class will store the image and call other classes that might manipulate the image
use the init function to load the image
here we load the first (and only) image from the list of files in raw_path_list
We also specify the set of parameters for this image.
%% Cell type:code id: tags:
```
python
MySingleImage
=
ins
.
SingleImage
(
MyParams
,
raw_path_list
[
0
])
```
%% Output
WARNING:fabio.cbfimage:Defaulting type to int32
Loaded image C:\Users\ge46bak\PycharmProjects\INSIGHT\insight-scatter\demo\data\lambda_01.cbf
%% Cell type:markdown id: tags:
To check the image we can plot it
If you need to rotate, flip etc your image check out the commands in the SingleImage class documentation
or via help(MySingleImage)
%% Cell type:code id: tags:
```
python
MySingleImage
.
plot_raw_image
(
pretty
=
True
)
```
%% Cell type:markdown id: tags:
Before we further proceed we might want to delete hot pixels
For this we set a maximum intensity of allowed values
%% Cell type:code id: tags:
```
python
MySingleImage
.
delete_hot_pixels
(
max_int
=
2e4
)
```
%% Cell type:markdown id: tags:
which you should control afterwards (not to delete good data as well!) by
%% Cell type:code id: tags:
```
python
MySingleImage
.
plot_raw_image
(
pretty
=
True
)
```
%% Cell type:markdown id: tags:
Now we are set up and can get to the real stuff.
Let's transform the image to q-space vie the calculate_geometry() function.
No further input is required, since SingleImage already got the image and the corresponding parameters.
%% Cell type:code id: tags:
```
python
MySingleImage
.
calculate_geometry
()
```
%% Output
[Geometry] boost=2 performed in 0.5 s!
%% Cell type:markdown id: tags:
And now we can continue to calculate various corrections to the intensity values.
For more details check out the documentation.
This class handles all intensity corrections: angular sensitivity, solid angle, air attenuation,
polarization correction and a user defined arbitrary correction map.
%% Cell type:code id: tags:
```
python
MySingleImage
.
calculate_corrected_intensity
()
```
%% Output
[Intensity] boost=2 performed in 0.16 s!
%% Cell type:markdown id: tags:
We can check the result by plotting the reshaped image
%% Cell type:code id: tags:
```
python
MySingleImage
.
plot_reshaped_image
(
pretty
=
True
,
show
=
True
)
```
%% Cell type:markdown id: tags:
To create a WAXS cut use the create_cut_waxs function and provide a name and the cut limits
to look up more details about a function hit Str+B to jump to the source code of the function at your cursor
%% Cell type:code id: tags:
```
python
MySingleImage
.
create_cut_waxs
(
'
MAPI
'
,
q_min
=
0.96
,
q_max
=
1.04
,
chi_min
=-
10
,
chi_max
=
90
)
```
%% Output
Creating MAPI with q_min = 0.96, q_max = 1.04, chi_min = -10, chi_max = 90
%% Cell type:markdown id: tags:
the following line to see the raw values of each pixel sorted along
`q`
in blue.
We can also bin the data and add it to the plot in yellos.
%% Cell type:code id: tags:
```
python
MySingleImage
.
MAPI
.
plot_raw
(
along
=
'
q
'
,
show
=
True
)
MySingleImage
.
MAPI
.
plot_binned
(
along
=
'
q
'
,
show
=
True
)
```
%% Cell type:markdown id: tags:
You can use this peak to optimize or correct the sdd by giving reference value
the cut should only contain the reference Bragg peak - so make it quite narrow
%% Cell type:code id: tags:
```
python
MySingleImage
.
optimize_sdd
(
'
MAPI
'
,
1.008
,
plot
=
True
)
```
%% Output
Using binned data for sdd optimization fit.
[[Model]]
(Model(gaussian, prefix='g1_') + Model(constant, prefix='c1_'))
[[Fit Statistics]]
# fitting method = least_squares
# function evals = 85
# data points = 1000
# variables = 4
chi-square = 31456498.7
reduced chi-square = 31582.8300
Akaike info crit = 10364.3609
Bayesian info crit = 10383.9919
R-squared = 0.97544076
[[Variables]]
g1_amplitude: 42.9443062 +/- 0.28115310 (0.65%) (init = 10)
g1_center: 0.98370979 +/- 2.7111e-05 (0.00%) (init = 1.008)
g1_sigma: 0.00422982 +/- 2.8824e-05 (0.68%) (init = 0.01)
c1_c: 237.294388 +/- 6.62849585 (2.79%) (init = 10)
g1_fwhm: 0.00996047 +/- 6.7875e-05 (0.68%) == '2.3548200*g1_sigma'
g1_height: 4050.35974 +/- 22.9657174 (0.57%) == '0.3989423*g1_amplitude/max(1e-15, g1_sigma)'
[[Correlations]] (unreported correlations are < 0.100)
C(g1_amplitude, g1_sigma) = +0.6405
C(g1_amplitude, c1_c) = -0.5303
C(g1_sigma, c1_c) = -0.3396
[[Model]]
(Model(gaussian, prefix='g1_') + Model(constant, prefix='c1_'))
[[Fit Statistics]]
# fitting method = least_squares
# function evals = 85
# data points = 1000
# variables = 4
chi-square = 31456498.7
reduced chi-square = 31582.8300
Akaike info crit = 10364.3609
Bayesian info crit = 10383.9919
R-squared = 0.97544076
[[Variables]]
g1_amplitude: 42.9443062 +/- 0.28115310 (0.65%) (init = 10)
g1_center: 0.98370979 +/- 2.7111e-05 (0.00%) (init = 1.008)
g1_sigma: 0.00422982 +/- 2.8824e-05 (0.68%) (init = 0.01)
c1_c: 237.294388 +/- 6.62849585 (2.79%) (init = 10)
g1_fwhm: 0.00996047 +/- 6.7875e-05 (0.68%) == '2.3548200*g1_sigma'
g1_height: 4050.35974 +/- 22.9657174 (0.57%) == '0.3989423*g1_amplitude/max(1e-15, g1_sigma)'
[[Correlations]] (unreported correlations are < 0.100)
C(g1_amplitude, g1_sigma) = +0.6405
C(g1_amplitude, c1_c) = -0.5303
C(g1_sigma, c1_c) = -0.3396
Reshaping with new sdd=234.17673603092985 mm. Old was sdd = 240.0 mm.
[Geometry] boost=2 performed in 0.46 s!
[Intensity] boost=2 performed in 0.17 s!
Updating all cuts.
Creating MAPI with q_min = 0.96, q_max = 1.04, chi_min = -10, chi_max = 90
The desired name of your cut MAPI existed already - overwriting!
%% Cell type:markdown id: tags:
The SDD changed - let's see if the peak position now matches better to the reference value
`1.008`
%% Cell type:code id: tags:
```
python
MySingleImage
.
MAPI
.
plot_raw
(
along
=
'
q
'
,
show
=
True
)
MySingleImage
.
MAPI
.
plot_binned
(
along
=
'
q
'
,
show
=
True
)
```
%% Cell type:markdown id: tags:
Let's now do a pseudo XRD cut of the complete image
%% Cell type:code id: tags:
```
python
MySingleImage
.
create_cut_waxs
(
'
psXRD
'
,
0
,
5
,
-
10
,
90
)
MySingleImage
.
psXRD
.
plot_binned
(
along
=
'
q
'
,
show
=
True
)
```
%% Output
Creating psXRD with q_min = 0, q_max = 5, chi_min = -10, chi_max = 90
The desired name of your cut psXRD existed already - overwriting!
%% Cell type:markdown id: tags:
Now we want to make an azimuthal tube cut
%% Cell type:code id: tags:
```
python
MySingleImage
.
create_cut_waxs
(
'
MAPItube
'
,
0.96
,
1.04
,
-
10
,
90
)
MySingleImage
.
MAPItube
.
plot_binned
(
along
=
'
chi
'
,
show
=
True
)
```
%% Output
Creating MAPItube with q_min = 0.96, q_max = 1.04, chi_min = -10, chi_max = 90
%% Cell type:markdown id: tags:
To visualize the cuts we can draw them on the reshaped image
if you closed it just copy and past the line above into the console to plot the image again.
You can do this also with the other cuts by providing the corresponding name
%% Cell type:code id: tags:
```
python
MySingleImage
.
plot_reshaped_image
(
pretty
=
True
,
show
=
True
)
MySingleImage
.
plot_cut_outline_waxs
(
'
MAPItube
'
,
color
=
'
red
'
)
```
%% Cell type:markdown id: tags:
Thank you for working you way through this tutorial. This is the same content as in
`demo_short.py`
, so now it is time
for you to move to the other demo.
This diff is collapsed.
Click to expand it.
demo/parameters/params_Lambda9M.txt
0 → 100644
+
36
−
0
View file @
fbc6ca18
# direct beam coordinates in pixel
db_x = 5
db_y = 1578.5
# specular beam position in pixel
spec_x = 3
spec_y = 1697
# sample-to-detector distance in mm
sdd = 240
# incidence angle in degree
inca = 0.4
# detector pixel size in mm
px_size = 0.055
# wavelength in Angstrom
wl = 1.0507
# detector rotation in degree (x along primary beam direction)
#det_rot_x = -0.6368657183977084
det_rot_y = 0.0
det_rot_z = 0.0
# air attenuation coefficient in
air_attenuation_coeff = 0.0011839
# silicon attenuation coefficient in
si_attenuation_coeff = 2.33
# detector thickness in mm
det_thickness = 0.3
# horizontal polarization fraction between 0 and 1, use 'None' for no correction
horizontal_polarization_fraction = 0.98
\ No newline at end of file
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