-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathtest_linear_expression.py
More file actions
1028 lines (757 loc) · 30.2 KB
/
test_linear_expression.py
File metadata and controls
1028 lines (757 loc) · 30.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Created on Wed Mar 17 17:06:36 2021.
@author: fabian
"""
import numpy as np
import pandas as pd
import polars as pl
import pytest
import xarray as xr
from xarray.testing import assert_equal
from linopy import LinearExpression, Model, merge
from linopy.constants import HELPER_DIMS, TERM_DIM
from linopy.testing import assert_linequal
@pytest.fixture
def m():
m = Model()
m.add_variables(pd.Series([0, 0]), 1, name="x")
m.add_variables(4, pd.Series([8, 10]), name="y")
m.add_variables(0, pd.DataFrame([[1, 2], [3, 4], [5, 6]]).T, name="z")
m.add_variables(coords=[pd.RangeIndex(20, name="dim_2")], name="v")
idx = pd.MultiIndex.from_product([[1, 2], ["a", "b"]], names=("level1", "level2"))
idx.name = "dim_3"
m.add_variables(coords=[idx], name="u")
return m
@pytest.fixture
def x(m):
return m.variables["x"]
@pytest.fixture
def y(m):
return m.variables["y"]
@pytest.fixture
def z(m):
return m.variables["z"]
@pytest.fixture
def v(m):
return m.variables["v"]
@pytest.fixture
def u(m):
return m.variables["u"]
def test_empty_linexpr(m):
LinearExpression(None, m)
def test_linexpr_with_wrong_data(m):
with pytest.raises(ValueError):
LinearExpression(xr.Dataset({"a": [1]}), m)
coeffs = xr.DataArray([1, 2], dims=["a"])
vars = xr.DataArray([1, 2], dims=["a"])
data = xr.Dataset({"coeffs": coeffs, "vars": vars})
with pytest.raises(ValueError):
LinearExpression(data, m)
# with model as None
coeffs = xr.DataArray(np.array([1, 2]), dims=[TERM_DIM])
vars = xr.DataArray(np.array([1, 2]), dims=[TERM_DIM])
data = xr.Dataset({"coeffs": coeffs, "vars": vars})
with pytest.raises(ValueError):
LinearExpression(data, None)
def test_linexpr_with_helper_dims_as_coords(m):
coords = [pd.Index([0], name="a"), pd.Index([1, 2], name=TERM_DIM)]
coeffs = xr.DataArray(np.array([[1, 2]]), coords=coords)
vars = xr.DataArray(np.array([[1, 2]]), coords=coords)
data = xr.Dataset({"coeffs": coeffs, "vars": vars})
assert set(HELPER_DIMS).intersection(set(data.coords))
expr = LinearExpression(data, m)
assert not set(HELPER_DIMS).intersection(set(expr.data.coords))
def test_linexpr_with_data_without_coords(m):
lhs = 1 * m["x"]
vars = xr.DataArray(lhs.vars.values, dims=["dim_0", TERM_DIM])
coeffs = xr.DataArray(lhs.coeffs.values, dims=["dim_0", TERM_DIM])
data = xr.Dataset({"vars": vars, "coeffs": coeffs})
expr = LinearExpression(data, m)
assert_linequal(expr, lhs)
def test_linexpr_from_constant_dataarray(m):
const = xr.DataArray([1, 2], dims=["dim_0"])
expr = LinearExpression(const, m)
assert (expr.const == const).all()
assert expr.nterm == 0
def test_linexpr_from_constant_pandas_series(m):
const = pd.Series([1, 2], index=pd.RangeIndex(2, name="dim_0"))
expr = LinearExpression(const, m)
assert (expr.const == const).all()
assert expr.nterm == 0
def test_linexpr_from_constant_pandas_dataframe(m):
const = pd.DataFrame([[1, 2], [3, 4]], columns=["a", "b"])
expr = LinearExpression(const, m)
assert (expr.const == const).all()
assert expr.nterm == 0
def test_linexpr_from_constant_numpy_array(m):
const = np.array([1, 2])
expr = LinearExpression(const, m)
assert (expr.const == const).all()
assert expr.nterm == 0
def test_linexpr_from_constant_scalar(m):
const = 1
expr = LinearExpression(const, m)
assert (expr.const == const).all()
assert expr.nterm == 0
def test_repr(m):
expr = m.linexpr((10, "x"), (1, "y"))
expr.__repr__()
def test_fill_value():
isinstance(LinearExpression._fill_value, dict)
with pytest.warns(DeprecationWarning):
LinearExpression.fill_value
def test_linexpr_with_scalars(m):
expr = m.linexpr((10, "x"), (1, "y"))
target = xr.DataArray(
[[10, 1], [10, 1]], coords={"dim_0": [0, 1]}, dims=["dim_0", TERM_DIM]
)
assert_equal(expr.coeffs, target)
def test_linexpr_with_series(m, v):
lhs = pd.Series(np.arange(20)), v
expr = m.linexpr(lhs)
isinstance(expr, LinearExpression)
def test_linexpr_with_dataframe(m, z):
lhs = pd.DataFrame(z.labels), z
expr = m.linexpr(lhs)
isinstance(expr, LinearExpression)
def test_linexpr_duplicated_index(m):
expr = m.linexpr((10, "x"), (-1, "x"))
assert (expr.data._term == [0, 1]).all()
def test_linear_expression_with_multiplication(x):
expr = 1 * x
assert isinstance(expr, LinearExpression)
assert expr.nterm == 1
assert len(expr.vars.dim_0) == x.shape[0]
expr = x * 1
assert isinstance(expr, LinearExpression)
expr2 = x.mul(1)
assert_linequal(expr, expr2)
expr = x / 1
assert isinstance(expr, LinearExpression)
expr = x / 1.0
assert isinstance(expr, LinearExpression)
expr2 = x.div(1)
assert_linequal(expr, expr2)
expr = np.array([1, 2]) * x
assert isinstance(expr, LinearExpression)
expr = xr.DataArray(np.array([[1, 2], [2, 3]])) * x
assert isinstance(expr, LinearExpression)
expr = pd.Series([1, 2], index=pd.RangeIndex(2, name="dim_0")) * x
assert isinstance(expr, LinearExpression)
def test_linear_expression_with_addition(m, x, y):
expr = 10 * x + y
assert isinstance(expr, LinearExpression)
assert_linequal(expr, m.linexpr((10, "x"), (1, "y")))
expr = x + 8 * y
assert isinstance(expr, LinearExpression)
assert_linequal(expr, m.linexpr((1, "x"), (8, "y")))
expr = x + y
assert isinstance(expr, LinearExpression)
assert_linequal(expr, m.linexpr((1, "x"), (1, "y")))
expr2 = x.add(y)
assert_linequal(expr, expr2)
def test_linear_expression_with_subtraction(m, x, y):
expr = x - y
assert isinstance(expr, LinearExpression)
assert_linequal(expr, m.linexpr((1, "x"), (-1, "y")))
expr2 = x.sub(y)
assert_linequal(expr, expr2)
expr = -x - 8 * y
assert isinstance(expr, LinearExpression)
assert_linequal(expr, m.linexpr((-1, "x"), (-8, "y")))
def test_linear_expression_with_constant(m, x, y):
expr = x + 1
assert isinstance(expr, LinearExpression)
assert (expr.const == 1).all()
expr = -x - 8 * y - 10
assert isinstance(expr, LinearExpression)
assert (expr.const == -10).all()
assert expr.nterm == 2
def test_linear_expression_with_constant_multiplication(m, x, y):
expr = x + 1
obs = expr * 10
assert isinstance(obs, LinearExpression)
assert (obs.const == 10).all()
obs = expr * pd.Series([1, 2, 3], index=pd.RangeIndex(3, name="new_dim"))
assert isinstance(obs, LinearExpression)
assert obs.shape == (2, 3, 1)
def test_linear_expression_multi_indexed(u):
expr = 3 * u + 1 * u
assert isinstance(expr, LinearExpression)
def test_linear_expression_with_errors(m, x):
with pytest.raises(TypeError):
x / x
with pytest.raises(TypeError):
x / (1 * x)
with pytest.raises(TypeError):
m.linexpr((10, x.labels), (1, "y"))
def test_linear_expression_from_rule(m, x, y):
def bound(m, i):
return (
(i - 1) * x.at[i - 1] + y.at[i] + 1 * x.at[i]
if i == 1
else i * x.at[i] - y.at[i]
)
expr = LinearExpression.from_rule(m, bound, x.coords)
assert isinstance(expr, LinearExpression)
assert expr.nterm == 3
repr(expr) # test repr
def test_linear_expression_from_rule_with_return_none(m, x, y):
# with return type None
def bound(m, i):
if i == 1:
return (i - 1) * x.at[i - 1] + y.at[i]
expr = LinearExpression.from_rule(m, bound, x.coords)
assert isinstance(expr, LinearExpression)
assert (expr.vars[0] == -1).all()
assert (expr.vars[1] != -1).all()
assert expr.coeffs[0].isnull().all()
assert expr.coeffs[1].notnull().all()
repr(expr) # test repr
def test_linear_expression_addition(x, y, z):
expr = 10 * x + y
other = 2 * y + z
res = expr + other
assert res.nterm == expr.nterm + other.nterm
assert (res.coords["dim_0"] == expr.coords["dim_0"]).all()
assert (res.coords["dim_1"] == other.coords["dim_1"]).all()
assert res.data.notnull().all().to_array().all()
assert isinstance(x - expr, LinearExpression)
assert isinstance(x + expr, LinearExpression)
def test_linear_expression_addition_with_constant(x, y, z):
expr = 10 * x + y + 10
assert (expr.const == 10).all()
expr = 10 * x + y + np.array([2, 3])
assert list(expr.const) == [2, 3]
expr = 10 * x + y + pd.Series([2, 3])
assert list(expr.const) == [2, 3]
def test_linear_expression_subtraction(x, y, z):
expr = 10 * x + y - 10
assert (expr.const == -10).all()
expr = 10 * x + y - np.array([2, 3])
assert list(expr.const) == [-2, -3]
expr = 10 * x + y - pd.Series([2, 3])
assert list(expr.const) == [-2, -3]
def test_linear_expression_substraction(x, y, z, v):
expr = 10 * x + y
other = 2 * y - z
res = expr - other
assert res.nterm == expr.nterm + other.nterm
assert (res.coords["dim_0"] == expr.coords["dim_0"]).all()
assert (res.coords["dim_1"] == other.coords["dim_1"]).all()
assert res.data.notnull().all().to_array().all()
def test_linear_expression_sum(x, y, z, v):
expr = 10 * x + y + z
res = expr.sum("dim_0")
assert res.size == expr.size
assert res.nterm == expr.nterm * len(expr.data.dim_0)
res = expr.sum()
assert res.size == expr.size
assert res.nterm == expr.size
assert res.data.notnull().all().to_array().all()
assert_linequal(expr.sum(["dim_0", TERM_DIM]), expr.sum("dim_0"))
# test special case otherride coords
expr = v.loc[:9] + v.loc[10:]
assert expr.nterm == 2
assert len(expr.coords["dim_2"]) == 10
def test_linear_expression_sum_with_const(x, y, z, v):
expr = 10 * x + y + z + 10
res = expr.sum("dim_0")
assert res.size == expr.size
assert res.nterm == expr.nterm * len(expr.data.dim_0)
assert (res.const == 20).all()
res = expr.sum()
assert res.size == expr.size
assert res.nterm == expr.size
assert res.data.notnull().all().to_array().all()
assert (res.const == 60).item()
assert_linequal(expr.sum(["dim_0", TERM_DIM]), expr.sum("dim_0"))
# test special case otherride coords
expr = v.loc[:9] + v.loc[10:]
assert expr.nterm == 2
assert len(expr.coords["dim_2"]) == 10
def test_linear_expression_sum_drop_zeros(z):
coeff = xr.zeros_like(z.labels)
coeff[1, 0] = 3
coeff[0, 2] = 5
expr = coeff * z
res = expr.sum("dim_0", drop_zeros=True)
assert res.nterm == 1
res = expr.sum("dim_1", drop_zeros=True)
assert res.nterm == 1
coeff[1, 2] = 4
expr.data["coeffs"] = coeff
res = expr.sum()
res = expr.sum("dim_0", drop_zeros=True)
assert res.nterm == 2
res = expr.sum("dim_1", drop_zeros=True)
assert res.nterm == 2
def test_linear_expression_sum_warn_using_dims(z):
with pytest.warns(DeprecationWarning):
(1 * z).sum(dims="dim_0")
def test_linear_expression_sum_warn_unknown_kwargs(z):
with pytest.raises(ValueError):
(1 * z).sum(unknown_kwarg="dim_0")
def test_linear_expression_multiplication(x, y, z):
expr = 10 * x + y + z
mexpr = expr * 10
assert (mexpr.coeffs.sel(dim_1=0, dim_0=0, _term=0) == 100).item()
mexpr = 10 * expr
assert (mexpr.coeffs.sel(dim_1=0, dim_0=0, _term=0) == 100).item()
mexpr = expr / 100
assert (mexpr.coeffs.sel(dim_1=0, dim_0=0, _term=0) == 1 / 10).item()
mexpr = expr / 100.0
assert (mexpr.coeffs.sel(dim_1=0, dim_0=0, _term=0) == 1 / 10).item()
def test_matmul_variable_and_const(x, y):
const = np.array([1, 2])
expr = x @ const
assert expr.nterm == 2
assert_linequal(expr, (x * const).sum())
assert_linequal(x @ const, (x * const).sum())
assert_linequal(x.dot(const), x @ const)
def test_matmul_expr_and_const(x, y):
expr = 10 * x + y
const = np.array([1, 2])
res = expr @ const
target = (10 * x) @ const + y @ const
assert res.nterm == 4
assert_linequal(res, target)
assert_linequal(expr.dot(const), target)
def test_matmul_wrong_input(x, y, z):
expr = 10 * x + y + z
with pytest.raises(TypeError):
expr @ expr
def test_linear_expression_multiplication_invalid(x, y, z):
expr = 10 * x + y + z
with pytest.raises(TypeError):
expr = 10 * x + y + z
expr * expr
with pytest.raises(TypeError):
expr = 10 * x + y + z
expr / x
def test_expression_inherited_properties(x, y):
expr = 10 * x + y
assert isinstance(expr.attrs, dict)
assert isinstance(expr.coords, xr.Coordinates)
assert isinstance(expr.indexes, xr.core.indexes.Indexes)
assert isinstance(expr.sizes, xr.core.utils.Frozen)
def test_linear_expression_getitem_single(x, y):
expr = 10 * x + y + 3
sel = expr[0]
assert isinstance(sel, LinearExpression)
assert sel.nterm == 2
# one expression with two terms (constant is not counted)
assert sel.size == 2
def test_linear_expression_getitem_slice(x, y):
expr = 10 * x + y + 3
sel = expr[:1]
assert isinstance(sel, LinearExpression)
assert sel.nterm == 2
# one expression with two terms (constant is not counted)
assert sel.size == 2
def test_linear_expression_getitem_list(x, y, z):
expr = 10 * x + z + 10
sel = expr[:, [0, 2]]
assert isinstance(sel, LinearExpression)
assert sel.nterm == 2
# four expressions with two terms (constant is not counted)
assert sel.size == 8
def test_linear_expression_loc(x, y):
expr = x + y
assert expr.loc[0].size < expr.loc[:5].size
def test_linear_expression_isnull(v):
expr = np.arange(20) * v
filter = (expr.coeffs >= 10).any(TERM_DIM)
expr = expr.where(filter)
assert expr.isnull().sum() == 10
def test_linear_expression_flat(v):
coeff = np.arange(1, 21) # use non-zero coefficients
expr = coeff * v
df = expr.flat
assert isinstance(df, pd.DataFrame)
assert (df.coeffs == coeff).all()
def test_iterate_slices(x, y):
expr = x + 10 * y
for s in expr.iterate_slices(slice_size=2):
assert isinstance(s, LinearExpression)
assert s.nterm == expr.nterm
assert s.coord_dims == expr.coord_dims
def test_linear_expression_to_polars(v):
coeff = np.arange(1, 21) # use non-zero coefficients
expr = coeff * v
df = expr.to_polars()
assert isinstance(df, pl.DataFrame)
assert (df["coeffs"].to_numpy() == coeff).all()
def test_linear_expression_where(v):
expr = np.arange(20) * v
filter = (expr.coeffs >= 10).any(TERM_DIM)
expr = expr.where(filter)
assert isinstance(expr, LinearExpression)
assert expr.nterm == 1
expr = np.arange(20) * v
expr = expr.where(filter, drop=True).sum()
assert isinstance(expr, LinearExpression)
assert expr.nterm == 10
def test_linear_expression_where_with_const(v):
expr = np.arange(20) * v + 10
filter = (expr.coeffs >= 10).any(TERM_DIM)
expr = expr.where(filter)
assert isinstance(expr, LinearExpression)
assert expr.nterm == 1
assert expr.const[:10].isnull().all()
assert (expr.const[10:] == 10).all()
expr = np.arange(20) * v + 10
expr = expr.where(filter, drop=True).sum()
assert isinstance(expr, LinearExpression)
assert expr.nterm == 10
assert expr.const == 100
def test_linear_expression_where_scalar_fill_value(v):
expr = np.arange(20) * v + 10
filter = (expr.coeffs >= 10).any(TERM_DIM)
expr = expr.where(filter, 200)
assert isinstance(expr, LinearExpression)
assert expr.nterm == 1
assert (expr.const[:10] == 200).all()
assert (expr.const[10:] == 10).all()
def test_linear_expression_where_array_fill_value(v):
expr = np.arange(20) * v + 10
filter = (expr.coeffs >= 10).any(TERM_DIM)
other = expr.coeffs
expr = expr.where(filter, other)
assert isinstance(expr, LinearExpression)
assert expr.nterm == 1
assert (expr.const[:10] == other[:10]).all()
assert (expr.const[10:] == 10).all()
def test_linear_expression_where_expr_fill_value(v):
expr = np.arange(20) * v + 10
expr2 = np.arange(20) * v + 5
filter = (expr.coeffs >= 10).any(TERM_DIM)
res = expr.where(filter, expr2)
assert isinstance(res, LinearExpression)
assert res.nterm == 1
assert (res.const[:10] == expr2.const[:10]).all()
assert (res.const[10:] == 10).all()
def test_where_with_helper_dim_false(v):
expr = np.arange(20) * v
with pytest.raises(ValueError):
filter = expr.coeffs >= 10
expr.where(filter)
def test_linear_expression_shift(v):
shifted = v.to_linexpr().shift(dim_2=2)
assert shifted.nterm == 1
assert shifted.coeffs.loc[:1].isnull().all()
assert (shifted.vars.loc[:1] == -1).all()
def test_linear_expression_swap_dims(v):
expr = v.to_linexpr()
expr = expr.assign_coords({"second": ("dim_2", expr.indexes["dim_2"] + 100)})
expr = expr.swap_dims({"dim_2": "second"})
assert isinstance(expr, LinearExpression)
assert expr.coord_dims == ("second",)
def test_linear_expression_set_index(v):
expr = v.to_linexpr()
expr = expr.assign_coords({"second": ("dim_2", expr.indexes["dim_2"] + 100)})
expr = expr.set_index({"multi": ["dim_2", "second"]})
assert isinstance(expr, LinearExpression)
assert expr.coord_dims == ("multi",)
assert isinstance(expr.indexes["multi"], pd.MultiIndex)
def test_linear_expression_fillna(v):
expr = np.arange(20) * v + 10
assert expr.const.sum() == 200
filter = (expr.coeffs >= 10).any(TERM_DIM)
filtered = expr.where(filter)
assert isinstance(filtered, LinearExpression)
assert filtered.const.sum() == 100
filled = filtered.fillna(10)
assert isinstance(filled, LinearExpression)
assert filled.const.sum() == 200
assert filled.coeffs.isnull().sum() == 10
def test_variable_expand_dims(v):
result = v.to_linexpr().expand_dims("new_dim")
assert isinstance(result, LinearExpression)
assert result.coord_dims == ("dim_2", "new_dim")
def test_variable_stack(v):
result = v.to_linexpr().expand_dims("new_dim").stack(new=("new_dim", "dim_2"))
assert isinstance(result, LinearExpression)
assert result.coord_dims == ("new",)
def test_linear_expression_unstack(v):
result = v.to_linexpr().expand_dims("new_dim").stack(new=("new_dim", "dim_2"))
result = result.unstack("new")
assert isinstance(result, LinearExpression)
assert result.coord_dims == ("new_dim", "dim_2")
def test_linear_expression_diff(v):
diff = v.to_linexpr().diff("dim_2")
assert diff.nterm == 2
@pytest.mark.parametrize("use_fallback", [True, False])
def test_linear_expression_groupby(v, use_fallback):
expr = 1 * v
dim = v.dims[0]
groups = xr.DataArray([1] * 10 + [2] * 10, coords=v.coords, name=dim)
grouped = expr.groupby(groups).sum(use_fallback=use_fallback)
assert dim in grouped.dims
assert (grouped.data[dim] == [1, 2]).all()
assert grouped.nterm == 10
@pytest.mark.parametrize("use_fallback", [True, False])
def test_linear_expression_groupby_on_same_name_as_target_dim(v, use_fallback):
expr = 1 * v
groups = xr.DataArray([1] * 10 + [2] * 10, coords=v.coords)
grouped = expr.groupby(groups).sum(use_fallback=use_fallback)
assert "group" in grouped.dims
assert (grouped.data.group == [1, 2]).all()
assert grouped.nterm == 10
@pytest.mark.parametrize("use_fallback", [True])
def test_linear_expression_groupby_ndim(z, use_fallback):
# TODO: implement fallback for n-dim groupby, see https://github.com/PyPSA/linopy/issues/299
expr = 1 * z
groups = xr.DataArray([[1, 1, 2], [1, 3, 3]], coords=z.coords)
grouped = expr.groupby(groups).sum(use_fallback=use_fallback)
assert "group" in grouped.dims
# there are three groups, 1, 2 and 3, the largest group has 3 elements
assert (grouped.data.group == [1, 2, 3]).all()
assert grouped.nterm == 3
@pytest.mark.parametrize("use_fallback", [True, False])
def test_linear_expression_groupby_with_name(v, use_fallback):
expr = 1 * v
groups = xr.DataArray([1] * 10 + [2] * 10, coords=v.coords, name="my_group")
grouped = expr.groupby(groups).sum(use_fallback=use_fallback)
assert "my_group" in grouped.dims
assert (grouped.data.my_group == [1, 2]).all()
assert grouped.nterm == 10
@pytest.mark.parametrize("use_fallback", [True, False])
def test_linear_expression_groupby_with_series(v, use_fallback):
expr = 1 * v
groups = pd.Series([1] * 10 + [2] * 10, index=v.indexes["dim_2"])
grouped = expr.groupby(groups).sum(use_fallback=use_fallback)
assert "group" in grouped.dims
assert (grouped.data.group == [1, 2]).all()
assert grouped.nterm == 10
@pytest.mark.parametrize("use_fallback", [True, False])
def test_linear_expression_groupby_series_with_name(v, use_fallback):
expr = 1 * v
groups = pd.Series([1] * 10 + [2] * 10, index=v.indexes[v.dims[0]], name="my_group")
grouped = expr.groupby(groups).sum(use_fallback=use_fallback)
assert "my_group" in grouped.dims
assert (grouped.data.my_group == [1, 2]).all()
assert grouped.nterm == 10
@pytest.mark.parametrize("use_fallback", [True, False])
def test_linear_expression_groupby_with_series_with_same_group_name(v, use_fallback):
"""
Test that the group by works with a series whose name is the same as
the dimension to group.
"""
expr = 1 * v
groups = pd.Series([1] * 10 + [2] * 10, index=v.indexes["dim_2"])
groups.name = "dim_2"
grouped = expr.groupby(groups).sum(use_fallback=use_fallback)
assert "dim_2" in grouped.dims
assert (grouped.data.dim_2 == [1, 2]).all()
assert grouped.nterm == 10
@pytest.mark.parametrize("use_fallback", [True, False])
def test_linear_expression_groupby_with_series_on_multiindex(u, use_fallback):
expr = 1 * u
len_grouped_dim = len(u.data["dim_3"])
groups = pd.Series([1] * len_grouped_dim, index=u.indexes["dim_3"])
grouped = expr.groupby(groups).sum(use_fallback=use_fallback)
assert "group" in grouped.dims
assert (grouped.data.group == [1]).all()
assert grouped.nterm == len_grouped_dim
@pytest.mark.parametrize("use_fallback", [True, False])
def test_linear_expression_groupby_with_dataframe(v, use_fallback):
expr = 1 * v
groups = pd.DataFrame(
{"a": [1] * 10 + [2] * 10, "b": list(range(4)) * 5}, index=v.indexes["dim_2"]
)
if use_fallback:
with pytest.raises(ValueError):
expr.groupby(groups).sum(use_fallback=use_fallback)
return
grouped = expr.groupby(groups).sum(use_fallback=use_fallback)
index = pd.MultiIndex.from_frame(groups)
assert "group" in grouped.dims
assert set(grouped.data.group.values) == set(index.values)
assert grouped.nterm == 3
@pytest.mark.parametrize("use_fallback", [True, False])
def test_linear_expression_groupby_with_dataframe_with_same_group_name(v, use_fallback):
"""
Test that the group by works with a dataframe whose column name is the same as
the dimension to group.
"""
expr = 1 * v
groups = pd.DataFrame(
{"dim_2": [1] * 10 + [2] * 10, "b": list(range(4)) * 5},
index=v.indexes["dim_2"],
)
if use_fallback:
with pytest.raises(ValueError):
expr.groupby(groups).sum(use_fallback=use_fallback)
return
grouped = expr.groupby(groups).sum(use_fallback=use_fallback)
index = pd.MultiIndex.from_frame(groups)
assert "group" in grouped.dims
assert set(grouped.data.group.values) == set(index.values)
assert grouped.nterm == 3
@pytest.mark.parametrize("use_fallback", [True, False])
def test_linear_expression_groupby_with_dataframe_on_multiindex(u, use_fallback):
expr = 1 * u
len_grouped_dim = len(u.data["dim_3"])
groups = pd.DataFrame({"a": [1] * len_grouped_dim}, index=u.indexes["dim_3"])
if use_fallback:
with pytest.raises(ValueError):
expr.groupby(groups).sum(use_fallback=use_fallback)
return
grouped = expr.groupby(groups).sum(use_fallback=use_fallback)
assert "group" in grouped.dims
assert isinstance(grouped.indexes["group"], pd.MultiIndex)
assert grouped.nterm == len_grouped_dim
@pytest.mark.parametrize("use_fallback", [True, False])
def test_linear_expression_groupby_with_dataarray(v, use_fallback):
expr = 1 * v
df = pd.DataFrame(
{"a": [1] * 10 + [2] * 10, "b": list(range(4)) * 5}, index=v.indexes["dim_2"]
)
groups = xr.DataArray(df)
# this should not be the case, see https://github.com/PyPSA/linopy/issues/351
if use_fallback:
with pytest.raises(KeyError):
expr.groupby(groups).sum(use_fallback=use_fallback)
return
grouped = expr.groupby(groups).sum(use_fallback=use_fallback)
index = pd.MultiIndex.from_frame(df)
assert "group" in grouped.dims
assert set(grouped.data.group.values) == set(index.values)
assert grouped.nterm == 3
def test_linear_expression_groupby_with_dataframe_non_aligned(v):
expr = 1 * v
groups = pd.DataFrame(
{"a": [1] * 10 + [2] * 10, "b": list(range(4)) * 5}, index=v.indexes["dim_2"]
)
target = expr.groupby(groups).sum()
groups_non_aligned = groups[::-1]
grouped = expr.groupby(groups_non_aligned).sum()
assert_linequal(grouped, target)
@pytest.mark.parametrize("use_fallback", [True, False])
def test_linear_expression_groupby_with_const(v, use_fallback):
expr = 1 * v + 15
groups = xr.DataArray([1] * 10 + [2] * 10, coords=v.coords)
grouped = expr.groupby(groups).sum(use_fallback=use_fallback)
assert "group" in grouped.dims
assert (grouped.data.group == [1, 2]).all()
assert grouped.nterm == 10
assert (grouped.const == 150).all()
@pytest.mark.parametrize("use_fallback", [True, False])
def test_linear_expression_groupby_asymmetric(v, use_fallback):
expr = 1 * v
# now asymetric groups which result in different nterms
groups = xr.DataArray([1] * 12 + [2] * 8, coords=v.coords)
grouped = expr.groupby(groups).sum(use_fallback=use_fallback)
assert "group" in grouped.dims
# first group must be full with vars
assert (grouped.data.sel(group=1) > 0).all()
# the last 4 entries of the second group must be empty, i.e. -1
assert (grouped.data.sel(group=2).isel(_term=slice(None, -4)).vars >= 0).all()
assert (grouped.data.sel(group=2).isel(_term=slice(-4, None)).vars == -1).all()
assert grouped.nterm == 12
@pytest.mark.parametrize("use_fallback", [True, False])
def test_linear_expression_groupby_asymmetric_with_const(v, use_fallback):
expr = 1 * v + 15
# now asymetric groups which result in different nterms
groups = xr.DataArray([1] * 12 + [2] * 8, coords=v.coords)
grouped = expr.groupby(groups).sum(use_fallback=use_fallback)
assert "group" in grouped.dims
# first group must be full with vars
assert (grouped.data.sel(group=1) > 0).all()
# the last 4 entries of the second group must be empty, i.e. -1
assert (grouped.data.sel(group=2).isel(_term=slice(None, -4)).vars >= 0).all()
assert (grouped.data.sel(group=2).isel(_term=slice(-4, None)).vars == -1).all()
assert grouped.nterm == 12
assert list(grouped.const) == [180, 120]
def test_linear_expression_groupby_roll(v):
expr = 1 * v
groups = xr.DataArray([1] * 10 + [2] * 10, coords=v.coords)
grouped = expr.groupby(groups).roll(dim_2=1)
assert grouped.nterm == 1
assert grouped.vars[0].item() == 19
def test_linear_expression_groupby_roll_with_const(v):
expr = 1 * v + np.arange(20)
groups = xr.DataArray([1] * 10 + [2] * 10, coords=v.coords)
grouped = expr.groupby(groups).roll(dim_2=1)
assert grouped.nterm == 1
assert grouped.vars[0].item() == 19
assert grouped.const[0].item() == 9
def test_linear_expression_groupby_from_variable(v):
groups = xr.DataArray([1] * 10 + [2] * 10, coords=v.coords)
grouped = v.groupby(groups).sum()
assert "group" in grouped.dims
assert (grouped.data.group == [1, 2]).all()
assert grouped.nterm == 10
def test_linear_expression_rolling(v):
expr = 1 * v
rolled = expr.rolling(dim_2=2).sum()
assert rolled.nterm == 2
rolled = expr.rolling(dim_2=3).sum()
assert rolled.nterm == 3
with pytest.raises(ValueError):
expr.rolling().sum()
def test_linear_expression_rolling_with_const(v):
expr = 1 * v + 15
rolled = expr.rolling(dim_2=2).sum()
assert rolled.nterm == 2
assert rolled.const[0].item() == 15
assert (rolled.const[1:] == 30).all()
rolled = expr.rolling(dim_2=3).sum()
assert rolled.nterm == 3
assert rolled.const[0].item() == 15
assert rolled.const[1].item() == 30
assert (rolled.const[2:] == 45).all()
def test_linear_expression_rolling_from_variable(v):
rolled = v.rolling(dim_2=2).sum()
assert rolled.nterm == 2
def test_linear_expression_sanitize(x, y, z):
expr = 10 * x + y + z
assert isinstance(expr.sanitize(), LinearExpression)
def test_merge(x, y, z):
expr1 = (10 * x + y).sum("dim_0")
expr2 = z.sum("dim_0")
res = merge([expr1, expr2])
assert res.nterm == 6
res = merge([expr1, expr2])
assert res.nterm == 6
# now concat with same length of terms
expr1 = z.sel(dim_0=0).sum("dim_1")
expr2 = z.sel(dim_0=1).sum("dim_1")
res = merge([expr1, expr2], dim="dim_1")
assert res.nterm == 3
# now with different length of terms
expr1 = z.sel(dim_0=0, dim_1=slice(0, 1)).sum("dim_1")
expr2 = z.sel(dim_0=1).sum("dim_1")
res = merge([expr1, expr2], dim="dim_1")
assert res.nterm == 3
assert res.sel(dim_1=0).vars[2].item() == -1
with pytest.warns(DeprecationWarning):
merge(expr1, expr2)
def test_linear_expression_outer_sum(x, y):
expr = x + y
expr2 = sum([x, y])
assert_linequal(expr, expr2)
expr = 1 * x + 2 * y