Iskaj commited on
Commit
30566f3
1 Parent(s): 2a1a736

change plotting to be more insightful, added mode as summary stat

Browse files
Files changed (1) hide show
  1. plot.py +28 -19
plot.py CHANGED
@@ -4,6 +4,7 @@ import numpy as np
4
  import pandas as pd
5
  import seaborn as sns
6
  import matplotlib.pyplot as plt
 
7
 
8
  from config import FPS
9
 
@@ -70,18 +71,18 @@ def add_seconds_to_datetime64(datetime64, seconds, subtract=False):
70
 
71
  def plot_segment_comparison(df, change_points):
72
  """ From the dataframe plot the current set of plots, where the bottom right is most indicative """
73
- fig, ax_arr = plt.subplots(2, 2, figsize=(12, 4), dpi=100, sharex=True)
74
- sns.scatterplot(data = df, x='time', y='SOURCE_S', ax=ax_arr[0,0])
75
- sns.lineplot(data = df, x='time', y='SOURCE_LIP_S', ax=ax_arr[0,1])
76
 
77
  # Plot change point as lines
78
- sns.lineplot(data = df, x='time', y='OFFSET_LIP', ax=ax_arr[1,0])
79
- sns.lineplot(data = df, x='time', y='OFFSET_LIP', ax=ax_arr[1,1])
80
  timestamps = change_points_to_segments(df, change_points)
81
 
82
  # To plot the detected segment lines
83
  for x in timestamps:
84
- plt.vlines(x=x, ymin=np.min(df['OFFSET_LIP']), ymax=np.max(df['OFFSET_LIP']), colors='black', lw=2)
85
  rand_y_pos = np.random.uniform(low=np.min(df['OFFSET_LIP']), high=np.max(df['OFFSET_LIP']), size=None)
86
 
87
  # To get each detected segment and their mean?
@@ -94,26 +95,34 @@ def plot_segment_comparison(df, change_points):
94
  # Cut out the segment between the segment lines
95
  segment = df[(df['time'] > start_time) & (df['time'] < end_time)] # Not offset LIP
96
  segment_no_nan = segment[~np.isnan(segment['OFFSET'])] # Remove NaNs
97
- seg_mean = np.mean(segment_no_nan['OFFSET'])
 
 
 
 
 
 
98
 
99
- # Get average difference from mean of the segment to see if it is a "straight line" or not
100
- # segment_no_nan = segment['OFFSET'][~np.isnan(segment['OFFSET'])] # Remove NaNs
101
- average_diff = np.mean(np.abs(segment_no_nan['OFFSET'] - seg_mean))
102
 
103
  # If the time where the segment comes from (origin time) is close to the start_time, it's a "good match", so no editing
104
- prefix = "GOOD" if average_diff < threshold_diff else "BAD"
105
- origin_time = add_seconds_to_datetime64(start_time, seg_mean + add_offset)
106
- # prefix = "BAD"
107
- # if (start_time < add_seconds_to_datetime64(origin_time, threshold) and (start_time > add_seconds_to_datetime64(origin_time, threshold, subtract=True))):
108
- # prefix = "GOOD"
109
 
110
  # Plot green for a confident prediction (straight line), red otherwise
111
- if prefix == "GOOD":
112
- plt.text(x=start_time, y=seg_mean, s=str(np.round(average_diff, 1)), color='g', rotation=-0.0, fontsize=14)
 
 
113
  else:
114
- plt.text(x=start_time, y=seg_mean, s=str(np.round(average_diff, 1)), color='r', rotation=-0.0, fontsize=14)
 
 
 
 
115
 
116
- print(f"[{prefix}] DIFF={average_diff:.1f} MEAN={seg_mean:.1f} {start_time} -> {end_time} comes from video X, from {origin_time}")
117
 
118
 
119
  # Return figure
 
4
  import pandas as pd
5
  import seaborn as sns
6
  import matplotlib.pyplot as plt
7
+ from scipy import stats as st
8
 
9
  from config import FPS
10
 
 
71
 
72
  def plot_segment_comparison(df, change_points):
73
  """ From the dataframe plot the current set of plots, where the bottom right is most indicative """
74
+ fig, ax_arr = plt.subplots(2, 1, figsize=(16, 6), dpi=100, sharex=True)
75
+ sns.scatterplot(data = df, x='time', y='SOURCE_S', ax=ax_arr[0])
76
+ # sns.lineplot(data = df, x='time', y='SOURCE_LIP_S', ax=ax_arr[0,1])
77
 
78
  # Plot change point as lines
79
+ # sns.lineplot(data = df, x='time', y='OFFSET_LIP', ax=ax_arr[1,0])
80
+ sns.lineplot(data = df, x='time', y='OFFSET_LIP', ax=ax_arr[1])
81
  timestamps = change_points_to_segments(df, change_points)
82
 
83
  # To plot the detected segment lines
84
  for x in timestamps:
85
+ plt.vlines(x=x, ymin=np.min(df['OFFSET_LIP']), ymax=np.max(df['OFFSET_LIP']), colors='black', lw=2, alpha=0.5)
86
  rand_y_pos = np.random.uniform(low=np.min(df['OFFSET_LIP']), high=np.max(df['OFFSET_LIP']), size=None)
87
 
88
  # To get each detected segment and their mean?
 
95
  # Cut out the segment between the segment lines
96
  segment = df[(df['time'] > start_time) & (df['time'] < end_time)] # Not offset LIP
97
  segment_no_nan = segment[~np.isnan(segment['OFFSET'])] # Remove NaNs
98
+ segment_offsets = segment_no_nan['OFFSET'] # np.round(segment_no_nan['OFFSET'], 1)
99
+ # segment_offsets = np.round(segment_no_nan['OFFSET'], 0)
100
+
101
+ # Calculate mean/median/mode
102
+ # seg_sum_stat = np.mean(segment_offsets)
103
+ # seg_sum_stat = np.median(segment_offsets)
104
+ seg_sum_stat = st.mode(segment_offsets)[0][0]
105
 
106
+ # Get average difference from mean/median/mode of the segment to see if it is a "straight line" or not
107
+ average_diff = np.mean(np.abs(segment_offsets - seg_sum_stat))
 
108
 
109
  # If the time where the segment comes from (origin time) is close to the start_time, it's a "good match", so no editing
110
+ noisy = False if average_diff < threshold_diff else True
111
+ origin_time = add_seconds_to_datetime64(start_time, seg_sum_stat + add_offset)
 
 
 
112
 
113
  # Plot green for a confident prediction (straight line), red otherwise
114
+ if not noisy:
115
+ # Plot estimated straight line
116
+ plt.hlines(y=seg_sum_stat, xmin=start_time, xmax=end_time, color='green', lw=3, alpha=0.5)
117
+ plt.text(x=start_time, y=seg_sum_stat, s=str(np.round(average_diff, 1)), color='green', rotation=-0.0, fontsize=14)
118
  else:
119
+ # Plot estimated straight line
120
+ plt.hlines(y=seg_sum_stat, xmin=start_time, xmax=end_time, color='red', lw=3, alpha=0.5)
121
+ plt.text(x=start_time, y=seg_sum_stat, s=str(np.round(average_diff, 1)), color='red', rotation=-0.0, fontsize=14)
122
+
123
+
124
 
125
+ # print(f"DIFF={average_diff:.1f} SUMSTAT={seg_sum_stat:.1f} {start_time} -> {end_time} comes from video X, from {origin_time}")
126
 
127
 
128
  # Return figure