Skip to content

Commit 8d3cbba

Browse files
authored
466 update context to include performance month and performance data and staff number (#469)
* staff_number, performance_month, performance_df, subject_graph moved to context
1 parent aeaebf1 commit 8d3cbba

File tree

13 files changed

+187
-139
lines changed

13 files changed

+187
-139
lines changed

scaffold/api.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import pandas as pd
21
from fastapi import FastAPI, HTTPException, Request
32
from fastapi.responses import RedirectResponse
43

54
from scaffold import context
65
from scaffold.pipeline import pipeline
76
from scaffold.startup import startup
87
from scaffold.utils.settings import settings
9-
from scaffold.utils.utils import get_performance_month
108

119
app = FastAPI()
1210

@@ -34,15 +32,10 @@ async def template():
3432
async def createprecisionfeedback(info: Request):
3533
req_info = await info.json()
3634

37-
performance_month = get_performance_month(req_info)
38-
performance_df = pd.DataFrame(
39-
req_info["Performance_data"][1:], columns=req_info["Performance_data"][0]
40-
)
41-
context.create(req_info, performance_df.at[0, "staff_number"])
4235
try:
43-
full_message = pipeline(
44-
performance_df, performance_df.at[0, "staff_number"], performance_month
45-
)
36+
context.from_req(req_info)
37+
38+
full_message = pipeline()
4639
full_message["message_instance_id"] = req_info["message_instance_id"]
4740
full_message["performance_data"] = req_info["Performance_data"]
4841
except HTTPException as e:

scaffold/bitstomach/bitstomach.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pandas as pd
22
from rdflib import RDF, BNode, Graph
33

4+
from scaffold import context
45
from scaffold.bitstomach.signals import SIGNALS
56
from scaffold.utils.namespace import PSDO, SLOWMO
67

@@ -31,18 +32,14 @@ def extract_signals(perf_df: pd.DataFrame) -> Graph:
3132
return g
3233

3334

34-
def prepare(performance_df, staff_number, performance_month):
35-
performance_df = performance_df[
36-
performance_df["staff_number"] == staff_number
37-
].reset_index(drop=True)
38-
39-
if not performance_month:
40-
performance_month = performance_df["month"].max()
41-
performance_df.attrs["staff_number"] = int(performance_df.at[0, "staff_number"])
35+
def prepare():
36+
performance_df = context.performance_df
4237

4338
performance_df["goal_comparator_content"] = performance_df["MPOG_goal"]
4439

45-
performance_df = performance_df[performance_df["month"] <= performance_month]
40+
performance_df = performance_df[
41+
performance_df["month"] <= context.performance_month
42+
]
4643

4744
performance_df["valid"] = performance_df["denominator"] >= 10
4845

@@ -53,7 +50,10 @@ def prepare(performance_df, staff_number, performance_month):
5350
performance_df.attrs["measures"] = performance_df["measure"].unique()
5451

5552
performance_df.attrs["valid_measures"] = performance_df[
56-
((performance_df["month"] == performance_month) & performance_df["valid"])
53+
(
54+
(performance_df["month"] == context.performance_month)
55+
& performance_df["valid"]
56+
)
5757
]["measure"]
5858

59-
return performance_df, performance_month
59+
return performance_df

scaffold/candidate_pudding/candidate_pudding.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from rdflib import RDF, BNode, Graph, Literal, URIRef
44
from rdflib.resource import Resource
55

6+
from scaffold import context
67
from scaffold.bitstomach.signals import Signal
78
from scaffold.utils.namespace import CPO, IAO, PSDO, RO, SCHEMA, SLOWMO
89

@@ -110,16 +111,20 @@ def add_convenience_properties(candidate: Resource):
110111
return candidate
111112

112113

113-
def create_candidates(graph: Graph):
114+
def create_candidates():
114115
# measures = graph[: RDF.type : PSDO.performance_measure_content]
115116
# How do we get the measures for all MI?
116117
measures: set[BNode] = set(
117-
graph.objects(None, PSDO.motivating_information / SLOWMO.RegardingMeasure)
118+
context.subject_graph.objects(
119+
None, PSDO.motivating_information / SLOWMO.RegardingMeasure
120+
)
118121
)
119122
for measure in measures:
120-
measure_resource = graph.resource(measure)
121-
for template in graph[: RDF.type : PERFORMANCE_SUMMARY_DISPLAY_TEMPLATE]:
122-
template_resource = graph.resource(template)
123+
measure_resource = context.subject_graph.resource(measure)
124+
for template in context.subject_graph[
125+
: RDF.type : PERFORMANCE_SUMMARY_DISPLAY_TEMPLATE
126+
]:
127+
template_resource = context.subject_graph.resource(template)
123128
candidate = create_candidate(measure_resource, template_resource)
124129
if not candidate:
125130
continue

scaffold/cli.py

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,18 @@
44
from typing import Annotated
55

66
import orjson
7-
import pandas as pd
87
import typer
98
from fastapi import HTTPException
109
from loguru import logger
1110

12-
from scaffold import context
11+
from scaffold import context, startup
1312
from scaffold.pipeline import pipeline
14-
from scaffold.startup import startup
1513
from scaffold.utils.utils import (
1614
add_candidates,
1715
add_response,
1816
analyse_candidates,
1917
analyse_responses,
2018
extract_number,
21-
get_performance_month,
2219
)
2320

2421
cli = typer.Typer(no_args_is_help=True)
@@ -41,7 +38,7 @@ def batch(
4138
),
4239
] = False,
4340
) -> None:
44-
startup()
41+
startup.startup()
4542

