-
Notifications
You must be signed in to change notification settings - Fork 0
/
animate.py
218 lines (148 loc) · 8.03 KB
/
animate.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
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
#!/usr/bin/env python3
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from tkinter.filedialog import askopenfilename
from datetime import datetime, timedelta
from itertools import cycle
from matplotlib.animation import FuncAnimation
import sys
import kaleido
from datetime import datetime
from datetime import date
from itertools import cycle
from matplotlib.pyplot import cm
import re
# import pandas as pd
# import plotly.express as pex
#
# d1 = dict(stack=1, start='2023-09-01', finish='2023-10-01', task='Sleep')
# d2 = dict(stack=1, start='2023-10-01', finish='2021-10-15', task='EAT')
# d3 = dict(stack=1, start='2023-09-15', finish='2023-09-30', task='Study')
# d4 = dict(stack=1, start='2023-10-10', finish='2023-10-30', task='Work')
# d5 = dict(stack=1, start='2023-10-15', finish='2023-10-30', task='EAT')
# d6 = dict(stack=1, start='2023-10-15', finish='2023-11-01', task='Study')
# d8 = dict(stack=1, start='2023-11-02', finish='2023-11-15', task='EAT')
# d7 = dict(stack=1, start='2023-11-02', finish='2023-11-15', task='Sleep')
#
# dict_list = [d1,d2,d3,d4,d5,d6,d7,d8]
# for dict in dict_list:
#
#
#
# df = pd.DataFrame([d1,d2,d3,d4,d5,d6,d7,d8])
#
# gantt = pex.timeline(df, x_start='start', x_end='finish', y='stack', color='task', height=300)
# gantt
#import plotly.graph_objects as go
filename = askopenfilename(title="Select project sheet")
#filename = "FY24 Projects DRAFT-v2.xlsx"
projects_df = pd.read_excel(filename, engine="openpyxl", dtype={'start_date': 'str', 'end_date': 'str'})
projects_df['stack'] = 0
projects_df = projects_df.sort_values(['start_date', 'end_date'])
pd.set_option('display.max_columns', None)
pd.options.display.max_colwidth = 200
print(projects_df)
# projects_df['start_date'] = projects_df['start_date'].apply(lambda x: re.sub(r'(\d{4}-\d{2}-\d{2}).*$', r'\1', x))
# projects_df['end_date'] = projects_df['end_date'].apply(lambda x: re.sub(r'(\d{4}-\d{2}-\d{2}).*$', r'\1', x))
min_start_date = projects_df['start_date'].apply(lambda x: datetime.strptime(x, "%Y-%m-%d")).min()
max_end_date = projects_df['end_date'].apply(lambda x: datetime.strptime(x, "%Y-%m-%d")).max()# max_end_date = projects_df.max(pd.to_datetime('finish'))
delta = max_end_date - min_start_date
length_of_matrix = delta
delta = int(delta.total_seconds()/60/60/24)
height_of_matrix = projects_df['level_of_effort'].sum()
print("Height of matrix: " + str(height_of_matrix))
print("Width of Matrix: " + str(delta))
rows, cols = (delta, height_of_matrix)
arr = [[0]*cols]*rows
date_range = pd.date_range(min_start_date, max_end_date)
range_list = list(reversed(list(range(0, height_of_matrix))))
for z in range(0, len(range_list)):
range_list[z] = str(range_list[z])
# print (range_list[z])
# print(type(range_list[z]))
master_plotting_df = pd.DataFrame(columns=date_range, index=range_list)
master_plotting_df = master_plotting_df.applymap(lambda x: 0)
# print(master_plotting_df)
animation_frames = []
# sys.exit()
project_plotting_df = master_plotting_df.copy()
for x in range(0, len(projects_df)):
# if x == 4:
# break
# print(project_plotting_df)
#print(str(x) + " time through")
y = 0
while y + int(projects_df.iloc[x, projects_df.columns.get_loc('level_of_effort')]) < height_of_matrix:
#print(str(y + int(projects_df.loc[x, 'level_of_effort']) - 1))
#try:
project_dates_and_effort_df = project_plotting_df.loc[str(y + int(projects_df.iloc[x, projects_df.columns.get_loc('level_of_effort')]) - 1): str(y), projects_df.iloc[x, projects_df.columns.get_loc('start_date')]: projects_df.iloc[x, projects_df.columns.get_loc('end_date')]]
#except:
# break
master_plotting_subslice_df = master_plotting_df.copy()
#master_plotting_subslice_df = master_plotting_df.loc[str(y + int(projects_df.iloc[x, projects_df.columns.get_loc('level_of_effort')]) - 1): str(y), projects_df.loc[x, 'start_date']: projects_df.loc[x, 'end_date']].loc[str(y + int(projects_df.iloc[x, projects_df.columns.get_loc('level_of_effort')]) - 1): str(y), projects_df.loc[x, 'start_date']: projects_df.loc[x, 'end_date']]
# print(project_dates_and_effort_df.equals(master_plotting_subslice_df))
# sys.exit()
if (project_dates_and_effort_df.equals(master_plotting_df.loc[str(y + int(projects_df.iloc[x, projects_df.columns.get_loc('level_of_effort')]) - 1): str(y), projects_df.iloc[x, projects_df.columns.get_loc('start_date')]: projects_df.iloc[x, projects_df.columns.get_loc('end_date')]])):
projects_df.iloc[x, projects_df.columns.get_loc('stack')] = y
#print("got in")
master_plotting_df.loc[str(y + int(projects_df.iloc[x, projects_df.columns.get_loc('level_of_effort')]) - 1): str(y), projects_df.iloc[x, projects_df.columns.get_loc('start_date')]: projects_df.iloc[x, projects_df.columns.get_loc('end_date')]] = master_plotting_df.loc[str(y + int(projects_df.iloc[x, projects_df.columns.get_loc('level_of_effort')]) - 1): str(y), projects_df.iloc[x, projects_df.columns.get_loc('start_date')]: projects_df.iloc[x, projects_df.columns.get_loc('end_date')]].applymap(lambda z: 1)
# if x == 1:
# print(master_plotting_df.loc[str(y + int(projects_df.iloc[x, projects_df.columns.get_loc('level_of_effort')]) - 1): str(y), projects_df.iloc[x, projects_df.columns.get_loc('start_date')]: projects_df.iloc[x, projects_df.columns.get_loc('end_date')]])
# print(project_dates_and_effort_df)
# sys.exit()
#print(master_plotting_df.loc[str(y + int(projects_df.iloc[x, projects_df.columns.get_loc('level_of_effort')]) - 1): str(y), projects_df.loc[x, 'start_date']: projects_df.loc[x, 'end_date']])
#master_plotting_df.to_excel("Plotting Dataframe for Testing - " +str(x) + ".xlsx")
y += 1
animation_frames.append(projects_df.copy())
break
else:
animation_frames.append(projects_df.copy())
y += 1
#print('miss')
#master_plotting_df.to_excel("Plotting DataFrame.xlsx")
print(projects_df)
new_max_height_df = projects_df.copy()
new_max_height = projects_df['stack'].max()
new_max_height_df = projects_df[projects_df['stack'] == new_max_height]
new_max_height_plus_level_of_effort = int(new_max_height_df['level_of_effort'].max()) + new_max_height
# print(new_max_height_df)
# print(new_max_height_plus_level_of_effort)
df = projects_df.copy()
# Set up the figure and axis for animation
fig, gnt = plt.subplots(figsize=(16, 10))
array = np.linspace(0, 1, len(df))
np.random.shuffle(array)
colors = cycle(iter(cm.rainbow(array)))
# Function to update the plot using animation_frames
def update_plot(i):
gnt.cla() # Clear the axis
current_frame = animation_frames[i]
for index, row in projects_df.iterrows():
try:
start_date = datetime.strptime(row['start_date'], "%Y-%m-%d")
end_date = datetime.strptime(row['end_date'], "%Y-%m-%d")
duration = (end_date - start_date).days
stack = int(current_frame.loc[index, 'stack'])
level_of_effort = int(current_frame.loc[index, 'level_of_effort'])
task = current_frame.loc[index, 'task']
color = next(colors)
gnt.broken_barh([(pd.to_datetime(start_date), pd.to_datetime(end_date)-pd.to_datetime(start_date))], [int(current_frame.loc[index, 'stack']), int(current_frame.loc[index, 'level_of_effort'])], color=color, label=current_frame.loc[index, 'task'])
gnt.text(
x=start_date + timedelta(days=duration / 2),
y=(stack + level_of_effort) - level_of_effort / 2,
s=task,
ha='center',
va='center',
color='blue',
fontsize='xx-small'
)
except Exception as e:
print(e)
gnt.set_xlabel('Time')
gnt.set_ylabel('Task Effort')
gnt.set_title('Animated Broken Barh Plot')
# Create the animation
ani = FuncAnimation(fig, update_plot, frames=len(animation_frames), repeat=False)
# Display the animation
plt.show()