Joel Woodfield commited on
Commit
97d2547
·
1 Parent(s): 0c4e50c

Add range and levels error handling

Browse files
backend/src/__pycache__/manager.cpython-312.pyc CHANGED
Binary files a/backend/src/__pycache__/manager.cpython-312.pyc and b/backend/src/__pycache__/manager.cpython-312.pyc differ
 
backend/src/manager.py CHANGED
@@ -106,8 +106,19 @@ class Manager:
106
  unknown_names = ", ".join(sorted(str(s) for s in unknown_symbols))
107
  raise ValueError(f"Unknown variable(s): {unknown_names}. Allowed: x1, x2")
108
 
109
- x1_range = self._parse_range(x1_range_input)
110
- x2_range = self._parse_range(x2_range_input)
 
 
 
 
 
 
 
 
 
 
 
111
 
112
  method = x_selection_method.lower()
113
  if method not in ("grid", "random"):
@@ -154,9 +165,19 @@ class Manager:
154
  if regularizer_type not in ("l1", "l2"):
155
  raise ValueError(f"Invalid regularizer_type: {regularizer_type}")
156
 
157
- reg_levels = self._parse_levels(reg_levels_input)
158
- w1_range = self._parse_range(w1_range_input)
159
- w2_range = self._parse_range(w2_range_input)
 
 
 
 
 
 
 
 
 
 
160
 
161
  self.plots_data = compute_plot_values(
162
  self.dataset,
@@ -352,16 +373,44 @@ class Manager:
352
 
353
  @staticmethod
354
  def _parse_range(range_input: str) -> tuple[float, float]:
355
- values = tuple(float(x.strip()) for x in range_input.split(","))
 
356
  if len(values) != 2:
357
  raise ValueError("Range must contain exactly two comma-separated values")
358
- return values
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
359
 
360
  @staticmethod
361
  def _parse_levels(levels_input: str) -> list[float]:
362
- values = [float(x.strip()) for x in levels_input.split(",")]
363
- if not values:
364
  raise ValueError("At least one regularization level is required")
 
 
 
 
 
 
 
 
 
365
  return values
366
 
367
  @staticmethod
 
106
  unknown_names = ", ".join(sorted(str(s) for s in unknown_symbols))
107
  raise ValueError(f"Unknown variable(s): {unknown_names}. Allowed: x1, x2")
108
 
109
+ if not x1_range_input.strip():
110
+ raise ValueError("x1 range cannot be empty")
111
+ if not x2_range_input.strip():
112
+ raise ValueError("x2 range cannot be empty")
113
+
114
+ try:
115
+ x1_range = self._parse_range(x1_range_input)
116
+ except Exception as e:
117
+ raise ValueError(f"Invalid x1 range: {e}")
118
+ try:
119
+ x2_range = self._parse_range(x2_range_input)
120
+ except Exception as e:
121
+ raise ValueError(f"Invalid x2 range: {e}")
122
 
123
  method = x_selection_method.lower()
124
  if method not in ("grid", "random"):
 
165
  if regularizer_type not in ("l1", "l2"):
166
  raise ValueError(f"Invalid regularizer_type: {regularizer_type}")
167
 
168
+ try:
169
+ reg_levels = self._parse_levels(reg_levels_input)
170
+ except Exception as e:
171
+ raise ValueError(f"Invalid regularization levels: {e}")
172
+
173
+ try:
174
+ w1_range = self._parse_range(w1_range_input)
175
+ except Exception as e:
176
+ raise ValueError(f"Invalid w1 range: {e}")
177
+ try:
178
+ w2_range = self._parse_range(w2_range_input)
179
+ except Exception as e:
180
+ raise ValueError(f"Invalid w2 range: {e}")
181
 
182
  self.plots_data = compute_plot_values(
183
  self.dataset,
 
373
 
374
  @staticmethod
375
  def _parse_range(range_input: str) -> tuple[float, float]:
376
+ values = tuple(x.strip() for x in range_input.split(","))
377
+
378
  if len(values) != 2:
379
  raise ValueError("Range must contain exactly two comma-separated values")
380
+
381
+ low = values[0]
382
+ high = values[1]
383
+
384
+ if low == "":
385
+ raise ValueError("Range lower bound cannot be empty")
386
+ if high == "":
387
+ raise ValueError("Range upper bound cannot be empty")
388
+
389
+ try:
390
+ low = float(low)
391
+ high = float(high)
392
+ except ValueError:
393
+ raise ValueError("Range values must be valid numbers")
394
+
395
+ if low >= high:
396
+ raise ValueError("Range lower bound must be less than upper bound")
397
+
398
+ return low, high
399
 
400
  @staticmethod
401
  def _parse_levels(levels_input: str) -> list[float]:
402
+ values = [x.strip() for x in levels_input.split(",")]
403
+ if not values or all(x == "" for x in values):
404
  raise ValueError("At least one regularization level is required")
405
+
406
+ if any(x == "" for x in values):
407
+ raise ValueError("Regularization levels cannot contain empty values")
408
+
409
+ try:
410
+ values = [float(x) for x in values]
411
+ except ValueError:
412
+ raise ValueError("Level values must be valid numbers")
413
+
414
  return values
415
 
416
  @staticmethod