4643
if file_path.is_file() and file_path.suffix == ".json":
4744
input_files = [file_path]
@@ -60,18 +57,10 @@ def batch(
6057
try:
6158
input_data = orjson.loads(input_file.read_bytes())
6259

63-
performance_month = get_performance_month(input_data)
64-
performance_df = pd.DataFrame(
65-
input_data["Performance_data"][1:],
66-
columns=input_data["Performance_data"][0],
67-
)
68-
context.create(input_data, performance_df.at[0, "staff_number"])
6960
try:
70-
full_message = pipeline(
71-
performance_df,
72-
performance_df.at[0, "staff_number"],
73-
performance_month,
74-
)
61+
context.from_req(input_data)
62+
63+
full_message = pipeline()
7564
full_message["message_instance_id"] = input_data["message_instance_id"]
7665
full_message["performance_data"] = input_data["Performance_data"]
7766
except HTTPException as e:
@@ -82,9 +71,8 @@ def batch(
8271
directory = input_file.parent / "messages"
8372
os.makedirs(directory, exist_ok=True)
8473

85-
performance_month = input_data.get("performance_month", "unknown_month")
8674
new_filename = (
87-
f"{input_file.stem} - message for {performance_month}.json"
75+
f"{input_file.stem} - message for {context.performance_month}.json"
8876
)
8977
output_path = directory / new_filename
9078

@@ -103,7 +91,7 @@ def batch(
10391

10492
add_response(full_message)
10593
if not stats_only:
106-
add_candidates(full_message, input_data["performance_month"])
94+
add_candidates(full_message)
10795

10896
logger.info(f"Total files scanned: {len(input_files[:max_files])}")
10997
logger.info(f"Successful: {success_count}, Failed: {failure_count}")
@@ -132,43 +120,48 @@ def batch_csv(
132120
),
133121
] = False,
134122
):
135-
startup()
123+
startup.startup(performance_data_path)
136124

137-
performance_data = pd.read_csv(performance_data_path, parse_dates=["month"])
138125
success_count = 0
139126
failure_count = 0
140-
for provider_id in (
141-
performance_data["staff_number"].drop_duplicates().head(max_files)
127+
for staff_number in (
128+
startup.performance_data["staff_number"].drop_duplicates().head(max_files)
142129
):
143130
try:
144-
context.create({}, provider_id)
145-
result = pipeline(performance_data, provider_id, performance_month)
131+
context.from_global(staff_number, performance_month)
132+
try:
133+
full_message = pipeline()
134+
# full_message["message_instance_id"] = input_data["message_instance_id"]
135+
full_message["performance_data"] = performance_month
136+
except Exception as e:
137+
# e.detail["message_instance_id"] = input_data["message_instance_id"]
138+
raise e
146139
if not stats_only:
147140
directory = performance_data_path.parent / "messages"
148141
os.makedirs(directory, exist_ok=True)
149142

150143
new_filename = (
151-
f"Provider_{provider_id} - message for {performance_month}.json"
144+
f"Provider_{staff_number} - message for {performance_month}.json"
152145
)
153146
output_path = directory / new_filename
154147

155148
output_path.write_bytes(
156-
orjson.dumps(result, option=orjson.OPT_INDENT_2)
149+
orjson.dumps(full_message, option=orjson.OPT_INDENT_2)
157150
)
158151
logger.info(f"Message created at {output_path}")
159152
else:
160-
logger.info(f"✔ Would process: Provider_{provider_id}")
153+
logger.info(f"✔ Would process: Provider_{staff_number}")
161154

162155
success_count += 1
163156

164157
except Exception as e:
165-
logger.error(f"✘ Failed to process Provider_{provider_id}: {e}")
158+
logger.error(f"✘ Failed to process Provider_{staff_number}: {e}")
166159
failure_count += 1
167-
result = e.detail
160+
full_message = e.detail
168161

169-
add_response(result)
162+
add_response(full_message)
170163
if not stats_only:
171-
add_candidates(result, performance_month)
164+
add_candidates(full_message)
172165

173166
logger.info(f"Successful: {success_count}, Failed: {failure_count}")
174167
analyse_responses()

scaffold/context.py

Lines changed: 68 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,88 @@
1+
import pandas as pd
2+
from rdflib import Graph
3+
14
from scaffold import startup
5+
from scaffold.utils.utils import get_performance_month
26

37
preferences_dict = {}
48
history_dict = {}
9+
staff_number = 0
10+
performance_month = ""
11+
performance_df: pd.DataFrame
12+
subject_graph = Graph()
513

614

7-
def create(req_info, staff_number):
8-
global preferences_dict, history_dict
15+
def from_req(req_info):
16+
global \
17+
preferences_dict, \
18+
history_dict, \
19+
staff_number, \
20+
performance_month, \
21+
performance_df, \
22+
subject_graph
23+
24+
try:
25+
performance_df = pd.DataFrame(
26+
req_info["Performance_data"][1:], columns=req_info["Performance_data"][0]
27+
)
28+
except Exception:
29+
pass
30+
31+
performance_month = get_performance_month(req_info, performance_df["month"].max())
32+
33+
staff_number = int(performance_df.at[0, "staff_number"])
934

10-
history_dict = {}
1135
preferences_dict = {}
36+
try:
37+
preferences_dict = set_preferences(req_info.get("Preferences", {}))
38+
except Exception:
39+
return set_preferences({})
40+
41+
history_dict = {}
42+
try:
43+
history_dict = req_info.get("History", {})
44+
except Exception:
45+
pass
46+
47+
subject_graph = Graph()
48+
subject_graph += startup.base_graph
1249

50+
51+
def from_global(staff_num, perf_month):
52+
global \
53+
preferences_dict, \
54+
history_dict, \
55+
staff_number, \
56+
performance_month, \
57+
performance_df, \
58+
subject_graph
59+
60+
staff_number = int(staff_num)
61+
history_dict = {}
62+
preferences_dict = {}
63+
performance_month = perf_month
1364
try:
14-
if req_info.get("Preferences", {}):
15-
preferences_dict = set_preferences(req_info.get("Preferences", {}))
16-
else:
17-
p = startup.preferences.loc[staff_number, "preferences"]
18-
preferences_dict = set_preferences(p)
65+
p = startup.preferences.loc[staff_number, "preferences"]
66+
preferences_dict = set_preferences(p)
1967
except Exception:
2068
return set_preferences({})
2169

2270
try:
23-
if req_info.get("History", {}):
24-
history_dict = req_info.get("History", {})
25-
else:
26-
staff_data = startup.history[
27-
startup.history["staff_number"] == staff_number
28-
]
29-
history_dict = staff_data.set_index("month")["history"].to_dict()
71+
staff_data = startup.history[startup.history["staff_number"] == staff_number]
72+
history_dict = staff_data.set_index("month")["history"].to_dict()
73+
except Exception:
74+
pass
75+
76+
try:
77+
performance_df = startup.performance_data[
78+
startup.performance_data["staff_number"] == staff_number
79+
].reset_index(drop=True)
3080
except Exception:
3181
pass
3282

83+
subject_graph = Graph()
84+
subject_graph += startup.base_graph
85+
3386

3487
def get_preferences():
3588
global preferences_dict

0 commit comments

Comments
 (0)