Local maxima 3D array pythonPythonic data processing? Should I use an iterator?Avoiding regexps abuse in...

Why is the design of haulage companies so “special”?

I probably found a bug with the sudo apt install function

declaring a variable twice in IIFE

A Journey Through Space and Time

How does one intimidate enemies without having the capacity for violence?

How do you conduct xenoanthropology after first contact?

Can Medicine checks be used, with decent rolls, to completely mitigate the risk of death from ongoing damage?

Copycat chess is back

Why can't I see bouncing of a switch on an oscilloscope?

How do I create uniquely male characters?

How is this relation reflexive?

Japan - Plan around max visa duration

N.B. ligature in Latex

How to type dʒ symbol (IPA) on Mac?

Email Account under attack (really) - anything I can do?

What are these boxed doors outside store fronts in New York?

What is the offset in a seaplane's hull?

The magic money tree problem

How old can references or sources in a thesis be?

Shell script can be run only with sh command

How can bays and straits be determined in a procedurally generated map?

What is the white spray-pattern residue inside these Falcon Heavy nozzles?

How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?

What do you call a Matrix-like slowdown and camera movement effect?



Local maxima 3D array python


Pythonic data processing? Should I use an iterator?Avoiding regexps abuse in generation of statements to be executedDepth First Search for percolation to find clusters in Go gamePython Octree ImplementationPython script to store nd array into imagesEllipse-detection algorithmSpeeding up maximum self-similarity test for heavy tail-exponentsTapeEquilibrium Codility implementation not achieving 100%kNN with PythonFuzzy c Means in Python






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







2












$begingroup$


I'm trying to find the local maxima in a 3D numpy array, but I can't seem to find an easy way to do that using numpy, scipy, or anything else.



For now I implemented it using scipy.signal.argrelexrema. But it's very long to process large arrays, and only works on separated axes.



import numpy as np
from scipy.signal import argrelextrema


def local_maxima_3D(data, order=1):
"""Detects local maxima in a 3D array

Parameters
---------
data : 3d ndarray
order : int
How many points on each side to use for the comparison

Returns
-------
coordinates : ndarray
coordinates of the local maxima
values : ndarray
values of the local maxima
"""
# Coordinates of local maxima along each axis
peaks0 = np.array(argrelextrema(data, np.greater, axis=0, order=order))
peaks1 = np.array(argrelextrema(data, np.greater, axis=1, order=order))
peaks2 = np.array(argrelextrema(data, np.greater, axis=2, order=order))

# Stack all coordinates
stacked = np.vstack((peaks0.transpose(), peaks1.transpose(),
peaks2.transpose()))

# We keep coordinates that appear three times (once for each axis)
elements, counts = np.unique(stacked, axis=0, return_counts=True)
coords = elements[np.where(counts == 3)[0]]

# Compute values at filtered coordinates
values = data[coords[:, 0], coords[:, 1], coords[:, 2]]

return coords, values


I know this solution is far from optimal and only works with order=1. Is there any better way to find local maxima in a 3D array in Python?










share|improve this question









New contributor




theobdt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$








  • 2




    $begingroup$
    Do you know anything about the data? If they're determined by a formula, then you should consider getting an analytical answer. Otherwise, the data being continuous, derivable and/or single-maximum would allow for your use of an optimizer, especially if you can provide a gradient function.
    $endgroup$
    – Reinderien
    Apr 1 at 13:54










  • $begingroup$
    As opposed to unique(stacked(...))[where(counts==3)], have you tried set intersection, like set(peaks0) & set(peaks1) & set(peaks2). Of course, the peaks# coordinates would need to be converted into something hashable (tuples) in order to create the sets. I don’t know if this would be faster, so this is an experiment not an answer ...
    $endgroup$
    – AJNeufeld
    Apr 1 at 14:28


















2












$begingroup$


I'm trying to find the local maxima in a 3D numpy array, but I can't seem to find an easy way to do that using numpy, scipy, or anything else.



For now I implemented it using scipy.signal.argrelexrema. But it's very long to process large arrays, and only works on separated axes.



import numpy as np
from scipy.signal import argrelextrema


