Skip to content

Instantly share code, notes, and snippets.

@MSeifert04
Created January 18, 2017 18:44
Show Gist options
  • Save MSeifert04/4ba7ed006428a0226a9d5b34b1bca1f0 to your computer and use it in GitHub Desktop.
Save MSeifert04/4ba7ed006428a0226a9d5b34b1bca1f0 to your computer and use it in GitHub Desktop.
from itertools import groupby
import math
import numpy as np
import numba as nb
def Divakar(a):
# Mask of NaNs
mask = np.concatenate(([False],np.isnan(a),[False]))
if ~mask.any():
return 0
else:
# Count of NaNs in each NaN group. Then, get max count as o/p.
c = np.flatnonzero(mask[1:] < mask[:-1]) - \
np.flatnonzero(mask[1:] > mask[:-1])
return c.max()
def Divakar2(a):
mask = np.concatenate(([False],np.isnan(a),[False]))
if ~mask.any():
return 0
else:
idx = np.nonzero(mask[1:] != mask[:-1])[0]
return (idx[1::2] - idx[::2]).max()
@nb.njit
def mine(arr):
max_ = 0
current = 0
idx = 0
while idx < arr.size:
while idx < arr.size and math.isnan(arr[idx]):
current += 1
idx += 1
if current > max_:
max_ = current
current = 0
idx += 1
return max_
def Tagc_special(sequence):
# This doesn't work with numpy arrays
return max((sum(1 for _ in group) for key, group in groupby(sequence) if key is nan), default=0)
def Tagc_other(sequence):
# This doesn't work with numpy arrays
return max((sum(1 for _ in group) for key, group in groupby(np.isnan(sequence)) if key), default=0)
def pltrdy(arr):
count, max_count = 0, 0
for e in arr:
if np.isnan(e):
count += 1
max_count = max(max_count, count)
else:
count = 0
return max_count
def Kasramvd(arr):
arr = np.isnan(arr)
return max(i.size for i in np.split(arr, np.where(np.diff(arr) != 0)[0]+1) if i.any())
from numpy import nan
arr = np.random.rand(10000)
arr[np.random.choice(range(len(arr)),size=1000,replace=0)] = np.nan
mine(arr) # numba needs one call to compile the function, so do a warmup
%timeit mine(arr)
%timeit Divakar2(arr)
%timeit Divakar(arr)
%timeit Tagc_special(arr)
%timeit Tagc_other(arr)
%timeit Kasramvd(arr)
%timeit pltrdy(arr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment