@@ -21,54 +21,51 @@ bfloat16 NumPy extension array.
21
21
If you are using Pandas, you can use the `lance.bfloat16 ` dtype string to create
22
22
the array:
23
23
24
- .. testcode ::
24
+ .. doctest ::
25
25
26
- import pandas as pd
27
- import lance.arrow
28
-
29
- series = pd.Series([1.1, 2.1, 3.4], dtype="lance.bfloat16")
30
- series
31
-
32
- .. testoutput ::
26
+ >>> import lance.arrow
33
27
28
+ >>> pd.Series([1.1 , 2.1 , 3.4 ], dtype = " lance.bfloat16" )
34
29
0 1.1015625
35
30
1 2.09375
36
31
2 3.40625
37
32
dtype: lance.bfloat16
38
33
39
34
To create an an arrow array, use the :func: `lance.arrow.bfloat16_array ` function:
40
35
41
- .. testcode ::
36
+ .. code-block :: python
42
37
43
- from lance.arrow import bfloat16_array
38
+ >> > from lance.arrow import bfloat16_array
44
39
45
- array = bfloat16_array([1.1, 2.1, 3.4])
46
- array
47
-
48
- .. testoutput ::
40
+ >> > bfloat16_array([1.1 , 2.1 , 3.4 ])
41
+ < lance.arrow.BFloat16Array object at 0x 000000016feb94e0>
42
+ [
43
+ 1.1015625 ,
44
+ 2.09375 ,
45
+ 3.40625
46
+ ]
49
47
50
- <lance.arrow.BFloat16Array object at 0x.+>
51
- [1.1015625, 2.09375, 3.40625]
52
48
53
49
Finally, if you have a pre-existing NumPy array, you can convert it into either:
54
50
55
- .. testcode ::
56
-
57
- import numpy as np
58
- from ml_dtypes import bfloat16
59
- from lance.arrow import PandasBFloat16Array, BFloat16Array
51
+ .. doctest ::
60
52
61
- np_array = np.array([1.1, 2.1, 3.4], dtype=bfloat16)
62
- PandasBFloat16Array.from_numpy(np_array)
63
- BFloat16Array.from_numpy(np_array)
53
+ >>> import numpy as np
54
+ >>> from ml_dtypes import bfloat16
55
+ >>> from lance.arrow import PandasBFloat16Array, BFloat16Array
64
56
65
- .. testoutput ::
66
-
57
+ >>> np_array = np.array([ 1.1 , 2.1 , 3.4 ], dtype = bfloat16)
58
+ >>> PandasBFloat16Array.from_numpy(np_array)
67
59
<PandasBFloat16Array>
68
60
[1.1015625, 2.09375, 3.40625]
69
61
Length: 3, dtype: lance.bfloat16
70
- <lance.arrow.BFloat16Array object at 0x.+>
71
- [1.1015625, 2.09375, 3.40625]
62
+ >>> BFloat16Array.from_numpy(np_array)
63
+ <lance.arrow.BFloat16Array object at 0x...>
64
+ [
65
+ 1.1015625,
66
+ 2.09375,
67
+ 3.40625
68
+ ]
72
69
73
70
When reading, these can be converted back to to the NumPy bfloat16 dtype using
74
71
each array class's ``to_numpy `` method.
@@ -86,25 +83,23 @@ with a list of URIs represented by either :py:class:`pyarrow.StringArray` or an
86
83
iterable that yields strings. Note that the URIs are not strongly validated and images
87
84
are not read into memory automatically.
88
85
89
- .. testcode ::
90
-
91
- from lance.arrow import ImageURIArray
86
+ .. doctest ::
92
87
93
- ImageURIArray.from_uris([
94
- "/tmp/image1.jpg",
95
- "file:///tmp/image2.jpg",
96
- "s3://example/image3.jpg"
97
- ])
88
+ >>> from lance.arrow import ImageURIArray
98
89
99
- .. testoutput ::
90
+ >>> ImageURIArray.from_uris([
91
+ ... " /tmp/image1.jpg" ,
92
+ ... " file:///tmp/image2.jpg" ,
93
+ ... " s3://example/image3.jpg"
94
+ ... ])
95
+ <lance.arrow.ImageURIArray object at 0x...>
96
+ ['/tmp/image1.jpg', 'file:///tmp/image2.jpg', 's3://example/image3.jpg']
100
97
101
- <lance.arrow.ImageURIArray object at 0x.+>
102
- ['/tmp/image1.jpg', 'file:///tmp/image2.jpg', 's3://example/image2.jpg']
103
98
104
99
:func: `lance.arrow.ImageURIArray.read_uris ` will read images into memory and return
105
100
them as a new :class: `lance.arrow.EncodedImageArray ` object.
106
101
107
- .. testcode ::
102
+ .. code-block :: python
108
103
109
104
from lance.arrow import ImageURIArray
110
105
@@ -139,7 +134,7 @@ function parameter. If decoder is not provided it will attempt to use
139
134
`Pillow `_ and `tensorflow `_ in that
140
135
order. If neither library or custom decoder is available an exception will be raised.
141
136
142
- .. testcode ::
137
+ .. code-block :: python
143
138
144
139
from lance.arrow import ImageURIArray
145
140
@@ -185,30 +180,20 @@ If encoder is not provided it will attempt to use
185
180
`tensorflow `_ and `Pillow `_ in that order. Default encoders will
186
181
encode to PNG. If neither library is available it will raise an exception.
187
182
188
- .. testcode ::
189
-
190
- from lance.arrow import ImageURIArray
191
-
192
- def jpeg_encoder(images):
193
- import tensorflow as tf
183
+ .. testsetup ::
194
184
195
- encoded_images = (
196
- tf.io.encode_jpeg(x).numpy() for x in tf.convert_to_tensor(images)
197
- )
198
- return pa.array(encoded_images, type=pa.binary())
185
+ image_uri = os.path.abspath(os.path.join(os.path.dirname(__name__), "_static", "icon.png"))
199
186
200
- uris = [os.path.join(os.path.dirname(__file__), "images/1.png")]
201
- tensor_images = ImageURIArray.from_uris(uris).read_uris().to_tensor()
202
- print(tensor_images.to_encoded())
203
- print(tensor_images.to_encoded(jpeg_encoder))
187
+ .. doctest ::
204
188
205
- .. testoutput ::
189
+ >>> from lance.arrow import ImageURIArray
206
190
191
+ >>> uris = [image_uri]
192
+ >>> tensor_images = ImageURIArray.from_uris(uris).read_uris().to_tensor()
193
+ >>> tensor_images.to_encoded()
207
194
<lance.arrow.EncodedImageArray object at 0x...>
208
- [b'\x 89PNG\r\n\x 1a\n\x 00\x 00\x 00\r IHDR\x 00\x 00\x 00...']
209
- <lance.arrow.EncodedImageArray object at 0x00007f8d90b91b40>
210
- [b'\x ff\x d8\x ff\x e0\x 00\x 10JFIF\x 00\x 01\x 01\x 01\x 01...']
211
-
195
+ [...
196
+ b'\x89PNG\r\n\x1a...'
212
197
213
198
.. _tensorflow : https://www.tensorflow.org/api_docs/python/tf/io/encode_png
214
199
.. _Pillow : https://pillow.readthedocs.io/en/stable/
0 commit comments