Aditya6263 commited on
Commit
79aaf74
·
verified ·
1 Parent(s): 367cb1e

Upload folder using huggingface_hub

Browse files
server/experiment_repo/src/train.py CHANGED
@@ -32,7 +32,14 @@ def main():
32
  model_type = config['model']['type']
33
  print(f"Initializing {model_type} model...")
34
 
35
- model = build_model(model_type, config, removed_columns=[])
 
 
 
 
 
 
 
36
 
37
  # training model
38
  print("Starting training...")
 
32
  model_type = config['model']['type']
33
  print(f"Initializing {model_type} model...")
34
 
35
+ removed_columns = config.get('removed_columns', [])
36
+ if not removed_columns:
37
+ if 'model' in config and 'removed_columns' in config['model']:
38
+ removed_columns = config['model']['removed_columns']
39
+ elif 'data' in config and 'removed_columns' in config['data']:
40
+ removed_columns = config['data']['removed_columns']
41
+
42
+ model = build_model(model_type, config, removed_columns=removed_columns)
43
 
44
  # training model
45
  print("Starting training...")
server/my_env_environment.py CHANGED
@@ -103,6 +103,7 @@ class MyEnvironment(Environment):
103
  for writing feature names remember you can write them as comma separated values or as plus separated values or as group names as given in the feature_description_txt file.
104
  comma separated means that, those individual features give different F1 when individually removed , whereas plus separated means that, all those features give a single F1 when removed as a group.
105
  similarly, writing a category name means , all the features in that category give a single F1 when removed as a group.
 
106
  ''')]
107
  },
108
  dir_structure=init_Directory("exp_repo",[
@@ -232,8 +233,13 @@ class MyEnvironment(Environment):
232
  if not b:
233
  raise AttributeError("The path does not lead to any existing directory.")
234
  self._state.cwd_contents=getattr(self._state.dir_structure,l[-1])
235
- self._state.cwd=action.path
236
- message="Changed directory to "+action.path
 
 
 
 
 
237
  except AttributeError as e:
238
  message=str(e)
239
  penalty+=-0.12
@@ -241,15 +247,17 @@ class MyEnvironment(Environment):
241
  def _read(self,action: MyAction,penalty:float)->tuple[str,float]:
242
  message=""
243
  try:
244
- b=isinstance(getattr(self._state.dir_structure,action.path),list)
 
 
245
  if b:
246
  raise AttributeError("The path does not lead to any existing file.")
247
- self._state.file_contents=getattr(self._state.dir_structure,action.path)
248
  message="Read file "+action.path
249
  # Emit event
250
  self.events["results_read"] += 1
251
- except AttributeError as e:
252
- message=str(e)
253
  penalty+=-0.12
254
  return message,penalty
255
  def _write(self,action: MyAction,penalty:float)->tuple[str,float]:
@@ -260,12 +268,27 @@ class MyEnvironment(Environment):
260
 
261
  # Dynamic physical directory path resolution
262
  server_dir = os.path.dirname(os.path.abspath(__file__))
263
- rel_cwd = os.path.relpath(self._state.cwd, "exp_repo")
264
- if rel_cwd == ".":
265
- rel_cwd = ""
 
 
266
 
267
- physical_cwd = os.path.normpath(os.path.join(server_dir, "experiment_repo", rel_cwd))
268
- resolved_file_path = os.path.normpath(os.path.join(physical_cwd, action.path))
 
 
 
 
 
 
 
 
 
 
 
 
 
269
 
270
  l = action.path.split('/')
271
  filename = l[-1]
@@ -311,11 +334,30 @@ class MyEnvironment(Environment):
311
  raise ValueError("Path is required for run action.")
312
 
313
  server_dir = os.path.dirname(os.path.abspath(__file__))
314
- rel_cwd = os.path.relpath(self._state.cwd, "exp_repo")
315
- if rel_cwd == ".":
316
- rel_cwd = ""
317
- physical_cwd = os.path.normpath(os.path.join(server_dir, "experiment_repo", rel_cwd))
318
- resolved_script_path = os.path.normpath(os.path.join(physical_cwd, action.path))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
319
  logical_path = resolved_script_path.replace(os.sep, "/")
320
 
321
  if not logical_path.endswith("src/train.py"):
@@ -323,11 +365,14 @@ class MyEnvironment(Environment):
323
 
324
  # Build command
325
  if action.content:
326
- command = ["python", resolved_script_path, "--config", action.content]
 
 
 
327
  else:
328
  command = ["python", resolved_script_path]
329
 
330
- result = subprocess.run(command, capture_output=True, text=True, cwd=physical_cwd)
331
 
332
  if result.returncode == 0:
333
  # Emit the correct event based on which config was used
@@ -335,9 +380,10 @@ class MyEnvironment(Environment):
335
  self.events["xgb_run"] += 1
336
  else:
337
  self.events["nn_run"] += 1
338
- message = f"Successfully ran training script with config: {action.content or 'default'}"
 
339
  else:
340
- raise RuntimeError(f"Script failed (code {result.returncode}):\n{result.stderr.strip()[:500]}")
341
  except Exception as e:
342
  message = str(e)
343
  penalty += -0.12
 
103
  for writing feature names remember you can write them as comma separated values or as plus separated values or as group names as given in the feature_description_txt file.
104
  comma separated means that, those individual features give different F1 when individually removed , whereas plus separated means that, all those features give a single F1 when removed as a group.
105
  similarly, writing a category name means , all the features in that category give a single F1 when removed as a group.
106
+ how to test:place a removed_columns: [...] list parameter anywhere under, 'model', or 'data' in the base config, to remove columns.
107
  ''')]
108
  },
109
  dir_structure=init_Directory("exp_repo",[
 
233
  if not b:
234
  raise AttributeError("The path does not lead to any existing directory.")
235
  self._state.cwd_contents=getattr(self._state.dir_structure,l[-1])
236
+
237
+ if action.path.startswith("exp_repo"):
238
+ self._state.cwd = action.path
239
+ else:
240
+ self._state.cwd = self._state.cwd.rstrip('/') + "/" + action.path.lstrip('/')
241
+
242
+ message="Changed directory to "+self._state.cwd
243
  except AttributeError as e:
244
  message=str(e)
245
  penalty+=-0.12
 
247
  def _read(self,action: MyAction,penalty:float)->tuple[str,float]:
248
  message=""
249
  try:
250
+ l=action.path.split('/')
251
+ attr_name=l[-1].replace('.', '_')
252
+ b=isinstance(getattr(self._state.dir_structure,attr_name),list)
253
  if b:
254
  raise AttributeError("The path does not lead to any existing file.")
255
+ self._state.file_contents=getattr(self._state.dir_structure,attr_name)
256
  message="Read file "+action.path
257
  # Emit event
258
  self.events["results_read"] += 1
259
+ except AttributeError:
260
+ message=f"The file {action.path} does not exist."
261
  penalty+=-0.12
262
  return message,penalty
263
  def _write(self,action: MyAction,penalty:float)->tuple[str,float]:
 
268
 
269
  # Dynamic physical directory path resolution
270
  server_dir = os.path.dirname(os.path.abspath(__file__))
271
+
272
+ if action.path.startswith("exp_repo"):
273
+ logical_path = action.path
274
+ else:
275
+ logical_path = self._state.cwd.rstrip('/') + "/" + action.path.lstrip('/')
276
 
277
+ logical_path = '/'.join([p for p in logical_path.split('/') if p])
278
+
279
+ if logical_path.startswith("exp_repo"):
280
+ rel_path = logical_path[len("exp_repo"):]
281
+ if rel_path.startswith('/'): rel_path = rel_path[1:]
282
+ else:
283
+ rel_path = logical_path
284
+
285
+ l_rel = rel_path.split('/')
286
+ filename = l_rel[-1]
287
+ attr_name = filename.replace('.', '_') # dummy name for dir_structure
288
+ actual_filename = attr_name[::-1].replace("_", ".", 1)[::-1] # convert dummy name to real filename
289
+ l_rel[-1] = actual_filename
290
+
291
+ resolved_file_path = os.path.normpath(os.path.join(server_dir, "experiment_repo", *l_rel))
292
 
293
  l = action.path.split('/')
294
  filename = l[-1]
 
334
  raise ValueError("Path is required for run action.")
335
 
336
  server_dir = os.path.dirname(os.path.abspath(__file__))
337
+
338
+ if action.path.startswith("exp_repo"):
339
+ logical_path = action.path
340
+ else:
341
+ logical_path = self._state.cwd.rstrip('/') + "/" + action.path.lstrip('/')
342
+
343
+ logical_path = '/'.join([p for p in logical_path.split('/') if p])
344
+
345
+ if logical_path.startswith("exp_repo"):
346
+ rel_path = logical_path[len("exp_repo"):]
347
+ if rel_path.startswith('/'): rel_path = rel_path[1:]
348
+ else:
349
+ rel_path = logical_path
350
+
351
+ physical_cwd = os.path.normpath(os.path.join(server_dir, "experiment_repo"))
352
+ resolved_script_path = os.path.normpath(os.path.join(physical_cwd, rel_path))
353
+
354
+ rel_cwd_str = self._state.cwd
355
+ if rel_cwd_str.startswith("exp_repo"):
356
+ rel_cwd_str = rel_cwd_str[len("exp_repo"):]
357
+ if rel_cwd_str.startswith('/'): rel_cwd_str = rel_cwd_str[1:]
358
+
359
+ cmd_cwd = os.path.normpath(os.path.join(server_dir, "experiment_repo", rel_cwd_str))
360
+
361
  logical_path = resolved_script_path.replace(os.sep, "/")
362
 
363
  if not logical_path.endswith("src/train.py"):
 
365
 
366
  # Build command
367
  if action.content:
368
+ content_real = action.content
369
+ if '.' not in content_real and '_' in content_real:
370
+ content_real = content_real[::-1].replace('_', '.', 1)[::-1]
371
+ command = ["python", resolved_script_path, "--config", content_real]
372
  else:
373
  command = ["python", resolved_script_path]
374
 
375
+ result = subprocess.run(command, capture_output=True, text=True, cwd=cmd_cwd)
376
 
377
  if result.returncode == 0:
378
  # Emit the correct event based on which config was used
 
380
  self.events["xgb_run"] += 1
381
  else:
382
  self.events["nn_run"] += 1
383
+ message = f"Successfully ran training script with config: {action.content or 'default'}\n"
384
+ message += f"Script Output:\n{result.stdout.strip()[:1000]}"
385
  else:
386
+ raise RuntimeError(f"Script failed (code {result.returncode}):\n{result.stderr.strip()[:1000]}")
387
  except Exception as e:
388
  message = str(e)
389
  penalty += -0.12