Cleanup, import sort, formatting

This commit is contained in:
usov_i 2020-10-14 15:32:38 +02:00
parent 6886279884
commit 0777d73566
8 changed files with 37 additions and 138 deletions

View File

@ -1,8 +1,8 @@
from pyzebra.anatric import *
from pyzebra.h5 import *
from pyzebra.xtal import *
from pyzebra.load_1D import load_1D, parse_1D
from pyzebra.ccl_findpeaks import ccl_findpeaks
from pyzebra.fit2 import fitccl
from pyzebra.comm_export import export_comm
import pyzebra.ccl_dict_operation
from pyzebra.anatric import *
from pyzebra.ccl_findpeaks import ccl_findpeaks
from pyzebra.comm_export import export_comm
from pyzebra.fit2 import fitccl
from pyzebra.h5 import *
from pyzebra.load_1D import load_1D, parse_1D
from pyzebra.xtal import *

View File

@ -1,19 +0,0 @@
import pickle
def save_dict(obj, name):
""" saves dictionary as pickle file in binary format
:arg obj - object to save
:arg name - name of the file
NOTE: path should be added later"""
with open(name + '.pkl', 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
def load_dict(name):
"""load dictionary from picle file
:arg name - name of the file to load
NOTE: expect the file in the same folder, path should be added later
:return dictionary"""
with open(name + '.pkl', 'rb') as f:
return pickle.load(f)

View File

@ -1,5 +1,6 @@
import numpy as np
import uncertainties as u
from .fit2 import create_uncertanities

View File

@ -1,16 +1,20 @@
import numpy as np
import uncertainties as u
from lmfit import Model, Parameters
from scipy.integrate import simps
import uncertainties as u
def bin_data(array, binsize):
if isinstance(binsize, int) and 0 < binsize < len(array):
return [np.mean(array[binsize * i:binsize * i + binsize]) for i in range(int(
np.ceil(len(array) / binsize)))]
return [
np.mean(array[binsize * i : binsize * i + binsize])
for i in range(int(np.ceil(len(array) / binsize)))
]
else:
print("Binsize need to be positive integer smaller than lenght of array")
return array
def find_nearest(array, value):
# find nearest value and return index
array = np.asarray(array)
@ -36,7 +40,7 @@ def fitccl(
constraints_max,
numfit_min=None,
numfit_max=None,
binning=None
binning=None,
):
"""Made for fitting of ccl date where 1 peak is expected. Allows for combination of gaussian and linear model combination
:param data: dictionary after peak fining
@ -75,17 +79,16 @@ def fitccl(
x = bin_data(x, binning)
y = list(meas["Counts"])
y_err = list(np.sqrt(y)) if meas.get("sigma", None) is None else list(meas["sigma"])
combined = bin_data(create_uncertanities(y,y_err), binning)
combined = bin_data(create_uncertanities(y, y_err), binning)
y = [combined[i].n for i in range(len(combined))]
y_err = [combined[i].s for i in range(len(combined))]
if len(meas["peak_indexes"]) == 0:
# Case for no peak, gaussian in centre, sigma as 20% of range
print("No peak")
peak_index = find_nearest(x, np.mean(x))
guess[0] = x[int(peak_index)] if guess[0] is None else guess[0]
guess[1] = (x[-1] - x[0])/5 if guess[1] is None else guess[1]
guess[1] = (x[-1] - x[0]) / 5 if guess[1] is None else guess[1]
guess[2] = 50 if guess[2] is None else guess[2]
guess[3] = 0 if guess[3] is None else guess[3]
guess[4] = np.mean(y) if guess[4] is None else guess[4]
@ -103,8 +106,6 @@ def fitccl(
constraints_min[0] = np.min(x) if constraints_min[0] is None else constraints_min[0]
constraints_max[0] = np.max(x) if constraints_max[0] is None else constraints_max[0]
def gaussian(x, g_cen, g_width, g_amp):
"""1-d gaussian: gaussian(x, amp, cen, wid)"""
return (g_amp / (np.sqrt(2 * np.pi) * g_width)) * np.exp(
@ -119,45 +120,15 @@ def fitccl(
params = Parameters()
params.add_many(
("g_cen", x[int(peak_index)], bool(vary[0]), np.min(x), np.max(x), None, None),
(
"g_width",
guess[1],
bool(vary[1]),
constraints_min[1],
constraints_max[1],
None,
None,
),
(
"g_amp",
guess[2],
bool(vary[2]),
constraints_min[2],
constraints_max[2],
None,
None,
),
(
"slope",
guess[3],
bool(vary[3]),
constraints_min[3],
constraints_max[3],
None,
None,
),
(
"intercept",
guess[4],
bool(vary[4]),
constraints_min[4],
constraints_max[4],
None,
None,
),
("g_width", guess[1], bool(vary[1]), constraints_min[1], constraints_max[1], None, None,),
("g_amp", guess[2], bool(vary[2]), constraints_min[2], constraints_max[2], None, None,),
("slope", guess[3], bool(vary[3]), constraints_min[3], constraints_max[3], None, None,),
("intercept", guess[4], bool(vary[4]), constraints_min[4], constraints_max[4], None, None,),
)
# the weighted fit
result = mod.fit(y, params, weights=[np.abs(1/y_err[i]) for i in range(len(y_err))], x=x, calc_covar=True)
result = mod.fit(
y, params, weights=[np.abs(1 / y_err[i]) for i in range(len(y_err))], x=x, calc_covar=True
)
# u.ufloat to work with uncertanities
fit_area = u.ufloat(result.params["g_amp"].value, result.params["g_amp"].stderr)
comps = result.eval_components()
@ -183,7 +154,8 @@ def fitccl(
it = -1
while abs(numfit_max - numfit_min) < 3:
# in the case the peak is very thin and numerical integration would be on zero omega difference, finds closes values
# in the case the peak is very thin and numerical integration would be on zero omega
# difference, finds closes values
it = it + 1
numfit_min = find_nearest(
x,

View File

@ -22,7 +22,7 @@ def parse_h5meta(file):
for line in file:
line = line.strip()
if line.startswith("#begin "):
section = line[len("#begin "):]
section = line[len("#begin ") :]
content[section] = []
elif line.startswith("#end"):
@ -64,20 +64,3 @@ def read_detector_data(filepath):
det_data["temperature"] = h5f["/entry1/sample/temperature"][:]
return det_data
def open_h5meta(filepath):
"""Open h5meta file like *.cami
Args:
filepath (str): File path of a h5meta file.
Returns:
dict: A dictionary with h5 names and their detector data and angles.
"""
data = dict()
h5meta_content = read_h5meta(filepath)
for file in h5meta_content["filelist"]:
data[file] = read_detector_data(file)
return data

View File

@ -97,7 +97,7 @@ def parse_1D(fileobj, data_type):
# read data
if data_type == ".ccl":
metadata['data_type'] = data_type
metadata["data_type"] = data_type
measurements = {}
decimal = list()
data = fileobj.readlines()
@ -162,7 +162,7 @@ def parse_1D(fileobj, data_type):
elif data_type == ".dat":
# skip the first 2 rows, the third row contans the column names
metadata['data_type'] = data_type
metadata["data_type"] = data_type
next(fileobj)
next(fileobj)
col_names = next(fileobj).split()

View File

@ -1,10 +1,11 @@
from load_1Dnn import load_1D
from ccl_dict_operation import add_dict
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D # dont delete, otherwise waterfall wont work
import matplotlib.pyplot as plt
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from load_1Dnn import load_1D
from mpl_toolkits.mplot3d import Axes3D # dont delete, otherwise waterfall wont work
from ccl_dict_operation import add_dict
def load_dats(filepath):

View File

@ -1,39 +0,0 @@
plt.figure(figsize=(20, 10))
plt.plot(x, y, "b-", label="Original data")
plt.plot(x, comps["gaussian"], "r--", label="Gaussian component")
plt.fill_between(x, comps["gaussian"], facecolor="red", alpha=0.4)
plt.plot(x, comps["background"], "g--", label="Line component")
plt.fill_between(x, comps["background"], facecolor="green", alpha=0.4)
plt.fill_between(
x[numfit_min:numfit_max],
y[numfit_min:numfit_max],
facecolor="yellow",
alpha=0.4,
label="Integrated area", )
plt.plot(x, result.best_fit, "k-", label="Best fit")
plt.title(
"%s \n Gaussian: centre = %9.4f, sigma = %9.4f, area = %9.4f \n"
"background: slope = %9.4f, intercept = %9.4f \n"
"Int. area = %9.4f +/- %9.4f \n"
"fit area = %9.4f +/- %9.4f \n"
"ratio((fit-int)/fit) = %9.4f"
% (
keys,
result.params["g_cen"].value,
result.params["g_width"].value,
result.params["g_amp"].value,
result.params["slope"].value,
result.params["intercept"].value,
int_area.n,
int_area.s,
result.params["g_amp"].value,
result.params["g_amp"].stderr,
(result.params["g_amp"].value - int_area.n) / result.params["g_amp"].value
)
)
plt.legend(loc="best")
plt.show()