Related
I have a use case where I want to sort boundary boxes from the top right to the bottom left.
args:
dt_boxes(array): detected text boxes with shape [4, 2]
return:
sorted boxes(array) with shape [4, 2]
Example 1:
[[[258.0, 52.0], [329.0, 46.0], [329.0, 72.0], [260.0, 76.0]], [[91.0, 32.0], [174.0, 43.0], [175.0, 68.0], [90.0, 64.0]], [[182.0, 45.0], [266.0, 42.0], [266.0, 69.0], [184.0, 74.0]], [[16.0, 41.0], [97.0, 39.0], [98.0, 64.0], [17.0, 69.0]], [[226.0, 4.0], [296.0, 4.0], [296.0, 32.0], [227.0, 35.0]], [[288.0, 9.0], [331.0, 2.0], [331.0, 36.0], [289.0, 39.0]]]
Example 2:
[[[224.0, 85.0], [381.0, 83.0], [381.0, 128.0], [223.0, 126.0]], [[412.0, 88.0], [544.0, 81.0], [545.0, 129.0], [413.0, 134.0]], [[291.0, 18.0], [357.0, 18.0], [357.0, 56.0], [292.0, 68.0]], [[122.0, 12.0], [295.0, 11.0], [296.0, 57.0], [125.0, 64.0]], [[350.0, 22.0], [435.0, 11.0], [435.0, 55.0], [351.0, 66.0]], [[442.0, 15.0], [538.0, 11.0], [539.0, 49.0], [442.0, 57.0]], [[9.0, 12.0], [125.0, 8.0], [127.0, 54.0], [10.0, 63.0]]]
I have used the sort function like this to sort using the 2nd element (Top Right) and 3rd Element (Bottom Right) in the array but it doesn't provide the correct sorting
Results to be achieved :
Boundaries are upper left, upper right, lower right, and lower left.
sorted_boxes = sorted(dt_boxes, key=lambda x: (x[1][1], x[1][0]),reverse=True)
I generate i plots with the code:
for i in range(len(hlist)):
p1 = np.array(hlist[i])
kde = gaussian_kde(p1[:,0], bw_method=.4, weights=p1[:,1])
x = np.linspace(0, 100, 1000)
plt.plot(x, kde(x), color='crimson')
plt.ylim([minmin, maxmax])
plt.show()
The x-axis and the y-axis of all plots are the same
So I want to make a video from all plots and if its possible with transition between each plots so it looks like a moving probability distribution
Full hlist:
hlist = np.array([[[0.0, 0.9003079895267837],
[10.0, 0.000654274453636099],
[15.0, 4.8062543883990662e-05],
[20.0, 0.0078540048399245721],
[25.0, 0.00030775066073687057],
[30.0, 0.0069461270425554486],
[35.0, 0.00038747043199781708],
[40.0, 0.0040570022317705538],
[45.0, 0.01242208947573863],
[50.0, 0.00042730445196067818],
[55.0, 9.2094566445921082e-05],
[60.0, 2.3533619494819096e-17],
[65.0, 6.8535505965721361e-15],
[70.0, 2.3163309687227612e-12],
[75.0, 4.7244481305730235e-14],
[80.0, 3.946900273532476e-15],
[85.0, 1.387482842660183e-12],
[90.0, 1.169953999346581e-10],
[95.0, 9.405541221977168e-06],
[100.0, 0.00020815494180476587]],
[[0.0, 0.093092478749998001],
[10.0, 1.4053195886715078e-07],
[15.0, 2.4433977582117203e-05],
[20.0, 0.00082219857285037044],
[25.0, 2.6948063300386935e-05],
[30.0, 0.093959576222220995],
[35.0, 0.0001656191073324495],
[40.0, 0.0029118221893314833],
[45.0, 0.01564108273403109],
[50.0, 1.6648778174359671e-05],
[55.0, 2.8100410030581244e-07],
[60.0, 6.0753841480616879e-17],
[65.0, 9.8080692203972863e-15],
[70.0, 6.9694730549144334e-17],
[75.0, 5.8326312613461052e-15],
[80.0, 2.4139075497471945e-16],
[85.0, 5.9084558298527006e-13],
[90.0, 1.6711579730178305e-10],
[95.0, 1.5864098433225844e-08],
[100.0, 2.2572886039178069e-08]],
[[0.0, 4.7418107222805056e-05],
[10.0, 0.72289205649751387],
[15.0, 3.308651897500352e-07],
[20.0, 0.076673642863552008],
[25.0, 0.0001501767582823212],
[30.0, 0.03027984877348755],
[35.0, 0.00011292381988867554],
[40.0, 0.00016702076039256828],
[45.0, 0.00081579620071354846],
[50.0, 1.0458962769769357e-05],
[55.0, 5.5802811319507686e-08],
[60.0, 4.1253613398572781e-19],
[65.0, 9.4051858824518925e-18],
[70.0, 1.3368786287741004e-14],
[75.0, 5.9931111040137393e-14],
[80.0, 3.2539929633588511e-15],
[85.0, 1.047255336132784e-15],
[90.0, 1.4017813965595768e-12],
[95.0, 2.8511569193363785e-09],
[100.0, 1.0183773063292469e-10]],
[[0.0, 0.0045609821258978814],
[10.0, 0.0001917787950273313],
[15.0, 0.99771505193318455],
[20.0, 0.0055938427174407005],
[25.0, 0.013201240944903663],
[30.0, 0.018570016510629072],
[35.0, 0.012700626279918454],
[40.0, 0.019933344753975846],
[45.0, 0.15619609747558014],
[50.0, 0.031265415297255877],
[55.0, 0.42528804082092103],
[60.0, 1.3054822476679266e-18],
[65.0, 8.152405640596557e-18],
[70.0, 1.4328231368499457e-17],
[75.0, 1.3158371543509022e-15],
[80.0, 2.0369465623715189e-18],
[85.0, 1.1462339774892004e-13],
[90.0, 4.7664892870840778e-13],
[95.0, 7.2181814960747638e-06],
[100.0, 0.0011762750538733889]],
[[0.0, 1.4364539307817039e-05],
[10.0, 0.0002382809395285733],
[15.0, 1.1335942314786911e-05],
[20.0, 0.52821149082545871],
[25.0, 0.1355189607672023],
[30.0, 0.0013765391762318477],
[35.0, 0.01044957461521267],
[40.0, 0.00056647346484793787],
[45.0, 0.00076442899303849577],
[50.0, 0.032497831121074787],
[55.0, 0.00032304336212375635],
[60.0, 8.7317718742465198e-16],
[65.0, 6.539726198592529e-12],
[70.0, 9.2846001586370999e-08],
[75.0, 1.1275715667447136e-16],
[80.0, 9.3449145004368205e-16],
[85.0, 7.6700176188571133e-13],
[90.0, 3.6113266426134806e-09],
[95.0, 8.8657633159259348e-07],
[100.0, 1.0088131613242799e-08]],
[[0.0, 0.00010259026745556786],
[10.0, 2.8416604131363134e-05],
[15.0, 1.5265724585429075e-08],
[20.0, 0.00035572734341432835],
[25.0, 0.74774814405543055],
[30.0, 0.20170120434943195],
[35.0, 0.31688948032690523],
[40.0, 0.016495873067229414],
[45.0, 0.019452385467953772],
[50.0, 0.0063096344918544449],
[55.0, 1.4586177114635856e-07],
[60.0, 1.2622740425026807e-15],
[65.0, 1.6230117141451513e-13],
[70.0, 2.7328268483555935e-15],
[75.0, 5.9099377394502244e-17],
[80.0, 1.0947896137035609e-17],
[85.0, 2.2319783866749845e-12],
[90.0, 7.3069777072490827e-10],
[95.0, 1.2982219252486507e-07],
[100.0, 9.2703590554295812e-09]],
[[0.0, 5.6467274191670147e-06],
[10.0, 1.9311914301356571e-15],
[15.0, 0.00030373592016130291],
[20.0, 0.37222022990454917],
[25.0, 7.1237862206873578e-07],
[30.0, 0.016227799829528728],
[35.0, 0.33096651567634761],
[40.0, 0.00014104696271763757],
[45.0, 0.001730151916132917],
[50.0, 0.00019647094586097279],
[55.0, 1.61437439554684e-14],
[60.0, 7.1139359284635841e-65],
[65.0, 8.3871906097721264e-63],
[70.0, 1.2336993844192023e-53],
[75.0, 5.0051598531858931e-64],
[80.0, 2.9758606051614185e-62],
[85.0, 2.8460978574131379e-51],
[90.0, 2.8677742500204446e-37],
[95.0, 3.7194535849027315e-27],
[100.0, 2.3439805162091607e-24]],
[[0.0, 0.0001430560406790707],
[10.0, 6.2797052001508247e-13],
[15.0, 4.8114669550502021e-06],
[20.0, 0.0007443231772534647],
[25.0, 0.00061070912573869406],
[30.0, 0.48116582167944905],
[35.0, 0.24698643991977953],
[40.0, 0.016407283121225951],
[45.0, 0.2557158314329116],
[50.0, 1.1252231121357235e-05],
[55.0, 0.064666668633158647],
[60.0, 1.7631447655837744e-17],
[65.0, 1.1294722466816786e-14],
[70.0, 2.9419020411134367e-16],
[75.0, 3.0887653014525822e-17],
[80.0, 4.4973693062706866e-17],
[85.0, 9.0975358174005147e-15],
[90.0, 1.0758266454985257e-10],
[95.0, 7.2923752473657924e-08],
[100.0, 1.8065366882584036e-08]],
[[0.0, 8.4950625072720679e-07],
[10.0, 1.1421138441091606e-09],
[15.0, 4.5866299179754217e-05],
[20.0, 1.5172948619703533e-06],
[25.0, 0.00052404930089682738],
[30.0, 0.0013085043037665261],
[35.0, 0.0011664239519834339],
[40.0, 0.82750281205997822],
[45.0, 0.10132991062898908],
[50.0, 0.0085433968529198406],
[55.0, 1.4344253354768514e-09],
[60.0, 4.1465759737420333e-63],
[65.0, 8.8542318068147446e-61],
[70.0, 6.5220929006588264e-57],
[75.0, 4.8147063172017478e-62],
[80.0, 1.6649888561929801e-54],
[85.0, 5.8101309745184615e-51],
[90.0, 1.0489469511522901e-34],
[95.0, 1.4172891722901137e-26],
[100.0, 7.7737342215176175e-21]],
[[0.0, 3.1632028979839915e-05],
[10.0, 0.22111326362785799],
[15.0, 5.0749184816502276e-05],
[20.0, 0.0037647084552139601],
[25.0, 0.06595171605012351],
[30.0, 0.020353996100110065],
[35.0, 0.064862755938758426],
[40.0, 0.063464515818315706],
[45.0, 0.28662321979050187],
[50.0, 0.29871027240136533],
[55.0, 0.0080037249999781925],
[60.0, 4.7989170718538567e-08],
[65.0, 7.1337119197448125e-13],
[70.0, 2.1156334756020827e-14],
[75.0, 5.2206045634456785e-13],
[80.0, 3.4493997391226592e-14],
[85.0, 1.0500710333351341e-12],
[90.0, 3.6230960591527929e-10],
[95.0, 5.4508034466239748e-07],
[100.0, 1.113734841284331e-08]],
[[0.0, 2.1072826422141414e-05],
[10.0, 5.2005089026009156e-06],
[15.0, 0.00035632479168291582],
[20.0, 0.00020117324964893726],
[25.0, 0.025321662490265816],
[30.0, 0.019375041125499575],
[35.0, 0.0072489358231882196],
[40.0, 0.027646703948782582],
[45.0, 0.061781297188577035],
[50.0, 0.61650443678873623],
[55.0, 6.2548100028575296e-10],
[60.0, 4.3624321149810504e-14],
[65.0, 2.1028732991018519e-15],
[70.0, 7.8258766740408378e-09],
[75.0, 2.3541542464960154e-15],
[80.0, 7.1311782140494435e-18],
[85.0, 7.9730756880702952e-14],
[90.0, 8.2168425453267622e-12],
[95.0, 2.7060634831745863e-07],
[100.0, 2.5445845858147043e-06]],
[[0.0, 5.2908114997246604e-05],
[10.0, 0.01017193185840104],
[15.0, 1.9357672782766133e-07],
[20.0, 0.00033902736145621922],
[25.0, 2.6389276490704271e-05],
[30.0, 0.018976734061801625],
[35.0, 0.00019847993445041843],
[40.0, 0.0003987651813445142],
[45.0, 0.00077280097799644102],
[50.0, 7.0623545425863526e-06],
[55.0, 0.49987468099653093],
[60.0, 2.2781104904754321e-10],
[65.0, 3.6325273589160318e-08],
[70.0, 1.3537824953350784e-07],
[75.0, 1.6173509221250697e-06],
[80.0, 1.4416343176395238e-08],
[85.0, 4.1563319348145668e-08],
[90.0, 3.889471307678879e-07],
[95.0, 5.5104492670400779e-05],
[100.0, 2.7151502910109198e-05]],
[[0.0, 6.7973224793022393e-05],
[10.0, 2.4433844497448081e-05],
[15.0, 3.1104921306578064e-06],
[20.0, 0.00034408444849371377],
[25.0, 0.00023308492218191118],
[30.0, 0.011131477320772989],
[35.0, 0.00065907967432373565],
[40.0, 0.0021975971207933783],
[45.0, 0.0064663180932303744],
[50.0, 0.00022182805626473468],
[55.0, 3.0364808672527216e-05],
[60.0, 0.5088867111766856],
[65.0, 0.022878262885332335],
[70.0, 4.5920971324139236e-06],
[75.0, 0.0022218172448998821],
[80.0, 6.2738973247353222e-07],
[85.0, 0.00029845009911656876],
[90.0, 0.00010489918264551884],
[95.0, 0.00060753276898116491],
[100.0, 0.00022654293893088778]],
[[0.0, 3.9706684562645418e-06],
[10.0, 5.1270388737533639e-06],
[15.0, 5.262507305695827e-08],
[20.0, 2.5999077691490454e-05],
[25.0, 2.7813624200874083e-05],
[30.0, 0.0031991625978026266],
[35.0, 7.9249804143628584e-05],
[40.0, 0.00041177406702603777],
[45.0, 0.0011392701023576817],
[50.0, 8.0323465327795955e-06],
[55.0, 4.8154250431354482e-08],
[60.0, 0.49105543193823659],
[65.0, 0.7287168056069141],
[70.0, 0.001517224119720642],
[75.0, 0.011333832180200976],
[80.0, 1.72411085405819e-06],
[85.0, 0.0024773347817556406],
[90.0, 0.0051538392104878905],
[95.0, 0.0012157783163112316],
[100.0, 6.9627245730982824e-06]],
[[0.0, 0.0002912083712195997],
[10.0, 0.00010000827323660753],
[15.0, 1.3045847050921363e-05],
[20.0, 0.00076214254175277312],
[25.0, 0.00069053586992726265],
[30.0, 0.019496501720614564],
[35.0, 0.0015575117915095839],
[40.0, 0.0061672295672820353],
[45.0, 0.024249792592964857],
[50.0, 0.00032644214446402552],
[55.0, 7.7266514916862301e-06],
[60.0, 7.7205272501591181e-06],
[65.0, 0.017897081479520592],
[70.0, 0.99798778384755971],
[75.0, 1.8624562046616415e-05],
[80.0, 0.0011212071784075478],
[85.0, 1.4365766090813676e-05],
[90.0, 0.0032064950461695454],
[95.0, 0.0012746996733909491],
[100.0, 0.002184674454638652]],
[[0.0, 1.4875831960944082e-05],
[10.0, 0.044356141901610378],
[15.0, 6.2599114973306784e-07],
[20.0, 0.0001137512258409283],
[25.0, 0.00074658171939148493],
[30.0, 0.0026392679644018989],
[35.0, 0.00016209963633384973],
[40.0, 0.00078042928094971609],
[45.0, 0.0036289420011120584],
[50.0, 0.0015766136061278139],
[55.0, 1.0905709141111546e-05],
[60.0, 2.7323842104187985e-06],
[65.0, 3.3133452546150147e-05],
[70.0, 9.4042250154063172e-07],
[75.0, 0.97971055505602644],
[80.0, 0.072870311800762033],
[85.0, 0.00053021457261792214],
[90.0, 0.022423571257812192],
[95.0, 0.00070111679082283159],
[100.0, 3.6497263003914267e-05]],
[[0.0, 3.5864031562522839e-05],
[10.0, 8.3611261574776709e-07],
[15.0, 3.1900347488304889e-06],
[20.0, 0.00018123337202990332],
[25.0, 0.0002136705754060659],
[30.0, 0.0057251370200655845],
[35.0, 0.0012906924788080578],
[40.0, 0.0025502956186607153],
[45.0, 0.01424309136060831],
[50.0, 6.8216937833192329e-05],
[55.0, 5.6413951413567533e-06],
[60.0, 3.395689693128361e-05],
[65.0, 0.0032457520420234795],
[70.0, 8.3915675399849457e-09],
[75.0, 0.00013330080558625364],
[80.0, 0.91501828269209085],
[85.0, 0.0020937770133090659],
[90.0, 0.0012871409503378462],
[95.0, 0.00014758805743257718],
[100.0, 0.00086787665688324784]],
[[0.0, 4.1652247577331996e-06],
[10.0, 1.2212829713673957e-06],
[15.0, 6.5906857192417344e-08],
[20.0, 0.00016745946587138236],
[25.0, 0.0054431111796765554],
[30.0, 0.0067575214586160616],
[35.0, 0.00011856110316632124],
[40.0, 0.00032181662132509944],
[45.0, 0.001397981055516994],
[50.0, 0.0027058954834684062],
[55.0, 2.553142406703067e-06],
[60.0, 1.1514033594755017e-08],
[65.0, 0.21961568282994792],
[70.0, 2.4658349829099807e-08],
[75.0, 0.0022850986575076743],
[80.0, 3.5603047823624507e-06],
[85.0, 0.99406392082894734],
[90.0, 0.24399923235645221],
[95.0, 0.0013470125217945798],
[100.0, 0.042582366972883985]],
[[0.0, 5.5827483149344828e-06],
[10.0, 4.2094534384536198e-07],
[15.0, 5.723201102401161e-08],
[20.0, 4.1809740042541515e-05],
[25.0, 0.00049057556903029373],
[30.0, 0.0073861106939193556],
[35.0, 0.00025382321327730041],
[40.0, 0.0013088484764716428],
[45.0, 0.0026974167667276621],
[50.0, 3.069464890989831e-05],
[55.0, 5.8148299885777761e-06],
[60.0, 7.4621165800794304e-07],
[65.0, 0.002374761247356731],
[70.0, 7.989880181871685e-05],
[75.0, 0.0042580794657908335],
[80.0, 0.010965726532792501],
[85.0, 0.00032799281228739028],
[90.0, 0.72335894920682331],
[95.0, 0.0028560935416300647],
[100.0, 0.00023222597525614469]],
[[0.0, 1.1150563179140846e-05],
[10.0, 3.9137754880943888e-06],
[15.0, 2.6540828280675093e-07],
[20.0, 0.00030085951063170473],
[25.0, 0.00099029668428149789],
[30.0, 0.0040087490203686207],
[35.0, 0.00086288885669378408],
[40.0, 0.0012383336640490626],
[45.0, 0.0024569535179785368],
[50.0, 5.3078640889812177e-05],
[55.0, 3.655855476404959e-05],
[60.0, 1.135303263695363e-05],
[65.0, 0.0052337161196729541],
[70.0, 0.00038799120104653003],
[75.0, 6.6611573170762462e-07],
[80.0, 1.551926769812043e-05],
[85.0, 3.0383118936603649e-05],
[90.0, 0.00045596510625991982],
[95.0, 0.97142691361436007],
[100.0, 0.10797398637691005]],
[[0.0, 0.0011842207743420531],
[10.0, 0.00021255186566113124],
[15.0, 0.0014186746952926585],
[20.0, 0.0012807740120210007],
[25.0, 0.0017758699839103857],
[30.0, 0.02941486302872582],
[35.0, 0.0028808476159808511],
[40.0, 0.0053310120235299015],
[45.0, 0.030475142227338822],
[50.0, 0.00050901345787313437],
[55.0, 0.0016516486463798513],
[60.0, 1.288101329664901e-06],
[65.0, 4.7680039665889616e-06],
[70.0, 2.1300407821392148e-05],
[75.0, 3.6408560648746925e-05],
[80.0, 3.0263064939939249e-06],
[85.0, 0.00016351943738745334],
[90.0, 9.5136297575506478e-06],
[95.0, 0.020349612775663113],
[100.0, 0.84447466931781623]]])
With plt.savefig you can create an individual .png file from a plot. To get a smooth transition, you can interpolate the plots in a number of steps. To convert the .png-files to an animation, ffmpeg is a very useful tool.
Here is some code starting from the data of your previous question:
from matplotlib import pyplot as plt
import numpy as np
from scipy.stats import gaussian_kde
p1 = np.array([[5.0, 0.0001430560406790707], [10.0, 6.2797052001508247e-13], [15.0, 4.8114669550502021e-06],
[20.0, 0.0007443231772534647], [25.0, 0.00061070912573869406], [30.0, 0.48116582167944905],
[35.0, 0.24698643991977953], [40.0, 0.016407283121225951], [45.0, 0.2557158314329116],
[50.0, 1.1252231121357235e-05], [55.0, 0.064666668633158647], [60.0, 1.7631447655837744e-17],
[65.0, 1.1294722466816786e-14], [70.0, 2.9419020411134367e-16], [75.0, 3.0887653014525822e-17],
[80.0, 4.4973693062706866e-17], [85.0, 9.0975358174005147e-15], [90.0, 1.0758266454985257e-10],
[95.0, 7.2923752473657924e-08], [100.0, 1.8065366882584036e-08]])
p2 = np.array([[5.0, 4.1652247577331996e-06], [10.0, 1.2212829713673957e-06], [15.0, 6.5906857192417344e-08],
[20.0, 0.00016745946587138236], [25.0, 0.0054431111796765554], [30.0, 0.0067575214586160616],
[35.0, 0.00011856110316632124], [40.0, 0.00032181662132509944], [45.0, 0.001397981055516994],
[50.0, 0.0027058954834684062], [55.0, 2.553142406703067e-06], [60.0, 1.1514033594755017e-08],
[65.0, 0.21961568282994792], [70.0, 2.4658349829099807e-08], [75.0, 0.0022850986575076743],
[80.0, 3.5603047823624507e-06], [85.0, 0.99406392082894734], [90.0, 0.24399923235645221],
[95.0, 0.0013470125217945798], [100.0, 0.042582366972883985]])
x = np.linspace(0, 100, 1000)
hlist = [p1, p2, p1]
kdelist = [gaussian_kde(p[:, 0], bw_method=.4, weights=p[:, 1]) for p in hlist]
steps = 20
fig, ax = plt.subplots()
for i in range(len(kdelist) - 1):
for s in range(steps + 1):
plt.plot(x, kdelist[i](x) * s / steps + kdelist[i + 1](x) * (1 - s / steps), color='crimson')
plt.ylim(0, 0.065)
plt.savefig(f'kde_{i*(steps+1)+s:04d}.png')
plt.cla() # needed to remove the plot because savefig doesn't clear it
An example way to convert the .png files to a .mp4 animation and then to an animated gif:
ffmpeg -i kde_%04d.png -c:v libx264 -r 25 -pix_fmt yuv420p out.mp4
ffmpeg -i out.mp4 out.gif
I have an numpy array called expected which is a list of a list of a list.
expected = [[[45.0, 10.0, 10.0], [110.0, 10.0, 8.0], [60.0, 10.0, 5.0], [170.0, 10.0, 4.0]], [[-80.0, 20.0, 10.0], [97.0, 15.0, 12.0], [5.0, 20.0, 8.0], [93.0, 10.0, 8.0], [12.0, 5.0, 15.0], [-88.0, 10.0, 10.0], [176.0, 10.0, 8.0]]]
I want to put it through a loop without having to hardcode so its applicable to different lengths of list.
When the loop runs for the first time i want it to solve this:
horizontal_exp = expected[0][0][1]*expected[0][0][2]
*np.cos(np.deg2rad(expected[0][0][0]))
Then the next loop to be like this:
horizontal_exp = expected[1][1][1]*expected[1][1][2]
*np.cos(np.deg2rad(expected[1][1][0]))
And the following loop to be like this:
horizontal_exp = expected[2][2][1]*expected[2][2][2]
*np.cos(np.deg2rad(expected[2][2][0]))
and so on until it finished the different sections of rows.
I don't understand why the 'i' never worked??
In the end I want horizontal expected to be a list of a list
e.g.
expected = [ [12,21,23,34], [12,32,54,65,76,87,65] ] # These are not the values I'm just giving an example
where the [12,21,23,24] corresponds to the [[45.0, 10.0, 10.0], [110.0, 10.0, 8.0], [60.0, 10.0, 5.0], [170.0, 10.0, 4.0]]
and the [12,32,54,65,76,87,65] corresponds to the [[-80.0, 20.0, 10.0], [97.0, 15.0, 12.0], [5.0, 20.0, 8.0], [93.0, 10.0, 8.0], [12.0, 5.0, 15.0], [-88.0, 10.0, 10.0], [176.0, 10.0, 8.0]]
I'm unsure how to do this, I know you have to append it with a for loop but how do you separate it into a list of a list??
horizontal_expected = []
for i in list(range(len(expected[i]))):
horizontal_exp = expected[i][i][1]*expected[i][i][2]
*np.cos(np.deg2rad(expected[i][i][0]))
horizontal_expected.append(horizontal_exp)
print(horizontal_expected)
The reason why you don't see the desired output is that, even though you have nested list expected, you are iterating only through the nested lists. You first need to iterate through the outer lists and then iterate through the nested lists internally:
import numpy as np
expected = [ [[45.0, 10.0, 10.0], [110.0, 10.0, 8.0], [60.0, 10.0, 5.0], [170.0, 10.0, 4.0]], [[-80.0, 20.0, 10.0], [97.0, 15.0, 12.0], [5.0, 20.0, 8.0], [93.0, 10.0, 8.0], [12.0, 5.0, 15.0], [-88.0, 10.0, 10.0], [176.0, 10.0, 8.0]] ]
horizontal_expected = []
for i in range(len(expected)):
tmp_list = []
for j in range(len(expected[i])):
horizontal_exp = expected[i][i][1]*expected[i][i][2]*np.cos(np.deg2rad(expected[i][i][0]))
tmp_list.append(horizontal_exp)
horizontal_expected.append(tmp_list)
print(horizontal_expected)
The output of that is a list of lists:
>>> print(horizontal_expected)
[[70.71067811865476, 70.71067811865476, 70.71067811865476, 70.71067811865476], [-21.936481812926527, -21.936481812926527, -21.936481812926527, -21.936481812926527, -21.936481812926527, -21.936481812926527, -21.936481812926527]]
As you can see, it holds a value for each of the lists in the input, but the value is the same. This is due to the way that your equation was set up.
You want the indices to be updated based on the level of the loop:
horizontal_exp = expected[i][j][1]*expected[i][j][2]*np.cos(np.deg2rad(expected[i][j][0]))
The full working code would look like this:
import numpy as np
expected = [ [[45.0, 10.0, 10.0], [110.0, 10.0, 8.0], [60.0, 10.0, 5.0], [170.0, 10.0, 4.0]], [[-80.0, 20.0, 10.0], [97.0, 15.0, 12.0], [5.0, 20.0, 8.0], [93.0, 10.0, 8.0], [12.0, 5.0, 15.0], [-88.0, 10.0, 10.0], [176.0, 10.0, 8.0]] ]
horizontal_expected = []
for i in range(len(expected)):
tmp_list = []
for j in range(len(expected[i])):
horizontal_exp = expected[i][j][1]*expected[i][j][2]*np.cos(np.deg2rad(expected[i][j][0]))
tmp_list.append(horizontal_exp)
horizontal_expected.append(tmp_list)
print(horizontal_expected)
And the output:
>>> print(horizontal_expected)
[[70.71067811865476, -27.361611466053496, 25.000000000000007, -39.39231012048832], [34.72963553338608, -21.936481812926527, 159.39115169467928, -4.186876499435507, 73.36107005503543, 3.489949670250108, -79.80512402078594]]
I have two lists: a and b. I want to concatenate all of the elements of the b that are between elements of a. All of the elements of a are in b, but b also has some extra elements that are extraneous. I would like to take the first instance of every element of a in b and concatenate it with the extraneous elements that follow it in b until we find another element of a in b. The following example should make it more clear.
a = [[11.0, 1.0], [11.0, 2.0], [11.0, 3.0], [11.0, 4.0], [11.0, 5.0], [12.0, 1.0], [12.0, 2.0], [12.0, 3.0], [12.0, 4.0], [12.0, 5.0], [12.0, 6.0], [12.0, 7.0], [12.0, 8.0], [12.0, 9.0], [12.0, 10.0], [12.0, 11.0], [12.0, 12.0], [12.0, 13.0], [12.0, 14.0], [13.0, 1.0], [13.0, 2.0], [13.0, 3.0], [13.0, 4.0], [13.0, 5.0], [13.0, 6.0], [13.0, 7.0], [13.0, 8.0], [13.0, 9.0], [13.0, 10.0]]
b = [[11.0, 1.0], [11.0, 1.0], [1281.0, 8.0], [11.0, 2.0], [11.0, 3.0], [11.0, 3.0], [11.0, 4.0], [11.0, 5.0], [12.0, 1.0], [12.0, 2.0], [12.0, 3.0], [12.0, 4.0], [12.0, 5.0], [12.0, 6.0], [12.0, 7.0], [12.0, 5.0], [12.0, 8.0], [12.0, 9.0], [12.0, 10.0], [13.0, 5.0], [12.0, 11.0], [12.0, 8.0], [3.0, 1.0], [13.0, 1.0], [9.0, 7.0], [12.0, 12.0], [12.0, 13.0], [12.0, 14.0], [13.0, 1.0], [13.0, 2.0], [11.0, 3.0], [13.0, 3.0], [13.0, 4.0], [13.0, 5.0], [13.0, 5.0], [13.0, 5.0], [13.0, 6.0], [13.0, 7.0], [13.0, 7.0], [13.0, 8.0], [13.0, 9.0], [13.0, 10.0]]
c = [[[11.0, 1.0], [11.0, 1.0], [1281.0, 8.0]], [[11.0, 2.0]], [[11.0, 3.0], [11.0, 3.0]], [[11.0, 4.0]], [[11.0, 5.0]], [[12.0, 1.0]], [[12.0, 2.0]], [[12.0, 3.0]], [[12.0, 4.0]], [[12.0, 5.0]], [[12.0, 6.0]], [[12.0, 7.0], [12.0, 5.0]], [[12.0, 8.0]], [[12.0, 9.0]], [[12.0, 10.0], [13.0, 5.0]], [[12.0, 11.0], [12.0, 8.0], [3.0, 1.0]], [[13.0, 1.0], [9.0, 7.0], [12.0, 12.0], [12.0, 13.0], [12.0, 14.0], [13.0, 1.0]], [[13.0, 2.0]], [[11.0, 3.0], [13.0, 3.0]], [[13.0, 4.0]], [[13.0, 5.0], [13.0, 5.0], [13.0, 5.0]], [[13.0, 6.0]], [[13.0, 7.0], [13.0, 7.0]], [[13.0, 8.0]], [[13.0, 9.0]], [[13.0, 10.0]]]
What I have thought of is something like this:
slice_list = []
for i, elem in enumerate(a):
if i < len(key_list)-1:
b_first_index = b.index(a[i])
b_second_index = b.index(a[i+1])
slice_list.append([b_first_index, b_second_index])
c = [[b[slice_list[i][0]:b[slice_list[i][1]]]] for i in range(len(slice_list))]
This however will not catch the last item in the list (which I am not quite sure how to fit into my list comprehension anyways) and it seems quite ugly. My question is, is there a neater way of doing this (perhaps in itertools)?
Let's simplify the visual a bit:
key_list = ['a', 'c', 'f']
wrong_list = ['a', 'b', 'c', 'd', 'e', 'f']
wrong_list_fixed = [['a', 'b'], ['c', 'd', 'e'], ['f']]
This will be semantically identical to what you have, but I think it is easier to see without all the extra nested brackets.
You could use itertools.groupby, if you could only come up with a clever key. Luckily, the mapping of key_list to wrong_list givs you exactly what you want:
class key:
def __init__(self, key_list):
self.last = -1
self.key_list = key_list
def __call__(self, item):
try:
self.last = self.key_list.index(item, self.last + 1)
except ValueError:
pass
return self.last
wrong_list_fixed = [list(g) for k, g in itertools.groupby(wrong_list, key(key_list))]
The key maps elements of wrong_list to key_list using index. For missing indices, it just returns the last one successfully found, ensuring that groups are not split until a new index is found. By starting the search from the next available index, you can ensure that duplicate entries in key_list get handled correctly.
[IDEOne Link]
I think your example wrong_list_fixed is incorrect.
[[12.0, 10.0], [13.0, 5.0], [12.0, 11.0], [12.0, 8.0],
# There should be a new list here -^
Here's a solution that walks the lists. It can be optimized further:
from contextlib import suppress
fixed = []
current = []
key_list_iter = iter(key_list)
next_key = next(key_list_iter)
for wrong in wrong_list:
if wrong == next_key:
if current:
fixed.append(current)
current = []
next_key = None
with suppress(StopIteration):
next_key = next(key_list_iter)
current.append(wrong)
if current:
fixed.append(current)
Here are the correct lists (modified to be easier to visually parse):
key_list = ['_a0', '_b0', '_c0', '_d0', '_e0', '_f0', '_g0', '_h0', '_i0', '_j0', '_k0', '_l0', '_m0', '_n0', '_o0', '_p0', '_q0', '_r0', '_s0', '_t0', '_u0', '_v0', '_w0', '_x0', '_y0', '_z0', '_A0', '_B0', '_C0']
wrong_list = ['_a0', '_a0', 'D0', '_b0', '_c0', '_c0', '_d0', '_e0', '_f0', '_g0', '_h0', '_i0', '_j0', '_k0', '_l0', '_j0', '_m0', '_n0', '_o0', '_x0', '_p0', '_m0', 'E0', '_t0', 'F0', '_q0', '_r0', '_s0', '_t0', '_u0', '_c0', '_v0', '_w0', '_x0', '_x0', '_x0', '_y0', '_z0', '_z0', '_A0', '_B0', '_C0']
wrong_list_fixed = [['_a0', '_a0', 'D0'], ['_b0'], ['_c0', '_c0'], ['_d0'], ['_e0'], ['_f0'], ['_g0'], ['_h0'], ['_i0'], ['_j0'], ['_k0'], ['_l0', '_j0'], ['_m0'], ['_n0'], ['_o0', '_x0'], ['_p0', '_m0', 'E0', '_t0', 'F0'], ['_q0'], ['_r0'], ['_s0'], ['_t0'], ['_u0', '_c0'], ['_v0'], ['_w0'], ['_x0', '_x0', '_x0'], ['_y0'], ['_z0', '_z0'], ['_A0'], ['_B0'], ['_C0']]
I get slightly different result from yours, but give it a try. If this is not what you want, I will delete my answer.
idx = sorted(set([b.index(ai) for ai in a] + [len(b)]))
c = [b[i:j] for i, j in zip(idx[:-1], idx[1:])]
I have (x,y) pixel coordinates for six points, in a subset of frames from a video clip. I would like to generate a smooth interpolator for (x, y) pixel values for frames (time) in-between these data points.
They are shown below both as trajectories in x, y, and individually versus time.
In scipy.interpolate there is a very generous selection of possible choices (many different kinds of interpolation methods). My question is: where should I start?? I'm looking for a recommendation from someone with a bit of experience with interpolation. Of all the available interpolation methods in SciPy, which ones should I try to learn first in order to interpolate this and similar data?
Some of the available methods can fit x and y together - e.g. MultivariateSplines but not sure if there is an advantage to treating them together, or independently.
note: I am working with NumPy and SciPy only, please don't recommend other packages. Thanks!
If you would like to see the data, it's all here in the paths = np.array... line:
import numpy as np
import matplotlib.pyplot as plt
# this line is very long - has all the data for the plot!
paths = np.array([[[45.0, 135.0], [42.0, 130.0], [41.0, 123.0], [41.0, 120.0], [38.0, 119.0], [35.0, 124.0], [34.0, 124.0], [36.0, 122.0], [36.0, 118.0], [37.0, 113.0], [41.0, 107.0], [45.0, 97.0], [49.0, 82.0], [54.0, 72.0], [56.0, 66.0], [57.0, 61.0], [58.0, 59.0], [57.0, 55.0], [-1, -1], [53.0, 47.0], [47.0, 41.0], [42.0, 35.0], [37.0, 27.0], [34.0, 23.0], [33.0, 21.0], [35.0, 23.0], [38.0, 23.0], [43.0, 21.0], [46.0, 17.0], [50.0, 16.0], [50.0, 19.0], [50.0, 26.0], [47.0, 35.0], [46.0, 41.0], [47.0, 44.0], [48.0, 48.0], [47.0, 51.0], [43.0, 55.0], [39.0, 56.0], [36.0, 52.0]], [[50.0, 139.0], [48.0, 135.0], [46.0, 128.0], [46.0, 125.0], [43.0, 124.0], [40.0, 128.0], [40.0, 130.0], [41.0, 127.0], [42.0, 124.0], [43.0, 120.0], [45.0, 112.0], [51.0, 102.0], [54.0, 91.0], [60.0, 79.0], [63.0, 70.0], [62.0, 67.0], [68.0, 62.0], [62.0, 62.0], [-1, -1], [63.0, 52.0], [53.0, 48.0], [49.0, 41.0], [44.0, 33.0], [40.0, 30.0], [40.0, 27.0], [41.0, 29.0], [44.0, 30.0], [48.0, 27.0], [52.0, 24.0], [56.0, 22.0], [58.0, 27.0], [56.0, 32.0], [54.0, 42.0], [52.0, 48.0], [53.0, 52.0], [55.0, 55.0], [54.0, 59.0], [50.0, 62.0], [45.0, 63.0], [42.0, 58.0]], [[113.0, 134.0], [110.0, 130.0], [110.0, 123.0], [110.0, 121.0], [107.0, 120.0], [105.0, 123.0], [105.0, 123.0], [107.0, 121.0], [108.0, 119.0], [110.0, 115.0], [113.0, 108.0], [118.0, 96.0], [-1, -1], [128.0, 72.0], [122.0, 68.0], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [-1, -1], [127.0, 40.0], [123.0, 35.0], [118.0, 29.0], [116.0, 24.0], [115.0, 21.0], [117.0, 22.0], [122.0, 23.0], [128.0, 19.0], [-1, -1], [136.0, 15.0], [-1, -1], [138.0, 26.0], [137.0, 35.0], [136.0, 41.0], [138.0, 43.0], [140.0, 48.0], [140.0, 50.0], [137.0, 54.0], [134.0, 55.0], [133.0, 51.0]], [[192.0, 154.0], [190.0, 148.0], [191.0, 140.0], [192.0, 137.0], [191.0, 137.0], [188.0, 142.0], [189.0, 143.0], [193.0, 139.0], [193.0, 137.0], [197.0, 134.0], [203.0, 128.0], [208.0, 117.0], [217.0, 104.0], [219.0, 93.0], [223.0, 85.0], [226.0, 81.0], [228.0, 78.0], [228.0, 76.0], [228.0, 73.0], [226.0, 68.0], [224.0, 63.0], [220.0, 58.0], [217.0, 52.0], [216.0, 46.0], [216.0, 42.0], [219.0, 42.0], [223.0, 45.0], [230.0, 46.0], [236.0, 48.0], [244.0, 50.0], [247.0, 50.0], [248.0, 50.0], [246.0, 55.0], [245.0, 61.0], [249.0, 71.0], [254.0, 79.0], [257.0, 87.0], [256.0, 89.0], [253.0, 87.0], [251.0, 80.0]], [[216.0, 144.0], [214.0, 139.0], [215.0, 131.0], [216.0, 129.0], [217.0, 127.0], [213.0, 133.0], [214.0, 133.0], [217.0, 130.0], [219.0, 128.0], [222.0, 125.0], [227.0, 118.0], [234.0, 107.0], [239.0, 94.0], [245.0, 82.0], [249.0, 76.0], [253.0, 72.0], [255.0, 69.0], [255.0, 67.0], [255.0, 62.0], [254.0, 57.0], [251.0, 52.0], [248.0, 48.0], [245.0, 42.0], [244.0, 36.0], [245.0, 31.0], [249.0, 32.0], [252.0, 33.0], [259.0, 34.0], [267.0, 38.0], [273.0, 38.0], [277.0, 38.0], [278.0, 39.0], [276.0, 43.0], [276.0, 50.0], [280.0, 58.0], [286.0, 67.0], [288.0, 74.0], [288.0, 76.0], [286.0, 74.0], [286.0, 67.0]], [[252.0, 149.0], [250.0, 143.0], [252.0, 137.0], [253.0, 133.0], [252.0, 132.0], [250.0, 137.0], [252.0, 137.0], [255.0, 134.0], [257.0, 132.0], [262.0, 130.0], [266.0, 123.0], [273.0, 111.0], [277.0, 97.0], [286.0, 86.0], [289.0, 80.0], [293.0, 77.0], [295.0, 75.0], [296.0, 71.0], [296.0, 66.0], [296.0, 61.0], [294.0, 57.0], [290.0, 52.0], [288.0, 48.0], [289.0, 42.0], [289.0, 37.0], [293.0, 35.0], [298.0, 37.0], [305.0, 39.0], [313.0, 42.0], [319.0, 43.0], [324.0, 44.0], [325.0, 44.0], [324.0, 49.0], [325.0, 55.0], [329.0, 63.0], [336.0, 71.0], [339.0, 78.0], [339.0, 80.0], [337.0, 79.0], [338.0, 73.0]]])
paths[paths<0] = np.nan # turn the -1's back into np.nan
plt.figure()
for path in paths:
x, y = path.T
plt.plot(x, y, linewidth=3)
plt.plot(x, y, '.k')
plt.show()
plt.figure()
for i, path in enumerate(paths):
plt.subplot(6, 1, i+1)
x, y = path.T
plt.plot(x)
plt.plot(x, '.k')
plt.plot(y)
plt.plot(y, '.k')
plt.show()
I'm using scipy.interpolate.InterpolatedUnivariateSpline.
One thing I had to deal with is that some of the methods tend to take an inordinate amount of memory space - the function above works well in that regard.