pepflow 0.1.4__py3-none-any.whl → 0.1.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pepflow/point_test.py CHANGED
@@ -24,10 +24,9 @@ import numpy as np
24
24
  import pytest
25
25
 
26
26
  from pepflow import expression_manager as exm
27
- from pepflow import function as fc
28
27
  from pepflow import pep as pep
29
28
  from pepflow import pep_context as pc
30
- from pepflow import point, scalar
29
+ from pepflow import point
31
30
 
32
31
 
33
32
  @pytest.fixture
@@ -103,12 +102,6 @@ def test_point_hash_different(pep_context: pc.PEPContext) -> None:
103
102
  assert p1.uid != p2.uid
104
103
 
105
104
 
106
- def test_scalar_hash_different(pep_context: pc.PEPContext) -> None:
107
- s1 = scalar.Scalar(is_basis=True, eval_expression=None)
108
- s2 = scalar.Scalar(is_basis=True, eval_expression=None)
109
- assert s1.uid != s2.uid
110
-
111
-
112
105
  def test_point_tag(pep_context: pc.PEPContext) -> None:
113
106
  p1 = point.Point(is_basis=True, eval_expression=None)
114
107
  p1.add_tag(tag="my_tag")
@@ -123,202 +116,61 @@ def test_point_repr(pep_context: pc.PEPContext) -> None:
123
116
  assert str(p1) == "my_tag"
124
117
 
125
118
 
126
- def test_scalar_tag():
127
- pep_builder = pep.PEPBuilder()
128
- with pep_builder.make_context("test"):
129
- s1 = scalar.Scalar(is_basis=True, eval_expression=None)
130
- s1.add_tag(tag="my_tag")
131
- assert s1.tags == ["my_tag"]
132
- assert s1.tag == "my_tag"
133
-
134
-
135
- def test_scalar_repr():
136
- pep_builder = pep.PEPBuilder()
137
- with pep_builder.make_context("test"):
138
- s1 = scalar.Scalar(is_basis=True, tags=["s1"])
139
- print(s1) # it should be fine without tag
140
- s1.add_tag("my_tag")
141
- assert str(s1) == "my_tag"
142
-
143
-
144
- def test_point_in_a_list():
145
- pep_builder = pep.PEPBuilder()
146
- with pep_builder.make_context("test"):
147
- p1 = point.Point(is_basis=True, eval_expression=None)
148
- p2 = point.Point(is_basis=True, eval_expression=None)
149
- p3 = point.Point(is_basis=True, eval_expression=None)
119
+ def test_point_in_a_list(pep_context: pc.PEPContext):
120
+ p1 = point.Point(is_basis=True, eval_expression=None)
121
+ p2 = point.Point(is_basis=True, eval_expression=None)
122
+ p3 = point.Point(is_basis=True, eval_expression=None)
150
123
  assert p1 in [p1, p2]
151
124
  assert p3 not in [p1, p2]
152
125
 
153
126
 
154
- def test_scalar_in_a_list():
155
- pep_builder = pep.PEPBuilder()
156
- with pep_builder.make_context("test"):
157
- s1 = scalar.Scalar(is_basis=True, eval_expression=None)
158
- s2 = scalar.Scalar(is_basis=True, eval_expression=None)
159
- s3 = scalar.Scalar(is_basis=True, eval_expression=None)
160
- assert s1 in [s1, s2]
161
- assert s3 not in [s1, s2]
162
-
163
-
164
- def test_expression_manager_on_basis_point():
165
- pep_builder = pep.PEPBuilder()
166
- with pep_builder.make_context("test") as ctx:
167
- p1 = point.Point(is_basis=True, eval_expression=None, tags=["p1"])
168
- p2 = point.Point(is_basis=True, eval_expression=None, tags=["p2"])
169
- pm = exm.ExpressionManager(ctx)
127
+ def test_expression_manager_on_basis_point(pep_context: pc.PEPContext):
128
+ p1 = point.Point(is_basis=True, eval_expression=None, tags=["p1"])
129
+ p2 = point.Point(is_basis=True, eval_expression=None, tags=["p2"])
130
+ pm = exm.ExpressionManager(pep_context)
170
131
 
171
- np.testing.assert_allclose(pm.eval_point(p1).vector, np.array([1, 0]))
172
- np.testing.assert_allclose(pm.eval_point(p2).vector, np.array([0, 1]))
132
+ np.testing.assert_allclose(pm.eval_point(p1).vector, np.array([1, 0]))
133
+ np.testing.assert_allclose(pm.eval_point(p2).vector, np.array([0, 1]))
173
134
 
174
- p3 = point.Point(is_basis=True, eval_expression=None, tags=["p3"]) # noqa: F841
175
- pm = exm.ExpressionManager(ctx)
135
+ p3 = point.Point(is_basis=True, eval_expression=None, tags=["p3"]) # noqa: F841
136
+ pm = exm.ExpressionManager(pep_context)
176
137
 
177
138
  np.testing.assert_allclose(pm.eval_point(p1).vector, np.array([1, 0, 0]))
178
139
  np.testing.assert_allclose(pm.eval_point(p2).vector, np.array([0, 1, 0]))
179
140
 
180
141
 
181
- def test_expression_manager_on_basis_scalar():
182
- pep_builder = pep.PEPBuilder()
183
- with pep_builder.make_context("test") as ctx:
184
- s1 = scalar.Scalar(is_basis=True, eval_expression=None, tags=["s1"])
185
- s2 = scalar.Scalar(is_basis=True, eval_expression=None, tags=["s2"])
186
- pm = exm.ExpressionManager(ctx)
142
+ def test_expression_manager_eval_point(pep_context: pc.PEPContext):
143
+ p1 = point.Point(is_basis=True, tags=["p1"])
144
+ p2 = point.Point(is_basis=True, tags=["p2"])
145
+ p3 = 2 * p1 + p2 / 4
146
+ p4 = p3 + p1
187
147
 
188
- np.testing.assert_allclose(pm.eval_scalar(s1).vector, np.array([1, 0]))
189
- np.testing.assert_allclose(pm.eval_scalar(s2).vector, np.array([0, 1]))
148
+ pm = exm.ExpressionManager(pep_context)
149
+ np.testing.assert_allclose(pm.eval_point(p3).vector, np.array([2, 0.25]))
150
+ np.testing.assert_allclose(pm.eval_point(p4).vector, np.array([3, 0.25]))
190
151
 
191
- s3 = scalar.Scalar(is_basis=True, eval_expression=None, tags=["s3"]) # noqa: F841
192
- pm = exm.ExpressionManager(ctx)
193
152
 
194
- np.testing.assert_allclose(pm.eval_scalar(s1).vector, np.array([1, 0, 0]))
195
- np.testing.assert_allclose(pm.eval_scalar(s2).vector, np.array([0, 1, 0]))
153
+ def test_expression_manager_eval_point_large_scale(pep_context):
154
+ all_basis = [point.Point(is_basis=True, tags=[f"p_{i}"]) for i in range(100)]
155
+ p = all_basis[0]
156
+ for i in range(len(all_basis)):
157
+ for j in range(i + 1, len(all_basis)):
158
+ p += all_basis[i] * 2 + all_basis[j]
159
+ pm = exm.ExpressionManager(pep_context)
160
+ t = time.time()
161
+ for pp in pep_context.points:
162
+ pm.eval_point(pp)
196
163
 
164
+ assert (time.time() - t) < 0.5
197
165
 
198
- def test_expression_manager_eval_point():
199
- pep_builder = pep.PEPBuilder()
200
- with pep_builder.make_context("test") as ctx:
201
- p1 = point.Point(is_basis=True, tags=["p1"])
202
- p2 = point.Point(is_basis=True, tags=["p2"])
203
- p3 = 2 * p1 + p2 / 4
204
- p4 = p3 + p1
205
166
 
206
- pm = exm.ExpressionManager(ctx)
207
- np.testing.assert_allclose(pm.eval_point(p3).vector, np.array([2, 0.25]))
208
- np.testing.assert_allclose(pm.eval_point(p4).vector, np.array([3, 0.25]))
167
+ def test_zero_point(pep_context):
168
+ _ = point.Point(is_basis=True, tags=["p1"])
169
+ p0 = point.Point.zero()
209
170
 
171
+ pm = exm.ExpressionManager(pep_context)
172
+ np.testing.assert_allclose(pm.eval_point(p0).vector, np.array([0]))
210
173
 
211
- def test_expression_manager_eval_point_large_scale():
212
- pep_builder = pep.PEPBuilder()
213
- with pep_builder.make_context("test") as ctx:
214
- all_basis = [point.Point(is_basis=True, tags=[f"p_{i}"]) for i in range(100)]
215
- p = all_basis[0]
216
- for i in range(len(all_basis)):
217
- for j in range(i + 1, len(all_basis)):
218
- p += all_basis[i] * 2 + all_basis[j]
219
- pm = exm.ExpressionManager(ctx)
220
- t = time.time()
221
- for pp in ctx.points:
222
- pm.eval_point(pp)
223
-
224
- assert (time.time() - t) < 0.5
225
-
226
-
227
- def test_function_generate_triplet():
228
- pep_builder = pep.PEPBuilder()
229
- with pep_builder.make_context("test") as ctx:
230
- f = fc.Function(is_basis=True, reuse_gradient=True)
231
- f.add_tag("f")
232
- g = fc.Function(is_basis=True, reuse_gradient=True)
233
- g.add_tag("g")
234
- h = 5 * f + 5 * g
235
- h.add_tag("h")
236
-
237
- f1 = fc.Function(is_basis=True, reuse_gradient=False)
238
- f1.add_tag("f1")
239
- g1 = fc.Function(is_basis=True, reuse_gradient=False)
240
- g1.add_tag("g1")
241
- h1 = 5 * f1 + 5 * g1
242
- h1.add_tag("h1")
243
-
244
- p1 = point.Point(is_basis=True)
245
- p1.add_tag("p1")
246
- p1_triplet = h.generate_triplet(p1)
247
- p1_triplet_1 = h.generate_triplet(p1)
248
-
249
- p1_triplet_2 = h1.generate_triplet(p1)
250
- p1_triplet_3 = h1.generate_triplet(p1)
251
-
252
- pm = exm.ExpressionManager(ctx)
253
-
254
- np.testing.assert_allclose(
255
- pm.eval_point(p1).vector, np.array([1, 0, 0, 0, 0, 0, 0])
256
- )
257
-
258
- np.testing.assert_allclose(
259
- pm.eval_point(p1_triplet.gradient).vector, np.array([0, 5, 5, 0, 0, 0, 0])
260
- )
261
- np.testing.assert_allclose(
262
- pm.eval_scalar(p1_triplet.function_value).vector, np.array([5, 5, 0, 0])
263
- )
264
-
265
- np.testing.assert_allclose(
266
- pm.eval_point(p1_triplet_1.gradient).vector, np.array([0, 5, 5, 0, 0, 0, 0])
267
- )
268
- np.testing.assert_allclose(
269
- pm.eval_scalar(p1_triplet_1.function_value).vector, np.array([5, 5, 0, 0])
270
- )
271
-
272
- np.testing.assert_allclose(
273
- pm.eval_point(p1_triplet_2.gradient).vector, np.array([0, 0, 0, 5, 5, 0, 0])
274
- )
275
- np.testing.assert_allclose(
276
- pm.eval_scalar(p1_triplet_2.function_value).vector, np.array([0, 0, 5, 5])
277
- )
278
-
279
- np.testing.assert_allclose(
280
- pm.eval_point(p1_triplet_3.gradient).vector, np.array([0, 0, 0, 0, 0, 5, 5])
281
- )
282
- np.testing.assert_allclose(
283
- pm.eval_scalar(p1_triplet_3.function_value).vector, np.array([0, 0, 5, 5])
284
- )
285
-
286
-
287
- def test_function_add_stationary_point():
288
- pep_builder = pep.PEPBuilder()
289
- with pep_builder.make_context("test") as ctx:
290
- f = fc.Function(is_basis=True, reuse_gradient=True)
291
- f.add_tag("f")
292
- x_opt = f.add_stationary_point("x_opt")
293
-
294
- pm = exm.ExpressionManager(ctx)
295
-
296
- np.testing.assert_allclose(pm.eval_point(x_opt).vector, np.array([1]))
297
-
298
-
299
- def test_smooth_interpolability_constraints():
300
- pep_builder = pep.PEPBuilder()
301
- with pep_builder.make_context("test") as ctx:
302
- f = fc.SmoothConvexFunction(L=1)
303
- f.add_tag("f")
304
- _ = f.add_stationary_point("x_opt")
305
-
306
- x_0 = point.Point(is_basis=True)
307
- x_0.add_tag("x_0")
308
- _ = f.generate_triplet(x_0)
309
-
310
- all_interpolation_constraints = f.get_interpolation_constraints()
311
-
312
- pm = exm.ExpressionManager(ctx)
313
-
314
- np.testing.assert_allclose(
315
- pm.eval_scalar(all_interpolation_constraints[1].scalar).vector, [1, -1]
316
- )
317
- np.testing.assert_allclose(
318
- pm.eval_scalar(all_interpolation_constraints[1].scalar).matrix,
319
- [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.5]],
320
- )
321
-
322
- np.testing.assert_allclose(
323
- pm.eval_scalar(all_interpolation_constraints[1].scalar).constant, 0
324
- )
174
+ _ = point.Point(is_basis=True, tags=["p2"])
175
+ pm = exm.ExpressionManager(pep_context)
176
+ np.testing.assert_allclose(pm.eval_point(p0).vector, np.array([0, 0]))