Skip to content

Commit

Permalink
Merge pull request #432 from flatironinstitute/dev
Browse files Browse the repository at this point in the history
fixing an error during kde for short traces
  • Loading branch information
epnev authored Jan 11, 2019
2 parents 30daf1e + ef7a259 commit 4ac7d65
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions caiman/utils/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@author: agiovann
"""

import logging
from builtins import range
from past.utils import old_div
try:
Expand Down Expand Up @@ -151,21 +151,28 @@ def df_percentile(inputData, axis = None):
if axis is not None:

def fnc(x): return df_percentile(x)
#import pdb
#pdb.set_trace()
result = np.apply_along_axis(fnc, axis, inputData)
data_prct = result[:,0]
val = result[:,1]
data_prct = result[:, 0]
val = result[:, 1]
else:
# Create the function that we can use for the half-sample mode
bandwidth, mesh, density, cdf = kde(inputData)
err = True
while err:
try:
bandwidth, mesh, density, cdf = kde(inputData)
err = False
except:
logging.warning("There are no components for DF/F extraction!")
if type(inputData) is not list:
inputData = inputData.tolist()
inputData += inputData

data_prct = cdf[np.argmax(density)]*100
val = mesh[np.argmax(density)]

return data_prct, val



"""
An implementation of the kde bandwidth selection method outlined in:
Z. I. Botev, J. F. Grotowski, and D. P. Kroese. Kernel density
Expand Down Expand Up @@ -196,7 +203,7 @@ def kde(data, N=None, MIN=None, MAX=None):

# Histogram the data to get a crude first approximation of the density
M = len(data)
DataHist, bins = sci.histogram(data, bins=N, range=(MIN,MAX))
DataHist, bins = sci.histogram(data, bins=N, range=(MIN, MAX))
DataHist = DataHist/M
DCTData = scipy.fftpack.dct(DataHist, norm=None)

Expand All @@ -218,10 +225,10 @@ def kde(data, N=None, MIN=None, MAX=None):
density = scipy.fftpack.idct(SmDCTData, norm=None)*N/R
mesh = [(bins[i]+bins[i+1])/2 for i in range(N)]
bandwidth = sci.sqrt(t_star)*R

density = density/sci.trapz(density, mesh)
cdf = np.cumsum(density)*(mesh[1]-mesh[0])

return bandwidth, mesh, density, cdf

def fixed_point(t, M, I, a2):
Expand Down

0 comments on commit 4ac7d65

Please sign in to comment.