-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis_data.py
More file actions
1327 lines (1044 loc) · 41.2 KB
/
Copy pathanalysis_data.py
File metadata and controls
1327 lines (1044 loc) · 41.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import google.generativeai as genai
import json
import pandas as pd
DISABILITY_TYPES = [
"permanent",
"temporary",
"recurring",
"situational",
"personal_trait_or_sensory_preference"
]
def build_prompt(csv_text):
return f"""
You are analyzing user constraints related to disabilities, limitations, or situational challenges.
We define four key layers:
1. personal_trait: Stable characteristics of the user that influence susceptibility.
Examples: noise sensitivity, clutter sensitivity, weak grip, attentional susceptibility, shorter height.
Traits themselves do not count as disabilities and should not receive a temporal_type label.
2. environmental_trigger: External or situational conditions that interact with the user's trait.
Examples: loud lab, visually cluttered interface, exposed cords, carrying multiple items, high upper cabinets, handle-less cabinets.
Triggers may be permanent or intermittent, but **temporal_type labels are not assigned to triggers**. Only the resulting disability gets a temporal_type.
3. experienced_disability: Actual limitations or problems the user experiences when the trait interacts with the trigger.
Examples: distraction, cognitive overload, dexterity limitation, risk of injury, discomfort, difficulty reaching, difficulty opening.
This is the **only layer that receives a temporal_type**.
4. temporal_type: Time profile of the experienced_disability. Must be one of:
- permanent
- temporary
- recurring
- situational
Rules for assigning temporal_type:
- **Permanent**: The disability always occurs whenever the relevant activity or environment is present, due to a stable trait or fixed environmental feature.
Examples:
1. Short stature → difficulty reaching high cabinets → permanent
2. Long-term vision impairment → difficulty reading → permanent
- **Recurring**: The disability occurs repeatedly when performing the task or interacting with the environment, but is not strictly unavoidable every single time.
Examples:
1. Handle-less cabinets → dexterity limitation → recurring
2. Near-sightedness without glasses → difficulty understanding speech → recurring
- **Situational**: The disability occurs only under certain conditions or moments, even if the hazard is always present.
Examples:
1. Exposed cord → risk of injury → situational
2. Slick marble stairs → risk of injury → situational
- **Temporary**: The disability occurs for a limited period of time.
Example: Post-surgery vision loss → inability to perform daily activities → temporary
Task:
For each PID and its constraint in the CSV:
1. Extract the exact text of the constraint (as "text").
2. Identify the personal_trait(s) (if any).
3. Identify the environmental_trigger(s) (if any).
4. Identify the experienced_disability(s).
5. Assign the correct temporal_type to the experienced_disability based on the rules above.
6. Provide a 1-2 sentence brief_summary in plain language describing how the trait and trigger produce the experienced disability.
Return a JSON list. Each item must follow this schema:
[
{{
"PID": "string",
"text": "string",
"personal_trait": ["string"],
"environmental_trigger": ["string"],
"experienced_disability": ["string"],
"temporal_type": "string",
"brief_summary": "string"
}}
]
Important:
- One PID may result in multiple entries if multiple distinct constraints exist.
- Do not merge different constraints into one entry.
- Only extract information directly from the "Constraint" field in the CSV.
- Keep the text exactly as written in the CSV.
Here is the CSV data:
{csv_text}
"""
def load_csv_as_text(csv_path):
df = pd.read_csv(csv_path)
# Only keep required columns
df = df[["PID", "Constraint"]]
# Drop rows with missing values (optional but safer)
df = df.dropna(subset=["PID", "Constraint"])
# Convert to structured text format for LLM
formatted_text = ""
for _, row in df.iterrows():
formatted_text += f"PID: {row['PID']}\n"
formatted_text += f"Constraint: {row['Constraint']}\n\n"
return formatted_text
def call_gemini(prompt):
try:
with open("api_keys/gemini_key.txt", "r") as f:
gemini_key = f.read().strip()
genai.configure(api_key=gemini_key)
print("Gemini API configured.")
except Exception:
print("Error: Gemini API key not found.")
exit()
model = genai.GenerativeModel(model_name="gemini-2.5-pro")
response = model.generate_content(
prompt,
generation_config=genai.types.GenerationConfig(
response_mime_type="application/json"
),
)
return json.loads(response.text)
def plot_type_count():
import json
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Load JSON file
with open("classified_constraints.json", "r") as f:
data = json.load(f)
# Convert to DataFrame
df = pd.DataFrame(data)
# Flatten the "primary_types" list column
df_exploded = df.explode("primary_types")
df_exploded["primary_types"] = df_exploded["primary_types"].replace(
{"personal_trait_or_sensory_preference": "personal_trait"}
)
# Count occurrences
counts = df_exploded["primary_types"].value_counts().reset_index()
counts.columns = ["primary_types", "count"]
# Print numbers in the terminal
print("Counts of each primary type:")
print(counts.to_string(index=False))
# Set aesthetic style
sns.set(style="whitegrid", context="talk")
# Create bar plot
plt.figure(figsize=(8, 5))
ax = sns.barplot(
data=counts,
x="primary_types",
y="count"
)
# Add counts on top of bars
for p in ax.patches:
height = p.get_height()
ax.text(
p.get_x() + p.get_width() / 2, # x position
height + 0.1, # y position slightly above bar
int(height), # text to display
ha="center", va="bottom", fontsize=12
)
plt.xticks(rotation=30, ha="right")
plt.xlabel("Primary Types")
plt.ylabel("Count")
plt.title("Distribution of Primary Disability Types")
plt.tight_layout()
# Save as high-resolution PNG
plt.savefig("primary_types_distribution.pdf", dpi=300, bbox_inches="tight")
plt.show()
import json
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
def plot_temporal_type_distribution(json_path="classified_constraints.json", save_path="figures/temporal_type_distribution.pdf"):
# 1️⃣ Load JSON
with open(json_path, "r") as f:
data = json.load(f)
# 2️⃣ Convert to DataFrame
df = pd.DataFrame(data)
# 3️⃣ Explode experienced_disability list so each row is one disability
df_exploded = df.explode("experienced_disability")
# 4️⃣ Count occurrences of each temporal_type
counts = df_exploded["temporal_type"].value_counts().reset_index()
counts.columns = ["temporal_type", "count"]
print("Counts of experienced disabilities by temporal type:")
print(counts.to_string(index=False))
# 5️⃣ Set plot style
sns.set(style="whitegrid", context="talk")
# 6️⃣ Create bar plot
plt.figure(figsize=(7, 5))
ax = sns.barplot(data=counts, x="temporal_type", y="count", palette="Set2")
# 7️⃣ Add counts on top of bars
for p in ax.patches:
height = p.get_height()
ax.text(
p.get_x() + p.get_width() / 2,
height + 0.1,
int(height),
ha="center", va="bottom", fontsize=12
)
plt.xlabel("Temporal Type")
plt.ylabel("Number of Experienced Disabilities")
plt.title("Distribution of Experienced Disabilities by Temporal Type")
plt.tight_layout()
# 8️⃣ Save high-res PDF
plt.savefig(save_path, dpi=300, bbox_inches="tight")
plt.show()
print(f"Plot saved to {save_path}")
def plot_traits_vs_temporal(data, filename="figures/traits_vs_temporal.pdf"):
import matplotlib.pyplot as plt
import pandas as pd
# Flatten personal traits per record
rows = []
for entry in data:
traits = entry.get("personal_trait", [])
if not traits:
traits = ["No trait"]
for trait in traits:
rows.append({"personal_trait": trait, "temporal_type": entry["temporal_type"]})
df = pd.DataFrame(rows)
# Count occurrences
count_df = df.groupby(["personal_trait", "temporal_type"]).size().unstack(fill_value=0)
# Plot stacked bar chart
ax = count_df.plot(kind="bar", stacked=True, figsize=(12,6))
ax.set_ylabel("Count of experiences")
ax.set_xlabel("Personal Trait")
ax.set_title("Personal Traits vs. Temporal Type of Disability")
plt.xticks(rotation=45, ha='right')
plt.legend(title="Temporal Type")
plt.tight_layout()
# Save as PDF
plt.savefig(filename, format="pdf")
plt.show()
def plot_triggers_vs_disability(data, filename="figures/triggers_vs_disability.pdf"):
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
# Flatten triggers and disabilities
rows = []
for entry in data:
triggers = entry.get("environmental_trigger", [])
disabilities = entry.get("experienced_disability", [])
if not triggers:
triggers = ["No trigger"]
if not disabilities:
disabilities = ["No disability"]
for trig in triggers:
for dis in disabilities:
rows.append({"trigger": trig, "disability": dis})
df = pd.DataFrame(rows)
# Create a pivot table
pivot_df = df.pivot_table(index="disability", columns="trigger", aggfunc=len, fill_value=0)
# Plot heatmap
plt.figure(figsize=(12,8))
sns.heatmap(pivot_df, annot=True, fmt="d", cmap="YlOrRd")
plt.title("Environmental Triggers vs. Experienced Disability")
plt.ylabel("Disability")
plt.xlabel("Environmental Trigger")
plt.xticks(rotation=45, ha='right')
plt.tight_layout()
# Save as PDF
plt.savefig(filename, format="pdf")
plt.show()
def process_csv():
csv_path = "/Users/kwon/owa_dataset/owa_participants_18.csv"
# 1. Load CSV
csv_text = load_csv_as_text(csv_path)
# 2. Build Prompt
prompt = build_prompt(csv_text)
# 3. Call Gemini
results = call_gemini(prompt)
# 4. Save JSON
with open("classified_constraints.json", "w") as f:
json.dump(results, f, indent=2)
print("Saved classified_constraints.json")
import os
import cv2
def extract_middle_frame(video_path, output_image_path):
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print(f"Error opening video: {video_path}")
return False
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
middle_frame_idx = frame_count // 2
cap.set(cv2.CAP_PROP_POS_FRAMES, middle_frame_idx)
success, frame = cap.read()
if success:
cv2.imwrite(output_image_path, frame)
cap.release()
return True
else:
print(f"Failed to extract frame from: {video_path}")
cap.release()
return False
def extract_frames_for_participants(base_video_folder, output_frame_folder, pid_list):
os.makedirs(output_frame_folder, exist_ok=True)
for pid in pid_list:
participant_folder = os.path.join(base_video_folder, f"P{pid}")
if not os.path.exists(participant_folder):
print(f"Folder not found: {participant_folder}")
continue
for filename in os.listdir(participant_folder):
if filename.endswith(".mp4"):
video_path = os.path.join(participant_folder, filename)
frame_filename = filename.replace(".mp4", ".jpg")
output_path = os.path.join(output_frame_folder, frame_filename)
if os.path.exists(output_path):
continue
extract_middle_frame(video_path, output_path)
print("Frame extraction completed.")
def build_scene_prompt(image_base64):
return f"""
You are analyzing an egocentric image from a user's daily life.
Describe the scene category in natural language.
Be concise but specific.
Examples:
- small apartment kitchen
- shared office workspace
- cluttered bedroom
- narrow hallway
- outdoor residential street
Return JSON format:
{{
"scene_category": "...",
"brief_description": "1-2 sentence description of what is happening in this space."
}}
Image (base64):
{image_base64}
"""
import base64
def image_to_base64(image_path):
with open(image_path, "rb") as img_file:
return base64.b64encode(img_file.read()).decode("utf-8")
def classify_scene_with_gemini(image_path):
image_base64 = image_to_base64(image_path)
prompt = build_scene_prompt(image_base64)
try:
result = call_gemini(prompt)
return result
except Exception as e:
print(f"Error processing {image_path}: {e}")
return None
def classify_all_frames(frame_folder, output_json_path):
results = []
for filename in os.listdir(frame_folder):
if filename.endswith(".jpg"):
image_path = os.path.join(frame_folder, filename)
result = classify_scene_with_gemini(image_path)
if result:
results.append({
"video_id": filename.replace(".jpg", ""),
"scene_category": result.get("scene_category"),
"brief_description": result.get("brief_description")
})
print(f"Processed {filename}")
with open(output_json_path, "w") as f:
json.dump(results, f, indent=2)
print("Scene classification completed.")
def scene_analysis_pipeline():
# -----------------------------
# 1. Configuration
# -----------------------------
base_video_folder = "/Users/kwon/owa_dataset/encoded_videos"
output_frame_folder = "/Users/kwon/owa_dataset/extracted_frames"
output_json_path = "scene_classification_results.json"
# ✅ Replace with your 18 completed participants
pid_list = [
0, 1, 2, 3, 4, 6, 7, 8,
11, 12, 13, 14, 15,
18, 19, 20, 21, 24
]
# -----------------------------
# 2. Extract Frames
# -----------------------------
print("Extracting frames...")
extract_frames_for_participants(
base_video_folder,
output_frame_folder,
pid_list
)
# -----------------------------
# 3. Classify Scenes
# -----------------------------
print("Classifying scenes with Gemini...")
classify_all_frames(
output_frame_folder,
output_json_path
)
print("Pipeline completed successfully.")
import json
import re
def sort_scene_results(input_json_path, output_json_path):
with open(input_json_path, "r") as f:
data = json.load(f)
def extract_pid_and_vid(video_id):
"""
Extract numeric PID and video index from string like 'P12_3'
"""
match = re.match(r"P(\d+)_(\d+)", video_id)
if match:
pid = int(match.group(1))
vid = int(match.group(2))
return pid, vid
return float("inf"), float("inf")
# Sort by (PID, video_index)
sorted_data = sorted(
data,
key=lambda x: extract_pid_and_vid(x["video_id"])
)
with open(output_json_path, "w") as f:
json.dump(sorted_data, f, indent=2)
print("Sorted results saved to:", output_json_path)
def missing_videos():
import os
import json
import re
EXTRACTED_FRAMES_DIR = "/Users/kwon/owa_dataset/extracted_frames"
JSON_PATH = "scene_classification_results_sorted.json"
# Load existing JSON results
with open(JSON_PATH, "r") as f:
existing_data = json.load(f)
# Extract video_ids that already have results
processed_video_ids = set(item["video_id"] for item in existing_data)
# Get all frame files from extracted_frames
all_frames = [f for f in os.listdir(EXTRACTED_FRAMES_DIR) if f.endswith(".jpg") or f.endswith(".png")]
# Extract video_id from frame filenames (assuming format P{PID}_{VID}.jpg)
def frame_to_video_id(frame_name):
match = re.match(r"(P\d+_\d+)", frame_name)
return match.group(1) if match else None
all_video_ids = set(filter(None, (frame_to_video_id(f) for f in all_frames)))
# Find missing videos
missing_videos = sorted(all_video_ids - processed_video_ids)
print("Missing videos:", missing_videos)
def map_scene_category(text):
"""
Map free-text scene descriptions to discrete categories.
"""
text = text.lower()
if any(k in text for k in ["kitchen", "kitchen counter", "kitchen table", "refrigerator"]):
return "home_kitchen"
elif any(k in text for k in ["living room", "sofa", "carpeted living room", "apartment living room"]):
return "home_living_room"
elif any(k in text for k in ["bedroom", "cluttered bedroom"]):
return "home_bedroom"
elif any(k in text for k in ["bathroom", "small bathroom", "residential bathroom"]):
return "home_bathroom"
elif any(k in text for k in ["home office", "personal office workspace", "home office desk", "home office workspace"]):
return "home_office_workspace"
elif any(k in text for k in ["office workspace", "shared office workspace", "personal workspace"]):
return "office_workspace"
elif any(k in text for k in ["restaurant", "dining area", "casual restaurant dining", "restaurant dining area", "indoor dining area"]):
return "restaurant_dining"
elif any(k in text for k in ["store", "mall", "big-box retail store"]):
return "retail_store"
elif any(k in text for k in ["park", "outdoor park", "outdoor park by a lake"]):
return "park"
elif any(k in text for k in ["car", "interior of a car", "inside a car", "car interior"]):
return "car_interior"
else:
return "other"
def mapping():
import json
# Load the raw scene data
with open("scene_classification_results_sorted.json", "r") as f:
data = json.load(f)
# Map each scene to a discrete category
for item in data:
item["discrete_category"] = map_scene_category(item["scene_category"])
# Save the mapped data
with open("scene_classification_discrete.json", "w") as f:
json.dump(data, f, indent=2)
import pandas as pd
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
import os
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
def demographics(csv_path, pid_list, save_dir="figures"):
os.makedirs(save_dir, exist_ok=True)
# 1️⃣ Load CSV
df = pd.read_csv(csv_path)
# Ensure numeric age
df["age"] = pd.to_numeric(df["age"], errors="coerce")
# 2️⃣ Filter by PID
df = df[df["PID"].isin(pid_list)].copy()
print(f"Total participants included: {len(df)}\n")
# -------------------------
# AGE STATISTICS
# -------------------------
age_mean = df["age"].mean()
age_sd = df["age"].std()
age_min = df["age"].min()
age_max = df["age"].max()
print("Age Statistics:")
print(f"Mean: {age_mean:.2f}, SD: {age_sd:.2f}, Min: {age_min}, Max: {age_max}\n")
# Categorize Age
def categorize_age(age):
if 20 <= age < 30:
return "20s"
elif 30 <= age < 40:
return "30s"
else:
return "Other"
df["age_group"] = df["age"].apply(categorize_age)
# -------------------------
# GENDER STATISTICS
# -------------------------
df["gender_group"] = df["gender"].astype(str).str.strip().str.capitalize()
gender_counts = df["gender_group"].value_counts()
print("Gender Counts:")
for g, c in gender_counts.items():
print(f"{g}: {c}")
print()
# -------------------------
# OCCUPATION STATISTICS
# -------------------------
if "occupation" in df.columns:
df["occupation_group"] = df["occupation"].astype(str).str.strip().str.capitalize()
occupation_counts = df["occupation_group"].value_counts()
print("Occupation Counts:")
for o, c in occupation_counts.items():
print(f"{o}: {c}")
print()
# -------------------------
# AGE DISTRIBUTION PLOT
# -------------------------
age_counts = df["age_group"].value_counts().reset_index()
age_counts.columns = ["age_group", "count"]
age_counts = age_counts.sort_values("age_group")
sns.set(style="whitegrid", context="talk")
plt.figure(figsize=(6, 5))
ax = sns.barplot(data=age_counts, x="age_group", y="count", palette="Set2")
for p in ax.patches:
ax.text(p.get_x() + p.get_width() / 2, p.get_height() + 0.1, int(p.get_height()),
ha="center", va="bottom", fontsize=12)
plt.xlabel("Age Group")
plt.ylabel("Number of Participants")
plt.title("Age Distribution")
plt.tight_layout()
age_path = os.path.join(save_dir, "age_distribution.pdf")
plt.savefig(age_path, dpi=300, bbox_inches="tight")
plt.show()
plt.close()
print(f"Age plot saved to {age_path}")
# -------------------------
# GENDER DISTRIBUTION PLOT
# -------------------------
gender_counts_df = gender_counts.reset_index()
gender_counts_df.columns = ["gender_group", "count"]
gender_counts_df = gender_counts_df.sort_values("gender_group")
plt.figure(figsize=(6, 5))
ax = sns.barplot(data=gender_counts_df, x="gender_group", y="count", palette="Set2")
for p in ax.patches:
ax.text(p.get_x() + p.get_width() / 2, p.get_height() + 0.1, int(p.get_height()),
ha="center", va="bottom", fontsize=12)
plt.xlabel("Gender")
plt.ylabel("Number of Participants")
plt.title("Gender Distribution")
plt.tight_layout()
gender_path = os.path.join(save_dir, "gender_distribution.pdf")
plt.savefig(gender_path, dpi=300, bbox_inches="tight")
plt.show()
plt.close()
print(f"Gender plot saved to {gender_path}")
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
def plot_occupation_distribution(csv_path,
pid_list,
save_dir="figures"):
os.makedirs(save_dir, exist_ok=True)
# 1️⃣ Load CSV
df = pd.read_csv(csv_path)
# 2️⃣ Filter by PID
df = df[df["PID"].isin(pid_list)].copy()
print(f"Total participants included: {len(df)}")
# 3️⃣ Clean occupation column
df["occupation_clean"] = (
df["occupation"]
.astype(str)
.str.strip()
)
# Optional: normalize wording (example)
df["occupation_clean"] = df["occupation_clean"].replace({
"Unemployed": "Not currently employed"
})
# 4️⃣ Count occupations
counts = (
df["occupation_clean"]
.value_counts()
.reset_index()
)
counts.columns = ["occupation", "count"]
print("Occupation distribution:")
print(counts.to_string(index=False))
# 5️⃣ Plot style
sns.set(style="whitegrid", context="talk")
plt.figure(figsize=(8, 5))
ax = sns.barplot(
data=counts,
x="occupation",
y="count",
palette="Set2"
)
# 6️⃣ Add counts on top
for p in ax.patches:
height = p.get_height()
ax.text(
p.get_x() + p.get_width() / 2,
height + 0.1,
int(height),
ha="center",
va="bottom",
fontsize=12
)
plt.xlabel("Occupation")
plt.ylabel("Number of Participants")
plt.title("Occupation Distribution")
plt.xticks(rotation=30, ha="right")
plt.tight_layout()
save_path = os.path.join(save_dir, "occupation_distribution.pdf")
plt.savefig(save_path, dpi=300, bbox_inches="tight")
plt.show()
plt.close()
print(f"Plot saved to {save_path}")
import json
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from collections import Counter
import string
def normalize_text(text):
"""Lowercase, strip punctuation, strip spaces."""
text = text.lower().strip()
text = text.translate(str.maketrans("", "", string.punctuation))
return text
import json
from collections import Counter
import matplotlib.pyplot as plt
import json
from collections import Counter
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
def plot_top_environmental_triggers(json_path, top_n=5):
import json
from collections import Counter
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
# --- Step 1: Load JSON data ---
with open(json_path, "r") as f:
data = json.load(f)
# --- Step 2: Define the mapping ---
trigger_mapping = {
"carrying groceries": "hands occupied",
"full hands": "hands occupied",
"occupied hands": "hands occupied",
"carrying items": "hands occupied",
"carrying multiple items": "hands occupied",
"hands occupied while cooking": "hands occupied",
"one hand occupied": "hands occupied",
"door": "door/cabinet",
"locked door": "door/cabinet",
"cat rail": "door/cabinet",
"cabinet door": "door/cabinet",
"upper kitchen cabinets": "door/cabinet",
"handle-less cabinets": "door/cabinet",
"awkwardly opening door": "door/cabinet",
"stairs": "stairs/steps",
"icy stairway": "stairs/steps",
"stairs with side bar": "stairs/steps",
"slick marble stairs": "stairs/steps",
"small and short sink head": "kitchen ergonomics",
"large kitchen items": "kitchen ergonomics",
"low kitchen sink": "kitchen ergonomics",
"angled table/monitor": "workspace ergonomics",
"non-ergonomic chair": "workspace ergonomics",
"lack of arm support": "workspace ergonomics",
"darker areas": "low visibility",
"insufficient sunlight": "low visibility",
"flickering lights": "light sensitivity",
"post-shower environment": "low visibility",
"ambient sounds": "noisy environment",
"potential to make noise": "noisy environment",
"clashing dishes": "noisy environment",
"sound of the washer": "noisy environment",
"noises from outside room": "noisy environment",
"wet hands": "slippery/unsafe surface",
"exposed cord": "slippery/unsafe surface",
"electrical drain switch": "slippery/unsafe surface",
"cold winter conditions": "cold environment",
"tightly sealed bottles or jars": "difficult to grip",
"pump dispenser": "difficult to grip",
"bottle cap": "difficult to grip",
"scissors": "difficult to grip",
"gas stove knob": "difficult to grip",
"low, floor-level shelves": "reaching items",
"high shelves": "reaching items",
"small kitchen space": "tight space",
"full sized couch": "tight space",
"washing up": "task difficulty",
"daily activities": "task difficulty",
"typing": "task difficulty",
"fast-paced classes": "task difficulty",
"holding items": "task difficulty",
"bending the knees": "task difficulty",
"objects on the floor": "task difficulty",
"paper towel dispenser": "task difficulty",
"torn backpack drink holder": "carrying difficulty"
}
# --- Step 3: Normalize triggers ---
normalized_triggers = []
for entry in data:
for trigger in entry.get("environmental_trigger", []):
norm_trigger = trigger_mapping.get(trigger.strip().lower(), trigger.strip())
normalized_triggers.append(norm_trigger)
# --- Step 4: Count occurrences ---
trigger_counts = Counter(normalized_triggers)
top_triggers = trigger_counts.most_common(top_n)
# --- Step 5: Prepare DataFrame for Seaborn ---
df = pd.DataFrame(top_triggers, columns=["Trigger", "Count"])
# --- Step 6: Print top triggers in terminal ---
print("Top Environmental Triggers:")
print(f"{'Environmental Trigger':<30} Count")
for trigger, count in top_triggers:
print(f"{trigger:<30} {count}")
# --- Step 7: Plot with Seaborn using disability palette ---
plt.figure(figsize=(8,6))
sns.barplot(
data=df,
y="Trigger",
x="Count",
palette=sns.color_palette("pastel", len(df))
)
# --- Step 8: Add counts on bars ---
for index, row in df.iterrows():
plt.text(row["Count"] + 0.1, index, str(row["Count"]), va='center')
plt.xlabel("Count")
plt.ylabel("Environmental Trigger")
plt.title(f"Top {top_n} Environmental Triggers")
plt.tight_layout()
# --- Step 9: Save PDF ---
os.makedirs("figures", exist_ok=True)
plt.savefig("figures/top_environmental_triggers.pdf")
plt.show()
import json
from collections import Counter
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import os
def plot_top_disabilities(json_path="classified_constraints.json", top_n=5):
# Ensure figures folder exists
os.makedirs("figures", exist_ok=True)
# Load JSON
with open(json_path, "r") as f:
data = json.load(f)
# Mapping strategy for normalization
disability_mapping = {
"difficulty opening": "difficulty manipulating objects",
"difficulty opening door": "difficulty manipulating objects",
"difficulty opening cabinet": "difficulty manipulating objects",
"difficulty opening bottle": "difficulty manipulating objects",
"difficulty tearing paper towel": "difficulty manipulating objects",
"inability to open gate": "difficulty manipulating objects",
"pain": "physical/mental discomfort",
"low mood": "physical/mental discomfort",
"discomfort": "physical/mental discomfort",
"restlessness": "physical/mental discomfort",
"fatigue": "physical/mental discomfort",
"difficulty navigating stairs": "mobility limitation",
"difficulty navigating": "mobility limitation",
"difficulty accessing items": "mobility limitation",
"difficulty bending": "mobility limitation",
"difficulty picking things up": "mobility limitation",
"difficulty walking": "mobility limitation",
"reduced mobility": "mobility limitation",
"dexterity limitation": "dexterity limitation",
"difficulty holding things": "dexterity limitation",
"risk of injury": "safety risk",
"inability to use safety feature": "safety risk",
"difficulty multitasking": "task performance challenge",
"difficulty finding things": "task performance challenge",
"difficulty understanding speech": "communication challenge",
"sleep disturbance": "sleep disruption",
"low vision": "vision limitation",
"difficulty seeing": "vision limitation",
"aversion": "emotional strain",
"stress": "emotional strain",
"anxiety": "emotional strain"
}
# Flatten all experienced_disability and map
all_disabilities = []
for entry in data:
for dis in entry["experienced_disability"]:
mapped = disability_mapping.get(dis, dis) # fallback to original
all_disabilities.append(mapped)
# Count top N disabilities
top_disabilities = Counter(all_disabilities).most_common(top_n)
df_top = pd.DataFrame(top_disabilities, columns=["Experienced Disability", "Count"])