Skip to content

Instantly share code, notes, and snippets.

@CJ-Wright

CJ-Wright/pol.py Secret

Created October 4, 2016 14:22
Show Gist options
  • Save CJ-Wright/7e1e5ad19e078bc013772c06bc03c637 to your computer and use it in GitHub Desktop.
Save CJ-Wright/7e1e5ad19e078bc013772c06bc03c637 to your computer and use it in GitHub Desktop.
polarization correction fitting
import numpy as np
from pyFAI.geometry import Geometry
from scipy.optimize import minimize_scalar
import scipy.stats as sts
import tifffile
import matplotlib.pyplot as plt
from xpdan.tools import *
from skimage.morphology import erosion, binary_erosion
import os
import pyFAI
plt.style.use(
os.path.join('/media/christopher/DATA/papers/mask_paper/figures/',
'thesis.mplstyle'))
img = tifffile.imread(
'/media/christopher/DATA/WrightJun16/Quartz/d25_glass-00000-00004.tif'
# '/media/christopher/DATA/WrightJun16/Quartz/calib/d25_CeO2-00000.tif'
)
geo = Geometry(
detector='Perkin', pixel1=.0002, pixel2=.0002,
dist=.23,
poni1=.209, poni2=.207,
# rot1=.0128, rot2=-.015, rot3=-5.2e-8,
wavelength=1.43e-11
)
geo = pyFAI.load('/media/christopher/DATA/WrightJun16/Quartz/calib/d25_CeO2-00000.poni')
npt = 1041
int_rings = range(npt)
q = geo.qArray(img.shape)
q_range = np.linspace(np.min(q), np.max(q), npt)
int_q = np.digitize(q, q_range)
chi = geo.chiArray(img.shape)
for i in range(2):
pol = None
img3 = img.copy()
if pol:
img3 /= geo.polarization(img.shape, .95)
# mask = mask_img(img3, geo, alpha=3, bs_width=None)
mask = None
def opt(polarization_factor):
cimg = img.copy()
cimg /= geo.polarization(img.shape, polarization_factor)
if mask is None:
rq = q.ravel()
cimg = cimg.ravel()
else:
rq = q[mask]
cimg = cimg[mask]
std = sts.binned_statistic(rq, cimg, bins=q_range, statistic='std')[0]
std = np.nan_to_num(std)
return np.sum(std)
res =minimize_scalar(opt)
print(res.x)
pol = res.x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment