Skip to content

Commit 1a6cbdd

Browse files
authored
Merge pull request #173 from static-frame/172/a2d-tuple
Support 1d arrays
2 parents 8e7937f + 4bc3192 commit 1a6cbdd

File tree

7 files changed

+230
-102
lines changed

7 files changed

+230
-102
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ What is New in ArrayKit
4040
0.7.0
4141
............
4242

43-
Added ``array2d_to_array1d()``.
43+
Added ``array_to_tuple_array()``.
4444

45-
Added ``array2d_tuple_iter()``.
45+
Added ``array_to_tuple_iter()``.
4646

4747

4848
0.6.3

doc/articles/array2d_to_1d.py renamed to doc/articles/array_to_tuple_array.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import timeit
44
import typing as tp
55

6-
from arraykit import array2d_to_array1d
6+
from arraykit import array_to_tuple_array
77
import arraykit as ak
88

99
import matplotlib.pyplot as plt
@@ -21,20 +21,24 @@ def __init__(self, array: np.ndarray):
2121

2222
#-------------------------------------------------------------------------------
2323
class AKArray2D1D(ArrayProcessor):
24-
NAME = 'ak.array2d_to_array1d()'
24+
NAME = 'ak.array_to_tuple_array()'
2525
SORT = 0
2626

2727
def __call__(self):
28-
_ = array2d_to_array1d(self.array)
28+
_ = array_to_tuple_array(self.array)
2929

3030
class PyArray2D1D(ArrayProcessor):
3131
NAME = 'Python construction'
3232
SORT = 1
3333

3434
def __call__(self):
3535
post = np.empty(self.array.shape[0], dtype=object)
36-
for i, row in enumerate(self.array):
37-
post[i] = tuple(row)
36+
if self.array.ndim == 1:
37+
for i, e in enumerate(self.array):
38+
post[i] = (e,)
39+
else:
40+
for i, row in enumerate(self.array):
41+
post[i] = tuple(row)
3842
post.flags.writeable = False
3943

4044
#-------------------------------------------------------------------------------
@@ -102,16 +106,16 @@ def plot_performance(frame):
102106
fig.set_size_inches(8, 4) # width, height
103107
fig.legend(post, names_display, loc='center right', fontsize=6)
104108
# horizontal, vertical
105-
fig.text(.05, .96, f'array2d_to_array1d() Performance: {NUMBER} Iterations', fontsize=10)
109+
fig.text(.05, .96, f'array_to_tuple_array() Performance: {NUMBER} Iterations', fontsize=10)
106110
fig.text(.05, .90, get_versions(), fontsize=6)
107111

108-
fp = '/tmp/array2d_to_array1d.png'
112+
fp = '/tmp/array_to_tuple_array.png'
109113
plt.subplots_adjust(
110114
left=0.05,
111115
bottom=0.05,
112116
right=0.8,
113117
top=0.85,
114-
wspace=0.9, # width
118+
wspace=1.0, # width
115119
hspace=0.5,
116120
)
117121
# plt.rcParams.update({'font.size': 22})
@@ -130,14 +134,17 @@ class FixtureFactory:
130134

