def test_resolve_ids(): data = pd.DataFrame( [[1, 0.4, "seven"], [2, 0.5, "eight"], [3, 0.6, "nine"]], columns=["int", "float", "str"]) assert [False, True, False] == resolve_ids(data, "", { "str": "eight" }).tolist() assert [False, False, False] == resolve_ids(data, "", { "int": 1, "str": "eight" }).tolist() assert [True, True, False] == resolve_ids(data, "", { "$or": [{ "str": "seven" }, { "float": (0.5, 0.5) }] }).tolist() assert [False, False, True] == resolve_ids(data, "", { "$and": [{ "str": "nine" }, { "int": 3 }] }).tolist() assert [False, False, True] == resolve_ids(data, "", { "$and": [{ "str": "nine" }, { "$or": [{ "int": 1 }, { "int": 3 }] }] }).tolist() assert [False, True, True] == resolve_ids(data, "", { "$or": [{ "float": (0.59, 0.61) }, { "$and": [{ "str": "eight" }, { "int": 2 }] }] }).tolist() assert [True, False, True] == resolve_ids(data, "", { "$or": [{ "node_id": 0 }, { "edge_id": 2 }] }).tolist() with pytest.raises(BluepySnapError) as e: resolve_ids(data, "", {"str": {"$regex": "*.some", "edge_id": 2}}) assert "Value operators can't be used with plain values" in e.value.args[0]
def _edge_ids_by_filter(self, queries, raise_missing_prop): """Return edge IDs if their properties match the `queries` dict. `props` values could be: pairs (range match for floating dtype fields) scalar or iterables (exact or "one of" match for other fields) You can use the special operators '$or' and '$and' also to combine different queries together. Examples: >>> self._edge_ids_by_filter({ Edge.POST_SECTION_ID: (0, 1), >>> Edge.AXONAL_DELAY: (.5, 2.) }) >>> self._edge_ids_by_filter({'$or': [{ Edge.PRE_X_CENTER: [2, 3]}, >>> { Edge.POST_SECTION_POS: (0, 1), >>> Edge.SYN_WEIGHT: (0.,1.4) }]}) """ properties = query.get_properties(queries) unknown_props = properties - self.property_names if raise_missing_prop and unknown_props: raise BluepySnapError(f"Unknown edge properties: {unknown_props}") res = [] ids = self.ids(None) chunk_size = int(1e8) for chunk in np.array_split(ids, 1 + len(ids) // chunk_size): data = self.get(chunk, properties - unknown_props) res.extend(chunk[query.resolve_ids(data, self.name, queries)]) return np.array(res, dtype=IDS_DTYPE)
def _node_ids_by_filter(self, queries, raise_missing_prop): """Return node IDs if their properties match the `queries` dict. `props` values could be: pairs (range match for floating dtype fields) scalar or iterables (exact or "one of" match for other fields) You can use the special operators '$or' and '$and' also to combine different queries together. Examples: >>> _node_ids_by_filter({ Node.X: (0, 1), Node.MTYPE: 'L1_SLAC' }) >>> _node_ids_by_filter({ Node.LAYER: [2, 3] }) >>> _node_ids_by_filter({'$or': [{ Node.LAYER: [2, 3]}, >>> { Node.X: (0, 1), Node.MTYPE: 'L1_SLAC' }]}) """ queries = self._resolve_nodesets(queries) if raise_missing_prop: properties = query.get_properties(queries) if not properties.issubset(self._data.columns): unknown_props = properties - set(self._data.columns) raise BluepySnapError( f"Unknown node properties: {unknown_props}") idx = query.resolve_ids(self._data, self.name, queries) return self._data.index[idx].values
def test_resolve_ids(): data = pd.DataFrame([ [1, .4, 'seven'], [2, .5, 'eight'], [3, .6, 'nine']], columns=['int', 'float', 'str']) assert [False, True, False] == resolve_ids(data, '', {'str': 'eight'}).tolist() assert [False, False, False] == resolve_ids(data, '', {'int': 1, 'str': 'eight'}).tolist() assert [True, True, False] == resolve_ids( data, '', {'$or': [{'str': 'seven'}, {'float': (.5, .5)}]}).tolist() assert [False, False, True] == resolve_ids( data, '', {'$and': [{'str': 'nine'}, {'int': 3}]}).tolist() assert [False, False, True] == resolve_ids( data, '', {'$and': [{'str': 'nine'}, {'$or': [{'int': 1}, {'int': 3}]}]}).tolist() assert [False, True, True] == resolve_ids(data, '', {'$or': [ {'float': (.59, .61)}, {'$and': [{'str': 'eight'}, {'int': 2}]}]}).tolist() assert [True, False, True] == resolve_ids( data, '', {'$or': [{'node_id': 0}, {'edge_id': 2}]}).tolist() with pytest.raises(BluepySnapError) as e: resolve_ids(data, '', {'str': {'$regex': '*.some', 'edge_id': 2}}) assert "Value operators can't be used with plain values" in e.value.args[0]