Skip to content
Snippets Groups Projects
Commit fbc6ca18 authored by RLennart's avatar RLennart
Browse files

adding file structure and jupyter demo notebook

parent 06b6b87e
No related branches found
No related tags found
No related merge requests found
%% 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.
# 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment