-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUCF_SpatialVariables.py
98 lines (75 loc) · 4.34 KB
/
UCF_SpatialVariables.py
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
import pandas as pd
from datetime import timedelta
def assign_zone(player_x, player_z, zone_coords):
"""
Based on coordination of the player, finds which zone is the player in
:param player_x: data.x
:param player_z: data.z
:param zone_coords: coordinations of all zones in the map
:return: the zone number if the x and z are within the map zones and -1 if not
"""
for zone in zone_coords:
[zone_number, xtl, xbr, ztl, zbr] = zone
if (xbr <= player_x <= xtl) and (ztl <= player_z <= zbr):
return zone_number
return -1
def zone_visits_revisits(df, zones, experiment):
"""
Calculate percent zone visited and revisited for one trial stored in df and zones of interest
:param df: events related to one trial (or events of a specific time period in a trial)
:param zones: zones of interest
:param experiment: a postfix that will be added to the variable names
:return: percent zones visited and revisited in df
"""
zone_coords = zones[['Zone Number', 'Xcoords-TopLeft', 'XCoords-BotRight',
'Zcoords-TopLeft', 'ZCoords-BotRight']].values.tolist()
df['zone'] = df.apply(lambda row: assign_zone(row['data.x'], row['data.z'], zone_coords), axis=1)
df['zone'] = df['zone'].astype(int)
df = df.loc[df['zone'] != df['zone'].shift()]
zone_visits_count = pd.DataFrame(df['zone'].value_counts())
zone_visits_count = zone_visits_count.drop(-1)
zone_visits = zone_visits_count != 0
zone_revisits = zone_visits_count > 1
total_zones_count = len(zone_coords)
percent_zone_visited = (zone_visits.values.sum() / total_zones_count)
percent_zone_revisited = (zone_revisits.values.sum() / total_zones_count)
visit_revisit_percent_df = pd.DataFrame.from_records([[percent_zone_visited, percent_zone_revisited]],
columns=["percent_zone_visited_" + experiment,
"percent_zone_revisited_" + experiment])
return visit_revisit_percent_df
def building_spatial_variables(df, building):
"""
Calculated different spatial variables related to trials of one specific building
:param df: events related to trials
:param building: an object containing info about the building
:return: spatial variables
"""
building_df = df[df['trial_id'].isin(building.trials)]
building_zones = pd.read_csv(building.zones_file)
building_df.dropna(subset=['data.x', 'data.z'], inplace=True)
grouped = building_df.groupby('trial_id')
dfs = []
for ti, trial_df in grouped:
res_df_simple = zone_visits_revisits(trial_df, building_zones, "simple")
building_zones['total_victims'] = building_zones['Number of Green'] + building_zones['Number of Yellow']
building_zones_with_victim = building_zones[building_zones['total_victims'] > 0]
res_df_with_victim = zone_visits_revisits(trial_df, building_zones_with_victim, "with_victim")
building_rooms = building_zones[building_zones['Zone Type'] == 3]
res_df_rooms = zone_visits_revisits(building_df, building_rooms, "rooms")
building_rooms = building_zones[building_zones['Zone Type'] == 1]
res_df_hallways = zone_visits_revisits(building_df, building_rooms, "hallways")
building_rooms = building_zones[building_zones['Zone Type'] == 2]
res_df_entrances = zone_visits_revisits(building_df, building_rooms, "entrances")
five_min_threshold = trial_df['msg.timestamp'].min() + timedelta(minutes=5)
first_5min_df = trial_df[trial_df['msg.timestamp'] < five_min_threshold]
second_5min_df = trial_df[trial_df['msg.timestamp'] >= five_min_threshold]
res_df_first_5min = zone_visits_revisits(first_5min_df, building_zones, "first_5min")
res_df_second_5min = zone_visits_revisits(second_5min_df, building_zones, "second_5min")
trial_spacial_df = pd.concat([res_df_simple, res_df_with_victim,
res_df_rooms, res_df_hallways, res_df_entrances,
res_df_first_5min, res_df_second_5min],
axis=1)
trial_spacial_df['trial_id'] = ti
dfs.append(trial_spacial_df.set_index('trial_id'))
spacial_variables_df = pd.concat(dfs)
return spacial_variables_df