def local_maxima_3D(data, order=1):
"""Detects local maxima in a 3D array

Parameters
---------
data : 3d ndarray
order : int
How many points on each side to use for the comparison

Returns
-------
coordinates : ndarray
coordinates of the local maxima
values : ndarray
values of the local maxima
"""
# Coordinates of local maxima along each axis
peaks0 = np.array(argrelextrema(data, np.greater, axis=0, order=order))
peaks1 = np.array(argrelextrema(data, np.greater, axis=1, order=order))
peaks2 = np.array(argrelextrema(data, np.greater, axis=2, order=order))

# Stack all coordinates
stacked = np.vstack((peaks0.transpose(), peaks1.transpose(),
peaks2.transpose()))

# We keep coordinates that appear three times (once for each axis)
elements, counts = np.unique(stacked, axis=0, return_counts=True)
coords = elements[np.where(counts == 3)[0]]

# Compute values at filtered coordinates
values = data[coords[:, 0], coords[:, 1], coords[:, 2]]

return coords, values


I know this solution is far from optimal and only works with order=1. Is there any better way to find local maxima in a 3D array in Python?










share|improve this question









New contributor




theobdt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$








  • 2




    $begingroup$
    Do you know anything about the data? If they're determined by a formula, then you should consider getting an analytical answer. Otherwise, the data being continuous, derivable and/or single-maximum would allow for your use of an optimizer, especially if you can provide a gradient function.
    $endgroup$
    – Reinderien
    Apr 1 at 13:54










  • $begingroup$
    As opposed to unique(stacked(...))[where(counts==3)], have you tried set intersection, like set(peaks0) & set(peaks1) & set(peaks2). Of course, the peaks# coordinates would need to be converted into something hashable (tuples) in order to create the sets. I don’t know if this would be faster, so this is an experiment not an answer ...
    $endgroup$
    – AJNeufeld
    Apr 1 at 14:28














2












2








2





$begingroup$


I'm trying to find the local maxima in a 3D numpy array, but I can't seem to find an easy way to do that using numpy, scipy, or anything else.



For now I implemented it using scipy.signal.argrelexrema. But it's very long to process large arrays, and only works on separated axes.



import numpy as np
from scipy.signal import argrelextrema


def local_maxima_3D(data, order=1):
"""Detects local maxima in a 3D array

Parameters
---------
data : 3d ndarray
order : int
How many points on each side to use for the comparison

Returns
-------
coordinates : ndarray
coordinates of the local maxima
values : ndarray
values of the local maxima
"""
# Coordinates of local maxima along each axis
peaks0 = np.array(argrelextrema(data, np.greater, axis=0, order=order))
peaks1 = np.array(argrelextrema(data, np.greater, axis=1, order=order))
peaks2 = np.array(argrelextrema(data, np.greater, axis=2, order=order))

# Stack all coordinates
stacked = np.vstack((peaks0.transpose(), peaks1.transpose(),
peaks2.transpose()))

# We keep coordinates that appear three times (once for each axis)
elements, counts = np.unique(stacked, axis=0, return_counts=True)
coords = elements[np.where(counts == 3)[0]]

# Compute values at filtered coordinates
values = data[coords[:, 0], coords[:, 1], coords[:, 2]]

return coords, values


I know this solution is far from optimal and only works with order=1. Is there any better way to find local maxima in a 3D array in Python?










share|improve this question









New contributor




theobdt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




I'm trying to find the local maxima in a 3D numpy array, but I can't seem to find an easy way to do that using numpy, scipy, or anything else.



For now I implemented it using scipy.signal.argrelexrema. But it's very long to process large arrays, and only works on separated axes.



import numpy as np
from scipy.signal import argrelextrema


def local_maxima_3D(data, order=1):
"""Detects local maxima in a 3D array

Parameters
---------
data : 3d ndarray
order : int
How many points on each side to use for the comparison

Returns
-------
coordinates : ndarray
coordinates of the local maxima
values : ndarray
values of the local maxima
"""
# Coordinates of local maxima along each axis
peaks0 = np.array(argrelextrema(data, np.greater, axis=0, order=order))
peaks1 = np.array(argrelextrema(data, np.greater, axis=1, order=order))
peaks2 = np.array(argrelextrema(data, np.greater, axis=2, order=order))

