Skip to content

Commit dedf6c7

Browse files
committed
RCDB great expectation integration
1 parent b205b9d commit dedf6c7

4 files changed

Lines changed: 70 additions & 1 deletion

File tree

.github/workflows/python-examples.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ jobs:
3434
cd $GITHUB_WORKSPACE/python
3535
pip install --editable .
3636
pip install jsonpickle # for writing object example
37+
pip install pandas # for the DataFrame examples (04, 05)
3738
3839
# - name: Download HallD SQLite snapshot
3940
# run: |
@@ -53,6 +54,12 @@ jobs:
5354
- name: "Example: 03_select_values_custom_runs"
5455
run: python $GITHUB_WORKSPACE/python/examples/03_select_values_custom_runs.py
5556

57+
- name: "Example: 04_conditions_dataframe_run_period"
58+
run: python $GITHUB_WORKSPACE/python/examples/04_conditions_dataframe_run_period.py
59+
60+
- name: "Example: 05_conditions_dataframe_last_runs"
61+
run: python $GITHUB_WORKSPACE/python/examples/05_conditions_dataframe_last_runs.py
62+
5663
- name: "Example: 10_create_conditions_basic"
5764
run: python $GITHUB_WORKSPACE/python/examples/10_create_conditions_basic.py
5865

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# E X A M P L E 4
2+
# Make a pandas DataFrame for a whole run period:
3+
# rows = runs of the period
4+
# columns = all known condition types
5+
# cells = condition values (missing ones become NaN)
6+
7+
import pandas as pd
8+
9+
# import RCDB
10+
from rcdb.provider import RCDBProvider
11+
12+
# connect to DB
13+
db = RCDBProvider("mysql://rcdb@hallddb.jlab.org/rcdb2")
14+
15+
# pick a run period (here the latest one by run number)
16+
run_period = max(db.get_run_periods(), key=lambda rp: rp.run_max)
17+
print("Run period: {} [{}-{}]".format(run_period.name, run_period.run_min, run_period.run_max))
18+
19+
# all known condition names -> these become the DataFrame columns
20+
condition_names = sorted(ct.name for ct in db.get_condition_types())
21+
22+
# select those values for every run in the period
23+
table = db.select_values(condition_names, run_min=run_period.run_min, run_max=run_period.run_max)
24+
25+
# build the DataFrame: conditions as columns, run number as the index
26+
df = pd.DataFrame(table.rows, columns=table.selected_conditions).set_index("run")
27+
28+
# P R I N T O U T
29+
print(df)
30+
print("It took {:.2f} sec ".format(table.performance['total']))
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# E X A M P L E 5
2+
# Make a pandas DataFrame for a set of runs (here the last 10):
3+
# rows = the selected runs
4+
# columns = all known condition types
5+
# cells = condition values (missing ones become NaN)
6+
7+
import pandas as pd
8+
9+
# import RCDB
10+
from rcdb.provider import RCDBProvider
11+
from rcdb.model import Run
12+
13+
# connect to DB
14+
db = RCDBProvider("mysql://rcdb@hallddb.jlab.org/rcdb2")
15+
16+
# the last 10 runs (highest run numbers)
17+
last_runs = [run.number for run in
18+
db.session.query(Run).order_by(Run.number.desc()).limit(10).all()]
19+
print("Last {} runs: {}".format(len(last_runs), last_runs))
20+
21+
# all known condition names -> these become the DataFrame columns
22+
condition_names = sorted(ct.name for ct in db.get_condition_types())
23+
24+
# select those values for exactly those runs (pass them via runs=)
25+
table = db.select_values(condition_names, runs=last_runs)
26+
27+
# build the DataFrame: conditions as columns, run number as the index
28+
df = pd.DataFrame(table.rows, columns=table.selected_conditions).set_index("run")
29+
30+
# P R I N T O U T
31+
print(df)
32+
print("It took {:.2f} sec ".format(table.performance['total']))

python/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ dependencies = [
4747
]
4848

4949
[project.optional-dependencies]
50-
examples = ["jsonpickle"]
50+
examples = ["jsonpickle", "pandas"]
5151
dev = ["pyflakes>=3"]
5252

5353

0 commit comments

Comments
 (0)