Skip to content
Snippets Groups Projects
Commit dbd8d74e authored by johannes bilk's avatar johannes bilk
Browse files

updated readme, extended the ‘where’ method

parent 3610e265
No related branches found
No related tags found
No related merge requests found
......@@ -89,10 +89,23 @@ loadFromRoot[0]
will return either the array containing the event numbers of the first entry of every
array contained in the classes dict.
It is possible to filter through the data:
```python
loadFromRoot.where('clsSize == 1')
loadFromRoot.where('clsSize > 1')
```
or even:
```python
loadFromRoot.where('eventNumbers in [0,1,2]')
```
And finally you can convert the dict into a structured Numpy array by simply writing:
```python
loadFromRoot.loadFromRoot()
loadFromRoot.getStructuredArray()
```
This last command returns a Numpy array. From there the user can save it using
......
......@@ -152,31 +152,35 @@ class Rootable:
def where(self, *conditions: str) -> dict:
"""
Filters the data based on the provided conditions.
:param conditions: Dictionary containing the conditions for filtering. The keys should be the names of the data fields, and the values should be callables or expressions that return a boolean mask.
:return: Dictionary containing the filtered data.
:param conditions: List of conditions as strings for filtering. The keys should be the names of the data fields, and the conditions should be in a format that can be split into key, operator, and value.
:return: Instance of the class containing the filtered data.
"""
filteredData = self.data.copy()
mask = np.ones(len(next(iter(self.data.values()))), dtype=bool) # Initial mask allowing all elements
mask = np.ones(len(next(iter(self.data.values()))), dtype=bool) # Initial mask allowing all elements
# Applying the conditions to create the mask
for condition in conditions:
key, op, value = condition.split()
comparison_value = float(value)
field_values = self.data[key].astype(float)
# Determine the correct comparison to apply
operation = {
'==': np.equal,
'<': np.less,
'>': np.greater,
'<=': np.less_equal,
'>=': np.greater_equal,
}.get(op)
if operation is None:
raise ValueError(f"Invalid operator {op}")
mask &= operation(field_values, comparison_value)
key, op, value = condition.split(None, 2) # Split by the first two spaces only
if op == 'in':
value = eval(value)
mask &= np.isin(self.data[key], value)
else:
comparisionValue = float(value)
fieldValues = self.data[key].astype(float)
# Determine the correct comparison to apply
operation = {
'==': np.equal,
'<': np.less,
'>': np.greater,
'<=': np.less_equal,
'>=': np.greater_equal,
}.get(op)
if operation is None:
raise ValueError(f"Invalid operator {op}")
mask &= operation(fieldValues, comparisionValue)
# Applying the mask to filter the data
for key, values in filteredData.items():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment