joel-woodfield commited on
Commit
0562a88
·
1 Parent(s): c8ba6d6

Add ability to change how x points are selected

Browse files
Files changed (1) hide show
  1. dataset.py +35 -17
dataset.py CHANGED
@@ -26,21 +26,18 @@ def get_function(function, x1lim, x2lim, nsample=100):
26
  return mesh_x1, mesh_x2, y
27
 
28
 
29
- def get_data_points(function, x1lim, x2lim, nsample=10, sigma=0., seed=0):
30
- num_points_to_generate = 100
31
- if nsample > num_points_to_generate:
32
- raise ValueError(f"nsample too large, limit to {num_points_to_generate}")
33
-
34
- rng = np.random.default_rng(seed)
35
- x1 = rng.uniform(x1lim[0], x1lim[1], size=num_points_to_generate)
36
- x1 = x1[:nsample]
37
- # Not sure why I put sorting here...
38
- # x1 = np.sort(x1)
39
-
40
- x2 = rng.uniform(x2lim[0], x2lim[1], size=num_points_to_generate)
41
- x2 = x2[:nsample]
42
- # Not sure why I put sorting here...
43
- # x2 = np.sort(x2)
44
 
45
  rng = np.random.default_rng(seed)
46
  noise = sigma * rng.standard_normal(nsample)
@@ -58,11 +55,12 @@ class Dataset:
58
  def __init__(
59
  self,
60
  mode: str = "generate",
61
- function: str = "25 * x1 + 50 * x2",
62
  x1lim: tuple[float, float] = (-1, 1),
63
  x2lim: tuple[float, float] = (-1, 1),
64
  nsample: int = 100,
65
- sigma: float = 0.0,
 
66
  seed: int = 0,
67
  csv_path: str | None = None,
68
  ):
@@ -73,6 +71,7 @@ class Dataset:
73
  self.x2lim = x2lim
74
  self.nsample = nsample
75
  self.sigma = sigma
 
76
  self.seed = seed
77
 
78
  self.csv_path = csv_path
@@ -95,6 +94,7 @@ class Dataset:
95
  x2lim=self.x2lim,
96
  nsample=self.nsample,
97
  sigma=self.sigma,
 
98
  seed=self.seed,
99
  )
100
 
@@ -121,6 +121,7 @@ class Dataset:
121
  x2lim=kwargs.get("x2lim", self.x2lim),
122
  nsample=kwargs.get("nsample", self.nsample),
123
  sigma=kwargs.get("sigma", self.sigma),
 
124
  seed=kwargs.get("seed", self.seed),
125
  csv_path=kwargs.get("csv_path", self.csv_path),
126
  )
@@ -142,6 +143,7 @@ class Dataset:
142
  self._safe_hash(self.x2lim[1]),
143
  self.nsample,
144
  self.sigma,
 
145
  self.seed,
146
  self.csv_path,
147
  )
@@ -201,6 +203,12 @@ class DatasetView:
201
 
202
  return state
203
 
 
 
 
 
 
 
204
  def upload_csv(self, file, state):
205
  try:
206
  state = state.update(
@@ -280,6 +288,11 @@ class DatasetView:
280
  value=f"{options.x2lim[0]}, {options.x2lim[1]}",
281
  interactive=True,
282
  )
 
 
 
 
 
283
 
284
  with gr.Row():
285
  sigma = gr.Number(
@@ -320,6 +333,11 @@ class DatasetView:
320
  inputs=[x2_textbox, state],
321
  outputs=[state],
322
  )
 
 
 
 
 
323
  sigma.submit(
324
  lambda sig, s: s.update(sigma=sig),
325
  inputs=[sigma, state],
 
26
  return mesh_x1, mesh_x2, y
27
 
28
 
29
+ def get_data_points(function, x1lim, x2lim, nsample=10, sigma=0., random_x=False, seed=0):
30
+ if random_x:
31
+ rng = np.random.default_rng(seed)
32
+ x1 = rng.uniform(x1lim[0], x1lim[1], size=nsample)
33
+ x2 = rng.uniform(x2lim[0], x2lim[1], size=nsample)
34
+ else:
35
+ size = int(np.ceil(np.sqrt(nsample)))
36
+ x1 = np.linspace(x1lim[0], x1lim[1], size)
37
+ x2 = np.linspace(x2lim[0], x2lim[1], size)
38
+ x1, x2 = np.meshgrid(x1, x2)
39
+ x1 = x1.ravel()[:nsample]
40
+ x2 = x2.ravel()[:nsample]
 
 
 
41
 
42
  rng = np.random.default_rng(seed)
43
  noise = sigma * rng.standard_normal(nsample)
 
55
  def __init__(
56
  self,
57
  mode: str = "generate",
58
+ function: str = "25 * x1 + 30 * x2",
59
  x1lim: tuple[float, float] = (-1, 1),
60
  x2lim: tuple[float, float] = (-1, 1),
61
  nsample: int = 100,
62
+ sigma: float = 0.1,
63
+ random_x: bool = False,
64
  seed: int = 0,
65
  csv_path: str | None = None,
66
  ):
 
71
  self.x2lim = x2lim
72
  self.nsample = nsample
73
  self.sigma = sigma
74
+ self.random_x = random_x
75
  self.seed = seed
76
 
77
  self.csv_path = csv_path
 
94
  x2lim=self.x2lim,
95
  nsample=self.nsample,
96
  sigma=self.sigma,
97
+ random_x=self.random_x,
98
  seed=self.seed,
99
  )
100
 
 
121
  x2lim=kwargs.get("x2lim", self.x2lim),
122
  nsample=kwargs.get("nsample", self.nsample),
123
  sigma=kwargs.get("sigma", self.sigma),
124
+ random_x=kwargs.get("random_x", self.random_x),
125
  seed=kwargs.get("seed", self.seed),
126
  csv_path=kwargs.get("csv_path", self.csv_path),
127
  )
 
143
  self._safe_hash(self.x2lim[1]),
144
  self.nsample,
145
  self.sigma,
146
+ self.random_x,
147
  self.seed,
148
  self.csv_path,
149
  )
 
203
 
204
  return state
205
 
206
+ def update_x_selection_method(self, method: str, state: gr.State):
207
+ random_x = method == "Uniformly sampled"
208
+ print("Updating random_x to", random_x)
209
+ state = state.update(random_x=random_x)
210
+ return state
211
+
212
  def upload_csv(self, file, state):
213
  try:
214
  state = state.update(
 
288
  value=f"{options.x2lim[0]}, {options.x2lim[1]}",
289
  interactive=True,
290
  )
291
+ x_selection_method = gr.Radio(
292
+ label="How to select x points",
293
+ choices=["Evenly spaced", "Uniformly sampled"],
294
+ value="Evenly spaced",
295
+ )
296
 
297
  with gr.Row():
298
  sigma = gr.Number(
 
333
  inputs=[x2_textbox, state],
334
  outputs=[state],
335
  )
336
+ x_selection_method.change(
337
+ fn=self.update_x_selection_method,
338
+ inputs=[x_selection_method, state],
339
+ outputs=[state],
340
+ )
341
  sigma.submit(
342
  lambda sig, s: s.update(sigma=sig),
343
  inputs=[sigma, state],