Skip to content

Commit 51103bf

Browse files
committed
Simplify docstring examples to use direct blosc2 idioms instead of numpy intermediates
1 parent f4ceafd commit 51103bf

1 file changed

Lines changed: 21 additions & 73 deletions

File tree

src/blosc2/ndarray.py

Lines changed: 21 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -457,12 +457,7 @@ def reshape(
457457
Examples
458458
--------
459459
>>> import blosc2
460-
>>> import numpy as np
461-
>>> shape = [23 * 11]
462-
>>> a = np.arange(np.prod(shape))
463-
>>> # Create an array
464-
>>> b = blosc2.asarray(a)
465-
>>> # Reshape the array
460+
>>> b = blosc2.arange(253)
466461
>>> c = blosc2.reshape(b, (11, 23))
467462
>>> print(c.shape)
468463
(11, 23)
@@ -603,11 +598,8 @@ def sum(
603598
604599
Examples
605600
--------
606-
>>> import numpy as np
607601
>>> import blosc2
608-
>>> # Example array
609-
>>> array = np.array([[1, 2, 3], [4, 5, 6]])
610-
>>> nd_array = blosc2.asarray(array)
602+
>>> nd_array = blosc2.array([[1, 2, 3], [4, 5, 6]])
611603
>>> # Sum all elements in the array (axis=None)
612604
>>> total_sum = blosc2.sum(nd_array)
613605
>>> print("Sum of all elements:", total_sum)
@@ -718,11 +710,8 @@ def mean(
718710
719711
Examples
720712
--------
721-
>>> import numpy as np
722713
>>> import blosc2
723-
>>> # Example array
724-
>>> array = np.array([[1, 2, 3], [4, 5, 6]]
725-
>>> nd_array = blosc2.asarray(array)
714+
>>> nd_array = blosc2.array([[1, 2, 3], [4, 5, 6]])
726715
>>> # Compute the mean of all elements in the array (axis=None)
727716
>>> overall_mean = blosc2.mean(nd_array)
728717
>>> print("Mean of all elements:", overall_mean)
@@ -781,11 +770,8 @@ def std(
781770
782771
Examples
783772
--------
784-
>>> import numpy as np
785773
>>> import blosc2
786-
>>> # Create an instance of NDArray with some data
787-
>>> array = np.array([[1, 2, 3], [4, 5, 6]])
788-
>>> nd_array = blosc2.asarray(array)
774+
>>> nd_array = blosc2.array([[1, 2, 3], [4, 5, 6]])
789775
>>> # Compute the standard deviation of the entire array
790776
>>> std_all = blosc2.std(nd_array)
791777
>>> print("Standard deviation of the entire array:", std_all)
@@ -826,11 +812,8 @@ def var(
826812
827813
Examples
828814
--------
829-
>>> import numpy as np
830815
>>> import blosc2
831-
>>> # Create an instance of NDArray with some data
832-
>>> array = np.array([[1, 2, 3], [4, 5, 6]])
833-
>>> nd_array = blosc2.asarray(array)
816+
>>> nd_array = blosc2.array([[1, 2, 3], [4, 5, 6]])
834817
>>> # Compute the variance of the entire array
835818
>>> var_all = blosc2.var(nd_array)
836819
>>> print("Variance of the entire array:", var_all)
@@ -869,11 +852,8 @@ def prod(
869852
870853
Examples
871854
--------
872-
>>> import numpy as np
873855
>>> import blosc2
874-
>>> # Create an instance of NDArray with some data
875-
>>> array = np.array([[11, 22, 33], [4, 15, 36]])
876-
>>> nd_array = blosc2.asarray(array)
856+
>>> nd_array = blosc2.array([[11, 22, 33], [4, 15, 36]])
877857
>>> # Compute the product of all elements in the array
878858
>>> prod_all = blosc2.prod(nd_array)
879859
>>> print("Product of all elements in the array:", prod_all)
@@ -927,10 +907,8 @@ def min(
927907
928908
Examples
929909
--------
930-
>>> import numpy as np
931910
>>> import blosc2
932-
>>> array = np.array([1, 3, 7, 8, 9, 31])
933-
>>> nd_array = blosc2.asarray(array)
911+
>>> nd_array = blosc2.array([1, 3, 7, 8, 9, 31])
934912
>>> min_all = blosc2.min(nd_array)
935913
>>> print("Minimum of all elements in the array:", min_all)
936914
Minimum of all elements in the array: 1
@@ -968,9 +946,7 @@ def max(
968946
Examples
969947
--------
970948
>>> import blosc2
971-
>>> import numpy as np
972-
>>> data = np.array([[11, 2, 36, 24, 5, 69], [73, 81, 49, 6, 73, 0]])
973-
>>> ndarray = blosc2.asarray(data)
949+
>>> ndarray = blosc2.array([[11, 2, 36, 24, 5, 69], [73, 81, 49, 6, 73, 0]])
974950
>>> print("NDArray data:", ndarray[:])
975951
NDArray data: [[11 2 36 24 5 69]
976952
[73 81 49 6 73 0]]
@@ -1013,10 +989,7 @@ def any(
1013989
Examples
1014990
--------
1015991
>>> import blosc2
1016-
>>> import numpy as np
1017-
>>> data = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 0]])
1018-
>>> # Convert the NumPy array to a Blosc2 NDArray
1019-
>>> ndarray = blosc2.asarray(data)
992+
>>> ndarray = blosc2.array([[1, 0, 0], [0, 1, 0], [0, 0, 0]])
1020993
>>> print("NDArray data:", ndarray[:])
1021994
NDArray data: [[1 0 0]
1022995
[0 1 0]
@@ -1113,10 +1086,8 @@ def all(
11131086
11141087
Examples
11151088
--------
1116-
>>> import numpy as np
11171089
>>> import blosc2
1118-
>>> data = np.array([True, True, False, True, True, True])
1119-
>>> ndarray = blosc2.asarray(data)
1090+
>>> ndarray = blosc2.array([True, True, False, True, True, True])
11201091
>>> # Test if all elements are True along the default axis (flattened array)
11211092
>>> result_flat = blosc2.all(ndarray)
11221093
>>> print("All elements are True (flattened):", result_flat)
@@ -1959,10 +1930,8 @@ def contains(ndarr: blosc2.Array, value: str | bytes | blosc2.Array, /) -> blosc
19591930
19601931
Examples
19611932
--------
1962-
>>> import numpy as np
19631933
>>> import blosc2
1964-
>>> values = np.array([b"apple", b"xxbananaxxx", b"cherry", b"date"])
1965-
>>> text_values = blosc2.asarray(values)
1934+
>>> text_values = blosc2.array([b"apple", b"xxbananaxxx", b"cherry", b"date"])
19661935
>>> value_to_check = b"banana"
19671936
>>> expr = blosc2.contains(text_values, value_to_check)
19681937
>>> result = expr.compute()
@@ -2036,8 +2005,7 @@ def isnan(ndarr: blosc2.Array, /) -> blosc2.LazyExpr:
20362005
--------
20372006
>>> import numpy as np
20382007
>>> import blosc2
2039-
>>> values = np.array([-5, -3, np.nan, 2, 4])
2040-
>>> ndarray = blosc2.asarray(values)
2008+
>>> ndarray = blosc2.array([-5, -3, np.nan, 2, 4])
20412009
>>> result_ = blosc2.isnan(ndarray)
20422010
>>> result = result_[:]
20432011
>>> print("isnan:", result)
@@ -2068,8 +2036,7 @@ def isfinite(ndarr: blosc2.Array, /) -> blosc2.LazyExpr:
20682036
--------
20692037
>>> import numpy as np
20702038
>>> import blosc2
2071-
>>> values = np.array([-5, -3, np.inf, 2, 4])
2072-
>>> ndarray = blosc2.asarray(values)
2039+
>>> ndarray = blosc2.array([-5, -3, np.inf, 2, 4])
20732040
>>> result_ = blosc2.isfinite(ndarray)
20742041
>>> result = result_[:]
20752042
>>> print("isfinite:", result)
@@ -2100,8 +2067,7 @@ def isinf(ndarr: blosc2.Array, /) -> blosc2.LazyExpr:
21002067
--------
21012068
>>> import numpy as np
21022069
>>> import blosc2
2103-
>>> values = np.array([-5, -3, np.inf, 2, 4])
2104-
>>> ndarray = blosc2.asarray(values)
2070+
>>> ndarray = blosc2.array([-5, -3, np.inf, 2, 4])
21052071
>>> result_ = blosc2.isinf(ndarray)
21062072
>>> result = result_[:]
21072073
>>> print("isinf:", result)
@@ -3925,10 +3891,8 @@ def info(self) -> InfoReporter:
39253891
39263892
Examples
39273893
--------
3928-
>>> import numpy as np
39293894
>>> import blosc2
3930-
>>> my_array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
3931-
>>> array = blosc2.asarray(my_array)
3895+
>>> array = blosc2.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
39323896
>>> print(array.info)
39333897
type : NDArray
39343898
shape : (10,)
@@ -4090,9 +4054,7 @@ def blocksize(self) -> int:
40904054
Examples
40914055
--------
40924056
>>> import blosc2
4093-
>>> import numpy as np
4094-
>>> array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
4095-
>>> ndarray = blosc2.asarray(array)
4057+
>>> ndarray = blosc2.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
40964058
>>> print("Block size:", ndarray.blocksize)
40974059
Block size: 80
40984060
"""
@@ -4717,8 +4679,7 @@ def get_chunk(self, nchunk: int) -> bytes:
47174679
>>> import blosc2
47184680
>>> import numpy as np
47194681
>>> # Create an SChunk with some data
4720-
>>> array = np.arange(10)
4721-
>>> ndarray = blosc2.asarray(array)
4682+
>>> ndarray = blosc2.arange(10)
47224683
>>> chunk = ndarray.get_chunk(0)
47234684
>>> # Decompress the chunk to convert it into a numpy array
47244685
>>> decompressed_chunk = blosc2.decompress(chunk)
@@ -5157,13 +5118,9 @@ def resize(self, newshape: tuple | list) -> None:
51575118
Examples
51585119
--------
51595120
>>> import blosc2
5160-
>>> import numpy as np
51615121
>>> import math
5162-
>>> dtype = np.dtype(np.float32)
51635122
>>> shape = [23, 11]
5164-
>>> a = np.linspace(1, 3, num=math.prod(shape)).reshape(shape)
5165-
>>> # Create an array
5166-
>>> b = blosc2.asarray(a)
5123+
>>> b = blosc2.linspace(1, 3, num=math.prod(shape), shape=shape)
51675124
>>> newshape = [50, 10]
51685125
>>> # Extend first dimension, shrink second dimension
51695126
>>> b.resize(newshape)
@@ -5250,11 +5207,8 @@ def slice(self, key: int | slice | Sequence[slice], **kwargs: Any) -> NDArray:
52505207
Examples
52515208
--------
52525209
>>> import blosc2
5253-
>>> import numpy as np
52545210
>>> shape = [23, 11]
5255-
>>> a = np.arange(np.prod(shape)).reshape(shape)
5256-
>>> # Create an array
5257-
>>> b = blosc2.asarray(a)
5211+
>>> b = blosc2.arange(253, shape=shape)
52585212
>>> slices = (slice(3, 7), slice(1, 11))
52595213
>>> # Get a slice as a new NDArray
52605214
>>> c = b.slice(slices)
@@ -6373,10 +6327,8 @@ def copy(array: NDArray, dtype: np.dtype | str = None, **kwargs: Any) -> NDArray
63736327
63746328
Examples
63756329
--------
6376-
>>> import numpy as np
63776330
>>> import blosc2
6378-
>>> # Create an instance of NDArray with some data
6379-
>>> original_array = blosc2.asarray(np.array([[1.1, 2.2, 3.3], [4.4, 5.5, 6.6]]))
6331+
>>> original_array = blosc2.array([[1.1, 2.2, 3.3], [4.4, 5.5, 6.6]])
63806332
>>> # Create a copy of the array without changing dtype
63816333
>>> copied_array = blosc2.copy(original_array)
63826334
>>> print("Copied array (default dtype):")
@@ -6617,11 +6569,7 @@ def asarray(array: Sequence | blosc2.Array, copy: bool | None = None, **kwargs:
66176569
--------
66186570
>>> import blosc2
66196571
>>> import numpy as np
6620-
>>> # Create some data
6621-
>>> shape = [25, 10]
6622-
>>> a = np.arange(0, np.prod(shape), dtype=np.int64).reshape(shape)
6623-
>>> # Create a NDArray from a NumPy array
6624-
>>> nda = blosc2.asarray(a)
6572+
>>> nda = blosc2.arange(250, dtype=np.int64, shape=(25, 10))
66256573
>>> # NDArray inputs are returned as-is unless a copy is requested
66266574
>>> blosc2.asarray(nda) is nda
66276575
True

0 commit comments

Comments
 (0)