# Stack all coordinates
stacked = np.vstack((peaks0.transpose(), peaks1.transpose(),
peaks2.transpose()))

# We keep coordinates that appear three times (once for each axis)
elements, counts = np.unique(stacked, axis=0, return_counts=True)
coords = elements[np.where(counts == 3)[0]]

# Compute values at filtered coordinates
values = data[coords[:, 0], coords[:, 1], coords[:, 2]]

return coords, values


I know this solution is far from optimal and only works with order=1. Is there any better way to find local maxima in a 3D array in Python?







python numpy scipy






share|improve this question









New contributor




theobdt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




theobdt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









Jamal

30.6k11121227




30.6k11121227






New contributor




theobdt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Apr 1 at 13:35









theobdttheobdt

111




111




New contributor




theobdt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





theobdt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






theobdt is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 2




    $begingroup$
    Do you know anything about the data? If they're determined by a formula, then you should consider getting an analytical answer. Otherwise, the data being continuous, derivable and/or single-maximum would allow for your use of an optimizer, especially if you can provide a gradient function.
    $endgroup$
    – Reinderien
    Apr 1 at 13:54










  • $begingroup$
    As opposed to unique(stacked(...))[where(counts==3)], have you tried set intersection, like set(peaks0) & set(peaks1) & set(peaks2). Of course, the peaks# coordinates would need to be converted into something hashable (tuples) in order to create the sets. I don’t know if this would be faster, so this is an experiment not an answer ...
    $endgroup$
    – AJNeufeld
    Apr 1 at 14:28














  • 2




    $begingroup$
    Do you know anything about the data? If they're determined by a formula, then you should consider getting an analytical answer. Otherwise, the data being continuous, derivable and/or single-maximum would allow for your use of an optimizer, especially if you can provide a gradient function.
    $endgroup$
    – Reinderien
    Apr 1 at 13:54










  • $begingroup$
    As opposed to unique(stacked(...))[where(counts==3)], have you tried set intersection, like set(peaks0) & set(peaks1) & set(peaks2). Of course, the peaks# coordinates would need to be converted into something hashable (tuples) in order to create the sets. I don’t know if this would be faster, so this is an experiment not an answer ...
    $endgroup$
    – AJNeufeld
    Apr 1 at 14:28








2




2




$begingroup$
Do you know anything about the data? If they're determined by a formula, then you should consider getting an analytical answer. Otherwise, the data being continuous, derivable and/or single-maximum would allow for your use of an optimizer, especially if you can provide a gradient function.
$endgroup$
– Reinderien
Apr 1 at 13:54




$begingroup$
Do you know anything about the data? If they're determined by a formula, then you should consider getting an analytical answer. Otherwise, the data being continuous, derivable and/or single-maximum would allow for your use of an optimizer, especially if you can provide a gradient function.
$endgroup$
– Reinderien
Apr 1 at 13:54












$begingroup$
As opposed to unique(stacked(...))[where(counts==3)], have you tried set intersection, like set(peaks0) & set(peaks1) & set(peaks2). Of course, the peaks# coordinates would need to be converted into something hashable (tuples) in order to create the sets. I don’t know if this would be faster, so this is an experiment not an answer ...
$endgroup$
– AJNeufeld
Apr 1 at 14:28




$begingroup$
As opposed to unique(stacked(...))[where(counts==3)], have you tried set intersection, like set(peaks0) & set(peaks1) & set(peaks2). Of course, the peaks# coordinates would need to be converted into something hashable (tuples) in order to create the sets. I don’t know if this would be faster, so this is an experiment not an answer ...
$endgroup$
– AJNeufeld
Apr 1 at 14:28










0






active

oldest

votes












Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






theobdt is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216655%2flocal-maxima-3d-array-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes








theobdt is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















theobdt is a new contributor. Be nice, and check out our Code of Conduct.













theobdt is a new contributor. Be nice, and check out our Code of Conduct.












theobdt is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216655%2flocal-maxima-3d-array-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Fairchild Swearingen Metro Inhaltsverzeichnis Geschichte | Innenausstattung | Nutzung | Zwischenfälle...

Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...

Marineschifffahrtleitung Inhaltsverzeichnis Geschichte | Heutige Organisation der NATO | Nationale und...