added small things to utilities
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import yaml
|
||||
import os
|
||||
|
||||
import numpy as np
|
||||
from sfdata import SFDataFiles, sfdatafile, SFScanInfo
|
||||
from xraydb import material_mu
|
||||
|
||||
|
||||
def print_run_info(
|
||||
@@ -136,7 +137,7 @@ def heuristic_extract_smalldata_path():
|
||||
|
||||
######################## Little useful functions ########################
|
||||
|
||||
def find_nearest(array, value):
|
||||
def find_nearest(array, value):
|
||||
'''Finds an index in an array with a value that is nearest to given number'''
|
||||
array = np.asarray(array)
|
||||
idx = (np.abs(array - value)).argmin()
|
||||
@@ -154,7 +155,7 @@ def gauss(x, H, A, x0, sigma):
|
||||
"""Returns gauss function value"""
|
||||
return H + A * np.exp(-(x - x0) ** 2 / (2 * sigma ** 2))
|
||||
|
||||
def gauss_fit(x, y, fit_details=None, plot=None:
|
||||
def gauss_fit(x, y, fit_details=None, plot=None):
|
||||
'''Returns [baseline_offset, Amplitude, center, sigma, FWHM]'''
|
||||
|
||||
# Initial guesses
|
||||
@@ -191,6 +192,20 @@ def gauss_fit(x, y, fit_details=None, plot=None:
|
||||
|
||||
return popt
|
||||
|
||||
def xray_transmission(energy,thickness,material='Si',density=[]):
|
||||
'''Calculate x-ray tranmission for given energy, thickness and material. Default material is Si. Add material=element as a string for another material. Density as optional parameter'''
|
||||
|
||||
mu = material_mu(material, energy)
|
||||
|
||||
if density == []:
|
||||
mu_array = material_mu(material, energy)
|
||||
else:
|
||||
mu_array = material_mu(formula, energy, density=density)
|
||||
|
||||
trans = np.exp(-0.1*(thickness*1000)*mu_array) # 0.1 is beccause mu is in 1/cm, thickness converted to mm from m
|
||||
|
||||
return trans
|
||||
|
||||
######################## Unit conversions ########################
|
||||
|
||||
def joules_to_eV(joules):
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import pytest
|
||||
import os
|
||||
|
||||
from cristallina.utils import ROI, print_run_info, heuristic_extract_pgroup, gauss
|
||||
import numpy as np
|
||||
from cristallina.utils import ROI, print_run_info, heuristic_extract_pgroup, gauss, find_nearest, xray_transmission
|
||||
|
||||
__author__ = "Alexander Steppke"
|
||||
|
||||
@@ -33,4 +33,10 @@ def test_gauss():
|
||||
def test_find_nearest():
|
||||
# Make an array of 1 to 100 and check that the nearest index is the value
|
||||
a = np.linspace(0,99,100)
|
||||
assert find_nearest(a,10.1) == 10
|
||||
assert find_nearest(a,10.1) == 10
|
||||
|
||||
def test_xray_transmission():
|
||||
T = xray_transmission(9000,100e-6,material='Si')
|
||||
assert T == 0.342385039732607
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user