code string | signature string | docstring string | loss_without_docstring float64 | loss_with_docstring float64 | factor float64 |
|---|---|---|---|---|---|
margin = compress_pruned(
self._slice.margin(
axis=None,
weighted=False,
include_transforms_for_dims=self._hs_dims,
prune=self._prune,
)
)
mask = margin < self._size
if margin.shape == self.... | def table_mask(self) | ndarray, True where table margin <= min_base_size, same shape as slice. | 8.673863 | 7.705242 | 1.125709 |
# TODO: Figure out how to intersperse pairwise objects for columns
# that represent H&S
return [
_ColumnPairwiseSignificance(
self._slice,
col_idx,
self._axis,
self._weighted,
self._alpha,
... | def values(self) | list of _ColumnPairwiseSignificance tests.
Result has as many elements as there are coliumns in the slice. Each
significance test contains `p_vals` and `t_stats` significance tests. | 9.957018 | 6.923354 | 1.438178 |
return np.array([sig.pairwise_indices for sig in self.values]).T | def pairwise_indices(self) | ndarray containing tuples of pairwise indices. | 11.346245 | 9.804601 | 1.157237 |
summary_pairwise_indices = np.empty(
self.values[0].t_stats.shape[1], dtype=object
)
summary_pairwise_indices[:] = [
sig.summary_pairwise_indices for sig in self.values
]
return summary_pairwise_indices | def summary_pairwise_indices(self) | ndarray containing tuples of pairwise indices for the column summary. | 4.139592 | 3.572109 | 1.158865 |
if self._heuristic_score is None:
matches = self.heuristic()
self._heuristic_score = float(sum(matches)) / float(len(matches))
return self._heuristic_score | def score(self) | Calculate and return a heuristic score for this Parser against the provided
script source and path. This is used to order the ArgumentParsers as "most likely to work"
against a given script/source file.
Each parser has a calculate_score() function that returns a list of booleans representing
... | 4.083982 | 3.599731 | 1.134524 |
simulation = self.survey_scenario.simulation
holder = simulation.get_holder(self.weight_name)
holder.array = numpy.array(self.initial_weight, dtype = holder.variable.dtype) | def reset(self) | Reset the calibration to it initial state | 11.571556 | 11.134177 | 1.039283 |
self.survey_scenario = survey_scenario
# TODO deal with baseline if reform is present
if survey_scenario.simulation is None:
survey_scenario.simulation = survey_scenario.new_simulation()
period = self.period
self.filter_by = filter_by = survey_scenario.calcul... | def _set_survey_scenario(self, survey_scenario) | Set survey scenario
:param survey_scenario: the survey scenario | 5.153721 | 5.561922 | 0.926608 |
if parameter == 'lo':
self.parameters['lo'] = 1 / value
else:
self.parameters[parameter] = value | def set_parameters(self, parameter, value) | Set parameters value
:param parameter: the parameter to be set
:param value: the valeu used to set the parameter | 5.316044 | 7.371407 | 0.721171 |
# Select only filtered entities
assert self.initial_weight_name is not None
data = pd.DataFrame()
data[self.initial_weight_name] = self.initial_weight * self.filter_by
for variable in self.margins_by_variable:
if variable == 'total_population':
... | def _build_calmar_data(self) | Builds the data dictionnary used as calmar input argument | 7.78891 | 7.760944 | 1.003603 |
data = self._build_calmar_data()
assert self.initial_weight_name is not None
parameters['initial_weight'] = self.initial_weight_name
val_pondfin, lambdasol, updated_margins = calmar(
data, margins, **parameters)
# Updating only afetr filtering weights
... | def _update_weights(self, margins, parameters = {}) | Run calmar, stores new weights and returns adjusted margins | 12.062605 | 10.272705 | 1.174238 |
period = self.period
survey_scenario = self.survey_scenario
assert survey_scenario.simulation is not None
for simulation in [survey_scenario.simulation, survey_scenario.baseline_simulation]:
if simulation is None:
continue
simulation.set_i... | def set_calibrated_weights(self) | Modify the weights to use the calibrated weights | 6.500571 | 6.698923 | 0.97039 |
actions = set()
if isinstance(action, argparse._AppendAction):
actions.add(SPECIFY_EVERY_PARAM)
return actions | def get_parameter_action(action) | To foster a general schema that can accomodate multiple parsers, the general behavior here is described
rather than the specific language of a given parser. For instance, the 'append' action of an argument
is collapsing each argument given to a single argument. It also returns a set of actions as well, since
... | 12.392835 | 8.221109 | 1.507441 |
import feather
assert receiver is not None and donor is not None
assert matching_variables is not None
temporary_directory_path = os.path.join(config_files_directory, 'tmp')
assert os.path.exists(temporary_directory_path)
receiver_path = os.path.join(temporary_directory_path, 'receiver.fe... | def nnd_hotdeck_using_feather(receiver = None, donor = None, matching_variables = None, z_variables = None) | Not working | 2.116979 | 2.100488 | 1.007851 |
return np.array(
[Pfaffian(self, val).value for i, val in np.ndenumerate(self._chisq)]
).reshape(self._chisq.shape) | def wishart_pfaffian(self) | ndarray of wishart pfaffian CDF, before normalization | 6.218621 | 5.868048 | 1.059743 |
return np.full(self.n_min, self.size - 1, dtype=np.int) | def other_ind(self) | last row or column of square A | 9.968516 | 8.435347 | 1.181755 |
K1 = np.float_power(pi, 0.5 * self.n_min * self.n_min)
K1 /= (
np.float_power(2, 0.5 * self.n_min * self._n_max)
* self._mgamma(0.5 * self._n_max, self.n_min)
* self._mgamma(0.5 * self.n_min, self.n_min)
)
K2 = np.float_power(
2, ... | def K(self) | Normalizing constant for wishart CDF. | 3.263257 | 3.062282 | 1.065629 |
wishart = self._wishart_cdf
# Prepare variables for integration algorithm
A = self.A
p = self._gammainc_a
g = gamma(wishart.alpha_vec)
q_ind = np.arange(2 * wishart.n_min - 2)
q_vec = 2 * wishart.alpha + q_ind + 2
q = np.float_power(0.5, q_vec) *... | def value(self) | return float Cumulative Distribution Function.
The return value represents a floating point number of the CDF of the
largest eigenvalue of a Wishart(n, p) evaluated at chisq_val. | 5.528644 | 5.292988 | 1.044522 |
wishart = self._wishart_cdf
base = np.zeros([wishart.size, wishart.size])
if wishart.n_min % 2:
# If matrix has odd number of elements, we need to append a
# row and a col, in order for the pfaffian algorithm to work
base = self._make_size_even(base)... | def A(self) | ndarray - a skew-symmetric matrix for integrating the target distribution | 11.976111 | 10.918336 | 1.096881 |
return cls()._data(cube, weighted, prune) | def data(cls, cube, weighted, prune) | Return ndarray representing table index by margin. | 7.710364 | 6.602339 | 1.167823 |
result = []
for slice_ in cube.slices:
if cube.has_mr:
return self._mr_index(cube, weighted, prune)
num = slice_.margin(axis=0, weighted=weighted, prune=prune)
den = slice_.margin(weighted=weighted, prune=prune)
margin = num / den
... | def _data(self, cube, weighted, prune) | ndarray representing table index by margin. | 3.331643 | 3.218545 | 1.03514 |
if weights is None:
weights = ones(len(values))
df = pd.DataFrame({'x': values, 'w': weights})
df = df.sort_values(by='x')
x = df['x']
w = df['w']
wx = w * x
cdf = cumsum(wx) - 0.5 * wx
numerator = (w * cdf).sum()
denominator = ((wx).sum()) * (w.sum())
gini = 1 - 2... | def gini(values, weights = None, bin_size = None) | Gini coefficient (normalized to 1)
Using fastgini formula :
i=N j=i
SUM W_i*(SUM W_j*X_j - W_i*X_i/2)
i=1 j=1
G = 1 - 2* ----------------------------------
i=N i=N
... | 2.757661 | 2.810901 | 0.981059 |
from scipy.integrate import simps
if weights is None:
weights = ones(len(values))
# sign = -1
# if tax == True:
# sign = -1
# else:
# sign = 1
PLCx, PLCy = pseudo_lorenz(values, ineq_axis, weights)
LCx, LCy = lorenz(ineq_axis, weights)
del PLCx
return sim... | def kakwani(values, ineq_axis, weights = None) | Computes the Kakwani index | 5.169709 | 5.025382 | 1.02872 |
if weights is None:
weights = ones(len(values))
df = pd.DataFrame({'v': values, 'w': weights})
df = df.sort_values(by = 'v')
x = cumsum(df['w'])
x = x / float(x[-1:])
y = cumsum(df['v'] * df['w'])
y = y / float(y[-1:])
return x, y | def lorenz(values, weights = None) | Computes Lorenz Curve coordinates | 2.52523 | 2.544411 | 0.992461 |
return cls._factory(slice_, axis, weighted).pvals | def pvals(cls, slice_, axis=0, weighted=True) | Wishart CDF values for slice columns as square ndarray.
Wishart CDF (Cumulative Distribution Function) is calculated to determine
statistical significance of slice columns, in relation to all other columns.
These values represent the answer to the question "How much is a particular
colu... | 12.054926 | 23.365568 | 0.515927 |
n = self._element_count
chi_squared = np.zeros([n, n])
for i in xrange(1, n):
for j in xrange(0, n - 1):
denominator = 1 / margin[i] + 1 / margin[j]
chi_squared[i, j] = chi_squared[j, i] = (
np.sum(np.square(proportions[:, ... | def _chi_squared(self, proportions, margin, observed) | return ndarray of chi-squared measures for proportions' columns.
*proportions* (ndarray): The basis of chi-squared calcualations
*margin* (ndarray): Column margin for proportions (See `def _margin`)
*observed* (ndarray): Row margin proportions (See `def _observed`) | 2.795166 | 2.754245 | 1.014857 |
return self._intersperse_insertion_rows_and_columns(
1.0 - WishartCDF(pairwise_chisq, self._n_min, self._n_max).values
) | def _pvals_from_chi_squared(self, pairwise_chisq) | return statistical significance for props' columns.
*pairwise_chisq* (ndarray) Matrix of chi-squared values (bases for Wishart CDF) | 11.940903 | 11.903934 | 1.003106 |
if slice_.dim_types[0] == DT.MR_SUBVAR:
return _MrXCatPairwiseSignificance(slice_, axis, weighted)
return _CatXCatPairwiseSignificance(slice_, axis, weighted) | def _factory(slice_, axis, weighted) | return subclass for PairwiseSignificance, based on slice dimension types. | 11.37481 | 6.447767 | 1.764147 |
for i in self._insertion_indices:
pairwise_pvals = np.insert(pairwise_pvals, i, np.nan, axis=0)
pairwise_pvals = np.insert(pairwise_pvals, i, np.nan, axis=1)
return pairwise_pvals | def _intersperse_insertion_rows_and_columns(self, pairwise_pvals) | Return pvals matrix with inserted NaN rows and columns, as numpy.ndarray.
Each insertion (a header or a subtotal) creates an offset in the calculated
pvals. These need to be taken into account when converting each pval to a
corresponding column letter. For this reason, we need to insert an all-... | 1.834433 | 1.713485 | 1.070586 |
off_axis = 1 - self._axis
return self._slice.margin(axis=off_axis, include_mr_cat=self._include_mr_cat) | def _opposite_axis_margin(self) | ndarray representing margin along the axis opposite of self._axis
In the process of calculating p-values for the column significance testing we
need both the margin along the primary axis and the percentage margin along
the opposite axis. | 12.920722 | 12.050739 | 1.072193 |
return self._slice.proportions(
axis=self._axis, include_mr_cat=self._include_mr_cat
) | def _proportions(self) | ndarray representing slice proportions along correct axis. | 11.724313 | 7.745131 | 1.513766 |
return self._chi_squared(self._proportions, self._margin, self._observed) | def _pairwise_chisq(self) | Pairwise comparisons (Chi-Square) along axis, as numpy.ndarray.
Returns a square, symmetric matrix of test statistics for the null
hypothesis that each vector along *axis* is equal to each other. | 15.101513 | 23.991819 | 0.629444 |
return [
self._chi_squared(
mr_subvar_proportions,
self._margin[idx],
self._opposite_axis_margin[idx]
/ np.sum(self._opposite_axis_margin[idx]),
)
for (idx, mr_subvar_proportions) in enumerate(self._prop... | def _pairwise_chisq(self) | Pairwise comparisons (Chi-Square) along axis, as numpy.ndarray.
Returns a list of square and symmetric matrices of test statistics for the null
hypothesis that each vector along *axis* is equal to each other. | 5.932135 | 6.499285 | 0.912737 |
try:
# Parse with --help to enforce exit
usage_sections = docopt.docopt(self.parser, ['--help'])
except SystemExit as e:
parser = inspect.trace()[-2][0].f_locals
'''
docopt represents all values as strings and doesn't automatically cast, we ... | def process_parser(self) | We can't use the exception catch trick for docopt because the module prevents access to
it's innards __all__ = ['docopt']. Instead call with --help enforced, catch sys.exit and
work up to the calling docopt function to pull out the elements. This is horrible.
:return: | 5.236472 | 4.970145 | 1.053585 |
unique_val_list = unique(data)
output = {}
for val in unique_val_list:
output[val] = (data == val)
return output | def build_dummies_dict(data) | Return a dict with unique values as keys and vectors as values | 3.714393 | 3.450897 | 1.076356 |
try:
ca_ind = self.dim_types.index(DT.CA_SUBVAR)
return 1 - ca_ind
except ValueError:
return None | def ca_main_axis(self) | For univariate CA, the main axis is the categorical axis | 10.567509 | 7.699182 | 1.37255 |
if self.ndim != 2:
return False
return all(dt in DT.ALLOWED_PAIRWISE_TYPES for dt in self.dim_types) | def can_compare_pairwise(self) | Return bool indicating if slice can compute pairwise comparisons.
Currently, only the CAT x CAT slice can compute pairwise comparisons. This also
includes the categorical array categories dimnension (CA_CAT). | 10.032195 | 9.217634 | 1.08837 |
if not prune:
return self.as_array(include_transforms_for_dims=hs_dims).shape
shape = compress_pruned(
self.as_array(prune=True, include_transforms_for_dims=hs_dims)
).shape
# Eliminate dimensions that get reduced to 1
# (e.g. single element cate... | def get_shape(self, prune=False, hs_dims=None) | Tuple of array dimensions' lengths.
It returns a tuple of ints, each representing the length of a cube
dimension, in the order those dimensions appear in the cube.
Pruning is supported. Dimensions that get reduced to a single element
(e.g. due to pruning) are removed from the returning ... | 6.389539 | 7.390684 | 0.86454 |
proportions = self.proportions(axis=axis)
baseline = (
baseline if baseline is not None else self._prepare_index_baseline(axis)
)
# Fix the shape to enable correct broadcasting
if (
axis == 0
and len(baseline.shape) <= 1
a... | def index_table(self, axis=None, baseline=None, prune=False) | Return index percentages for a given axis and baseline.
The index values represent the difference of the percentages to the
corresponding baseline values. The baseline values are the univariate
percentages of the corresponding variable. | 5.128181 | 4.882817 | 1.050251 |
if self.ca_as_0th:
labels = self._cube.labels(include_transforms_for_dims=hs_dims)[1:]
else:
labels = self._cube.labels(include_transforms_for_dims=hs_dims)[-2:]
if not prune:
return labels
def prune_dimension_labels(labels, prune_indices):
... | def labels(self, hs_dims=None, prune=False) | Get labels for the cube slice, and perform pruning by slice. | 3.823307 | 3.579165 | 1.068212 |
axis = self._calculate_correct_axis_for_cube(axis)
hs_dims = self._hs_dims_for_cube(include_transforms_for_dims)
margin = self._cube.margin(
axis=axis,
weighted=weighted,
include_missing=include_missing,
include_transforms_for_dims=hs_di... | def margin(
self,
axis=None,
weighted=True,
include_missing=False,
include_transforms_for_dims=None,
prune=False,
include_mr_cat=False,
) | Return ndarray representing slice margin across selected axis.
A margin (or basis) can be calculated for a contingency table, provided
that the dimensions of the desired directions are marginable. The
dimensions are marginable if they represent mutualy exclusive data,
such as true categ... | 3.269297 | 3.208903 | 1.018821 |
return MinBaseSizeMask(self, size, hs_dims=hs_dims, prune=prune) | def min_base_size_mask(self, size, hs_dims=None, prune=False) | Returns MinBaseSizeMask object with correct row, col and table masks.
The returned object stores the necessary information about the base size, as
well as about the base values. It can create corresponding masks in teh row,
column, and table directions, based on the corresponding base values
... | 3.507439 | 4.753248 | 0.737904 |
mr_dim_ind = self._cube.mr_dim_ind
if self._cube.ndim == 3:
if isinstance(mr_dim_ind, int):
if mr_dim_ind == 0:
# If only the 0th dimension of a 3D is an MR, the sliced
# don't actuall have the MR... Thus return None.
... | def mr_dim_ind(self) | Get the correct index of the MR dimension in the cube slice. | 6.432319 | 6.103087 | 1.053945 |
scale_means = self._cube.scale_means(hs_dims, prune)
if self.ca_as_0th:
# If slice is used as 0th CA, then we need to observe the 1st dimension,
# because the 0th dimension is CA items, which is only used for slicing
# (and thus doesn't have numerical values... | def scale_means(self, hs_dims=None, prune=False) | Return list of column and row scaled means for this slice.
If a row/col doesn't have numerical values, return None for the
corresponding dimension. If a slice only has 1D, return only the column
scaled mean (as numpy array). If both row and col scaled means are
present, return them as t... | 9.282194 | 8.370766 | 1.108882 |
if self._cube.ndim < 3 and not self.ca_as_0th:
return None
title = self._cube.name
table_name = self._cube.labels()[0][self._index]
return "%s: %s" % (title, table_name) | def table_name(self) | Get slice name.
In case of 2D return cube name. In case of 3D, return the combination
of the cube name with the label of the corresponding slice
(nth label of the 0th dimension). | 9.114209 | 7.363985 | 1.237674 |
if axis != 0:
raise NotImplementedError("Pairwise comparison only implemented for colums")
return WishartPairwiseSignificance.pvals(self, axis=axis) | def wishart_pairwise_pvals(self, axis=0) | Return square symmetric matrix of pairwise column-comparison p-values.
Square, symmetric matrix along *axis* of pairwise p-values for the
null hypothesis that col[i] = col[j] for each pair of columns.
*axis* (int): axis along which to perform comparison. Only columns (0)
are implemente... | 6.116395 | 7.04345 | 0.868381 |
stats = self.zscore(weighted=weighted, prune=prune, hs_dims=hs_dims)
pvals = 2 * (1 - norm.cdf(np.abs(stats)))
return self._apply_pruning_mask(pvals, hs_dims) if prune else pvals | def pvals(self, weighted=True, prune=False, hs_dims=None) | Return 2D ndarray with calculated P values
This function calculates statistically significant cells for
categorical contingency tables under the null hypothesis that the
row and column variables are independent (uncorrelated).
The values are calculated for 2D tables only.
:para... | 3.649156 | 4.649496 | 0.78485 |
counts = self.as_array(weighted=weighted)
total = self.margin(weighted=weighted)
colsum = self.margin(axis=0, weighted=weighted)
rowsum = self.margin(axis=1, weighted=weighted)
zscore = self._calculate_std_res(counts, total, colsum, rowsum)
if hs_dims:
... | def zscore(self, weighted=True, prune=False, hs_dims=None) | Return ndarray with slices's standardized residuals (Z-scores).
(Only applicable to a 2D contingency tables.) The Z-score or
standardized residual is the difference between observed and expected
cell counts if row and column variables were independent divided
by the residual cell varian... | 4.273972 | 4.278886 | 0.998852 |
return PairwiseSignificance(
self, alpha=alpha, only_larger=only_larger, hs_dims=hs_dims
).pairwise_indices | def pairwise_indices(self, alpha=0.05, only_larger=True, hs_dims=None) | Indices of columns where p < alpha for column-comparison t-tests
Returns an array of tuples of columns that are significant at p<alpha,
from a series of pairwise t-tests.
Argument both_pairs returns indices striclty on the test statistic. If
False, however, only the index of values *si... | 3.107242 | 3.941356 | 0.788369 |
if self.mr_dim_ind == 0:
# --This is a special case where broadcasting cannot be
# --automatically done. We need to "inflate" the single dimensional
# --ndarrays, to be able to treat them as "columns" (essentially a
# --Nx1 ndarray). This is needed for su... | def _array_type_std_res(self, counts, total, colsum, rowsum) | Return ndarray containing standard residuals for array values.
The shape of the return value is the same as that of *counts*.
Array variables require special processing because of the
underlying math. Essentially, it boils down to the fact that the
variable dimensions are mutually indep... | 7.464437 | 7.428878 | 1.004786 |
if set(self.dim_types) & DT.ARRAY_TYPES: # ---has-mr-or-ca---
return self._array_type_std_res(counts, total, colsum, rowsum)
return self._scalar_type_std_res(counts, total, colsum, rowsum) | def _calculate_std_res(self, counts, total, colsum, rowsum) | Return ndarray containing standard residuals.
The shape of the return value is the same as that of *counts*. | 7.25081 | 7.694875 | 0.942291 |
if self._cube.ndim < 3:
if self.ca_as_0th and axis is None:
# Special case for CA slices (in multitables). In this case,
# we need to calculate a measurement across CA categories
# dimension (and not across items, because it's not
... | def _calculate_correct_axis_for_cube(self, axis) | Return correct axis for cube, based on ndim.
If cube has 3 dimensions, increase axis by 1. This will translate the
default 0 (cols direction) and 1 (rows direction) to actual 1
(cols direction) and 2 (rows direction). This is needed because the
0th dimension of the 3D cube is only used ... | 13.767962 | 12.409173 | 1.109499 |
expected_counts = expected_freq(counts)
residuals = counts - expected_counts
variance = (
np.outer(rowsum, colsum)
* np.outer(total - rowsum, total - colsum)
/ total ** 3
)
return residuals / np.sqrt(variance) | def _scalar_type_std_res(self, counts, total, colsum, rowsum) | Return ndarray containing standard residuals for category values.
The shape of the return value is the same as that of *counts*. | 4.452149 | 3.974455 | 1.120191 |
means = []
table = self._slice.as_array()
products = self._inner_prods(table, self.values)
for axis, product in enumerate(products):
if product is None:
means.append(product)
continue
# Calculate means
valid_i... | def data(self) | list of mean numeric values of categorical responses. | 4.327613 | 4.020605 | 1.076359 |
if self._slice.ndim < 2:
msg = (
"Scale Means marginal cannot be calculated on 1D cubes, as"
"the scale means already get reduced to a scalar value."
)
raise ValueError(msg)
dimension_index = 1 - axis
margin = self._sl... | def margin(self, axis) | Return marginal value of the current slice scaled means.
This value is the the same what you would get from a single variable
(constituting a 2D cube/slice), when the "non-missing" filter of the
opposite variable would be applied. This behavior is consistent with
what is visible in the ... | 6.170323 | 5.328402 | 1.158006 |
return [
(
np.array(dim.numeric_values)
if (dim.numeric_values and any(~np.isnan(dim.numeric_values)))
else None
)
for dim in self._slice.dimensions
] | def values(self) | list of ndarray value-ids for each dimension in slice.
The values for each dimension appear as an ndarray. None appears
instead of the array for each dimension having only NaN values. | 6.144017 | 5.038944 | 1.219306 |
if not isinstance(table, np.ma.core.MaskedArray):
return table
if table.ndim == 0:
return table.data
if table.ndim == 1:
return np.ma.compressed(table)
row_inds = ~table.mask.all(axis=1)
col_inds = ~table.mask.all(axis=0)
table = table[row_inds, :][:, col_inds]
... | def compress_pruned(table) | Compress table based on pruning mask.
Only the rows/cols in which all of the elements are masked need to be
pruned. | 2.415282 | 2.486671 | 0.971292 |
for dim, inds in enumerate(slice_.inserted_hs_indices()):
if dim not in hs_dims:
continue
for i in inds:
res = np.insert(res, i, np.nan, axis=(dim - slice_.ndim))
return res | def intersperse_hs_in_std_res(slice_, hs_dims, res) | Perform the insertions of place-holding rows and cols for insertions. | 4.695099 | 4.515057 | 1.039876 |
if isinstance(sub_parameter, Scale):
if unit_type == 'threshold_unit':
for bracket in sub_parameter.brackets:
threshold = bracket.children['threshold']
inflate_parameter_leaf(threshold, base_year, inflator)
return
else:
# Remove new v... | def inflate_parameter_leaf(sub_parameter, base_year, inflator, unit_type = 'unit') | Inflate a Parameter leaf according to unit type
Basic unit type are supposed by default
Other admissible unit types are threshold_unit and rate_unit | 2.943833 | 2.842006 | 1.035829 |
if use_baseline:
assert self.baseline_simulation is not None, "self.baseline_simulation is None"
simulation = self.baseline_simulation
else:
assert self.simulation is not None
simulation = self.simulation
tax_benefit_system = simulation.t... | def calculate_variable(self, variable = None, period = None, use_baseline = False) | Compute and return the variable values for period and baseline or reform tax_benefit_system | 2.503136 | 2.390462 | 1.047135 |
assert input_data_frame is not None
assert simulation is not None
id_variable_by_entity_key = self.id_variable_by_entity_key
role_variable_by_entity_key = self.role_variable_by_entity_key
used_as_input_variables = self.used_as_input_variables
tax_benefit_system ... | def filter_input_variables(self, input_data_frame = None, simulation = None) | Filter the input data frame from variables that won't be used or are set to be computed | 2.614572 | 2.57421 | 1.015679 |
'''Initialises a survey scenario from data.
:param rebuild_input_data: Whether or not to clean, format and save data.
Take a look at :func:`build_input_data`
:param data: Contains the data, or metadata needed to know where to find it.
... | def init_from_data(self, calibration_kwargs = None, inflation_kwargs = None,
rebuild_input_data = False, rebuild_kwargs = None, data = None, memory_config = None) | Initialises a survey scenario from data.
:param rebuild_input_data: Whether or not to clean, format and save data.
Take a look at :func:`build_input_data`
:param data: Contains the data, or metadata needed to know where to find it. | 5.696631 | 4.431943 | 1.285357 |
assert entity is not None
assert input_data_frame is not None
assert period is not None
assert simulation is not None
used_as_input_variables = self.used_as_input_variables_by_entity[entity]
variables_mismatch = set(used_as_input_variables).difference(set(input_... | def init_entity(self, entity = None, input_data_frame = None, period = None, simulation = None) | Initialize the simulation period with current input_data_frame | 2.378111 | 2.382457 | 0.998176 |
for variable_name, variable in tax_benefit_system.variables.items():
if variable.formulas:
continue
if self.used_as_input_variables and (variable_name in self.used_as_input_variables):
continue
if self.non_neutralizable_variables and (... | def neutralize_variables(self, tax_benefit_system) | Neutralizing input variables not in input dataframe and keep some crucial variables | 3.085436 | 2.896783 | 1.065125 |
assert tax_benefit_system is not None
self.tax_benefit_system = tax_benefit_system
if self.cache_blacklist is not None:
self.tax_benefit_system.cache_blacklist = self.cache_blacklist
if baseline_tax_benefit_system is not None:
self.baseline_tax_benefit_sy... | def set_tax_benefit_systems(self, tax_benefit_system = None, baseline_tax_benefit_system = None) | Set the tax and benefit system and eventually the baseline tax and benefit system | 1.779652 | 1.742465 | 1.021341 |
'''Identify and set the good ids for the different entities'''
if self.id_variable_by_entity_key is None:
self.id_variable_by_entity_key = dict(
(entity.key, entity.key + '_id') for entity in self.tax_benefit_system.entities)
log.debug("Use default id_variable nam... | def _set_id_variable_by_entity_key(self) -> Dict[str, str] | Identify and set the good ids for the different entities | 4.988366 | 3.780956 | 1.31934 |
'''Identify and set the good roles for the different entities'''
if self.role_variable_by_entity_key is None:
self.role_variable_by_entity_key = dict(
(entity.key, entity.key + '_legacy_role') for entity in self.tax_benefit_system.entities)
return self.role_variable_... | def _set_role_variable_by_entity_key(self) -> Dict[str, str] | Identify and set the good roles for the different entities | 5.674668 | 4.041657 | 1.404045 |
'''Identify and set the good input variables for the different entities'''
if self.used_as_input_variables_by_entity is not None:
return
tax_benefit_system = self.tax_benefit_system
assert set(self.used_as_input_variables) <= set(tax_benefit_system.variables.keys()), \
... | def _set_used_as_input_variables_by_entity(self) -> Dict[str, List[str]] | Identify and set the good input variables for the different entities | 2.674293 | 2.304962 | 1.160233 |
return tuple(d for d in self._all_dimensions if d.dimension_type != DT.MR_CAT) | def _dimensions(self) | tuple of dimension objects in this collection.
This composed tuple is the source for the dimension objects in this
collection. | 21.484261 | 17.652842 | 1.217043 |
return (
Dimension(raw_dimension.dimension_dict, raw_dimension.dimension_type)
for raw_dimension in self._raw_dimensions
) | def _iter_dimensions(self) | Generate Dimension object for each dimension dict. | 6.820291 | 4.524449 | 1.50743 |
return tuple(
_RawDimension(dimension_dict, self._dimension_dicts)
for dimension_dict in self._dimension_dicts
) | def _raw_dimensions(self) | Sequence of _RawDimension objects wrapping each dimension dict. | 7.309967 | 3.688987 | 1.981565 |
base_type = self._base_type
if base_type == "categorical":
return self._resolve_categorical()
if base_type == "enum.variable":
return self._resolve_array_type()
if base_type == "enum.datetime":
return DT.DATETIME
if base_type == "enum.... | def dimension_type(self) | Return member of DIMENSION_TYPE appropriate to dimension_dict. | 4.142323 | 3.928003 | 1.054562 |
type_class = self._dimension_dict["type"]["class"]
if type_class == "categorical":
return "categorical"
if type_class == "enum":
subclass = self._dimension_dict["type"]["subtype"]["class"]
return "enum.%s" % subclass
raise NotImplementedError(... | def _base_type(self) | Return str like 'enum.numeric' representing dimension type.
This string is a 'type.subclass' concatenation of the str keys
used to identify the dimension type in the cube response JSON.
The '.subclass' suffix only appears where a subtype is present. | 4.455284 | 3.454287 | 1.289784 |
dimension_dicts = self._dimension_dicts
this_idx = dimension_dicts.index(self._dimension_dict)
if this_idx > len(dimension_dicts) - 2:
return None
return _RawDimension(dimension_dicts[this_idx + 1], self._dimension_dicts) | def _next_raw_dimension(self) | _RawDimension for next *dimension_dict* in sequence or None for last.
Returns None if this dimension is the last in sequence for this cube. | 3.903221 | 3.101586 | 1.25846 |
next_raw_dimension = self._next_raw_dimension
if next_raw_dimension is None:
return DT.CA
is_mr_subvar = (
next_raw_dimension._base_type == "categorical"
and next_raw_dimension._has_selected_category
and next_raw_dimension._alias == self.... | def _resolve_array_type(self) | Return one of the ARRAY_TYPES members of DIMENSION_TYPE.
This method distinguishes between CA and MR dimensions. The return
value is only meaningful if the dimension is known to be of array
type (i.e. either CA or MR, base-type 'enum.variable'). | 6.861956 | 5.107459 | 1.343517 |
# ---an array categorical is either CA_CAT or MR_CAT---
if self._is_array_cat:
return DT.MR_CAT if self._has_selected_category else DT.CA_CAT
# ---what's left is logical or plain-old categorical---
return DT.LOGICAL if self._has_selected_category else DT.CAT | def _resolve_categorical(self) | Return one of the categorical members of DIMENSION_TYPE.
This method distinguishes between CAT, CA_CAT, MR_CAT, and LOGICAL
dimension types, all of which have the base type 'categorical'. The
return value is only meaningful if the dimension is known to be one
of the categorical types (h... | 13.972817 | 8.458959 | 1.651837 |
if self.dimension_type in {DT.MR_CAT, DT.LOGICAL}:
return ()
return tuple(
(subtotal.anchor_idx, subtotal.addend_idxs) for subtotal in self._subtotals
) | def hs_indices(self) | tuple of (anchor_idx, addend_idxs) pair for each subtotal.
Example::
(
(2, (0, 1, 2)),
(3, (3,)),
('bottom', (4, 5))
)
Note that the `anchor_idx` item in the first position of each pair
can be 'top' or 'bottom' as well as... | 17.931236 | 9.821087 | 1.825789 |
# ---don't do H&S insertions for CA and MR subvar dimensions---
if self.dimension_type in DT.ARRAY_TYPES:
return []
return [
idx
for idx, item in enumerate(
self._iter_interleaved_items(self.valid_elements)
)
i... | def inserted_hs_indices(self) | list of int index of each inserted subtotal for the dimension.
Each value represents the position of a subtotal in the interleaved
sequence of elements and subtotals items. | 19.923937 | 15.576268 | 1.279121 |
return self.dimension_type not in {DT.CA, DT.MR, DT.MR_CAT, DT.LOGICAL} | def is_marginable(self) | True if adding counts across this dimension axis is meaningful. | 23.654371 | 15.011918 | 1.575706 |
# TODO: Having an alternate return type triggered by a flag-parameter
# (`include_cat_ids` in this case) is poor practice. Using flags like
# that effectively squashes what should be two methods into one.
# Either get rid of the need for that alternate return value type or
... | def labels(
self, include_missing=False, include_transforms=False, include_cat_ids=False
) | Return list of str labels for the elements of this dimension.
Returns a list of (label, element_id) pairs if *include_cat_ids* is
True. The `element_id` value in the second position of the pair is
None for subtotal items (which don't have an element-id). | 7.862132 | 6.739735 | 1.166534 |
subtotals = self._subtotals
for subtotal in subtotals.iter_for_anchor("top"):
yield subtotal
for element in elements:
yield element
for subtotal in subtotals.iter_for_anchor(element.element_id):
yield subtotal
for subtotal i... | def _iter_interleaved_items(self, elements) | Generate element or subtotal items in interleaved order.
This ordering corresponds to how value "rows" (or columns) are to
appear after subtotals have been inserted at their anchor locations.
Where more than one subtotal is anchored to the same location, they
appear in their document or... | 3.350098 | 3.152176 | 1.062789 |
view = self._dimension_dict.get("references", {}).get("view", {})
# ---view can be both None and {}, thus the edge case.---
insertion_dicts = (
[] if view is None else view.get("transform", {}).get("insertions", [])
)
return _Subtotals(insertion_dicts, self.v... | def _subtotals(self) | _Subtotals sequence object for this dimension.
The subtotals sequence provides access to any subtotal insertions
defined on this dimension. | 16.764797 | 13.674681 | 1.225974 |
if self._type_dict["class"] == "categorical":
return _Category, self._type_dict["categories"]
return _Element, self._type_dict["elements"] | def _element_makings(self) | (ElementCls, element_dicts) pair for this dimension's elements.
All the elements of a given dimension are the same type. This method
determines the type (class) and source dicts for the elements of this
dimension and provides them for the element factory. | 7.871023 | 8.265862 | 0.952233 |
ElementCls, element_dicts = self._element_makings
return tuple(
ElementCls(element_dict, idx, element_dicts)
for idx, element_dict in enumerate(element_dicts)
) | def _elements(self) | Composed tuple storing actual sequence of element objects. | 7.531737 | 6.437346 | 1.170007 |
numeric_value = self._element_dict.get("numeric_value")
return np.nan if numeric_value is None else numeric_value | def numeric_value(self) | Numeric value assigned to element by user, np.nan if absent. | 5.816726 | 3.389546 | 1.716078 |
value = self._element_dict.get("value")
type_name = type(value).__name__
if type_name == "NoneType":
return ""
if type_name == "list":
# ---like '10-15' or 'A-F'---
return "-".join([str(item) for item in value])
if type_name in ("fl... | def label(self) | str display-name for this element, '' when absent from cube response.
This property handles numeric, datetime and text variables, but also
subvar dimensions | 5.964522 | 4.998851 | 1.193179 |
return (subtotal for subtotal in self._subtotals if subtotal.anchor == anchor) | def iter_for_anchor(self, anchor) | Generate each subtotal having matching *anchor*. | 7.744063 | 3.094155 | 2.502804 |
for insertion_dict in self._insertion_dicts:
# ---skip any non-dicts---
if not isinstance(insertion_dict, dict):
continue
# ---skip any non-subtotal insertions---
if insertion_dict.get("function") != "subtotal":
continue
... | def _iter_valid_subtotal_dicts(self) | Generate each insertion dict that represents a valid subtotal. | 6.123902 | 5.426286 | 1.128562 |
return tuple(
_Subtotal(subtotal_dict, self.valid_elements)
for subtotal_dict in self._iter_valid_subtotal_dicts()
) | def _subtotals(self) | Composed tuple storing actual sequence of _Subtotal objects. | 8.865394 | 5.932077 | 1.494484 |
anchor = self._subtotal_dict["anchor"]
try:
anchor = int(anchor)
if anchor not in self.valid_elements.element_ids:
return "bottom"
return anchor
except (TypeError, ValueError):
return anchor.lower() | def anchor(self) | int or str indicating element under which to insert this subtotal.
An int anchor is the id of the dimension element (category or
subvariable) under which to place this subtotal. The return value can
also be one of 'top' or 'bottom'.
The return value defaults to 'bottom' for an anchor r... | 9.434911 | 6.938154 | 1.359859 |
anchor = self.anchor
if anchor in ["top", "bottom"]:
return anchor
return self.valid_elements.get_by_id(anchor).index_in_valids | def anchor_idx(self) | int or str representing index of anchor element in dimension.
When the anchor is an operation, like 'top' or 'bottom' | 12.241226 | 9.913991 | 1.234743 |
return tuple(
arg
for arg in self._subtotal_dict.get("args", [])
if arg in self.valid_elements.element_ids
) | def addend_ids(self) | tuple of int ids of elements contributing to this subtotal.
Any element id not present in the dimension or present but
representing missing data is excluded. | 15.35349 | 11.156205 | 1.376229 |
return tuple(
self.valid_elements.get_by_id(addend_id).index_in_valids
for addend_id in self.addend_ids
) | def addend_idxs(self) | tuple of int index of each addend element for this subtotal.
The length of the tuple is the same as that for `.addend_ids`, but
each value repesents the offset of that element within the dimension,
rather than its element id. | 6.784226 | 6.082621 | 1.115346 |
stata_files = []
sas_files = []
for root, subdirs, files in os.walk(directory_path):
for file_name in files:
file_path = os.path.join(root, file_name)
if os.path.basename(file_name).endswith(".dta"):
log.info("Found stata file {}".format(file_path))
... | def create_data_file_by_format(directory_path = None) | Browse subdirectories to extract stata and sas files | 1.91983 | 1.658262 | 1.157736 |
array = self._as_array(
include_missing=include_missing,
weighted=weighted,
include_transforms_for_dims=include_transforms_for_dims,
)
# ---prune array if pruning was requested---
if prune:
array = self._prune_body(array, transfor... | def as_array(
self,
include_missing=False,
weighted=True,
include_transforms_for_dims=None,
prune=False,
) | Return `ndarray` representing cube values.
Returns the tabular representation of the crunch cube. The returned
array has the same number of dimensions as the cube. E.g. for
a cross-tab representation of a categorical and numerical variable,
the resulting cube will have two dimensions.
... | 4.063844 | 5.183751 | 0.783958 |
return self._measures.weighted_n if weighted else self._measures.unweighted_n | def count(self, weighted=True) | Return numberic count of rows considered for cube response. | 8.249582 | 7.118002 | 1.158974 |
if self.ndim < 3 and not ca_as_0th:
return [CubeSlice(self, 0)]
return [CubeSlice(self, i, ca_as_0th) for i, _ in enumerate(self.labels()[0])] | def get_slices(self, ca_as_0th=False) | Return list of :class:`.CubeSlice` objects.
The number of slice objects in the returned list depends on the
dimensionality of this cube. A 1D or 2D cube will return a list
containing one slice object. A 3D cube will return a list of slices
the same length as the first dimension. | 4.935656 | 3.716521 | 1.328031 |
warnings.warn(
"CrunchCube.index() is deprecated. Use CubeSlice.index_table().",
DeprecationWarning,
)
return Index.data(self, weighted, prune) | def index(self, weighted=True, prune=False) | Return cube index measurement.
This function is deprecated. Use index_table from CubeSlice. | 11.236129 | 5.365052 | 2.094319 |
if self.ndim == 2 and prune:
# If pruning is applied, we need to subtract from the H&S indes
# the number of pruned rows (cols) that come before that index.
pruning_bases = [self._pruning_base(axis=i, hs_dims=[0, 1]) for i in [1, 0]]
pruning_bases = [
... | def inserted_hs_indices(self, prune=False) | Get indices of the inserted H&S (for formatting purposes). | 4.590243 | 4.360991 | 1.052569 |
return self.ndim == 2 and set(self.dim_types) == {DT.CA_SUBVAR, DT.CA_CAT} | def is_univariate_ca(self) | True if cube only contains a CA dimension-pair, in either order. | 11.485125 | 8.108836 | 1.416372 |
return [
dim.labels(include_missing, include_transforms_for_dims)
for dim in self.dimensions
] | def labels(self, include_missing=False, include_transforms_for_dims=False) | Gets labels for each cube's dimension.
Args
include_missing (bool): Include labels for missing values
Returns
labels (list of lists): Labels for each dimension | 3.606422 | 5.082134 | 0.709628 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.