-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from neuromusic/detectors
implements edge detection
- Loading branch information
Showing
8 changed files
with
208 additions
and
24 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import numpy as np | ||
import pandas as pd | ||
from sklearn.base import TransformerMixin, BaseEstimator | ||
from sklearn.preprocessing import binarize | ||
|
||
class Binarizer(BaseEstimator, TransformerMixin): | ||
"""docstring for scikit learn Binarizer | ||
""" | ||
|
||
def __init__(self, threshold=0.0, copy=True): | ||
self.threshold = threshold | ||
self.copy = copy | ||
|
||
def fit(self, X, y=None): | ||
return self | ||
|
||
def transform(self, X): | ||
df = True | ||
try: | ||
index = X.index | ||
columns = X.columns | ||
except AttributeError: | ||
df = False | ||
|
||
X_ = binarize(X, threshold=self.threshold, copy=self.copy) | ||
|
||
if df: | ||
return pd.DataFrame(data=X_,index=index,columns=columns) | ||
else: | ||
return X_ | ||
|
||
|
||
|
||
def edge_detector(X,falling=False): | ||
|
||
df = True | ||
try: | ||
index = X.index | ||
columns = X.columns | ||
except AttributeError: | ||
df = False | ||
|
||
X = np.apply_along_axis( | ||
func1d=np.diff, | ||
axis=0, | ||
arr=X.copy(), | ||
) | ||
empty_row = np.zeros(shape=(1,X.shape[1]),dtype=X.dtype) | ||
X = np.vstack((empty_row,X)) | ||
|
||
if falling: | ||
X = X < 0 | ||
else: | ||
X = X > 0 | ||
|
||
X = X.astype(int) | ||
|
||
if df: | ||
return pd.DataFrame(data=X,index=index,columns=columns) | ||
else: | ||
return X | ||
|
||
class EdgeDetector(BaseEstimator,TransformerMixin): | ||
"""docstring for EdgeDetector.""" | ||
def __init__(self, falling=False): | ||
self.falling = falling | ||
|
||
def fit(self,X,y=None): | ||
return self | ||
|
||
def transform(self,X): | ||
|
||
return edge_detector(X,self.falling) | ||
|
||
class WhenTrueFinder(BaseEstimator,TransformerMixin): | ||
"""docstring for WhenTrueFinder.""" | ||
def __init__(self): | ||
pass | ||
|
||
def fit(self,X,y=None): | ||
return self | ||
|
||
def transform(self,X): | ||
return (X[X > 0] | ||
.stack() | ||
.reset_index()[['level_0','level_1']] | ||
.rename(columns={'level_0':'time','level_1':'neuron'}) | ||
) |
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
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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import numpy as np | ||
import numpy.testing as npt | ||
|
||
import pandas as pd | ||
import pandas.testing as pdt | ||
|
||
from neuroglia.trace import EdgeDetector, WhenTrueFinder, Binarizer | ||
|
||
X = np.array( | ||
[[0, 0, 1], | ||
[1, 1, 0], | ||
[0, 1, 1] | ||
]) | ||
|
||
XDF = pd.DataFrame( | ||
data=X, | ||
index=[0.1,0.2,0.3], | ||
columns=['n1','n2','n3'], | ||
) | ||
|
||
RISING = np.array( | ||
[[0, 0, 0], | ||
[1, 1, 0], | ||
[0, 0, 1], | ||
] | ||
) | ||
|
||
DF = pd.DataFrame( | ||
data=RISING, | ||
index=[0.1,0.2,0.3], | ||
columns=['n1','n2','n3'], | ||
) | ||
|
||
FALLING = np.array( | ||
[[0, 0, 0], | ||
[0, 0, 1], | ||
[1, 0, 0], | ||
] | ||
) | ||
|
||
def test_EdgeDetector(): | ||
detector = EdgeDetector() | ||
output = detector.fit_transform(X) | ||
npt.assert_array_equal(output,RISING) | ||
|
||
detector = EdgeDetector(falling=True) | ||
output = detector.fit_transform(X) | ||
npt.assert_array_equal(output,FALLING) | ||
|
||
detector = EdgeDetector() | ||
output = detector.fit_transform(XDF) | ||
npt.assert_array_equal(output.values,RISING) | ||
print(output) | ||
print(DF) | ||
pdt.assert_frame_equal(output,DF) | ||
|
||
|
||
WHENTRUE = pd.DataFrame(dict( | ||
neuron=['n1','n2','n3'], | ||
time=[0.2,0.2,0.3], | ||
)).set_index('time') | ||
|
||
def test_WhenTrueFinder(): | ||
finder = WhenTrueFinder() | ||
output = finder.fit_transform(DF) | ||
output = output.sort_values(['time','neuron']).set_index('time') | ||
print(output) | ||
print(DF) | ||
pdt.assert_frame_equal(output,WHENTRUE) | ||
|
||
X2 = np.array( | ||
[[0, 0, 1], | ||
[1, 2, 0], | ||
[0, 1, 10] | ||
]) | ||
|
||
X2DF = pd.DataFrame( | ||
data=X, | ||
index=[0.1,0.2,0.3], | ||
columns=['n1','n2','n3'], | ||
) | ||
|
||
def test_Binarizer(): | ||
binarizer = Binarizer() | ||
output = binarizer.fit_transform(X2) | ||
npt.assert_array_equal(output,X) | ||
|
||
binarizer = Binarizer() | ||
output = binarizer.fit_transform(X2DF) | ||
pdt.assert_frame_equal(output,XDF) |