Skip to content

Instantly share code, notes, and snippets.

@antnieszka
Created October 4, 2016 01:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antnieszka/45b998b7d0d709e7cd93d5bf8c4f28d0 to your computer and use it in GitHub Desktop.
Save antnieszka/45b998b7d0d709e7cd93d5bf8c4f28d0 to your computer and use it in GitHub Desktop.
import numpy as np
import scipy.optimize
import beprof.profile
import logging
# logging = logging.getLogger()
# logging.setLevel(30)
# nbps = 3
bp = beprof.profile.Profile([(0, 0), (1, 0), (2, 0.2), (3, 0.5), (4, 1), (5, 0.2), (6, 0), (7, 0)])
# mesh = {3: 0, 3.1: 0, 3.2: 0, 3.3: 0, 3.4: 0, 3.5: 0, 3.6: 0, 3.7: 0, 3.8: 0, 3.9: 0, 4: 0}
# 3.0 - 4.0, every 0.05
mesh = np.arange(3, 4, 0.05)
# {pos: height}
# ph = {3: 0.5, 3.5: 0.5, 4: 0.5}
ph = {5.5: 1}
kle = beprof.profile.Profile(np.array([bp.x, bp.y*0.5]).T)
double_peak = beprof.profile.Profile(np.array([bp.x, bp.y + kle.y]).T)
# normalize?
double_peak.rescale(double_peak.y.max())
# print("bpx", bp.x)
# print("klx", double_peak.x)
# print("bpy", bp.y)
# print("kly", double_peak.y)
# print((bp*0.5).y)
# print(kle.y_at_x(1.7))
# exit(0)
#####################################################################
class BraggPeak:
def __init__(self, bragg_peak, position, height):
self.peak = bragg_peak
self.position = position
self.height = height
# todo: this should be "class return", not separate method
def data_dict(self):
return {"bp": self.peak, "properties": {"position": self.position, "height": self.height}}
class SOBP:
def __init__(self, peak_list):
if isinstance(peak_list, list):
self.sobp = self.calc_sobp(peak_list)
else:
raise TypeError("List object required!")
@staticmethod
def calc_sobp(plist):
temp_peak = None
for p in plist:
# todo: multiplying by coefficients and shifting(?)
if temp_peak is not None:
if np.array_equal(temp_peak.x, p.data_dict()["bp"].x):
temp_peak.y += p.data_dict()["bp"].y * p.data_dict()["properties"]["height"]
else:
raise ValueError("Inconsistent domains!")
else:
temp_peak = p.data_dict()["bp"]
temp_peak.y *= p.data_dict()["properties"]["height"]
temp_peak.rescale(temp_peak.y.max())
return temp_peak
def val(self, x):
return self.sobp.y_at_x(x)
def calc_on_mesh(self, mesh):
res = 0
for m in mesh:
temp = (self.val(m) - 1) ** 2
if str(temp) != 'nan':
res += temp
else:
raise ValueError("Got 'nan' instead of number!")
return res
w = BraggPeak(double_peak, 1, 0.5)
q = BraggPeak(bp, 2, 0.7)
d = SOBP([w, q, w])
print(d.sobp)
print(d.sobp.x)
print(d.sobp.y)
print(d.calc_on_mesh(mesh))
exit(0)
#####################################################################
peak = d
print("Res =", peak.val(0))
print("Res =", peak.val(1))
print("Res =", peak.val(2))
print("Res =", peak.val(3))
print("Res =", peak.val(4))
target = 0
for m in mesh:
# print("For point:", m)
temp = (peak.val(m) - 1)**2
if str(temp) != 'nan':
target += temp
else:
raise ValueError("Got 'nan' instead of number!")
# print("Val:", peak.val(m), "\n")
print("target:", target)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment