1212
1313sys .path .append (os .getcwd ())
1414
15+
1516class ArrayProcessor :
16- NAME = ''
17+ NAME = ""
1718 SORT = - 1
1819
1920 def __init__ (self , array : np .ndarray ):
2021 self .array = array
2122
22- #-------------------------------------------------------------------------------
23+
24+ # -------------------------------------------------------------------------------
2325class AKArray2D1D (ArrayProcessor ):
24- NAME = ' ak.array_to_tuple_array()'
26+ NAME = " ak.array_to_tuple_array()"
2527 SORT = 0
2628
2729 def __call__ (self ):
2830 _ = array_to_tuple_array (self .array )
2931
32+
3033class PyArray2D1D (ArrayProcessor ):
31- NAME = ' Python construction'
34+ NAME = " Python construction"
3235 SORT = 1
3336
3437 def __call__ (self ):
@@ -41,114 +44,131 @@ def __call__(self):
4144 post [i ] = tuple (row )
4245 post .flags .writeable = False
4346
44- #-------------------------------------------------------------------------------
47+
48+ # -------------------------------------------------------------------------------
4549NUMBER = 200
4650
51+
4752def seconds_to_display (seconds : float ) -> str :
4853 seconds /= NUMBER
4954 if seconds < 1e-4 :
50- return f' { seconds * 1e6 : .1f} (µs)'
55+ return f" { seconds * 1e6 : .1f} (µs)"
5156 if seconds < 1e-1 :
52- return f' { seconds * 1e3 : .1f} (ms)'
53- return f' { seconds : .1f} (s)'
57+ return f" { seconds * 1e3 : .1f} (ms)"
58+ return f" { seconds : .1f} (s)"
5459
5560
5661def plot_performance (frame ):
57- fixture_total = len (frame [' fixture' ].unique ())
58- cat_total = len (frame [' size' ].unique ())
59- processor_total = len (frame [' cls_processor' ].unique ())
62+ fixture_total = len (frame [" fixture" ].unique ())
63+ cat_total = len (frame [" size" ].unique ())
64+ processor_total = len (frame [" cls_processor" ].unique ())
6065 fig , axes = plt .subplots (cat_total , fixture_total )
6166
6267 # cmap = plt.get_cmap('terrain')
63- cmap = plt .get_cmap (' plasma' )
68+ cmap = plt .get_cmap (" plasma" )
6469
6570 color = cmap (np .arange (processor_total ) / max (processor_total , 3 ))
6671
6772 # category is the size of the array
68- for cat_count , (cat_label , cat ) in enumerate (frame .groupby (' size' )):
73+ for cat_count , (cat_label , cat ) in enumerate (frame .groupby (" size" )):
6974 # each fixture is a collection of tests for one display
70- fixtures = {fixture_label : fixture for fixture_label , fixture in cat .groupby ('fixture' )}
75+ fixtures = {
76+ fixture_label : fixture for fixture_label , fixture in cat .groupby ("fixture" )
77+ }
7178 for fixture_count , (fixture_label , fixture ) in enumerate (
72- (k , fixtures [k ]) for k in FixtureFactory .DENSITY_TO_DISPLAY ):
79+ (k , fixtures [k ]) for k in FixtureFactory .DENSITY_TO_DISPLAY
80+ ):
7381 ax = axes [cat_count ][fixture_count ]
7482
7583 # set order
76- fixture [' sort' ] = [f .SORT for f in fixture [' cls_processor' ]]
77- fixture = fixture .sort_values (' sort' )
84+ fixture [" sort" ] = [f .SORT for f in fixture [" cls_processor" ]]
85+ fixture = fixture .sort_values (" sort" )
7886
79- results = fixture [' time' ].values .tolist ()
80- names = [cls .NAME for cls in fixture [' cls_processor' ]]
87+ results = fixture [" time" ].values .tolist ()
88+ names = [cls .NAME for cls in fixture [" cls_processor" ]]
8189 # x = np.arange(len(results))
8290 names_display = names
8391 post = ax .bar (names_display , results , color = color )
8492
8593 # density, position = fixture_label.split('-')
8694 # cat_label is the size of the array
87- title = f'{ cat_label :.0e} \n { FixtureFactory .DENSITY_TO_DISPLAY [fixture_label ]} '
95+ title = (
96+ f"{ cat_label :.0e} \n { FixtureFactory .DENSITY_TO_DISPLAY [fixture_label ]} "
97+ )
8898
8999 ax .set_title (title , fontsize = 6 )
90- ax .set_box_aspect (0.75 ) # makes taller than wide
91- time_max = fixture [' time' ].max ()
100+ ax .set_box_aspect (0.75 ) # makes taller than wide
101+ time_max = fixture [" time" ].max ()
92102 ax .set_yticks ([0 , time_max * 0.5 , time_max ])
93- ax .set_yticklabels (['' ,
94- seconds_to_display (time_max * .5 ),
103+ ax .set_yticklabels (
104+ [
105+ "" ,
106+ seconds_to_display (time_max * 0.5 ),
95107 seconds_to_display (time_max ),
96- ], fontsize = 4 )
108+ ],
109+ fontsize = 4 ,
110+ )
97111 # ax.set_xticks(x, names_display, rotation='vertical')
98112 ax .tick_params (
99- axis = 'x' ,
100- which = ' both' ,
101- bottom = False ,
102- top = False ,
103- labelbottom = False ,
104- )
105-
106- fig .set_size_inches (8 , 4 ) # width, height
107- fig .legend (post , names_display , loc = ' center right' , fontsize = 6 )
113+ axis = "x" ,
114+ which = " both" ,
115+ bottom = False ,
116+ top = False ,
117+ labelbottom = False ,
118+ )
119+
120+ fig .set_size_inches (8 , 4 ) # width, height
121+ fig .legend (post , names_display , loc = " center right" , fontsize = 6 )
108122 # horizontal, vertical
109- fig .text (.05 , .96 , f'array_to_tuple_array() Performance: { NUMBER } Iterations' , fontsize = 10 )
110- fig .text (.05 , .90 , get_versions (), fontsize = 6 )
123+ fig .text (
124+ 0.05 ,
125+ 0.96 ,
126+ f"array_to_tuple_array() Performance: { NUMBER } Iterations" ,
127+ fontsize = 10 ,
128+ )
129+ fig .text (0.05 , 0.90 , get_versions (), fontsize = 6 )
111130
112- fp = ' /tmp/array_to_tuple_array.png'
131+ fp = " /tmp/array_to_tuple_array.png"
113132 plt .subplots_adjust (
114- left = 0.05 ,
115- bottom = 0.05 ,
116- right = 0.8 ,
117- top = 0.85 ,
118- wspace = 1.0 , # width
119- hspace = 0.5 ,
120- )
133+ left = 0.05 ,
134+ bottom = 0.05 ,
135+ right = 0.8 ,
136+ top = 0.85 ,
137+ wspace = 1.0 , # width
138+ hspace = 0.5 ,
139+ )
121140 # plt.rcParams.update({'font.size': 22})
122141 plt .savefig (fp , dpi = 300 )
123142
124- if sys .platform .startswith (' linux' ):
125- os .system (f' eog { fp } &' )
143+ if sys .platform .startswith (" linux" ):
144+ os .system (f" eog { fp } &" )
126145 else :
127- os .system (f'open { fp } ' )
146+ os .system (f"open { fp } " )
147+
128148
149+ # -------------------------------------------------------------------------------
129150
130- #-------------------------------------------------------------------------------
131151
132152class FixtureFactory :
133- NAME = ''
153+ NAME = ""
134154
135155 @staticmethod
136156 def get_array (size : int , width_ratio : int ) -> np .ndarray :
137157 if width_ratio > 1 :
138158 return np .arange (size ).reshape (size // width_ratio , width_ratio )
139- return np .arange (size ) # return 1D array
159+ return np .arange (size ) # return 1D array
140160
141161 @classmethod
142162 def get_label_array (cls , size : int ) -> tp .Tuple [str , np .ndarray ]:
143163 array = cls .get_array (size )
144164 return cls .NAME , array
145165
146166 DENSITY_TO_DISPLAY = {
147- ' column-1' : ' 1 Column' ,
148- ' column-2' : ' 2 Column' ,
149- ' column-5' : ' 5 Column' ,
150- ' column-10' : ' 10 Column' ,
151- ' column-20' : ' 20 Column' ,
167+ " column-1" : " 1 Column" ,
168+ " column-2" : " 2 Column" ,
169+ " column-5" : " 5 Column" ,
170+ " column-10" : " 10 Column" ,
171+ " column-20" : " 20 Column" ,
152172 }
153173
154174 # POSITION_TO_DISPLAY = {
@@ -158,7 +178,7 @@ def get_label_array(cls, size: int) -> tp.Tuple[str, np.ndarray]:
158178
159179
160180class FFC1 (FixtureFactory ):
161- NAME = ' column-1'
181+ NAME = " column-1"
162182
163183 @staticmethod
164184 def get_array (size : int ) -> np .ndarray :
@@ -167,46 +187,51 @@ def get_array(size: int) -> np.ndarray:
167187
168188
169189class FFC2 (FixtureFactory ):
170- NAME = ' column-2'
190+ NAME = " column-2"
171191
172192 @staticmethod
173193 def get_array (size : int ) -> np .ndarray :
174194 a = FixtureFactory .get_array (size , 2 )
175195 return a
176196
197+
177198class FFC5 (FixtureFactory ):
178- NAME = ' column-5'
199+ NAME = " column-5"
179200
180201 @staticmethod
181202 def get_array (size : int ) -> np .ndarray :
182203 a = FixtureFactory .get_array (size , 5 )
183204 return a
184205
206+
185207class FFC10 (FixtureFactory ):
186- NAME = ' column-10'
208+ NAME = " column-10"
187209
188210 @staticmethod
189211 def get_array (size : int ) -> np .ndarray :
190212 a = FixtureFactory .get_array (size , 10 )
191213 return a
192214
215+
193216class FFC20 (FixtureFactory ):
194- NAME = ' column-20'
217+ NAME = " column-20"
195218
196219 @staticmethod
197220 def get_array (size : int ) -> np .ndarray :
198221 a = FixtureFactory .get_array (size , 20 )
199222 return a
200223
224+
201225def get_versions () -> str :
202226 import platform
203- return f'OS: { platform .system ()} / ArrayKit: { ak .__version__ } / NumPy: { np .__version__ } \n '
227+
228+ return f"OS: { platform .system ()} / ArrayKit: { ak .__version__ } / NumPy: { np .__version__ } \n "
204229
205230
206231CLS_PROCESSOR = (
207232 AKArray2D1D ,
208233 PyArray2D1D ,
209- )
234+ )
210235
211236CLS_FF = (
212237 FFC1 ,
@@ -228,26 +253,20 @@ def run_test():
228253 record = [cls , NUMBER , fixture_label , size ]
229254 print (record )
230255 try :
231- result = timeit .timeit (
232- f'runner()' ,
233- globals = locals (),
234- number = NUMBER )
256+ result = timeit .timeit (f"runner()" , globals = locals (), number = NUMBER )
235257 except OSError :
236258 result = np .nan
237259 finally :
238260 pass
239261 record .append (result )
240262 records .append (record )
241263
242- f = pd .DataFrame .from_records (records ,
243- columns = (' cls_processor' , ' number' , ' fixture' , ' size' , ' time' )
244- )
264+ f = pd .DataFrame .from_records (
265+ records , columns = (" cls_processor" , " number" , " fixture" , " size" , " time" )
266+ )
245267 print (f )
246268 plot_performance (f )
247269
248- if __name__ == '__main__' :
249270
271+ if __name__ == "__main__" :
250272 run_test ()
251-
252-
253-
0 commit comments