Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions xpdtools/pipelines/qoi.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np

from streamz import collect
from toolz import get

from xpdtools.tools import avg_curvature,group_pearson,complete_pearson

def max_intensity_mean(mean, q, **kwargs):
q_at_mean_max = (
Expand All @@ -21,6 +21,13 @@ def max_gr_mean(pdf, **kwargs):
return locals()


def fluc_high_q(iq_comp, high_q_val=45, **kwargs):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this need docstrings.

q = iq_comp.pluck(0)
iq = iq_comp.pluck(1)
high_q_fluc = avg_curvature(iq, q, high_q_val)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might use the q and mean and combine them, see: https://github.com/xpdAcq/xpdtools/blob/master/xpdtools/pipelines/raw_pipeline.py#L340
This needs to run through a starmap.

return locals()


"""
r = pdf.pluck(0)
true_pdf = pdf.pluck(1)
Expand Down
41 changes: 41 additions & 0 deletions xpdtools/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
generate_map_bin,
generate_binner,
move_center,
avg_curvature,
complete_pearson,
group_pearson
)
from xpdtools.jit_tools import mask_ring_median, mask_ring_mean

Expand Down Expand Up @@ -131,3 +134,41 @@ def test_move_center():
g2 = move_center(m, geo)
for mm, n in zip(m, ["poni1", "poni2"]):
assert getattr(g2, n) == getattr(geo, n) + mm


def test_avg_curvature():
t = np.linspace(0,20*np.pi, 100)

def damp_sin(t, damp_coef):
return 5 * np.sin(t)*np.e**(-damp_coef*t)

assert avg_curvature(damp_sin(t, .05), t, 40) \
< avg_curvature(damp_sin(t, .03), t, 40)


def test_complete_pearson():
x_dif = []
x_same = []
t = np.linspace(0, 200)
y = np.sin(5 * t)
for i in np.arange(20)[1:]:
x_dif.append(np.sin(i * t))
x_same.append(np.sin(5 * t))
dif = complete_pearson(x_dif, y)
same = complete_pearson(x_same, y)
assert (np.array_equal(same, np.ones(len(x_same)))) \
and not (np.array_equal(same, dif))


def test_group_pearson():
x_dif = []
x_same = []
t = np.linspace(0, 200)
for i in np.arange(20)[1:]:
x_dif.append(np.sin(i * t))
x_same.append(np.sin(5 * t))
dif = group_pearson(x_dif)
same = group_pearson(x_same)
same = np.asarray(same)
assert (np.array_equal(same, np.ones(same.shape))) \
and not (np.array_equal(same, dif))
79 changes: 79 additions & 0 deletions xpdtools/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import numpy as np
from scipy.integrate import simps
import scipy.stats as stats
from skbeam.core.accumulators.binned_statistic import BinnedStatistic1D
from skbeam.core.mask import margin
from xpdtools.jit_tools import mask_ring_median, mask_ring_mean, ring_zscore
Expand Down Expand Up @@ -580,3 +581,81 @@ def inner(x, *args, **kwargs):
return func(*args, **kwargs)

return inner


def avg_curvature(y, x, high_val):
"""Computes the average of the curvature of a given signal beyond a
(currently) user provided high Q value, this will be used as the scalar
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't say what this will be used for. If it is just for that qoi it shouldn't be a separate function. If it has more broad application, it should be its own function but the docstring should just describe what it does.

representation for fluctuations at high Q values.

Parameters
----------
y : ndarray
Dependent variable of a given signal
x : ndarray
Independent variable of a given signal
high_val : float
User-defined value start point of analysis. This should remain constant
over a scan.

Returns
-------
float :
The average of curvature beyond a given high_val

"""
idx = np.abs(x - high_val).argmin()
first_der = np.gradient(y[idx:], x[idx:])
sec_der = np.gradient(first_der, x[idx:])
return np.average(np.abs(sec_der)/(1+first_der**2)**(3/2))


def complete_pearson(group, y):
""" Computes a array of scalars representing the similarity of a given
dataset to others in a group. This function returns an array of Pearson
Product-Moment Correlation coefficients corresponding to the inputed dataset
and every member of the inputed group.

Parameters
----------
y : ndarray
data that is to be compared to rest of the group.

group : ndarray
group of data to be compared to.
Returns
-------
ndarray :
Array of Pearson's Correlation coefficients, index corresponds to
the index of group member.

"""
pearsons = []
for i in group:
r, p = stats.pearsonr(y, i)
pearsons.append(r)
pearsons = np.asarray(pearsons)
return pearsons


def group_pearson(group):
""" This function returns an array of an array of Pearson Product-Moment
Correlation coefficients corresponding the similarity between members in the
group.

Parameters
----------
group : ndarray
group of data to be compared.
Returns
-------
ndarray :
Array of arrays Pearson's Correlation coefficients, index corresponds to
index of group member.

"""
pearson_list = []
for i, x in enumerate(group):
pearson_list.append(complete_pearson(group, x))
pearson_list = np.asarray(pearson_list)
return pearson_list