131135
@staticmethod
132136
def get_array(size: int, width_ratio: int) -> np.ndarray:
133-
return np.arange(size).reshape(size // width_ratio, width_ratio)
137+
if width_ratio > 1:
138+
return np.arange(size).reshape(size // width_ratio, width_ratio)
139+
return np.arange(size) # return 1D array
134140

135141
@classmethod
136142
def get_label_array(cls, size: int) -> tp.Tuple[str, np.ndarray]:
137143
array = cls.get_array(size)
138144
return cls.NAME, array
139145

140146
DENSITY_TO_DISPLAY = {
147+
'column-1': '1 Column',
141148
'column-2': '2 Column',
142149
'column-5': '5 Column',
143150
'column-10': '10 Column',
@@ -150,6 +157,15 @@ def get_label_array(cls, size: int) -> tp.Tuple[str, np.ndarray]:
150157
# }
151158

152159

160+
class FFC1(FixtureFactory):
161+
NAME = 'column-1'
162+
163+
@staticmethod
164+
def get_array(size: int) -> np.ndarray:
165+
a = FixtureFactory.get_array(size, 1)
166+
return a
167+
168+
153169
class FFC2(FixtureFactory):
154170
NAME = 'column-2'
155171

@@ -193,6 +209,7 @@ def get_versions() -> str:
193209
)
194210

195211
CLS_FF = (
212+
FFC1,
196213
FFC2,
197214
FFC5,
198215
FFC10,

doc/articles/array2d_tuple_iter.py renamed to doc/articles/array_to_tuple_iter.py

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import timeit
44
import typing as tp
55

6-
from arraykit import array2d_tuple_iter
6+
from arraykit import array_to_tuple_iter
77
import arraykit as ak
88

99
import matplotlib.pyplot as plt
@@ -21,18 +21,18 @@ def __init__(self, array: np.ndarray):
2121

2222
#-------------------------------------------------------------------------------
2323
class AKArray2DTupleList(ArrayProcessor):
24-
NAME = 'list(ak.array2d_tuple_iter(a2d))'
24+
NAME = 'list(ak.array_to_tuple_iter(a2d))'
2525
SORT = 0
2626

2727
def __call__(self):
28-
_ = list(array2d_tuple_iter(self.array))
28+
_ = list(array_to_tuple_iter(self.array))
2929

3030
class AKArray2DTupleNext(ArrayProcessor):
31-
NAME = 'next(ak.array2d_tuple_iter(a2d))'
31+
NAME = 'next(ak.array_to_tuple_iter(a2d))'
3232
SORT = 1
3333

3434
def __call__(self):
35-
it = array2d_tuple_iter(self.array)
35+
it = array_to_tuple_iter(self.array)
3636
while True:
3737
try:
3838
_ = next(it)
@@ -45,19 +45,31 @@ class PyArray2DTupleMapList(ArrayProcessor):
4545

4646
def __call__(self):
4747
array = self.array
48-
_ = list(map(tuple, array))
48+
if array.ndim == 2:
49+
_ = list(map(tuple, array))
50+
else:
51+
_ = list(map(lambda e: (e,), array))
4952

5053
class PyArray2DTupleIterNext(ArrayProcessor):
5154
NAME = 'tuple(next(iter(a2d)))'
5255
SORT = 3
5356

5457
def __call__(self):
55-
it = iter(self.array)
56-
while True:
57-
try:
58-
_ = tuple(next(it))
59-
except StopIteration:
60-
break
58+
array = self.array
59+
it = iter(array)
60+
if array.ndim == 2:
61+
while True:
62+
try:
63+
_ = tuple(next(it))
64+
except StopIteration:
65+
break
66+
else:
67+
while True:
68+
try:
69+
_ = (next(it),)
70+
except StopIteration:
71+
break
72+
6173

6274

6375

@@ -128,10 +140,10 @@ def plot_performance(frame):
128140
fig.set_size_inches(8, 4) # width, height
129141
fig.legend(post, names_display, loc='center right', fontsize=6)
130142
# horizontal, vertical
131-
fig.text(.05, .96, f'array2d_tuple_iter() Performance: {NUMBER} Iterations', fontsize=10)
143+
fig.text(.05, .96, f'array_to_tuple_iter() Performance: {NUMBER} Iterations', fontsize=10)
132144
fig.text(.05, .90, get_versions(), fontsize=6)
133145

134-
fp = '/tmp/array2d_tuple_iter.png'
146+
fp = '/tmp/array_to_tuple_iter.png'
135147
plt.subplots_adjust(
136148
left=0.05,
137149
bottom=0.05,
@@ -156,21 +168,33 @@ class FixtureFactory:
156168

157169
@staticmethod
158170
def get_array(size: int, width_ratio: int) -> np.ndarray:
159-
return np.arange(size).reshape(size // width_ratio, width_ratio)
171+
if width_ratio > 1:
172+
return np.arange(size).reshape(size // width_ratio, width_ratio)
173+
return np.arange(size) # return 1D array
160174

161175
@classmethod
162176
def get_label_array(cls, size: int) -> tp.Tuple[str, np.ndarray]:
163177
array = cls.get_array(size)
164178
return cls.NAME, array
165179

166180
DENSITY_TO_DISPLAY = {
181+
'column-1': '1 Column',
167182
'column-2': '2 Column',
168183
'column-5': '5 Column',
169184
'column-10': '10 Column',
170185
'column-20': '20 Column',
171186
}
172187

173188

189+
class FFC1(FixtureFactory):
190+
NAME = 'column-1'
191+
192+
@staticmethod
193+
def get_array(size: int) -> np.ndarray:
194+
a = FixtureFactory.get_array(size, 1)
195+
return a
196+
197+
174198
class FFC2(FixtureFactory):
175199
NAME = 'column-2'
176200

@@ -217,6 +241,7 @@ def get_versions() -> str:
217241

218242

219243
CLS_FF = (
244+
FFC1,
220245
FFC2,
221246
FFC5,
222247
FFC10,

src/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@
2828
from ._arraykit import first_true_1d as first_true_1d
2929
from ._arraykit import first_true_2d as first_true_2d
3030
from ._arraykit import slice_to_ascending_slice as slice_to_ascending_slice
31-
from ._arraykit import array2d_to_array1d as array2d_to_array1d
32-
from ._arraykit import array2d_tuple_iter as array2d_tuple_iter
31+
from ._arraykit import array_to_tuple_array as array_to_tuple_array
32+
from ._arraykit import array_to_tuple_iter as array_to_tuple_iter
3333
from ._arraykit import nonzero_1d as nonzero_1d

src/__init__.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,5 +161,5 @@ def first_true_1d(__array: np.ndarray, *, forward: bool) -> int: ...
161161
def first_true_2d(__array: np.ndarray, *, forward: bool, axis: int) -> np.ndarray: ...
162162
def nonzero_1d(__array: np.ndarray, /) -> np.ndarray: ...
163163
def slice_to_ascending_slice(__slice: slice, __size: int) -> slice: ...
164-
def array2d_to_array1d(__array: np.ndarray) -> np.ndarray: ...
165-
def array2d_tuple_iter(__array: np.ndarray) -> tp.Iterator[tp.Tuple[tp.Any, ...]]: ...
164+
def array_to_tuple_array(__array: np.ndarray) -> np.ndarray: ...
165+
def array_to_tuple_iter(__array: np.ndarray) -> tp.Iterator[tp.Tuple[tp.Any, ...]]: ...

0 commit comments

Comments
 (0)