-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
114 lines (101 loc) · 3.55 KB
/
Copy pathutils.py
File metadata and controls
114 lines (101 loc) · 3.55 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
import pandas as pd
import numpy as np
import openpyxl
from openpyxl import Workbook
from openpyxl.styles import PatternFill
from openpyxl.utils import get_column_letter
import random
def create_color_palettes(N, seed = 42):
"""
Create a dictionary with the color palettes for the profiles.
"""
color_palettes = set()
np.random.seed(seed)
while len(color_palettes) < N:
color = "#{:02X}{:02X}{:02X}".format(*np.random.randint(128, 255, size=(3,)))
color_palettes.add(color)
dict_color = dict(zip(range(1,N+1), color_palettes))
return dict_color
def hex_to_argb(hex_color):
"""
Convert a hex color to ARGB. This is need for the excel file.
"""
hex_color = hex_color.lstrip("#")
return "FF" + hex_color
def create_random_data_frame(N, pNperiods, pNpeople):
data = np.random.randint(1, N + 1, size=(pNpeople, pNperiods))
df = pd.DataFrame(data)
return df
def apply_color(value, color_palettes):
"""
Apply the color to the cell in the excel file.
"""
if value == 0:
return None
color_ref = color_palettes[value]
color = hex_to_argb(color_ref)
fill = PatternFill(start_color=color, end_color=color, fill_type='solid')
return fill
def access_model_variables(name_variable, index_variables, model):
"""
Access to the variables of the model.
"""
variable = {}
if index_variables == 1:
for var in model.getVars():
if name_variable in var.VarName:
i = int(var.VarName.split('_')[1])
variable[i] = var
elif index_variables == 2:
for var in model.getVars():
if name_variable in var.VarName:
i, t = map(int, var.VarName.split('_')[1:])
variable[i, t] = var
elif index_variables == 3:
for var in model.getVars():
if name_variable in var.VarName:
i, j, t = map(int, var.VarName.split('_')[1:])
variable[i, j, t] = var
return variable
def add_sheet_excel(excel_file, name_sheet, df_data, index = False):
"""
Add a sheet to the excel file.
"""
if name_sheet in openpyxl.load_workbook(excel_file).sheetnames:
wb = openpyxl.load_workbook(excel_file)
wb.remove(wb[name_sheet])
wb.save(excel_file)
with pd.ExcelWriter(excel_file, engine='openpyxl', mode='a') as writer:
df_data.to_excel(writer, sheet_name=name_sheet, index = index)
def columns_dimensions(excel_file, wb, sheet, df, width = 10):
"""
Modify the width of the columns in the excel file.
"""
for i in range(df.shape[1]+1):
column_letter = get_column_letter(i+1)
sheet.column_dimensions[column_letter].width = width
wb.save(excel_file)
def find_first_period(row):
"""
Find the first period that the person is attending the emergency.
"""
for i, item in enumerate(row):
if item:
return i
def find_last_period(row, pNperiods):
"""
Find the last period that the person is attending the emergency.
"""
for i in range(5,0, -1):
if row[i]:
return i
return None
def sorted_profiles(df, pNperiods):
"""
Sort the profiles based on the periods the person is attending the emergency.
"""
df['First_Period'] = df.apply(find_first_period, axis=1)
df['Last_Period'] = df.apply(find_last_period, axis=1, pNperiods = pNperiods)
df_sorted = df.sort_values(by=['First_Period', 'Last_Period'])
df_sorted = df_sorted.drop(columns=['First_Period', 'Last_Period'])
return df_sorted