Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def compute_density(x, weight, range, **params):
x = np.asarray(x, dtype=np.float)
not_nan = ~np.isnan(x)
x = x[not_nan]
bw = params['bw']
n = len(x)
if n == 0 or (n == 1 and isinstance(bw, str)):
if n == 1:
warn("To compute the density of a group with only one "
"value set the bandwidth manually. e.g `bw=0.1`",
PlotnineWarning)
warn("Groups with fewer than 2 data points have been removed.",
PlotnineWarning)
return pd.DataFrame()
if weight is None:
weight = np.ones(n) / n
# kde is computed efficiently using fft. But the fft does
# not support weights and is only available with the
# gaussian kernel. When weights are relevant we
# turn off the fft.
if params['kernel'] == 'gau':
fft, weight = True, None
else:
fft = False
def setup_data(self, data):
gp = self.params
sp = self._stat.params
# Issue warnings when parameters don't make sense
if gp['position'] == 'stack':
warn("position='stack' doesn't work properly with "
"geom_dotplot. Use stackgroups=True instead.",
PlotnineWarning)
if (gp['stackgroups'] and
sp['method'] == 'dotdensity' and
sp['binpositions'] == 'bygroup'):
warn("geom_dotplot called with stackgroups=TRUE and "
"method='dotdensity'. You probably want to set "
"binpositions='all'", PlotnineWarning)
if 'width' not in data:
if sp['width']:
data['width'] = sp['width']
else:
data['width'] = resolution(data['x'], False) * 0.9
# Set up the stacking function and range
if gp['stackdir'] in (None, 'up'):
def stackdots(a):
mapping, data = order_as_mapping_data(mapping, data)
slope = kwargs.pop('slope', None)
intercept = kwargs.pop('intercept', None)
# If nothing is set, it defaults to y=x
if mapping is None and slope is None and intercept is None:
slope = 1
intercept = 0
if slope is not None or intercept is not None:
if mapping:
warn("The 'intercept' and 'slope' when specified override "
"the aes() mapping.", PlotnineWarning)
if data is not None:
warn("The 'intercept' and 'slope' when specified override "
"the data", PlotnineWarning)
if slope is None:
slope = 1
if intercept is None:
intercept = 0
data = pd.DataFrame({
'intercept': make_iterable(intercept),
'slope': slope
})
mapping = aes(intercept=intercept, slope=slope)
kwargs['show_legend'] = False
geom.__init__(self, mapping, data, **kwargs)
w0 = w
X = scale_simp(x, loc, n, p)
_, s, v = linalg.svd(np.sqrt(w/np.sum(w)) * X)
# wX = X @ v.T @ np.diag(np.full(p, 1/s))
wX = np.dot(np.dot(X, v.T), np.diag(np.full(p, 1/s)))
# Q = np.squeeze((wX**2) @ np.ones(p))
Q = np.squeeze(np.dot(wX**2, np.ones(p)))
w = (wt * (nu + p)) / (nu + Q)[:, np.newaxis]
if use_loc:
loc = np.sum(w*x, axis=0) / w.sum()
if all(np.abs(w-w0) < tol):
break
else:
if ((np.mean(w) - np.mean(wt) > tol) or
(np.abs(np.mean(w * Q)/p - 1) > tol)):
warn("Probable convergence failure.", PlotnineWarning)
_a = np.sqrt(w) * X
# cov = (_a.T @ _a) / np.sum(wt)
cov = np.dot(_a.T, _a) / np.sum(wt)
if miss_wt:
ans = dict(cov=cov, center=loc, n_obs=n)
else:
ans = dict(cov=cov, center=loc, wt=wt0, n_obs=n)
if cor:
sd = np.sqrt(np.diag(cov))
cor = (cov/sd)/np.repeat([sd], p, axis=0).T
ans['cor'] = cor
ans['iter'] = iteration
def check_dimensions(nrow, ncol):
if nrow is not None:
if nrow < 1:
warn("'nrow' must be greater than 0. "
"Your value has been ignored.", PlotnineWarning)
nrow = None
else:
nrow = int(nrow)
if ncol is not None:
if ncol < 1:
warn("'ncol' must be greater than 0. "
"Your value has been ignored.", PlotnineWarning)
ncol = None
else:
ncol = int(ncol)
return nrow, ncol
def setup_params(self, data):
"""
Verify, modify & return a copy of the params.
"""
# Variable for which to do the stacking
if 'ymax' in data:
if any((data['ymin'] != 0) & (data['ymax'] != 0)):
warn("Stacking not well defined when not "
"anchored on the axis.", PlotnineWarning)
var = 'ymax'
elif 'y' in data:
var = 'y'
else:
warn("Stacking requires either ymin & ymax or y "
"aesthetics. Maybe you want position = 'identity'?",
PlotnineWarning)
var = None
params = self.params.copy()
params['var'] = var
params['fill'] = self.fill
return params
def draw_panel(self, data, panel_params, coord, ax, **params):
if not any(data['group'].duplicated()):
warn("geom_path: Each group consist of only one "
"observation. Do you need to adjust the "
"group aesthetic?", PlotnineWarning)
# drop lines with less than two points
c = Counter(data['group'])
counts = np.array([c[v] for v in data['group']])
data = data[counts >= 2]
if len(data) < 2:
return
# dataframe mergesort is stable, we rely on that here
data = data.sort_values('group', kind='mergesort')
data.reset_index(drop=True, inplace=True)
# When the parameters of the path are not constant
# with in the group, then the lines that make the paths
# can be drawn as separate segments
def scale_type(series):
if array_kind.continuous(series):
stype = 'continuous'
elif array_kind.ordinal(series):
stype = 'ordinal'
elif array_kind.discrete(series):
stype = 'discrete'
elif array_kind.datetime(series):
stype = 'datetime'
elif array_kind.timedelta(series):
stype = 'timedelta'
else:
msg = ("Don't know how to automatically pick scale for "
"object of type {}. Defaulting to 'continuous'")
warn(msg.format(series.dtype), PlotnineWarning)
stype = 'continuous'
return stype
# Get indices where any row for the select aesthetics has
# NaNs at the beginning or the end. Those we drop
bool_idx = (data[['x', 'y', 'size', 'color', 'linetype']]
.isnull() # Missing
.apply(keep, axis=0)) # Beginning or the End
bool_idx = np.all(bool_idx, axis=1) # Across the aesthetics
# return data
n1 = len(data)
data = data[bool_idx]
data.reset_index(drop=True, inplace=True)
n2 = len(data)
if (n2 != n1 and not self.params['na_rm']):
msg = "geom_path: Removed {} rows containing missing values."
warn(msg.format(n1-n2), PlotnineWarning)
return data
def __init__(self, mapping=None, data=None, **kwargs):
mapping, data = order_as_mapping_data(mapping, data)
xintercept = kwargs.pop('xintercept', None)
if xintercept is not None:
if mapping:
warn("The 'xintercept' parameter has overridden "
"the aes() mapping.", PlotnineWarning)
data = pd.DataFrame({'xintercept': make_iterable(xintercept)})
mapping = aes(xintercept='xintercept')
kwargs['show_legend'] = False
geom.__init__(self, mapping, data, **kwargs)