repository_name
stringlengths
5
67
func_path_in_repository
stringlengths
4
234
func_name
stringlengths
0
314
whole_func_string
stringlengths
52
3.87M
language
stringclasses
6 values
func_code_string
stringlengths
39
1.84M
func_code_tokens
listlengths
15
672k
func_documentation_string
stringlengths
1
47.2k
func_documentation_tokens
listlengths
1
3.92k
split_name
stringclasses
1 value
func_code_url
stringlengths
85
339
UDST/urbansim
urbansim/models/regression.py
RegressionModel.columns_used
def columns_used(self): """ Returns all the columns used in this model for filtering and in the model expression. """ return list(tz.unique(tz.concatv( util.columns_in_filters(self.fit_filters), util.columns_in_filters(self.predict_filters), util.columns_in_formula(self.model_expression))))
python
def columns_used(self): return list(tz.unique(tz.concatv( util.columns_in_filters(self.fit_filters), util.columns_in_filters(self.predict_filters), util.columns_in_formula(self.model_expression))))
[ "def", "columns_used", "(", "self", ")", ":", "return", "list", "(", "tz", ".", "unique", "(", "tz", ".", "concatv", "(", "util", ".", "columns_in_filters", "(", "self", ".", "fit_filters", ")", ",", "util", ".", "columns_in_filters", "(", "self", ".", "predict_filters", ")", ",", "util", ".", "columns_in_formula", "(", "self", ".", "model_expression", ")", ")", ")", ")" ]
Returns all the columns used in this model for filtering and in the model expression.
[ "Returns", "all", "the", "columns", "used", "in", "this", "model", "for", "filtering", "and", "in", "the", "model", "expression", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/models/regression.py#L460-L469
UDST/urbansim
urbansim/models/regression.py
RegressionModel.fit_from_cfg
def fit_from_cfg(cls, df, cfgname, debug=False, outcfgname=None): """ Parameters ---------- df : DataFrame The dataframe which contains the columns to use for the estimation. cfgname : string The name of the yaml config file which describes the hedonic model. debug : boolean, optional (default False) Whether to generate debug information on the model. outcfgname : string, optional (default cfgname) The name of the output yaml config file where estimation results are written into. Returns ------- RegressionModel which was used to fit """ logger.debug('start: fit from configuration {}'.format(cfgname)) hm = cls.from_yaml(str_or_buffer=cfgname) ret = hm.fit(df, debug=debug) print(ret.summary()) outcfgname = outcfgname or cfgname hm.to_yaml(str_or_buffer=outcfgname) logger.debug('finish: fit into configuration {}'.format(outcfgname)) return hm
python
def fit_from_cfg(cls, df, cfgname, debug=False, outcfgname=None): logger.debug('start: fit from configuration {}'.format(cfgname)) hm = cls.from_yaml(str_or_buffer=cfgname) ret = hm.fit(df, debug=debug) print(ret.summary()) outcfgname = outcfgname or cfgname hm.to_yaml(str_or_buffer=outcfgname) logger.debug('finish: fit into configuration {}'.format(outcfgname)) return hm
[ "def", "fit_from_cfg", "(", "cls", ",", "df", ",", "cfgname", ",", "debug", "=", "False", ",", "outcfgname", "=", "None", ")", ":", "logger", ".", "debug", "(", "'start: fit from configuration {}'", ".", "format", "(", "cfgname", ")", ")", "hm", "=", "cls", ".", "from_yaml", "(", "str_or_buffer", "=", "cfgname", ")", "ret", "=", "hm", ".", "fit", "(", "df", ",", "debug", "=", "debug", ")", "print", "(", "ret", ".", "summary", "(", ")", ")", "outcfgname", "=", "outcfgname", "or", "cfgname", "hm", ".", "to_yaml", "(", "str_or_buffer", "=", "outcfgname", ")", "logger", ".", "debug", "(", "'finish: fit into configuration {}'", ".", "format", "(", "outcfgname", ")", ")", "return", "hm" ]
Parameters ---------- df : DataFrame The dataframe which contains the columns to use for the estimation. cfgname : string The name of the yaml config file which describes the hedonic model. debug : boolean, optional (default False) Whether to generate debug information on the model. outcfgname : string, optional (default cfgname) The name of the output yaml config file where estimation results are written into. Returns ------- RegressionModel which was used to fit
[ "Parameters", "----------", "df", ":", "DataFrame", "The", "dataframe", "which", "contains", "the", "columns", "to", "use", "for", "the", "estimation", ".", "cfgname", ":", "string", "The", "name", "of", "the", "yaml", "config", "file", "which", "describes", "the", "hedonic", "model", ".", "debug", ":", "boolean", "optional", "(", "default", "False", ")", "Whether", "to", "generate", "debug", "information", "on", "the", "model", ".", "outcfgname", ":", "string", "optional", "(", "default", "cfgname", ")", "The", "name", "of", "the", "output", "yaml", "config", "file", "where", "estimation", "results", "are", "written", "into", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/models/regression.py#L472-L496
UDST/urbansim
urbansim/models/regression.py
RegressionModel.predict_from_cfg
def predict_from_cfg(cls, df, cfgname): """ Parameters ---------- df : DataFrame The dataframe which contains the columns to use for the estimation. cfgname : string The name of the yaml config file which describes the hedonic model. Returns ------- predicted : pandas.Series Predicted data in a pandas Series. Will have the index of `data` after applying filters and minus any groups that do not have models. hm : RegressionModel which was used to predict """ logger.debug('start: predict from configuration {}'.format(cfgname)) hm = cls.from_yaml(str_or_buffer=cfgname) price_or_rent = hm.predict(df) print(price_or_rent.describe()) logger.debug('start: predict from configuration {}'.format(cfgname)) return price_or_rent, hm
python
def predict_from_cfg(cls, df, cfgname): logger.debug('start: predict from configuration {}'.format(cfgname)) hm = cls.from_yaml(str_or_buffer=cfgname) price_or_rent = hm.predict(df) print(price_or_rent.describe()) logger.debug('start: predict from configuration {}'.format(cfgname)) return price_or_rent, hm
[ "def", "predict_from_cfg", "(", "cls", ",", "df", ",", "cfgname", ")", ":", "logger", ".", "debug", "(", "'start: predict from configuration {}'", ".", "format", "(", "cfgname", ")", ")", "hm", "=", "cls", ".", "from_yaml", "(", "str_or_buffer", "=", "cfgname", ")", "price_or_rent", "=", "hm", ".", "predict", "(", "df", ")", "print", "(", "price_or_rent", ".", "describe", "(", ")", ")", "logger", ".", "debug", "(", "'start: predict from configuration {}'", ".", "format", "(", "cfgname", ")", ")", "return", "price_or_rent", ",", "hm" ]
Parameters ---------- df : DataFrame The dataframe which contains the columns to use for the estimation. cfgname : string The name of the yaml config file which describes the hedonic model. Returns ------- predicted : pandas.Series Predicted data in a pandas Series. Will have the index of `data` after applying filters and minus any groups that do not have models. hm : RegressionModel which was used to predict
[ "Parameters", "----------", "df", ":", "DataFrame", "The", "dataframe", "which", "contains", "the", "columns", "to", "use", "for", "the", "estimation", ".", "cfgname", ":", "string", "The", "name", "of", "the", "yaml", "config", "file", "which", "describes", "the", "hedonic", "model", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/models/regression.py#L499-L523
UDST/urbansim
urbansim/models/regression.py
RegressionModelGroup.add_model
def add_model(self, model): """ Add a `RegressionModel` instance. Parameters ---------- model : `RegressionModel` Should have a ``.name`` attribute matching one of the groupby segments. """ logger.debug( 'adding model {} to group {}'.format(model.name, self.name)) self.models[model.name] = model
python
def add_model(self, model): logger.debug( 'adding model {} to group {}'.format(model.name, self.name)) self.models[model.name] = model
[ "def", "add_model", "(", "self", ",", "model", ")", ":", "logger", ".", "debug", "(", "'adding model {} to group {}'", ".", "format", "(", "model", ".", "name", ",", "self", ".", "name", ")", ")", "self", ".", "models", "[", "model", ".", "name", "]", "=", "model" ]
Add a `RegressionModel` instance. Parameters ---------- model : `RegressionModel` Should have a ``.name`` attribute matching one of the groupby segments.
[ "Add", "a", "RegressionModel", "instance", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/models/regression.py#L546-L559
UDST/urbansim
urbansim/models/regression.py
RegressionModelGroup.add_model_from_params
def add_model_from_params(self, name, fit_filters, predict_filters, model_expression, ytransform=None): """ Add a model by passing arguments through to `RegressionModel`. Parameters ---------- name : any Must match a groupby segment name. fit_filters : list of str Filters applied before fitting the model. predict_filters : list of str Filters applied before calculating new data points. model_expression : str A patsy model expression that can be used with statsmodels. Should contain both the left- and right-hand sides. ytransform : callable, optional A function to call on the array of predicted output. For example, if the model relation is predicting the log of price, you might pass ``ytransform=np.exp`` so that the results reflect actual price. By default no transformation is applied. """ logger.debug( 'adding model {} to group {}'.format(name, self.name)) model = RegressionModel( fit_filters, predict_filters, model_expression, ytransform, name) self.models[name] = model
python
def add_model_from_params(self, name, fit_filters, predict_filters, model_expression, ytransform=None): logger.debug( 'adding model {} to group {}'.format(name, self.name)) model = RegressionModel( fit_filters, predict_filters, model_expression, ytransform, name) self.models[name] = model
[ "def", "add_model_from_params", "(", "self", ",", "name", ",", "fit_filters", ",", "predict_filters", ",", "model_expression", ",", "ytransform", "=", "None", ")", ":", "logger", ".", "debug", "(", "'adding model {} to group {}'", ".", "format", "(", "name", ",", "self", ".", "name", ")", ")", "model", "=", "RegressionModel", "(", "fit_filters", ",", "predict_filters", ",", "model_expression", ",", "ytransform", ",", "name", ")", "self", ".", "models", "[", "name", "]", "=", "model" ]
Add a model by passing arguments through to `RegressionModel`. Parameters ---------- name : any Must match a groupby segment name. fit_filters : list of str Filters applied before fitting the model. predict_filters : list of str Filters applied before calculating new data points. model_expression : str A patsy model expression that can be used with statsmodels. Should contain both the left- and right-hand sides. ytransform : callable, optional A function to call on the array of predicted output. For example, if the model relation is predicting the log of price, you might pass ``ytransform=np.exp`` so that the results reflect actual price. By default no transformation is applied.
[ "Add", "a", "model", "by", "passing", "arguments", "through", "to", "RegressionModel", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/models/regression.py#L561-L590
UDST/urbansim
urbansim/models/regression.py
RegressionModelGroup._iter_groups
def _iter_groups(self, data): """ Iterate over the groups in `data` after grouping by `segmentation_col`. Skips any groups for which there is no model stored. Yields tuples of (name, df) where name is the group key and df is the group DataFrame. Parameters ---------- data : pandas.DataFrame Must have a column with the same name as `segmentation_col`. """ groups = data.groupby(self.segmentation_col) for name in self.models: yield name, groups.get_group(name)
python
def _iter_groups(self, data): groups = data.groupby(self.segmentation_col) for name in self.models: yield name, groups.get_group(name)
[ "def", "_iter_groups", "(", "self", ",", "data", ")", ":", "groups", "=", "data", ".", "groupby", "(", "self", ".", "segmentation_col", ")", "for", "name", "in", "self", ".", "models", ":", "yield", "name", ",", "groups", ".", "get_group", "(", "name", ")" ]
Iterate over the groups in `data` after grouping by `segmentation_col`. Skips any groups for which there is no model stored. Yields tuples of (name, df) where name is the group key and df is the group DataFrame. Parameters ---------- data : pandas.DataFrame Must have a column with the same name as `segmentation_col`.
[ "Iterate", "over", "the", "groups", "in", "data", "after", "grouping", "by", "segmentation_col", ".", "Skips", "any", "groups", "for", "which", "there", "is", "no", "model", "stored", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/models/regression.py#L592-L610
UDST/urbansim
urbansim/models/regression.py
RegressionModelGroup.fit
def fit(self, data, debug=False): """ Fit each of the models in the group. Parameters ---------- data : pandas.DataFrame Must have a column with the same name as `segmentation_col`. debug : bool If set to true (default false) will pass the debug parameter to model estimation. Returns ------- fits : dict of statsmodels.regression.linear_model.OLSResults Keys are the segment names. """ with log_start_finish( 'fitting models in group {}'.format(self.name), logger): return {name: self.models[name].fit(df, debug=debug) for name, df in self._iter_groups(data)}
python
def fit(self, data, debug=False): with log_start_finish( 'fitting models in group {}'.format(self.name), logger): return {name: self.models[name].fit(df, debug=debug) for name, df in self._iter_groups(data)}
[ "def", "fit", "(", "self", ",", "data", ",", "debug", "=", "False", ")", ":", "with", "log_start_finish", "(", "'fitting models in group {}'", ".", "format", "(", "self", ".", "name", ")", ",", "logger", ")", ":", "return", "{", "name", ":", "self", ".", "models", "[", "name", "]", ".", "fit", "(", "df", ",", "debug", "=", "debug", ")", "for", "name", ",", "df", "in", "self", ".", "_iter_groups", "(", "data", ")", "}" ]
Fit each of the models in the group. Parameters ---------- data : pandas.DataFrame Must have a column with the same name as `segmentation_col`. debug : bool If set to true (default false) will pass the debug parameter to model estimation. Returns ------- fits : dict of statsmodels.regression.linear_model.OLSResults Keys are the segment names.
[ "Fit", "each", "of", "the", "models", "in", "the", "group", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/models/regression.py#L612-L633
UDST/urbansim
urbansim/models/regression.py
RegressionModelGroup.predict
def predict(self, data): """ Predict new data for each group in the segmentation. Parameters ---------- data : pandas.DataFrame Data to use for prediction. Must have a column with the same name as `segmentation_col`. Returns ------- predicted : pandas.Series Predicted data in a pandas Series. Will have the index of `data` after applying filters and minus any groups that do not have models. """ with log_start_finish( 'predicting models in group {}'.format(self.name), logger): results = [self.models[name].predict(df) for name, df in self._iter_groups(data)] return pd.concat(results)
python
def predict(self, data): with log_start_finish( 'predicting models in group {}'.format(self.name), logger): results = [self.models[name].predict(df) for name, df in self._iter_groups(data)] return pd.concat(results)
[ "def", "predict", "(", "self", ",", "data", ")", ":", "with", "log_start_finish", "(", "'predicting models in group {}'", ".", "format", "(", "self", ".", "name", ")", ",", "logger", ")", ":", "results", "=", "[", "self", ".", "models", "[", "name", "]", ".", "predict", "(", "df", ")", "for", "name", ",", "df", "in", "self", ".", "_iter_groups", "(", "data", ")", "]", "return", "pd", ".", "concat", "(", "results", ")" ]
Predict new data for each group in the segmentation. Parameters ---------- data : pandas.DataFrame Data to use for prediction. Must have a column with the same name as `segmentation_col`. Returns ------- predicted : pandas.Series Predicted data in a pandas Series. Will have the index of `data` after applying filters and minus any groups that do not have models.
[ "Predict", "new", "data", "for", "each", "group", "in", "the", "segmentation", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/models/regression.py#L644-L666
UDST/urbansim
urbansim/models/regression.py
SegmentedRegressionModel.from_yaml
def from_yaml(cls, yaml_str=None, str_or_buffer=None): """ Create a SegmentedRegressionModel instance from a saved YAML configuration. Arguments are mutally exclusive. Parameters ---------- yaml_str : str, optional A YAML string from which to load model. str_or_buffer : str or file like, optional File name or buffer from which to load YAML. Returns ------- SegmentedRegressionModel """ cfg = yamlio.yaml_to_dict(yaml_str, str_or_buffer) default_model_expr = cfg['default_config']['model_expression'] default_ytransform = cfg['default_config']['ytransform'] seg = cls( cfg['segmentation_col'], cfg['fit_filters'], cfg['predict_filters'], default_model_expr, YTRANSFORM_MAPPING[default_ytransform], cfg['min_segment_size'], cfg['name']) if "models" not in cfg: cfg["models"] = {} for name, m in cfg['models'].items(): m['model_expression'] = m.get( 'model_expression', default_model_expr) m['ytransform'] = m.get('ytransform', default_ytransform) m['fit_filters'] = None m['predict_filters'] = None reg = RegressionModel.from_yaml(yamlio.convert_to_yaml(m, None)) seg._group.add_model(reg) logger.debug( 'loaded segmented regression model {} from yaml'.format(seg.name)) return seg
python
def from_yaml(cls, yaml_str=None, str_or_buffer=None): cfg = yamlio.yaml_to_dict(yaml_str, str_or_buffer) default_model_expr = cfg['default_config']['model_expression'] default_ytransform = cfg['default_config']['ytransform'] seg = cls( cfg['segmentation_col'], cfg['fit_filters'], cfg['predict_filters'], default_model_expr, YTRANSFORM_MAPPING[default_ytransform], cfg['min_segment_size'], cfg['name']) if "models" not in cfg: cfg["models"] = {} for name, m in cfg['models'].items(): m['model_expression'] = m.get( 'model_expression', default_model_expr) m['ytransform'] = m.get('ytransform', default_ytransform) m['fit_filters'] = None m['predict_filters'] = None reg = RegressionModel.from_yaml(yamlio.convert_to_yaml(m, None)) seg._group.add_model(reg) logger.debug( 'loaded segmented regression model {} from yaml'.format(seg.name)) return seg
[ "def", "from_yaml", "(", "cls", ",", "yaml_str", "=", "None", ",", "str_or_buffer", "=", "None", ")", ":", "cfg", "=", "yamlio", ".", "yaml_to_dict", "(", "yaml_str", ",", "str_or_buffer", ")", "default_model_expr", "=", "cfg", "[", "'default_config'", "]", "[", "'model_expression'", "]", "default_ytransform", "=", "cfg", "[", "'default_config'", "]", "[", "'ytransform'", "]", "seg", "=", "cls", "(", "cfg", "[", "'segmentation_col'", "]", ",", "cfg", "[", "'fit_filters'", "]", ",", "cfg", "[", "'predict_filters'", "]", ",", "default_model_expr", ",", "YTRANSFORM_MAPPING", "[", "default_ytransform", "]", ",", "cfg", "[", "'min_segment_size'", "]", ",", "cfg", "[", "'name'", "]", ")", "if", "\"models\"", "not", "in", "cfg", ":", "cfg", "[", "\"models\"", "]", "=", "{", "}", "for", "name", ",", "m", "in", "cfg", "[", "'models'", "]", ".", "items", "(", ")", ":", "m", "[", "'model_expression'", "]", "=", "m", ".", "get", "(", "'model_expression'", ",", "default_model_expr", ")", "m", "[", "'ytransform'", "]", "=", "m", ".", "get", "(", "'ytransform'", ",", "default_ytransform", ")", "m", "[", "'fit_filters'", "]", "=", "None", "m", "[", "'predict_filters'", "]", "=", "None", "reg", "=", "RegressionModel", ".", "from_yaml", "(", "yamlio", ".", "convert_to_yaml", "(", "m", ",", "None", ")", ")", "seg", ".", "_group", ".", "add_model", "(", "reg", ")", "logger", ".", "debug", "(", "'loaded segmented regression model {} from yaml'", ".", "format", "(", "seg", ".", "name", ")", ")", "return", "seg" ]
Create a SegmentedRegressionModel instance from a saved YAML configuration. Arguments are mutally exclusive. Parameters ---------- yaml_str : str, optional A YAML string from which to load model. str_or_buffer : str or file like, optional File name or buffer from which to load YAML. Returns ------- SegmentedRegressionModel
[ "Create", "a", "SegmentedRegressionModel", "instance", "from", "a", "saved", "YAML", "configuration", ".", "Arguments", "are", "mutally", "exclusive", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/models/regression.py#L726-L768
UDST/urbansim
urbansim/models/regression.py
SegmentedRegressionModel.add_segment
def add_segment(self, name, model_expression=None, ytransform='default'): """ Add a new segment with its own model expression and ytransform. Parameters ---------- name : Segment name. Must match a segment in the groupby of the data. model_expression : str or dict, optional A patsy model expression that can be used with statsmodels. Should contain both the left- and right-hand sides. If not given the default model will be used, which must not be None. ytransform : callable, optional A function to call on the array of predicted output. For example, if the model relation is predicting the log of price, you might pass ``ytransform=np.exp`` so that the results reflect actual price. If not given the default ytransform will be used. """ if not model_expression: if self.default_model_expr is None: raise ValueError( 'No default model available, ' 'you must supply a model experssion.') model_expression = self.default_model_expr if ytransform == 'default': ytransform = self.default_ytransform # no fit or predict filters, we'll take care of that this side. self._group.add_model_from_params( name, None, None, model_expression, ytransform) logger.debug('added segment {} to model {}'.format(name, self.name))
python
def add_segment(self, name, model_expression=None, ytransform='default'): if not model_expression: if self.default_model_expr is None: raise ValueError( 'No default model available, ' 'you must supply a model experssion.') model_expression = self.default_model_expr if ytransform == 'default': ytransform = self.default_ytransform self._group.add_model_from_params( name, None, None, model_expression, ytransform) logger.debug('added segment {} to model {}'.format(name, self.name))
[ "def", "add_segment", "(", "self", ",", "name", ",", "model_expression", "=", "None", ",", "ytransform", "=", "'default'", ")", ":", "if", "not", "model_expression", ":", "if", "self", ".", "default_model_expr", "is", "None", ":", "raise", "ValueError", "(", "'No default model available, '", "'you must supply a model experssion.'", ")", "model_expression", "=", "self", ".", "default_model_expr", "if", "ytransform", "==", "'default'", ":", "ytransform", "=", "self", ".", "default_ytransform", "# no fit or predict filters, we'll take care of that this side.", "self", ".", "_group", ".", "add_model_from_params", "(", "name", ",", "None", ",", "None", ",", "model_expression", ",", "ytransform", ")", "logger", ".", "debug", "(", "'added segment {} to model {}'", ".", "format", "(", "name", ",", "self", ".", "name", ")", ")" ]
Add a new segment with its own model expression and ytransform. Parameters ---------- name : Segment name. Must match a segment in the groupby of the data. model_expression : str or dict, optional A patsy model expression that can be used with statsmodels. Should contain both the left- and right-hand sides. If not given the default model will be used, which must not be None. ytransform : callable, optional A function to call on the array of predicted output. For example, if the model relation is predicting the log of price, you might pass ``ytransform=np.exp`` so that the results reflect actual price. If not given the default ytransform will be used.
[ "Add", "a", "new", "segment", "with", "its", "own", "model", "expression", "and", "ytransform", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/models/regression.py#L770-L806
UDST/urbansim
urbansim/models/regression.py
SegmentedRegressionModel.fit
def fit(self, data, debug=False): """ Fit each segment. Segments that have not already been explicitly added will be automatically added with default model and ytransform. Parameters ---------- data : pandas.DataFrame Must have a column with the same name as `segmentation_col`. debug : bool If set to true will pass debug to the fit method of each model. Returns ------- fits : dict of statsmodels.regression.linear_model.OLSResults Keys are the segment names. """ data = util.apply_filter_query(data, self.fit_filters) unique = data[self.segmentation_col].unique() value_counts = data[self.segmentation_col].value_counts() # Remove any existing segments that may no longer have counterparts # in the data. This can happen when loading a saved model and then # calling this method with data that no longer has segments that # were there the last time this was called. gone = set(self._group.models) - set(unique) for g in gone: del self._group.models[g] for x in unique: if x not in self._group.models and \ value_counts[x] > self.min_segment_size: self.add_segment(x) with log_start_finish( 'fitting models in segmented model {}'.format(self.name), logger): return self._group.fit(data, debug=debug)
python
def fit(self, data, debug=False): data = util.apply_filter_query(data, self.fit_filters) unique = data[self.segmentation_col].unique() value_counts = data[self.segmentation_col].value_counts() gone = set(self._group.models) - set(unique) for g in gone: del self._group.models[g] for x in unique: if x not in self._group.models and \ value_counts[x] > self.min_segment_size: self.add_segment(x) with log_start_finish( 'fitting models in segmented model {}'.format(self.name), logger): return self._group.fit(data, debug=debug)
[ "def", "fit", "(", "self", ",", "data", ",", "debug", "=", "False", ")", ":", "data", "=", "util", ".", "apply_filter_query", "(", "data", ",", "self", ".", "fit_filters", ")", "unique", "=", "data", "[", "self", ".", "segmentation_col", "]", ".", "unique", "(", ")", "value_counts", "=", "data", "[", "self", ".", "segmentation_col", "]", ".", "value_counts", "(", ")", "# Remove any existing segments that may no longer have counterparts", "# in the data. This can happen when loading a saved model and then", "# calling this method with data that no longer has segments that", "# were there the last time this was called.", "gone", "=", "set", "(", "self", ".", "_group", ".", "models", ")", "-", "set", "(", "unique", ")", "for", "g", "in", "gone", ":", "del", "self", ".", "_group", ".", "models", "[", "g", "]", "for", "x", "in", "unique", ":", "if", "x", "not", "in", "self", ".", "_group", ".", "models", "and", "value_counts", "[", "x", "]", ">", "self", ".", "min_segment_size", ":", "self", ".", "add_segment", "(", "x", ")", "with", "log_start_finish", "(", "'fitting models in segmented model {}'", ".", "format", "(", "self", ".", "name", ")", ",", "logger", ")", ":", "return", "self", ".", "_group", ".", "fit", "(", "data", ",", "debug", "=", "debug", ")" ]
Fit each segment. Segments that have not already been explicitly added will be automatically added with default model and ytransform. Parameters ---------- data : pandas.DataFrame Must have a column with the same name as `segmentation_col`. debug : bool If set to true will pass debug to the fit method of each model. Returns ------- fits : dict of statsmodels.regression.linear_model.OLSResults Keys are the segment names.
[ "Fit", "each", "segment", ".", "Segments", "that", "have", "not", "already", "been", "explicitly", "added", "will", "be", "automatically", "added", "with", "default", "model", "and", "ytransform", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/models/regression.py#L808-L847
UDST/urbansim
urbansim/models/regression.py
SegmentedRegressionModel.predict
def predict(self, data): """ Predict new data for each group in the segmentation. Parameters ---------- data : pandas.DataFrame Data to use for prediction. Must have a column with the same name as `segmentation_col`. Returns ------- predicted : pandas.Series Predicted data in a pandas Series. Will have the index of `data` after applying filters. """ with log_start_finish( 'predicting models in segmented model {}'.format(self.name), logger): data = util.apply_filter_query(data, self.predict_filters) return self._group.predict(data)
python
def predict(self, data): with log_start_finish( 'predicting models in segmented model {}'.format(self.name), logger): data = util.apply_filter_query(data, self.predict_filters) return self._group.predict(data)
[ "def", "predict", "(", "self", ",", "data", ")", ":", "with", "log_start_finish", "(", "'predicting models in segmented model {}'", ".", "format", "(", "self", ".", "name", ")", ",", "logger", ")", ":", "data", "=", "util", ".", "apply_filter_query", "(", "data", ",", "self", ".", "predict_filters", ")", "return", "self", ".", "_group", ".", "predict", "(", "data", ")" ]
Predict new data for each group in the segmentation. Parameters ---------- data : pandas.DataFrame Data to use for prediction. Must have a column with the same name as `segmentation_col`. Returns ------- predicted : pandas.Series Predicted data in a pandas Series. Will have the index of `data` after applying filters.
[ "Predict", "new", "data", "for", "each", "group", "in", "the", "segmentation", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/models/regression.py#L857-L878
UDST/urbansim
urbansim/models/regression.py
SegmentedRegressionModel._process_model_dict
def _process_model_dict(self, d): """ Remove redundant items from a model's configuration dict. Parameters ---------- d : dict Modified in place. Returns ------- dict Modified `d`. """ del d['model_type'] del d['fit_filters'] del d['predict_filters'] if d['model_expression'] == self.default_model_expr: del d['model_expression'] if YTRANSFORM_MAPPING[d['ytransform']] == self.default_ytransform: del d['ytransform'] d["name"] = yamlio.to_scalar_safe(d["name"]) return d
python
def _process_model_dict(self, d): del d['model_type'] del d['fit_filters'] del d['predict_filters'] if d['model_expression'] == self.default_model_expr: del d['model_expression'] if YTRANSFORM_MAPPING[d['ytransform']] == self.default_ytransform: del d['ytransform'] d["name"] = yamlio.to_scalar_safe(d["name"]) return d
[ "def", "_process_model_dict", "(", "self", ",", "d", ")", ":", "del", "d", "[", "'model_type'", "]", "del", "d", "[", "'fit_filters'", "]", "del", "d", "[", "'predict_filters'", "]", "if", "d", "[", "'model_expression'", "]", "==", "self", ".", "default_model_expr", ":", "del", "d", "[", "'model_expression'", "]", "if", "YTRANSFORM_MAPPING", "[", "d", "[", "'ytransform'", "]", "]", "==", "self", ".", "default_ytransform", ":", "del", "d", "[", "'ytransform'", "]", "d", "[", "\"name\"", "]", "=", "yamlio", ".", "to_scalar_safe", "(", "d", "[", "\"name\"", "]", ")", "return", "d" ]
Remove redundant items from a model's configuration dict. Parameters ---------- d : dict Modified in place. Returns ------- dict Modified `d`.
[ "Remove", "redundant", "items", "from", "a", "model", "s", "configuration", "dict", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/models/regression.py#L880-L907
UDST/urbansim
urbansim/models/regression.py
SegmentedRegressionModel.to_dict
def to_dict(self): """ Returns a dict representation of this instance suitable for conversion to YAML. """ return { 'model_type': 'segmented_regression', 'name': self.name, 'segmentation_col': self.segmentation_col, 'fit_filters': self.fit_filters, 'predict_filters': self.predict_filters, 'min_segment_size': self.min_segment_size, 'default_config': { 'model_expression': self.default_model_expr, 'ytransform': YTRANSFORM_MAPPING[self.default_ytransform] }, 'fitted': self.fitted, 'models': { yamlio.to_scalar_safe(name): self._process_model_dict(m.to_dict()) for name, m in self._group.models.items()} }
python
def to_dict(self): return { 'model_type': 'segmented_regression', 'name': self.name, 'segmentation_col': self.segmentation_col, 'fit_filters': self.fit_filters, 'predict_filters': self.predict_filters, 'min_segment_size': self.min_segment_size, 'default_config': { 'model_expression': self.default_model_expr, 'ytransform': YTRANSFORM_MAPPING[self.default_ytransform] }, 'fitted': self.fitted, 'models': { yamlio.to_scalar_safe(name): self._process_model_dict(m.to_dict()) for name, m in self._group.models.items()} }
[ "def", "to_dict", "(", "self", ")", ":", "return", "{", "'model_type'", ":", "'segmented_regression'", ",", "'name'", ":", "self", ".", "name", ",", "'segmentation_col'", ":", "self", ".", "segmentation_col", ",", "'fit_filters'", ":", "self", ".", "fit_filters", ",", "'predict_filters'", ":", "self", ".", "predict_filters", ",", "'min_segment_size'", ":", "self", ".", "min_segment_size", ",", "'default_config'", ":", "{", "'model_expression'", ":", "self", ".", "default_model_expr", ",", "'ytransform'", ":", "YTRANSFORM_MAPPING", "[", "self", ".", "default_ytransform", "]", "}", ",", "'fitted'", ":", "self", ".", "fitted", ",", "'models'", ":", "{", "yamlio", ".", "to_scalar_safe", "(", "name", ")", ":", "self", ".", "_process_model_dict", "(", "m", ".", "to_dict", "(", ")", ")", "for", "name", ",", "m", "in", "self", ".", "_group", ".", "models", ".", "items", "(", ")", "}", "}" ]
Returns a dict representation of this instance suitable for conversion to YAML.
[ "Returns", "a", "dict", "representation", "of", "this", "instance", "suitable", "for", "conversion", "to", "YAML", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/models/regression.py#L909-L931
UDST/urbansim
urbansim/models/regression.py
SegmentedRegressionModel.columns_used
def columns_used(self): """ Returns all the columns used across all models in the group for filtering and in the model expression. """ return list(tz.unique(tz.concatv( util.columns_in_filters(self.fit_filters), util.columns_in_filters(self.predict_filters), util.columns_in_formula(self.default_model_expr), self._group.columns_used(), [self.segmentation_col])))
python
def columns_used(self): return list(tz.unique(tz.concatv( util.columns_in_filters(self.fit_filters), util.columns_in_filters(self.predict_filters), util.columns_in_formula(self.default_model_expr), self._group.columns_used(), [self.segmentation_col])))
[ "def", "columns_used", "(", "self", ")", ":", "return", "list", "(", "tz", ".", "unique", "(", "tz", ".", "concatv", "(", "util", ".", "columns_in_filters", "(", "self", ".", "fit_filters", ")", ",", "util", ".", "columns_in_filters", "(", "self", ".", "predict_filters", ")", ",", "util", ".", "columns_in_formula", "(", "self", ".", "default_model_expr", ")", ",", "self", ".", "_group", ".", "columns_used", "(", ")", ",", "[", "self", ".", "segmentation_col", "]", ")", ")", ")" ]
Returns all the columns used across all models in the group for filtering and in the model expression.
[ "Returns", "all", "the", "columns", "used", "across", "all", "models", "in", "the", "group", "for", "filtering", "and", "in", "the", "model", "expression", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/models/regression.py#L956-L967
UDST/urbansim
urbansim/models/regression.py
SegmentedRegressionModel.fit_from_cfg
def fit_from_cfg(cls, df, cfgname, debug=False, min_segment_size=None, outcfgname=None): """ Parameters ---------- df : DataFrame The dataframe which contains the columns to use for the estimation. cfgname : string The name of the yaml config file which describes the hedonic model. debug : boolean, optional (default False) Whether to generate debug information on the model. min_segment_size : int, optional Set attribute on the model. outcfgname : string, optional (default cfgname) The name of the output yaml config file where estimation results are written into. Returns ------- hm : SegmentedRegressionModel which was used to fit """ logger.debug('start: fit from configuration {}'.format(cfgname)) hm = cls.from_yaml(str_or_buffer=cfgname) if min_segment_size: hm.min_segment_size = min_segment_size for k, v in hm.fit(df, debug=debug).items(): print("REGRESSION RESULTS FOR SEGMENT %s\n" % str(k)) print(v.summary()) outcfgname = outcfgname or cfgname hm.to_yaml(str_or_buffer=outcfgname) logger.debug('finish: fit into configuration {}'.format(outcfgname)) return hm
python
def fit_from_cfg(cls, df, cfgname, debug=False, min_segment_size=None, outcfgname=None): logger.debug('start: fit from configuration {}'.format(cfgname)) hm = cls.from_yaml(str_or_buffer=cfgname) if min_segment_size: hm.min_segment_size = min_segment_size for k, v in hm.fit(df, debug=debug).items(): print("REGRESSION RESULTS FOR SEGMENT %s\n" % str(k)) print(v.summary()) outcfgname = outcfgname or cfgname hm.to_yaml(str_or_buffer=outcfgname) logger.debug('finish: fit into configuration {}'.format(outcfgname)) return hm
[ "def", "fit_from_cfg", "(", "cls", ",", "df", ",", "cfgname", ",", "debug", "=", "False", ",", "min_segment_size", "=", "None", ",", "outcfgname", "=", "None", ")", ":", "logger", ".", "debug", "(", "'start: fit from configuration {}'", ".", "format", "(", "cfgname", ")", ")", "hm", "=", "cls", ".", "from_yaml", "(", "str_or_buffer", "=", "cfgname", ")", "if", "min_segment_size", ":", "hm", ".", "min_segment_size", "=", "min_segment_size", "for", "k", ",", "v", "in", "hm", ".", "fit", "(", "df", ",", "debug", "=", "debug", ")", ".", "items", "(", ")", ":", "print", "(", "\"REGRESSION RESULTS FOR SEGMENT %s\\n\"", "%", "str", "(", "k", ")", ")", "print", "(", "v", ".", "summary", "(", ")", ")", "outcfgname", "=", "outcfgname", "or", "cfgname", "hm", ".", "to_yaml", "(", "str_or_buffer", "=", "outcfgname", ")", "logger", ".", "debug", "(", "'finish: fit into configuration {}'", ".", "format", "(", "outcfgname", ")", ")", "return", "hm" ]
Parameters ---------- df : DataFrame The dataframe which contains the columns to use for the estimation. cfgname : string The name of the yaml config file which describes the hedonic model. debug : boolean, optional (default False) Whether to generate debug information on the model. min_segment_size : int, optional Set attribute on the model. outcfgname : string, optional (default cfgname) The name of the output yaml config file where estimation results are written into. Returns ------- hm : SegmentedRegressionModel which was used to fit
[ "Parameters", "----------", "df", ":", "DataFrame", "The", "dataframe", "which", "contains", "the", "columns", "to", "use", "for", "the", "estimation", ".", "cfgname", ":", "string", "The", "name", "of", "the", "yaml", "config", "file", "which", "describes", "the", "hedonic", "model", ".", "debug", ":", "boolean", "optional", "(", "default", "False", ")", "Whether", "to", "generate", "debug", "information", "on", "the", "model", ".", "min_segment_size", ":", "int", "optional", "Set", "attribute", "on", "the", "model", ".", "outcfgname", ":", "string", "optional", "(", "default", "cfgname", ")", "The", "name", "of", "the", "output", "yaml", "config", "file", "where", "estimation", "results", "are", "written", "into", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/models/regression.py#L970-L1000
UDST/urbansim
urbansim/models/relocation.py
find_movers
def find_movers(choosers, rates, rate_column): """ Returns an array of the indexes of the `choosers` that are slated to move. Parameters ---------- choosers : pandas.DataFrame Table of agents from which to find movers. rates : pandas.DataFrame Table of relocation rates. Index is unused. Other columns describe filters on the `choosers` table so that different segments can have different relocation rates. Columns that ends with '_max' will be used to create a "less than" filters, columns that end with '_min' will be used to create "greater than or equal to" filters. A column with no suffix will be used to make an 'equal to' filter. An example `rates` structure: age_of_head_max age_of_head_min nan 65 65 40 In this example the `choosers` table would need to have an 'age_of_head' column on which to filter. nan should be used to flag filters that do not apply in a given row. rate_column : object Name of column in `rates` table that has relocation rates. Returns ------- movers : pandas.Index Suitable for indexing `choosers` by index. """ logger.debug('start: find movers for relocation') relocation_rates = pd.Series( np.zeros(len(choosers)), index=choosers.index) for _, row in rates.iterrows(): indexes = util.filter_table(choosers, row, ignore={rate_column}).index relocation_rates.loc[indexes] = row[rate_column] movers = relocation_rates.index[ relocation_rates > np.random.random(len(choosers))] logger.debug('picked {} movers for relocation'.format(len(movers))) logger.debug('finish: find movers for relocation') return movers
python
def find_movers(choosers, rates, rate_column): logger.debug('start: find movers for relocation') relocation_rates = pd.Series( np.zeros(len(choosers)), index=choosers.index) for _, row in rates.iterrows(): indexes = util.filter_table(choosers, row, ignore={rate_column}).index relocation_rates.loc[indexes] = row[rate_column] movers = relocation_rates.index[ relocation_rates > np.random.random(len(choosers))] logger.debug('picked {} movers for relocation'.format(len(movers))) logger.debug('finish: find movers for relocation') return movers
[ "def", "find_movers", "(", "choosers", ",", "rates", ",", "rate_column", ")", ":", "logger", ".", "debug", "(", "'start: find movers for relocation'", ")", "relocation_rates", "=", "pd", ".", "Series", "(", "np", ".", "zeros", "(", "len", "(", "choosers", ")", ")", ",", "index", "=", "choosers", ".", "index", ")", "for", "_", ",", "row", "in", "rates", ".", "iterrows", "(", ")", ":", "indexes", "=", "util", ".", "filter_table", "(", "choosers", ",", "row", ",", "ignore", "=", "{", "rate_column", "}", ")", ".", "index", "relocation_rates", ".", "loc", "[", "indexes", "]", "=", "row", "[", "rate_column", "]", "movers", "=", "relocation_rates", ".", "index", "[", "relocation_rates", ">", "np", ".", "random", ".", "random", "(", "len", "(", "choosers", ")", ")", "]", "logger", ".", "debug", "(", "'picked {} movers for relocation'", ".", "format", "(", "len", "(", "movers", ")", ")", ")", "logger", ".", "debug", "(", "'finish: find movers for relocation'", ")", "return", "movers" ]
Returns an array of the indexes of the `choosers` that are slated to move. Parameters ---------- choosers : pandas.DataFrame Table of agents from which to find movers. rates : pandas.DataFrame Table of relocation rates. Index is unused. Other columns describe filters on the `choosers` table so that different segments can have different relocation rates. Columns that ends with '_max' will be used to create a "less than" filters, columns that end with '_min' will be used to create "greater than or equal to" filters. A column with no suffix will be used to make an 'equal to' filter. An example `rates` structure: age_of_head_max age_of_head_min nan 65 65 40 In this example the `choosers` table would need to have an 'age_of_head' column on which to filter. nan should be used to flag filters that do not apply in a given row. rate_column : object Name of column in `rates` table that has relocation rates. Returns ------- movers : pandas.Index Suitable for indexing `choosers` by index.
[ "Returns", "an", "array", "of", "the", "indexes", "of", "the", "choosers", "that", "are", "slated", "to", "move", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/models/relocation.py#L16-L67
UDST/urbansim
urbansim/models/supplydemand.py
_calculate_adjustment
def _calculate_adjustment( lcm, choosers, alternatives, alt_segmenter, clip_change_low, clip_change_high, multiplier_func=None): """ Calculate adjustments to prices to compensate for supply and demand effects. Parameters ---------- lcm : LocationChoiceModel Used to calculate the probability of agents choosing among alternatives. Must be fully configured and fitted. choosers : pandas.DataFrame alternatives : pandas.DataFrame alt_segmenter : pandas.Series Will be used to segment alternatives and probabilities to do comparisons of supply and demand by submarket. clip_change_low : float The minimum amount by which to multiply prices each iteration. clip_change_high : float The maximum amount by which to multiply prices each iteration. multiplier_func : function (returns Series, boolean) A function which takes separate demand and supply Series and returns a tuple where the first item is a Series with the ratio of new price to old price (all indexes should be the same) - by default the ratio of demand to supply is the ratio of the new price to the old price. The second return value is a boolean which when True tells this module to stop looping (that convergence has been satisfied) Returns ------- alts_muliplier : pandas.Series Same index as `alternatives`, values clipped to `clip_change_low` and `clip_change_high`. submarkets_multiplier : pandas.Series Index is unique values from `alt_segmenter`, values are the ratio of demand / supply for each segment in `alt_segmenter`. finished : boolean boolean indicator that this adjustment should be considered the final adjustment (if True). If false, the iterative algorithm should continue. """ logger.debug('start: calculate supply and demand price adjustment ratio') # probabilities of agents choosing * number of agents = demand demand = lcm.summed_probabilities(choosers, alternatives) # group by submarket demand = demand.groupby(alt_segmenter.loc[demand.index].values).sum() # number of alternatives supply = alt_segmenter.value_counts() if multiplier_func is not None: multiplier, finished = multiplier_func(demand, supply) else: multiplier, finished = (demand / supply), False multiplier = multiplier.clip(clip_change_low, clip_change_high) # broadcast multiplier back to alternatives index alts_muliplier = multiplier.loc[alt_segmenter] alts_muliplier.index = alt_segmenter.index logger.debug( ('finish: calculate supply and demand price adjustment multiplier ' 'with mean multiplier {}').format(multiplier.mean())) return alts_muliplier, multiplier, finished
python
def _calculate_adjustment( lcm, choosers, alternatives, alt_segmenter, clip_change_low, clip_change_high, multiplier_func=None): logger.debug('start: calculate supply and demand price adjustment ratio') demand = lcm.summed_probabilities(choosers, alternatives) demand = demand.groupby(alt_segmenter.loc[demand.index].values).sum() supply = alt_segmenter.value_counts() if multiplier_func is not None: multiplier, finished = multiplier_func(demand, supply) else: multiplier, finished = (demand / supply), False multiplier = multiplier.clip(clip_change_low, clip_change_high) alts_muliplier = multiplier.loc[alt_segmenter] alts_muliplier.index = alt_segmenter.index logger.debug( ('finish: calculate supply and demand price adjustment multiplier ' 'with mean multiplier {}').format(multiplier.mean())) return alts_muliplier, multiplier, finished
[ "def", "_calculate_adjustment", "(", "lcm", ",", "choosers", ",", "alternatives", ",", "alt_segmenter", ",", "clip_change_low", ",", "clip_change_high", ",", "multiplier_func", "=", "None", ")", ":", "logger", ".", "debug", "(", "'start: calculate supply and demand price adjustment ratio'", ")", "# probabilities of agents choosing * number of agents = demand", "demand", "=", "lcm", ".", "summed_probabilities", "(", "choosers", ",", "alternatives", ")", "# group by submarket", "demand", "=", "demand", ".", "groupby", "(", "alt_segmenter", ".", "loc", "[", "demand", ".", "index", "]", ".", "values", ")", ".", "sum", "(", ")", "# number of alternatives", "supply", "=", "alt_segmenter", ".", "value_counts", "(", ")", "if", "multiplier_func", "is", "not", "None", ":", "multiplier", ",", "finished", "=", "multiplier_func", "(", "demand", ",", "supply", ")", "else", ":", "multiplier", ",", "finished", "=", "(", "demand", "/", "supply", ")", ",", "False", "multiplier", "=", "multiplier", ".", "clip", "(", "clip_change_low", ",", "clip_change_high", ")", "# broadcast multiplier back to alternatives index", "alts_muliplier", "=", "multiplier", ".", "loc", "[", "alt_segmenter", "]", "alts_muliplier", ".", "index", "=", "alt_segmenter", ".", "index", "logger", ".", "debug", "(", "(", "'finish: calculate supply and demand price adjustment multiplier '", "'with mean multiplier {}'", ")", ".", "format", "(", "multiplier", ".", "mean", "(", ")", ")", ")", "return", "alts_muliplier", ",", "multiplier", ",", "finished" ]
Calculate adjustments to prices to compensate for supply and demand effects. Parameters ---------- lcm : LocationChoiceModel Used to calculate the probability of agents choosing among alternatives. Must be fully configured and fitted. choosers : pandas.DataFrame alternatives : pandas.DataFrame alt_segmenter : pandas.Series Will be used to segment alternatives and probabilities to do comparisons of supply and demand by submarket. clip_change_low : float The minimum amount by which to multiply prices each iteration. clip_change_high : float The maximum amount by which to multiply prices each iteration. multiplier_func : function (returns Series, boolean) A function which takes separate demand and supply Series and returns a tuple where the first item is a Series with the ratio of new price to old price (all indexes should be the same) - by default the ratio of demand to supply is the ratio of the new price to the old price. The second return value is a boolean which when True tells this module to stop looping (that convergence has been satisfied) Returns ------- alts_muliplier : pandas.Series Same index as `alternatives`, values clipped to `clip_change_low` and `clip_change_high`. submarkets_multiplier : pandas.Series Index is unique values from `alt_segmenter`, values are the ratio of demand / supply for each segment in `alt_segmenter`. finished : boolean boolean indicator that this adjustment should be considered the final adjustment (if True). If false, the iterative algorithm should continue.
[ "Calculate", "adjustments", "to", "prices", "to", "compensate", "for", "supply", "and", "demand", "effects", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/models/supplydemand.py#L15-L81
UDST/urbansim
urbansim/models/supplydemand.py
supply_and_demand
def supply_and_demand( lcm, choosers, alternatives, alt_segmenter, price_col, base_multiplier=None, clip_change_low=0.75, clip_change_high=1.25, iterations=5, multiplier_func=None): """ Adjust real estate prices to compensate for supply and demand effects. Parameters ---------- lcm : LocationChoiceModel Used to calculate the probability of agents choosing among alternatives. Must be fully configured and fitted. choosers : pandas.DataFrame alternatives : pandas.DataFrame alt_segmenter : str, array, or pandas.Series Will be used to segment alternatives and probabilities to do comparisons of supply and demand by submarket. If a string, it is expected to be the name of a column in `alternatives`. If a Series it should have the same index as `alternatives`. price_col : str The name of the column in `alternatives` that corresponds to price. This column is what is adjusted by this model. base_multiplier : pandas.Series, optional A series describing a starting multiplier for submarket prices. Index should be submarket IDs. clip_change_low : float, optional The minimum amount by which to multiply prices each iteration. clip_change_high : float, optional The maximum amount by which to multiply prices each iteration. iterations : int, optional Number of times to update prices based on supply/demand comparisons. multiplier_func : function (returns Series, boolean) A function which takes separate demand and supply Series and returns a tuple where the first item is a Series with the ratio of new price to old price (all indexes should be the same) - by default the ratio of demand to supply is the ratio of the new price to the old price. The second return value is a boolean which when True tells this module to stop looping (that convergence has been satisfied) Returns ------- new_prices : pandas.Series Equivalent of the `price_col` in `alternatives`. submarkets_ratios : pandas.Series Price adjustment ratio for each submarket. If `base_multiplier` is given this will be a cummulative multiplier including the `base_multiplier` and the multipliers calculated for this year. """ logger.debug('start: calculating supply and demand price adjustment') # copy alternatives so we don't modify the user's original alternatives = alternatives.copy() # if alt_segmenter is a string, get the actual column for segmenting demand if isinstance(alt_segmenter, str): alt_segmenter = alternatives[alt_segmenter] elif isinstance(alt_segmenter, np.array): alt_segmenter = pd.Series(alt_segmenter, index=alternatives.index) choosers, alternatives = lcm.apply_predict_filters(choosers, alternatives) alt_segmenter = alt_segmenter.loc[alternatives.index] # check base ratio and apply it to prices if given if base_multiplier is not None: bm = base_multiplier.loc[alt_segmenter] bm.index = alt_segmenter.index alternatives[price_col] = alternatives[price_col] * bm base_multiplier = base_multiplier.copy() for _ in range(iterations): alts_muliplier, submarkets_multiplier, finished = _calculate_adjustment( lcm, choosers, alternatives, alt_segmenter, clip_change_low, clip_change_high, multiplier_func=multiplier_func) alternatives[price_col] = alternatives[price_col] * alts_muliplier # might need to initialize this for holding cumulative multiplier if base_multiplier is None: base_multiplier = pd.Series( np.ones(len(submarkets_multiplier)), index=submarkets_multiplier.index) base_multiplier *= submarkets_multiplier if finished: break logger.debug('finish: calculating supply and demand price adjustment') return alternatives[price_col], base_multiplier
python
def supply_and_demand( lcm, choosers, alternatives, alt_segmenter, price_col, base_multiplier=None, clip_change_low=0.75, clip_change_high=1.25, iterations=5, multiplier_func=None): logger.debug('start: calculating supply and demand price adjustment') alternatives = alternatives.copy() if isinstance(alt_segmenter, str): alt_segmenter = alternatives[alt_segmenter] elif isinstance(alt_segmenter, np.array): alt_segmenter = pd.Series(alt_segmenter, index=alternatives.index) choosers, alternatives = lcm.apply_predict_filters(choosers, alternatives) alt_segmenter = alt_segmenter.loc[alternatives.index] if base_multiplier is not None: bm = base_multiplier.loc[alt_segmenter] bm.index = alt_segmenter.index alternatives[price_col] = alternatives[price_col] * bm base_multiplier = base_multiplier.copy() for _ in range(iterations): alts_muliplier, submarkets_multiplier, finished = _calculate_adjustment( lcm, choosers, alternatives, alt_segmenter, clip_change_low, clip_change_high, multiplier_func=multiplier_func) alternatives[price_col] = alternatives[price_col] * alts_muliplier if base_multiplier is None: base_multiplier = pd.Series( np.ones(len(submarkets_multiplier)), index=submarkets_multiplier.index) base_multiplier *= submarkets_multiplier if finished: break logger.debug('finish: calculating supply and demand price adjustment') return alternatives[price_col], base_multiplier
[ "def", "supply_and_demand", "(", "lcm", ",", "choosers", ",", "alternatives", ",", "alt_segmenter", ",", "price_col", ",", "base_multiplier", "=", "None", ",", "clip_change_low", "=", "0.75", ",", "clip_change_high", "=", "1.25", ",", "iterations", "=", "5", ",", "multiplier_func", "=", "None", ")", ":", "logger", ".", "debug", "(", "'start: calculating supply and demand price adjustment'", ")", "# copy alternatives so we don't modify the user's original", "alternatives", "=", "alternatives", ".", "copy", "(", ")", "# if alt_segmenter is a string, get the actual column for segmenting demand", "if", "isinstance", "(", "alt_segmenter", ",", "str", ")", ":", "alt_segmenter", "=", "alternatives", "[", "alt_segmenter", "]", "elif", "isinstance", "(", "alt_segmenter", ",", "np", ".", "array", ")", ":", "alt_segmenter", "=", "pd", ".", "Series", "(", "alt_segmenter", ",", "index", "=", "alternatives", ".", "index", ")", "choosers", ",", "alternatives", "=", "lcm", ".", "apply_predict_filters", "(", "choosers", ",", "alternatives", ")", "alt_segmenter", "=", "alt_segmenter", ".", "loc", "[", "alternatives", ".", "index", "]", "# check base ratio and apply it to prices if given", "if", "base_multiplier", "is", "not", "None", ":", "bm", "=", "base_multiplier", ".", "loc", "[", "alt_segmenter", "]", "bm", ".", "index", "=", "alt_segmenter", ".", "index", "alternatives", "[", "price_col", "]", "=", "alternatives", "[", "price_col", "]", "*", "bm", "base_multiplier", "=", "base_multiplier", ".", "copy", "(", ")", "for", "_", "in", "range", "(", "iterations", ")", ":", "alts_muliplier", ",", "submarkets_multiplier", ",", "finished", "=", "_calculate_adjustment", "(", "lcm", ",", "choosers", ",", "alternatives", ",", "alt_segmenter", ",", "clip_change_low", ",", "clip_change_high", ",", "multiplier_func", "=", "multiplier_func", ")", "alternatives", "[", "price_col", "]", "=", "alternatives", "[", "price_col", "]", "*", "alts_muliplier", "# might need to initialize this for holding cumulative multiplier", "if", "base_multiplier", "is", "None", ":", "base_multiplier", "=", "pd", ".", "Series", "(", "np", ".", "ones", "(", "len", "(", "submarkets_multiplier", ")", ")", ",", "index", "=", "submarkets_multiplier", ".", "index", ")", "base_multiplier", "*=", "submarkets_multiplier", "if", "finished", ":", "break", "logger", ".", "debug", "(", "'finish: calculating supply and demand price adjustment'", ")", "return", "alternatives", "[", "price_col", "]", ",", "base_multiplier" ]
Adjust real estate prices to compensate for supply and demand effects. Parameters ---------- lcm : LocationChoiceModel Used to calculate the probability of agents choosing among alternatives. Must be fully configured and fitted. choosers : pandas.DataFrame alternatives : pandas.DataFrame alt_segmenter : str, array, or pandas.Series Will be used to segment alternatives and probabilities to do comparisons of supply and demand by submarket. If a string, it is expected to be the name of a column in `alternatives`. If a Series it should have the same index as `alternatives`. price_col : str The name of the column in `alternatives` that corresponds to price. This column is what is adjusted by this model. base_multiplier : pandas.Series, optional A series describing a starting multiplier for submarket prices. Index should be submarket IDs. clip_change_low : float, optional The minimum amount by which to multiply prices each iteration. clip_change_high : float, optional The maximum amount by which to multiply prices each iteration. iterations : int, optional Number of times to update prices based on supply/demand comparisons. multiplier_func : function (returns Series, boolean) A function which takes separate demand and supply Series and returns a tuple where the first item is a Series with the ratio of new price to old price (all indexes should be the same) - by default the ratio of demand to supply is the ratio of the new price to the old price. The second return value is a boolean which when True tells this module to stop looping (that convergence has been satisfied) Returns ------- new_prices : pandas.Series Equivalent of the `price_col` in `alternatives`. submarkets_ratios : pandas.Series Price adjustment ratio for each submarket. If `base_multiplier` is given this will be a cummulative multiplier including the `base_multiplier` and the multipliers calculated for this year.
[ "Adjust", "real", "estate", "prices", "to", "compensate", "for", "supply", "and", "demand", "effects", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/models/supplydemand.py#L84-L173
UDST/urbansim
urbansim/developer/developer.py
Developer._max_form
def _max_form(f, colname): """ Assumes dataframe with hierarchical columns with first index equal to the use and second index equal to the attribute. e.g. f.columns equal to:: mixedoffice building_cost building_revenue building_size max_profit max_profit_far total_cost industrial building_cost building_revenue building_size max_profit max_profit_far total_cost """ df = f.stack(level=0)[[colname]].stack().unstack(level=1).reset_index(level=1, drop=True) return df.idxmax(axis=1)
python
def _max_form(f, colname): df = f.stack(level=0)[[colname]].stack().unstack(level=1).reset_index(level=1, drop=True) return df.idxmax(axis=1)
[ "def", "_max_form", "(", "f", ",", "colname", ")", ":", "df", "=", "f", ".", "stack", "(", "level", "=", "0", ")", "[", "[", "colname", "]", "]", ".", "stack", "(", ")", ".", "unstack", "(", "level", "=", "1", ")", ".", "reset_index", "(", "level", "=", "1", ",", "drop", "=", "True", ")", "return", "df", ".", "idxmax", "(", "axis", "=", "1", ")" ]
Assumes dataframe with hierarchical columns with first index equal to the use and second index equal to the attribute. e.g. f.columns equal to:: mixedoffice building_cost building_revenue building_size max_profit max_profit_far total_cost industrial building_cost building_revenue building_size max_profit max_profit_far total_cost
[ "Assumes", "dataframe", "with", "hierarchical", "columns", "with", "first", "index", "equal", "to", "the", "use", "and", "second", "index", "equal", "to", "the", "attribute", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/developer/developer.py#L23-L44
UDST/urbansim
urbansim/developer/developer.py
Developer.keep_form_with_max_profit
def keep_form_with_max_profit(self, forms=None): """ This converts the dataframe, which shows all profitable forms, to the form with the greatest profit, so that more profitable forms outcompete less profitable forms. Parameters ---------- forms: list of strings List of forms which compete which other. Can leave some out. Returns ------- Nothing. Goes from a multi-index to a single index with only the most profitable form. """ f = self.feasibility if forms is not None: f = f[forms] if len(f) > 0: mu = self._max_form(f, "max_profit") indexes = [tuple(x) for x in mu.reset_index().values] else: indexes = [] df = f.stack(level=0).loc[indexes] df.index.names = ["parcel_id", "form"] df = df.reset_index(level=1) return df
python
def keep_form_with_max_profit(self, forms=None): f = self.feasibility if forms is not None: f = f[forms] if len(f) > 0: mu = self._max_form(f, "max_profit") indexes = [tuple(x) for x in mu.reset_index().values] else: indexes = [] df = f.stack(level=0).loc[indexes] df.index.names = ["parcel_id", "form"] df = df.reset_index(level=1) return df
[ "def", "keep_form_with_max_profit", "(", "self", ",", "forms", "=", "None", ")", ":", "f", "=", "self", ".", "feasibility", "if", "forms", "is", "not", "None", ":", "f", "=", "f", "[", "forms", "]", "if", "len", "(", "f", ")", ">", "0", ":", "mu", "=", "self", ".", "_max_form", "(", "f", ",", "\"max_profit\"", ")", "indexes", "=", "[", "tuple", "(", "x", ")", "for", "x", "in", "mu", ".", "reset_index", "(", ")", ".", "values", "]", "else", ":", "indexes", "=", "[", "]", "df", "=", "f", ".", "stack", "(", "level", "=", "0", ")", ".", "loc", "[", "indexes", "]", "df", ".", "index", ".", "names", "=", "[", "\"parcel_id\"", ",", "\"form\"", "]", "df", "=", "df", ".", "reset_index", "(", "level", "=", "1", ")", "return", "df" ]
This converts the dataframe, which shows all profitable forms, to the form with the greatest profit, so that more profitable forms outcompete less profitable forms. Parameters ---------- forms: list of strings List of forms which compete which other. Can leave some out. Returns ------- Nothing. Goes from a multi-index to a single index with only the most profitable form.
[ "This", "converts", "the", "dataframe", "which", "shows", "all", "profitable", "forms", "to", "the", "form", "with", "the", "greatest", "profit", "so", "that", "more", "profitable", "forms", "outcompete", "less", "profitable", "forms", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/developer/developer.py#L46-L75
UDST/urbansim
urbansim/developer/developer.py
Developer.compute_units_to_build
def compute_units_to_build(num_agents, num_units, target_vacancy): """ Compute number of units to build to match target vacancy. Parameters ---------- num_agents : int number of agents that need units in the region num_units : int number of units in buildings target_vacancy : float (0-1.0) target vacancy rate Returns ------- number_of_units : int the number of units that need to be built """ print("Number of agents: {:,}".format(num_agents)) print("Number of agent spaces: {:,}".format(int(num_units))) assert target_vacancy < 1.0 target_units = int(max(num_agents / (1 - target_vacancy) - num_units, 0)) print("Current vacancy = {:.2f}" .format(1 - num_agents / float(num_units))) print("Target vacancy = {:.2f}, target of new units = {:,}" .format(target_vacancy, target_units)) return target_units
python
def compute_units_to_build(num_agents, num_units, target_vacancy): print("Number of agents: {:,}".format(num_agents)) print("Number of agent spaces: {:,}".format(int(num_units))) assert target_vacancy < 1.0 target_units = int(max(num_agents / (1 - target_vacancy) - num_units, 0)) print("Current vacancy = {:.2f}" .format(1 - num_agents / float(num_units))) print("Target vacancy = {:.2f}, target of new units = {:,}" .format(target_vacancy, target_units)) return target_units
[ "def", "compute_units_to_build", "(", "num_agents", ",", "num_units", ",", "target_vacancy", ")", ":", "print", "(", "\"Number of agents: {:,}\"", ".", "format", "(", "num_agents", ")", ")", "print", "(", "\"Number of agent spaces: {:,}\"", ".", "format", "(", "int", "(", "num_units", ")", ")", ")", "assert", "target_vacancy", "<", "1.0", "target_units", "=", "int", "(", "max", "(", "num_agents", "/", "(", "1", "-", "target_vacancy", ")", "-", "num_units", ",", "0", ")", ")", "print", "(", "\"Current vacancy = {:.2f}\"", ".", "format", "(", "1", "-", "num_agents", "/", "float", "(", "num_units", ")", ")", ")", "print", "(", "\"Target vacancy = {:.2f}, target of new units = {:,}\"", ".", "format", "(", "target_vacancy", ",", "target_units", ")", ")", "return", "target_units" ]
Compute number of units to build to match target vacancy. Parameters ---------- num_agents : int number of agents that need units in the region num_units : int number of units in buildings target_vacancy : float (0-1.0) target vacancy rate Returns ------- number_of_units : int the number of units that need to be built
[ "Compute", "number", "of", "units", "to", "build", "to", "match", "target", "vacancy", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/developer/developer.py#L78-L104
UDST/urbansim
urbansim/developer/developer.py
Developer.pick
def pick(self, form, target_units, parcel_size, ave_unit_size, current_units, max_parcel_size=200000, min_unit_size=400, drop_after_build=True, residential=True, bldg_sqft_per_job=400.0, profit_to_prob_func=None): """ Choose the buildings from the list that are feasible to build in order to match the specified demand. Parameters ---------- form : string or list One or more of the building forms from the pro forma specification - e.g. "residential" or "mixedresidential" - these are configuration parameters passed previously to the pro forma. If more than one form is passed the forms compete with each other (based on profitability) for which one gets built in order to meet demand. target_units : int The number of units to build. For non-residential buildings this should be passed as the number of job spaces that need to be created. parcel_size : series The size of the parcels. This was passed to feasibility as well, but should be passed here as well. Index should be parcel_ids. ave_unit_size : series The average residential unit size around each parcel - this is indexed by parcel, but is usually a disaggregated version of a zonal or accessibility aggregation. bldg_sqft_per_job : float (default 400.0) The average square feet per job for this building form. min_unit_size : float Values less than this number in ave_unit_size will be set to this number. Deals with cases where units are currently not built. current_units : series The current number of units on the parcel. Is used to compute the net number of units produced by the developer model. Many times the developer model is redeveloping units (demolishing them) and is trying to meet a total number of net units produced. max_parcel_size : float Parcels larger than this size will not be considered for development - usually large parcels should be specified manually in a development projects table. drop_after_build : bool Whether or not to drop parcels from consideration after they have been chosen for development. Usually this is true so as to not develop the same parcel twice. residential: bool If creating non-residential buildings set this to false and developer will fill in job_spaces rather than residential_units profit_to_prob_func: function As there are so many ways to turn the development feasibility into a probability to select it for building, the user may pass a function which takes the feasibility dataframe and returns a series of probabilities. If no function is passed, the behavior of this method will not change Returns ------- None if thar are no feasible buildings new_buildings : dataframe DataFrame of buildings to add. These buildings are rows from the DataFrame that is returned from feasibility. """ if len(self.feasibility) == 0: # no feasible buildings, might as well bail return if form is None: df = self.feasibility elif isinstance(form, list): df = self.keep_form_with_max_profit(form) else: df = self.feasibility[form] # feasible buildings only for this building type df = df[df.max_profit_far > 0] ave_unit_size[ave_unit_size < min_unit_size] = min_unit_size df["ave_unit_size"] = ave_unit_size df["parcel_size"] = parcel_size df['current_units'] = current_units df = df[df.parcel_size < max_parcel_size] df['residential_units'] = (df.residential_sqft / df.ave_unit_size).round() df['job_spaces'] = (df.non_residential_sqft / bldg_sqft_per_job).round() if residential: df['net_units'] = df.residential_units - df.current_units else: df['net_units'] = df.job_spaces - df.current_units df = df[df.net_units > 0] if len(df) == 0: print("WARNING THERE ARE NO FEASIBLE BUILDING TO CHOOSE FROM") return # print "Describe of net units\n", df.net_units.describe() print("Sum of net units that are profitable: {:,}" .format(int(df.net_units.sum()))) if profit_to_prob_func: p = profit_to_prob_func(df) else: df['max_profit_per_size'] = df.max_profit / df.parcel_size p = df.max_profit_per_size.values / df.max_profit_per_size.sum() if df.net_units.sum() < target_units: print("WARNING THERE WERE NOT ENOUGH PROFITABLE UNITS TO", "MATCH DEMAND") build_idx = df.index.values elif target_units <= 0: build_idx = [] else: # we don't know how many developments we will need, as they differ in net_units. # If all developments have net_units of 1 than we need target_units of them. # So we choose the smaller of available developments and target_units. choices = np.random.choice(df.index.values, size=min(len(df.index), target_units), replace=False, p=p) tot_units = df.net_units.loc[choices].values.cumsum() ind = int(np.searchsorted(tot_units, target_units, side="left")) + 1 build_idx = choices[:ind] if drop_after_build: self.feasibility = self.feasibility.drop(build_idx) new_df = df.loc[build_idx] new_df.index.name = "parcel_id" return new_df.reset_index()
python
def pick(self, form, target_units, parcel_size, ave_unit_size, current_units, max_parcel_size=200000, min_unit_size=400, drop_after_build=True, residential=True, bldg_sqft_per_job=400.0, profit_to_prob_func=None): if len(self.feasibility) == 0: return if form is None: df = self.feasibility elif isinstance(form, list): df = self.keep_form_with_max_profit(form) else: df = self.feasibility[form] df = df[df.max_profit_far > 0] ave_unit_size[ave_unit_size < min_unit_size] = min_unit_size df["ave_unit_size"] = ave_unit_size df["parcel_size"] = parcel_size df['current_units'] = current_units df = df[df.parcel_size < max_parcel_size] df['residential_units'] = (df.residential_sqft / df.ave_unit_size).round() df['job_spaces'] = (df.non_residential_sqft / bldg_sqft_per_job).round() if residential: df['net_units'] = df.residential_units - df.current_units else: df['net_units'] = df.job_spaces - df.current_units df = df[df.net_units > 0] if len(df) == 0: print("WARNING THERE ARE NO FEASIBLE BUILDING TO CHOOSE FROM") return print("Sum of net units that are profitable: {:,}" .format(int(df.net_units.sum()))) if profit_to_prob_func: p = profit_to_prob_func(df) else: df['max_profit_per_size'] = df.max_profit / df.parcel_size p = df.max_profit_per_size.values / df.max_profit_per_size.sum() if df.net_units.sum() < target_units: print("WARNING THERE WERE NOT ENOUGH PROFITABLE UNITS TO", "MATCH DEMAND") build_idx = df.index.values elif target_units <= 0: build_idx = [] else: choices = np.random.choice(df.index.values, size=min(len(df.index), target_units), replace=False, p=p) tot_units = df.net_units.loc[choices].values.cumsum() ind = int(np.searchsorted(tot_units, target_units, side="left")) + 1 build_idx = choices[:ind] if drop_after_build: self.feasibility = self.feasibility.drop(build_idx) new_df = df.loc[build_idx] new_df.index.name = "parcel_id" return new_df.reset_index()
[ "def", "pick", "(", "self", ",", "form", ",", "target_units", ",", "parcel_size", ",", "ave_unit_size", ",", "current_units", ",", "max_parcel_size", "=", "200000", ",", "min_unit_size", "=", "400", ",", "drop_after_build", "=", "True", ",", "residential", "=", "True", ",", "bldg_sqft_per_job", "=", "400.0", ",", "profit_to_prob_func", "=", "None", ")", ":", "if", "len", "(", "self", ".", "feasibility", ")", "==", "0", ":", "# no feasible buildings, might as well bail", "return", "if", "form", "is", "None", ":", "df", "=", "self", ".", "feasibility", "elif", "isinstance", "(", "form", ",", "list", ")", ":", "df", "=", "self", ".", "keep_form_with_max_profit", "(", "form", ")", "else", ":", "df", "=", "self", ".", "feasibility", "[", "form", "]", "# feasible buildings only for this building type", "df", "=", "df", "[", "df", ".", "max_profit_far", ">", "0", "]", "ave_unit_size", "[", "ave_unit_size", "<", "min_unit_size", "]", "=", "min_unit_size", "df", "[", "\"ave_unit_size\"", "]", "=", "ave_unit_size", "df", "[", "\"parcel_size\"", "]", "=", "parcel_size", "df", "[", "'current_units'", "]", "=", "current_units", "df", "=", "df", "[", "df", ".", "parcel_size", "<", "max_parcel_size", "]", "df", "[", "'residential_units'", "]", "=", "(", "df", ".", "residential_sqft", "/", "df", ".", "ave_unit_size", ")", ".", "round", "(", ")", "df", "[", "'job_spaces'", "]", "=", "(", "df", ".", "non_residential_sqft", "/", "bldg_sqft_per_job", ")", ".", "round", "(", ")", "if", "residential", ":", "df", "[", "'net_units'", "]", "=", "df", ".", "residential_units", "-", "df", ".", "current_units", "else", ":", "df", "[", "'net_units'", "]", "=", "df", ".", "job_spaces", "-", "df", ".", "current_units", "df", "=", "df", "[", "df", ".", "net_units", ">", "0", "]", "if", "len", "(", "df", ")", "==", "0", ":", "print", "(", "\"WARNING THERE ARE NO FEASIBLE BUILDING TO CHOOSE FROM\"", ")", "return", "# print \"Describe of net units\\n\", df.net_units.describe()", "print", "(", "\"Sum of net units that are profitable: {:,}\"", ".", "format", "(", "int", "(", "df", ".", "net_units", ".", "sum", "(", ")", ")", ")", ")", "if", "profit_to_prob_func", ":", "p", "=", "profit_to_prob_func", "(", "df", ")", "else", ":", "df", "[", "'max_profit_per_size'", "]", "=", "df", ".", "max_profit", "/", "df", ".", "parcel_size", "p", "=", "df", ".", "max_profit_per_size", ".", "values", "/", "df", ".", "max_profit_per_size", ".", "sum", "(", ")", "if", "df", ".", "net_units", ".", "sum", "(", ")", "<", "target_units", ":", "print", "(", "\"WARNING THERE WERE NOT ENOUGH PROFITABLE UNITS TO\"", ",", "\"MATCH DEMAND\"", ")", "build_idx", "=", "df", ".", "index", ".", "values", "elif", "target_units", "<=", "0", ":", "build_idx", "=", "[", "]", "else", ":", "# we don't know how many developments we will need, as they differ in net_units.", "# If all developments have net_units of 1 than we need target_units of them.", "# So we choose the smaller of available developments and target_units.", "choices", "=", "np", ".", "random", ".", "choice", "(", "df", ".", "index", ".", "values", ",", "size", "=", "min", "(", "len", "(", "df", ".", "index", ")", ",", "target_units", ")", ",", "replace", "=", "False", ",", "p", "=", "p", ")", "tot_units", "=", "df", ".", "net_units", ".", "loc", "[", "choices", "]", ".", "values", ".", "cumsum", "(", ")", "ind", "=", "int", "(", "np", ".", "searchsorted", "(", "tot_units", ",", "target_units", ",", "side", "=", "\"left\"", ")", ")", "+", "1", "build_idx", "=", "choices", "[", ":", "ind", "]", "if", "drop_after_build", ":", "self", ".", "feasibility", "=", "self", ".", "feasibility", ".", "drop", "(", "build_idx", ")", "new_df", "=", "df", ".", "loc", "[", "build_idx", "]", "new_df", ".", "index", ".", "name", "=", "\"parcel_id\"", "return", "new_df", ".", "reset_index", "(", ")" ]
Choose the buildings from the list that are feasible to build in order to match the specified demand. Parameters ---------- form : string or list One or more of the building forms from the pro forma specification - e.g. "residential" or "mixedresidential" - these are configuration parameters passed previously to the pro forma. If more than one form is passed the forms compete with each other (based on profitability) for which one gets built in order to meet demand. target_units : int The number of units to build. For non-residential buildings this should be passed as the number of job spaces that need to be created. parcel_size : series The size of the parcels. This was passed to feasibility as well, but should be passed here as well. Index should be parcel_ids. ave_unit_size : series The average residential unit size around each parcel - this is indexed by parcel, but is usually a disaggregated version of a zonal or accessibility aggregation. bldg_sqft_per_job : float (default 400.0) The average square feet per job for this building form. min_unit_size : float Values less than this number in ave_unit_size will be set to this number. Deals with cases where units are currently not built. current_units : series The current number of units on the parcel. Is used to compute the net number of units produced by the developer model. Many times the developer model is redeveloping units (demolishing them) and is trying to meet a total number of net units produced. max_parcel_size : float Parcels larger than this size will not be considered for development - usually large parcels should be specified manually in a development projects table. drop_after_build : bool Whether or not to drop parcels from consideration after they have been chosen for development. Usually this is true so as to not develop the same parcel twice. residential: bool If creating non-residential buildings set this to false and developer will fill in job_spaces rather than residential_units profit_to_prob_func: function As there are so many ways to turn the development feasibility into a probability to select it for building, the user may pass a function which takes the feasibility dataframe and returns a series of probabilities. If no function is passed, the behavior of this method will not change Returns ------- None if thar are no feasible buildings new_buildings : dataframe DataFrame of buildings to add. These buildings are rows from the DataFrame that is returned from feasibility.
[ "Choose", "the", "buildings", "from", "the", "list", "that", "are", "feasible", "to", "build", "in", "order", "to", "match", "the", "specified", "demand", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/developer/developer.py#L106-L231
UDST/urbansim
urbansim/developer/developer.py
Developer.merge
def merge(old_df, new_df, return_index=False): """ Merge two dataframes of buildings. The old dataframe is usually the buildings dataset and the new dataframe is a modified (by the user) version of what is returned by the pick method. Parameters ---------- old_df : dataframe Current set of buildings new_df : dataframe New buildings to add, usually comes from this module return_index : bool If return_index is true, this method will return the new index of new_df (which changes in order to create a unique index after the merge) Returns ------- df : dataframe Combined DataFrame of buildings, makes sure indexes don't overlap index : pd.Index If and only if return_index is True, return the new index for the new_df dataframe (which changes in order to create a unique index after the merge) """ maxind = np.max(old_df.index.values) new_df = new_df.reset_index(drop=True) new_df.index = new_df.index + maxind + 1 concat_df = pd.concat([old_df, new_df], verify_integrity=True) concat_df.index.name = 'building_id' if return_index: return concat_df, new_df.index return concat_df
python
def merge(old_df, new_df, return_index=False): maxind = np.max(old_df.index.values) new_df = new_df.reset_index(drop=True) new_df.index = new_df.index + maxind + 1 concat_df = pd.concat([old_df, new_df], verify_integrity=True) concat_df.index.name = 'building_id' if return_index: return concat_df, new_df.index return concat_df
[ "def", "merge", "(", "old_df", ",", "new_df", ",", "return_index", "=", "False", ")", ":", "maxind", "=", "np", ".", "max", "(", "old_df", ".", "index", ".", "values", ")", "new_df", "=", "new_df", ".", "reset_index", "(", "drop", "=", "True", ")", "new_df", ".", "index", "=", "new_df", ".", "index", "+", "maxind", "+", "1", "concat_df", "=", "pd", ".", "concat", "(", "[", "old_df", ",", "new_df", "]", ",", "verify_integrity", "=", "True", ")", "concat_df", ".", "index", ".", "name", "=", "'building_id'", "if", "return_index", ":", "return", "concat_df", ",", "new_df", ".", "index", "return", "concat_df" ]
Merge two dataframes of buildings. The old dataframe is usually the buildings dataset and the new dataframe is a modified (by the user) version of what is returned by the pick method. Parameters ---------- old_df : dataframe Current set of buildings new_df : dataframe New buildings to add, usually comes from this module return_index : bool If return_index is true, this method will return the new index of new_df (which changes in order to create a unique index after the merge) Returns ------- df : dataframe Combined DataFrame of buildings, makes sure indexes don't overlap index : pd.Index If and only if return_index is True, return the new index for the new_df dataframe (which changes in order to create a unique index after the merge)
[ "Merge", "two", "dataframes", "of", "buildings", ".", "The", "old", "dataframe", "is", "usually", "the", "buildings", "dataset", "and", "the", "new", "dataframe", "is", "a", "modified", "(", "by", "the", "user", ")", "version", "of", "what", "is", "returned", "by", "the", "pick", "method", "." ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/developer/developer.py#L234-L269
UDST/urbansim
urbansim/maps/dframe_explorer.py
start
def start(views, center=[37.7792, -122.2191], zoom=11, shape_json='data/zones.json', geom_name='ZONE_ID', # from JSON file join_name='zone_id', # from data frames precision=8, port=8765, host='localhost', testing=False): """ Start the web service which serves the Pandas queries and generates the HTML for the map. You will need to open a web browser and navigate to http://localhost:8765 (or the specified port) Parameters ---------- views : Python dictionary This is the data that will be displayed in the maps. Keys are strings (table names) and values are dataframes. Each data frame should have a column with the name specified as join_name below center : a Python list with two floats The initial latitude and longitude of the center of the map zoom : int The initial zoom level of the map shape_json : str The path to the geojson file which contains that shapes that will be displayed geom_name : str The field name from the JSON file which contains the id of the geometry join_name : str The column name from the dataframes passed as views (must be in each view) which joins to geom_name in the shapes precision : int The precision of values to show in the legend on the map port : int The port for the web service to respond on host : str The hostname to run the web service from testing : bool Whether to print extra debug information Returns ------- Does not return - takes over control of the thread and responds to queries from a web browser """ global DFRAMES, CONFIG DFRAMES = {str(k): views[k] for k in views} root = "http://{}:{}/".format(host, port) config = { 'center': str(center), 'zoom': zoom, 'shape_json': shape_json, 'geom_name': geom_name, 'join_name': join_name, 'precision': precision, 'root': root } for k in views: if join_name not in views[k].columns: raise Exception("Join name must be present on all dataframes - " "'%s' not present on '%s'" % (join_name, k)) config['schema'] = simplejson.dumps(get_schema()) CONFIG = config if testing: return run(host=host, port=port, debug=True)
python
def start(views, center=[37.7792, -122.2191], zoom=11, shape_json='data/zones.json', geom_name='ZONE_ID', join_name='zone_id', precision=8, port=8765, host='localhost', testing=False): global DFRAMES, CONFIG DFRAMES = {str(k): views[k] for k in views} root = "http://{}:{}/".format(host, port) config = { 'center': str(center), 'zoom': zoom, 'shape_json': shape_json, 'geom_name': geom_name, 'join_name': join_name, 'precision': precision, 'root': root } for k in views: if join_name not in views[k].columns: raise Exception("Join name must be present on all dataframes - " "'%s' not present on '%s'" % (join_name, k)) config['schema'] = simplejson.dumps(get_schema()) CONFIG = config if testing: return run(host=host, port=port, debug=True)
[ "def", "start", "(", "views", ",", "center", "=", "[", "37.7792", ",", "-", "122.2191", "]", ",", "zoom", "=", "11", ",", "shape_json", "=", "'data/zones.json'", ",", "geom_name", "=", "'ZONE_ID'", ",", "# from JSON file", "join_name", "=", "'zone_id'", ",", "# from data frames", "precision", "=", "8", ",", "port", "=", "8765", ",", "host", "=", "'localhost'", ",", "testing", "=", "False", ")", ":", "global", "DFRAMES", ",", "CONFIG", "DFRAMES", "=", "{", "str", "(", "k", ")", ":", "views", "[", "k", "]", "for", "k", "in", "views", "}", "root", "=", "\"http://{}:{}/\"", ".", "format", "(", "host", ",", "port", ")", "config", "=", "{", "'center'", ":", "str", "(", "center", ")", ",", "'zoom'", ":", "zoom", ",", "'shape_json'", ":", "shape_json", ",", "'geom_name'", ":", "geom_name", ",", "'join_name'", ":", "join_name", ",", "'precision'", ":", "precision", ",", "'root'", ":", "root", "}", "for", "k", "in", "views", ":", "if", "join_name", "not", "in", "views", "[", "k", "]", ".", "columns", ":", "raise", "Exception", "(", "\"Join name must be present on all dataframes - \"", "\"'%s' not present on '%s'\"", "%", "(", "join_name", ",", "k", ")", ")", "config", "[", "'schema'", "]", "=", "simplejson", ".", "dumps", "(", "get_schema", "(", ")", ")", "CONFIG", "=", "config", "if", "testing", ":", "return", "run", "(", "host", "=", "host", ",", "port", "=", "port", ",", "debug", "=", "True", ")" ]
Start the web service which serves the Pandas queries and generates the HTML for the map. You will need to open a web browser and navigate to http://localhost:8765 (or the specified port) Parameters ---------- views : Python dictionary This is the data that will be displayed in the maps. Keys are strings (table names) and values are dataframes. Each data frame should have a column with the name specified as join_name below center : a Python list with two floats The initial latitude and longitude of the center of the map zoom : int The initial zoom level of the map shape_json : str The path to the geojson file which contains that shapes that will be displayed geom_name : str The field name from the JSON file which contains the id of the geometry join_name : str The column name from the dataframes passed as views (must be in each view) which joins to geom_name in the shapes precision : int The precision of values to show in the legend on the map port : int The port for the web service to respond on host : str The hostname to run the web service from testing : bool Whether to print extra debug information Returns ------- Does not return - takes over control of the thread and responds to queries from a web browser
[ "Start", "the", "web", "service", "which", "serves", "the", "Pandas", "queries", "and", "generates", "the", "HTML", "for", "the", "map", ".", "You", "will", "need", "to", "open", "a", "web", "browser", "and", "navigate", "to", "http", ":", "//", "localhost", ":", "8765", "(", "or", "the", "specified", "port", ")" ]
train
https://github.com/UDST/urbansim/blob/79f815a6503e109f50be270cee92d0f4a34f49ef/urbansim/maps/dframe_explorer.py#L69-L144
linkedin/luminol
src/luminol/__init__.py
Luminol._analyze_root_causes
def _analyze_root_causes(self): """ Conduct root cause analysis. The first metric of the list is taken as the root cause right now. """ causes = {} for a in self.anomalies: try: causes[a] = self.correlations[a][0] except IndexError: raise exceptions.InvalidDataFormat('luminol.luminol: dict correlations contains empty list.') self.causes = causes
python
def _analyze_root_causes(self): causes = {} for a in self.anomalies: try: causes[a] = self.correlations[a][0] except IndexError: raise exceptions.InvalidDataFormat('luminol.luminol: dict correlations contains empty list.') self.causes = causes
[ "def", "_analyze_root_causes", "(", "self", ")", ":", "causes", "=", "{", "}", "for", "a", "in", "self", ".", "anomalies", ":", "try", ":", "causes", "[", "a", "]", "=", "self", ".", "correlations", "[", "a", "]", "[", "0", "]", "except", "IndexError", ":", "raise", "exceptions", ".", "InvalidDataFormat", "(", "'luminol.luminol: dict correlations contains empty list.'", ")", "self", ".", "causes", "=", "causes" ]
Conduct root cause analysis. The first metric of the list is taken as the root cause right now.
[ "Conduct", "root", "cause", "analysis", ".", "The", "first", "metric", "of", "the", "list", "is", "taken", "as", "the", "root", "cause", "right", "now", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/__init__.py#L32-L43
linkedin/luminol
src/luminol/correlator.py
Correlator._load
def _load(self, time_series): """ Load time series into a TimeSeries object. :param timeseries: a TimeSeries, a dictionary or a path to a csv file(str). :return TimeSeries: a TimeSeries object. """ if isinstance(time_series, TimeSeries): return time_series if isinstance(time_series, dict): return TimeSeries(time_series) return TimeSeries(utils.read_csv(time_series))
python
def _load(self, time_series): if isinstance(time_series, TimeSeries): return time_series if isinstance(time_series, dict): return TimeSeries(time_series) return TimeSeries(utils.read_csv(time_series))
[ "def", "_load", "(", "self", ",", "time_series", ")", ":", "if", "isinstance", "(", "time_series", ",", "TimeSeries", ")", ":", "return", "time_series", "if", "isinstance", "(", "time_series", ",", "dict", ")", ":", "return", "TimeSeries", "(", "time_series", ")", "return", "TimeSeries", "(", "utils", ".", "read_csv", "(", "time_series", ")", ")" ]
Load time series into a TimeSeries object. :param timeseries: a TimeSeries, a dictionary or a path to a csv file(str). :return TimeSeries: a TimeSeries object.
[ "Load", "time", "series", "into", "a", "TimeSeries", "object", ".", ":", "param", "timeseries", ":", "a", "TimeSeries", "a", "dictionary", "or", "a", "path", "to", "a", "csv", "file", "(", "str", ")", ".", ":", "return", "TimeSeries", ":", "a", "TimeSeries", "object", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/correlator.py#L60-L70
linkedin/luminol
src/luminol/correlator.py
Correlator._get_algorithm_and_params
def _get_algorithm_and_params(self, algorithm_name, algorithm_params): """ Get the specific algorithm and merge the algorithm params. :param str algorithm: name of the algorithm to use. :param dict algorithm_params: additional params for the specific algorithm. """ algorithm_name = algorithm_name or CORRELATOR_ALGORITHM try: self.algorithm = correlator_algorithms[algorithm_name] except KeyError: raise exceptions.AlgorithmNotFound('luminol.Correlator: ' + str(algorithm_name) + ' not found.') # Merge parameters. if algorithm_params: if not isinstance(algorithm_params, dict): raise exceptions.InvalidDataFormat('luminol.Correlator: algorithm_params passed is not a dictionary.') else: # self.algorithm_params = dict(algorithm_params.items() + self.algorithm_params.items()) self.algorithm_params = self.algorithm_params.copy() self.algorithm_params.update(algorithm_params)
python
def _get_algorithm_and_params(self, algorithm_name, algorithm_params): algorithm_name = algorithm_name or CORRELATOR_ALGORITHM try: self.algorithm = correlator_algorithms[algorithm_name] except KeyError: raise exceptions.AlgorithmNotFound('luminol.Correlator: ' + str(algorithm_name) + ' not found.') if algorithm_params: if not isinstance(algorithm_params, dict): raise exceptions.InvalidDataFormat('luminol.Correlator: algorithm_params passed is not a dictionary.') else: self.algorithm_params = self.algorithm_params.copy() self.algorithm_params.update(algorithm_params)
[ "def", "_get_algorithm_and_params", "(", "self", ",", "algorithm_name", ",", "algorithm_params", ")", ":", "algorithm_name", "=", "algorithm_name", "or", "CORRELATOR_ALGORITHM", "try", ":", "self", ".", "algorithm", "=", "correlator_algorithms", "[", "algorithm_name", "]", "except", "KeyError", ":", "raise", "exceptions", ".", "AlgorithmNotFound", "(", "'luminol.Correlator: '", "+", "str", "(", "algorithm_name", ")", "+", "' not found.'", ")", "# Merge parameters.", "if", "algorithm_params", ":", "if", "not", "isinstance", "(", "algorithm_params", ",", "dict", ")", ":", "raise", "exceptions", ".", "InvalidDataFormat", "(", "'luminol.Correlator: algorithm_params passed is not a dictionary.'", ")", "else", ":", "# self.algorithm_params = dict(algorithm_params.items() + self.algorithm_params.items())", "self", ".", "algorithm_params", "=", "self", ".", "algorithm_params", ".", "copy", "(", ")", "self", ".", "algorithm_params", ".", "update", "(", "algorithm_params", ")" ]
Get the specific algorithm and merge the algorithm params. :param str algorithm: name of the algorithm to use. :param dict algorithm_params: additional params for the specific algorithm.
[ "Get", "the", "specific", "algorithm", "and", "merge", "the", "algorithm", "params", ".", ":", "param", "str", "algorithm", ":", "name", "of", "the", "algorithm", "to", "use", ".", ":", "param", "dict", "algorithm_params", ":", "additional", "params", "for", "the", "specific", "algorithm", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/correlator.py#L72-L90
linkedin/luminol
src/luminol/correlator.py
Correlator._sanity_check
def _sanity_check(self): """ Check if the time series have more than two data points. """ if len(self.time_series_a) < 2 or len(self.time_series_b) < 2: raise exceptions.NotEnoughDataPoints('luminol.Correlator: Too few data points!')
python
def _sanity_check(self): if len(self.time_series_a) < 2 or len(self.time_series_b) < 2: raise exceptions.NotEnoughDataPoints('luminol.Correlator: Too few data points!')
[ "def", "_sanity_check", "(", "self", ")", ":", "if", "len", "(", "self", ".", "time_series_a", ")", "<", "2", "or", "len", "(", "self", ".", "time_series_b", ")", "<", "2", ":", "raise", "exceptions", ".", "NotEnoughDataPoints", "(", "'luminol.Correlator: Too few data points!'", ")" ]
Check if the time series have more than two data points.
[ "Check", "if", "the", "time", "series", "have", "more", "than", "two", "data", "points", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/correlator.py#L92-L97
linkedin/luminol
src/luminol/correlator.py
Correlator._correlate
def _correlate(self): """ Run correlation algorithm. """ a = self.algorithm(**self.algorithm_params) self.correlation_result = a.run()
python
def _correlate(self): a = self.algorithm(**self.algorithm_params) self.correlation_result = a.run()
[ "def", "_correlate", "(", "self", ")", ":", "a", "=", "self", ".", "algorithm", "(", "*", "*", "self", ".", "algorithm_params", ")", "self", ".", "correlation_result", "=", "a", ".", "run", "(", ")" ]
Run correlation algorithm.
[ "Run", "correlation", "algorithm", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/correlator.py#L99-L104
linkedin/luminol
src/luminol/correlator.py
Correlator.is_correlated
def is_correlated(self, threshold=0): """ Compare with a threshold to determine whether two timeseries correlate to each other. :return: a CorrelationResult object if two time series correlate otherwise false. """ return self.correlation_result if self.correlation_result.coefficient >= threshold else False
python
def is_correlated(self, threshold=0): return self.correlation_result if self.correlation_result.coefficient >= threshold else False
[ "def", "is_correlated", "(", "self", ",", "threshold", "=", "0", ")", ":", "return", "self", ".", "correlation_result", "if", "self", ".", "correlation_result", ".", "coefficient", ">=", "threshold", "else", "False" ]
Compare with a threshold to determine whether two timeseries correlate to each other. :return: a CorrelationResult object if two time series correlate otherwise false.
[ "Compare", "with", "a", "threshold", "to", "determine", "whether", "two", "timeseries", "correlate", "to", "each", "other", ".", ":", "return", ":", "a", "CorrelationResult", "object", "if", "two", "time", "series", "correlate", "otherwise", "false", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/correlator.py#L113-L118
linkedin/luminol
src/luminol/algorithms/anomaly_detector_algorithms/diff_percent_threshold.py
DiffPercentThreshold._set_scores
def _set_scores(self): """ Compute anomaly scores for the time series This algorithm just takes the diff of threshold with current value as anomaly score """ anom_scores = {} for i, (timestamp, value) in enumerate(self.time_series.items()): baseline_value = self.baseline_time_series[i] if baseline_value > 0: diff_percent = 100 * (value - baseline_value) / baseline_value elif value > 0: diff_percent = 100.0 else: diff_percent = 0.0 anom_scores[timestamp] = 0.0 if self.percent_threshold_upper and diff_percent > 0 and diff_percent > self.percent_threshold_upper: anom_scores[timestamp] = diff_percent if self.percent_threshold_lower and diff_percent < 0 and diff_percent < self.percent_threshold_lower: anom_scores[timestamp] = -1 * diff_percent self.anom_scores = TimeSeries(self._denoise_scores(anom_scores))
python
def _set_scores(self): anom_scores = {} for i, (timestamp, value) in enumerate(self.time_series.items()): baseline_value = self.baseline_time_series[i] if baseline_value > 0: diff_percent = 100 * (value - baseline_value) / baseline_value elif value > 0: diff_percent = 100.0 else: diff_percent = 0.0 anom_scores[timestamp] = 0.0 if self.percent_threshold_upper and diff_percent > 0 and diff_percent > self.percent_threshold_upper: anom_scores[timestamp] = diff_percent if self.percent_threshold_lower and diff_percent < 0 and diff_percent < self.percent_threshold_lower: anom_scores[timestamp] = -1 * diff_percent self.anom_scores = TimeSeries(self._denoise_scores(anom_scores))
[ "def", "_set_scores", "(", "self", ")", ":", "anom_scores", "=", "{", "}", "for", "i", ",", "(", "timestamp", ",", "value", ")", "in", "enumerate", "(", "self", ".", "time_series", ".", "items", "(", ")", ")", ":", "baseline_value", "=", "self", ".", "baseline_time_series", "[", "i", "]", "if", "baseline_value", ">", "0", ":", "diff_percent", "=", "100", "*", "(", "value", "-", "baseline_value", ")", "/", "baseline_value", "elif", "value", ">", "0", ":", "diff_percent", "=", "100.0", "else", ":", "diff_percent", "=", "0.0", "anom_scores", "[", "timestamp", "]", "=", "0.0", "if", "self", ".", "percent_threshold_upper", "and", "diff_percent", ">", "0", "and", "diff_percent", ">", "self", ".", "percent_threshold_upper", ":", "anom_scores", "[", "timestamp", "]", "=", "diff_percent", "if", "self", ".", "percent_threshold_lower", "and", "diff_percent", "<", "0", "and", "diff_percent", "<", "self", ".", "percent_threshold_lower", ":", "anom_scores", "[", "timestamp", "]", "=", "-", "1", "*", "diff_percent", "self", ".", "anom_scores", "=", "TimeSeries", "(", "self", ".", "_denoise_scores", "(", "anom_scores", ")", ")" ]
Compute anomaly scores for the time series This algorithm just takes the diff of threshold with current value as anomaly score
[ "Compute", "anomaly", "scores", "for", "the", "time", "series", "This", "algorithm", "just", "takes", "the", "diff", "of", "threshold", "with", "current", "value", "as", "anomaly", "score" ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/algorithms/anomaly_detector_algorithms/diff_percent_threshold.py#L41-L64
linkedin/luminol
demo/src/rca.py
RCA._load
def _load(self, metrix): """ Load time series. :param timeseries: a TimeSeries, a dictionary or a path to a csv file(str). :return TimeSeries: a TimeSeries object. """ if isinstance(metrix, TimeSeries): return metrix if isinstance(metrix, dict): return TimeSeries(metrix) return TimeSeries(utils.read_csv(metrix))
python
def _load(self, metrix): if isinstance(metrix, TimeSeries): return metrix if isinstance(metrix, dict): return TimeSeries(metrix) return TimeSeries(utils.read_csv(metrix))
[ "def", "_load", "(", "self", ",", "metrix", ")", ":", "if", "isinstance", "(", "metrix", ",", "TimeSeries", ")", ":", "return", "metrix", "if", "isinstance", "(", "metrix", ",", "dict", ")", ":", "return", "TimeSeries", "(", "metrix", ")", "return", "TimeSeries", "(", "utils", ".", "read_csv", "(", "metrix", ")", ")" ]
Load time series. :param timeseries: a TimeSeries, a dictionary or a path to a csv file(str). :return TimeSeries: a TimeSeries object.
[ "Load", "time", "series", ".", ":", "param", "timeseries", ":", "a", "TimeSeries", "a", "dictionary", "or", "a", "path", "to", "a", "csv", "file", "(", "str", ")", ".", ":", "return", "TimeSeries", ":", "a", "TimeSeries", "object", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/demo/src/rca.py#L37-L47
linkedin/luminol
demo/src/rca.py
RCA._analyze
def _analyze(self): """ Analyzes if a matrix has anomalies. If any anomaly is found, determine if the matrix correlates with any other matrixes. To be implemented. """ output = defaultdict(list) output_by_name = defaultdict(list) scores = self.anomaly_detector.get_all_scores() if self.anomalies: for anomaly in self.anomalies: metrix_scores = scores start_t, end_t = anomaly.get_time_window() t = anomaly.exact_timestamp # Compute extended start timestamp and extended end timestamp. room = (end_t - start_t) / 2 if not room: room = 30 extended_start_t = start_t - room extended_end_t = end_t + room metrix_scores_cropped = metrix_scores.crop(extended_start_t, extended_end_t) # Adjust the two timestamps if not enough data points are included. while len(metrix_scores_cropped) < 2: extended_start_t = extended_start_t - room extended_end_t = extended_end_t + room metrix_scores_cropped = metrix_scores.crop(extended_start_t, extended_end_t) # Correlate with other metrics for entry in self.related_metrices: try: entry_correlation_result = Correlator(self.metrix, entry, time_period=(extended_start_t, extended_end_t), use_anomaly_score=True).get_correlation_result() record = extended_start_t, extended_end_t, entry_correlation_result.__dict__, entry record_by_name = extended_start_t, extended_end_t, entry_correlation_result.__dict__ output[t].append(record) output_by_name[entry].append(record_by_name) except exceptions.NotEnoughDataPoints: pass self.output = output self.output_by_name = output_by_name
python
def _analyze(self): output = defaultdict(list) output_by_name = defaultdict(list) scores = self.anomaly_detector.get_all_scores() if self.anomalies: for anomaly in self.anomalies: metrix_scores = scores start_t, end_t = anomaly.get_time_window() t = anomaly.exact_timestamp room = (end_t - start_t) / 2 if not room: room = 30 extended_start_t = start_t - room extended_end_t = end_t + room metrix_scores_cropped = metrix_scores.crop(extended_start_t, extended_end_t) while len(metrix_scores_cropped) < 2: extended_start_t = extended_start_t - room extended_end_t = extended_end_t + room metrix_scores_cropped = metrix_scores.crop(extended_start_t, extended_end_t) for entry in self.related_metrices: try: entry_correlation_result = Correlator(self.metrix, entry, time_period=(extended_start_t, extended_end_t), use_anomaly_score=True).get_correlation_result() record = extended_start_t, extended_end_t, entry_correlation_result.__dict__, entry record_by_name = extended_start_t, extended_end_t, entry_correlation_result.__dict__ output[t].append(record) output_by_name[entry].append(record_by_name) except exceptions.NotEnoughDataPoints: pass self.output = output self.output_by_name = output_by_name
[ "def", "_analyze", "(", "self", ")", ":", "output", "=", "defaultdict", "(", "list", ")", "output_by_name", "=", "defaultdict", "(", "list", ")", "scores", "=", "self", ".", "anomaly_detector", ".", "get_all_scores", "(", ")", "if", "self", ".", "anomalies", ":", "for", "anomaly", "in", "self", ".", "anomalies", ":", "metrix_scores", "=", "scores", "start_t", ",", "end_t", "=", "anomaly", ".", "get_time_window", "(", ")", "t", "=", "anomaly", ".", "exact_timestamp", "# Compute extended start timestamp and extended end timestamp.", "room", "=", "(", "end_t", "-", "start_t", ")", "/", "2", "if", "not", "room", ":", "room", "=", "30", "extended_start_t", "=", "start_t", "-", "room", "extended_end_t", "=", "end_t", "+", "room", "metrix_scores_cropped", "=", "metrix_scores", ".", "crop", "(", "extended_start_t", ",", "extended_end_t", ")", "# Adjust the two timestamps if not enough data points are included.", "while", "len", "(", "metrix_scores_cropped", ")", "<", "2", ":", "extended_start_t", "=", "extended_start_t", "-", "room", "extended_end_t", "=", "extended_end_t", "+", "room", "metrix_scores_cropped", "=", "metrix_scores", ".", "crop", "(", "extended_start_t", ",", "extended_end_t", ")", "# Correlate with other metrics", "for", "entry", "in", "self", ".", "related_metrices", ":", "try", ":", "entry_correlation_result", "=", "Correlator", "(", "self", ".", "metrix", ",", "entry", ",", "time_period", "=", "(", "extended_start_t", ",", "extended_end_t", ")", ",", "use_anomaly_score", "=", "True", ")", ".", "get_correlation_result", "(", ")", "record", "=", "extended_start_t", ",", "extended_end_t", ",", "entry_correlation_result", ".", "__dict__", ",", "entry", "record_by_name", "=", "extended_start_t", ",", "extended_end_t", ",", "entry_correlation_result", ".", "__dict__", "output", "[", "t", "]", ".", "append", "(", "record", ")", "output_by_name", "[", "entry", "]", ".", "append", "(", "record_by_name", ")", "except", "exceptions", ".", "NotEnoughDataPoints", ":", "pass", "self", ".", "output", "=", "output", "self", ".", "output_by_name", "=", "output_by_name" ]
Analyzes if a matrix has anomalies. If any anomaly is found, determine if the matrix correlates with any other matrixes. To be implemented.
[ "Analyzes", "if", "a", "matrix", "has", "anomalies", ".", "If", "any", "anomaly", "is", "found", "determine", "if", "the", "matrix", "correlates", "with", "any", "other", "matrixes", ".", "To", "be", "implemented", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/demo/src/rca.py#L49-L92
linkedin/luminol
src/luminol/algorithms/anomaly_detector_algorithms/default_detector.py
DefaultDetector._set_scores
def _set_scores(self): """ Set anomaly scores using a weighted sum. """ anom_scores_ema = self.exp_avg_detector.run() anom_scores_deri = self.derivative_detector.run() anom_scores = {} for timestamp in anom_scores_ema.timestamps: # Compute a weighted anomaly score. anom_scores[timestamp] = max(anom_scores_ema[timestamp], anom_scores_ema[timestamp] * DEFAULT_DETECTOR_EMA_WEIGHT + anom_scores_deri[timestamp] * (1 - DEFAULT_DETECTOR_EMA_WEIGHT)) # If ema score is significant enough, take the bigger one of the weighted score and deri score. if anom_scores_ema[timestamp] > DEFAULT_DETECTOR_EMA_SIGNIFICANT: anom_scores[timestamp] = max(anom_scores[timestamp], anom_scores_deri[timestamp]) self.anom_scores = TimeSeries(self._denoise_scores(anom_scores))
python
def _set_scores(self): anom_scores_ema = self.exp_avg_detector.run() anom_scores_deri = self.derivative_detector.run() anom_scores = {} for timestamp in anom_scores_ema.timestamps: anom_scores[timestamp] = max(anom_scores_ema[timestamp], anom_scores_ema[timestamp] * DEFAULT_DETECTOR_EMA_WEIGHT + anom_scores_deri[timestamp] * (1 - DEFAULT_DETECTOR_EMA_WEIGHT)) if anom_scores_ema[timestamp] > DEFAULT_DETECTOR_EMA_SIGNIFICANT: anom_scores[timestamp] = max(anom_scores[timestamp], anom_scores_deri[timestamp]) self.anom_scores = TimeSeries(self._denoise_scores(anom_scores))
[ "def", "_set_scores", "(", "self", ")", ":", "anom_scores_ema", "=", "self", ".", "exp_avg_detector", ".", "run", "(", ")", "anom_scores_deri", "=", "self", ".", "derivative_detector", ".", "run", "(", ")", "anom_scores", "=", "{", "}", "for", "timestamp", "in", "anom_scores_ema", ".", "timestamps", ":", "# Compute a weighted anomaly score.", "anom_scores", "[", "timestamp", "]", "=", "max", "(", "anom_scores_ema", "[", "timestamp", "]", ",", "anom_scores_ema", "[", "timestamp", "]", "*", "DEFAULT_DETECTOR_EMA_WEIGHT", "+", "anom_scores_deri", "[", "timestamp", "]", "*", "(", "1", "-", "DEFAULT_DETECTOR_EMA_WEIGHT", ")", ")", "# If ema score is significant enough, take the bigger one of the weighted score and deri score.", "if", "anom_scores_ema", "[", "timestamp", "]", ">", "DEFAULT_DETECTOR_EMA_SIGNIFICANT", ":", "anom_scores", "[", "timestamp", "]", "=", "max", "(", "anom_scores", "[", "timestamp", "]", ",", "anom_scores_deri", "[", "timestamp", "]", ")", "self", ".", "anom_scores", "=", "TimeSeries", "(", "self", ".", "_denoise_scores", "(", "anom_scores", ")", ")" ]
Set anomaly scores using a weighted sum.
[ "Set", "anomaly", "scores", "using", "a", "weighted", "sum", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/algorithms/anomaly_detector_algorithms/default_detector.py#L35-L49
linkedin/luminol
src/luminol/algorithms/anomaly_detector_algorithms/derivative_detector.py
DerivativeDetector._compute_derivatives
def _compute_derivatives(self): """ Compute derivatives of the time series. """ derivatives = [] for i, (timestamp, value) in enumerate(self.time_series_items): if i > 0: pre_item = self.time_series_items[i - 1] pre_timestamp = pre_item[0] pre_value = pre_item[1] td = timestamp - pre_timestamp derivative = (value - pre_value) / td if td != 0 else value - pre_value derivative = abs(derivative) derivatives.append(derivative) # First timestamp is assigned the same derivative as the second timestamp. if derivatives: derivatives.insert(0, derivatives[0]) self.derivatives = derivatives
python
def _compute_derivatives(self): derivatives = [] for i, (timestamp, value) in enumerate(self.time_series_items): if i > 0: pre_item = self.time_series_items[i - 1] pre_timestamp = pre_item[0] pre_value = pre_item[1] td = timestamp - pre_timestamp derivative = (value - pre_value) / td if td != 0 else value - pre_value derivative = abs(derivative) derivatives.append(derivative) if derivatives: derivatives.insert(0, derivatives[0]) self.derivatives = derivatives
[ "def", "_compute_derivatives", "(", "self", ")", ":", "derivatives", "=", "[", "]", "for", "i", ",", "(", "timestamp", ",", "value", ")", "in", "enumerate", "(", "self", ".", "time_series_items", ")", ":", "if", "i", ">", "0", ":", "pre_item", "=", "self", ".", "time_series_items", "[", "i", "-", "1", "]", "pre_timestamp", "=", "pre_item", "[", "0", "]", "pre_value", "=", "pre_item", "[", "1", "]", "td", "=", "timestamp", "-", "pre_timestamp", "derivative", "=", "(", "value", "-", "pre_value", ")", "/", "td", "if", "td", "!=", "0", "else", "value", "-", "pre_value", "derivative", "=", "abs", "(", "derivative", ")", "derivatives", ".", "append", "(", "derivative", ")", "# First timestamp is assigned the same derivative as the second timestamp.", "if", "derivatives", ":", "derivatives", ".", "insert", "(", "0", ",", "derivatives", "[", "0", "]", ")", "self", ".", "derivatives", "=", "derivatives" ]
Compute derivatives of the time series.
[ "Compute", "derivatives", "of", "the", "time", "series", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/algorithms/anomaly_detector_algorithms/derivative_detector.py#L38-L55
linkedin/luminol
src/luminol/algorithms/anomaly_detector_algorithms/bitmap_detector.py
BitmapDetector._sanity_check
def _sanity_check(self): """ Check if there are enough data points. """ windows = self.lag_window_size + self.future_window_size if (not self.lag_window_size or not self.future_window_size or self.time_series_length < windows or windows < DEFAULT_BITMAP_MINIMAL_POINTS_IN_WINDOWS): raise exceptions.NotEnoughDataPoints # If window size is too big, too many data points will be assigned a score of 0 in the first lag window # and the last future window. if self.lag_window_size > DEFAULT_BITMAP_MAXIMAL_POINTS_IN_WINDOWS: self.lag_window_size = DEFAULT_BITMAP_MAXIMAL_POINTS_IN_WINDOWS if self.future_window_size > DEFAULT_BITMAP_MAXIMAL_POINTS_IN_WINDOWS: self.future_window_size = DEFAULT_BITMAP_MAXIMAL_POINTS_IN_WINDOWS
python
def _sanity_check(self): windows = self.lag_window_size + self.future_window_size if (not self.lag_window_size or not self.future_window_size or self.time_series_length < windows or windows < DEFAULT_BITMAP_MINIMAL_POINTS_IN_WINDOWS): raise exceptions.NotEnoughDataPoints if self.lag_window_size > DEFAULT_BITMAP_MAXIMAL_POINTS_IN_WINDOWS: self.lag_window_size = DEFAULT_BITMAP_MAXIMAL_POINTS_IN_WINDOWS if self.future_window_size > DEFAULT_BITMAP_MAXIMAL_POINTS_IN_WINDOWS: self.future_window_size = DEFAULT_BITMAP_MAXIMAL_POINTS_IN_WINDOWS
[ "def", "_sanity_check", "(", "self", ")", ":", "windows", "=", "self", ".", "lag_window_size", "+", "self", ".", "future_window_size", "if", "(", "not", "self", ".", "lag_window_size", "or", "not", "self", ".", "future_window_size", "or", "self", ".", "time_series_length", "<", "windows", "or", "windows", "<", "DEFAULT_BITMAP_MINIMAL_POINTS_IN_WINDOWS", ")", ":", "raise", "exceptions", ".", "NotEnoughDataPoints", "# If window size is too big, too many data points will be assigned a score of 0 in the first lag window", "# and the last future window.", "if", "self", ".", "lag_window_size", ">", "DEFAULT_BITMAP_MAXIMAL_POINTS_IN_WINDOWS", ":", "self", ".", "lag_window_size", "=", "DEFAULT_BITMAP_MAXIMAL_POINTS_IN_WINDOWS", "if", "self", ".", "future_window_size", ">", "DEFAULT_BITMAP_MAXIMAL_POINTS_IN_WINDOWS", ":", "self", ".", "future_window_size", "=", "DEFAULT_BITMAP_MAXIMAL_POINTS_IN_WINDOWS" ]
Check if there are enough data points.
[ "Check", "if", "there", "are", "enough", "data", "points", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/algorithms/anomaly_detector_algorithms/bitmap_detector.py#L60-L73
linkedin/luminol
src/luminol/algorithms/anomaly_detector_algorithms/bitmap_detector.py
BitmapDetector._generate_SAX_single
def _generate_SAX_single(self, sections, value): """ Generate SAX representation(Symbolic Aggregate approXimation) for a single data point. Read more about it here: Assumption-Free Anomaly Detection in Time Series(http://alumni.cs.ucr.edu/~ratana/SSDBM05.pdf). :param dict sections: value sections. :param float value: value to be categorized. :return str: a SAX representation. """ sax = 0 for section_number in sections.keys(): section_lower_bound = sections[section_number] if value >= section_lower_bound: sax = section_number else: break return str(sax)
python
def _generate_SAX_single(self, sections, value): sax = 0 for section_number in sections.keys(): section_lower_bound = sections[section_number] if value >= section_lower_bound: sax = section_number else: break return str(sax)
[ "def", "_generate_SAX_single", "(", "self", ",", "sections", ",", "value", ")", ":", "sax", "=", "0", "for", "section_number", "in", "sections", ".", "keys", "(", ")", ":", "section_lower_bound", "=", "sections", "[", "section_number", "]", "if", "value", ">=", "section_lower_bound", ":", "sax", "=", "section_number", "else", ":", "break", "return", "str", "(", "sax", ")" ]
Generate SAX representation(Symbolic Aggregate approXimation) for a single data point. Read more about it here: Assumption-Free Anomaly Detection in Time Series(http://alumni.cs.ucr.edu/~ratana/SSDBM05.pdf). :param dict sections: value sections. :param float value: value to be categorized. :return str: a SAX representation.
[ "Generate", "SAX", "representation", "(", "Symbolic", "Aggregate", "approXimation", ")", "for", "a", "single", "data", "point", ".", "Read", "more", "about", "it", "here", ":", "Assumption", "-", "Free", "Anomaly", "Detection", "in", "Time", "Series", "(", "http", ":", "//", "alumni", ".", "cs", ".", "ucr", ".", "edu", "/", "~ratana", "/", "SSDBM05", ".", "pdf", ")", ".", ":", "param", "dict", "sections", ":", "value", "sections", ".", ":", "param", "float", "value", ":", "value", "to", "be", "categorized", ".", ":", "return", "str", ":", "a", "SAX", "representation", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/algorithms/anomaly_detector_algorithms/bitmap_detector.py#L75-L90
linkedin/luminol
src/luminol/algorithms/anomaly_detector_algorithms/bitmap_detector.py
BitmapDetector._generate_SAX
def _generate_SAX(self): """ Generate SAX representation for all values of the time series. """ sections = {} self.value_min = self.time_series.min() self.value_max = self.time_series.max() # Break the whole value range into different sections. section_height = (self.value_max - self.value_min) / self.precision for section_number in range(self.precision): sections[section_number] = self.value_min + section_number * section_height # Generate SAX representation. self.sax = ''.join(self._generate_SAX_single(sections, value) for value in self.time_series.values)
python
def _generate_SAX(self): sections = {} self.value_min = self.time_series.min() self.value_max = self.time_series.max() section_height = (self.value_max - self.value_min) / self.precision for section_number in range(self.precision): sections[section_number] = self.value_min + section_number * section_height self.sax = ''.join(self._generate_SAX_single(sections, value) for value in self.time_series.values)
[ "def", "_generate_SAX", "(", "self", ")", ":", "sections", "=", "{", "}", "self", ".", "value_min", "=", "self", ".", "time_series", ".", "min", "(", ")", "self", ".", "value_max", "=", "self", ".", "time_series", ".", "max", "(", ")", "# Break the whole value range into different sections.", "section_height", "=", "(", "self", ".", "value_max", "-", "self", ".", "value_min", ")", "/", "self", ".", "precision", "for", "section_number", "in", "range", "(", "self", ".", "precision", ")", ":", "sections", "[", "section_number", "]", "=", "self", ".", "value_min", "+", "section_number", "*", "section_height", "# Generate SAX representation.", "self", ".", "sax", "=", "''", ".", "join", "(", "self", ".", "_generate_SAX_single", "(", "sections", ",", "value", ")", "for", "value", "in", "self", ".", "time_series", ".", "values", ")" ]
Generate SAX representation for all values of the time series.
[ "Generate", "SAX", "representation", "for", "all", "values", "of", "the", "time", "series", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/algorithms/anomaly_detector_algorithms/bitmap_detector.py#L92-L104
linkedin/luminol
src/luminol/algorithms/anomaly_detector_algorithms/bitmap_detector.py
BitmapDetector._construct_SAX_chunk_dict
def _construct_SAX_chunk_dict(self, sax): """ Form a chunk frequency dictionary from a SAX representation. :param str sax: a SAX representation. :return dict: frequency dictionary for chunks in the SAX representation. """ frequency = defaultdict(int) chunk_size = self.chunk_size length = len(sax) for i in range(length): if i + chunk_size <= length: chunk = sax[i: i + chunk_size] frequency[chunk] += 1 return frequency
python
def _construct_SAX_chunk_dict(self, sax): frequency = defaultdict(int) chunk_size = self.chunk_size length = len(sax) for i in range(length): if i + chunk_size <= length: chunk = sax[i: i + chunk_size] frequency[chunk] += 1 return frequency
[ "def", "_construct_SAX_chunk_dict", "(", "self", ",", "sax", ")", ":", "frequency", "=", "defaultdict", "(", "int", ")", "chunk_size", "=", "self", ".", "chunk_size", "length", "=", "len", "(", "sax", ")", "for", "i", "in", "range", "(", "length", ")", ":", "if", "i", "+", "chunk_size", "<=", "length", ":", "chunk", "=", "sax", "[", "i", ":", "i", "+", "chunk_size", "]", "frequency", "[", "chunk", "]", "+=", "1", "return", "frequency" ]
Form a chunk frequency dictionary from a SAX representation. :param str sax: a SAX representation. :return dict: frequency dictionary for chunks in the SAX representation.
[ "Form", "a", "chunk", "frequency", "dictionary", "from", "a", "SAX", "representation", ".", ":", "param", "str", "sax", ":", "a", "SAX", "representation", ".", ":", "return", "dict", ":", "frequency", "dictionary", "for", "chunks", "in", "the", "SAX", "representation", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/algorithms/anomaly_detector_algorithms/bitmap_detector.py#L106-L119
linkedin/luminol
src/luminol/algorithms/anomaly_detector_algorithms/bitmap_detector.py
BitmapDetector._construct_all_SAX_chunk_dict
def _construct_all_SAX_chunk_dict(self): """ Construct the chunk dicts for lagging window and future window at each index. e.g: Suppose we have a SAX sequence as '1234567890', both window sizes are 3, and the chunk size is 2. The first index that has a lagging window is 3. For index equals 3, the lagging window has sequence '123', the chunk to leave lagging window(lw_leave_chunk) is '12', and the chunk to enter lagging window(lw_enter_chunk) is '34'. Therefore, given chunk dicts at i, to compute chunk dicts at i+1, simply decrement the count for lw_leave_chunk, and increment the count for lw_enter_chunk from chunk dicts at i. Same method applies to future window as well. """ lag_dicts = {} fut_dicts = {} length = self.time_series_length lws = self.lag_window_size fws = self.future_window_size chunk_size = self.chunk_size for i in range(length): # If i is too small or too big, there will be no chunk dicts. if i < lws or i > length - fws: lag_dicts[i] = None else: # Just enter valid range. if lag_dicts[i - 1] is None: lag_dict = self._construct_SAX_chunk_dict(self.sax[i - lws: i]) lag_dicts[i] = lag_dict lw_leave_chunk = self.sax[0:chunk_size] lw_enter_chunk = self.sax[i - chunk_size + 1: i + 1] fut_dict = self._construct_SAX_chunk_dict(self.sax[i: i + fws]) fut_dicts[i] = fut_dict fw_leave_chunk = self.sax[i: i + chunk_size] fw_enter_chunk = self.sax[i + fws + 1 - chunk_size: i + fws + 1] else: # Update dicts according to leave_chunks and enter_chunks. lag_dict = copy(lag_dicts[i - 1]) lag_dict[lw_leave_chunk] -= 1 lag_dict[lw_enter_chunk] += 1 lag_dicts[i] = lag_dict fut_dict = copy(fut_dicts[i - 1]) fut_dict[fw_leave_chunk] -= 1 fut_dict[fw_enter_chunk] += 1 fut_dicts[i] = fut_dict # Updata leave_chunks and enter_chunks. lw_leave_chunk = self.sax[i - lws: i - lws + chunk_size] lw_enter_chunk = self.sax[i - chunk_size + 1: i + 1] fw_leave_chunk = self.sax[i: i + chunk_size] fw_enter_chunk = self.sax[i + fws + 1 - chunk_size: i + fws + 1] self.lag_dicts = lag_dicts self.fut_dicts = fut_dicts
python
def _construct_all_SAX_chunk_dict(self): lag_dicts = {} fut_dicts = {} length = self.time_series_length lws = self.lag_window_size fws = self.future_window_size chunk_size = self.chunk_size for i in range(length): if i < lws or i > length - fws: lag_dicts[i] = None else: if lag_dicts[i - 1] is None: lag_dict = self._construct_SAX_chunk_dict(self.sax[i - lws: i]) lag_dicts[i] = lag_dict lw_leave_chunk = self.sax[0:chunk_size] lw_enter_chunk = self.sax[i - chunk_size + 1: i + 1] fut_dict = self._construct_SAX_chunk_dict(self.sax[i: i + fws]) fut_dicts[i] = fut_dict fw_leave_chunk = self.sax[i: i + chunk_size] fw_enter_chunk = self.sax[i + fws + 1 - chunk_size: i + fws + 1] else: lag_dict = copy(lag_dicts[i - 1]) lag_dict[lw_leave_chunk] -= 1 lag_dict[lw_enter_chunk] += 1 lag_dicts[i] = lag_dict fut_dict = copy(fut_dicts[i - 1]) fut_dict[fw_leave_chunk] -= 1 fut_dict[fw_enter_chunk] += 1 fut_dicts[i] = fut_dict lw_leave_chunk = self.sax[i - lws: i - lws + chunk_size] lw_enter_chunk = self.sax[i - chunk_size + 1: i + 1] fw_leave_chunk = self.sax[i: i + chunk_size] fw_enter_chunk = self.sax[i + fws + 1 - chunk_size: i + fws + 1] self.lag_dicts = lag_dicts self.fut_dicts = fut_dicts
[ "def", "_construct_all_SAX_chunk_dict", "(", "self", ")", ":", "lag_dicts", "=", "{", "}", "fut_dicts", "=", "{", "}", "length", "=", "self", ".", "time_series_length", "lws", "=", "self", ".", "lag_window_size", "fws", "=", "self", ".", "future_window_size", "chunk_size", "=", "self", ".", "chunk_size", "for", "i", "in", "range", "(", "length", ")", ":", "# If i is too small or too big, there will be no chunk dicts.", "if", "i", "<", "lws", "or", "i", ">", "length", "-", "fws", ":", "lag_dicts", "[", "i", "]", "=", "None", "else", ":", "# Just enter valid range.", "if", "lag_dicts", "[", "i", "-", "1", "]", "is", "None", ":", "lag_dict", "=", "self", ".", "_construct_SAX_chunk_dict", "(", "self", ".", "sax", "[", "i", "-", "lws", ":", "i", "]", ")", "lag_dicts", "[", "i", "]", "=", "lag_dict", "lw_leave_chunk", "=", "self", ".", "sax", "[", "0", ":", "chunk_size", "]", "lw_enter_chunk", "=", "self", ".", "sax", "[", "i", "-", "chunk_size", "+", "1", ":", "i", "+", "1", "]", "fut_dict", "=", "self", ".", "_construct_SAX_chunk_dict", "(", "self", ".", "sax", "[", "i", ":", "i", "+", "fws", "]", ")", "fut_dicts", "[", "i", "]", "=", "fut_dict", "fw_leave_chunk", "=", "self", ".", "sax", "[", "i", ":", "i", "+", "chunk_size", "]", "fw_enter_chunk", "=", "self", ".", "sax", "[", "i", "+", "fws", "+", "1", "-", "chunk_size", ":", "i", "+", "fws", "+", "1", "]", "else", ":", "# Update dicts according to leave_chunks and enter_chunks.", "lag_dict", "=", "copy", "(", "lag_dicts", "[", "i", "-", "1", "]", ")", "lag_dict", "[", "lw_leave_chunk", "]", "-=", "1", "lag_dict", "[", "lw_enter_chunk", "]", "+=", "1", "lag_dicts", "[", "i", "]", "=", "lag_dict", "fut_dict", "=", "copy", "(", "fut_dicts", "[", "i", "-", "1", "]", ")", "fut_dict", "[", "fw_leave_chunk", "]", "-=", "1", "fut_dict", "[", "fw_enter_chunk", "]", "+=", "1", "fut_dicts", "[", "i", "]", "=", "fut_dict", "# Updata leave_chunks and enter_chunks.", "lw_leave_chunk", "=", "self", ".", "sax", "[", "i", "-", "lws", ":", "i", "-", "lws", "+", "chunk_size", "]", "lw_enter_chunk", "=", "self", ".", "sax", "[", "i", "-", "chunk_size", "+", "1", ":", "i", "+", "1", "]", "fw_leave_chunk", "=", "self", ".", "sax", "[", "i", ":", "i", "+", "chunk_size", "]", "fw_enter_chunk", "=", "self", ".", "sax", "[", "i", "+", "fws", "+", "1", "-", "chunk_size", ":", "i", "+", "fws", "+", "1", "]", "self", ".", "lag_dicts", "=", "lag_dicts", "self", ".", "fut_dicts", "=", "fut_dicts" ]
Construct the chunk dicts for lagging window and future window at each index. e.g: Suppose we have a SAX sequence as '1234567890', both window sizes are 3, and the chunk size is 2. The first index that has a lagging window is 3. For index equals 3, the lagging window has sequence '123', the chunk to leave lagging window(lw_leave_chunk) is '12', and the chunk to enter lagging window(lw_enter_chunk) is '34'. Therefore, given chunk dicts at i, to compute chunk dicts at i+1, simply decrement the count for lw_leave_chunk, and increment the count for lw_enter_chunk from chunk dicts at i. Same method applies to future window as well.
[ "Construct", "the", "chunk", "dicts", "for", "lagging", "window", "and", "future", "window", "at", "each", "index", ".", "e", ".", "g", ":", "Suppose", "we", "have", "a", "SAX", "sequence", "as", "1234567890", "both", "window", "sizes", "are", "3", "and", "the", "chunk", "size", "is", "2", ".", "The", "first", "index", "that", "has", "a", "lagging", "window", "is", "3", ".", "For", "index", "equals", "3", "the", "lagging", "window", "has", "sequence", "123", "the", "chunk", "to", "leave", "lagging", "window", "(", "lw_leave_chunk", ")", "is", "12", "and", "the", "chunk", "to", "enter", "lagging", "window", "(", "lw_enter_chunk", ")", "is", "34", ".", "Therefore", "given", "chunk", "dicts", "at", "i", "to", "compute", "chunk", "dicts", "at", "i", "+", "1", "simply", "decrement", "the", "count", "for", "lw_leave_chunk", "and", "increment", "the", "count", "for", "lw_enter_chunk", "from", "chunk", "dicts", "at", "i", ".", "Same", "method", "applies", "to", "future", "window", "as", "well", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/algorithms/anomaly_detector_algorithms/bitmap_detector.py#L121-L174
linkedin/luminol
src/luminol/algorithms/anomaly_detector_algorithms/bitmap_detector.py
BitmapDetector._compute_anom_score_between_two_windows
def _compute_anom_score_between_two_windows(self, i): """ Compute distance difference between two windows' chunk frequencies, which is then marked as the anomaly score of the data point on the window boundary in the middle. :param int i: index of the data point between two windows. :return float: the anomaly score. """ lag_window_chunk_dict = self.lag_dicts[i] future_window_chunk_dict = self.fut_dicts[i] score = 0 for chunk in lag_window_chunk_dict: if chunk in future_window_chunk_dict: score += math.pow(future_window_chunk_dict[chunk] - lag_window_chunk_dict[chunk], 2) else: score += math.pow(lag_window_chunk_dict[chunk], 2) for chunk in future_window_chunk_dict: if chunk not in lag_window_chunk_dict: score += math.pow(future_window_chunk_dict[chunk], 2) return score
python
def _compute_anom_score_between_two_windows(self, i): lag_window_chunk_dict = self.lag_dicts[i] future_window_chunk_dict = self.fut_dicts[i] score = 0 for chunk in lag_window_chunk_dict: if chunk in future_window_chunk_dict: score += math.pow(future_window_chunk_dict[chunk] - lag_window_chunk_dict[chunk], 2) else: score += math.pow(lag_window_chunk_dict[chunk], 2) for chunk in future_window_chunk_dict: if chunk not in lag_window_chunk_dict: score += math.pow(future_window_chunk_dict[chunk], 2) return score
[ "def", "_compute_anom_score_between_two_windows", "(", "self", ",", "i", ")", ":", "lag_window_chunk_dict", "=", "self", ".", "lag_dicts", "[", "i", "]", "future_window_chunk_dict", "=", "self", ".", "fut_dicts", "[", "i", "]", "score", "=", "0", "for", "chunk", "in", "lag_window_chunk_dict", ":", "if", "chunk", "in", "future_window_chunk_dict", ":", "score", "+=", "math", ".", "pow", "(", "future_window_chunk_dict", "[", "chunk", "]", "-", "lag_window_chunk_dict", "[", "chunk", "]", ",", "2", ")", "else", ":", "score", "+=", "math", ".", "pow", "(", "lag_window_chunk_dict", "[", "chunk", "]", ",", "2", ")", "for", "chunk", "in", "future_window_chunk_dict", ":", "if", "chunk", "not", "in", "lag_window_chunk_dict", ":", "score", "+=", "math", ".", "pow", "(", "future_window_chunk_dict", "[", "chunk", "]", ",", "2", ")", "return", "score" ]
Compute distance difference between two windows' chunk frequencies, which is then marked as the anomaly score of the data point on the window boundary in the middle. :param int i: index of the data point between two windows. :return float: the anomaly score.
[ "Compute", "distance", "difference", "between", "two", "windows", "chunk", "frequencies", "which", "is", "then", "marked", "as", "the", "anomaly", "score", "of", "the", "data", "point", "on", "the", "window", "boundary", "in", "the", "middle", ".", ":", "param", "int", "i", ":", "index", "of", "the", "data", "point", "between", "two", "windows", ".", ":", "return", "float", ":", "the", "anomaly", "score", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/algorithms/anomaly_detector_algorithms/bitmap_detector.py#L176-L194
linkedin/luminol
src/luminol/algorithms/anomaly_detector_algorithms/bitmap_detector.py
BitmapDetector._set_scores
def _set_scores(self): """ Compute anomaly scores for the time series by sliding both lagging window and future window. """ anom_scores = {} self._generate_SAX() self._construct_all_SAX_chunk_dict() length = self.time_series_length lws = self.lag_window_size fws = self.future_window_size for i, timestamp in enumerate(self.time_series.timestamps): if i < lws or i > length - fws: anom_scores[timestamp] = 0 else: anom_scores[timestamp] = self._compute_anom_score_between_two_windows(i) self.anom_scores = TimeSeries(self._denoise_scores(anom_scores))
python
def _set_scores(self): anom_scores = {} self._generate_SAX() self._construct_all_SAX_chunk_dict() length = self.time_series_length lws = self.lag_window_size fws = self.future_window_size for i, timestamp in enumerate(self.time_series.timestamps): if i < lws or i > length - fws: anom_scores[timestamp] = 0 else: anom_scores[timestamp] = self._compute_anom_score_between_two_windows(i) self.anom_scores = TimeSeries(self._denoise_scores(anom_scores))
[ "def", "_set_scores", "(", "self", ")", ":", "anom_scores", "=", "{", "}", "self", ".", "_generate_SAX", "(", ")", "self", ".", "_construct_all_SAX_chunk_dict", "(", ")", "length", "=", "self", ".", "time_series_length", "lws", "=", "self", ".", "lag_window_size", "fws", "=", "self", ".", "future_window_size", "for", "i", ",", "timestamp", "in", "enumerate", "(", "self", ".", "time_series", ".", "timestamps", ")", ":", "if", "i", "<", "lws", "or", "i", ">", "length", "-", "fws", ":", "anom_scores", "[", "timestamp", "]", "=", "0", "else", ":", "anom_scores", "[", "timestamp", "]", "=", "self", ".", "_compute_anom_score_between_two_windows", "(", "i", ")", "self", ".", "anom_scores", "=", "TimeSeries", "(", "self", ".", "_denoise_scores", "(", "anom_scores", ")", ")" ]
Compute anomaly scores for the time series by sliding both lagging window and future window.
[ "Compute", "anomaly", "scores", "for", "the", "time", "series", "by", "sliding", "both", "lagging", "window", "and", "future", "window", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/algorithms/anomaly_detector_algorithms/bitmap_detector.py#L196-L212
linkedin/luminol
src/luminol/algorithms/anomaly_detector_algorithms/__init__.py
AnomalyDetectorAlgorithm._denoise_scores
def _denoise_scores(self, scores): """ Denoise anomaly scores. Low anomaly scores could be noisy. The following two series will have good correlation result with out denoise: [0.08, 4.6, 4.6, 4.6, 1.0, 1.0] [0.0010, 0.0012, 0.0012, 0.0008, 0.0008] while the second series is pretty flat(suppose it has a max score of 100). param dict scores: the scores to be denoised. """ if scores: maximal = max(scores.values()) if maximal: for key in scores: if scores[key] < DEFAULT_NOISE_PCT_THRESHOLD * maximal: scores[key] = 0 return scores
python
def _denoise_scores(self, scores): if scores: maximal = max(scores.values()) if maximal: for key in scores: if scores[key] < DEFAULT_NOISE_PCT_THRESHOLD * maximal: scores[key] = 0 return scores
[ "def", "_denoise_scores", "(", "self", ",", "scores", ")", ":", "if", "scores", ":", "maximal", "=", "max", "(", "scores", ".", "values", "(", ")", ")", "if", "maximal", ":", "for", "key", "in", "scores", ":", "if", "scores", "[", "key", "]", "<", "DEFAULT_NOISE_PCT_THRESHOLD", "*", "maximal", ":", "scores", "[", "key", "]", "=", "0", "return", "scores" ]
Denoise anomaly scores. Low anomaly scores could be noisy. The following two series will have good correlation result with out denoise: [0.08, 4.6, 4.6, 4.6, 1.0, 1.0] [0.0010, 0.0012, 0.0012, 0.0008, 0.0008] while the second series is pretty flat(suppose it has a max score of 100). param dict scores: the scores to be denoised.
[ "Denoise", "anomaly", "scores", ".", "Low", "anomaly", "scores", "could", "be", "noisy", ".", "The", "following", "two", "series", "will", "have", "good", "correlation", "result", "with", "out", "denoise", ":", "[", "0", ".", "08", "4", ".", "6", "4", ".", "6", "4", ".", "6", "1", ".", "0", "1", ".", "0", "]", "[", "0", ".", "0010", "0", ".", "0012", "0", ".", "0012", "0", ".", "0008", "0", ".", "0008", "]", "while", "the", "second", "series", "is", "pretty", "flat", "(", "suppose", "it", "has", "a", "max", "score", "of", "100", ")", ".", "param", "dict", "scores", ":", "the", "scores", "to", "be", "denoised", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/algorithms/anomaly_detector_algorithms/__init__.py#L44-L59
linkedin/luminol
src/luminol/algorithms/correlator_algorithms/cross_correlator.py
CrossCorrelator._detect_correlation
def _detect_correlation(self): """ Detect correlation by computing correlation coefficients for all allowed shift steps, then take the maximum. """ correlations = [] shifted_correlations = [] self.time_series_a.normalize() self.time_series_b.normalize() a, b = self.time_series_a.align(self.time_series_b) a_values, b_values = a.values, b.values a_avg, b_avg = a.average(), b.average() a_stdev, b_stdev = a.stdev(), b.stdev() n = len(a) denom = a_stdev * b_stdev * n # Find the maximal shift steps according to the maximal shift seconds. allowed_shift_step = self._find_allowed_shift(a.timestamps) if allowed_shift_step: shift_upper_bound = allowed_shift_step shift_lower_bound = -allowed_shift_step else: shift_upper_bound = 1 shift_lower_bound = 0 for delay in range(shift_lower_bound, shift_upper_bound): delay_in_seconds = a.timestamps[abs(delay)] - a.timestamps[0] if delay < 0: delay_in_seconds = -delay_in_seconds s = 0 for i in range(n): j = i + delay if j < 0 or j >= n: continue else: s += ((a_values[i] - a_avg) * (b_values[j] - b_avg)) r = s / denom if denom != 0 else s correlations.append([delay_in_seconds, r]) # Take shift into account to create a "shifted correlation coefficient". if self.max_shift_milliseconds: shifted_correlations.append(r * (1 + float(delay_in_seconds) / self.max_shift_milliseconds * self.shift_impact)) else: shifted_correlations.append(r) max_correlation = list(max(correlations, key=lambda k: k[1])) max_shifted_correlation = max(shifted_correlations) max_correlation.append(max_shifted_correlation) self.correlation_result = CorrelationResult(*max_correlation)
python
def _detect_correlation(self): correlations = [] shifted_correlations = [] self.time_series_a.normalize() self.time_series_b.normalize() a, b = self.time_series_a.align(self.time_series_b) a_values, b_values = a.values, b.values a_avg, b_avg = a.average(), b.average() a_stdev, b_stdev = a.stdev(), b.stdev() n = len(a) denom = a_stdev * b_stdev * n allowed_shift_step = self._find_allowed_shift(a.timestamps) if allowed_shift_step: shift_upper_bound = allowed_shift_step shift_lower_bound = -allowed_shift_step else: shift_upper_bound = 1 shift_lower_bound = 0 for delay in range(shift_lower_bound, shift_upper_bound): delay_in_seconds = a.timestamps[abs(delay)] - a.timestamps[0] if delay < 0: delay_in_seconds = -delay_in_seconds s = 0 for i in range(n): j = i + delay if j < 0 or j >= n: continue else: s += ((a_values[i] - a_avg) * (b_values[j] - b_avg)) r = s / denom if denom != 0 else s correlations.append([delay_in_seconds, r]) if self.max_shift_milliseconds: shifted_correlations.append(r * (1 + float(delay_in_seconds) / self.max_shift_milliseconds * self.shift_impact)) else: shifted_correlations.append(r) max_correlation = list(max(correlations, key=lambda k: k[1])) max_shifted_correlation = max(shifted_correlations) max_correlation.append(max_shifted_correlation) self.correlation_result = CorrelationResult(*max_correlation)
[ "def", "_detect_correlation", "(", "self", ")", ":", "correlations", "=", "[", "]", "shifted_correlations", "=", "[", "]", "self", ".", "time_series_a", ".", "normalize", "(", ")", "self", ".", "time_series_b", ".", "normalize", "(", ")", "a", ",", "b", "=", "self", ".", "time_series_a", ".", "align", "(", "self", ".", "time_series_b", ")", "a_values", ",", "b_values", "=", "a", ".", "values", ",", "b", ".", "values", "a_avg", ",", "b_avg", "=", "a", ".", "average", "(", ")", ",", "b", ".", "average", "(", ")", "a_stdev", ",", "b_stdev", "=", "a", ".", "stdev", "(", ")", ",", "b", ".", "stdev", "(", ")", "n", "=", "len", "(", "a", ")", "denom", "=", "a_stdev", "*", "b_stdev", "*", "n", "# Find the maximal shift steps according to the maximal shift seconds.", "allowed_shift_step", "=", "self", ".", "_find_allowed_shift", "(", "a", ".", "timestamps", ")", "if", "allowed_shift_step", ":", "shift_upper_bound", "=", "allowed_shift_step", "shift_lower_bound", "=", "-", "allowed_shift_step", "else", ":", "shift_upper_bound", "=", "1", "shift_lower_bound", "=", "0", "for", "delay", "in", "range", "(", "shift_lower_bound", ",", "shift_upper_bound", ")", ":", "delay_in_seconds", "=", "a", ".", "timestamps", "[", "abs", "(", "delay", ")", "]", "-", "a", ".", "timestamps", "[", "0", "]", "if", "delay", "<", "0", ":", "delay_in_seconds", "=", "-", "delay_in_seconds", "s", "=", "0", "for", "i", "in", "range", "(", "n", ")", ":", "j", "=", "i", "+", "delay", "if", "j", "<", "0", "or", "j", ">=", "n", ":", "continue", "else", ":", "s", "+=", "(", "(", "a_values", "[", "i", "]", "-", "a_avg", ")", "*", "(", "b_values", "[", "j", "]", "-", "b_avg", ")", ")", "r", "=", "s", "/", "denom", "if", "denom", "!=", "0", "else", "s", "correlations", ".", "append", "(", "[", "delay_in_seconds", ",", "r", "]", ")", "# Take shift into account to create a \"shifted correlation coefficient\".", "if", "self", ".", "max_shift_milliseconds", ":", "shifted_correlations", ".", "append", "(", "r", "*", "(", "1", "+", "float", "(", "delay_in_seconds", ")", "/", "self", ".", "max_shift_milliseconds", "*", "self", ".", "shift_impact", ")", ")", "else", ":", "shifted_correlations", ".", "append", "(", "r", ")", "max_correlation", "=", "list", "(", "max", "(", "correlations", ",", "key", "=", "lambda", "k", ":", "k", "[", "1", "]", ")", ")", "max_shifted_correlation", "=", "max", "(", "shifted_correlations", ")", "max_correlation", ".", "append", "(", "max_shifted_correlation", ")", "self", ".", "correlation_result", "=", "CorrelationResult", "(", "*", "max_correlation", ")" ]
Detect correlation by computing correlation coefficients for all allowed shift steps, then take the maximum.
[ "Detect", "correlation", "by", "computing", "correlation", "coefficients", "for", "all", "allowed", "shift", "steps", "then", "take", "the", "maximum", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/algorithms/correlator_algorithms/cross_correlator.py#L39-L83
linkedin/luminol
src/luminol/algorithms/anomaly_detector_algorithms/absolute_threshold.py
AbsoluteThreshold._set_scores
def _set_scores(self): """ Compute anomaly scores for the time series This algorithm just takes the diff of threshold with current value as anomaly score """ anom_scores = {} for timestamp, value in self.time_series.items(): anom_scores[timestamp] = 0.0 if self.absolute_threshold_value_upper and value > self.absolute_threshold_value_upper: anom_scores[timestamp] = value - self.absolute_threshold_value_upper if self.absolute_threshold_value_lower and value < self.absolute_threshold_value_lower: anom_scores[timestamp] = self.absolute_threshold_value_lower - value self.anom_scores = TimeSeries(self._denoise_scores(anom_scores))
python
def _set_scores(self): anom_scores = {} for timestamp, value in self.time_series.items(): anom_scores[timestamp] = 0.0 if self.absolute_threshold_value_upper and value > self.absolute_threshold_value_upper: anom_scores[timestamp] = value - self.absolute_threshold_value_upper if self.absolute_threshold_value_lower and value < self.absolute_threshold_value_lower: anom_scores[timestamp] = self.absolute_threshold_value_lower - value self.anom_scores = TimeSeries(self._denoise_scores(anom_scores))
[ "def", "_set_scores", "(", "self", ")", ":", "anom_scores", "=", "{", "}", "for", "timestamp", ",", "value", "in", "self", ".", "time_series", ".", "items", "(", ")", ":", "anom_scores", "[", "timestamp", "]", "=", "0.0", "if", "self", ".", "absolute_threshold_value_upper", "and", "value", ">", "self", ".", "absolute_threshold_value_upper", ":", "anom_scores", "[", "timestamp", "]", "=", "value", "-", "self", ".", "absolute_threshold_value_upper", "if", "self", ".", "absolute_threshold_value_lower", "and", "value", "<", "self", ".", "absolute_threshold_value_lower", ":", "anom_scores", "[", "timestamp", "]", "=", "self", ".", "absolute_threshold_value_lower", "-", "value", "self", ".", "anom_scores", "=", "TimeSeries", "(", "self", ".", "_denoise_scores", "(", "anom_scores", ")", ")" ]
Compute anomaly scores for the time series This algorithm just takes the diff of threshold with current value as anomaly score
[ "Compute", "anomaly", "scores", "for", "the", "time", "series", "This", "algorithm", "just", "takes", "the", "diff", "of", "threshold", "with", "current", "value", "as", "anomaly", "score" ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/algorithms/anomaly_detector_algorithms/absolute_threshold.py#L42-L54
linkedin/luminol
src/luminol/algorithms/anomaly_detector_algorithms/exp_avg_detector.py
ExpAvgDetector._compute_anom_score
def _compute_anom_score(self, lag_window_points, point): """ Compute anomaly score for a single data point. Anomaly score for a single data point(t,v) equals: abs(v - ema(lagging window)). :param list lag_window_points: values in the lagging window. :param float point: data point value. :return float: the anomaly score. """ ema = utils.compute_ema(self.smoothing_factor, lag_window_points)[-1] return abs(point - ema)
python
def _compute_anom_score(self, lag_window_points, point): ema = utils.compute_ema(self.smoothing_factor, lag_window_points)[-1] return abs(point - ema)
[ "def", "_compute_anom_score", "(", "self", ",", "lag_window_points", ",", "point", ")", ":", "ema", "=", "utils", ".", "compute_ema", "(", "self", ".", "smoothing_factor", ",", "lag_window_points", ")", "[", "-", "1", "]", "return", "abs", "(", "point", "-", "ema", ")" ]
Compute anomaly score for a single data point. Anomaly score for a single data point(t,v) equals: abs(v - ema(lagging window)). :param list lag_window_points: values in the lagging window. :param float point: data point value. :return float: the anomaly score.
[ "Compute", "anomaly", "score", "for", "a", "single", "data", "point", ".", "Anomaly", "score", "for", "a", "single", "data", "point", "(", "t", "v", ")", "equals", ":", "abs", "(", "v", "-", "ema", "(", "lagging", "window", "))", ".", ":", "param", "list", "lag_window_points", ":", "values", "in", "the", "lagging", "window", ".", ":", "param", "float", "point", ":", "data", "point", "value", ".", ":", "return", "float", ":", "the", "anomaly", "score", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/algorithms/anomaly_detector_algorithms/exp_avg_detector.py#L42-L51
linkedin/luminol
src/luminol/algorithms/anomaly_detector_algorithms/exp_avg_detector.py
ExpAvgDetector._compute_anom_data_using_window
def _compute_anom_data_using_window(self): """ Compute anomaly scores using a lagging window. """ anom_scores = {} values = self.time_series.values stdev = numpy.std(values) for i, (timestamp, value) in enumerate(self.time_series_items): if i < self.lag_window_size: anom_score = self._compute_anom_score(values[:i + 1], value) else: anom_score = self._compute_anom_score(values[i - self.lag_window_size: i + 1], value) if stdev: anom_scores[timestamp] = anom_score / stdev else: anom_scores[timestamp] = anom_score self.anom_scores = TimeSeries(self._denoise_scores(anom_scores))
python
def _compute_anom_data_using_window(self): anom_scores = {} values = self.time_series.values stdev = numpy.std(values) for i, (timestamp, value) in enumerate(self.time_series_items): if i < self.lag_window_size: anom_score = self._compute_anom_score(values[:i + 1], value) else: anom_score = self._compute_anom_score(values[i - self.lag_window_size: i + 1], value) if stdev: anom_scores[timestamp] = anom_score / stdev else: anom_scores[timestamp] = anom_score self.anom_scores = TimeSeries(self._denoise_scores(anom_scores))
[ "def", "_compute_anom_data_using_window", "(", "self", ")", ":", "anom_scores", "=", "{", "}", "values", "=", "self", ".", "time_series", ".", "values", "stdev", "=", "numpy", ".", "std", "(", "values", ")", "for", "i", ",", "(", "timestamp", ",", "value", ")", "in", "enumerate", "(", "self", ".", "time_series_items", ")", ":", "if", "i", "<", "self", ".", "lag_window_size", ":", "anom_score", "=", "self", ".", "_compute_anom_score", "(", "values", "[", ":", "i", "+", "1", "]", ",", "value", ")", "else", ":", "anom_score", "=", "self", ".", "_compute_anom_score", "(", "values", "[", "i", "-", "self", ".", "lag_window_size", ":", "i", "+", "1", "]", ",", "value", ")", "if", "stdev", ":", "anom_scores", "[", "timestamp", "]", "=", "anom_score", "/", "stdev", "else", ":", "anom_scores", "[", "timestamp", "]", "=", "anom_score", "self", ".", "anom_scores", "=", "TimeSeries", "(", "self", ".", "_denoise_scores", "(", "anom_scores", ")", ")" ]
Compute anomaly scores using a lagging window.
[ "Compute", "anomaly", "scores", "using", "a", "lagging", "window", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/algorithms/anomaly_detector_algorithms/exp_avg_detector.py#L53-L69
linkedin/luminol
src/luminol/algorithms/anomaly_detector_algorithms/exp_avg_detector.py
ExpAvgDetector._compute_anom_data_decay_all
def _compute_anom_data_decay_all(self): """ Compute anomaly scores using a lagging window covering all the data points before. """ anom_scores = {} values = self.time_series.values ema = utils.compute_ema(self.smoothing_factor, values) stdev = numpy.std(values) for i, (timestamp, value) in enumerate(self.time_series_items): anom_score = abs((value - ema[i]) / stdev) if stdev else value - ema[i] anom_scores[timestamp] = anom_score self.anom_scores = TimeSeries(self._denoise_scores(anom_scores))
python
def _compute_anom_data_decay_all(self): anom_scores = {} values = self.time_series.values ema = utils.compute_ema(self.smoothing_factor, values) stdev = numpy.std(values) for i, (timestamp, value) in enumerate(self.time_series_items): anom_score = abs((value - ema[i]) / stdev) if stdev else value - ema[i] anom_scores[timestamp] = anom_score self.anom_scores = TimeSeries(self._denoise_scores(anom_scores))
[ "def", "_compute_anom_data_decay_all", "(", "self", ")", ":", "anom_scores", "=", "{", "}", "values", "=", "self", ".", "time_series", ".", "values", "ema", "=", "utils", ".", "compute_ema", "(", "self", ".", "smoothing_factor", ",", "values", ")", "stdev", "=", "numpy", ".", "std", "(", "values", ")", "for", "i", ",", "(", "timestamp", ",", "value", ")", "in", "enumerate", "(", "self", ".", "time_series_items", ")", ":", "anom_score", "=", "abs", "(", "(", "value", "-", "ema", "[", "i", "]", ")", "/", "stdev", ")", "if", "stdev", "else", "value", "-", "ema", "[", "i", "]", "anom_scores", "[", "timestamp", "]", "=", "anom_score", "self", ".", "anom_scores", "=", "TimeSeries", "(", "self", ".", "_denoise_scores", "(", "anom_scores", ")", ")" ]
Compute anomaly scores using a lagging window covering all the data points before.
[ "Compute", "anomaly", "scores", "using", "a", "lagging", "window", "covering", "all", "the", "data", "points", "before", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/algorithms/anomaly_detector_algorithms/exp_avg_detector.py#L71-L82
linkedin/luminol
src/luminol/modules/time_series.py
TimeSeries._generic_binary_op
def _generic_binary_op(self, other, op): """ Perform the method operation specified in the op parameter on the values within the instance's time series values and either another time series or a constant number value. :param other: Time series of values or a constant number to use in calculations with instance's time series. :param func op: The method to perform the calculation between the values. :return: :class:`TimeSeries` object. """ output = {} if isinstance(other, TimeSeries): for key, value in self.items(): if key in other: try: result = op(value, other[key]) if result is NotImplemented: other_type = type(other[key]) other_op = vars(other_type).get(op.__name__) if other_op: output[key] = other_op(other_type(value), other[key]) else: output[key] = result except ZeroDivisionError: continue else: for key, value in self.items(): try: result = op(value, other) if result is NotImplemented: other_type = type(other) other_op = vars(other_type).get(op.__name__) if other_op: output[key] = other_op(other_type(value), other) else: output[key] = result except ZeroDivisionError: continue if output: return TimeSeries(output) else: raise ValueError('TimeSeries data was empty or invalid.')
python
def _generic_binary_op(self, other, op): output = {} if isinstance(other, TimeSeries): for key, value in self.items(): if key in other: try: result = op(value, other[key]) if result is NotImplemented: other_type = type(other[key]) other_op = vars(other_type).get(op.__name__) if other_op: output[key] = other_op(other_type(value), other[key]) else: output[key] = result except ZeroDivisionError: continue else: for key, value in self.items(): try: result = op(value, other) if result is NotImplemented: other_type = type(other) other_op = vars(other_type).get(op.__name__) if other_op: output[key] = other_op(other_type(value), other) else: output[key] = result except ZeroDivisionError: continue if output: return TimeSeries(output) else: raise ValueError('TimeSeries data was empty or invalid.')
[ "def", "_generic_binary_op", "(", "self", ",", "other", ",", "op", ")", ":", "output", "=", "{", "}", "if", "isinstance", "(", "other", ",", "TimeSeries", ")", ":", "for", "key", ",", "value", "in", "self", ".", "items", "(", ")", ":", "if", "key", "in", "other", ":", "try", ":", "result", "=", "op", "(", "value", ",", "other", "[", "key", "]", ")", "if", "result", "is", "NotImplemented", ":", "other_type", "=", "type", "(", "other", "[", "key", "]", ")", "other_op", "=", "vars", "(", "other_type", ")", ".", "get", "(", "op", ".", "__name__", ")", "if", "other_op", ":", "output", "[", "key", "]", "=", "other_op", "(", "other_type", "(", "value", ")", ",", "other", "[", "key", "]", ")", "else", ":", "output", "[", "key", "]", "=", "result", "except", "ZeroDivisionError", ":", "continue", "else", ":", "for", "key", ",", "value", "in", "self", ".", "items", "(", ")", ":", "try", ":", "result", "=", "op", "(", "value", ",", "other", ")", "if", "result", "is", "NotImplemented", ":", "other_type", "=", "type", "(", "other", ")", "other_op", "=", "vars", "(", "other_type", ")", ".", "get", "(", "op", ".", "__name__", ")", "if", "other_op", ":", "output", "[", "key", "]", "=", "other_op", "(", "other_type", "(", "value", ")", ",", "other", ")", "else", ":", "output", "[", "key", "]", "=", "result", "except", "ZeroDivisionError", ":", "continue", "if", "output", ":", "return", "TimeSeries", "(", "output", ")", "else", ":", "raise", "ValueError", "(", "'TimeSeries data was empty or invalid.'", ")" ]
Perform the method operation specified in the op parameter on the values within the instance's time series values and either another time series or a constant number value. :param other: Time series of values or a constant number to use in calculations with instance's time series. :param func op: The method to perform the calculation between the values. :return: :class:`TimeSeries` object.
[ "Perform", "the", "method", "operation", "specified", "in", "the", "op", "parameter", "on", "the", "values", "within", "the", "instance", "s", "time", "series", "values", "and", "either", "another", "time", "series", "or", "a", "constant", "number", "value", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/modules/time_series.py#L150-L192
linkedin/luminol
src/luminol/modules/time_series.py
TimeSeries._get_value_type
def _get_value_type(self, other): """ Get the object type of the value within the values portion of the time series. :return: `type` of object """ if self.values: return type(self.values[0]) elif isinstance(other, TimeSeries) and other.values: return type(other.values[0]) else: raise ValueError('Cannot perform arithmetic on empty time series.')
python
def _get_value_type(self, other): if self.values: return type(self.values[0]) elif isinstance(other, TimeSeries) and other.values: return type(other.values[0]) else: raise ValueError('Cannot perform arithmetic on empty time series.')
[ "def", "_get_value_type", "(", "self", ",", "other", ")", ":", "if", "self", ".", "values", ":", "return", "type", "(", "self", ".", "values", "[", "0", "]", ")", "elif", "isinstance", "(", "other", ",", "TimeSeries", ")", "and", "other", ".", "values", ":", "return", "type", "(", "other", ".", "values", "[", "0", "]", ")", "else", ":", "raise", "ValueError", "(", "'Cannot perform arithmetic on empty time series.'", ")" ]
Get the object type of the value within the values portion of the time series. :return: `type` of object
[ "Get", "the", "object", "type", "of", "the", "value", "within", "the", "values", "portion", "of", "the", "time", "series", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/modules/time_series.py#L194-L205
linkedin/luminol
src/luminol/modules/time_series.py
TimeSeries.align
def align(self, other): """ Align two time series so that len(self) == len(other) and self.timstamps == other.timestamps. :return: :tuple:(`TimeSeries` object(the aligned self), `TimeSeries` object(the aligned other)) """ if isinstance(other, TimeSeries): aligned, other_aligned = {}, {} i, other_i = self.iteritems_silent(), other.iteritems_silent() item, other_item = next(i), next(other_i) while item and other_item: # Unpack timestamps and values. timestamp, value = item other_timestamp, other_value = other_item if timestamp == other_timestamp: aligned[timestamp] = value other_aligned[other_timestamp] = other_value item = next(i) other_item = next(other_i) elif timestamp < other_timestamp: aligned[timestamp] = value other_aligned[timestamp] = other_value item = next(i) else: aligned[other_timestamp] = value other_aligned[other_timestamp] = other_value other_item = next(other_i) # Align remaining items. while item: timestamp, value = item aligned[timestamp] = value other_aligned[timestamp] = other.values[-1] item = next(i) while other_item: other_timestamp, other_value = other_item aligned[other_timestamp] = self.values[-1] other_aligned[other_timestamp] = other_value other_item = next(other_i) return TimeSeries(aligned), TimeSeries(other_aligned)
python
def align(self, other): if isinstance(other, TimeSeries): aligned, other_aligned = {}, {} i, other_i = self.iteritems_silent(), other.iteritems_silent() item, other_item = next(i), next(other_i) while item and other_item: timestamp, value = item other_timestamp, other_value = other_item if timestamp == other_timestamp: aligned[timestamp] = value other_aligned[other_timestamp] = other_value item = next(i) other_item = next(other_i) elif timestamp < other_timestamp: aligned[timestamp] = value other_aligned[timestamp] = other_value item = next(i) else: aligned[other_timestamp] = value other_aligned[other_timestamp] = other_value other_item = next(other_i) while item: timestamp, value = item aligned[timestamp] = value other_aligned[timestamp] = other.values[-1] item = next(i) while other_item: other_timestamp, other_value = other_item aligned[other_timestamp] = self.values[-1] other_aligned[other_timestamp] = other_value other_item = next(other_i) return TimeSeries(aligned), TimeSeries(other_aligned)
[ "def", "align", "(", "self", ",", "other", ")", ":", "if", "isinstance", "(", "other", ",", "TimeSeries", ")", ":", "aligned", ",", "other_aligned", "=", "{", "}", ",", "{", "}", "i", ",", "other_i", "=", "self", ".", "iteritems_silent", "(", ")", ",", "other", ".", "iteritems_silent", "(", ")", "item", ",", "other_item", "=", "next", "(", "i", ")", ",", "next", "(", "other_i", ")", "while", "item", "and", "other_item", ":", "# Unpack timestamps and values.", "timestamp", ",", "value", "=", "item", "other_timestamp", ",", "other_value", "=", "other_item", "if", "timestamp", "==", "other_timestamp", ":", "aligned", "[", "timestamp", "]", "=", "value", "other_aligned", "[", "other_timestamp", "]", "=", "other_value", "item", "=", "next", "(", "i", ")", "other_item", "=", "next", "(", "other_i", ")", "elif", "timestamp", "<", "other_timestamp", ":", "aligned", "[", "timestamp", "]", "=", "value", "other_aligned", "[", "timestamp", "]", "=", "other_value", "item", "=", "next", "(", "i", ")", "else", ":", "aligned", "[", "other_timestamp", "]", "=", "value", "other_aligned", "[", "other_timestamp", "]", "=", "other_value", "other_item", "=", "next", "(", "other_i", ")", "# Align remaining items.", "while", "item", ":", "timestamp", ",", "value", "=", "item", "aligned", "[", "timestamp", "]", "=", "value", "other_aligned", "[", "timestamp", "]", "=", "other", ".", "values", "[", "-", "1", "]", "item", "=", "next", "(", "i", ")", "while", "other_item", ":", "other_timestamp", ",", "other_value", "=", "other_item", "aligned", "[", "other_timestamp", "]", "=", "self", ".", "values", "[", "-", "1", "]", "other_aligned", "[", "other_timestamp", "]", "=", "other_value", "other_item", "=", "next", "(", "other_i", ")", "return", "TimeSeries", "(", "aligned", ")", ",", "TimeSeries", "(", "other_aligned", ")" ]
Align two time series so that len(self) == len(other) and self.timstamps == other.timestamps. :return: :tuple:(`TimeSeries` object(the aligned self), `TimeSeries` object(the aligned other))
[ "Align", "two", "time", "series", "so", "that", "len", "(", "self", ")", "==", "len", "(", "other", ")", "and", "self", ".", "timstamps", "==", "other", ".", "timestamps", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/modules/time_series.py#L207-L246
linkedin/luminol
src/luminol/modules/time_series.py
TimeSeries.smooth
def smooth(self, smoothing_factor): """ return a new time series which is a exponential smoothed version of the original data series. soomth forward once, backward once, and then take the average. :param float smoothing_factor: smoothing factor :return: :class:`TimeSeries` object. """ forward_smooth = {} backward_smooth = {} output = {} if self: pre = self.values[0] next = self.values[-1] for key, value in self.items(): forward_smooth[key] = smoothing_factor * pre + (1 - smoothing_factor) * value pre = forward_smooth[key] for key, value in reversed(self.items()): backward_smooth[key] = smoothing_factor * next + (1 - smoothing_factor) * value next = backward_smooth[key] for key in forward_smooth.keys(): output[key] = (forward_smooth[key] + backward_smooth[key]) / 2 return TimeSeries(output)
python
def smooth(self, smoothing_factor): forward_smooth = {} backward_smooth = {} output = {} if self: pre = self.values[0] next = self.values[-1] for key, value in self.items(): forward_smooth[key] = smoothing_factor * pre + (1 - smoothing_factor) * value pre = forward_smooth[key] for key, value in reversed(self.items()): backward_smooth[key] = smoothing_factor * next + (1 - smoothing_factor) * value next = backward_smooth[key] for key in forward_smooth.keys(): output[key] = (forward_smooth[key] + backward_smooth[key]) / 2 return TimeSeries(output)
[ "def", "smooth", "(", "self", ",", "smoothing_factor", ")", ":", "forward_smooth", "=", "{", "}", "backward_smooth", "=", "{", "}", "output", "=", "{", "}", "if", "self", ":", "pre", "=", "self", ".", "values", "[", "0", "]", "next", "=", "self", ".", "values", "[", "-", "1", "]", "for", "key", ",", "value", "in", "self", ".", "items", "(", ")", ":", "forward_smooth", "[", "key", "]", "=", "smoothing_factor", "*", "pre", "+", "(", "1", "-", "smoothing_factor", ")", "*", "value", "pre", "=", "forward_smooth", "[", "key", "]", "for", "key", ",", "value", "in", "reversed", "(", "self", ".", "items", "(", ")", ")", ":", "backward_smooth", "[", "key", "]", "=", "smoothing_factor", "*", "next", "+", "(", "1", "-", "smoothing_factor", ")", "*", "value", "next", "=", "backward_smooth", "[", "key", "]", "for", "key", "in", "forward_smooth", ".", "keys", "(", ")", ":", "output", "[", "key", "]", "=", "(", "forward_smooth", "[", "key", "]", "+", "backward_smooth", "[", "key", "]", ")", "/", "2", "return", "TimeSeries", "(", "output", ")" ]
return a new time series which is a exponential smoothed version of the original data series. soomth forward once, backward once, and then take the average. :param float smoothing_factor: smoothing factor :return: :class:`TimeSeries` object.
[ "return", "a", "new", "time", "series", "which", "is", "a", "exponential", "smoothed", "version", "of", "the", "original", "data", "series", ".", "soomth", "forward", "once", "backward", "once", "and", "then", "take", "the", "average", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/modules/time_series.py#L248-L272
linkedin/luminol
src/luminol/modules/time_series.py
TimeSeries.add_offset
def add_offset(self, offset): """ Return a new time series with all timestamps incremented by some offset. :param int offset: The number of seconds to offset the time series. :return: `None` """ self.timestamps = [ts + offset for ts in self.timestamps]
python
def add_offset(self, offset): self.timestamps = [ts + offset for ts in self.timestamps]
[ "def", "add_offset", "(", "self", ",", "offset", ")", ":", "self", ".", "timestamps", "=", "[", "ts", "+", "offset", "for", "ts", "in", "self", ".", "timestamps", "]" ]
Return a new time series with all timestamps incremented by some offset. :param int offset: The number of seconds to offset the time series. :return: `None`
[ "Return", "a", "new", "time", "series", "with", "all", "timestamps", "incremented", "by", "some", "offset", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/modules/time_series.py#L274-L281
linkedin/luminol
src/luminol/modules/time_series.py
TimeSeries.normalize
def normalize(self): """ Return a new time series with all values normalized to 0 to 1. :return: `None` """ maximum = self.max() if maximum: self.values = [value / maximum for value in self.values]
python
def normalize(self): maximum = self.max() if maximum: self.values = [value / maximum for value in self.values]
[ "def", "normalize", "(", "self", ")", ":", "maximum", "=", "self", ".", "max", "(", ")", "if", "maximum", ":", "self", ".", "values", "=", "[", "value", "/", "maximum", "for", "value", "in", "self", ".", "values", "]" ]
Return a new time series with all values normalized to 0 to 1. :return: `None`
[ "Return", "a", "new", "time", "series", "with", "all", "values", "normalized", "to", "0", "to", "1", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/modules/time_series.py#L283-L291
linkedin/luminol
src/luminol/modules/time_series.py
TimeSeries.crop
def crop(self, start_timestamp, end_timestamp): """ Return a new TimeSeries object contains all the timstamps and values within the specified range. :param int start_timestamp: the start timestamp value :param int end_timestamp: the end timestamp value :return: :class:`TimeSeries` object. """ output = {} for key, value in self.items(): if key >= start_timestamp and key <= end_timestamp: output[key] = value if output: return TimeSeries(output) else: raise ValueError('TimeSeries data was empty or invalid.')
python
def crop(self, start_timestamp, end_timestamp): output = {} for key, value in self.items(): if key >= start_timestamp and key <= end_timestamp: output[key] = value if output: return TimeSeries(output) else: raise ValueError('TimeSeries data was empty or invalid.')
[ "def", "crop", "(", "self", ",", "start_timestamp", ",", "end_timestamp", ")", ":", "output", "=", "{", "}", "for", "key", ",", "value", "in", "self", ".", "items", "(", ")", ":", "if", "key", ">=", "start_timestamp", "and", "key", "<=", "end_timestamp", ":", "output", "[", "key", "]", "=", "value", "if", "output", ":", "return", "TimeSeries", "(", "output", ")", "else", ":", "raise", "ValueError", "(", "'TimeSeries data was empty or invalid.'", ")" ]
Return a new TimeSeries object contains all the timstamps and values within the specified range. :param int start_timestamp: the start timestamp value :param int end_timestamp: the end timestamp value :return: :class:`TimeSeries` object.
[ "Return", "a", "new", "TimeSeries", "object", "contains", "all", "the", "timstamps", "and", "values", "within", "the", "specified", "range", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/modules/time_series.py#L293-L310
linkedin/luminol
src/luminol/modules/time_series.py
TimeSeries.average
def average(self, default=None): """ Calculate the average value over the time series. :param default: Value to return as a default should the calculation not be possible. :return: Float representing the average value or `None`. """ return numpy.asscalar(numpy.average(self.values)) if self.values else default
python
def average(self, default=None): return numpy.asscalar(numpy.average(self.values)) if self.values else default
[ "def", "average", "(", "self", ",", "default", "=", "None", ")", ":", "return", "numpy", ".", "asscalar", "(", "numpy", ".", "average", "(", "self", ".", "values", ")", ")", "if", "self", ".", "values", "else", "default" ]
Calculate the average value over the time series. :param default: Value to return as a default should the calculation not be possible. :return: Float representing the average value or `None`.
[ "Calculate", "the", "average", "value", "over", "the", "time", "series", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/modules/time_series.py#L312-L319
linkedin/luminol
src/luminol/modules/time_series.py
TimeSeries.median
def median(self, default=None): """ Calculate the median value over the time series. :param default: Value to return as a default should the calculation not be possible. :return: Float representing the median value or `None`. """ return numpy.asscalar(numpy.median(self.values)) if self.values else default
python
def median(self, default=None): return numpy.asscalar(numpy.median(self.values)) if self.values else default
[ "def", "median", "(", "self", ",", "default", "=", "None", ")", ":", "return", "numpy", ".", "asscalar", "(", "numpy", ".", "median", "(", "self", ".", "values", ")", ")", "if", "self", ".", "values", "else", "default" ]
Calculate the median value over the time series. :param default: Value to return as a default should the calculation not be possible. :return: Float representing the median value or `None`.
[ "Calculate", "the", "median", "value", "over", "the", "time", "series", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/modules/time_series.py#L321-L328
linkedin/luminol
src/luminol/modules/time_series.py
TimeSeries.max
def max(self, default=None): """ Calculate the maximum value over the time series. :param default: Value to return as a default should the calculation not be possible. :return: Float representing the maximum value or `None`. """ return numpy.asscalar(numpy.max(self.values)) if self.values else default
python
def max(self, default=None): return numpy.asscalar(numpy.max(self.values)) if self.values else default
[ "def", "max", "(", "self", ",", "default", "=", "None", ")", ":", "return", "numpy", ".", "asscalar", "(", "numpy", ".", "max", "(", "self", ".", "values", ")", ")", "if", "self", ".", "values", "else", "default" ]
Calculate the maximum value over the time series. :param default: Value to return as a default should the calculation not be possible. :return: Float representing the maximum value or `None`.
[ "Calculate", "the", "maximum", "value", "over", "the", "time", "series", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/modules/time_series.py#L330-L337
linkedin/luminol
src/luminol/modules/time_series.py
TimeSeries.min
def min(self, default=None): """ Calculate the minimum value over the time series. :param default: Value to return as a default should the calculation not be possible. :return: Float representing the maximum value or `None`. """ return numpy.asscalar(numpy.min(self.values)) if self.values else default
python
def min(self, default=None): return numpy.asscalar(numpy.min(self.values)) if self.values else default
[ "def", "min", "(", "self", ",", "default", "=", "None", ")", ":", "return", "numpy", ".", "asscalar", "(", "numpy", ".", "min", "(", "self", ".", "values", ")", ")", "if", "self", ".", "values", "else", "default" ]
Calculate the minimum value over the time series. :param default: Value to return as a default should the calculation not be possible. :return: Float representing the maximum value or `None`.
[ "Calculate", "the", "minimum", "value", "over", "the", "time", "series", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/modules/time_series.py#L339-L346
linkedin/luminol
src/luminol/modules/time_series.py
TimeSeries.percentile
def percentile(self, n, default=None): """ Calculate the Nth Percentile value over the time series. :param int n: Integer value of the percentile to calculate. :param default: Value to return as a default should the calculation not be possible. :return: Float representing the Nth percentile value or `None`. """ return numpy.asscalar(numpy.percentile(self.values, n)) if self.values else default
python
def percentile(self, n, default=None): return numpy.asscalar(numpy.percentile(self.values, n)) if self.values else default
[ "def", "percentile", "(", "self", ",", "n", ",", "default", "=", "None", ")", ":", "return", "numpy", ".", "asscalar", "(", "numpy", ".", "percentile", "(", "self", ".", "values", ",", "n", ")", ")", "if", "self", ".", "values", "else", "default" ]
Calculate the Nth Percentile value over the time series. :param int n: Integer value of the percentile to calculate. :param default: Value to return as a default should the calculation not be possible. :return: Float representing the Nth percentile value or `None`.
[ "Calculate", "the", "Nth", "Percentile", "value", "over", "the", "time", "series", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/modules/time_series.py#L348-L356
linkedin/luminol
src/luminol/modules/time_series.py
TimeSeries.stdev
def stdev(self, default=None): """ Calculate the standard deviation of the time series. :param default: Value to return as a default should the calculation not be possible. :return: Float representing the standard deviation value or `None`. """ return numpy.asscalar(numpy.std(self.values)) if self.values else default
python
def stdev(self, default=None): return numpy.asscalar(numpy.std(self.values)) if self.values else default
[ "def", "stdev", "(", "self", ",", "default", "=", "None", ")", ":", "return", "numpy", ".", "asscalar", "(", "numpy", ".", "std", "(", "self", ".", "values", ")", ")", "if", "self", ".", "values", "else", "default" ]
Calculate the standard deviation of the time series. :param default: Value to return as a default should the calculation not be possible. :return: Float representing the standard deviation value or `None`.
[ "Calculate", "the", "standard", "deviation", "of", "the", "time", "series", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/modules/time_series.py#L358-L365
linkedin/luminol
src/luminol/modules/time_series.py
TimeSeries.sum
def sum(self, default=None): """ Calculate the sum of all the values in the times series. :param default: Value to return as a default should the calculation not be possible. :return: Float representing the sum or `None`. """ return numpy.asscalar(numpy.sum(self.values)) if self.values else default
python
def sum(self, default=None): return numpy.asscalar(numpy.sum(self.values)) if self.values else default
[ "def", "sum", "(", "self", ",", "default", "=", "None", ")", ":", "return", "numpy", ".", "asscalar", "(", "numpy", ".", "sum", "(", "self", ".", "values", ")", ")", "if", "self", ".", "values", "else", "default" ]
Calculate the sum of all the values in the times series. :param default: Value to return as a default should the calculation not be possible. :return: Float representing the sum or `None`.
[ "Calculate", "the", "sum", "of", "all", "the", "values", "in", "the", "times", "series", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/modules/time_series.py#L367-L374
linkedin/luminol
src/luminol/anomaly_detector.py
AnomalyDetector._get_algorithm
def _get_algorithm(self, algorithm_name): """ Get the specific algorithm. :param str algorithm_name: name of the algorithm to use(file name). :return: algorithm object. """ try: algorithm = anomaly_detector_algorithms[algorithm_name] return algorithm except KeyError: raise exceptions.AlgorithmNotFound('luminol.AnomalyDetector: ' + str(algorithm_name) + ' not found.')
python
def _get_algorithm(self, algorithm_name): try: algorithm = anomaly_detector_algorithms[algorithm_name] return algorithm except KeyError: raise exceptions.AlgorithmNotFound('luminol.AnomalyDetector: ' + str(algorithm_name) + ' not found.')
[ "def", "_get_algorithm", "(", "self", ",", "algorithm_name", ")", ":", "try", ":", "algorithm", "=", "anomaly_detector_algorithms", "[", "algorithm_name", "]", "return", "algorithm", "except", "KeyError", ":", "raise", "exceptions", ".", "AlgorithmNotFound", "(", "'luminol.AnomalyDetector: '", "+", "str", "(", "algorithm_name", ")", "+", "' not found.'", ")" ]
Get the specific algorithm. :param str algorithm_name: name of the algorithm to use(file name). :return: algorithm object.
[ "Get", "the", "specific", "algorithm", ".", ":", "param", "str", "algorithm_name", ":", "name", "of", "the", "algorithm", "to", "use", "(", "file", "name", ")", ".", ":", "return", ":", "algorithm", "object", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/anomaly_detector.py#L79-L89
linkedin/luminol
src/luminol/anomaly_detector.py
AnomalyDetector._detect
def _detect(self, score_only): """ Detect anomaly periods. :param bool score_only: if true, only anomaly scores are computed. """ try: algorithm = self.algorithm(**self.algorithm_params) self.anom_scores = algorithm.run() except exceptions.NotEnoughDataPoints: algorithm = anomaly_detector_algorithms['default_detector'](self.time_series) self.threshold = self.threshold or ANOMALY_THRESHOLD['default_detector'] self.anom_scores = algorithm.run() if not score_only: self._detect_anomalies()
python
def _detect(self, score_only): try: algorithm = self.algorithm(**self.algorithm_params) self.anom_scores = algorithm.run() except exceptions.NotEnoughDataPoints: algorithm = anomaly_detector_algorithms['default_detector'](self.time_series) self.threshold = self.threshold or ANOMALY_THRESHOLD['default_detector'] self.anom_scores = algorithm.run() if not score_only: self._detect_anomalies()
[ "def", "_detect", "(", "self", ",", "score_only", ")", ":", "try", ":", "algorithm", "=", "self", ".", "algorithm", "(", "*", "*", "self", ".", "algorithm_params", ")", "self", ".", "anom_scores", "=", "algorithm", ".", "run", "(", ")", "except", "exceptions", ".", "NotEnoughDataPoints", ":", "algorithm", "=", "anomaly_detector_algorithms", "[", "'default_detector'", "]", "(", "self", ".", "time_series", ")", "self", ".", "threshold", "=", "self", ".", "threshold", "or", "ANOMALY_THRESHOLD", "[", "'default_detector'", "]", "self", ".", "anom_scores", "=", "algorithm", ".", "run", "(", ")", "if", "not", "score_only", ":", "self", ".", "_detect_anomalies", "(", ")" ]
Detect anomaly periods. :param bool score_only: if true, only anomaly scores are computed.
[ "Detect", "anomaly", "periods", ".", ":", "param", "bool", "score_only", ":", "if", "true", "only", "anomaly", "scores", "are", "computed", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/anomaly_detector.py#L91-L104
linkedin/luminol
src/luminol/anomaly_detector.py
AnomalyDetector._detect_anomalies
def _detect_anomalies(self): """ Detect anomalies using a threshold on anomaly scores. """ anom_scores = self.anom_scores max_anom_score = anom_scores.max() anomalies = [] if max_anom_score: threshold = self.threshold or max_anom_score * self.score_percent_threshold # Find all the anomaly intervals. intervals = [] start, end = None, None for timestamp, value in anom_scores.iteritems(): if value > threshold: end = timestamp if not start: start = timestamp elif start and end is not None: intervals.append([start, end]) start = None end = None if start is not None: intervals.append([start, end]) # Locate the exact anomaly point within each anomaly interval. for interval_start, interval_end in intervals: interval_series = anom_scores.crop(interval_start, interval_end) self.refine_algorithm_params['time_series'] = interval_series refine_algorithm = self.refine_algorithm(**self.refine_algorithm_params) scores = refine_algorithm.run() max_refine_score = scores.max() # Get the timestamp of the maximal score. max_refine_timestamp = scores.timestamps[scores.values.index(max_refine_score)] anomaly = Anomaly(interval_start, interval_end, interval_series.max(), max_refine_timestamp) anomalies.append(anomaly) self.anomalies = anomalies
python
def _detect_anomalies(self): anom_scores = self.anom_scores max_anom_score = anom_scores.max() anomalies = [] if max_anom_score: threshold = self.threshold or max_anom_score * self.score_percent_threshold intervals = [] start, end = None, None for timestamp, value in anom_scores.iteritems(): if value > threshold: end = timestamp if not start: start = timestamp elif start and end is not None: intervals.append([start, end]) start = None end = None if start is not None: intervals.append([start, end]) for interval_start, interval_end in intervals: interval_series = anom_scores.crop(interval_start, interval_end) self.refine_algorithm_params['time_series'] = interval_series refine_algorithm = self.refine_algorithm(**self.refine_algorithm_params) scores = refine_algorithm.run() max_refine_score = scores.max() max_refine_timestamp = scores.timestamps[scores.values.index(max_refine_score)] anomaly = Anomaly(interval_start, interval_end, interval_series.max(), max_refine_timestamp) anomalies.append(anomaly) self.anomalies = anomalies
[ "def", "_detect_anomalies", "(", "self", ")", ":", "anom_scores", "=", "self", ".", "anom_scores", "max_anom_score", "=", "anom_scores", ".", "max", "(", ")", "anomalies", "=", "[", "]", "if", "max_anom_score", ":", "threshold", "=", "self", ".", "threshold", "or", "max_anom_score", "*", "self", ".", "score_percent_threshold", "# Find all the anomaly intervals.", "intervals", "=", "[", "]", "start", ",", "end", "=", "None", ",", "None", "for", "timestamp", ",", "value", "in", "anom_scores", ".", "iteritems", "(", ")", ":", "if", "value", ">", "threshold", ":", "end", "=", "timestamp", "if", "not", "start", ":", "start", "=", "timestamp", "elif", "start", "and", "end", "is", "not", "None", ":", "intervals", ".", "append", "(", "[", "start", ",", "end", "]", ")", "start", "=", "None", "end", "=", "None", "if", "start", "is", "not", "None", ":", "intervals", ".", "append", "(", "[", "start", ",", "end", "]", ")", "# Locate the exact anomaly point within each anomaly interval.", "for", "interval_start", ",", "interval_end", "in", "intervals", ":", "interval_series", "=", "anom_scores", ".", "crop", "(", "interval_start", ",", "interval_end", ")", "self", ".", "refine_algorithm_params", "[", "'time_series'", "]", "=", "interval_series", "refine_algorithm", "=", "self", ".", "refine_algorithm", "(", "*", "*", "self", ".", "refine_algorithm_params", ")", "scores", "=", "refine_algorithm", ".", "run", "(", ")", "max_refine_score", "=", "scores", ".", "max", "(", ")", "# Get the timestamp of the maximal score.", "max_refine_timestamp", "=", "scores", ".", "timestamps", "[", "scores", ".", "values", ".", "index", "(", "max_refine_score", ")", "]", "anomaly", "=", "Anomaly", "(", "interval_start", ",", "interval_end", ",", "interval_series", ".", "max", "(", ")", ",", "max_refine_timestamp", ")", "anomalies", ".", "append", "(", "anomaly", ")", "self", ".", "anomalies", "=", "anomalies" ]
Detect anomalies using a threshold on anomaly scores.
[ "Detect", "anomalies", "using", "a", "threshold", "on", "anomaly", "scores", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/anomaly_detector.py#L106-L145
linkedin/luminol
src/luminol/utils.py
compute_ema
def compute_ema(smoothing_factor, points): """ Compute exponential moving average of a list of points. :param float smoothing_factor: the smoothing factor. :param list points: the data points. :return list: all ema in a list. """ ema = [] # The initial point has a ema equal to itself. if(len(points) > 0): ema.append(points[0]) for i in range(1, len(points)): ema.append(smoothing_factor * points[i] + (1 - smoothing_factor) * ema[i - 1]) return ema
python
def compute_ema(smoothing_factor, points): ema = [] if(len(points) > 0): ema.append(points[0]) for i in range(1, len(points)): ema.append(smoothing_factor * points[i] + (1 - smoothing_factor) * ema[i - 1]) return ema
[ "def", "compute_ema", "(", "smoothing_factor", ",", "points", ")", ":", "ema", "=", "[", "]", "# The initial point has a ema equal to itself.", "if", "(", "len", "(", "points", ")", ">", "0", ")", ":", "ema", ".", "append", "(", "points", "[", "0", "]", ")", "for", "i", "in", "range", "(", "1", ",", "len", "(", "points", ")", ")", ":", "ema", ".", "append", "(", "smoothing_factor", "*", "points", "[", "i", "]", "+", "(", "1", "-", "smoothing_factor", ")", "*", "ema", "[", "i", "-", "1", "]", ")", "return", "ema" ]
Compute exponential moving average of a list of points. :param float smoothing_factor: the smoothing factor. :param list points: the data points. :return list: all ema in a list.
[ "Compute", "exponential", "moving", "average", "of", "a", "list", "of", "points", ".", ":", "param", "float", "smoothing_factor", ":", "the", "smoothing", "factor", ".", ":", "param", "list", "points", ":", "the", "data", "points", ".", ":", "return", "list", ":", "all", "ema", "in", "a", "list", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/utils.py#L25-L38
linkedin/luminol
src/luminol/utils.py
to_epoch
def to_epoch(t_str): """ Covert a timestamp string to an epoch number. :param str t_str: a timestamp string. :return int: epoch number of the timestamp. """ try: t = float(t_str) return t except ValueError: for format in constants.TIMESTAMP_STR_FORMATS: try: t = datetime.datetime.strptime(t_str, format) return float(time.mktime(t.utctimetuple()) * 1000.0 + t.microsecond / 1000.0) except ValueError: pass raise exceptions.InvalidDataFormat
python
def to_epoch(t_str): try: t = float(t_str) return t except ValueError: for format in constants.TIMESTAMP_STR_FORMATS: try: t = datetime.datetime.strptime(t_str, format) return float(time.mktime(t.utctimetuple()) * 1000.0 + t.microsecond / 1000.0) except ValueError: pass raise exceptions.InvalidDataFormat
[ "def", "to_epoch", "(", "t_str", ")", ":", "try", ":", "t", "=", "float", "(", "t_str", ")", "return", "t", "except", "ValueError", ":", "for", "format", "in", "constants", ".", "TIMESTAMP_STR_FORMATS", ":", "try", ":", "t", "=", "datetime", ".", "datetime", ".", "strptime", "(", "t_str", ",", "format", ")", "return", "float", "(", "time", ".", "mktime", "(", "t", ".", "utctimetuple", "(", ")", ")", "*", "1000.0", "+", "t", ".", "microsecond", "/", "1000.0", ")", "except", "ValueError", ":", "pass", "raise", "exceptions", ".", "InvalidDataFormat" ]
Covert a timestamp string to an epoch number. :param str t_str: a timestamp string. :return int: epoch number of the timestamp.
[ "Covert", "a", "timestamp", "string", "to", "an", "epoch", "number", ".", ":", "param", "str", "t_str", ":", "a", "timestamp", "string", ".", ":", "return", "int", ":", "epoch", "number", "of", "the", "timestamp", "." ]
train
https://github.com/linkedin/luminol/blob/42e4ab969b774ff98f902d064cb041556017f635/src/luminol/utils.py#L66-L82
jpoullet2000/atlasclient
atlasclient/exceptions.py
handle_response
def handle_response(response): """ Given a requests.Response object, throw the appropriate exception, if applicable. """ # ignore valid responses if response.status_code < 400: return cls = _status_to_exception_type.get(response.status_code, HttpError) kwargs = { 'code': response.status_code, 'method': response.request.method, 'url': response.request.url, 'details': response.text, } if response.headers and 'retry-after' in response.headers: kwargs['retry_after'] = response.headers.get('retry-after') raise cls(**kwargs)
python
def handle_response(response): if response.status_code < 400: return cls = _status_to_exception_type.get(response.status_code, HttpError) kwargs = { 'code': response.status_code, 'method': response.request.method, 'url': response.request.url, 'details': response.text, } if response.headers and 'retry-after' in response.headers: kwargs['retry_after'] = response.headers.get('retry-after') raise cls(**kwargs)
[ "def", "handle_response", "(", "response", ")", ":", "# ignore valid responses", "if", "response", ".", "status_code", "<", "400", ":", "return", "cls", "=", "_status_to_exception_type", ".", "get", "(", "response", ".", "status_code", ",", "HttpError", ")", "kwargs", "=", "{", "'code'", ":", "response", ".", "status_code", ",", "'method'", ":", "response", ".", "request", ".", "method", ",", "'url'", ":", "response", ".", "request", ".", "url", ",", "'details'", ":", "response", ".", "text", ",", "}", "if", "response", ".", "headers", "and", "'retry-after'", "in", "response", ".", "headers", ":", "kwargs", "[", "'retry_after'", "]", "=", "response", ".", "headers", ".", "get", "(", "'retry-after'", ")", "raise", "cls", "(", "*", "*", "kwargs", ")" ]
Given a requests.Response object, throw the appropriate exception, if applicable.
[ "Given", "a", "requests", ".", "Response", "object", "throw", "the", "appropriate", "exception", "if", "applicable", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/exceptions.py#L178-L199
jpoullet2000/atlasclient
atlasclient/models.py
Classification.create
def create(self, **kwargs): """Create a new instance of this resource type. As a general rule, the identifier should have been provided, but in some subclasses the identifier is server-side-generated. Those classes have to overload this method to deal with that scenario. """ if self.primary_key in kwargs: del kwargs[self.primary_key] data = self._generate_input_dict(**kwargs) self.load(self.client.post('/'.join(self.url.split('/')[:-1]) + 's', data=data)) return self
python
def create(self, **kwargs): if self.primary_key in kwargs: del kwargs[self.primary_key] data = self._generate_input_dict(**kwargs) self.load(self.client.post('/'.join(self.url.split('/')[:-1]) + 's', data=data)) return self
[ "def", "create", "(", "self", ",", "*", "*", "kwargs", ")", ":", "if", "self", ".", "primary_key", "in", "kwargs", ":", "del", "kwargs", "[", "self", ".", "primary_key", "]", "data", "=", "self", ".", "_generate_input_dict", "(", "*", "*", "kwargs", ")", "self", ".", "load", "(", "self", ".", "client", ".", "post", "(", "'/'", ".", "join", "(", "self", ".", "url", ".", "split", "(", "'/'", ")", "[", ":", "-", "1", "]", ")", "+", "'s'", ",", "data", "=", "data", ")", ")", "return", "self" ]
Create a new instance of this resource type. As a general rule, the identifier should have been provided, but in some subclasses the identifier is server-side-generated. Those classes have to overload this method to deal with that scenario.
[ "Create", "a", "new", "instance", "of", "this", "resource", "type", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/models.py#L128-L139
jpoullet2000/atlasclient
atlasclient/models.py
Classification.update
def update(self, **kwargs): """ Update a resource by passing in modifications via keyword arguments. """ data = self._generate_input_dict(**kwargs) self.load(self.client.put('/'.join(self.url.split('/')[:-1]) + 's', data=data)) return self
python
def update(self, **kwargs): data = self._generate_input_dict(**kwargs) self.load(self.client.put('/'.join(self.url.split('/')[:-1]) + 's', data=data)) return self
[ "def", "update", "(", "self", ",", "*", "*", "kwargs", ")", ":", "data", "=", "self", ".", "_generate_input_dict", "(", "*", "*", "kwargs", ")", "self", ".", "load", "(", "self", ".", "client", ".", "put", "(", "'/'", ".", "join", "(", "self", ".", "url", ".", "split", "(", "'/'", ")", "[", ":", "-", "1", "]", ")", "+", "'s'", ",", "data", "=", "data", ")", ")", "return", "self" ]
Update a resource by passing in modifications via keyword arguments.
[ "Update", "a", "resource", "by", "passing", "in", "modifications", "via", "keyword", "arguments", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/models.py#L142-L148
jpoullet2000/atlasclient
atlasclient/models.py
EntityGuidClassificationCollection.update
def update(self, **kwargs): """Update a resource by passing in modifications via keyword arguments. """ data = [] for cl in self: for classification_item in cl.list: class_item_dict = dict() for field in classification_item.fields: class_item_dict[field] = getattr(classification_item, field) data.append(class_item_dict) self.load(self.client.put(self.url, data=data)) return self
python
def update(self, **kwargs): data = [] for cl in self: for classification_item in cl.list: class_item_dict = dict() for field in classification_item.fields: class_item_dict[field] = getattr(classification_item, field) data.append(class_item_dict) self.load(self.client.put(self.url, data=data)) return self
[ "def", "update", "(", "self", ",", "*", "*", "kwargs", ")", ":", "data", "=", "[", "]", "for", "cl", "in", "self", ":", "for", "classification_item", "in", "cl", ".", "list", ":", "class_item_dict", "=", "dict", "(", ")", "for", "field", "in", "classification_item", ".", "fields", ":", "class_item_dict", "[", "field", "]", "=", "getattr", "(", "classification_item", ",", "field", ")", "data", ".", "append", "(", "class_item_dict", ")", "self", ".", "load", "(", "self", ".", "client", ".", "put", "(", "self", ".", "url", ",", "data", "=", "data", ")", ")", "return", "self" ]
Update a resource by passing in modifications via keyword arguments.
[ "Update", "a", "resource", "by", "passing", "in", "modifications", "via", "keyword", "arguments", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/models.py#L169-L181
jpoullet2000/atlasclient
atlasclient/models.py
EntityBulkCollection.create
def create(self, data, **kwargs): """ Create classifitions for specific entity """ self.client.post(self.url, data=data)
python
def create(self, data, **kwargs): self.client.post(self.url, data=data)
[ "def", "create", "(", "self", ",", "data", ",", "*", "*", "kwargs", ")", ":", "self", ".", "client", ".", "post", "(", "self", ".", "url", ",", "data", "=", "data", ")" ]
Create classifitions for specific entity
[ "Create", "classifitions", "for", "specific", "entity" ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/models.py#L259-L263
jpoullet2000/atlasclient
atlasclient/models.py
EntityBulkCollection.delete
def delete(self, guid): """ Delete guid """ return self.client.delete(self.url, params={'guid': guid})
python
def delete(self, guid): return self.client.delete(self.url, params={'guid': guid})
[ "def", "delete", "(", "self", ",", "guid", ")", ":", "return", "self", ".", "client", ".", "delete", "(", "self", ".", "url", ",", "params", "=", "{", "'guid'", ":", "guid", "}", ")" ]
Delete guid
[ "Delete", "guid" ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/models.py#L265-L269
jpoullet2000/atlasclient
atlasclient/models.py
RelationshipGuid.update
def update(self, **kwargs): """Update a resource by passing in modifications via keyword arguments. """ data = self._generate_input_dict(**kwargs) url = self.parent.url + '/relationship' self.load(self.client.put(url, data=data)) return self
python
def update(self, **kwargs): data = self._generate_input_dict(**kwargs) url = self.parent.url + '/relationship' self.load(self.client.put(url, data=data)) return self
[ "def", "update", "(", "self", ",", "*", "*", "kwargs", ")", ":", "data", "=", "self", ".", "_generate_input_dict", "(", "*", "*", "kwargs", ")", "url", "=", "self", ".", "parent", ".", "url", "+", "'/relationship'", "self", ".", "load", "(", "self", ".", "client", ".", "put", "(", "url", ",", "data", "=", "data", ")", ")", "return", "self" ]
Update a resource by passing in modifications via keyword arguments.
[ "Update", "a", "resource", "by", "passing", "in", "modifications", "via", "keyword", "arguments", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/models.py#L697-L704
jpoullet2000/atlasclient
atlasclient/models.py
RelationshipGuid.create
def create(self, **kwargs): """Raise error since guid cannot be duplicated """ raise exceptions.MethodNotImplemented(method=self.create, url=self.url, details='GUID cannot be duplicated, to create a new GUID use the relationship resource')
python
def create(self, **kwargs): raise exceptions.MethodNotImplemented(method=self.create, url=self.url, details='GUID cannot be duplicated, to create a new GUID use the relationship resource')
[ "def", "create", "(", "self", ",", "*", "*", "kwargs", ")", ":", "raise", "exceptions", ".", "MethodNotImplemented", "(", "method", "=", "self", ".", "create", ",", "url", "=", "self", ".", "url", ",", "details", "=", "'GUID cannot be duplicated, to create a new GUID use the relationship resource'", ")" ]
Raise error since guid cannot be duplicated
[ "Raise", "error", "since", "guid", "cannot", "be", "duplicated" ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/models.py#L706-L709
jpoullet2000/atlasclient
atlasclient/utils.py
normalize_underscore_case
def normalize_underscore_case(name): """Normalize an underscore-separated descriptor to something more readable. i.e. 'NAGIOS_SERVER' becomes 'Nagios Server', and 'host_components' becomes 'Host Components' """ normalized = name.lower() normalized = re.sub(r'_(\w)', lambda match: ' ' + match.group(1).upper(), normalized) return normalized[0].upper() + normalized[1:]
python
def normalize_underscore_case(name): normalized = name.lower() normalized = re.sub(r'_(\w)', lambda match: ' ' + match.group(1).upper(), normalized) return normalized[0].upper() + normalized[1:]
[ "def", "normalize_underscore_case", "(", "name", ")", ":", "normalized", "=", "name", ".", "lower", "(", ")", "normalized", "=", "re", ".", "sub", "(", "r'_(\\w)'", ",", "lambda", "match", ":", "' '", "+", "match", ".", "group", "(", "1", ")", ".", "upper", "(", ")", ",", "normalized", ")", "return", "normalized", "[", "0", "]", ".", "upper", "(", ")", "+", "normalized", "[", "1", ":", "]" ]
Normalize an underscore-separated descriptor to something more readable. i.e. 'NAGIOS_SERVER' becomes 'Nagios Server', and 'host_components' becomes 'Host Components'
[ "Normalize", "an", "underscore", "-", "separated", "descriptor", "to", "something", "more", "readable", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/utils.py#L32-L42
jpoullet2000/atlasclient
atlasclient/utils.py
normalize_camel_case
def normalize_camel_case(name): """Normalize a camelCase descriptor to something more readable. i.e. 'camelCase' or 'CamelCase' becomes 'Camel Case' """ normalized = re.sub('([a-z])([A-Z])', lambda match: ' '.join([match.group(1), match.group(2)]), name) return normalized[0].upper() + normalized[1:]
python
def normalize_camel_case(name): normalized = re.sub('([a-z])([A-Z])', lambda match: ' '.join([match.group(1), match.group(2)]), name) return normalized[0].upper() + normalized[1:]
[ "def", "normalize_camel_case", "(", "name", ")", ":", "normalized", "=", "re", ".", "sub", "(", "'([a-z])([A-Z])'", ",", "lambda", "match", ":", "' '", ".", "join", "(", "[", "match", ".", "group", "(", "1", ")", ",", "match", ".", "group", "(", "2", ")", "]", ")", ",", "name", ")", "return", "normalized", "[", "0", "]", ".", "upper", "(", ")", "+", "normalized", "[", "1", ":", "]" ]
Normalize a camelCase descriptor to something more readable. i.e. 'camelCase' or 'CamelCase' becomes 'Camel Case'
[ "Normalize", "a", "camelCase", "descriptor", "to", "something", "more", "readable", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/utils.py#L45-L53
jpoullet2000/atlasclient
atlasclient/utils.py
version_tuple
def version_tuple(version): """Convert a version string or tuple to a tuple. Should be returned in the form: (major, minor, release). """ if isinstance(version, str): return tuple(int(x) for x in version.split('.')) elif isinstance(version, tuple): return version else: raise ValueError("Invalid version: %s" % version)
python
def version_tuple(version): if isinstance(version, str): return tuple(int(x) for x in version.split('.')) elif isinstance(version, tuple): return version else: raise ValueError("Invalid version: %s" % version)
[ "def", "version_tuple", "(", "version", ")", ":", "if", "isinstance", "(", "version", ",", "str", ")", ":", "return", "tuple", "(", "int", "(", "x", ")", "for", "x", "in", "version", ".", "split", "(", "'.'", ")", ")", "elif", "isinstance", "(", "version", ",", "tuple", ")", ":", "return", "version", "else", ":", "raise", "ValueError", "(", "\"Invalid version: %s\"", "%", "version", ")" ]
Convert a version string or tuple to a tuple. Should be returned in the form: (major, minor, release).
[ "Convert", "a", "version", "string", "or", "tuple", "to", "a", "tuple", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/utils.py#L56-L66
jpoullet2000/atlasclient
atlasclient/utils.py
version_str
def version_str(version): """Convert a version tuple or string to a string. Should be returned in the form: major.minor.release """ if isinstance(version, str): return version elif isinstance(version, tuple): return '.'.join([str(int(x)) for x in version]) else: raise ValueError("Invalid version: %s" % version)
python
def version_str(version): if isinstance(version, str): return version elif isinstance(version, tuple): return '.'.join([str(int(x)) for x in version]) else: raise ValueError("Invalid version: %s" % version)
[ "def", "version_str", "(", "version", ")", ":", "if", "isinstance", "(", "version", ",", "str", ")", ":", "return", "version", "elif", "isinstance", "(", "version", ",", "tuple", ")", ":", "return", "'.'", ".", "join", "(", "[", "str", "(", "int", "(", "x", ")", ")", "for", "x", "in", "version", "]", ")", "else", ":", "raise", "ValueError", "(", "\"Invalid version: %s\"", "%", "version", ")" ]
Convert a version tuple or string to a string. Should be returned in the form: major.minor.release
[ "Convert", "a", "version", "tuple", "or", "string", "to", "a", "string", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/utils.py#L69-L79
jpoullet2000/atlasclient
atlasclient/utils.py
generate_http_basic_token
def generate_http_basic_token(username, password): """ Generates a HTTP basic token from username and password Returns a token string (not a byte) """ token = base64.b64encode('{}:{}'.format(username, password).encode('utf-8')).decode('utf-8') return token
python
def generate_http_basic_token(username, password): token = base64.b64encode('{}:{}'.format(username, password).encode('utf-8')).decode('utf-8') return token
[ "def", "generate_http_basic_token", "(", "username", ",", "password", ")", ":", "token", "=", "base64", ".", "b64encode", "(", "'{}:{}'", ".", "format", "(", "username", ",", "password", ")", ".", "encode", "(", "'utf-8'", ")", ")", ".", "decode", "(", "'utf-8'", ")", "return", "token" ]
Generates a HTTP basic token from username and password Returns a token string (not a byte)
[ "Generates", "a", "HTTP", "basic", "token", "from", "username", "and", "password" ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/utils.py#L81-L88
jpoullet2000/atlasclient
atlasclient/base.py
GeneratedIdentifierMixin.identifier
def identifier(self): """These models have server-generated identifiers. If we don't already have it in memory, then assume that it has not yet been generated. """ if self.primary_key not in self._data: return 'Unknown' return str(self._data[self.primary_key])
python
def identifier(self): if self.primary_key not in self._data: return 'Unknown' return str(self._data[self.primary_key])
[ "def", "identifier", "(", "self", ")", ":", "if", "self", ".", "primary_key", "not", "in", "self", ".", "_data", ":", "return", "'Unknown'", "return", "str", "(", "self", ".", "_data", "[", "self", ".", "primary_key", "]", ")" ]
These models have server-generated identifiers. If we don't already have it in memory, then assume that it has not yet been generated.
[ "These", "models", "have", "server", "-", "generated", "identifiers", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/base.py#L79-L87
jpoullet2000/atlasclient
atlasclient/base.py
QueryableModelCollection.url
def url(self): """The url for this collection.""" if self.parent is None: # TODO: differing API Versions? pieces = [self.client.base_url, 'api', 'atlas', 'v2'] else: pieces = [self.parent.url] pieces.append(self.model_class.path) return '/'.join(pieces)
python
def url(self): if self.parent is None: pieces = [self.client.base_url, 'api', 'atlas', 'v2'] else: pieces = [self.parent.url] pieces.append(self.model_class.path) return '/'.join(pieces)
[ "def", "url", "(", "self", ")", ":", "if", "self", ".", "parent", "is", "None", ":", "# TODO: differing API Versions?", "pieces", "=", "[", "self", ".", "client", ".", "base_url", ",", "'api'", ",", "'atlas'", ",", "'v2'", "]", "else", ":", "pieces", "=", "[", "self", ".", "parent", ".", "url", "]", "pieces", ".", "append", "(", "self", ".", "model_class", ".", "path", ")", "return", "'/'", ".", "join", "(", "pieces", ")" ]
The url for this collection.
[ "The", "url", "for", "this", "collection", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/base.py#L230-L239
jpoullet2000/atlasclient
atlasclient/base.py
QueryableModelCollection.inflate
def inflate(self): """Load the collection from the server, if necessary.""" if not self._is_inflated: self.check_version() for k, v in self._filter.items(): if '[' in v: self._filter[k] = ast.literal_eval(v) self.load(self.client.get(self.url, params=self._filter)) self._is_inflated = True return self
python
def inflate(self): if not self._is_inflated: self.check_version() for k, v in self._filter.items(): if '[' in v: self._filter[k] = ast.literal_eval(v) self.load(self.client.get(self.url, params=self._filter)) self._is_inflated = True return self
[ "def", "inflate", "(", "self", ")", ":", "if", "not", "self", ".", "_is_inflated", ":", "self", ".", "check_version", "(", ")", "for", "k", ",", "v", "in", "self", ".", "_filter", ".", "items", "(", ")", ":", "if", "'['", "in", "v", ":", "self", ".", "_filter", "[", "k", "]", "=", "ast", ".", "literal_eval", "(", "v", ")", "self", ".", "load", "(", "self", ".", "client", ".", "get", "(", "self", ".", "url", ",", "params", "=", "self", ".", "_filter", ")", ")", "self", ".", "_is_inflated", "=", "True", "return", "self" ]
Load the collection from the server, if necessary.
[ "Load", "the", "collection", "from", "the", "server", "if", "necessary", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/base.py#L241-L251
jpoullet2000/atlasclient
atlasclient/base.py
QueryableModelCollection.load
def load(self, response): """Parse the GET response for the collection. This operates as a lazy-loader, meaning that the data are only downloaded from the server if there are not already loaded. Collection items are loaded sequentially. In some rare cases, a collection can have an asynchronous request triggered. For those cases, we handle it here. """ self._models = [] if isinstance(response, dict): for key in response.keys(): model = self.model_class(self, href='') model.load(response[key]) self._models.append(model) else: for item in response: model = self.model_class(self, href=item.get('href')) model.load(item) self._models.append(model)
python
def load(self, response): self._models = [] if isinstance(response, dict): for key in response.keys(): model = self.model_class(self, href='') model.load(response[key]) self._models.append(model) else: for item in response: model = self.model_class(self, href=item.get('href')) model.load(item) self._models.append(model)
[ "def", "load", "(", "self", ",", "response", ")", ":", "self", ".", "_models", "=", "[", "]", "if", "isinstance", "(", "response", ",", "dict", ")", ":", "for", "key", "in", "response", ".", "keys", "(", ")", ":", "model", "=", "self", ".", "model_class", "(", "self", ",", "href", "=", "''", ")", "model", ".", "load", "(", "response", "[", "key", "]", ")", "self", ".", "_models", ".", "append", "(", "model", ")", "else", ":", "for", "item", "in", "response", ":", "model", "=", "self", ".", "model_class", "(", "self", ",", "href", "=", "item", ".", "get", "(", "'href'", ")", ")", "model", ".", "load", "(", "item", ")", "self", ".", "_models", ".", "append", "(", "model", ")" ]
Parse the GET response for the collection. This operates as a lazy-loader, meaning that the data are only downloaded from the server if there are not already loaded. Collection items are loaded sequentially. In some rare cases, a collection can have an asynchronous request triggered. For those cases, we handle it here.
[ "Parse", "the", "GET", "response", "for", "the", "collection", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/base.py#L254-L275
jpoullet2000/atlasclient
atlasclient/base.py
QueryableModelCollection.create
def create(self, *args, **kwargs): """Add a resource to this collection.""" href = self.url if len(args) == 1: kwargs[self.model_class.primary_key] = args[0] href = '/'.join([href, args[0]]) model = self.model_class(self, href=href.replace('classifications/', 'classification/'), data=kwargs) model.create(**kwargs) self._models.append(model) return model
python
def create(self, *args, **kwargs): href = self.url if len(args) == 1: kwargs[self.model_class.primary_key] = args[0] href = '/'.join([href, args[0]]) model = self.model_class(self, href=href.replace('classifications/', 'classification/'), data=kwargs) model.create(**kwargs) self._models.append(model) return model
[ "def", "create", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "href", "=", "self", ".", "url", "if", "len", "(", "args", ")", "==", "1", ":", "kwargs", "[", "self", ".", "model_class", ".", "primary_key", "]", "=", "args", "[", "0", "]", "href", "=", "'/'", ".", "join", "(", "[", "href", ",", "args", "[", "0", "]", "]", ")", "model", "=", "self", ".", "model_class", "(", "self", ",", "href", "=", "href", ".", "replace", "(", "'classifications/'", ",", "'classification/'", ")", ",", "data", "=", "kwargs", ")", "model", ".", "create", "(", "*", "*", "kwargs", ")", "self", ".", "_models", ".", "append", "(", "model", ")", "return", "model" ]
Add a resource to this collection.
[ "Add", "a", "resource", "to", "this", "collection", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/base.py#L277-L288
jpoullet2000/atlasclient
atlasclient/base.py
QueryableModelCollection.update
def update(self, **kwargs): """Update all resources in this collection.""" self.inflate() for model in self._models: model.update(**kwargs) return self
python
def update(self, **kwargs): self.inflate() for model in self._models: model.update(**kwargs) return self
[ "def", "update", "(", "self", ",", "*", "*", "kwargs", ")", ":", "self", ".", "inflate", "(", ")", "for", "model", "in", "self", ".", "_models", ":", "model", ".", "update", "(", "*", "*", "kwargs", ")", "return", "self" ]
Update all resources in this collection.
[ "Update", "all", "resources", "in", "this", "collection", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/base.py#L290-L295
jpoullet2000/atlasclient
atlasclient/base.py
QueryableModelCollection.delete
def delete(self, **kwargs): """Delete all resources in this collection.""" self.inflate() for model in self._models: model.delete(**kwargs) return
python
def delete(self, **kwargs): self.inflate() for model in self._models: model.delete(**kwargs) return
[ "def", "delete", "(", "self", ",", "*", "*", "kwargs", ")", ":", "self", ".", "inflate", "(", ")", "for", "model", "in", "self", ".", "_models", ":", "model", ".", "delete", "(", "*", "*", "kwargs", ")", "return" ]
Delete all resources in this collection.
[ "Delete", "all", "resources", "in", "this", "collection", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/base.py#L297-L302
jpoullet2000/atlasclient
atlasclient/base.py
QueryableModelCollection.wait
def wait(self, **kwargs): """Wait until any pending asynchronous requests are finished for this collection.""" if self.request: self.request.wait(**kwargs) self.request = None return self.inflate()
python
def wait(self, **kwargs): if self.request: self.request.wait(**kwargs) self.request = None return self.inflate()
[ "def", "wait", "(", "self", ",", "*", "*", "kwargs", ")", ":", "if", "self", ".", "request", ":", "self", ".", "request", ".", "wait", "(", "*", "*", "kwargs", ")", "self", ".", "request", "=", "None", "return", "self", ".", "inflate", "(", ")" ]
Wait until any pending asynchronous requests are finished for this collection.
[ "Wait", "until", "any", "pending", "asynchronous", "requests", "are", "finished", "for", "this", "collection", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/base.py#L305-L310
jpoullet2000/atlasclient
atlasclient/base.py
QueryableModel.url
def url(self): """Gets the url for the resource this model represents. It will just use the 'href' passed in to the constructor if that exists. Otherwise, it will generated it based on the collection's url and the model's identifier. """ if self._href is not None: return self._href if self.identifier: # for some reason atlas does not use classifications here in the path when considering one classification path = '/'.join([self.parent.url.replace('classifications/', 'classficiation/'), self.identifier]) return path raise exceptions.ClientError("Not able to determine object URL")
python
def url(self): if self._href is not None: return self._href if self.identifier: path = '/'.join([self.parent.url.replace('classifications/', 'classficiation/'), self.identifier]) return path raise exceptions.ClientError("Not able to determine object URL")
[ "def", "url", "(", "self", ")", ":", "if", "self", ".", "_href", "is", "not", "None", ":", "return", "self", ".", "_href", "if", "self", ".", "identifier", ":", "# for some reason atlas does not use classifications here in the path when considering one classification", "path", "=", "'/'", ".", "join", "(", "[", "self", ".", "parent", ".", "url", ".", "replace", "(", "'classifications/'", ",", "'classficiation/'", ")", ",", "self", ".", "identifier", "]", ")", "return", "path", "raise", "exceptions", ".", "ClientError", "(", "\"Not able to determine object URL\"", ")" ]
Gets the url for the resource this model represents. It will just use the 'href' passed in to the constructor if that exists. Otherwise, it will generated it based on the collection's url and the model's identifier.
[ "Gets", "the", "url", "for", "the", "resource", "this", "model", "represents", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/base.py#L568-L581
jpoullet2000/atlasclient
atlasclient/base.py
QueryableModel.inflate
def inflate(self): """Load the resource from the server, if not already loaded.""" if not self._is_inflated: if self._is_inflating: # catch infinite recursion when attempting to inflate # an object that doesn't have enough data to inflate msg = ("There is not enough data to inflate this object. " "Need either an href: {} or a {}: {}") msg = msg.format(self._href, self.primary_key, self._data.get(self.primary_key)) raise exceptions.ClientError(msg) self._is_inflating = True try: params = self.searchParameters if hasattr(self, 'searchParameters') else {} # To keep the method same as the original request. The default is GET self.load(self.client.request(self.method, self.url, **params)) except Exception: self.load(self._data) self._is_inflated = True self._is_inflating = False return self
python
def inflate(self): if not self._is_inflated: if self._is_inflating: msg = ("There is not enough data to inflate this object. " "Need either an href: {} or a {}: {}") msg = msg.format(self._href, self.primary_key, self._data.get(self.primary_key)) raise exceptions.ClientError(msg) self._is_inflating = True try: params = self.searchParameters if hasattr(self, 'searchParameters') else {} self.load(self.client.request(self.method, self.url, **params)) except Exception: self.load(self._data) self._is_inflated = True self._is_inflating = False return self
[ "def", "inflate", "(", "self", ")", ":", "if", "not", "self", ".", "_is_inflated", ":", "if", "self", ".", "_is_inflating", ":", "# catch infinite recursion when attempting to inflate", "# an object that doesn't have enough data to inflate", "msg", "=", "(", "\"There is not enough data to inflate this object. \"", "\"Need either an href: {} or a {}: {}\"", ")", "msg", "=", "msg", ".", "format", "(", "self", ".", "_href", ",", "self", ".", "primary_key", ",", "self", ".", "_data", ".", "get", "(", "self", ".", "primary_key", ")", ")", "raise", "exceptions", ".", "ClientError", "(", "msg", ")", "self", ".", "_is_inflating", "=", "True", "try", ":", "params", "=", "self", ".", "searchParameters", "if", "hasattr", "(", "self", ",", "'searchParameters'", ")", "else", "{", "}", "# To keep the method same as the original request. The default is GET", "self", ".", "load", "(", "self", ".", "client", ".", "request", "(", "self", ".", "method", ",", "self", ".", "url", ",", "*", "*", "params", ")", ")", "except", "Exception", ":", "self", ".", "load", "(", "self", ".", "_data", ")", "self", ".", "_is_inflated", "=", "True", "self", ".", "_is_inflating", "=", "False", "return", "self" ]
Load the resource from the server, if not already loaded.
[ "Load", "the", "resource", "from", "the", "server", "if", "not", "already", "loaded", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/base.py#L583-L605
jpoullet2000/atlasclient
atlasclient/base.py
QueryableModel.load
def load(self, response): """The load method parses the raw JSON response from the server. Most models are not returned in the main response body, but in a key such as 'entity', defined by the 'data_key' attribute on the class. Also, related objects are often returned and can be used to pre-cache related model objects without having to contact the server again. This method handles all of those cases. Also, if a request has triggered a background operation, the request details are returned in a 'Requests' section. We need to store that request object so we can poll it until completion. """ if 'href' in response: self._href = response.pop('href') if self.data_key and self.data_key in response: self._data.update(response.pop(self.data_key)) # preload related object collections, if received for rel in [x for x in self.relationships if x in response and response[x]]: rel_class = self.relationships[rel] collection = rel_class.collection_class( self.client, rel_class, parent=self ) self._relationship_cache[rel] = collection(response[rel]) else: self._data.update(response)
python
def load(self, response): if 'href' in response: self._href = response.pop('href') if self.data_key and self.data_key in response: self._data.update(response.pop(self.data_key)) for rel in [x for x in self.relationships if x in response and response[x]]: rel_class = self.relationships[rel] collection = rel_class.collection_class( self.client, rel_class, parent=self ) self._relationship_cache[rel] = collection(response[rel]) else: self._data.update(response)
[ "def", "load", "(", "self", ",", "response", ")", ":", "if", "'href'", "in", "response", ":", "self", ".", "_href", "=", "response", ".", "pop", "(", "'href'", ")", "if", "self", ".", "data_key", "and", "self", ".", "data_key", "in", "response", ":", "self", ".", "_data", ".", "update", "(", "response", ".", "pop", "(", "self", ".", "data_key", ")", ")", "# preload related object collections, if received", "for", "rel", "in", "[", "x", "for", "x", "in", "self", ".", "relationships", "if", "x", "in", "response", "and", "response", "[", "x", "]", "]", ":", "rel_class", "=", "self", ".", "relationships", "[", "rel", "]", "collection", "=", "rel_class", ".", "collection_class", "(", "self", ".", "client", ",", "rel_class", ",", "parent", "=", "self", ")", "self", ".", "_relationship_cache", "[", "rel", "]", "=", "collection", "(", "response", "[", "rel", "]", ")", "else", ":", "self", ".", "_data", ".", "update", "(", "response", ")" ]
The load method parses the raw JSON response from the server. Most models are not returned in the main response body, but in a key such as 'entity', defined by the 'data_key' attribute on the class. Also, related objects are often returned and can be used to pre-cache related model objects without having to contact the server again. This method handles all of those cases. Also, if a request has triggered a background operation, the request details are returned in a 'Requests' section. We need to store that request object so we can poll it until completion.
[ "The", "load", "method", "parses", "the", "raw", "JSON", "response", "from", "the", "server", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/base.py#L623-L648
jpoullet2000/atlasclient
atlasclient/base.py
QueryableModel.create
def create(self, **kwargs): """Create a new instance of this resource type. As a general rule, the identifier should have been provided, but in some subclasses the identifier is server-side-generated. Those classes have to overload this method to deal with that scenario. """ self.method = 'post' if self.primary_key in kwargs: del kwargs[self.primary_key] data = self._generate_input_dict(**kwargs) self.load(self.client.post(self.url, data=data)) return self
python
def create(self, **kwargs): self.method = 'post' if self.primary_key in kwargs: del kwargs[self.primary_key] data = self._generate_input_dict(**kwargs) self.load(self.client.post(self.url, data=data)) return self
[ "def", "create", "(", "self", ",", "*", "*", "kwargs", ")", ":", "self", ".", "method", "=", "'post'", "if", "self", ".", "primary_key", "in", "kwargs", ":", "del", "kwargs", "[", "self", ".", "primary_key", "]", "data", "=", "self", ".", "_generate_input_dict", "(", "*", "*", "kwargs", ")", "self", ".", "load", "(", "self", ".", "client", ".", "post", "(", "self", ".", "url", ",", "data", "=", "data", ")", ")", "return", "self" ]
Create a new instance of this resource type. As a general rule, the identifier should have been provided, but in some subclasses the identifier is server-side-generated. Those classes have to overload this method to deal with that scenario.
[ "Create", "a", "new", "instance", "of", "this", "resource", "type", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/base.py#L651-L663
jpoullet2000/atlasclient
atlasclient/base.py
QueryableModel.delete
def delete(self, **kwargs): """Delete a resource by issuing a DELETE http request against it.""" self.method = 'delete' if len(kwargs) > 0: self.load(self.client.delete(self.url, params=kwargs)) else: self.load(self.client.delete(self.url)) self.parent.remove(self) return
python
def delete(self, **kwargs): self.method = 'delete' if len(kwargs) > 0: self.load(self.client.delete(self.url, params=kwargs)) else: self.load(self.client.delete(self.url)) self.parent.remove(self) return
[ "def", "delete", "(", "self", ",", "*", "*", "kwargs", ")", ":", "self", ".", "method", "=", "'delete'", "if", "len", "(", "kwargs", ")", ">", "0", ":", "self", ".", "load", "(", "self", ".", "client", ".", "delete", "(", "self", ".", "url", ",", "params", "=", "kwargs", ")", ")", "else", ":", "self", ".", "load", "(", "self", ".", "client", ".", "delete", "(", "self", ".", "url", ")", ")", "self", ".", "parent", ".", "remove", "(", "self", ")", "return" ]
Delete a resource by issuing a DELETE http request against it.
[ "Delete", "a", "resource", "by", "issuing", "a", "DELETE", "http", "request", "against", "it", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/base.py#L686-L694
jpoullet2000/atlasclient
atlasclient/events.py
publish
def publish(obj, event, event_state, **kwargs): """Publish an event from an object. This is a really basic pub-sub event system to allow for tracking progress on methods externally. It fires the events for the first match it finds in the object hierarchy, going most specific to least. If no match is found for the exact event+event_state, the most specific event+ANY is fired instead. Multiple callbacks can be bound to the event+event_state if desired. All will be fired in the order they were registered. """ # short-circuit if nothing is listening if len(EVENT_HANDLERS) == 0: return if inspect.isclass(obj): pub_cls = obj else: pub_cls = obj.__class__ potential = [x.__name__ for x in inspect.getmro(pub_cls)] # if we don't find a match for this event/event_state we fire the events # for this event/ANY instead for the closest match fallbacks = None callbacks = [] for cls in potential: event_key = '.'.join([cls, event, event_state]) backup_key = '.'.join([cls, event, states.ANY]) if event_key in EVENT_HANDLERS: callbacks = EVENT_HANDLERS[event_key] break elif fallbacks is None and backup_key in EVENT_HANDLERS: fallbacks = EVENT_HANDLERS[backup_key] if fallbacks is not None: callbacks = fallbacks for callback in callbacks: callback(obj, **kwargs) return
python
def publish(obj, event, event_state, **kwargs): if len(EVENT_HANDLERS) == 0: return if inspect.isclass(obj): pub_cls = obj else: pub_cls = obj.__class__ potential = [x.__name__ for x in inspect.getmro(pub_cls)] fallbacks = None callbacks = [] for cls in potential: event_key = '.'.join([cls, event, event_state]) backup_key = '.'.join([cls, event, states.ANY]) if event_key in EVENT_HANDLERS: callbacks = EVENT_HANDLERS[event_key] break elif fallbacks is None and backup_key in EVENT_HANDLERS: fallbacks = EVENT_HANDLERS[backup_key] if fallbacks is not None: callbacks = fallbacks for callback in callbacks: callback(obj, **kwargs) return
[ "def", "publish", "(", "obj", ",", "event", ",", "event_state", ",", "*", "*", "kwargs", ")", ":", "# short-circuit if nothing is listening", "if", "len", "(", "EVENT_HANDLERS", ")", "==", "0", ":", "return", "if", "inspect", ".", "isclass", "(", "obj", ")", ":", "pub_cls", "=", "obj", "else", ":", "pub_cls", "=", "obj", ".", "__class__", "potential", "=", "[", "x", ".", "__name__", "for", "x", "in", "inspect", ".", "getmro", "(", "pub_cls", ")", "]", "# if we don't find a match for this event/event_state we fire the events", "# for this event/ANY instead for the closest match", "fallbacks", "=", "None", "callbacks", "=", "[", "]", "for", "cls", "in", "potential", ":", "event_key", "=", "'.'", ".", "join", "(", "[", "cls", ",", "event", ",", "event_state", "]", ")", "backup_key", "=", "'.'", ".", "join", "(", "[", "cls", ",", "event", ",", "states", ".", "ANY", "]", ")", "if", "event_key", "in", "EVENT_HANDLERS", ":", "callbacks", "=", "EVENT_HANDLERS", "[", "event_key", "]", "break", "elif", "fallbacks", "is", "None", "and", "backup_key", "in", "EVENT_HANDLERS", ":", "fallbacks", "=", "EVENT_HANDLERS", "[", "backup_key", "]", "if", "fallbacks", "is", "not", "None", ":", "callbacks", "=", "fallbacks", "for", "callback", "in", "callbacks", ":", "callback", "(", "obj", ",", "*", "*", "kwargs", ")", "return" ]
Publish an event from an object. This is a really basic pub-sub event system to allow for tracking progress on methods externally. It fires the events for the first match it finds in the object hierarchy, going most specific to least. If no match is found for the exact event+event_state, the most specific event+ANY is fired instead. Multiple callbacks can be bound to the event+event_state if desired. All will be fired in the order they were registered.
[ "Publish", "an", "event", "from", "an", "object", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/events.py#L41-L81
jpoullet2000/atlasclient
atlasclient/events.py
subscribe
def subscribe(obj, event, callback, event_state=None): """Subscribe an event from an class. Subclasses of the class/object will also fire events for this class, unless a more specific event exists. """ if inspect.isclass(obj): cls = obj.__name__ else: cls = obj.__class__.__name__ if event_state is None: event_state = states.ANY event_key = '.'.join([cls, event, event_state]) if event_key not in EVENT_HANDLERS: EVENT_HANDLERS[event_key] = [] EVENT_HANDLERS[event_key].append(callback) return
python
def subscribe(obj, event, callback, event_state=None): if inspect.isclass(obj): cls = obj.__name__ else: cls = obj.__class__.__name__ if event_state is None: event_state = states.ANY event_key = '.'.join([cls, event, event_state]) if event_key not in EVENT_HANDLERS: EVENT_HANDLERS[event_key] = [] EVENT_HANDLERS[event_key].append(callback) return
[ "def", "subscribe", "(", "obj", ",", "event", ",", "callback", ",", "event_state", "=", "None", ")", ":", "if", "inspect", ".", "isclass", "(", "obj", ")", ":", "cls", "=", "obj", ".", "__name__", "else", ":", "cls", "=", "obj", ".", "__class__", ".", "__name__", "if", "event_state", "is", "None", ":", "event_state", "=", "states", ".", "ANY", "event_key", "=", "'.'", ".", "join", "(", "[", "cls", ",", "event", ",", "event_state", "]", ")", "if", "event_key", "not", "in", "EVENT_HANDLERS", ":", "EVENT_HANDLERS", "[", "event_key", "]", "=", "[", "]", "EVENT_HANDLERS", "[", "event_key", "]", ".", "append", "(", "callback", ")", "return" ]
Subscribe an event from an class. Subclasses of the class/object will also fire events for this class, unless a more specific event exists.
[ "Subscribe", "an", "event", "from", "an", "class", "." ]
train
https://github.com/jpoullet2000/atlasclient/blob/4548b441143ebf7fc4075d113db5ca5a23e0eed2/atlasclient/events.py#L84-L103
psolin/cleanco
cleanco.py
cleanco.clean_name
def clean_name(self, suffix=True, prefix=False, middle=False, multi=False): "return cleared version of the business name" name = self.business_name # Run it through the string_stripper once more name = self.string_stripper(name) loname = name.lower() # return name without suffixed/prefixed/middle type term(s) for item in suffix_sort: if suffix: if loname.endswith(" " + item): start = loname.find(item) end = len(item) name = name[0:-end-1] name = self.string_stripper(name) if multi==False: break if prefix: if loname.startswith(item+' '): name = name[len(item)+1:] if multi==False: break if middle: term = ' ' + item + ' ' if term in loname: start = loname.find(term) end = start + len(term) name = name[:start] + " " + name[end:] if multi==False: break return self.string_stripper(name)
python
def clean_name(self, suffix=True, prefix=False, middle=False, multi=False): "return cleared version of the business name" name = self.business_name name = self.string_stripper(name) loname = name.lower() for item in suffix_sort: if suffix: if loname.endswith(" " + item): start = loname.find(item) end = len(item) name = name[0:-end-1] name = self.string_stripper(name) if multi==False: break if prefix: if loname.startswith(item+' '): name = name[len(item)+1:] if multi==False: break if middle: term = ' ' + item + ' ' if term in loname: start = loname.find(term) end = start + len(term) name = name[:start] + " " + name[end:] if multi==False: break return self.string_stripper(name)
[ "def", "clean_name", "(", "self", ",", "suffix", "=", "True", ",", "prefix", "=", "False", ",", "middle", "=", "False", ",", "multi", "=", "False", ")", ":", "name", "=", "self", ".", "business_name", "# Run it through the string_stripper once more", "name", "=", "self", ".", "string_stripper", "(", "name", ")", "loname", "=", "name", ".", "lower", "(", ")", "# return name without suffixed/prefixed/middle type term(s)", "for", "item", "in", "suffix_sort", ":", "if", "suffix", ":", "if", "loname", ".", "endswith", "(", "\" \"", "+", "item", ")", ":", "start", "=", "loname", ".", "find", "(", "item", ")", "end", "=", "len", "(", "item", ")", "name", "=", "name", "[", "0", ":", "-", "end", "-", "1", "]", "name", "=", "self", ".", "string_stripper", "(", "name", ")", "if", "multi", "==", "False", ":", "break", "if", "prefix", ":", "if", "loname", ".", "startswith", "(", "item", "+", "' '", ")", ":", "name", "=", "name", "[", "len", "(", "item", ")", "+", "1", ":", "]", "if", "multi", "==", "False", ":", "break", "if", "middle", ":", "term", "=", "' '", "+", "item", "+", "' '", "if", "term", "in", "loname", ":", "start", "=", "loname", ".", "find", "(", "term", ")", "end", "=", "start", "+", "len", "(", "term", ")", "name", "=", "name", "[", ":", "start", "]", "+", "\" \"", "+", "name", "[", "end", ":", "]", "if", "multi", "==", "False", ":", "break", "return", "self", ".", "string_stripper", "(", "name", ")" ]
return cleared version of the business name
[ "return", "cleared", "version", "of", "the", "business", "name" ]
train
https://github.com/psolin/cleanco/blob/56ff6542c339df625adcaf7f4ed4c81035fd575a/cleanco.py#L70-L104
mosdef-hub/foyer
foyer/smarts_graph.py
_find_chordless_cycles
def _find_chordless_cycles(bond_graph, max_cycle_size): """Find all chordless cycles (i.e. rings) in the bond graph Traverses the bond graph to determine all cycles (i.e. rings) each atom is contained within. Algorithm has been adapted from: https://stackoverflow.com/questions/4022662/find-all-chordless-cycles-in-an-undirected-graph/4028855#4028855 """ cycles = [[] for _ in bond_graph.nodes] ''' For all nodes we need to find the cycles that they are included within. ''' for i, node in enumerate(bond_graph.nodes): neighbors = list(bond_graph.neighbors(node)) pairs = list(itertools.combinations(neighbors, 2)) ''' Loop over all pairs of neighbors of the node. We will see if a ring exists that includes these branches. ''' for pair in pairs: ''' We need to store all node sequences that could be rings. We will update this as we traverse the graph. ''' connected = False possible_rings = [] last_node = pair[0] ring = [last_node, node, pair[1]] possible_rings.append(ring) if bond_graph.has_edge(last_node, pair[1]): cycles[i].append(ring) connected = True while not connected: ''' Branch and create a new list of possible rings ''' new_possible_rings = [] for possible_ring in possible_rings: next_neighbors = list(bond_graph.neighbors(possible_ring[-1])) for next_neighbor in next_neighbors: if next_neighbor != possible_ring[-2]: new_possible_rings.append(possible_ring + \ [next_neighbor]) possible_rings = new_possible_rings for possible_ring in possible_rings: if bond_graph.has_edge(possible_ring[-1], last_node): if any([bond_graph.has_edge(possible_ring[-1], internal_node) for internal_node in possible_ring[1:-2]]): pass else: cycles[i].append(possible_ring) connected = True if not possible_rings or len(possible_rings[0]) == max_cycle_size: break return cycles
python
def _find_chordless_cycles(bond_graph, max_cycle_size): cycles = [[] for _ in bond_graph.nodes] for i, node in enumerate(bond_graph.nodes): neighbors = list(bond_graph.neighbors(node)) pairs = list(itertools.combinations(neighbors, 2)) for pair in pairs: connected = False possible_rings = [] last_node = pair[0] ring = [last_node, node, pair[1]] possible_rings.append(ring) if bond_graph.has_edge(last_node, pair[1]): cycles[i].append(ring) connected = True while not connected: new_possible_rings = [] for possible_ring in possible_rings: next_neighbors = list(bond_graph.neighbors(possible_ring[-1])) for next_neighbor in next_neighbors: if next_neighbor != possible_ring[-2]: new_possible_rings.append(possible_ring + \ [next_neighbor]) possible_rings = new_possible_rings for possible_ring in possible_rings: if bond_graph.has_edge(possible_ring[-1], last_node): if any([bond_graph.has_edge(possible_ring[-1], internal_node) for internal_node in possible_ring[1:-2]]): pass else: cycles[i].append(possible_ring) connected = True if not possible_rings or len(possible_rings[0]) == max_cycle_size: break return cycles
[ "def", "_find_chordless_cycles", "(", "bond_graph", ",", "max_cycle_size", ")", ":", "cycles", "=", "[", "[", "]", "for", "_", "in", "bond_graph", ".", "nodes", "]", "''' \n For all nodes we need to find the cycles that they are included within.\n '''", "for", "i", ",", "node", "in", "enumerate", "(", "bond_graph", ".", "nodes", ")", ":", "neighbors", "=", "list", "(", "bond_graph", ".", "neighbors", "(", "node", ")", ")", "pairs", "=", "list", "(", "itertools", ".", "combinations", "(", "neighbors", ",", "2", ")", ")", "''' \n Loop over all pairs of neighbors of the node. We will see if a ring\n exists that includes these branches.\n '''", "for", "pair", "in", "pairs", ":", "''' \n We need to store all node sequences that could be rings. We will\n update this as we traverse the graph.\n '''", "connected", "=", "False", "possible_rings", "=", "[", "]", "last_node", "=", "pair", "[", "0", "]", "ring", "=", "[", "last_node", ",", "node", ",", "pair", "[", "1", "]", "]", "possible_rings", ".", "append", "(", "ring", ")", "if", "bond_graph", ".", "has_edge", "(", "last_node", ",", "pair", "[", "1", "]", ")", ":", "cycles", "[", "i", "]", ".", "append", "(", "ring", ")", "connected", "=", "True", "while", "not", "connected", ":", "'''\n Branch and create a new list of possible rings\n '''", "new_possible_rings", "=", "[", "]", "for", "possible_ring", "in", "possible_rings", ":", "next_neighbors", "=", "list", "(", "bond_graph", ".", "neighbors", "(", "possible_ring", "[", "-", "1", "]", ")", ")", "for", "next_neighbor", "in", "next_neighbors", ":", "if", "next_neighbor", "!=", "possible_ring", "[", "-", "2", "]", ":", "new_possible_rings", ".", "append", "(", "possible_ring", "+", "[", "next_neighbor", "]", ")", "possible_rings", "=", "new_possible_rings", "for", "possible_ring", "in", "possible_rings", ":", "if", "bond_graph", ".", "has_edge", "(", "possible_ring", "[", "-", "1", "]", ",", "last_node", ")", ":", "if", "any", "(", "[", "bond_graph", ".", "has_edge", "(", "possible_ring", "[", "-", "1", "]", ",", "internal_node", ")", "for", "internal_node", "in", "possible_ring", "[", "1", ":", "-", "2", "]", "]", ")", ":", "pass", "else", ":", "cycles", "[", "i", "]", ".", "append", "(", "possible_ring", ")", "connected", "=", "True", "if", "not", "possible_rings", "or", "len", "(", "possible_rings", "[", "0", "]", ")", "==", "max_cycle_size", ":", "break", "return", "cycles" ]
Find all chordless cycles (i.e. rings) in the bond graph Traverses the bond graph to determine all cycles (i.e. rings) each atom is contained within. Algorithm has been adapted from: https://stackoverflow.com/questions/4022662/find-all-chordless-cycles-in-an-undirected-graph/4028855#4028855
[ "Find", "all", "chordless", "cycles", "(", "i", ".", "e", ".", "rings", ")", "in", "the", "bond", "graph" ]
train
https://github.com/mosdef-hub/foyer/blob/9e39c71208fc01a6cc7b7cbe5a533c56830681d3/foyer/smarts_graph.py#L241-L301
mosdef-hub/foyer
foyer/smarts_graph.py
_prepare_atoms
def _prepare_atoms(topology, compute_cycles=False): """Compute cycles and add white-/blacklists to atoms.""" atom1 = next(topology.atoms()) has_whitelists = hasattr(atom1, 'whitelist') has_cycles = hasattr(atom1, 'cycles') compute_cycles = compute_cycles and not has_cycles if compute_cycles or not has_whitelists: for atom in topology.atoms(): if compute_cycles: atom.cycles = set() if not has_whitelists: atom.whitelist = OrderedSet() atom.blacklist = OrderedSet() if compute_cycles: bond_graph = nx.Graph() bond_graph.add_nodes_from(topology.atoms()) bond_graph.add_edges_from(topology.bonds()) all_cycles = _find_chordless_cycles(bond_graph, max_cycle_size=8) for atom, cycles in zip(bond_graph.nodes, all_cycles): for cycle in cycles: atom.cycles.add(tuple(cycle))
python
def _prepare_atoms(topology, compute_cycles=False): atom1 = next(topology.atoms()) has_whitelists = hasattr(atom1, 'whitelist') has_cycles = hasattr(atom1, 'cycles') compute_cycles = compute_cycles and not has_cycles if compute_cycles or not has_whitelists: for atom in topology.atoms(): if compute_cycles: atom.cycles = set() if not has_whitelists: atom.whitelist = OrderedSet() atom.blacklist = OrderedSet() if compute_cycles: bond_graph = nx.Graph() bond_graph.add_nodes_from(topology.atoms()) bond_graph.add_edges_from(topology.bonds()) all_cycles = _find_chordless_cycles(bond_graph, max_cycle_size=8) for atom, cycles in zip(bond_graph.nodes, all_cycles): for cycle in cycles: atom.cycles.add(tuple(cycle))
[ "def", "_prepare_atoms", "(", "topology", ",", "compute_cycles", "=", "False", ")", ":", "atom1", "=", "next", "(", "topology", ".", "atoms", "(", ")", ")", "has_whitelists", "=", "hasattr", "(", "atom1", ",", "'whitelist'", ")", "has_cycles", "=", "hasattr", "(", "atom1", ",", "'cycles'", ")", "compute_cycles", "=", "compute_cycles", "and", "not", "has_cycles", "if", "compute_cycles", "or", "not", "has_whitelists", ":", "for", "atom", "in", "topology", ".", "atoms", "(", ")", ":", "if", "compute_cycles", ":", "atom", ".", "cycles", "=", "set", "(", ")", "if", "not", "has_whitelists", ":", "atom", ".", "whitelist", "=", "OrderedSet", "(", ")", "atom", ".", "blacklist", "=", "OrderedSet", "(", ")", "if", "compute_cycles", ":", "bond_graph", "=", "nx", ".", "Graph", "(", ")", "bond_graph", ".", "add_nodes_from", "(", "topology", ".", "atoms", "(", ")", ")", "bond_graph", ".", "add_edges_from", "(", "topology", ".", "bonds", "(", ")", ")", "all_cycles", "=", "_find_chordless_cycles", "(", "bond_graph", ",", "max_cycle_size", "=", "8", ")", "for", "atom", ",", "cycles", "in", "zip", "(", "bond_graph", ".", "nodes", ",", "all_cycles", ")", ":", "for", "cycle", "in", "cycles", ":", "atom", ".", "cycles", ".", "add", "(", "tuple", "(", "cycle", ")", ")" ]
Compute cycles and add white-/blacklists to atoms.
[ "Compute", "cycles", "and", "add", "white", "-", "/", "blacklists", "to", "atoms", "." ]
train
https://github.com/mosdef-hub/foyer/blob/9e39c71208fc01a6cc7b7cbe5a533c56830681d3/foyer/smarts_graph.py#L304-L326
mosdef-hub/foyer
foyer/smarts_graph.py
SMARTSGraph._add_nodes
def _add_nodes(self): """Add all atoms in the SMARTS string as nodes in the graph.""" for n, atom in enumerate(self.ast.select('atom')): self.add_node(n, atom=atom) self._atom_indices[id(atom)] = n
python
def _add_nodes(self): for n, atom in enumerate(self.ast.select('atom')): self.add_node(n, atom=atom) self._atom_indices[id(atom)] = n
[ "def", "_add_nodes", "(", "self", ")", ":", "for", "n", ",", "atom", "in", "enumerate", "(", "self", ".", "ast", ".", "select", "(", "'atom'", ")", ")", ":", "self", ".", "add_node", "(", "n", ",", "atom", "=", "atom", ")", "self", ".", "_atom_indices", "[", "id", "(", "atom", ")", "]", "=", "n" ]
Add all atoms in the SMARTS string as nodes in the graph.
[ "Add", "all", "atoms", "in", "the", "SMARTS", "string", "as", "nodes", "in", "the", "graph", "." ]
train
https://github.com/mosdef-hub/foyer/blob/9e39c71208fc01a6cc7b7cbe5a533c56830681d3/foyer/smarts_graph.py#L51-L55