-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
find_sample_from_2dscan #57
Open
MehmetTopsakal
wants to merge
4
commits into
xpdAcq:master
Choose a base branch
from
MehmetTopsakal:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,9 @@ | |
from concurrent.futures import ThreadPoolExecutor, as_completed | ||
from functools import wraps | ||
|
||
from sklearn.cluster import DBSCAN | ||
from scipy.stats import spearmanr | ||
|
||
try: | ||
from diffpy.pdfgetx import PDFGetter | ||
except ImportError: | ||
|
@@ -581,3 +584,89 @@ def inner(x, *args, **kwargs): | |
return func(*args, **kwargs) | ||
|
||
return inner | ||
|
||
|
||
|
||
def find_sample_from_2dscan(I_arr, xy_arr, Q_arr=None, | ||
eps=0.05, min_samples=20, n_jobs=1, | ||
b_ratio_thres=0.5, qrange=(1,5), use_unclassified=True): | ||
"""Find sample positions from xy-scan | ||
|
||
Parameters | ||
---------- | ||
xy_arr : x,y of scan points | ||
I_arr : Intensities for each scan point | ||
Q_arr : Q-points (optional) | ||
|
||
See DBSCAN documentation: | ||
http://scikit-learn.org/stable/modules/generated/sklearn.cluster.DBSCAN.html | ||
|
||
eps: The maximum distance between two samples for them to | ||
be considered as in the same neighborhood. | ||
|
||
min_samples: The number of samples (or total weight) | ||
in a neighborhood for a point to be considered as a core point. | ||
This includes the point itself. | ||
|
||
n_jobs: The number of parallel jobs to run. None means 1 | ||
unless in a joblib.parallel_backend context. | ||
-1 means using all processors. | ||
|
||
b_ratio_thres: Clusters more than this threshold will be | ||
condidered as background (not belonging to sample). | ||
|
||
use_unclassified: Sometimes DBSCAN is unable to classify | ||
points around sample boundary. It that case, it gives -1. | ||
If this keyword is True, that point is considered in the | ||
sample positions (pts). | ||
|
||
|
||
Returns | ||
------- | ||
center : ndarray | ||
xy coordinates of the center of the sample | ||
pts : ndarray | ||
xy coordinates of points considered within the sample | ||
|
||
""" | ||
|
||
if isinstance(Q_arr,np.ndarray): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this might be able to be |
||
# Trim to selected Q range. Because we do not want mess around | ||
# beam stopper and high q. This also speedups DBSCAN calculation | ||
sel = (Q_arr > qrange[0]) & (Q_arr < qrange[1]) | ||
I_arr = np.array([i[sel] for i in I_arr]) | ||
else: | ||
print('Q array is not provided. Using all points') | ||
|
||
|
||
# Use DBSCAN package to cluster I_arr | ||
dbs = DBSCAN(eps, min_samples=min_samples, | ||
metric=lambda i, j: 1 - spearmanr(i, j)[0], n_jobs=n_jobs) | ||
preds = dbs.fit_predict(np.array(I_arr)) | ||
uniques, counts = np.unique(preds, return_counts=True) | ||
ratios = counts / sum(counts) | ||
|
||
# Collect x,y data for determining points which should correspond to the sample. | ||
pts = [] | ||
|
||
for j,u in enumerate(uniques): | ||
|
||
mask = (preds == u) | ||
masked = [] | ||
for i,tf in enumerate(mask): | ||
if tf: | ||
masked.append(xy_arr[i]) | ||
|
||
if u == -1: | ||
if use_unclassified: | ||
pts.extend(masked) | ||
else: | ||
if (ratios[j] <= b_ratio_thres): | ||
pts.extend(masked) | ||
pts = np.array(pts) | ||
|
||
center = np.mean(pts, axis=0) | ||
|
||
# TODO: Get rid of s_ratio | ||
|
||
return center, pts |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a little confused about whether this is in the right place. It seems that most things in
xpdtools.tools
are fairly low-level functions for the functioning of the pipelines. On the other hand, I think this is some kind of script for actually doing xy scans to find a sample? Or is it for analyzing 2d data from such a scan to find positions of the sample given the data? I am afraid that if we don't hae the right structure to our files and projects it will become a maintenance (and user) nightmare. Can we have a discussion (maybe a call) to discuss some of these issues? I would like to define different activities and group code accordingly.