@@ -574,56 +574,53 @@ def test_fill_graph():
574574
575575
576576def test_stretch ():
577- nrec = 5
578- arr = np .empty (nrec ,
577+ arr = np .empty (5 ,
579578 dtype = [
580579 ('scalar' , np .int ),
581- ('df1' , 'O' ),
582- ('df2' , 'O' ),
583- ('df3' , 'O' )])
584-
585- for i in range (nrec ):
586- df1 = np .array (range (i + 1 ), dtype = np .float )
587- df2 = np .array (range (i + 1 ), dtype = np .int ) * 2
588- df3 = np .array (range (i + 1 ), dtype = np .double ) * 3
589- arr [i ] = (i , df1 , df2 , df3 )
580+ ('vl1' , 'O' ),
581+ ('vl2' , 'O' ),
582+ ('vl3' , 'O' ),
583+ ('fl1' , np .int , (2 , 2 )),
584+ ('fl2' , np .float , (2 , 3 )),
585+ ('fl3' , np .double , (3 , 2 ))])
586+
587+ for i in range (arr .shape [0 ]):
588+ vl1 = np .array (range (i + 1 ), dtype = np .int )
589+ vl2 = np .array (range (i + 2 ), dtype = np .float ) * 2
590+ vl3 = np .array (range (2 ), dtype = np .double ) * 3
591+ fl1 = np .array (range (4 ), dtype = np .int ).reshape ((2 , 2 ))
592+ fl2 = np .array (range (6 ), dtype = np .float ).reshape ((2 , 3 ))
593+ fl3 = np .array (range (6 ), dtype = np .double ).reshape ((3 , 2 ))
594+ arr [i ] = (i , vl1 , vl2 , vl3 , fl1 , fl2 , fl3 )
595+
596+ # no array columns included
597+ assert_raises (RuntimeError , rnp .stretch , arr , ['scalar' ,])
590598
591- stretched = rnp .stretch (
592- arr , ['scalar' , 'df1' , 'df2' , 'df3' ])
599+ # lengths don't match
600+ assert_raises (ValueError , rnp .stretch , arr , ['scalar' , 'vl1' , 'vl2' ,])
601+ assert_raises (ValueError , rnp .stretch , arr , ['scalar' , 'fl1' , 'fl3' ,])
602+ assert_raises (ValueError , rnp .stretch , arr )
593603
604+ # variable-length stretch
605+ stretched = rnp .stretch (arr , ['scalar' , 'vl1' ,])
594606 assert_equal (stretched .dtype ,
595- [('scalar' , np .int ),
596- ('df1' , np .float ),
597- ('df2' , np .int ),
598- ('df3' , np .double )])
599- assert_equal (stretched .size , 15 )
600-
601- assert_almost_equal (stretched ['df1' ][14 ], 4.0 )
602- assert_almost_equal (stretched ['df2' ][14 ], 8 )
603- assert_almost_equal (stretched ['df3' ][14 ], 12.0 )
604- assert_almost_equal (stretched ['scalar' ][14 ], 4 )
605- assert_almost_equal (stretched ['scalar' ][13 ], 4 )
606- assert_almost_equal (stretched ['scalar' ][12 ], 4 )
607- assert_almost_equal (stretched ['scalar' ][11 ], 4 )
608- assert_almost_equal (stretched ['scalar' ][10 ], 4 )
609- assert_almost_equal (stretched ['scalar' ][9 ], 3 )
610-
611- arr = np .empty (1 , dtype = [('scalar' , np .int ),])
612- arr [0 ] = (1 ,)
613- assert_raises (RuntimeError , rnp .stretch , arr , ['scalar' ,])
607+ [('scalar' , np .int ),
608+ ('vl1' , np .int )])
609+ assert_equal (stretched .shape [0 ], 15 )
610+ assert_array_equal (
611+ stretched ['scalar' ],
612+ np .repeat (arr ['scalar' ], np .vectorize (len )(arr ['vl1' ])))
614613
615- nrec = 5
616- arr = np .empty (nrec ,
617- dtype = [
618- ('scalar' , np .int ),
619- ('df1' , 'O' ),
620- ('df2' , 'O' )])
621-
622- for i in range (nrec ):
623- df1 = np .array (range (i + 1 ), dtype = np .float )
624- df2 = np .array (range (i + 2 ), dtype = np .int ) * 2
625- arr [i ] = (i , df1 , df2 )
626- assert_raises (ValueError , rnp .stretch , arr , ['scalar' , 'df1' , 'df2' ])
614+ # fixed-length stretch
615+ stretched = rnp .stretch (arr , ['scalar' , 'vl3' , 'fl1' , 'fl2' ,])
616+ assert_equal (stretched .dtype ,
617+ [('scalar' , np .int ),
618+ ('vl3' , np .double ),
619+ ('fl1' , np .int , (2 ,)),
620+ ('fl2' , np .float , (3 ,))])
621+ assert_equal (stretched .shape [0 ], 10 )
622+ assert_array_equal (
623+ stretched ['scalar' ], np .repeat (arr ['scalar' ], 2 ))
627624
628625
629626def test_blockwise_inner_join ():
0 commit comments