array-api-shape-check 0.1.0__py3-none-any.whl → 0.1.2__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.
@@ -1,4 +1,4 @@
1
- __version__ = "0.1.0"
1
+ __version__ = "0.1.2"
2
2
  from ._main import (
3
3
  InconsistentNdimError,
4
4
  InconsistentNdimErrorMultipleSolutions,
@@ -23,7 +23,7 @@ class SubscriptInfoFromSubcriptItem:
23
23
  @attrs.frozen(kw_only=True)
24
24
  class SubscriptInfoFromSubcript:
25
25
  all: tuple[tuple[SubscriptInfoFromSubcriptItem, ...], ...]
26
- unique: tuple[SubscriptInfoFromSubcriptItem, ...]
26
+ unique: dict[str, SubscriptInfoFromSubcriptItem]
27
27
 
28
28
 
29
29
  @attrs.frozen(kw_only=True)
@@ -70,7 +70,7 @@ class SubscriptInfoFromShapeItem(SubscriptInfoFromShapeItemUnchecked):
70
70
  class SubscriptInfoFromShape:
71
71
  all: tuple[tuple[SubscriptInfoFromShapeItem, ...], ...]
72
72
  """The subscript info grouped by order, then by operand"""
73
- unique: tuple[SubscriptInfoFromShapeItemUnique, ...]
73
+ unique: dict[str, SubscriptInfoFromShapeItemUnique]
74
74
  """The unique subscripts,
75
75
  ignoring order and shapes not broadcasted,
76
76
  sorted lexicographically by name"""
@@ -157,8 +157,11 @@ def parse_subscripts(subscripts: str, /) -> SubscriptInfoFromSubcript:
157
157
 
158
158
  Examples
159
159
  --------
160
- >>> parse_subscripts("ij,*k*l,*li")
161
- SubscriptInfoFromSubcript(all=((i, j), (*k, *l), (*l, i)), unique=(i, j, *k, *l))
160
+ >>> info = parse_subscripts("ij,*k*l,*li")
161
+ >>> info.all
162
+ ((i, j), (*k, *l), (*l, i))
163
+ >>> info.unique
164
+ {'i': i, 'j': j, 'k': *k, 'l': *l}
162
165
 
163
166
  """
164
167
  # If . other than ...
@@ -181,9 +184,9 @@ def parse_subscripts(subscripts: str, /) -> SubscriptInfoFromSubcript:
181
184
  is_variable = False
182
185
  info_all += (info_array,)
183
186
 
184
- info_unique: set[SubscriptInfoFromSubcriptItem] = {
185
- x for info_array in info_all for x in info_array
186
- }
187
+ info_unique = tuple(
188
+ sorted({x for info_array in info_all for x in info_array}, key=lambda x: x.name)
189
+ )
187
190
  # raise if there are subscripts with same name but different is_variable
188
191
  names = set()
189
192
  for info in info_unique:
@@ -193,9 +196,7 @@ def parse_subscripts(subscripts: str, /) -> SubscriptInfoFromSubcript:
193
196
  )
194
197
  names.add(info.name)
195
198
 
196
- return SubscriptInfoFromSubcript(
197
- all=info_all, unique=tuple(sorted(info_unique, key=lambda x: x.name))
198
- )
199
+ return SubscriptInfoFromSubcript(all=info_all, unique={item.name: item for item in info_unique})
199
200
 
200
201
 
201
202
  def parse_variable_ndim(subscripts: str, ndims: Sequence[int], /) -> dict[str, int]:
@@ -253,7 +254,9 @@ def parse_variable_ndim(subscripts: str, ndims: Sequence[int], /) -> dict[str, i
253
254
  )
254
255
 
255
256
  # decide dimensions by solving linear equations
256
- info_variable_unique = [subscript for subscript in info.unique if subscript.is_variable]
257
+ info_variable_unique = [
258
+ subscript for subscript in info.unique.values() if subscript.is_variable
259
+ ]
257
260
  if len(info_variable_unique) > len(info.all):
258
261
  raise InconsistentNdimErrorMultipleSolutions("number of variables")
259
262
  rhs = np.asarray(ndims, dtype=int)
@@ -363,19 +366,30 @@ def check_shapes(
363
366
  >>> info.all
364
367
  ((i:1->3, j:4), (*k:(5,), *l:(6, 7)), (*l:(1, 7)->(6, 7), i:3))
365
368
  >>> info.unique
366
- (i:3, j:4, *k:(5,), *l:(6, 7))
369
+ {'i': i:3, 'j': j:4, 'k': *k:(5,), 'l': *l:(6, 7)}
370
+
371
+ Internally `check_shapes()` calls `parse_variable_ndim()`,
372
+ which determines the number of dimensions for variable subscripts by least squares.
373
+ If this is successful, checks if each subscript is consistent,
374
+ then finnaly raises error for all inconsistencies at once.
375
+
376
+ Diving into the details of the first item:
377
+
378
+ >>> item = info.all[0][0]
379
+ >>> item.name # the name of the subscript
380
+ 'i'
381
+ >>> item.is_variable # whether the subscript is variable (starts with "*")
382
+ False
383
+ >>> item.shape_current # the current shape of the subscript
384
+ (1,)
385
+ >>> item.shape_broadcasted # the broadcasted shape of the subscript
386
+ (3,)
367
387
 
368
388
  Not enough information to determine variable subscript ndims:
369
389
 
370
390
  >>> import pytest
371
391
  >>> with pytest.raises(InconsistentNdimErrorMultipleSolutions, match="number of variables"):
372
- ... check_shapes(
373
- ... "*i*j",
374
- ... (
375
- ... 1,
376
- ... 1,
377
- ... ),
378
- ... )
392
+ ... check_shapes("*i*j", (1, 1))
379
393
  >>> with pytest.raises(InconsistentNdimErrorMultipleSolutions, match="rank"):
380
394
  ... check_shapes("*i*j,*i*j", (1, 1), (1, 1))
381
395
 
@@ -437,7 +451,8 @@ def check_shapes(
437
451
  for item in info_array
438
452
  }
439
453
  result = SubscriptInfoFromShape(
440
- all=info_all_new, unique=tuple(sorted(info_unique, key=lambda x: x.name))
454
+ all=info_all_new,
455
+ unique={item.name: item for item in sorted(info_unique, key=lambda x: x.name)},
441
456
  )
442
457
 
443
458
  if errors:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: array-api-shape-check
3
- Version: 0.1.0
3
+ Version: 0.1.2
4
4
  Summary: Check shapes of input arrays easily
5
5
  Author-email: 34j <34j.95a2p@simplelogin.com>
6
6
  License-Expression: MIT
@@ -82,7 +82,7 @@ Install this via pip (or your favourite package manager):
82
82
  >>> info.all
83
83
  ((i:1->3, j:4), (*k:(5,), *l:(6, 7)), (*l:(1, 7)->(6, 7), i:3))
84
84
  >>> info.unique
85
- (i:3, j:4, *k:(5,), *l:(6, 7))
85
+ {'i': i:3, 'j': j:4, 'k': *k:(5,), 'l': *l:(6, 7)}
86
86
  ```
87
87
 
88
88
  Internally `check_shapes()` calls `parse_variable_ndim()`, which determines the number of dimensions for variable subscripts by least squares. If this is successful, checks if each subscript is consistent, then finnaly raises error for all inconsistencies at once.
@@ -107,13 +107,7 @@ Not enough information to determine variable subscript ndims:
107
107
  >>> import pytest
108
108
  >>> from array_api_shape_check import InconsistentNdimErrorMultipleSolutions, InconsistentNdimErrorNoSolutions, InconsistentShapeError
109
109
  >>> with pytest.raises(InconsistentNdimErrorMultipleSolutions, match="number of variables"):
110
- ... check_shapes(
111
- ... "*i*j",
112
- ... (
113
- ... 1,
114
- ... 1,
115
- ... ),
116
- ... )
110
+ ... check_shapes("*i*j", (1, 1))
117
111
  >>> with pytest.raises(InconsistentNdimErrorMultipleSolutions, match="rank"):
118
112
  ... check_shapes("*i*j,*i*j", (1, 1), (1, 1))
119
113
  ```
@@ -0,0 +1,8 @@
1
+ array_api_shape_check/__init__.py,sha256=peDt2aytKiIHNe9K5YRB8VJG15wxDIt0bKm7Z6Wu8s8,940
2
+ array_api_shape_check/_main.py,sha256=kqGml_xsXX1CtieBZDyyd_m-_kBqe-F-oj4PVpfKsek,16029
3
+ array_api_shape_check/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ array_api_shape_check-0.1.2.dist-info/licenses/LICENSE,sha256=Otwb96xcK4QBqXXVdBKpLZVnc4mJuFTa8rsMUEpphMM,1061
5
+ array_api_shape_check-0.1.2.dist-info/METADATA,sha256=bQKTAYo0XcoLyASUyChwsJ0RdNwlb-CBN5oHQrGkgV4,6213
6
+ array_api_shape_check-0.1.2.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
7
+ array_api_shape_check-0.1.2.dist-info/top_level.txt,sha256=pirw2DeaWnDU_rSg3CZ1MsE-tJWEvndYSQG0illDMkI,22
8
+ array_api_shape_check-0.1.2.dist-info/RECORD,,
@@ -1,8 +0,0 @@
1
- array_api_shape_check/__init__.py,sha256=Nr4JGOF0JGdFAgQ84lyiQMcW0k1i1DYvVVusW_d_T-k,940
2
- array_api_shape_check/_main.py,sha256=ebEBTb8o3yG19d0F9yfMB44dMLxJVJWt9KuH7e6IYV0,15391
3
- array_api_shape_check/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- array_api_shape_check-0.1.0.dist-info/licenses/LICENSE,sha256=Otwb96xcK4QBqXXVdBKpLZVnc4mJuFTa8rsMUEpphMM,1061
5
- array_api_shape_check-0.1.0.dist-info/METADATA,sha256=X1F3n9ScdYFEcftDb5LpXrNA1wefvX8X7RysP2iO7vg,6275
6
- array_api_shape_check-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
7
- array_api_shape_check-0.1.0.dist-info/top_level.txt,sha256=pirw2DeaWnDU_rSg3CZ1MsE-tJWEvndYSQG0illDMkI,22
8
- array_api_shape_check-0.1.0.dist-info/RECORD,,