Skip to content
Snippets Groups Projects
Commit ca12d7b1 authored by Pospelov, Gennady's avatar Pospelov, Gennady
Browse files

Merge branch 'ImportDataExample' into develop

parents 216ad775 ad81fe2f
No related branches found
No related tags found
No related merge requests found
Showing
with 117 additions and 7 deletions
......@@ -3,36 +3,40 @@ BornAgain fitting examples.
In this directory one can find examples of complete applications
used for fitting.
--- ex001_SampleParametersIntro ---
--- ex01_SampleParametersIntro ---
This example shows how to create a sample with fixed parameters and then
change this parameters on the fly during runtime.
The example doesn't contain any fitting and serve as a gentle introduction
to other fitting examples.
--- ex002_FitCylindersAndPrisms ---
--- ex02_FitCylindersAndPrisms ---
The model "mixture of cylinders and prisms without interference"
will be used to fit real data.
FitCylindersAndPrisms.py shows minimal fitting example
FitCylindersAndPrisms_detailed.py is more detailed version + some graphical output
--- ex003_FitSpheresInHexLattice ---
--- ex03_FitSpheresInHexLattice ---
Two parameter fit of spheres in a hex lattice.
FitSpheresInHexLattice.py - demonstrate standard approach
FitSpheresInHexLattice_builder.py - shows advanced way using sample builder, which might be useful for complex
samples
--- ex004_FitScaleAndShift ---
--- ex04_FitScaleAndShift ---
FitScaleAndShift.py - in this example we are trying to fit the data representing cylinders without interference
on top of substrate. Real data contains some "unknown" background and scale factors.
In four parameters fit we are looking for cylinder's height and radius and for background and scale factors.
--- ex005_FitWithMasks ---
--- ex05_FitWithMasks ---
FitWithMasks.py - two parameter fit of cylinders without interference.
Real data contains rectangular mask to simulate and fit only the area inside the mask.
Real data contains rectangular masks to simulate and fit only the area outside the masks.
--- ex006_FitStrategies ---
--- ex06_FitStrategies ---
FitStrategyAdjustMinimizer.py - in this example we are trying to find cylinder's height and radius using chain
of minimizers. During the first fit round Genetic minimizer will be used. It will roughly look for possible
global minimas. After it is done, the second Minuit2 minimizer will continue to find the precise location of local
minima.
--- ex07_ImportUserData
This directory contains examples for importing raw X,Y detector data into BornAgain's
IntensityData object (phi_f, alpha_f space)
ImportMariaData.py - in this example we load ASCII data from Maria reflectometer
\ No newline at end of file
"""
Loading raw data (gzipped ASCII format) from Maria reflectometer.
Maria is a reflectometer at FRM-II, with vertical sample orientation. The detector image is an array
of pixels [1024][1024]. Detector X corresponds to our alpha_f, detector Y corresponds to phi_f.
See ./maria_geometry.png
"""
import os
import sys
import pylab
import matplotlib
import numpy
import gzip
from bornagain import *
# coordinates of the detector center according to the instrument responsible
DETECTOR_ALPHA = 2.5*degree
DETECTOR_PHI = 0.4*degree
DETECTOR_DISTANCE = 1965.0 # in mm
NBINS_X = 1024
NBINS_Y = 1024
PIXEL_SIZE = 0.64 # in mm
def read_maria_data(fileName):
"""
Reads zipped file with raw detector image data and returns numpy array.
The file contains 1024 lines with 1024 amplitudes (in counts) in each line.
"""
print "read_maria_data() -> Reading ", fileName
fin = gzip.GzipFile(fileName)
data = numpy.array([[int(val) for val in line.split()] for line in fin])
return numpy.flipud(data)
def get_phif_alphaf_edges():
"""
Returns two arrays of size [NBINS+1] representing non-equidistant bin edges in phi_f, alpha_f space in radians.
The first element of the array phi_f_edges[0] corresponds to the left bound of first y-pixel, the last element of
the array phi_f_edges[1024] corresponds to the right bound of the last y-pixel.
"""
phi_f_edges = []
for i_phi in range(0, NBINS_Y+1):
center_y = NBINS_Y*PIXEL_SIZE/2.
phi_f_edges.append(numpy.arctan2(i_phi*PIXEL_SIZE - center_y, DETECTOR_DISTANCE) + DETECTOR_PHI)
alpha_f_edges = []
for i_alpha in range(0, NBINS_X+1):
center_x = NBINS_X*PIXEL_SIZE/2.
alpha_f_edges.append(numpy.arctan2(i_alpha*PIXEL_SIZE - center_x, DETECTOR_DISTANCE) + DETECTOR_ALPHA)
return phi_f_edges, alpha_f_edges
def create_intensity_data_object(raw_data):
"""
Creates IntensityData object from numpy array with raw data.
"""
phi_f_edges, alpha_f_edges = get_phif_alphaf_edges()
result = IntensityData()
result.addAxis(VariableBinAxis("phi_f", NBINS_Y, phi_f_edges))
result.addAxis(VariableBinAxis("alpha_f", NBINS_X, alpha_f_edges))
# Filling IntensityData object with raw data
rotated_data = numpy.rot90(raw_data, 3) # Maria is a vertical reflectometer, need to rotate the data
result.setRawDataVector(rotated_data.flatten(order='F').tolist())
return result
if __name__ == '__main__':
# reading raw data
raw_data = read_maria_data("maria_raw_data.gz")
# creating IntensityData object
intensity = create_intensity_data_object(raw_data)
# plotting raw detector data
pylab.figure("Raw data in X,Y space")
im1 = pylab.imshow(raw_data, norm=matplotlib.colors.LogNorm(1, 1e+04), origin='lower', extent=[0, 1024, 0, 1024])
pylab.colorbar(im1)
pylab.xlabel("nbin_x", fontsize=16)
pylab.ylabel("nbin_y", fontsize=16)
# plotting intensity data object
pylab.figure("Imported data in phi_f, alpha_f space")
im = pylab.imshow(numpy.rot90(intensity.getArray(), 1), norm=matplotlib.colors.LogNorm(),
extent=[intensity.getAxis(0).getMin(), intensity.getAxis(0).getMax(), intensity.getAxis(1).getMin(), intensity.getAxis(1).getMax()])
pylab.colorbar(im)
pylab.xlabel(r'$\phi_f, rad$', fontsize=16)
pylab.ylabel(r'$\alpha_f, rad$', fontsize=16)
# clipping the dataset
clip = IntensityDataFunctions.createClippedDataSet(intensity, -5.0*degree, 0.0*degree, 5.0*degree, 5.0*degree)
pylab.figure("Clipped dataset in phi_f, alpha_f space")
im = pylab.imshow(numpy.rot90(clip.getArray(), 1), norm=matplotlib.colors.LogNorm(), aspect='auto',
extent=[clip.getAxis(0).getMin(), clip.getAxis(0).getMax(), clip.getAxis(1).getMin(), clip.getAxis(1).getMax()])
pylab.colorbar(im)
pylab.xlabel(r'$\phi_f, rad$', fontsize=16)
pylab.ylabel(r'$\alpha_f, rad$', fontsize=16)
pylab.show()
Examples/python/fitting/ex07_ImportUserData/maria_geometry.png

131 KiB

File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment