File size: 11,158 Bytes
d4c7aae | 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 | import pandas as pd
import numpy as np
def generate_material_usage_cost_matrix(process_df, material_usage_df):
overall_cost_matrix = pd.DataFrame()
overall_amount_matrix = pd.DataFrame()
process_df = process_df.reset_index()
# Find Unique for fixed the column of dataframe and Matrix
unique_material_id = material_usage_df["_id"].unique()
for index in process_df.index:
selected_process = process_df.iloc[index]
# Find Unique Material
# Find All Material Usage
selected_process_material = material_usage_df[
material_usage_df["process_id"] == selected_process["process_id"]
]
cost_dict = {}
amount_dict = {}
# Iteration Over Material
for idx in range(len(selected_process_material)):
selected_data = selected_process_material.iloc[idx]
material_id = selected_data["_id"]
cost = selected_data["unit_cost"]
amount = selected_data["amount"]
cost_dict[material_id] = cost
amount_dict[material_id] = amount
# Iteration Over Rest of Material
for material_id in unique_material_id:
if material_id not in cost_dict:
cost_dict[material_id] = 0
amount_dict[material_id] = 0
cost_df = pd.DataFrame(
cost_dict, index=[selected_process["process_id"]])
amount_df = pd.DataFrame(
amount_dict, index=[selected_process["process_id"]])
overall_cost_matrix = pd.concat([overall_cost_matrix, cost_df], axis=0)
overall_amount_matrix = pd.concat(
[overall_amount_matrix, amount_df], axis=0)
# Reorder Column to be same normal and validation
column_list = unique_material_id.tolist()
ordered_columns = sorted(column_list)
overall_cost_matrix = overall_cost_matrix[ordered_columns]
overall_amount_matrix = overall_amount_matrix[ordered_columns]
overall_cost_matrix.fillna(0, inplace=True)
overall_amount_matrix.fillna(0, inplace=True)
return (overall_cost_matrix, overall_amount_matrix)
def generate_employee_usage_cost_matrix(process_df, employee_usage_df):
overall_cost_matrix = pd.DataFrame()
overall_duration_matrix = pd.DataFrame()
overall_day_amount_matrix = pd.DataFrame()
employee_usage_df["duration"] = (
employee_usage_df["duration"] * employee_usage_df["amount"]
)
unique_emp_id = employee_usage_df["employee_id"].unique()
process_df = process_df.reset_index()
# Iteration Over each process
for index in process_df.index:
selected_process = process_df.iloc[index]
selected_process_employee = employee_usage_df[
employee_usage_df["process_id"] == selected_process["process_id"]
]
ec_cost_dict = {}
ec_duration_dict = {}
ec_day_amount_dict = {}
unique_process_employee = selected_process_employee["employee_id"].unique(
)
unique_pe_df = pd.DataFrame(
unique_process_employee, columns=["employee_id"])
# Adust Employee
# Converse many employee record to one record for each employee in one process
# Process will can contain many of employee but 1 employee only 1 record
for idx in range(len(unique_pe_df)):
selected_data = unique_pe_df.iloc[idx]
all_record = selected_process_employee[
selected_process_employee["employee_id"] == selected_data["employee_id"]
]
first_record = all_record.iloc[0]
# Cost also the same pick from the first record
cost = first_record["cost"]
day_amount = first_record["day_amount"]
# Duration is the sum of all duration of employee in the process
duration = all_record["duration"].sum()
# Update the record of unique_pe_df add cost and duration
unique_pe_df.loc[idx, "cost"] = cost
unique_pe_df.loc[idx, "duration"] = duration
unique_pe_df.loc[idx, "day_amount"] = day_amount
unique_pe_df.loc[idx, "process_id"] = first_record["process_id"]
for idx in range(len(unique_pe_df)):
selected_data = unique_pe_df.iloc[idx]
cost = selected_data["cost"]
employee_id = selected_data["employee_id"]
duration = selected_data["duration"]
day_amount = selected_data["day_amount"]
ec_cost_dict[employee_id] = cost
ec_duration_dict[employee_id] = duration
ec_day_amount_dict[employee_id] = day_amount
# Iteration Over Rest of Employee
for employee_id in unique_emp_id:
if employee_id not in ec_cost_dict:
ec_cost_dict[employee_id] = 0
ec_duration_dict[employee_id] = 0
ec_day_amount_dict[employee_id] = 1
employee_cost_df = pd.DataFrame(
ec_cost_dict, index=[selected_process["process_id"]]
)
employee_duration_df = pd.DataFrame(
ec_duration_dict, index=[selected_process["process_id"]]
)
employee_dayamount_df = pd.DataFrame(
ec_day_amount_dict, index=[selected_process["process_id"]]
)
overall_cost_matrix = pd.concat(
[overall_cost_matrix, employee_cost_df], axis=0
)
overall_duration_matrix = pd.concat(
[overall_duration_matrix, employee_duration_df], axis=0
)
overall_day_amount_matrix = pd.concat(
[overall_day_amount_matrix, employee_dayamount_df], axis=0
)
# Reorder Column to be same normal and validation
# Reorder for Emploee
column_list = unique_emp_id.tolist()
ordered_columns = sorted(column_list)
overall_cost_matrix = overall_cost_matrix[ordered_columns]
overall_duration_matrix = overall_duration_matrix[ordered_columns]
overall_day_amount_matrix = overall_day_amount_matrix[ordered_columns]
overall_cost_matrix.fillna(0, inplace=True)
overall_duration_matrix.fillna(0, inplace=True)
overall_day_amount_matrix.fillna(1, inplace=True)
return (
overall_cost_matrix,
overall_duration_matrix,
overall_day_amount_matrix,
)
def generate_capital_cost_matrix(process_df, capital_cost_df):
cost_df = pd.DataFrame()
day_amount_df = pd.DataFrame()
duration_df = pd.DataFrame()
process_df = process_df.reset_index()
# Find Unique id to fixed the column of dataframe and Matrix
unique_capital_cost_id = capital_cost_df["_id"].unique()
# Iteration Over each process
for index in process_df.index:
selected_process = process_df.iloc[index]
selected_process_capitalcost = capital_cost_df[
capital_cost_df["process_id"] == selected_process["process_id"]
]
cost_dict = {}
day_amount_dict = {}
duration_dict = {}
uniq_process_cc = selected_process_capitalcost["_id"].unique()
uniq_pcc_df = pd.DataFrame(
uniq_process_cc, columns=["capital_cost_id"])
# Adust Capital Cost
# Converse many capital Cost record to one record for each capital Cost in one process
# Process will can contain many of capital cost but 1 capital cost only 1 record
for idx in range(len(uniq_pcc_df)):
selected_data = uniq_pcc_df.iloc[idx]
all_record = selected_process_capitalcost[
selected_process_capitalcost["_id"] == selected_data["capital_cost_id"]
]
first_record = all_record.iloc[0]
# Cost also the same pick from the first record
cost = first_record["cost"]
# Duration is the sum of all duration of employee in the process
duration = all_record["duration"].sum()
# Update the record of unique_pe_df add cost and duration
uniq_pcc_df.loc[idx, "cost"] = cost
uniq_pcc_df.loc[idx, "duration"] = duration
uniq_pcc_df.loc[idx, "day_amount"] = first_record["day_amount"]
uniq_pcc_df.loc[idx, "process_id"] = first_record["process_id"]
# Iteration Only for 1 cc per record data
for idx in range(len(uniq_pcc_df)):
selected_data = uniq_pcc_df.iloc[idx]
cost = selected_data["cost"]
duration = selected_data["duration"]
captial_cost_id = selected_data["capital_cost_id"]
cost_dict[captial_cost_id] = cost
day_amount_dict[captial_cost_id] = selected_data["day_amount"]
duration_dict[captial_cost_id] = duration
# Do for the rest of capital cost object
for cc_id in unique_capital_cost_id:
if cc_id not in cost_dict:
cost_dict[cc_id] = 0
day_amount_dict[cc_id] = 1
duration_dict[cc_id] = 0
# Dataframe Generation
sub_cost_df = pd.DataFrame(
cost_dict, index=[selected_process["process_id"]])
sub_duration_df = pd.DataFrame(
duration_dict, index=[selected_process["process_id"]]
)
sub_dayamount_df = pd.DataFrame(
day_amount_dict, index=[selected_process["process_id"]]
)
# Generate Matrix
cost_df = pd.concat([cost_df, sub_cost_df], axis=0)
day_amount_df = pd.concat([day_amount_df, sub_dayamount_df], axis=0)
duration_df = pd.concat([duration_df, sub_duration_df], axis=0)
# Reorder the Column to be same on Training and validation
column_list = unique_capital_cost_id.tolist()
sorted_column = sorted(column_list)
day_amount_df = day_amount_df[sorted_column]
cost_df = cost_df[sorted_column]
duration_df = duration_df[sorted_column]
day_amount_df.fillna(1, inplace=True)
cost_df.fillna(0, inplace=True)
duration_df.fillna(1, inplace=True)
return (cost_df, day_amount_df, duration_df)
def generate_price_matrix(process_df, use_3d=False, use_unit_cost=True):
price_arr = []
process_df = process_df.reset_index()
if use_unit_cost:
for index in process_df.index:
selected_data = process_df.iloc[index]
cost = selected_data["cost"]
if use_3d:
price_arr.append([[cost]])
else:
price_arr.append([cost])
else:
for index in process_df.index:
selected_data = process_df.iloc[index]
if use_3d:
price_arr.append([[selected_data["cost"]]])
else:
price_arr.append([selected_data["cost"]])
price_arr = np.array(price_arr)
return price_arr
def generate_duration_matrix(process_df, use_3d=False):
duration_array = []
process_df = process_df.reset_index()
for index in process_df.index:
selected_data = process_df.iloc[index]
duration = selected_data["duration"]
if use_3d:
duration_array.append([[duration]])
else:
duration_array.append([duration])
duration_array = np.array(duration_array)
return duration_array
|