Tin Theethawat Savastham commited on
Commit
9a8258d
·
1 Parent(s): fb514b0

✨ Update Modeling Notebook

Browse files
README.md CHANGED
@@ -12,6 +12,7 @@ Creating ENV followed by `.env.example` and then Creating your virtual environme
12
 
13
  ```
14
  python -m venv venv
 
15
  ```
16
 
17
  Install all requirements
 
12
 
13
  ```
14
  python -m venv venv
15
+ venv/Script/activate
16
  ```
17
 
18
  Install all requirements
example/.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ datasets/
example/1-Basic-Model-Constructor.ipynb CHANGED
@@ -0,0 +1,445 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "7259c012",
6
+ "metadata": {},
7
+ "source": [
8
+ "# TDCE Learning Model Basic Model Construction\n",
9
+ "This notebook display the basic construction of Time Driven Cost Estimation Learning Model step by step without using the experiment script (experiment_script.py)"
10
+ ]
11
+ },
12
+ {
13
+ "cell_type": "markdown",
14
+ "id": "050178b5",
15
+ "metadata": {},
16
+ "source": [
17
+ "Import the library"
18
+ ]
19
+ },
20
+ {
21
+ "cell_type": "code",
22
+ "execution_count": null,
23
+ "id": "ae7385ba",
24
+ "metadata": {},
25
+ "outputs": [],
26
+ "source": [
27
+ "import pandas as pd\n",
28
+ "import importlib\n",
29
+ "import sys\n",
30
+ "import os\n",
31
+ "import time\n",
32
+ "import numpy as np\n",
33
+ "import requests\n",
34
+ "import ipywidgets as widgets\n",
35
+ "from IPython.display import display\n",
36
+ "\n",
37
+ "# fmt:off\n",
38
+ "sys.path.append('../model')\n",
39
+ "sys.path.append('../functions/matrix_generator')\n",
40
+ "sys.path.append('../functions')\n",
41
+ "sys.path.append('../functions/data_extractor')\n",
42
+ "\n",
43
+ "import tdce_model as tdce\n",
44
+ "import material_fc_layer as mfl\n",
45
+ "import employee_fc_layer as efl\n",
46
+ "import capital_fc_layer as cfl\n",
47
+ "import loss\n",
48
+ "import cost_matrix_class as cmc\n",
49
+ "import display_input_variation as diva\n",
50
+ "import viyacrab_augmentation as viya\n",
51
+ "import adjust_data as ajd\n",
52
+ "import result_display as rd\n",
53
+ "\n",
54
+ "importlib.reload(tdce)\n",
55
+ "importlib.reload(mfl)\n",
56
+ "importlib.reload(efl)\n",
57
+ "importlib.reload(cfl)\n",
58
+ "importlib.reload(loss)\n",
59
+ "importlib.reload(tdce)\n",
60
+ "importlib.reload(cmc)\n",
61
+ "importlib.reload(diva)\n",
62
+ "importlib.reload(viya)\n",
63
+ "importlib.reload(ajd)\n",
64
+ "importlib.reload(rd)\n",
65
+ "# fmt:on\n"
66
+ ]
67
+ },
68
+ {
69
+ "cell_type": "markdown",
70
+ "id": "6b7fd72c",
71
+ "metadata": {},
72
+ "source": [
73
+ "## Dataset \n",
74
+ "We will use our project dataset for experimental, we pick the [extended-random-dataset](https://huggingface.co/datasets/theethawats98/tdce-example-extended-random) which is the dataset with high dimension but moderate variation to use as case study for out demonstation. We create the dataset in folder `datasets` and then inside it have the folder `extended-random` again. We will create the folder if it is not exist and download the datafile from the our huggingface."
75
+ ]
76
+ },
77
+ {
78
+ "cell_type": "code",
79
+ "execution_count": null,
80
+ "id": "aa31e7f6",
81
+ "metadata": {},
82
+ "outputs": [],
83
+ "source": [
84
+ "try:\n",
85
+ " os.mkdir(f\"datasets\")\n",
86
+ " os.mkdir(f\"datasets/extended-random\")\n",
87
+ "except FileExistsError:\n",
88
+ " print(\"Folder is Exist\")\n",
89
+ " pass"
90
+ ]
91
+ },
92
+ {
93
+ "cell_type": "markdown",
94
+ "id": "ca939290",
95
+ "metadata": {},
96
+ "source": [
97
+ "Download Files"
98
+ ]
99
+ },
100
+ {
101
+ "cell_type": "code",
102
+ "execution_count": null,
103
+ "id": "f98a61ed",
104
+ "metadata": {},
105
+ "outputs": [],
106
+ "source": [
107
+ "capital_cost_link = \"https://huggingface.co/datasets/theethawats98/tdce-example-extended-random/resolve/main/generated_capital_cost.csv\"\n",
108
+ "capital_path = 'datasets/extended-random/generated_capital_cost.csv'\n",
109
+ "employee_usage_link = \"https://huggingface.co/datasets/theethawats98/tdce-example-extended-random/resolve/main/generated_employee_usage.csv\"\n",
110
+ "employee_path = 'datasets/extended-random/generated_employee_usage.csv'\n",
111
+ "material_usage_link = \"https://huggingface.co/datasets/theethawats98/tdce-example-extended-random/resolve/main/generated_material_usage.csv\"\n",
112
+ "material_path = 'datasets/extended-random/generated_material_usage.csv'\n",
113
+ "process_data_link = \"https://huggingface.co/datasets/theethawats98/tdce-example-extended-random/resolve/main/generated_process_data.csv\"\n",
114
+ "process_path = 'datasets/extended-random/generated_process_data.csv'\n",
115
+ "\n",
116
+ "\n",
117
+ "for link, path in [\n",
118
+ " (capital_cost_link, capital_path),\n",
119
+ " (employee_usage_link, employee_path),\n",
120
+ " (material_usage_link, material_path),\n",
121
+ " (process_data_link, process_path)\n",
122
+ "]:\n",
123
+ " if not os.path.exists(path):\n",
124
+ " response = requests.get(link)\n",
125
+ " if response.status_code == 200:\n",
126
+ " with open(path, 'wb') as file:\n",
127
+ " file.write(response.content)\n",
128
+ " print(f'File {path} downloaded successfully')\n",
129
+ " else:\n",
130
+ " print(f'Failed to download file {path}')\n",
131
+ "# Downloading the datasets\n",
132
+ "print(\"Downloading datasets...\")"
133
+ ]
134
+ },
135
+ {
136
+ "cell_type": "markdown",
137
+ "id": "8b97d5b2",
138
+ "metadata": {},
139
+ "source": [
140
+ "## Model Setting\n",
141
+ "Select the correct setting for your model."
142
+ ]
143
+ },
144
+ {
145
+ "cell_type": "code",
146
+ "execution_count": null,
147
+ "id": "3a387bbe",
148
+ "metadata": {},
149
+ "outputs": [],
150
+ "source": [
151
+ "hour_day_employee_widget = widgets.BoundedIntText(\n",
152
+ " value=8,\n",
153
+ " min=1,\n",
154
+ " max=24,\n",
155
+ " step=1,\n",
156
+ " description='Hours per Day for Employee:',\n",
157
+ " disabled=False\n",
158
+ ")\n",
159
+ "\n",
160
+ "\n",
161
+ "hour_day_capital_cost_widget = widgets.BoundedIntText(\n",
162
+ " value=21,\n",
163
+ " min=1,\n",
164
+ " max=24,\n",
165
+ " step=1,\n",
166
+ " description='Hours per Day for Utility / Capital Cost:',\n",
167
+ " disabled=False\n",
168
+ ")\n",
169
+ "\n",
170
+ "use_outlier_removal_widget = widgets.Checkbox(\n",
171
+ " value=False,\n",
172
+ " description='Enable Outlier Removal',\n",
173
+ " disabled=False,\n",
174
+ " indent=False\n",
175
+ ")\n",
176
+ "\n",
177
+ "outlier_index_widget = widgets.Dropdown(\n",
178
+ " options=['1', '1.5', '2'],\n",
179
+ " value='1.5',\n",
180
+ " description='Removal Idication Index:',\n",
181
+ " disabled=False,\n",
182
+ ")\n",
183
+ "\n",
184
+ "use_augmentation_widget = widgets.Checkbox(\n",
185
+ " value=False,\n",
186
+ " description='Enable Data Augmentation',\n",
187
+ " disabled=False,\n",
188
+ " indent=False\n",
189
+ ")\n",
190
+ "\n",
191
+ "element_level_lr_widget = widgets.Dropdown(\n",
192
+ " options=['0.001','0.05', '0.01','0.1','0.5'],\n",
193
+ " value='0.01',\n",
194
+ " description='Element Level Learning Rate:',\n",
195
+ " disabled=False,\n",
196
+ ")\n",
197
+ "\n",
198
+ "model_level_lr_widget = widgets.Dropdown(\n",
199
+ " options=['0.0000001','0.00000001','0.000000001'],\n",
200
+ " value='0.00000001',\n",
201
+ " description='Model Level Learning Rate:',\n",
202
+ " disabled=False,\n",
203
+ ")\n",
204
+ "\n",
205
+ "\n",
206
+ "display(hour_day_employee_widget)\n",
207
+ "display(hour_day_capital_cost_widget)\n",
208
+ "display(use_outlier_removal_widget)\n",
209
+ "display(outlier_index_widget)\n",
210
+ "display(use_augmentation_widget)"
211
+ ]
212
+ },
213
+ {
214
+ "cell_type": "markdown",
215
+ "id": "ac4c4fc1",
216
+ "metadata": {},
217
+ "source": [
218
+ "Select Learning Rate"
219
+ ]
220
+ },
221
+ {
222
+ "cell_type": "code",
223
+ "execution_count": null,
224
+ "id": "fce6161f",
225
+ "metadata": {},
226
+ "outputs": [],
227
+ "source": [
228
+ "display(element_level_lr_widget)\n",
229
+ "display(model_level_lr_widget)"
230
+ ]
231
+ },
232
+ {
233
+ "cell_type": "code",
234
+ "execution_count": null,
235
+ "id": "8e7c42b1",
236
+ "metadata": {},
237
+ "outputs": [],
238
+ "source": [
239
+ "hour_day_employee = hour_day_employee_widget.value\n",
240
+ "hour_day_capital_cost = hour_day_capital_cost_widget.value\n",
241
+ "use_outlier_removal= use_outlier_removal_widget.value\n",
242
+ "outlier_index = float(outlier_index_widget.value)\n",
243
+ "use_augmentation = use_augmentation_widget.value"
244
+ ]
245
+ },
246
+ {
247
+ "cell_type": "markdown",
248
+ "id": "842aa666",
249
+ "metadata": {},
250
+ "source": [
251
+ "Generated Cost Matrix"
252
+ ]
253
+ },
254
+ {
255
+ "cell_type": "code",
256
+ "execution_count": null,
257
+ "id": "f7ed24d9",
258
+ "metadata": {},
259
+ "outputs": [],
260
+ "source": [
261
+ "folder_path = 'datasets/extended-random'\n",
262
+ "\n",
263
+ "cost_generator = cmc.CostMatrixGenerator()\n",
264
+ "cost_generator.change_data_directory(folder_path)\n",
265
+ "cost_generator.load_data()\n",
266
+ "input_variation = diva.display_input_variation_by_directory(folder_path)\n",
267
+ "input_variation.to_csv(f\"{folder_path}/data_variation.csv\")"
268
+ ]
269
+ },
270
+ {
271
+ "cell_type": "markdown",
272
+ "id": "3bd65eb3",
273
+ "metadata": {},
274
+ "source": [
275
+ "## Pre-Processing\n",
276
+ "djust and filter in input datasets (Material, Employee, Capital Cost) to match the output dataset (Process Dataset) and Depend on Outlier Removal Condition"
277
+ ]
278
+ },
279
+ {
280
+ "cell_type": "code",
281
+ "execution_count": null,
282
+ "id": "cec6dcf7",
283
+ "metadata": {},
284
+ "outputs": [],
285
+ "source": [
286
+ "if use_outlier_removal:\n",
287
+ " cost_generator.remove_outlier_iqr(outlier_index)\n",
288
+ " (\n",
289
+ " new_process_df,\n",
290
+ " new_employee_usage,\n",
291
+ " new_material_usage,\n",
292
+ " new_capital_cost_usage,\n",
293
+ " ) = cost_generator.get_data()\n",
294
+ " (new_capital_cost_usage, new_employee_usage, new_material_usage) = (\n",
295
+ " ajd.adjust_to_match_process(\n",
296
+ " capital_cost_usage=new_capital_cost_usage,\n",
297
+ " employee_usage=new_employee_usage,\n",
298
+ " material_usage=new_material_usage,\n",
299
+ " new_process_df=new_process_df,\n",
300
+ " )\n",
301
+ " )\n",
302
+ " new_variation = diva.display_input_variation(\n",
303
+ " new_process_df,\n",
304
+ " new_material_usage,\n",
305
+ " new_employee_usage,\n",
306
+ " new_capital_cost_usage,\n",
307
+ " )\n",
308
+ " new_variation.to_csv(f\"{folder_path}/data_variation_after_outlier.csv\")\n",
309
+ " new_process_df.to_csv(f\"{folder_path}/process_df_after_outlier.csv\")\n",
310
+ " try:\n",
311
+ " print(\"Data Variation After Outlier Removed\")\n",
312
+ " display(new_variation)\n",
313
+ " except Exception as e:\n",
314
+ " print(\"This is not Jupyter Notebook\", e)\n",
315
+ "else:\n",
316
+ " display(input_variation)"
317
+ ]
318
+ },
319
+ {
320
+ "cell_type": "markdown",
321
+ "id": "00976358",
322
+ "metadata": {},
323
+ "source": [
324
+ "### Data Splitting\n",
325
+ "Split training and validation dataset"
326
+ ]
327
+ },
328
+ {
329
+ "cell_type": "code",
330
+ "execution_count": null,
331
+ "id": "d1904264",
332
+ "metadata": {},
333
+ "outputs": [],
334
+ "source": [
335
+ "(\n",
336
+ " train_process_df,\n",
337
+ " train_employee_usage,\n",
338
+ " train_material_usage,\n",
339
+ " train_capital_cost,\n",
340
+ " validate_process_df,\n",
341
+ " validate_employee_usage,\n",
342
+ " validate_material_usage,\n",
343
+ " validate_capital_cost,\n",
344
+ ") = cost_generator.train_test_split_without_matrix(0.7)\n",
345
+ "\n",
346
+ "\n",
347
+ "# Generate Cost Matrix for Validation Set\n",
348
+ "validation_payload = cost_generator.get_validation_payload(\n",
349
+ " validate_process_df\n",
350
+ ")\n",
351
+ "\n",
352
+ "\n",
353
+ "# Display Variation of Train Data\n",
354
+ "train_variation = diva.display_input_variation(\n",
355
+ " train_process_df,\n",
356
+ " train_material_usage,\n",
357
+ " train_employee_usage,\n",
358
+ " train_capital_cost,\n",
359
+ ")\n",
360
+ "train_variation.to_csv(\n",
361
+ " f\"{folder_path}/train_data_variation.csv\")\n",
362
+ "train_process_df.to_csv(\n",
363
+ " f\"{folder_path}/train_process_df.csv\")\n",
364
+ "# Display Variation of Validation Data\n",
365
+ "validate_variation = diva.display_input_variation(\n",
366
+ " validate_process_df,\n",
367
+ " validate_material_usage,\n",
368
+ " validate_employee_usage,\n",
369
+ " validate_capital_cost,\n",
370
+ ")\n",
371
+ "validate_variation.to_csv(\n",
372
+ " f\"{folder_path}/validate_data_variation.csv\"\n",
373
+ ")\n",
374
+ "if use_augmentation:\n",
375
+ " # TODO: Increase the Generalization of the Model\n",
376
+ " # Augmented the Imbalance Class of Training Data\n",
377
+ " train_process_df.to_csv(\n",
378
+ " f\"{folder_path}/train_process_df_before_augmented.csv\"\n",
379
+ " )\n",
380
+ " train_process_df = viya.vy_training_augmentation(\n",
381
+ " train_process_df)\n",
382
+ " # Display Variation of Train Data After Augmented\n",
383
+ " train_variation = diva.display_input_variation(\n",
384
+ " train_process_df,\n",
385
+ " train_material_usage,\n",
386
+ " train_employee_usage,\n",
387
+ " train_capital_cost,\n",
388
+ " )\n",
389
+ " train_variation.to_csv(\n",
390
+ " f\"{folder_path}/train_data_variation_after_augmented.csv\"\n",
391
+ " )\n",
392
+ " train_process_df.to_csv(\n",
393
+ " f\"{folder_path}/train_process_df_after_augmented_{round}.csv\"\n",
394
+ " )\n",
395
+ " \n",
396
+ "# Generate Matrix From Training Set\n",
397
+ "(\n",
398
+ " material_cost_matrix,\n",
399
+ " material_amount_matrix,\n",
400
+ " employee_cost_matrix,\n",
401
+ " employee_duration_matrix,\n",
402
+ " employee_day_amount_matrix,\n",
403
+ " capital_cost_matrix,\n",
404
+ " day_amount_matrix,\n",
405
+ " capital_cost_duration_matrix, # New On Finetune\n",
406
+ " result_matrix,\n",
407
+ ") = cost_generator.generate_data_from_input(\n",
408
+ " train_process_df,\n",
409
+ " train_material_usage,\n",
410
+ " train_employee_usage,\n",
411
+ " train_capital_cost,\n",
412
+ ")"
413
+ ]
414
+ },
415
+ {
416
+ "cell_type": "markdown",
417
+ "id": "d1bdcbbc",
418
+ "metadata": {},
419
+ "source": [
420
+ "## Initial Model"
421
+ ]
422
+ }
423
+ ],
424
+ "metadata": {
425
+ "kernelspec": {
426
+ "display_name": "venv",
427
+ "language": "python",
428
+ "name": "python3"
429
+ },
430
+ "language_info": {
431
+ "codemirror_mode": {
432
+ "name": "ipython",
433
+ "version": 3
434
+ },
435
+ "file_extension": ".py",
436
+ "mimetype": "text/x-python",
437
+ "name": "python",
438
+ "nbconvert_exporter": "python",
439
+ "pygments_lexer": "ipython3",
440
+ "version": "3.13.2"
441
+ }
442
+ },
443
+ "nbformat": 4,
444
+ "nbformat_minor": 5
445
+ }
functions/display_input_variation.py CHANGED
@@ -155,7 +155,7 @@ def display_input_variation_by_directory(folder_name):
155
  f"{folder_name}/generated_material_usage.csv")
156
  employee_usage_df = pd.read_csv(
157
  f"{folder_name}/generated_employee_usage.csv")
158
- capital_cost_df = pd.read_csv(f"{folder_name}/generated_captial_cost.csv")
159
 
160
  result_variation = display_input_variation(
161
  process_df,
 
155
  f"{folder_name}/generated_material_usage.csv")
156
  employee_usage_df = pd.read_csv(
157
  f"{folder_name}/generated_employee_usage.csv")
158
+ capital_cost_df = pd.read_csv(f"{folder_name}/generated_capital_cost.csv")
159
 
160
  result_variation = display_input_variation(
161
  process_df,
requirement.txt CHANGED
@@ -8,4 +8,6 @@ scipy
8
  cowsay
9
  pyfiglet
10
  seaborn
11
- statsmodels
 
 
 
8
  cowsay
9
  pyfiglet
10
  seaborn
11
+ statsmodels
12
+ requests
13
+ ipywidgets