[docs]deftransform(self,row):"""Nearest Neighbor schema transforms the row into Datum, its associated ID. If row_id does not be set, assign uuid as row_id. """row_id=row.get(self._id_key,None)ifrow_idisnotNone:row_id=unicode_t(row_id)else:row_id=unicode_t(uuid.uuid4())d=self._transform_as_datum(row,None,[self._id_key])returnrow_id,d

[docs]defset_row(self,dataset):"""Updates the row whose id is id with given row. If the row with the same id already exists, the row is overwritten with row (note that this behavior is different from that of recommender). Otherwise, new row entry will be created. If the server that manages the row and the server that received this RPC request are same, this operation is reflected instantly. If not, update operation is reflected after mix."""cli=self._client()for(idx,(row_id,d))indataset:ifrow_idisNone:raiseRuntimeError('dataset must have id.')result=cli.set_row(row_id,d)yield(idx,row_id,result)

[docs]defneighbor_row_from_id(self,dataset,size=10):"""Returns size rows (at maximum) that have most similar datum to id and their distance values."""cli=self._client()for(idx,(row_id,_))indataset:ifrow_idisNone:raiseRuntimeError('each data point must have its id.')result=cli.neighbor_row_from_id(row_id,size)yield(idx,row_id,result)

[docs]defneighbor_row_from_datum(self,dataset,size=10):"""Returns size rows (at maximum) of which datum are most similar to query and their distance values."""cli=self._client()for(idx,(row_id,d))indataset:ifrow_idisNone:raiseRuntimeError('each data point must have its id.')result=cli.neighbor_row_from_datum(d,size)yield(idx,row_id,result)

[docs]defsimilar_row_from_id(self,dataset,size=10):"""Returns ret_num rows (at maximum) that have most similar datum to id and their similarity values. """cli=self._client()for(idx,(row_id,_))indataset:ifrow_idisNone:raiseRuntimeError('Non ID-based datasets must use `similar_row_from_datum`')result=cli.similar_row_from_id(row_id,size)yield(idx,row_id,result)

[docs]defsimilar_row_from_datum(self,dataset,size=10):"""Returns ret_num rows (at maximum) of which datum are most similar to query and their similarity values. """cli=self._client()for(idx,(row_id,d))indataset:result=cli.similar_row_from_datum(d,size)yield(idx,row_id,result)

[docs]defget_all_rows(self):"""Returns the list of all row IDs."""cli=self._client()returncli.get_all_rows()

[docs]classConfig(GenericConfig):"""Configuration to run Nearest Neighbor service."""