Skip to content

Commit 7c6d023

Browse files
committed
feat:add osm ref to results (wip)
1 parent 087a17b commit 7c6d023

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

mapswipe_workers/mapswipe_workers/firebase_to_postgres/transfer_results.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import csv
22
import io
3+
import json
34
from typing import List, Tuple
45

56
import dateutil.parser
@@ -269,6 +270,10 @@ def results_to_file(
269270

270271
if type(result_data["results"]) is dict:
271272
for taskId, result in result_data["results"].items():
273+
274+
ref_data = result_data.get("ref", {}).get(taskId, {})
275+
ref_json = json.dumps(ref_data) if ref_data else None
276+
272277
if result_type == "geometry":
273278
result = geojson.dumps(geojson.GeometryCollection(result))
274279
w.writerow(
@@ -283,6 +288,7 @@ def results_to_file(
283288
result,
284289
app_version,
285290
client_type,
291+
ref_json,
286292
]
287293
)
288294
elif type(result_data["results"]) is list:
@@ -292,6 +298,10 @@ def results_to_file(
292298
# if first key (list index) is 5
293299
# list indicies 0-4 will have value None
294300
for taskId, result in enumerate(result_data["results"]):
301+
302+
ref_data = result_data.get("ref", {}).get(taskId, {})
303+
ref_json = json.dumps(ref_data) if ref_data else None
304+
295305
if result is None:
296306
continue
297307
else:
@@ -309,6 +319,7 @@ def results_to_file(
309319
result,
310320
app_version,
311321
client_type,
322+
ref_json,
312323
]
313324
)
314325
else:
@@ -369,6 +380,7 @@ def save_results_to_postgres(
369380
"result",
370381
"app_version",
371382
"client_type",
383+
"ref",
372384
]
373385
p_con.copy_from(results_file, result_temp_table, columns)
374386
results_file.close()
@@ -439,7 +451,8 @@ def save_results_to_postgres(
439451
SELECT
440452
ms.mapping_session_id,
441453
r.task_id,
442-
{result_sql}
454+
{result_sql},
455+
r.ref
443456
FROM {result_temp_table} r
444457
JOIN mapping_sessions ms ON
445458
ms.project_id = r.project_id

mapswipe_workers/mapswipe_workers/generate_stats/project_stats.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def get_results(
9898
Parse timestamp as datetime object and add attribute "day" for each result.
9999
Return None if there are no results for this project.
100100
Otherwise, return dataframe.
101+
Include the 'ref' JSON field in integer results if it exists.
101102
102103
Parameters
103104
----------
@@ -108,7 +109,10 @@ def get_results(
108109
if result_table == "mapping_sessions_results_geometry":
109110
result_sql = "ST_AsGeoJSON(msr.result) as result"
110111
else:
111-
result_sql = "msr.result"
112+
result_sql = """
113+
(msr.result->>'result')::int as result,
114+
msr.result->'ref' as ref
115+
"""
112116

113117
sql_query = sql.SQL(
114118
f"""
@@ -504,6 +508,44 @@ def get_statistics_for_geometry_result_project(project_id: str):
504508
return project_stats_dict
505509

506510

511+
def unify_refs(ref_list):
512+
if not ref_list:
513+
return None
514+
first_ref = json.dumps(ref_list[0], sort_keys=True)
515+
for r in ref_list[1:]:
516+
if json.dumps(r, sort_keys=True) != first_ref:
517+
return "multiple"
518+
return ref_list[0]
519+
520+
521+
def add_ref_to_agg_results(
522+
results_df: pd.DataFrame, agg_results_df: pd.DataFrame
523+
) -> pd.DataFrame:
524+
"""
525+
Add a 'ref' column to agg_results_df.
526+
If all user refs for a task are identical, use that ref.
527+
If refs differ, set ref to 'multiple'.
528+
"""
529+
530+
# collect refs per task
531+
refs_per_task = (
532+
results_df.groupby(["project_id", "group_id", "task_id"])["ref"]
533+
.apply(list)
534+
.reset_index()
535+
)
536+
537+
refs_per_task["ref"] = refs_per_task["ref"].apply(unify_refs)
538+
539+
# merge into agg_results_df
540+
agg_results_df = agg_results_df.merge(
541+
refs_per_task[["project_id", "group_id", "task_id", "ref"]],
542+
on=["project_id", "group_id", "task_id"],
543+
how="left",
544+
)
545+
546+
return agg_results_df
547+
548+
507549
def get_statistics_for_integer_result_project(
508550
project_id: str, project_info: pd.Series, generate_hot_tm_geometries: bool
509551
) -> dict:
@@ -550,6 +592,9 @@ def get_statistics_for_integer_result_project(
550592
tasks_df,
551593
project_info["custom_options"],
552594
)
595+
596+
agg_results_df = add_ref_to_agg_results(results_df, agg_results_df)
597+
553598
agg_results_df.to_csv(agg_results_filename, index_label="idx")
554599

555600
geojson_functions.gzipped_csv_to_gzipped_geojson(

mapswipe_workers/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ flake8==3.8.3
66
geojson==3.0.1
77
mapswipe-workers==3.0
88
pandas==1.5.2
9+
numpy==1.26.4
910
pre-commit==2.9.2
1011
psycopg2-binary==2.9.3
1112
python-dateutil==2.8.1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE mapping_sessions_results
2+
ADD COLUMN ref jsonb;

0 commit comments

Comments
 (0)