[docs]deftransform(self,row):""" Anomaly schema transforms the row into Datum, its associated ID and flag. Flag can be a value of any type. It is provided for convenience to calculate precision. """row_id=row.get(self._id_key,None)ifrow_idisnotNone:row_id=unicode_t(row_id)row_flag=row.get(self._flag_key,None)d=self._transform_as_datum(row,None,[self._id_key,self._flag_key])return(row_id,row_flag,d)

[docs]defadd(self,dataset):""" Adds data points to the anomaly model using the given dataset and returns LOF scores. """cli=self._client()for(idx,(row_id,row_flag,d))indataset:ifrow_idisnotNone:raiseRuntimeError('ID-based datasets must use `overwrite` or `update` instead of `add`')result=cli.add(d)yield(idx,result.id,row_flag,result.score)

[docs]defadd_bulk(self,dataset):""" Adds data points to the anomaly model using the given dataset and returns a list of data point IDs. """cli=self._client()data=[d[1][2]fordindataset]returncli.add_bulk(data)

[docs]defupdate(self,dataset):""" Updates data points in the anomaly model using the given dataset and returns LOF scores. """cli=self._client()for(idx,(row_id,row_flag,d))indataset:ifrow_idisNone:raiseRuntimeError('Non ID-based datasets must use `add` instead of `update`')result=cli.update(row_id,d)yield(idx,row_id,row_flag,result)

[docs]defoverwrite(self,dataset):""" Overwrites data points in the anomaly model using the given dataset and returns LOF scores. """cli=self._client()for(idx,(row_id,row_flag,d))indataset:ifrow_idisNone:raiseRuntimeError('Non ID-based datasets must use `add` instead of `overwrite`')result=cli.overwrite(row_id,d)yield(idx,row_id,row_flag,result)

[docs]defcalc_score(self,dataset):""" Calculates LOF scores for the given dataset. """cli=self._client()for(idx,(row_id,row_flag,d))indataset:result=cli.calc_score(d)yield(idx,row_id,row_flag,result)

@classmethoddef_default_method(cls):return'lof'@classmethoddef_default_parameter(cls,method):params={'nearest_neighbor_num':10,'reverse_nearest_neighbor_num':30,'method':None,'parameter':{},'ignore_kth_same_point':True,}ifmethod=='lof':params['method']='inverted_index_euclid'elifmethod=='light_lof':params['method']='euclid_lsh'params['parameter']={'threads':-1,# use number of logical CPU cores'hash_num':64,}else:raiseRuntimeError('unknown method: {0}'.format(method))returnparams