test_multiarray.py 321 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743
  1. import collections.abc
  2. import tempfile
  3. import sys
  4. import shutil
  5. import warnings
  6. import operator
  7. import io
  8. import itertools
  9. import functools
  10. import ctypes
  11. import os
  12. import gc
  13. import weakref
  14. import pytest
  15. from contextlib import contextmanager
  16. from numpy.compat import pickle
  17. import pathlib
  18. import builtins
  19. from decimal import Decimal
  20. import numpy as np
  21. import numpy.core._multiarray_tests as _multiarray_tests
  22. from numpy.core._rational_tests import rational
  23. from numpy.testing import (
  24. assert_, assert_raises, assert_warns, assert_equal, assert_almost_equal,
  25. assert_array_equal, assert_raises_regex, assert_array_almost_equal,
  26. assert_allclose, IS_PYPY, HAS_REFCOUNT, assert_array_less, runstring,
  27. temppath, suppress_warnings, break_cycles,
  28. )
  29. from numpy.testing._private.utils import _no_tracing
  30. from numpy.core.tests._locales import CommaDecimalPointLocale
  31. # Need to test an object that does not fully implement math interface
  32. from datetime import timedelta, datetime
  33. def _aligned_zeros(shape, dtype=float, order="C", align=None):
  34. """
  35. Allocate a new ndarray with aligned memory.
  36. The ndarray is guaranteed *not* aligned to twice the requested alignment.
  37. Eg, if align=4, guarantees it is not aligned to 8. If align=None uses
  38. dtype.alignment."""
  39. dtype = np.dtype(dtype)
  40. if dtype == np.dtype(object):
  41. # Can't do this, fall back to standard allocation (which
  42. # should always be sufficiently aligned)
  43. if align is not None:
  44. raise ValueError("object array alignment not supported")
  45. return np.zeros(shape, dtype=dtype, order=order)
  46. if align is None:
  47. align = dtype.alignment
  48. if not hasattr(shape, '__len__'):
  49. shape = (shape,)
  50. size = functools.reduce(operator.mul, shape) * dtype.itemsize
  51. buf = np.empty(size + 2*align + 1, np.uint8)
  52. ptr = buf.__array_interface__['data'][0]
  53. offset = ptr % align
  54. if offset != 0:
  55. offset = align - offset
  56. if (ptr % (2*align)) == 0:
  57. offset += align
  58. # Note: slices producing 0-size arrays do not necessarily change
  59. # data pointer --- so we use and allocate size+1
  60. buf = buf[offset:offset+size+1][:-1]
  61. data = np.ndarray(shape, dtype, buf, order=order)
  62. data.fill(0)
  63. return data
  64. class TestFlags:
  65. def setup(self):
  66. self.a = np.arange(10)
  67. def test_writeable(self):
  68. mydict = locals()
  69. self.a.flags.writeable = False
  70. assert_raises(ValueError, runstring, 'self.a[0] = 3', mydict)
  71. assert_raises(ValueError, runstring, 'self.a[0:1].itemset(3)', mydict)
  72. self.a.flags.writeable = True
  73. self.a[0] = 5
  74. self.a[0] = 0
  75. def test_writeable_any_base(self):
  76. # Ensure that any base being writeable is sufficient to change flag;
  77. # this is especially interesting for arrays from an array interface.
  78. arr = np.arange(10)
  79. class subclass(np.ndarray):
  80. pass
  81. # Create subclass so base will not be collapsed, this is OK to change
  82. view1 = arr.view(subclass)
  83. view2 = view1[...]
  84. arr.flags.writeable = False
  85. view2.flags.writeable = False
  86. view2.flags.writeable = True # Can be set to True again.
  87. arr = np.arange(10)
  88. class frominterface:
  89. def __init__(self, arr):
  90. self.arr = arr
  91. self.__array_interface__ = arr.__array_interface__
  92. view1 = np.asarray(frominterface)
  93. view2 = view1[...]
  94. view2.flags.writeable = False
  95. view2.flags.writeable = True
  96. view1.flags.writeable = False
  97. view2.flags.writeable = False
  98. with assert_raises(ValueError):
  99. # Must assume not writeable, since only base is not:
  100. view2.flags.writeable = True
  101. def test_writeable_from_readonly(self):
  102. # gh-9440 - make sure fromstring, from buffer on readonly buffers
  103. # set writeable False
  104. data = b'\x00' * 100
  105. vals = np.frombuffer(data, 'B')
  106. assert_raises(ValueError, vals.setflags, write=True)
  107. types = np.dtype( [('vals', 'u1'), ('res3', 'S4')] )
  108. values = np.core.records.fromstring(data, types)
  109. vals = values['vals']
  110. assert_raises(ValueError, vals.setflags, write=True)
  111. def test_writeable_from_buffer(self):
  112. data = bytearray(b'\x00' * 100)
  113. vals = np.frombuffer(data, 'B')
  114. assert_(vals.flags.writeable)
  115. vals.setflags(write=False)
  116. assert_(vals.flags.writeable is False)
  117. vals.setflags(write=True)
  118. assert_(vals.flags.writeable)
  119. types = np.dtype( [('vals', 'u1'), ('res3', 'S4')] )
  120. values = np.core.records.fromstring(data, types)
  121. vals = values['vals']
  122. assert_(vals.flags.writeable)
  123. vals.setflags(write=False)
  124. assert_(vals.flags.writeable is False)
  125. vals.setflags(write=True)
  126. assert_(vals.flags.writeable)
  127. @pytest.mark.skipif(IS_PYPY, reason="PyPy always copies")
  128. def test_writeable_pickle(self):
  129. import pickle
  130. # Small arrays will be copied without setting base.
  131. # See condition for using PyArray_SetBaseObject in
  132. # array_setstate.
  133. a = np.arange(1000)
  134. for v in range(pickle.HIGHEST_PROTOCOL):
  135. vals = pickle.loads(pickle.dumps(a, v))
  136. assert_(vals.flags.writeable)
  137. assert_(isinstance(vals.base, bytes))
  138. def test_writeable_from_c_data(self):
  139. # Test that the writeable flag can be changed for an array wrapping
  140. # low level C-data, but not owning its data.
  141. # Also see that this is deprecated to change from python.
  142. from numpy.core._multiarray_tests import get_c_wrapping_array
  143. arr_writeable = get_c_wrapping_array(True)
  144. assert not arr_writeable.flags.owndata
  145. assert arr_writeable.flags.writeable
  146. view = arr_writeable[...]
  147. # Toggling the writeable flag works on the view:
  148. view.flags.writeable = False
  149. assert not view.flags.writeable
  150. view.flags.writeable = True
  151. assert view.flags.writeable
  152. # Flag can be unset on the arr_writeable:
  153. arr_writeable.flags.writeable = False
  154. arr_readonly = get_c_wrapping_array(False)
  155. assert not arr_readonly.flags.owndata
  156. assert not arr_readonly.flags.writeable
  157. for arr in [arr_writeable, arr_readonly]:
  158. view = arr[...]
  159. view.flags.writeable = False # make sure it is readonly
  160. arr.flags.writeable = False
  161. assert not arr.flags.writeable
  162. with assert_raises(ValueError):
  163. view.flags.writeable = True
  164. with warnings.catch_warnings():
  165. warnings.simplefilter("error", DeprecationWarning)
  166. with assert_raises(DeprecationWarning):
  167. arr.flags.writeable = True
  168. with assert_warns(DeprecationWarning):
  169. arr.flags.writeable = True
  170. def test_warnonwrite(self):
  171. a = np.arange(10)
  172. a.flags._warn_on_write = True
  173. with warnings.catch_warnings(record=True) as w:
  174. warnings.filterwarnings('always')
  175. a[1] = 10
  176. a[2] = 10
  177. # only warn once
  178. assert_(len(w) == 1)
  179. @pytest.mark.parametrize(["flag", "flag_value", "writeable"],
  180. [("writeable", True, True),
  181. # Delete _warn_on_write after deprecation and simplify
  182. # the parameterization:
  183. ("_warn_on_write", True, False),
  184. ("writeable", False, False)])
  185. def test_readonly_flag_protocols(self, flag, flag_value, writeable):
  186. a = np.arange(10)
  187. setattr(a.flags, flag, flag_value)
  188. class MyArr():
  189. __array_struct__ = a.__array_struct__
  190. assert memoryview(a).readonly is not writeable
  191. assert a.__array_interface__['data'][1] is not writeable
  192. assert np.asarray(MyArr()).flags.writeable is writeable
  193. def test_otherflags(self):
  194. assert_equal(self.a.flags.carray, True)
  195. assert_equal(self.a.flags['C'], True)
  196. assert_equal(self.a.flags.farray, False)
  197. assert_equal(self.a.flags.behaved, True)
  198. assert_equal(self.a.flags.fnc, False)
  199. assert_equal(self.a.flags.forc, True)
  200. assert_equal(self.a.flags.owndata, True)
  201. assert_equal(self.a.flags.writeable, True)
  202. assert_equal(self.a.flags.aligned, True)
  203. with assert_warns(DeprecationWarning):
  204. assert_equal(self.a.flags.updateifcopy, False)
  205. with assert_warns(DeprecationWarning):
  206. assert_equal(self.a.flags['U'], False)
  207. assert_equal(self.a.flags['UPDATEIFCOPY'], False)
  208. assert_equal(self.a.flags.writebackifcopy, False)
  209. assert_equal(self.a.flags['X'], False)
  210. assert_equal(self.a.flags['WRITEBACKIFCOPY'], False)
  211. def test_string_align(self):
  212. a = np.zeros(4, dtype=np.dtype('|S4'))
  213. assert_(a.flags.aligned)
  214. # not power of two are accessed byte-wise and thus considered aligned
  215. a = np.zeros(5, dtype=np.dtype('|S4'))
  216. assert_(a.flags.aligned)
  217. def test_void_align(self):
  218. a = np.zeros(4, dtype=np.dtype([("a", "i4"), ("b", "i4")]))
  219. assert_(a.flags.aligned)
  220. class TestHash:
  221. # see #3793
  222. def test_int(self):
  223. for st, ut, s in [(np.int8, np.uint8, 8),
  224. (np.int16, np.uint16, 16),
  225. (np.int32, np.uint32, 32),
  226. (np.int64, np.uint64, 64)]:
  227. for i in range(1, s):
  228. assert_equal(hash(st(-2**i)), hash(-2**i),
  229. err_msg="%r: -2**%d" % (st, i))
  230. assert_equal(hash(st(2**(i - 1))), hash(2**(i - 1)),
  231. err_msg="%r: 2**%d" % (st, i - 1))
  232. assert_equal(hash(st(2**i - 1)), hash(2**i - 1),
  233. err_msg="%r: 2**%d - 1" % (st, i))
  234. i = max(i - 1, 1)
  235. assert_equal(hash(ut(2**(i - 1))), hash(2**(i - 1)),
  236. err_msg="%r: 2**%d" % (ut, i - 1))
  237. assert_equal(hash(ut(2**i - 1)), hash(2**i - 1),
  238. err_msg="%r: 2**%d - 1" % (ut, i))
  239. class TestAttributes:
  240. def setup(self):
  241. self.one = np.arange(10)
  242. self.two = np.arange(20).reshape(4, 5)
  243. self.three = np.arange(60, dtype=np.float64).reshape(2, 5, 6)
  244. def test_attributes(self):
  245. assert_equal(self.one.shape, (10,))
  246. assert_equal(self.two.shape, (4, 5))
  247. assert_equal(self.three.shape, (2, 5, 6))
  248. self.three.shape = (10, 3, 2)
  249. assert_equal(self.three.shape, (10, 3, 2))
  250. self.three.shape = (2, 5, 6)
  251. assert_equal(self.one.strides, (self.one.itemsize,))
  252. num = self.two.itemsize
  253. assert_equal(self.two.strides, (5*num, num))
  254. num = self.three.itemsize
  255. assert_equal(self.three.strides, (30*num, 6*num, num))
  256. assert_equal(self.one.ndim, 1)
  257. assert_equal(self.two.ndim, 2)
  258. assert_equal(self.three.ndim, 3)
  259. num = self.two.itemsize
  260. assert_equal(self.two.size, 20)
  261. assert_equal(self.two.nbytes, 20*num)
  262. assert_equal(self.two.itemsize, self.two.dtype.itemsize)
  263. assert_equal(self.two.base, np.arange(20))
  264. def test_dtypeattr(self):
  265. assert_equal(self.one.dtype, np.dtype(np.int_))
  266. assert_equal(self.three.dtype, np.dtype(np.float_))
  267. assert_equal(self.one.dtype.char, 'l')
  268. assert_equal(self.three.dtype.char, 'd')
  269. assert_(self.three.dtype.str[0] in '<>')
  270. assert_equal(self.one.dtype.str[1], 'i')
  271. assert_equal(self.three.dtype.str[1], 'f')
  272. def test_int_subclassing(self):
  273. # Regression test for https://github.com/numpy/numpy/pull/3526
  274. numpy_int = np.int_(0)
  275. # int_ doesn't inherit from Python int, because it's not fixed-width
  276. assert_(not isinstance(numpy_int, int))
  277. def test_stridesattr(self):
  278. x = self.one
  279. def make_array(size, offset, strides):
  280. return np.ndarray(size, buffer=x, dtype=int,
  281. offset=offset*x.itemsize,
  282. strides=strides*x.itemsize)
  283. assert_equal(make_array(4, 4, -1), np.array([4, 3, 2, 1]))
  284. assert_raises(ValueError, make_array, 4, 4, -2)
  285. assert_raises(ValueError, make_array, 4, 2, -1)
  286. assert_raises(ValueError, make_array, 8, 3, 1)
  287. assert_equal(make_array(8, 3, 0), np.array([3]*8))
  288. # Check behavior reported in gh-2503:
  289. assert_raises(ValueError, make_array, (2, 3), 5, np.array([-2, -3]))
  290. make_array(0, 0, 10)
  291. def test_set_stridesattr(self):
  292. x = self.one
  293. def make_array(size, offset, strides):
  294. try:
  295. r = np.ndarray([size], dtype=int, buffer=x,
  296. offset=offset*x.itemsize)
  297. except Exception as e:
  298. raise RuntimeError(e)
  299. r.strides = strides = strides*x.itemsize
  300. return r
  301. assert_equal(make_array(4, 4, -1), np.array([4, 3, 2, 1]))
  302. assert_equal(make_array(7, 3, 1), np.array([3, 4, 5, 6, 7, 8, 9]))
  303. assert_raises(ValueError, make_array, 4, 4, -2)
  304. assert_raises(ValueError, make_array, 4, 2, -1)
  305. assert_raises(RuntimeError, make_array, 8, 3, 1)
  306. # Check that the true extent of the array is used.
  307. # Test relies on as_strided base not exposing a buffer.
  308. x = np.lib.stride_tricks.as_strided(np.arange(1), (10, 10), (0, 0))
  309. def set_strides(arr, strides):
  310. arr.strides = strides
  311. assert_raises(ValueError, set_strides, x, (10*x.itemsize, x.itemsize))
  312. # Test for offset calculations:
  313. x = np.lib.stride_tricks.as_strided(np.arange(10, dtype=np.int8)[-1],
  314. shape=(10,), strides=(-1,))
  315. assert_raises(ValueError, set_strides, x[::-1], -1)
  316. a = x[::-1]
  317. a.strides = 1
  318. a[::2].strides = 2
  319. # test 0d
  320. arr_0d = np.array(0)
  321. arr_0d.strides = ()
  322. assert_raises(TypeError, set_strides, arr_0d, None)
  323. def test_fill(self):
  324. for t in "?bhilqpBHILQPfdgFDGO":
  325. x = np.empty((3, 2, 1), t)
  326. y = np.empty((3, 2, 1), t)
  327. x.fill(1)
  328. y[...] = 1
  329. assert_equal(x, y)
  330. def test_fill_max_uint64(self):
  331. x = np.empty((3, 2, 1), dtype=np.uint64)
  332. y = np.empty((3, 2, 1), dtype=np.uint64)
  333. value = 2**64 - 1
  334. y[...] = value
  335. x.fill(value)
  336. assert_array_equal(x, y)
  337. def test_fill_struct_array(self):
  338. # Filling from a scalar
  339. x = np.array([(0, 0.0), (1, 1.0)], dtype='i4,f8')
  340. x.fill(x[0])
  341. assert_equal(x['f1'][1], x['f1'][0])
  342. # Filling from a tuple that can be converted
  343. # to a scalar
  344. x = np.zeros(2, dtype=[('a', 'f8'), ('b', 'i4')])
  345. x.fill((3.5, -2))
  346. assert_array_equal(x['a'], [3.5, 3.5])
  347. assert_array_equal(x['b'], [-2, -2])
  348. class TestArrayConstruction:
  349. def test_array(self):
  350. d = np.ones(6)
  351. r = np.array([d, d])
  352. assert_equal(r, np.ones((2, 6)))
  353. d = np.ones(6)
  354. tgt = np.ones((2, 6))
  355. r = np.array([d, d])
  356. assert_equal(r, tgt)
  357. tgt[1] = 2
  358. r = np.array([d, d + 1])
  359. assert_equal(r, tgt)
  360. d = np.ones(6)
  361. r = np.array([[d, d]])
  362. assert_equal(r, np.ones((1, 2, 6)))
  363. d = np.ones(6)
  364. r = np.array([[d, d], [d, d]])
  365. assert_equal(r, np.ones((2, 2, 6)))
  366. d = np.ones((6, 6))
  367. r = np.array([d, d])
  368. assert_equal(r, np.ones((2, 6, 6)))
  369. d = np.ones((6, ))
  370. r = np.array([[d, d + 1], d + 2], dtype=object)
  371. assert_equal(len(r), 2)
  372. assert_equal(r[0], [d, d + 1])
  373. assert_equal(r[1], d + 2)
  374. tgt = np.ones((2, 3), dtype=bool)
  375. tgt[0, 2] = False
  376. tgt[1, 0:2] = False
  377. r = np.array([[True, True, False], [False, False, True]])
  378. assert_equal(r, tgt)
  379. r = np.array([[True, False], [True, False], [False, True]])
  380. assert_equal(r, tgt.T)
  381. def test_array_empty(self):
  382. assert_raises(TypeError, np.array)
  383. def test_array_copy_false(self):
  384. d = np.array([1, 2, 3])
  385. e = np.array(d, copy=False)
  386. d[1] = 3
  387. assert_array_equal(e, [1, 3, 3])
  388. e = np.array(d, copy=False, order='F')
  389. d[1] = 4
  390. assert_array_equal(e, [1, 4, 3])
  391. e[2] = 7
  392. assert_array_equal(d, [1, 4, 7])
  393. def test_array_copy_true(self):
  394. d = np.array([[1,2,3], [1, 2, 3]])
  395. e = np.array(d, copy=True)
  396. d[0, 1] = 3
  397. e[0, 2] = -7
  398. assert_array_equal(e, [[1, 2, -7], [1, 2, 3]])
  399. assert_array_equal(d, [[1, 3, 3], [1, 2, 3]])
  400. e = np.array(d, copy=True, order='F')
  401. d[0, 1] = 5
  402. e[0, 2] = 7
  403. assert_array_equal(e, [[1, 3, 7], [1, 2, 3]])
  404. assert_array_equal(d, [[1, 5, 3], [1,2,3]])
  405. def test_array_cont(self):
  406. d = np.ones(10)[::2]
  407. assert_(np.ascontiguousarray(d).flags.c_contiguous)
  408. assert_(np.ascontiguousarray(d).flags.f_contiguous)
  409. assert_(np.asfortranarray(d).flags.c_contiguous)
  410. assert_(np.asfortranarray(d).flags.f_contiguous)
  411. d = np.ones((10, 10))[::2,::2]
  412. assert_(np.ascontiguousarray(d).flags.c_contiguous)
  413. assert_(np.asfortranarray(d).flags.f_contiguous)
  414. class TestAssignment:
  415. def test_assignment_broadcasting(self):
  416. a = np.arange(6).reshape(2, 3)
  417. # Broadcasting the input to the output
  418. a[...] = np.arange(3)
  419. assert_equal(a, [[0, 1, 2], [0, 1, 2]])
  420. a[...] = np.arange(2).reshape(2, 1)
  421. assert_equal(a, [[0, 0, 0], [1, 1, 1]])
  422. # For compatibility with <= 1.5, a limited version of broadcasting
  423. # the output to the input.
  424. #
  425. # This behavior is inconsistent with NumPy broadcasting
  426. # in general, because it only uses one of the two broadcasting
  427. # rules (adding a new "1" dimension to the left of the shape),
  428. # applied to the output instead of an input. In NumPy 2.0, this kind
  429. # of broadcasting assignment will likely be disallowed.
  430. a[...] = np.arange(6)[::-1].reshape(1, 2, 3)
  431. assert_equal(a, [[5, 4, 3], [2, 1, 0]])
  432. # The other type of broadcasting would require a reduction operation.
  433. def assign(a, b):
  434. a[...] = b
  435. assert_raises(ValueError, assign, a, np.arange(12).reshape(2, 2, 3))
  436. def test_assignment_errors(self):
  437. # Address issue #2276
  438. class C:
  439. pass
  440. a = np.zeros(1)
  441. def assign(v):
  442. a[0] = v
  443. assert_raises((AttributeError, TypeError), assign, C())
  444. assert_raises(ValueError, assign, [1])
  445. def test_unicode_assignment(self):
  446. # gh-5049
  447. from numpy.core.numeric import set_string_function
  448. @contextmanager
  449. def inject_str(s):
  450. """ replace ndarray.__str__ temporarily """
  451. set_string_function(lambda x: s, repr=False)
  452. try:
  453. yield
  454. finally:
  455. set_string_function(None, repr=False)
  456. a1d = np.array([u'test'])
  457. a0d = np.array(u'done')
  458. with inject_str(u'bad'):
  459. a1d[0] = a0d # previously this would invoke __str__
  460. assert_equal(a1d[0], u'done')
  461. # this would crash for the same reason
  462. np.array([np.array(u'\xe5\xe4\xf6')])
  463. def test_stringlike_empty_list(self):
  464. # gh-8902
  465. u = np.array([u'done'])
  466. b = np.array([b'done'])
  467. class bad_sequence:
  468. def __getitem__(self): pass
  469. def __len__(self): raise RuntimeError
  470. assert_raises(ValueError, operator.setitem, u, 0, [])
  471. assert_raises(ValueError, operator.setitem, b, 0, [])
  472. assert_raises(ValueError, operator.setitem, u, 0, bad_sequence())
  473. assert_raises(ValueError, operator.setitem, b, 0, bad_sequence())
  474. def test_longdouble_assignment(self):
  475. # only relevant if longdouble is larger than float
  476. # we're looking for loss of precision
  477. for dtype in (np.longdouble, np.longcomplex):
  478. # gh-8902
  479. tinyb = np.nextafter(np.longdouble(0), 1).astype(dtype)
  480. tinya = np.nextafter(np.longdouble(0), -1).astype(dtype)
  481. # construction
  482. tiny1d = np.array([tinya])
  483. assert_equal(tiny1d[0], tinya)
  484. # scalar = scalar
  485. tiny1d[0] = tinyb
  486. assert_equal(tiny1d[0], tinyb)
  487. # 0d = scalar
  488. tiny1d[0, ...] = tinya
  489. assert_equal(tiny1d[0], tinya)
  490. # 0d = 0d
  491. tiny1d[0, ...] = tinyb[...]
  492. assert_equal(tiny1d[0], tinyb)
  493. # scalar = 0d
  494. tiny1d[0] = tinyb[...]
  495. assert_equal(tiny1d[0], tinyb)
  496. arr = np.array([np.array(tinya)])
  497. assert_equal(arr[0], tinya)
  498. def test_cast_to_string(self):
  499. # cast to str should do "str(scalar)", not "str(scalar.item())"
  500. # Example: In python2, str(float) is truncated, so we want to avoid
  501. # str(np.float64(...).item()) as this would incorrectly truncate.
  502. a = np.zeros(1, dtype='S20')
  503. a[:] = np.array(['1.12345678901234567890'], dtype='f8')
  504. assert_equal(a[0], b"1.1234567890123457")
  505. class TestDtypedescr:
  506. def test_construction(self):
  507. d1 = np.dtype('i4')
  508. assert_equal(d1, np.dtype(np.int32))
  509. d2 = np.dtype('f8')
  510. assert_equal(d2, np.dtype(np.float64))
  511. def test_byteorders(self):
  512. assert_(np.dtype('<i4') != np.dtype('>i4'))
  513. assert_(np.dtype([('a', '<i4')]) != np.dtype([('a', '>i4')]))
  514. def test_structured_non_void(self):
  515. fields = [('a', '<i2'), ('b', '<i2')]
  516. dt_int = np.dtype(('i4', fields))
  517. assert_equal(str(dt_int), "(numpy.int32, [('a', '<i2'), ('b', '<i2')])")
  518. # gh-9821
  519. arr_int = np.zeros(4, dt_int)
  520. assert_equal(repr(arr_int),
  521. "array([0, 0, 0, 0], dtype=(numpy.int32, [('a', '<i2'), ('b', '<i2')]))")
  522. class TestZeroRank:
  523. def setup(self):
  524. self.d = np.array(0), np.array('x', object)
  525. def test_ellipsis_subscript(self):
  526. a, b = self.d
  527. assert_equal(a[...], 0)
  528. assert_equal(b[...], 'x')
  529. assert_(a[...].base is a) # `a[...] is a` in numpy <1.9.
  530. assert_(b[...].base is b) # `b[...] is b` in numpy <1.9.
  531. def test_empty_subscript(self):
  532. a, b = self.d
  533. assert_equal(a[()], 0)
  534. assert_equal(b[()], 'x')
  535. assert_(type(a[()]) is a.dtype.type)
  536. assert_(type(b[()]) is str)
  537. def test_invalid_subscript(self):
  538. a, b = self.d
  539. assert_raises(IndexError, lambda x: x[0], a)
  540. assert_raises(IndexError, lambda x: x[0], b)
  541. assert_raises(IndexError, lambda x: x[np.array([], int)], a)
  542. assert_raises(IndexError, lambda x: x[np.array([], int)], b)
  543. def test_ellipsis_subscript_assignment(self):
  544. a, b = self.d
  545. a[...] = 42
  546. assert_equal(a, 42)
  547. b[...] = ''
  548. assert_equal(b.item(), '')
  549. def test_empty_subscript_assignment(self):
  550. a, b = self.d
  551. a[()] = 42
  552. assert_equal(a, 42)
  553. b[()] = ''
  554. assert_equal(b.item(), '')
  555. def test_invalid_subscript_assignment(self):
  556. a, b = self.d
  557. def assign(x, i, v):
  558. x[i] = v
  559. assert_raises(IndexError, assign, a, 0, 42)
  560. assert_raises(IndexError, assign, b, 0, '')
  561. assert_raises(ValueError, assign, a, (), '')
  562. def test_newaxis(self):
  563. a, b = self.d
  564. assert_equal(a[np.newaxis].shape, (1,))
  565. assert_equal(a[..., np.newaxis].shape, (1,))
  566. assert_equal(a[np.newaxis, ...].shape, (1,))
  567. assert_equal(a[..., np.newaxis].shape, (1,))
  568. assert_equal(a[np.newaxis, ..., np.newaxis].shape, (1, 1))
  569. assert_equal(a[..., np.newaxis, np.newaxis].shape, (1, 1))
  570. assert_equal(a[np.newaxis, np.newaxis, ...].shape, (1, 1))
  571. assert_equal(a[(np.newaxis,)*10].shape, (1,)*10)
  572. def test_invalid_newaxis(self):
  573. a, b = self.d
  574. def subscript(x, i):
  575. x[i]
  576. assert_raises(IndexError, subscript, a, (np.newaxis, 0))
  577. assert_raises(IndexError, subscript, a, (np.newaxis,)*50)
  578. def test_constructor(self):
  579. x = np.ndarray(())
  580. x[()] = 5
  581. assert_equal(x[()], 5)
  582. y = np.ndarray((), buffer=x)
  583. y[()] = 6
  584. assert_equal(x[()], 6)
  585. # strides and shape must be the same length
  586. with pytest.raises(ValueError):
  587. np.ndarray((2,), strides=())
  588. with pytest.raises(ValueError):
  589. np.ndarray((), strides=(2,))
  590. def test_output(self):
  591. x = np.array(2)
  592. assert_raises(ValueError, np.add, x, [1], x)
  593. def test_real_imag(self):
  594. # contiguity checks are for gh-11245
  595. x = np.array(1j)
  596. xr = x.real
  597. xi = x.imag
  598. assert_equal(xr, np.array(0))
  599. assert_(type(xr) is np.ndarray)
  600. assert_equal(xr.flags.contiguous, True)
  601. assert_equal(xr.flags.f_contiguous, True)
  602. assert_equal(xi, np.array(1))
  603. assert_(type(xi) is np.ndarray)
  604. assert_equal(xi.flags.contiguous, True)
  605. assert_equal(xi.flags.f_contiguous, True)
  606. class TestScalarIndexing:
  607. def setup(self):
  608. self.d = np.array([0, 1])[0]
  609. def test_ellipsis_subscript(self):
  610. a = self.d
  611. assert_equal(a[...], 0)
  612. assert_equal(a[...].shape, ())
  613. def test_empty_subscript(self):
  614. a = self.d
  615. assert_equal(a[()], 0)
  616. assert_equal(a[()].shape, ())
  617. def test_invalid_subscript(self):
  618. a = self.d
  619. assert_raises(IndexError, lambda x: x[0], a)
  620. assert_raises(IndexError, lambda x: x[np.array([], int)], a)
  621. def test_invalid_subscript_assignment(self):
  622. a = self.d
  623. def assign(x, i, v):
  624. x[i] = v
  625. assert_raises(TypeError, assign, a, 0, 42)
  626. def test_newaxis(self):
  627. a = self.d
  628. assert_equal(a[np.newaxis].shape, (1,))
  629. assert_equal(a[..., np.newaxis].shape, (1,))
  630. assert_equal(a[np.newaxis, ...].shape, (1,))
  631. assert_equal(a[..., np.newaxis].shape, (1,))
  632. assert_equal(a[np.newaxis, ..., np.newaxis].shape, (1, 1))
  633. assert_equal(a[..., np.newaxis, np.newaxis].shape, (1, 1))
  634. assert_equal(a[np.newaxis, np.newaxis, ...].shape, (1, 1))
  635. assert_equal(a[(np.newaxis,)*10].shape, (1,)*10)
  636. def test_invalid_newaxis(self):
  637. a = self.d
  638. def subscript(x, i):
  639. x[i]
  640. assert_raises(IndexError, subscript, a, (np.newaxis, 0))
  641. assert_raises(IndexError, subscript, a, (np.newaxis,)*50)
  642. def test_overlapping_assignment(self):
  643. # With positive strides
  644. a = np.arange(4)
  645. a[:-1] = a[1:]
  646. assert_equal(a, [1, 2, 3, 3])
  647. a = np.arange(4)
  648. a[1:] = a[:-1]
  649. assert_equal(a, [0, 0, 1, 2])
  650. # With positive and negative strides
  651. a = np.arange(4)
  652. a[:] = a[::-1]
  653. assert_equal(a, [3, 2, 1, 0])
  654. a = np.arange(6).reshape(2, 3)
  655. a[::-1,:] = a[:, ::-1]
  656. assert_equal(a, [[5, 4, 3], [2, 1, 0]])
  657. a = np.arange(6).reshape(2, 3)
  658. a[::-1, ::-1] = a[:, ::-1]
  659. assert_equal(a, [[3, 4, 5], [0, 1, 2]])
  660. # With just one element overlapping
  661. a = np.arange(5)
  662. a[:3] = a[2:]
  663. assert_equal(a, [2, 3, 4, 3, 4])
  664. a = np.arange(5)
  665. a[2:] = a[:3]
  666. assert_equal(a, [0, 1, 0, 1, 2])
  667. a = np.arange(5)
  668. a[2::-1] = a[2:]
  669. assert_equal(a, [4, 3, 2, 3, 4])
  670. a = np.arange(5)
  671. a[2:] = a[2::-1]
  672. assert_equal(a, [0, 1, 2, 1, 0])
  673. a = np.arange(5)
  674. a[2::-1] = a[:1:-1]
  675. assert_equal(a, [2, 3, 4, 3, 4])
  676. a = np.arange(5)
  677. a[:1:-1] = a[2::-1]
  678. assert_equal(a, [0, 1, 0, 1, 2])
  679. class TestCreation:
  680. """
  681. Test the np.array constructor
  682. """
  683. def test_from_attribute(self):
  684. class x:
  685. def __array__(self, dtype=None):
  686. pass
  687. assert_raises(ValueError, np.array, x())
  688. def test_from_string(self):
  689. types = np.typecodes['AllInteger'] + np.typecodes['Float']
  690. nstr = ['123', '123']
  691. result = np.array([123, 123], dtype=int)
  692. for type in types:
  693. msg = 'String conversion for %s' % type
  694. assert_equal(np.array(nstr, dtype=type), result, err_msg=msg)
  695. def test_void(self):
  696. arr = np.array([], dtype='V')
  697. assert arr.dtype == 'V8' # current default
  698. # Same length scalars (those that go to the same void) work:
  699. arr = np.array([b"1234", b"1234"], dtype="V")
  700. assert arr.dtype == "V4"
  701. # Promoting different lengths will fail (pre 1.20 this worked)
  702. # by going via S5 and casting to V5.
  703. with pytest.raises(TypeError):
  704. np.array([b"1234", b"12345"], dtype="V")
  705. with pytest.raises(TypeError):
  706. np.array([b"12345", b"1234"], dtype="V")
  707. # Check the same for the casting path:
  708. arr = np.array([b"1234", b"1234"], dtype="O").astype("V")
  709. assert arr.dtype == "V4"
  710. with pytest.raises(TypeError):
  711. np.array([b"1234", b"12345"], dtype="O").astype("V")
  712. @pytest.mark.parametrize("idx",
  713. [pytest.param(Ellipsis, id="arr"), pytest.param((), id="scalar")])
  714. def test_structured_void_promotion(self, idx):
  715. arr = np.array(
  716. [np.array(1, dtype="i,i")[idx], np.array(2, dtype='i,i')[idx]],
  717. dtype="V")
  718. assert_array_equal(arr, np.array([(1, 1), (2, 2)], dtype="i,i"))
  719. # The following fails to promote the two dtypes, resulting in an error
  720. with pytest.raises(TypeError):
  721. np.array(
  722. [np.array(1, dtype="i,i")[idx], np.array(2, dtype='i,i,i')[idx]],
  723. dtype="V")
  724. def test_too_big_error(self):
  725. # 45341 is the smallest integer greater than sqrt(2**31 - 1).
  726. # 3037000500 is the smallest integer greater than sqrt(2**63 - 1).
  727. # We want to make sure that the square byte array with those dimensions
  728. # is too big on 32 or 64 bit systems respectively.
  729. if np.iinfo('intp').max == 2**31 - 1:
  730. shape = (46341, 46341)
  731. elif np.iinfo('intp').max == 2**63 - 1:
  732. shape = (3037000500, 3037000500)
  733. else:
  734. return
  735. assert_raises(ValueError, np.empty, shape, dtype=np.int8)
  736. assert_raises(ValueError, np.zeros, shape, dtype=np.int8)
  737. assert_raises(ValueError, np.ones, shape, dtype=np.int8)
  738. @pytest.mark.skipif(np.dtype(np.intp).itemsize != 8,
  739. reason="malloc may not fail on 32 bit systems")
  740. def test_malloc_fails(self):
  741. # This test is guaranteed to fail due to a too large allocation
  742. with assert_raises(np.core._exceptions._ArrayMemoryError):
  743. np.empty(np.iinfo(np.intp).max, dtype=np.uint8)
  744. def test_zeros(self):
  745. types = np.typecodes['AllInteger'] + np.typecodes['AllFloat']
  746. for dt in types:
  747. d = np.zeros((13,), dtype=dt)
  748. assert_equal(np.count_nonzero(d), 0)
  749. # true for ieee floats
  750. assert_equal(d.sum(), 0)
  751. assert_(not d.any())
  752. d = np.zeros(2, dtype='(2,4)i4')
  753. assert_equal(np.count_nonzero(d), 0)
  754. assert_equal(d.sum(), 0)
  755. assert_(not d.any())
  756. d = np.zeros(2, dtype='4i4')
  757. assert_equal(np.count_nonzero(d), 0)
  758. assert_equal(d.sum(), 0)
  759. assert_(not d.any())
  760. d = np.zeros(2, dtype='(2,4)i4, (2,4)i4')
  761. assert_equal(np.count_nonzero(d), 0)
  762. @pytest.mark.slow
  763. def test_zeros_big(self):
  764. # test big array as they might be allocated different by the system
  765. types = np.typecodes['AllInteger'] + np.typecodes['AllFloat']
  766. for dt in types:
  767. d = np.zeros((30 * 1024**2,), dtype=dt)
  768. assert_(not d.any())
  769. # This test can fail on 32-bit systems due to insufficient
  770. # contiguous memory. Deallocating the previous array increases the
  771. # chance of success.
  772. del(d)
  773. def test_zeros_obj(self):
  774. # test initialization from PyLong(0)
  775. d = np.zeros((13,), dtype=object)
  776. assert_array_equal(d, [0] * 13)
  777. assert_equal(np.count_nonzero(d), 0)
  778. def test_zeros_obj_obj(self):
  779. d = np.zeros(10, dtype=[('k', object, 2)])
  780. assert_array_equal(d['k'], 0)
  781. def test_zeros_like_like_zeros(self):
  782. # test zeros_like returns the same as zeros
  783. for c in np.typecodes['All']:
  784. if c == 'V':
  785. continue
  786. d = np.zeros((3,3), dtype=c)
  787. assert_array_equal(np.zeros_like(d), d)
  788. assert_equal(np.zeros_like(d).dtype, d.dtype)
  789. # explicitly check some special cases
  790. d = np.zeros((3,3), dtype='S5')
  791. assert_array_equal(np.zeros_like(d), d)
  792. assert_equal(np.zeros_like(d).dtype, d.dtype)
  793. d = np.zeros((3,3), dtype='U5')
  794. assert_array_equal(np.zeros_like(d), d)
  795. assert_equal(np.zeros_like(d).dtype, d.dtype)
  796. d = np.zeros((3,3), dtype='<i4')
  797. assert_array_equal(np.zeros_like(d), d)
  798. assert_equal(np.zeros_like(d).dtype, d.dtype)
  799. d = np.zeros((3,3), dtype='>i4')
  800. assert_array_equal(np.zeros_like(d), d)
  801. assert_equal(np.zeros_like(d).dtype, d.dtype)
  802. d = np.zeros((3,3), dtype='<M8[s]')
  803. assert_array_equal(np.zeros_like(d), d)
  804. assert_equal(np.zeros_like(d).dtype, d.dtype)
  805. d = np.zeros((3,3), dtype='>M8[s]')
  806. assert_array_equal(np.zeros_like(d), d)
  807. assert_equal(np.zeros_like(d).dtype, d.dtype)
  808. d = np.zeros((3,3), dtype='f4,f4')
  809. assert_array_equal(np.zeros_like(d), d)
  810. assert_equal(np.zeros_like(d).dtype, d.dtype)
  811. def test_empty_unicode(self):
  812. # don't throw decode errors on garbage memory
  813. for i in range(5, 100, 5):
  814. d = np.empty(i, dtype='U')
  815. str(d)
  816. def test_sequence_non_homogeneous(self):
  817. assert_equal(np.array([4, 2**80]).dtype, object)
  818. assert_equal(np.array([4, 2**80, 4]).dtype, object)
  819. assert_equal(np.array([2**80, 4]).dtype, object)
  820. assert_equal(np.array([2**80] * 3).dtype, object)
  821. assert_equal(np.array([[1, 1],[1j, 1j]]).dtype, complex)
  822. assert_equal(np.array([[1j, 1j],[1, 1]]).dtype, complex)
  823. assert_equal(np.array([[1, 1, 1],[1, 1j, 1.], [1, 1, 1]]).dtype, complex)
  824. def test_non_sequence_sequence(self):
  825. """Should not segfault.
  826. Class Fail breaks the sequence protocol for new style classes, i.e.,
  827. those derived from object. Class Map is a mapping type indicated by
  828. raising a ValueError. At some point we may raise a warning instead
  829. of an error in the Fail case.
  830. """
  831. class Fail:
  832. def __len__(self):
  833. return 1
  834. def __getitem__(self, index):
  835. raise ValueError()
  836. class Map:
  837. def __len__(self):
  838. return 1
  839. def __getitem__(self, index):
  840. raise KeyError()
  841. a = np.array([Map()])
  842. assert_(a.shape == (1,))
  843. assert_(a.dtype == np.dtype(object))
  844. assert_raises(ValueError, np.array, [Fail()])
  845. def test_no_len_object_type(self):
  846. # gh-5100, want object array from iterable object without len()
  847. class Point2:
  848. def __init__(self):
  849. pass
  850. def __getitem__(self, ind):
  851. if ind in [0, 1]:
  852. return ind
  853. else:
  854. raise IndexError()
  855. d = np.array([Point2(), Point2(), Point2()])
  856. assert_equal(d.dtype, np.dtype(object))
  857. def test_false_len_sequence(self):
  858. # gh-7264, segfault for this example
  859. class C:
  860. def __getitem__(self, i):
  861. raise IndexError
  862. def __len__(self):
  863. return 42
  864. a = np.array(C()) # segfault?
  865. assert_equal(len(a), 0)
  866. def test_false_len_iterable(self):
  867. # Special case where a bad __getitem__ makes us fall back on __iter__:
  868. class C:
  869. def __getitem__(self, x):
  870. raise Exception
  871. def __iter__(self):
  872. return iter(())
  873. def __len__(self):
  874. return 2
  875. a = np.empty(2)
  876. with assert_raises(ValueError):
  877. a[:] = C() # Segfault!
  878. np.array(C()) == list(C())
  879. def test_failed_len_sequence(self):
  880. # gh-7393
  881. class A:
  882. def __init__(self, data):
  883. self._data = data
  884. def __getitem__(self, item):
  885. return type(self)(self._data[item])
  886. def __len__(self):
  887. return len(self._data)
  888. # len(d) should give 3, but len(d[0]) will fail
  889. d = A([1,2,3])
  890. assert_equal(len(np.array(d)), 3)
  891. def test_array_too_big(self):
  892. # Test that array creation succeeds for arrays addressable by intp
  893. # on the byte level and fails for too large arrays.
  894. buf = np.zeros(100)
  895. max_bytes = np.iinfo(np.intp).max
  896. for dtype in ["intp", "S20", "b"]:
  897. dtype = np.dtype(dtype)
  898. itemsize = dtype.itemsize
  899. np.ndarray(buffer=buf, strides=(0,),
  900. shape=(max_bytes//itemsize,), dtype=dtype)
  901. assert_raises(ValueError, np.ndarray, buffer=buf, strides=(0,),
  902. shape=(max_bytes//itemsize + 1,), dtype=dtype)
  903. def _ragged_creation(self, seq):
  904. # without dtype=object, the ragged object should raise
  905. with assert_warns(np.VisibleDeprecationWarning):
  906. a = np.array(seq)
  907. b = np.array(seq, dtype=object)
  908. assert_equal(a, b)
  909. return b
  910. def test_ragged_ndim_object(self):
  911. # Lists of mismatching depths are treated as object arrays
  912. a = self._ragged_creation([[1], 2, 3])
  913. assert_equal(a.shape, (3,))
  914. assert_equal(a.dtype, object)
  915. a = self._ragged_creation([1, [2], 3])
  916. assert_equal(a.shape, (3,))
  917. assert_equal(a.dtype, object)
  918. a = self._ragged_creation([1, 2, [3]])
  919. assert_equal(a.shape, (3,))
  920. assert_equal(a.dtype, object)
  921. def test_ragged_shape_object(self):
  922. # The ragged dimension of a list is turned into an object array
  923. a = self._ragged_creation([[1, 1], [2], [3]])
  924. assert_equal(a.shape, (3,))
  925. assert_equal(a.dtype, object)
  926. a = self._ragged_creation([[1], [2, 2], [3]])
  927. assert_equal(a.shape, (3,))
  928. assert_equal(a.dtype, object)
  929. a = self._ragged_creation([[1], [2], [3, 3]])
  930. assert a.shape == (3,)
  931. assert a.dtype == object
  932. def test_array_of_ragged_array(self):
  933. outer = np.array([None, None])
  934. outer[0] = outer[1] = np.array([1, 2, 3])
  935. assert np.array(outer).shape == (2,)
  936. assert np.array([outer]).shape == (1, 2)
  937. outer_ragged = np.array([None, None])
  938. outer_ragged[0] = np.array([1, 2, 3])
  939. outer_ragged[1] = np.array([1, 2, 3, 4])
  940. # should both of these emit deprecation warnings?
  941. assert np.array(outer_ragged).shape == (2,)
  942. assert np.array([outer_ragged]).shape == (1, 2,)
  943. def test_deep_nonragged_object(self):
  944. # None of these should raise, even though they are missing dtype=object
  945. a = np.array([[[Decimal(1)]]])
  946. a = np.array([1, Decimal(1)])
  947. a = np.array([[1], [Decimal(1)]])
  948. class TestStructured:
  949. def test_subarray_field_access(self):
  950. a = np.zeros((3, 5), dtype=[('a', ('i4', (2, 2)))])
  951. a['a'] = np.arange(60).reshape(3, 5, 2, 2)
  952. # Since the subarray is always in C-order, a transpose
  953. # does not swap the subarray:
  954. assert_array_equal(a.T['a'], a['a'].transpose(1, 0, 2, 3))
  955. # In Fortran order, the subarray gets appended
  956. # like in all other cases, not prepended as a special case
  957. b = a.copy(order='F')
  958. assert_equal(a['a'].shape, b['a'].shape)
  959. assert_equal(a.T['a'].shape, a.T.copy()['a'].shape)
  960. def test_subarray_comparison(self):
  961. # Check that comparisons between record arrays with
  962. # multi-dimensional field types work properly
  963. a = np.rec.fromrecords(
  964. [([1, 2, 3], 'a', [[1, 2], [3, 4]]), ([3, 3, 3], 'b', [[0, 0], [0, 0]])],
  965. dtype=[('a', ('f4', 3)), ('b', object), ('c', ('i4', (2, 2)))])
  966. b = a.copy()
  967. assert_equal(a == b, [True, True])
  968. assert_equal(a != b, [False, False])
  969. b[1].b = 'c'
  970. assert_equal(a == b, [True, False])
  971. assert_equal(a != b, [False, True])
  972. for i in range(3):
  973. b[0].a = a[0].a
  974. b[0].a[i] = 5
  975. assert_equal(a == b, [False, False])
  976. assert_equal(a != b, [True, True])
  977. for i in range(2):
  978. for j in range(2):
  979. b = a.copy()
  980. b[0].c[i, j] = 10
  981. assert_equal(a == b, [False, True])
  982. assert_equal(a != b, [True, False])
  983. # Check that broadcasting with a subarray works
  984. a = np.array([[(0,)], [(1,)]], dtype=[('a', 'f8')])
  985. b = np.array([(0,), (0,), (1,)], dtype=[('a', 'f8')])
  986. assert_equal(a == b, [[True, True, False], [False, False, True]])
  987. assert_equal(b == a, [[True, True, False], [False, False, True]])
  988. a = np.array([[(0,)], [(1,)]], dtype=[('a', 'f8', (1,))])
  989. b = np.array([(0,), (0,), (1,)], dtype=[('a', 'f8', (1,))])
  990. assert_equal(a == b, [[True, True, False], [False, False, True]])
  991. assert_equal(b == a, [[True, True, False], [False, False, True]])
  992. a = np.array([[([0, 0],)], [([1, 1],)]], dtype=[('a', 'f8', (2,))])
  993. b = np.array([([0, 0],), ([0, 1],), ([1, 1],)], dtype=[('a', 'f8', (2,))])
  994. assert_equal(a == b, [[True, False, False], [False, False, True]])
  995. assert_equal(b == a, [[True, False, False], [False, False, True]])
  996. # Check that broadcasting Fortran-style arrays with a subarray work
  997. a = np.array([[([0, 0],)], [([1, 1],)]], dtype=[('a', 'f8', (2,))], order='F')
  998. b = np.array([([0, 0],), ([0, 1],), ([1, 1],)], dtype=[('a', 'f8', (2,))])
  999. assert_equal(a == b, [[True, False, False], [False, False, True]])
  1000. assert_equal(b == a, [[True, False, False], [False, False, True]])
  1001. # Check that incompatible sub-array shapes don't result to broadcasting
  1002. x = np.zeros((1,), dtype=[('a', ('f4', (1, 2))), ('b', 'i1')])
  1003. y = np.zeros((1,), dtype=[('a', ('f4', (2,))), ('b', 'i1')])
  1004. # This comparison invokes deprecated behaviour, and will probably
  1005. # start raising an error eventually. What we really care about in this
  1006. # test is just that it doesn't return True.
  1007. with suppress_warnings() as sup:
  1008. sup.filter(FutureWarning, "elementwise == comparison failed")
  1009. assert_equal(x == y, False)
  1010. x = np.zeros((1,), dtype=[('a', ('f4', (2, 1))), ('b', 'i1')])
  1011. y = np.zeros((1,), dtype=[('a', ('f4', (2,))), ('b', 'i1')])
  1012. # This comparison invokes deprecated behaviour, and will probably
  1013. # start raising an error eventually. What we really care about in this
  1014. # test is just that it doesn't return True.
  1015. with suppress_warnings() as sup:
  1016. sup.filter(FutureWarning, "elementwise == comparison failed")
  1017. assert_equal(x == y, False)
  1018. # Check that structured arrays that are different only in
  1019. # byte-order work
  1020. a = np.array([(5, 42), (10, 1)], dtype=[('a', '>i8'), ('b', '<f8')])
  1021. b = np.array([(5, 43), (10, 1)], dtype=[('a', '<i8'), ('b', '>f8')])
  1022. assert_equal(a == b, [False, True])
  1023. def test_casting(self):
  1024. # Check that casting a structured array to change its byte order
  1025. # works
  1026. a = np.array([(1,)], dtype=[('a', '<i4')])
  1027. assert_(np.can_cast(a.dtype, [('a', '>i4')], casting='unsafe'))
  1028. b = a.astype([('a', '>i4')])
  1029. assert_equal(b, a.byteswap().newbyteorder())
  1030. assert_equal(a['a'][0], b['a'][0])
  1031. # Check that equality comparison works on structured arrays if
  1032. # they are 'equiv'-castable
  1033. a = np.array([(5, 42), (10, 1)], dtype=[('a', '>i4'), ('b', '<f8')])
  1034. b = np.array([(5, 42), (10, 1)], dtype=[('a', '<i4'), ('b', '>f8')])
  1035. assert_(np.can_cast(a.dtype, b.dtype, casting='equiv'))
  1036. assert_equal(a == b, [True, True])
  1037. # Check that 'equiv' casting can change byte order
  1038. assert_(np.can_cast(a.dtype, b.dtype, casting='equiv'))
  1039. c = a.astype(b.dtype, casting='equiv')
  1040. assert_equal(a == c, [True, True])
  1041. # Check that 'safe' casting can change byte order and up-cast
  1042. # fields
  1043. t = [('a', '<i8'), ('b', '>f8')]
  1044. assert_(np.can_cast(a.dtype, t, casting='safe'))
  1045. c = a.astype(t, casting='safe')
  1046. assert_equal((c == np.array([(5, 42), (10, 1)], dtype=t)),
  1047. [True, True])
  1048. # Check that 'same_kind' casting can change byte order and
  1049. # change field widths within a "kind"
  1050. t = [('a', '<i4'), ('b', '>f4')]
  1051. assert_(np.can_cast(a.dtype, t, casting='same_kind'))
  1052. c = a.astype(t, casting='same_kind')
  1053. assert_equal((c == np.array([(5, 42), (10, 1)], dtype=t)),
  1054. [True, True])
  1055. # Check that casting fails if the casting rule should fail on
  1056. # any of the fields
  1057. t = [('a', '>i8'), ('b', '<f4')]
  1058. assert_(not np.can_cast(a.dtype, t, casting='safe'))
  1059. assert_raises(TypeError, a.astype, t, casting='safe')
  1060. t = [('a', '>i2'), ('b', '<f8')]
  1061. assert_(not np.can_cast(a.dtype, t, casting='equiv'))
  1062. assert_raises(TypeError, a.astype, t, casting='equiv')
  1063. t = [('a', '>i8'), ('b', '<i2')]
  1064. assert_(not np.can_cast(a.dtype, t, casting='same_kind'))
  1065. assert_raises(TypeError, a.astype, t, casting='same_kind')
  1066. assert_(not np.can_cast(a.dtype, b.dtype, casting='no'))
  1067. assert_raises(TypeError, a.astype, b.dtype, casting='no')
  1068. # Check that non-'unsafe' casting can't change the set of field names
  1069. for casting in ['no', 'safe', 'equiv', 'same_kind']:
  1070. t = [('a', '>i4')]
  1071. assert_(not np.can_cast(a.dtype, t, casting=casting))
  1072. t = [('a', '>i4'), ('b', '<f8'), ('c', 'i4')]
  1073. assert_(not np.can_cast(a.dtype, t, casting=casting))
  1074. def test_objview(self):
  1075. # https://github.com/numpy/numpy/issues/3286
  1076. a = np.array([], dtype=[('a', 'f'), ('b', 'f'), ('c', 'O')])
  1077. a[['a', 'b']] # TypeError?
  1078. # https://github.com/numpy/numpy/issues/3253
  1079. dat2 = np.zeros(3, [('A', 'i'), ('B', '|O')])
  1080. dat2[['B', 'A']] # TypeError?
  1081. def test_setfield(self):
  1082. # https://github.com/numpy/numpy/issues/3126
  1083. struct_dt = np.dtype([('elem', 'i4', 5),])
  1084. dt = np.dtype([('field', 'i4', 10),('struct', struct_dt)])
  1085. x = np.zeros(1, dt)
  1086. x[0]['field'] = np.ones(10, dtype='i4')
  1087. x[0]['struct'] = np.ones(1, dtype=struct_dt)
  1088. assert_equal(x[0]['field'], np.ones(10, dtype='i4'))
  1089. def test_setfield_object(self):
  1090. # make sure object field assignment with ndarray value
  1091. # on void scalar mimics setitem behavior
  1092. b = np.zeros(1, dtype=[('x', 'O')])
  1093. # next line should work identically to b['x'][0] = np.arange(3)
  1094. b[0]['x'] = np.arange(3)
  1095. assert_equal(b[0]['x'], np.arange(3))
  1096. # check that broadcasting check still works
  1097. c = np.zeros(1, dtype=[('x', 'O', 5)])
  1098. def testassign():
  1099. c[0]['x'] = np.arange(3)
  1100. assert_raises(ValueError, testassign)
  1101. def test_zero_width_string(self):
  1102. # Test for PR #6430 / issues #473, #4955, #2585
  1103. dt = np.dtype([('I', int), ('S', 'S0')])
  1104. x = np.zeros(4, dtype=dt)
  1105. assert_equal(x['S'], [b'', b'', b'', b''])
  1106. assert_equal(x['S'].itemsize, 0)
  1107. x['S'] = ['a', 'b', 'c', 'd']
  1108. assert_equal(x['S'], [b'', b'', b'', b''])
  1109. assert_equal(x['I'], [0, 0, 0, 0])
  1110. # Variation on test case from #4955
  1111. x['S'][x['I'] == 0] = 'hello'
  1112. assert_equal(x['S'], [b'', b'', b'', b''])
  1113. assert_equal(x['I'], [0, 0, 0, 0])
  1114. # Variation on test case from #2585
  1115. x['S'] = 'A'
  1116. assert_equal(x['S'], [b'', b'', b'', b''])
  1117. assert_equal(x['I'], [0, 0, 0, 0])
  1118. # Allow zero-width dtypes in ndarray constructor
  1119. y = np.ndarray(4, dtype=x['S'].dtype)
  1120. assert_equal(y.itemsize, 0)
  1121. assert_equal(x['S'], y)
  1122. # More tests for indexing an array with zero-width fields
  1123. assert_equal(np.zeros(4, dtype=[('a', 'S0,S0'),
  1124. ('b', 'u1')])['a'].itemsize, 0)
  1125. assert_equal(np.empty(3, dtype='S0,S0').itemsize, 0)
  1126. assert_equal(np.zeros(4, dtype='S0,u1')['f0'].itemsize, 0)
  1127. xx = x['S'].reshape((2, 2))
  1128. assert_equal(xx.itemsize, 0)
  1129. assert_equal(xx, [[b'', b''], [b'', b'']])
  1130. # check for no uninitialized memory due to viewing S0 array
  1131. assert_equal(xx[:].dtype, xx.dtype)
  1132. assert_array_equal(eval(repr(xx), dict(array=np.array)), xx)
  1133. b = io.BytesIO()
  1134. np.save(b, xx)
  1135. b.seek(0)
  1136. yy = np.load(b)
  1137. assert_equal(yy.itemsize, 0)
  1138. assert_equal(xx, yy)
  1139. with temppath(suffix='.npy') as tmp:
  1140. np.save(tmp, xx)
  1141. yy = np.load(tmp)
  1142. assert_equal(yy.itemsize, 0)
  1143. assert_equal(xx, yy)
  1144. def test_base_attr(self):
  1145. a = np.zeros(3, dtype='i4,f4')
  1146. b = a[0]
  1147. assert_(b.base is a)
  1148. def test_assignment(self):
  1149. def testassign(arr, v):
  1150. c = arr.copy()
  1151. c[0] = v # assign using setitem
  1152. c[1:] = v # assign using "dtype_transfer" code paths
  1153. return c
  1154. dt = np.dtype([('foo', 'i8'), ('bar', 'i8')])
  1155. arr = np.ones(2, dt)
  1156. v1 = np.array([(2,3)], dtype=[('foo', 'i8'), ('bar', 'i8')])
  1157. v2 = np.array([(2,3)], dtype=[('bar', 'i8'), ('foo', 'i8')])
  1158. v3 = np.array([(2,3)], dtype=[('bar', 'i8'), ('baz', 'i8')])
  1159. v4 = np.array([(2,)], dtype=[('bar', 'i8')])
  1160. v5 = np.array([(2,3)], dtype=[('foo', 'f8'), ('bar', 'f8')])
  1161. w = arr.view({'names': ['bar'], 'formats': ['i8'], 'offsets': [8]})
  1162. ans = np.array([(2,3),(2,3)], dtype=dt)
  1163. assert_equal(testassign(arr, v1), ans)
  1164. assert_equal(testassign(arr, v2), ans)
  1165. assert_equal(testassign(arr, v3), ans)
  1166. assert_raises(ValueError, lambda: testassign(arr, v4))
  1167. assert_equal(testassign(arr, v5), ans)
  1168. w[:] = 4
  1169. assert_equal(arr, np.array([(1,4),(1,4)], dtype=dt))
  1170. # test field-reordering, assignment by position, and self-assignment
  1171. a = np.array([(1,2,3)],
  1172. dtype=[('foo', 'i8'), ('bar', 'i8'), ('baz', 'f4')])
  1173. a[['foo', 'bar']] = a[['bar', 'foo']]
  1174. assert_equal(a[0].item(), (2,1,3))
  1175. # test that this works even for 'simple_unaligned' structs
  1176. # (ie, that PyArray_EquivTypes cares about field order too)
  1177. a = np.array([(1,2)], dtype=[('a', 'i4'), ('b', 'i4')])
  1178. a[['a', 'b']] = a[['b', 'a']]
  1179. assert_equal(a[0].item(), (2,1))
  1180. def test_scalar_assignment(self):
  1181. with assert_raises(ValueError):
  1182. arr = np.arange(25).reshape(5, 5)
  1183. arr.itemset(3)
  1184. def test_structuredscalar_indexing(self):
  1185. # test gh-7262
  1186. x = np.empty(shape=1, dtype="(2)3S,(2)3U")
  1187. assert_equal(x[["f0","f1"]][0], x[0][["f0","f1"]])
  1188. assert_equal(x[0], x[0][()])
  1189. def test_multiindex_titles(self):
  1190. a = np.zeros(4, dtype=[(('a', 'b'), 'i'), ('c', 'i'), ('d', 'i')])
  1191. assert_raises(KeyError, lambda : a[['a','c']])
  1192. assert_raises(KeyError, lambda : a[['a','a']])
  1193. assert_raises(ValueError, lambda : a[['b','b']]) # field exists, but repeated
  1194. a[['b','c']] # no exception
  1195. def test_structured_asarray_is_view(self):
  1196. # A scalar viewing an array preserves its view even when creating a
  1197. # new array. This test documents behaviour, it may not be the best
  1198. # desired behaviour.
  1199. arr = np.array([1], dtype="i,i")
  1200. scalar = arr[0]
  1201. assert not scalar.flags.owndata # view into the array
  1202. assert np.asarray(scalar).base is scalar
  1203. # But never when a dtype is passed in:
  1204. assert np.asarray(scalar, dtype=scalar.dtype).base is None
  1205. # A scalar which owns its data does not have this property.
  1206. # It is not easy to create one, one method is to use pickle:
  1207. scalar = pickle.loads(pickle.dumps(scalar))
  1208. assert scalar.flags.owndata
  1209. assert np.asarray(scalar).base is None
  1210. class TestBool:
  1211. def test_test_interning(self):
  1212. a0 = np.bool_(0)
  1213. b0 = np.bool_(False)
  1214. assert_(a0 is b0)
  1215. a1 = np.bool_(1)
  1216. b1 = np.bool_(True)
  1217. assert_(a1 is b1)
  1218. assert_(np.array([True])[0] is a1)
  1219. assert_(np.array(True)[()] is a1)
  1220. def test_sum(self):
  1221. d = np.ones(101, dtype=bool)
  1222. assert_equal(d.sum(), d.size)
  1223. assert_equal(d[::2].sum(), d[::2].size)
  1224. assert_equal(d[::-2].sum(), d[::-2].size)
  1225. d = np.frombuffer(b'\xff\xff' * 100, dtype=bool)
  1226. assert_equal(d.sum(), d.size)
  1227. assert_equal(d[::2].sum(), d[::2].size)
  1228. assert_equal(d[::-2].sum(), d[::-2].size)
  1229. def check_count_nonzero(self, power, length):
  1230. powers = [2 ** i for i in range(length)]
  1231. for i in range(2**power):
  1232. l = [(i & x) != 0 for x in powers]
  1233. a = np.array(l, dtype=bool)
  1234. c = builtins.sum(l)
  1235. assert_equal(np.count_nonzero(a), c)
  1236. av = a.view(np.uint8)
  1237. av *= 3
  1238. assert_equal(np.count_nonzero(a), c)
  1239. av *= 4
  1240. assert_equal(np.count_nonzero(a), c)
  1241. av[av != 0] = 0xFF
  1242. assert_equal(np.count_nonzero(a), c)
  1243. def test_count_nonzero(self):
  1244. # check all 12 bit combinations in a length 17 array
  1245. # covers most cases of the 16 byte unrolled code
  1246. self.check_count_nonzero(12, 17)
  1247. @pytest.mark.slow
  1248. def test_count_nonzero_all(self):
  1249. # check all combinations in a length 17 array
  1250. # covers all cases of the 16 byte unrolled code
  1251. self.check_count_nonzero(17, 17)
  1252. def test_count_nonzero_unaligned(self):
  1253. # prevent mistakes as e.g. gh-4060
  1254. for o in range(7):
  1255. a = np.zeros((18,), dtype=bool)[o+1:]
  1256. a[:o] = True
  1257. assert_equal(np.count_nonzero(a), builtins.sum(a.tolist()))
  1258. a = np.ones((18,), dtype=bool)[o+1:]
  1259. a[:o] = False
  1260. assert_equal(np.count_nonzero(a), builtins.sum(a.tolist()))
  1261. def _test_cast_from_flexible(self, dtype):
  1262. # empty string -> false
  1263. for n in range(3):
  1264. v = np.array(b'', (dtype, n))
  1265. assert_equal(bool(v), False)
  1266. assert_equal(bool(v[()]), False)
  1267. assert_equal(v.astype(bool), False)
  1268. assert_(isinstance(v.astype(bool), np.ndarray))
  1269. assert_(v[()].astype(bool) is np.False_)
  1270. # anything else -> true
  1271. for n in range(1, 4):
  1272. for val in [b'a', b'0', b' ']:
  1273. v = np.array(val, (dtype, n))
  1274. assert_equal(bool(v), True)
  1275. assert_equal(bool(v[()]), True)
  1276. assert_equal(v.astype(bool), True)
  1277. assert_(isinstance(v.astype(bool), np.ndarray))
  1278. assert_(v[()].astype(bool) is np.True_)
  1279. def test_cast_from_void(self):
  1280. self._test_cast_from_flexible(np.void)
  1281. @pytest.mark.xfail(reason="See gh-9847")
  1282. def test_cast_from_unicode(self):
  1283. self._test_cast_from_flexible(np.unicode_)
  1284. @pytest.mark.xfail(reason="See gh-9847")
  1285. def test_cast_from_bytes(self):
  1286. self._test_cast_from_flexible(np.bytes_)
  1287. class TestZeroSizeFlexible:
  1288. @staticmethod
  1289. def _zeros(shape, dtype=str):
  1290. dtype = np.dtype(dtype)
  1291. if dtype == np.void:
  1292. return np.zeros(shape, dtype=(dtype, 0))
  1293. # not constructable directly
  1294. dtype = np.dtype([('x', dtype, 0)])
  1295. return np.zeros(shape, dtype=dtype)['x']
  1296. def test_create(self):
  1297. zs = self._zeros(10, bytes)
  1298. assert_equal(zs.itemsize, 0)
  1299. zs = self._zeros(10, np.void)
  1300. assert_equal(zs.itemsize, 0)
  1301. zs = self._zeros(10, str)
  1302. assert_equal(zs.itemsize, 0)
  1303. def _test_sort_partition(self, name, kinds, **kwargs):
  1304. # Previously, these would all hang
  1305. for dt in [bytes, np.void, str]:
  1306. zs = self._zeros(10, dt)
  1307. sort_method = getattr(zs, name)
  1308. sort_func = getattr(np, name)
  1309. for kind in kinds:
  1310. sort_method(kind=kind, **kwargs)
  1311. sort_func(zs, kind=kind, **kwargs)
  1312. def test_sort(self):
  1313. self._test_sort_partition('sort', kinds='qhs')
  1314. def test_argsort(self):
  1315. self._test_sort_partition('argsort', kinds='qhs')
  1316. def test_partition(self):
  1317. self._test_sort_partition('partition', kinds=['introselect'], kth=2)
  1318. def test_argpartition(self):
  1319. self._test_sort_partition('argpartition', kinds=['introselect'], kth=2)
  1320. def test_resize(self):
  1321. # previously an error
  1322. for dt in [bytes, np.void, str]:
  1323. zs = self._zeros(10, dt)
  1324. zs.resize(25)
  1325. zs.resize((10, 10))
  1326. def test_view(self):
  1327. for dt in [bytes, np.void, str]:
  1328. zs = self._zeros(10, dt)
  1329. # viewing as itself should be allowed
  1330. assert_equal(zs.view(dt).dtype, np.dtype(dt))
  1331. # viewing as any non-empty type gives an empty result
  1332. assert_equal(zs.view((dt, 1)).shape, (0,))
  1333. def test_dumps(self):
  1334. zs = self._zeros(10, int)
  1335. assert_equal(zs, pickle.loads(zs.dumps()))
  1336. def test_pickle(self):
  1337. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  1338. for dt in [bytes, np.void, str]:
  1339. zs = self._zeros(10, dt)
  1340. p = pickle.dumps(zs, protocol=proto)
  1341. zs2 = pickle.loads(p)
  1342. assert_equal(zs.dtype, zs2.dtype)
  1343. @pytest.mark.skipif(pickle.HIGHEST_PROTOCOL < 5,
  1344. reason="requires pickle protocol 5")
  1345. def test_pickle_with_buffercallback(self):
  1346. array = np.arange(10)
  1347. buffers = []
  1348. bytes_string = pickle.dumps(array, buffer_callback=buffers.append,
  1349. protocol=5)
  1350. array_from_buffer = pickle.loads(bytes_string, buffers=buffers)
  1351. # when using pickle protocol 5 with buffer callbacks,
  1352. # array_from_buffer is reconstructed from a buffer holding a view
  1353. # to the initial array's data, so modifying an element in array
  1354. # should modify it in array_from_buffer too.
  1355. array[0] = -1
  1356. assert array_from_buffer[0] == -1, array_from_buffer[0]
  1357. class TestMethods:
  1358. sort_kinds = ['quicksort', 'heapsort', 'stable']
  1359. def test_all_where(self):
  1360. a = np.array([[True, False, True],
  1361. [False, False, False],
  1362. [True, True, True]])
  1363. wh_full = np.array([[True, False, True],
  1364. [False, False, False],
  1365. [True, False, True]])
  1366. wh_lower = np.array([[False],
  1367. [False],
  1368. [True]])
  1369. for _ax in [0, None]:
  1370. assert_equal(a.all(axis=_ax, where=wh_lower),
  1371. np.all(a[wh_lower[:,0],:], axis=_ax))
  1372. assert_equal(np.all(a, axis=_ax, where=wh_lower),
  1373. a[wh_lower[:,0],:].all(axis=_ax))
  1374. assert_equal(a.all(where=wh_full), True)
  1375. assert_equal(np.all(a, where=wh_full), True)
  1376. assert_equal(a.all(where=False), True)
  1377. assert_equal(np.all(a, where=False), True)
  1378. def test_any_where(self):
  1379. a = np.array([[True, False, True],
  1380. [False, False, False],
  1381. [True, True, True]])
  1382. wh_full = np.array([[False, True, False],
  1383. [True, True, True],
  1384. [False, False, False]])
  1385. wh_middle = np.array([[False],
  1386. [True],
  1387. [False]])
  1388. for _ax in [0, None]:
  1389. assert_equal(a.any(axis=_ax, where=wh_middle),
  1390. np.any(a[wh_middle[:,0],:], axis=_ax))
  1391. assert_equal(np.any(a, axis=_ax, where=wh_middle),
  1392. a[wh_middle[:,0],:].any(axis=_ax))
  1393. assert_equal(a.any(where=wh_full), False)
  1394. assert_equal(np.any(a, where=wh_full), False)
  1395. assert_equal(a.any(where=False), False)
  1396. assert_equal(np.any(a, where=False), False)
  1397. def test_compress(self):
  1398. tgt = [[5, 6, 7, 8, 9]]
  1399. arr = np.arange(10).reshape(2, 5)
  1400. out = arr.compress([0, 1], axis=0)
  1401. assert_equal(out, tgt)
  1402. tgt = [[1, 3], [6, 8]]
  1403. out = arr.compress([0, 1, 0, 1, 0], axis=1)
  1404. assert_equal(out, tgt)
  1405. tgt = [[1], [6]]
  1406. arr = np.arange(10).reshape(2, 5)
  1407. out = arr.compress([0, 1], axis=1)
  1408. assert_equal(out, tgt)
  1409. arr = np.arange(10).reshape(2, 5)
  1410. out = arr.compress([0, 1])
  1411. assert_equal(out, 1)
  1412. def test_choose(self):
  1413. x = 2*np.ones((3,), dtype=int)
  1414. y = 3*np.ones((3,), dtype=int)
  1415. x2 = 2*np.ones((2, 3), dtype=int)
  1416. y2 = 3*np.ones((2, 3), dtype=int)
  1417. ind = np.array([0, 0, 1])
  1418. A = ind.choose((x, y))
  1419. assert_equal(A, [2, 2, 3])
  1420. A = ind.choose((x2, y2))
  1421. assert_equal(A, [[2, 2, 3], [2, 2, 3]])
  1422. A = ind.choose((x, y2))
  1423. assert_equal(A, [[2, 2, 3], [2, 2, 3]])
  1424. oned = np.ones(1)
  1425. # gh-12031, caused SEGFAULT
  1426. assert_raises(TypeError, oned.choose,np.void(0), [oned])
  1427. out = np.array(0)
  1428. ret = np.choose(np.array(1), [10, 20, 30], out=out)
  1429. assert out is ret
  1430. assert_equal(out[()], 20)
  1431. # gh-6272 check overlap on out
  1432. x = np.arange(5)
  1433. y = np.choose([0,0,0], [x[:3], x[:3], x[:3]], out=x[1:4], mode='wrap')
  1434. assert_equal(y, np.array([0, 1, 2]))
  1435. def test_prod(self):
  1436. ba = [1, 2, 10, 11, 6, 5, 4]
  1437. ba2 = [[1, 2, 3, 4], [5, 6, 7, 9], [10, 3, 4, 5]]
  1438. for ctype in [np.int16, np.uint16, np.int32, np.uint32,
  1439. np.float32, np.float64, np.complex64, np.complex128]:
  1440. a = np.array(ba, ctype)
  1441. a2 = np.array(ba2, ctype)
  1442. if ctype in ['1', 'b']:
  1443. assert_raises(ArithmeticError, a.prod)
  1444. assert_raises(ArithmeticError, a2.prod, axis=1)
  1445. else:
  1446. assert_equal(a.prod(axis=0), 26400)
  1447. assert_array_equal(a2.prod(axis=0),
  1448. np.array([50, 36, 84, 180], ctype))
  1449. assert_array_equal(a2.prod(axis=-1),
  1450. np.array([24, 1890, 600], ctype))
  1451. def test_repeat(self):
  1452. m = np.array([1, 2, 3, 4, 5, 6])
  1453. m_rect = m.reshape((2, 3))
  1454. A = m.repeat([1, 3, 2, 1, 1, 2])
  1455. assert_equal(A, [1, 2, 2, 2, 3,
  1456. 3, 4, 5, 6, 6])
  1457. A = m.repeat(2)
  1458. assert_equal(A, [1, 1, 2, 2, 3, 3,
  1459. 4, 4, 5, 5, 6, 6])
  1460. A = m_rect.repeat([2, 1], axis=0)
  1461. assert_equal(A, [[1, 2, 3],
  1462. [1, 2, 3],
  1463. [4, 5, 6]])
  1464. A = m_rect.repeat([1, 3, 2], axis=1)
  1465. assert_equal(A, [[1, 2, 2, 2, 3, 3],
  1466. [4, 5, 5, 5, 6, 6]])
  1467. A = m_rect.repeat(2, axis=0)
  1468. assert_equal(A, [[1, 2, 3],
  1469. [1, 2, 3],
  1470. [4, 5, 6],
  1471. [4, 5, 6]])
  1472. A = m_rect.repeat(2, axis=1)
  1473. assert_equal(A, [[1, 1, 2, 2, 3, 3],
  1474. [4, 4, 5, 5, 6, 6]])
  1475. def test_reshape(self):
  1476. arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
  1477. tgt = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]
  1478. assert_equal(arr.reshape(2, 6), tgt)
  1479. tgt = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
  1480. assert_equal(arr.reshape(3, 4), tgt)
  1481. tgt = [[1, 10, 8, 6], [4, 2, 11, 9], [7, 5, 3, 12]]
  1482. assert_equal(arr.reshape((3, 4), order='F'), tgt)
  1483. tgt = [[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]
  1484. assert_equal(arr.T.reshape((3, 4), order='C'), tgt)
  1485. def test_round(self):
  1486. def check_round(arr, expected, *round_args):
  1487. assert_equal(arr.round(*round_args), expected)
  1488. # With output array
  1489. out = np.zeros_like(arr)
  1490. res = arr.round(*round_args, out=out)
  1491. assert_equal(out, expected)
  1492. assert out is res
  1493. check_round(np.array([1.2, 1.5]), [1, 2])
  1494. check_round(np.array(1.5), 2)
  1495. check_round(np.array([12.2, 15.5]), [10, 20], -1)
  1496. check_round(np.array([12.15, 15.51]), [12.2, 15.5], 1)
  1497. # Complex rounding
  1498. check_round(np.array([4.5 + 1.5j]), [4 + 2j])
  1499. check_round(np.array([12.5 + 15.5j]), [10 + 20j], -1)
  1500. def test_squeeze(self):
  1501. a = np.array([[[1], [2], [3]]])
  1502. assert_equal(a.squeeze(), [1, 2, 3])
  1503. assert_equal(a.squeeze(axis=(0,)), [[1], [2], [3]])
  1504. assert_raises(ValueError, a.squeeze, axis=(1,))
  1505. assert_equal(a.squeeze(axis=(2,)), [[1, 2, 3]])
  1506. def test_transpose(self):
  1507. a = np.array([[1, 2], [3, 4]])
  1508. assert_equal(a.transpose(), [[1, 3], [2, 4]])
  1509. assert_raises(ValueError, lambda: a.transpose(0))
  1510. assert_raises(ValueError, lambda: a.transpose(0, 0))
  1511. assert_raises(ValueError, lambda: a.transpose(0, 1, 2))
  1512. def test_sort(self):
  1513. # test ordering for floats and complex containing nans. It is only
  1514. # necessary to check the less-than comparison, so sorts that
  1515. # only follow the insertion sort path are sufficient. We only
  1516. # test doubles and complex doubles as the logic is the same.
  1517. # check doubles
  1518. msg = "Test real sort order with nans"
  1519. a = np.array([np.nan, 1, 0])
  1520. b = np.sort(a)
  1521. assert_equal(b, a[::-1], msg)
  1522. # check complex
  1523. msg = "Test complex sort order with nans"
  1524. a = np.zeros(9, dtype=np.complex128)
  1525. a.real += [np.nan, np.nan, np.nan, 1, 0, 1, 1, 0, 0]
  1526. a.imag += [np.nan, 1, 0, np.nan, np.nan, 1, 0, 1, 0]
  1527. b = np.sort(a)
  1528. assert_equal(b, a[::-1], msg)
  1529. # all c scalar sorts use the same code with different types
  1530. # so it suffices to run a quick check with one type. The number
  1531. # of sorted items must be greater than ~50 to check the actual
  1532. # algorithm because quick and merge sort fall over to insertion
  1533. # sort for small arrays.
  1534. @pytest.mark.parametrize('dtype', [np.uint8, np.uint16, np.uint32, np.uint64,
  1535. np.float16, np.float32, np.float64,
  1536. np.longdouble])
  1537. def test_sort_unsigned(self, dtype):
  1538. a = np.arange(101, dtype=dtype)
  1539. b = a[::-1].copy()
  1540. for kind in self.sort_kinds:
  1541. msg = "scalar sort, kind=%s" % kind
  1542. c = a.copy()
  1543. c.sort(kind=kind)
  1544. assert_equal(c, a, msg)
  1545. c = b.copy()
  1546. c.sort(kind=kind)
  1547. assert_equal(c, a, msg)
  1548. @pytest.mark.parametrize('dtype',
  1549. [np.int8, np.int16, np.int32, np.int64, np.float16,
  1550. np.float32, np.float64, np.longdouble])
  1551. def test_sort_signed(self, dtype):
  1552. a = np.arange(-50, 51, dtype=dtype)
  1553. b = a[::-1].copy()
  1554. for kind in self.sort_kinds:
  1555. msg = "scalar sort, kind=%s" % (kind)
  1556. c = a.copy()
  1557. c.sort(kind=kind)
  1558. assert_equal(c, a, msg)
  1559. c = b.copy()
  1560. c.sort(kind=kind)
  1561. assert_equal(c, a, msg)
  1562. @pytest.mark.parametrize('dtype', [np.float32, np.float64, np.longdouble])
  1563. @pytest.mark.parametrize('part', ['real', 'imag'])
  1564. def test_sort_complex(self, part, dtype):
  1565. # test complex sorts. These use the same code as the scalars
  1566. # but the compare function differs.
  1567. cdtype = {
  1568. np.single: np.csingle,
  1569. np.double: np.cdouble,
  1570. np.longdouble: np.clongdouble,
  1571. }[dtype]
  1572. a = np.arange(-50, 51, dtype=dtype)
  1573. b = a[::-1].copy()
  1574. ai = (a * (1+1j)).astype(cdtype)
  1575. bi = (b * (1+1j)).astype(cdtype)
  1576. setattr(ai, part, 1)
  1577. setattr(bi, part, 1)
  1578. for kind in self.sort_kinds:
  1579. msg = "complex sort, %s part == 1, kind=%s" % (part, kind)
  1580. c = ai.copy()
  1581. c.sort(kind=kind)
  1582. assert_equal(c, ai, msg)
  1583. c = bi.copy()
  1584. c.sort(kind=kind)
  1585. assert_equal(c, ai, msg)
  1586. def test_sort_complex_byte_swapping(self):
  1587. # test sorting of complex arrays requiring byte-swapping, gh-5441
  1588. for endianness in '<>':
  1589. for dt in np.typecodes['Complex']:
  1590. arr = np.array([1+3.j, 2+2.j, 3+1.j], dtype=endianness + dt)
  1591. c = arr.copy()
  1592. c.sort()
  1593. msg = 'byte-swapped complex sort, dtype={0}'.format(dt)
  1594. assert_equal(c, arr, msg)
  1595. @pytest.mark.parametrize('dtype', [np.bytes_, np.unicode_])
  1596. def test_sort_string(self, dtype):
  1597. # np.array will perform the encoding to bytes for us in the bytes test
  1598. a = np.array(['aaaaaaaa' + chr(i) for i in range(101)], dtype=dtype)
  1599. b = a[::-1].copy()
  1600. for kind in self.sort_kinds:
  1601. msg = "kind=%s" % kind
  1602. c = a.copy()
  1603. c.sort(kind=kind)
  1604. assert_equal(c, a, msg)
  1605. c = b.copy()
  1606. c.sort(kind=kind)
  1607. assert_equal(c, a, msg)
  1608. def test_sort_object(self):
  1609. # test object array sorts.
  1610. a = np.empty((101,), dtype=object)
  1611. a[:] = list(range(101))
  1612. b = a[::-1]
  1613. for kind in ['q', 'h', 'm']:
  1614. msg = "kind=%s" % kind
  1615. c = a.copy()
  1616. c.sort(kind=kind)
  1617. assert_equal(c, a, msg)
  1618. c = b.copy()
  1619. c.sort(kind=kind)
  1620. assert_equal(c, a, msg)
  1621. def test_sort_structured(self):
  1622. # test record array sorts.
  1623. dt = np.dtype([('f', float), ('i', int)])
  1624. a = np.array([(i, i) for i in range(101)], dtype=dt)
  1625. b = a[::-1]
  1626. for kind in ['q', 'h', 'm']:
  1627. msg = "kind=%s" % kind
  1628. c = a.copy()
  1629. c.sort(kind=kind)
  1630. assert_equal(c, a, msg)
  1631. c = b.copy()
  1632. c.sort(kind=kind)
  1633. assert_equal(c, a, msg)
  1634. @pytest.mark.parametrize('dtype', ['datetime64[D]', 'timedelta64[D]'])
  1635. def test_sort_time(self, dtype):
  1636. # test datetime64 and timedelta64 sorts.
  1637. a = np.arange(0, 101, dtype=dtype)
  1638. b = a[::-1]
  1639. for kind in ['q', 'h', 'm']:
  1640. msg = "kind=%s" % kind
  1641. c = a.copy()
  1642. c.sort(kind=kind)
  1643. assert_equal(c, a, msg)
  1644. c = b.copy()
  1645. c.sort(kind=kind)
  1646. assert_equal(c, a, msg)
  1647. def test_sort_axis(self):
  1648. # check axis handling. This should be the same for all type
  1649. # specific sorts, so we only check it for one type and one kind
  1650. a = np.array([[3, 2], [1, 0]])
  1651. b = np.array([[1, 0], [3, 2]])
  1652. c = np.array([[2, 3], [0, 1]])
  1653. d = a.copy()
  1654. d.sort(axis=0)
  1655. assert_equal(d, b, "test sort with axis=0")
  1656. d = a.copy()
  1657. d.sort(axis=1)
  1658. assert_equal(d, c, "test sort with axis=1")
  1659. d = a.copy()
  1660. d.sort()
  1661. assert_equal(d, c, "test sort with default axis")
  1662. def test_sort_size_0(self):
  1663. # check axis handling for multidimensional empty arrays
  1664. a = np.array([])
  1665. a.shape = (3, 2, 1, 0)
  1666. for axis in range(-a.ndim, a.ndim):
  1667. msg = 'test empty array sort with axis={0}'.format(axis)
  1668. assert_equal(np.sort(a, axis=axis), a, msg)
  1669. msg = 'test empty array sort with axis=None'
  1670. assert_equal(np.sort(a, axis=None), a.ravel(), msg)
  1671. def test_sort_bad_ordering(self):
  1672. # test generic class with bogus ordering,
  1673. # should not segfault.
  1674. class Boom:
  1675. def __lt__(self, other):
  1676. return True
  1677. a = np.array([Boom()] * 100, dtype=object)
  1678. for kind in self.sort_kinds:
  1679. msg = "kind=%s" % kind
  1680. c = a.copy()
  1681. c.sort(kind=kind)
  1682. assert_equal(c, a, msg)
  1683. def test_void_sort(self):
  1684. # gh-8210 - previously segfaulted
  1685. for i in range(4):
  1686. rand = np.random.randint(256, size=4000, dtype=np.uint8)
  1687. arr = rand.view('V4')
  1688. arr[::-1].sort()
  1689. dt = np.dtype([('val', 'i4', (1,))])
  1690. for i in range(4):
  1691. rand = np.random.randint(256, size=4000, dtype=np.uint8)
  1692. arr = rand.view(dt)
  1693. arr[::-1].sort()
  1694. def test_sort_raises(self):
  1695. #gh-9404
  1696. arr = np.array([0, datetime.now(), 1], dtype=object)
  1697. for kind in self.sort_kinds:
  1698. assert_raises(TypeError, arr.sort, kind=kind)
  1699. #gh-3879
  1700. class Raiser:
  1701. def raises_anything(*args, **kwargs):
  1702. raise TypeError("SOMETHING ERRORED")
  1703. __eq__ = __ne__ = __lt__ = __gt__ = __ge__ = __le__ = raises_anything
  1704. arr = np.array([[Raiser(), n] for n in range(10)]).reshape(-1)
  1705. np.random.shuffle(arr)
  1706. for kind in self.sort_kinds:
  1707. assert_raises(TypeError, arr.sort, kind=kind)
  1708. def test_sort_degraded(self):
  1709. # test degraded dataset would take minutes to run with normal qsort
  1710. d = np.arange(1000000)
  1711. do = d.copy()
  1712. x = d
  1713. # create a median of 3 killer where each median is the sorted second
  1714. # last element of the quicksort partition
  1715. while x.size > 3:
  1716. mid = x.size // 2
  1717. x[mid], x[-2] = x[-2], x[mid]
  1718. x = x[:-2]
  1719. assert_equal(np.sort(d), do)
  1720. assert_equal(d[np.argsort(d)], do)
  1721. def test_copy(self):
  1722. def assert_fortran(arr):
  1723. assert_(arr.flags.fortran)
  1724. assert_(arr.flags.f_contiguous)
  1725. assert_(not arr.flags.c_contiguous)
  1726. def assert_c(arr):
  1727. assert_(not arr.flags.fortran)
  1728. assert_(not arr.flags.f_contiguous)
  1729. assert_(arr.flags.c_contiguous)
  1730. a = np.empty((2, 2), order='F')
  1731. # Test copying a Fortran array
  1732. assert_c(a.copy())
  1733. assert_c(a.copy('C'))
  1734. assert_fortran(a.copy('F'))
  1735. assert_fortran(a.copy('A'))
  1736. # Now test starting with a C array.
  1737. a = np.empty((2, 2), order='C')
  1738. assert_c(a.copy())
  1739. assert_c(a.copy('C'))
  1740. assert_fortran(a.copy('F'))
  1741. assert_c(a.copy('A'))
  1742. def test_sort_order(self):
  1743. # Test sorting an array with fields
  1744. x1 = np.array([21, 32, 14])
  1745. x2 = np.array(['my', 'first', 'name'])
  1746. x3 = np.array([3.1, 4.5, 6.2])
  1747. r = np.rec.fromarrays([x1, x2, x3], names='id,word,number')
  1748. r.sort(order=['id'])
  1749. assert_equal(r.id, np.array([14, 21, 32]))
  1750. assert_equal(r.word, np.array(['name', 'my', 'first']))
  1751. assert_equal(r.number, np.array([6.2, 3.1, 4.5]))
  1752. r.sort(order=['word'])
  1753. assert_equal(r.id, np.array([32, 21, 14]))
  1754. assert_equal(r.word, np.array(['first', 'my', 'name']))
  1755. assert_equal(r.number, np.array([4.5, 3.1, 6.2]))
  1756. r.sort(order=['number'])
  1757. assert_equal(r.id, np.array([21, 32, 14]))
  1758. assert_equal(r.word, np.array(['my', 'first', 'name']))
  1759. assert_equal(r.number, np.array([3.1, 4.5, 6.2]))
  1760. assert_raises_regex(ValueError, 'duplicate',
  1761. lambda: r.sort(order=['id', 'id']))
  1762. if sys.byteorder == 'little':
  1763. strtype = '>i2'
  1764. else:
  1765. strtype = '<i2'
  1766. mydtype = [('name', 'U5'), ('col2', strtype)]
  1767. r = np.array([('a', 1), ('b', 255), ('c', 3), ('d', 258)],
  1768. dtype=mydtype)
  1769. r.sort(order='col2')
  1770. assert_equal(r['col2'], [1, 3, 255, 258])
  1771. assert_equal(r, np.array([('a', 1), ('c', 3), ('b', 255), ('d', 258)],
  1772. dtype=mydtype))
  1773. def test_argsort(self):
  1774. # all c scalar argsorts use the same code with different types
  1775. # so it suffices to run a quick check with one type. The number
  1776. # of sorted items must be greater than ~50 to check the actual
  1777. # algorithm because quick and merge sort fall over to insertion
  1778. # sort for small arrays.
  1779. for dtype in [np.int32, np.uint32, np.float32]:
  1780. a = np.arange(101, dtype=dtype)
  1781. b = a[::-1].copy()
  1782. for kind in self.sort_kinds:
  1783. msg = "scalar argsort, kind=%s, dtype=%s" % (kind, dtype)
  1784. assert_equal(a.copy().argsort(kind=kind), a, msg)
  1785. assert_equal(b.copy().argsort(kind=kind), b, msg)
  1786. # test complex argsorts. These use the same code as the scalars
  1787. # but the compare function differs.
  1788. ai = a*1j + 1
  1789. bi = b*1j + 1
  1790. for kind in self.sort_kinds:
  1791. msg = "complex argsort, kind=%s" % kind
  1792. assert_equal(ai.copy().argsort(kind=kind), a, msg)
  1793. assert_equal(bi.copy().argsort(kind=kind), b, msg)
  1794. ai = a + 1j
  1795. bi = b + 1j
  1796. for kind in self.sort_kinds:
  1797. msg = "complex argsort, kind=%s" % kind
  1798. assert_equal(ai.copy().argsort(kind=kind), a, msg)
  1799. assert_equal(bi.copy().argsort(kind=kind), b, msg)
  1800. # test argsort of complex arrays requiring byte-swapping, gh-5441
  1801. for endianness in '<>':
  1802. for dt in np.typecodes['Complex']:
  1803. arr = np.array([1+3.j, 2+2.j, 3+1.j], dtype=endianness + dt)
  1804. msg = 'byte-swapped complex argsort, dtype={0}'.format(dt)
  1805. assert_equal(arr.argsort(),
  1806. np.arange(len(arr), dtype=np.intp), msg)
  1807. # test string argsorts.
  1808. s = 'aaaaaaaa'
  1809. a = np.array([s + chr(i) for i in range(101)])
  1810. b = a[::-1].copy()
  1811. r = np.arange(101)
  1812. rr = r[::-1]
  1813. for kind in self.sort_kinds:
  1814. msg = "string argsort, kind=%s" % kind
  1815. assert_equal(a.copy().argsort(kind=kind), r, msg)
  1816. assert_equal(b.copy().argsort(kind=kind), rr, msg)
  1817. # test unicode argsorts.
  1818. s = 'aaaaaaaa'
  1819. a = np.array([s + chr(i) for i in range(101)], dtype=np.unicode_)
  1820. b = a[::-1]
  1821. r = np.arange(101)
  1822. rr = r[::-1]
  1823. for kind in self.sort_kinds:
  1824. msg = "unicode argsort, kind=%s" % kind
  1825. assert_equal(a.copy().argsort(kind=kind), r, msg)
  1826. assert_equal(b.copy().argsort(kind=kind), rr, msg)
  1827. # test object array argsorts.
  1828. a = np.empty((101,), dtype=object)
  1829. a[:] = list(range(101))
  1830. b = a[::-1]
  1831. r = np.arange(101)
  1832. rr = r[::-1]
  1833. for kind in self.sort_kinds:
  1834. msg = "object argsort, kind=%s" % kind
  1835. assert_equal(a.copy().argsort(kind=kind), r, msg)
  1836. assert_equal(b.copy().argsort(kind=kind), rr, msg)
  1837. # test structured array argsorts.
  1838. dt = np.dtype([('f', float), ('i', int)])
  1839. a = np.array([(i, i) for i in range(101)], dtype=dt)
  1840. b = a[::-1]
  1841. r = np.arange(101)
  1842. rr = r[::-1]
  1843. for kind in self.sort_kinds:
  1844. msg = "structured array argsort, kind=%s" % kind
  1845. assert_equal(a.copy().argsort(kind=kind), r, msg)
  1846. assert_equal(b.copy().argsort(kind=kind), rr, msg)
  1847. # test datetime64 argsorts.
  1848. a = np.arange(0, 101, dtype='datetime64[D]')
  1849. b = a[::-1]
  1850. r = np.arange(101)
  1851. rr = r[::-1]
  1852. for kind in ['q', 'h', 'm']:
  1853. msg = "datetime64 argsort, kind=%s" % kind
  1854. assert_equal(a.copy().argsort(kind=kind), r, msg)
  1855. assert_equal(b.copy().argsort(kind=kind), rr, msg)
  1856. # test timedelta64 argsorts.
  1857. a = np.arange(0, 101, dtype='timedelta64[D]')
  1858. b = a[::-1]
  1859. r = np.arange(101)
  1860. rr = r[::-1]
  1861. for kind in ['q', 'h', 'm']:
  1862. msg = "timedelta64 argsort, kind=%s" % kind
  1863. assert_equal(a.copy().argsort(kind=kind), r, msg)
  1864. assert_equal(b.copy().argsort(kind=kind), rr, msg)
  1865. # check axis handling. This should be the same for all type
  1866. # specific argsorts, so we only check it for one type and one kind
  1867. a = np.array([[3, 2], [1, 0]])
  1868. b = np.array([[1, 1], [0, 0]])
  1869. c = np.array([[1, 0], [1, 0]])
  1870. assert_equal(a.copy().argsort(axis=0), b)
  1871. assert_equal(a.copy().argsort(axis=1), c)
  1872. assert_equal(a.copy().argsort(), c)
  1873. # check axis handling for multidimensional empty arrays
  1874. a = np.array([])
  1875. a.shape = (3, 2, 1, 0)
  1876. for axis in range(-a.ndim, a.ndim):
  1877. msg = 'test empty array argsort with axis={0}'.format(axis)
  1878. assert_equal(np.argsort(a, axis=axis),
  1879. np.zeros_like(a, dtype=np.intp), msg)
  1880. msg = 'test empty array argsort with axis=None'
  1881. assert_equal(np.argsort(a, axis=None),
  1882. np.zeros_like(a.ravel(), dtype=np.intp), msg)
  1883. # check that stable argsorts are stable
  1884. r = np.arange(100)
  1885. # scalars
  1886. a = np.zeros(100)
  1887. assert_equal(a.argsort(kind='m'), r)
  1888. # complex
  1889. a = np.zeros(100, dtype=complex)
  1890. assert_equal(a.argsort(kind='m'), r)
  1891. # string
  1892. a = np.array(['aaaaaaaaa' for i in range(100)])
  1893. assert_equal(a.argsort(kind='m'), r)
  1894. # unicode
  1895. a = np.array(['aaaaaaaaa' for i in range(100)], dtype=np.unicode_)
  1896. assert_equal(a.argsort(kind='m'), r)
  1897. def test_sort_unicode_kind(self):
  1898. d = np.arange(10)
  1899. k = b'\xc3\xa4'.decode("UTF8")
  1900. assert_raises(ValueError, d.sort, kind=k)
  1901. assert_raises(ValueError, d.argsort, kind=k)
  1902. def test_searchsorted(self):
  1903. # test for floats and complex containing nans. The logic is the
  1904. # same for all float types so only test double types for now.
  1905. # The search sorted routines use the compare functions for the
  1906. # array type, so this checks if that is consistent with the sort
  1907. # order.
  1908. # check double
  1909. a = np.array([0, 1, np.nan])
  1910. msg = "Test real searchsorted with nans, side='l'"
  1911. b = a.searchsorted(a, side='left')
  1912. assert_equal(b, np.arange(3), msg)
  1913. msg = "Test real searchsorted with nans, side='r'"
  1914. b = a.searchsorted(a, side='right')
  1915. assert_equal(b, np.arange(1, 4), msg)
  1916. # check keyword arguments
  1917. a.searchsorted(v=1)
  1918. # check double complex
  1919. a = np.zeros(9, dtype=np.complex128)
  1920. a.real += [0, 0, 1, 1, 0, 1, np.nan, np.nan, np.nan]
  1921. a.imag += [0, 1, 0, 1, np.nan, np.nan, 0, 1, np.nan]
  1922. msg = "Test complex searchsorted with nans, side='l'"
  1923. b = a.searchsorted(a, side='left')
  1924. assert_equal(b, np.arange(9), msg)
  1925. msg = "Test complex searchsorted with nans, side='r'"
  1926. b = a.searchsorted(a, side='right')
  1927. assert_equal(b, np.arange(1, 10), msg)
  1928. msg = "Test searchsorted with little endian, side='l'"
  1929. a = np.array([0, 128], dtype='<i4')
  1930. b = a.searchsorted(np.array(128, dtype='<i4'))
  1931. assert_equal(b, 1, msg)
  1932. msg = "Test searchsorted with big endian, side='l'"
  1933. a = np.array([0, 128], dtype='>i4')
  1934. b = a.searchsorted(np.array(128, dtype='>i4'))
  1935. assert_equal(b, 1, msg)
  1936. # Check 0 elements
  1937. a = np.ones(0)
  1938. b = a.searchsorted([0, 1, 2], 'left')
  1939. assert_equal(b, [0, 0, 0])
  1940. b = a.searchsorted([0, 1, 2], 'right')
  1941. assert_equal(b, [0, 0, 0])
  1942. a = np.ones(1)
  1943. # Check 1 element
  1944. b = a.searchsorted([0, 1, 2], 'left')
  1945. assert_equal(b, [0, 0, 1])
  1946. b = a.searchsorted([0, 1, 2], 'right')
  1947. assert_equal(b, [0, 1, 1])
  1948. # Check all elements equal
  1949. a = np.ones(2)
  1950. b = a.searchsorted([0, 1, 2], 'left')
  1951. assert_equal(b, [0, 0, 2])
  1952. b = a.searchsorted([0, 1, 2], 'right')
  1953. assert_equal(b, [0, 2, 2])
  1954. # Test searching unaligned array
  1955. a = np.arange(10)
  1956. aligned = np.empty(a.itemsize * a.size + 1, 'uint8')
  1957. unaligned = aligned[1:].view(a.dtype)
  1958. unaligned[:] = a
  1959. # Test searching unaligned array
  1960. b = unaligned.searchsorted(a, 'left')
  1961. assert_equal(b, a)
  1962. b = unaligned.searchsorted(a, 'right')
  1963. assert_equal(b, a + 1)
  1964. # Test searching for unaligned keys
  1965. b = a.searchsorted(unaligned, 'left')
  1966. assert_equal(b, a)
  1967. b = a.searchsorted(unaligned, 'right')
  1968. assert_equal(b, a + 1)
  1969. # Test smart resetting of binsearch indices
  1970. a = np.arange(5)
  1971. b = a.searchsorted([6, 5, 4], 'left')
  1972. assert_equal(b, [5, 5, 4])
  1973. b = a.searchsorted([6, 5, 4], 'right')
  1974. assert_equal(b, [5, 5, 5])
  1975. # Test all type specific binary search functions
  1976. types = ''.join((np.typecodes['AllInteger'], np.typecodes['AllFloat'],
  1977. np.typecodes['Datetime'], '?O'))
  1978. for dt in types:
  1979. if dt == 'M':
  1980. dt = 'M8[D]'
  1981. if dt == '?':
  1982. a = np.arange(2, dtype=dt)
  1983. out = np.arange(2)
  1984. else:
  1985. a = np.arange(0, 5, dtype=dt)
  1986. out = np.arange(5)
  1987. b = a.searchsorted(a, 'left')
  1988. assert_equal(b, out)
  1989. b = a.searchsorted(a, 'right')
  1990. assert_equal(b, out + 1)
  1991. # Test empty array, use a fresh array to get warnings in
  1992. # valgrind if access happens.
  1993. e = np.ndarray(shape=0, buffer=b'', dtype=dt)
  1994. b = e.searchsorted(a, 'left')
  1995. assert_array_equal(b, np.zeros(len(a), dtype=np.intp))
  1996. b = a.searchsorted(e, 'left')
  1997. assert_array_equal(b, np.zeros(0, dtype=np.intp))
  1998. def test_searchsorted_unicode(self):
  1999. # Test searchsorted on unicode strings.
  2000. # 1.6.1 contained a string length miscalculation in
  2001. # arraytypes.c.src:UNICODE_compare() which manifested as
  2002. # incorrect/inconsistent results from searchsorted.
  2003. a = np.array(['P:\\20x_dapi_cy3\\20x_dapi_cy3_20100185_1',
  2004. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100186_1',
  2005. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100187_1',
  2006. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100189_1',
  2007. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100190_1',
  2008. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100191_1',
  2009. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100192_1',
  2010. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100193_1',
  2011. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100194_1',
  2012. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100195_1',
  2013. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100196_1',
  2014. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100197_1',
  2015. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100198_1',
  2016. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100199_1'],
  2017. dtype=np.unicode_)
  2018. ind = np.arange(len(a))
  2019. assert_equal([a.searchsorted(v, 'left') for v in a], ind)
  2020. assert_equal([a.searchsorted(v, 'right') for v in a], ind + 1)
  2021. assert_equal([a.searchsorted(a[i], 'left') for i in ind], ind)
  2022. assert_equal([a.searchsorted(a[i], 'right') for i in ind], ind + 1)
  2023. def test_searchsorted_with_invalid_sorter(self):
  2024. a = np.array([5, 2, 1, 3, 4])
  2025. s = np.argsort(a)
  2026. assert_raises(TypeError, np.searchsorted, a, 0,
  2027. sorter=np.array((1, (2, 3)), dtype=object))
  2028. assert_raises(TypeError, np.searchsorted, a, 0, sorter=[1.1])
  2029. assert_raises(ValueError, np.searchsorted, a, 0, sorter=[1, 2, 3, 4])
  2030. assert_raises(ValueError, np.searchsorted, a, 0, sorter=[1, 2, 3, 4, 5, 6])
  2031. # bounds check
  2032. assert_raises(ValueError, np.searchsorted, a, 4, sorter=[0, 1, 2, 3, 5])
  2033. assert_raises(ValueError, np.searchsorted, a, 0, sorter=[-1, 0, 1, 2, 3])
  2034. assert_raises(ValueError, np.searchsorted, a, 0, sorter=[4, 0, -1, 2, 3])
  2035. def test_searchsorted_with_sorter(self):
  2036. a = np.random.rand(300)
  2037. s = a.argsort()
  2038. b = np.sort(a)
  2039. k = np.linspace(0, 1, 20)
  2040. assert_equal(b.searchsorted(k), a.searchsorted(k, sorter=s))
  2041. a = np.array([0, 1, 2, 3, 5]*20)
  2042. s = a.argsort()
  2043. k = [0, 1, 2, 3, 5]
  2044. expected = [0, 20, 40, 60, 80]
  2045. assert_equal(a.searchsorted(k, side='left', sorter=s), expected)
  2046. expected = [20, 40, 60, 80, 100]
  2047. assert_equal(a.searchsorted(k, side='right', sorter=s), expected)
  2048. # Test searching unaligned array
  2049. keys = np.arange(10)
  2050. a = keys.copy()
  2051. np.random.shuffle(s)
  2052. s = a.argsort()
  2053. aligned = np.empty(a.itemsize * a.size + 1, 'uint8')
  2054. unaligned = aligned[1:].view(a.dtype)
  2055. # Test searching unaligned array
  2056. unaligned[:] = a
  2057. b = unaligned.searchsorted(keys, 'left', s)
  2058. assert_equal(b, keys)
  2059. b = unaligned.searchsorted(keys, 'right', s)
  2060. assert_equal(b, keys + 1)
  2061. # Test searching for unaligned keys
  2062. unaligned[:] = keys
  2063. b = a.searchsorted(unaligned, 'left', s)
  2064. assert_equal(b, keys)
  2065. b = a.searchsorted(unaligned, 'right', s)
  2066. assert_equal(b, keys + 1)
  2067. # Test all type specific indirect binary search functions
  2068. types = ''.join((np.typecodes['AllInteger'], np.typecodes['AllFloat'],
  2069. np.typecodes['Datetime'], '?O'))
  2070. for dt in types:
  2071. if dt == 'M':
  2072. dt = 'M8[D]'
  2073. if dt == '?':
  2074. a = np.array([1, 0], dtype=dt)
  2075. # We want the sorter array to be of a type that is different
  2076. # from np.intp in all platforms, to check for #4698
  2077. s = np.array([1, 0], dtype=np.int16)
  2078. out = np.array([1, 0])
  2079. else:
  2080. a = np.array([3, 4, 1, 2, 0], dtype=dt)
  2081. # We want the sorter array to be of a type that is different
  2082. # from np.intp in all platforms, to check for #4698
  2083. s = np.array([4, 2, 3, 0, 1], dtype=np.int16)
  2084. out = np.array([3, 4, 1, 2, 0], dtype=np.intp)
  2085. b = a.searchsorted(a, 'left', s)
  2086. assert_equal(b, out)
  2087. b = a.searchsorted(a, 'right', s)
  2088. assert_equal(b, out + 1)
  2089. # Test empty array, use a fresh array to get warnings in
  2090. # valgrind if access happens.
  2091. e = np.ndarray(shape=0, buffer=b'', dtype=dt)
  2092. b = e.searchsorted(a, 'left', s[:0])
  2093. assert_array_equal(b, np.zeros(len(a), dtype=np.intp))
  2094. b = a.searchsorted(e, 'left', s)
  2095. assert_array_equal(b, np.zeros(0, dtype=np.intp))
  2096. # Test non-contiguous sorter array
  2097. a = np.array([3, 4, 1, 2, 0])
  2098. srt = np.empty((10,), dtype=np.intp)
  2099. srt[1::2] = -1
  2100. srt[::2] = [4, 2, 3, 0, 1]
  2101. s = srt[::2]
  2102. out = np.array([3, 4, 1, 2, 0], dtype=np.intp)
  2103. b = a.searchsorted(a, 'left', s)
  2104. assert_equal(b, out)
  2105. b = a.searchsorted(a, 'right', s)
  2106. assert_equal(b, out + 1)
  2107. def test_searchsorted_return_type(self):
  2108. # Functions returning indices should always return base ndarrays
  2109. class A(np.ndarray):
  2110. pass
  2111. a = np.arange(5).view(A)
  2112. b = np.arange(1, 3).view(A)
  2113. s = np.arange(5).view(A)
  2114. assert_(not isinstance(a.searchsorted(b, 'left'), A))
  2115. assert_(not isinstance(a.searchsorted(b, 'right'), A))
  2116. assert_(not isinstance(a.searchsorted(b, 'left', s), A))
  2117. assert_(not isinstance(a.searchsorted(b, 'right', s), A))
  2118. def test_argpartition_out_of_range(self):
  2119. # Test out of range values in kth raise an error, gh-5469
  2120. d = np.arange(10)
  2121. assert_raises(ValueError, d.argpartition, 10)
  2122. assert_raises(ValueError, d.argpartition, -11)
  2123. # Test also for generic type argpartition, which uses sorting
  2124. # and used to not bound check kth
  2125. d_obj = np.arange(10, dtype=object)
  2126. assert_raises(ValueError, d_obj.argpartition, 10)
  2127. assert_raises(ValueError, d_obj.argpartition, -11)
  2128. def test_partition_out_of_range(self):
  2129. # Test out of range values in kth raise an error, gh-5469
  2130. d = np.arange(10)
  2131. assert_raises(ValueError, d.partition, 10)
  2132. assert_raises(ValueError, d.partition, -11)
  2133. # Test also for generic type partition, which uses sorting
  2134. # and used to not bound check kth
  2135. d_obj = np.arange(10, dtype=object)
  2136. assert_raises(ValueError, d_obj.partition, 10)
  2137. assert_raises(ValueError, d_obj.partition, -11)
  2138. def test_argpartition_integer(self):
  2139. # Test non-integer values in kth raise an error/
  2140. d = np.arange(10)
  2141. assert_raises(TypeError, d.argpartition, 9.)
  2142. # Test also for generic type argpartition, which uses sorting
  2143. # and used to not bound check kth
  2144. d_obj = np.arange(10, dtype=object)
  2145. assert_raises(TypeError, d_obj.argpartition, 9.)
  2146. def test_partition_integer(self):
  2147. # Test out of range values in kth raise an error, gh-5469
  2148. d = np.arange(10)
  2149. assert_raises(TypeError, d.partition, 9.)
  2150. # Test also for generic type partition, which uses sorting
  2151. # and used to not bound check kth
  2152. d_obj = np.arange(10, dtype=object)
  2153. assert_raises(TypeError, d_obj.partition, 9.)
  2154. def test_partition_empty_array(self):
  2155. # check axis handling for multidimensional empty arrays
  2156. a = np.array([])
  2157. a.shape = (3, 2, 1, 0)
  2158. for axis in range(-a.ndim, a.ndim):
  2159. msg = 'test empty array partition with axis={0}'.format(axis)
  2160. assert_equal(np.partition(a, 0, axis=axis), a, msg)
  2161. msg = 'test empty array partition with axis=None'
  2162. assert_equal(np.partition(a, 0, axis=None), a.ravel(), msg)
  2163. def test_argpartition_empty_array(self):
  2164. # check axis handling for multidimensional empty arrays
  2165. a = np.array([])
  2166. a.shape = (3, 2, 1, 0)
  2167. for axis in range(-a.ndim, a.ndim):
  2168. msg = 'test empty array argpartition with axis={0}'.format(axis)
  2169. assert_equal(np.partition(a, 0, axis=axis),
  2170. np.zeros_like(a, dtype=np.intp), msg)
  2171. msg = 'test empty array argpartition with axis=None'
  2172. assert_equal(np.partition(a, 0, axis=None),
  2173. np.zeros_like(a.ravel(), dtype=np.intp), msg)
  2174. def test_partition(self):
  2175. d = np.arange(10)
  2176. assert_raises(TypeError, np.partition, d, 2, kind=1)
  2177. assert_raises(ValueError, np.partition, d, 2, kind="nonsense")
  2178. assert_raises(ValueError, np.argpartition, d, 2, kind="nonsense")
  2179. assert_raises(ValueError, d.partition, 2, axis=0, kind="nonsense")
  2180. assert_raises(ValueError, d.argpartition, 2, axis=0, kind="nonsense")
  2181. for k in ("introselect",):
  2182. d = np.array([])
  2183. assert_array_equal(np.partition(d, 0, kind=k), d)
  2184. assert_array_equal(np.argpartition(d, 0, kind=k), d)
  2185. d = np.ones(1)
  2186. assert_array_equal(np.partition(d, 0, kind=k)[0], d)
  2187. assert_array_equal(d[np.argpartition(d, 0, kind=k)],
  2188. np.partition(d, 0, kind=k))
  2189. # kth not modified
  2190. kth = np.array([30, 15, 5])
  2191. okth = kth.copy()
  2192. np.partition(np.arange(40), kth)
  2193. assert_array_equal(kth, okth)
  2194. for r in ([2, 1], [1, 2], [1, 1]):
  2195. d = np.array(r)
  2196. tgt = np.sort(d)
  2197. assert_array_equal(np.partition(d, 0, kind=k)[0], tgt[0])
  2198. assert_array_equal(np.partition(d, 1, kind=k)[1], tgt[1])
  2199. assert_array_equal(d[np.argpartition(d, 0, kind=k)],
  2200. np.partition(d, 0, kind=k))
  2201. assert_array_equal(d[np.argpartition(d, 1, kind=k)],
  2202. np.partition(d, 1, kind=k))
  2203. for i in range(d.size):
  2204. d[i:].partition(0, kind=k)
  2205. assert_array_equal(d, tgt)
  2206. for r in ([3, 2, 1], [1, 2, 3], [2, 1, 3], [2, 3, 1],
  2207. [1, 1, 1], [1, 2, 2], [2, 2, 1], [1, 2, 1]):
  2208. d = np.array(r)
  2209. tgt = np.sort(d)
  2210. assert_array_equal(np.partition(d, 0, kind=k)[0], tgt[0])
  2211. assert_array_equal(np.partition(d, 1, kind=k)[1], tgt[1])
  2212. assert_array_equal(np.partition(d, 2, kind=k)[2], tgt[2])
  2213. assert_array_equal(d[np.argpartition(d, 0, kind=k)],
  2214. np.partition(d, 0, kind=k))
  2215. assert_array_equal(d[np.argpartition(d, 1, kind=k)],
  2216. np.partition(d, 1, kind=k))
  2217. assert_array_equal(d[np.argpartition(d, 2, kind=k)],
  2218. np.partition(d, 2, kind=k))
  2219. for i in range(d.size):
  2220. d[i:].partition(0, kind=k)
  2221. assert_array_equal(d, tgt)
  2222. d = np.ones(50)
  2223. assert_array_equal(np.partition(d, 0, kind=k), d)
  2224. assert_array_equal(d[np.argpartition(d, 0, kind=k)],
  2225. np.partition(d, 0, kind=k))
  2226. # sorted
  2227. d = np.arange(49)
  2228. assert_equal(np.partition(d, 5, kind=k)[5], 5)
  2229. assert_equal(np.partition(d, 15, kind=k)[15], 15)
  2230. assert_array_equal(d[np.argpartition(d, 5, kind=k)],
  2231. np.partition(d, 5, kind=k))
  2232. assert_array_equal(d[np.argpartition(d, 15, kind=k)],
  2233. np.partition(d, 15, kind=k))
  2234. # rsorted
  2235. d = np.arange(47)[::-1]
  2236. assert_equal(np.partition(d, 6, kind=k)[6], 6)
  2237. assert_equal(np.partition(d, 16, kind=k)[16], 16)
  2238. assert_array_equal(d[np.argpartition(d, 6, kind=k)],
  2239. np.partition(d, 6, kind=k))
  2240. assert_array_equal(d[np.argpartition(d, 16, kind=k)],
  2241. np.partition(d, 16, kind=k))
  2242. assert_array_equal(np.partition(d, -6, kind=k),
  2243. np.partition(d, 41, kind=k))
  2244. assert_array_equal(np.partition(d, -16, kind=k),
  2245. np.partition(d, 31, kind=k))
  2246. assert_array_equal(d[np.argpartition(d, -6, kind=k)],
  2247. np.partition(d, 41, kind=k))
  2248. # median of 3 killer, O(n^2) on pure median 3 pivot quickselect
  2249. # exercises the median of median of 5 code used to keep O(n)
  2250. d = np.arange(1000000)
  2251. x = np.roll(d, d.size // 2)
  2252. mid = x.size // 2 + 1
  2253. assert_equal(np.partition(x, mid)[mid], mid)
  2254. d = np.arange(1000001)
  2255. x = np.roll(d, d.size // 2 + 1)
  2256. mid = x.size // 2 + 1
  2257. assert_equal(np.partition(x, mid)[mid], mid)
  2258. # max
  2259. d = np.ones(10)
  2260. d[1] = 4
  2261. assert_equal(np.partition(d, (2, -1))[-1], 4)
  2262. assert_equal(np.partition(d, (2, -1))[2], 1)
  2263. assert_equal(d[np.argpartition(d, (2, -1))][-1], 4)
  2264. assert_equal(d[np.argpartition(d, (2, -1))][2], 1)
  2265. d[1] = np.nan
  2266. assert_(np.isnan(d[np.argpartition(d, (2, -1))][-1]))
  2267. assert_(np.isnan(np.partition(d, (2, -1))[-1]))
  2268. # equal elements
  2269. d = np.arange(47) % 7
  2270. tgt = np.sort(np.arange(47) % 7)
  2271. np.random.shuffle(d)
  2272. for i in range(d.size):
  2273. assert_equal(np.partition(d, i, kind=k)[i], tgt[i])
  2274. assert_array_equal(d[np.argpartition(d, 6, kind=k)],
  2275. np.partition(d, 6, kind=k))
  2276. assert_array_equal(d[np.argpartition(d, 16, kind=k)],
  2277. np.partition(d, 16, kind=k))
  2278. for i in range(d.size):
  2279. d[i:].partition(0, kind=k)
  2280. assert_array_equal(d, tgt)
  2281. d = np.array([0, 1, 2, 3, 4, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2282. 7, 7, 7, 7, 7, 9])
  2283. kth = [0, 3, 19, 20]
  2284. assert_equal(np.partition(d, kth, kind=k)[kth], (0, 3, 7, 7))
  2285. assert_equal(d[np.argpartition(d, kth, kind=k)][kth], (0, 3, 7, 7))
  2286. d = np.array([2, 1])
  2287. d.partition(0, kind=k)
  2288. assert_raises(ValueError, d.partition, 2)
  2289. assert_raises(np.AxisError, d.partition, 3, axis=1)
  2290. assert_raises(ValueError, np.partition, d, 2)
  2291. assert_raises(np.AxisError, np.partition, d, 2, axis=1)
  2292. assert_raises(ValueError, d.argpartition, 2)
  2293. assert_raises(np.AxisError, d.argpartition, 3, axis=1)
  2294. assert_raises(ValueError, np.argpartition, d, 2)
  2295. assert_raises(np.AxisError, np.argpartition, d, 2, axis=1)
  2296. d = np.arange(10).reshape((2, 5))
  2297. d.partition(1, axis=0, kind=k)
  2298. d.partition(4, axis=1, kind=k)
  2299. np.partition(d, 1, axis=0, kind=k)
  2300. np.partition(d, 4, axis=1, kind=k)
  2301. np.partition(d, 1, axis=None, kind=k)
  2302. np.partition(d, 9, axis=None, kind=k)
  2303. d.argpartition(1, axis=0, kind=k)
  2304. d.argpartition(4, axis=1, kind=k)
  2305. np.argpartition(d, 1, axis=0, kind=k)
  2306. np.argpartition(d, 4, axis=1, kind=k)
  2307. np.argpartition(d, 1, axis=None, kind=k)
  2308. np.argpartition(d, 9, axis=None, kind=k)
  2309. assert_raises(ValueError, d.partition, 2, axis=0)
  2310. assert_raises(ValueError, d.partition, 11, axis=1)
  2311. assert_raises(TypeError, d.partition, 2, axis=None)
  2312. assert_raises(ValueError, np.partition, d, 9, axis=1)
  2313. assert_raises(ValueError, np.partition, d, 11, axis=None)
  2314. assert_raises(ValueError, d.argpartition, 2, axis=0)
  2315. assert_raises(ValueError, d.argpartition, 11, axis=1)
  2316. assert_raises(ValueError, np.argpartition, d, 9, axis=1)
  2317. assert_raises(ValueError, np.argpartition, d, 11, axis=None)
  2318. td = [(dt, s) for dt in [np.int32, np.float32, np.complex64]
  2319. for s in (9, 16)]
  2320. for dt, s in td:
  2321. aae = assert_array_equal
  2322. at = assert_
  2323. d = np.arange(s, dtype=dt)
  2324. np.random.shuffle(d)
  2325. d1 = np.tile(np.arange(s, dtype=dt), (4, 1))
  2326. map(np.random.shuffle, d1)
  2327. d0 = np.transpose(d1)
  2328. for i in range(d.size):
  2329. p = np.partition(d, i, kind=k)
  2330. assert_equal(p[i], i)
  2331. # all before are smaller
  2332. assert_array_less(p[:i], p[i])
  2333. # all after are larger
  2334. assert_array_less(p[i], p[i + 1:])
  2335. aae(p, d[np.argpartition(d, i, kind=k)])
  2336. p = np.partition(d1, i, axis=1, kind=k)
  2337. aae(p[:, i], np.array([i] * d1.shape[0], dtype=dt))
  2338. # array_less does not seem to work right
  2339. at((p[:, :i].T <= p[:, i]).all(),
  2340. msg="%d: %r <= %r" % (i, p[:, i], p[:, :i].T))
  2341. at((p[:, i + 1:].T > p[:, i]).all(),
  2342. msg="%d: %r < %r" % (i, p[:, i], p[:, i + 1:].T))
  2343. aae(p, d1[np.arange(d1.shape[0])[:, None],
  2344. np.argpartition(d1, i, axis=1, kind=k)])
  2345. p = np.partition(d0, i, axis=0, kind=k)
  2346. aae(p[i, :], np.array([i] * d1.shape[0], dtype=dt))
  2347. # array_less does not seem to work right
  2348. at((p[:i, :] <= p[i, :]).all(),
  2349. msg="%d: %r <= %r" % (i, p[i, :], p[:i, :]))
  2350. at((p[i + 1:, :] > p[i, :]).all(),
  2351. msg="%d: %r < %r" % (i, p[i, :], p[:, i + 1:]))
  2352. aae(p, d0[np.argpartition(d0, i, axis=0, kind=k),
  2353. np.arange(d0.shape[1])[None, :]])
  2354. # check inplace
  2355. dc = d.copy()
  2356. dc.partition(i, kind=k)
  2357. assert_equal(dc, np.partition(d, i, kind=k))
  2358. dc = d0.copy()
  2359. dc.partition(i, axis=0, kind=k)
  2360. assert_equal(dc, np.partition(d0, i, axis=0, kind=k))
  2361. dc = d1.copy()
  2362. dc.partition(i, axis=1, kind=k)
  2363. assert_equal(dc, np.partition(d1, i, axis=1, kind=k))
  2364. def assert_partitioned(self, d, kth):
  2365. prev = 0
  2366. for k in np.sort(kth):
  2367. assert_array_less(d[prev:k], d[k], err_msg='kth %d' % k)
  2368. assert_((d[k:] >= d[k]).all(),
  2369. msg="kth %d, %r not greater equal %d" % (k, d[k:], d[k]))
  2370. prev = k + 1
  2371. def test_partition_iterative(self):
  2372. d = np.arange(17)
  2373. kth = (0, 1, 2, 429, 231)
  2374. assert_raises(ValueError, d.partition, kth)
  2375. assert_raises(ValueError, d.argpartition, kth)
  2376. d = np.arange(10).reshape((2, 5))
  2377. assert_raises(ValueError, d.partition, kth, axis=0)
  2378. assert_raises(ValueError, d.partition, kth, axis=1)
  2379. assert_raises(ValueError, np.partition, d, kth, axis=1)
  2380. assert_raises(ValueError, np.partition, d, kth, axis=None)
  2381. d = np.array([3, 4, 2, 1])
  2382. p = np.partition(d, (0, 3))
  2383. self.assert_partitioned(p, (0, 3))
  2384. self.assert_partitioned(d[np.argpartition(d, (0, 3))], (0, 3))
  2385. assert_array_equal(p, np.partition(d, (-3, -1)))
  2386. assert_array_equal(p, d[np.argpartition(d, (-3, -1))])
  2387. d = np.arange(17)
  2388. np.random.shuffle(d)
  2389. d.partition(range(d.size))
  2390. assert_array_equal(np.arange(17), d)
  2391. np.random.shuffle(d)
  2392. assert_array_equal(np.arange(17), d[d.argpartition(range(d.size))])
  2393. # test unsorted kth
  2394. d = np.arange(17)
  2395. np.random.shuffle(d)
  2396. keys = np.array([1, 3, 8, -2])
  2397. np.random.shuffle(d)
  2398. p = np.partition(d, keys)
  2399. self.assert_partitioned(p, keys)
  2400. p = d[np.argpartition(d, keys)]
  2401. self.assert_partitioned(p, keys)
  2402. np.random.shuffle(keys)
  2403. assert_array_equal(np.partition(d, keys), p)
  2404. assert_array_equal(d[np.argpartition(d, keys)], p)
  2405. # equal kth
  2406. d = np.arange(20)[::-1]
  2407. self.assert_partitioned(np.partition(d, [5]*4), [5])
  2408. self.assert_partitioned(np.partition(d, [5]*4 + [6, 13]),
  2409. [5]*4 + [6, 13])
  2410. self.assert_partitioned(d[np.argpartition(d, [5]*4)], [5])
  2411. self.assert_partitioned(d[np.argpartition(d, [5]*4 + [6, 13])],
  2412. [5]*4 + [6, 13])
  2413. d = np.arange(12)
  2414. np.random.shuffle(d)
  2415. d1 = np.tile(np.arange(12), (4, 1))
  2416. map(np.random.shuffle, d1)
  2417. d0 = np.transpose(d1)
  2418. kth = (1, 6, 7, -1)
  2419. p = np.partition(d1, kth, axis=1)
  2420. pa = d1[np.arange(d1.shape[0])[:, None],
  2421. d1.argpartition(kth, axis=1)]
  2422. assert_array_equal(p, pa)
  2423. for i in range(d1.shape[0]):
  2424. self.assert_partitioned(p[i,:], kth)
  2425. p = np.partition(d0, kth, axis=0)
  2426. pa = d0[np.argpartition(d0, kth, axis=0),
  2427. np.arange(d0.shape[1])[None,:]]
  2428. assert_array_equal(p, pa)
  2429. for i in range(d0.shape[1]):
  2430. self.assert_partitioned(p[:, i], kth)
  2431. def test_partition_cdtype(self):
  2432. d = np.array([('Galahad', 1.7, 38), ('Arthur', 1.8, 41),
  2433. ('Lancelot', 1.9, 38)],
  2434. dtype=[('name', '|S10'), ('height', '<f8'), ('age', '<i4')])
  2435. tgt = np.sort(d, order=['age', 'height'])
  2436. assert_array_equal(np.partition(d, range(d.size),
  2437. order=['age', 'height']),
  2438. tgt)
  2439. assert_array_equal(d[np.argpartition(d, range(d.size),
  2440. order=['age', 'height'])],
  2441. tgt)
  2442. for k in range(d.size):
  2443. assert_equal(np.partition(d, k, order=['age', 'height'])[k],
  2444. tgt[k])
  2445. assert_equal(d[np.argpartition(d, k, order=['age', 'height'])][k],
  2446. tgt[k])
  2447. d = np.array(['Galahad', 'Arthur', 'zebra', 'Lancelot'])
  2448. tgt = np.sort(d)
  2449. assert_array_equal(np.partition(d, range(d.size)), tgt)
  2450. for k in range(d.size):
  2451. assert_equal(np.partition(d, k)[k], tgt[k])
  2452. assert_equal(d[np.argpartition(d, k)][k], tgt[k])
  2453. def test_partition_unicode_kind(self):
  2454. d = np.arange(10)
  2455. k = b'\xc3\xa4'.decode("UTF8")
  2456. assert_raises(ValueError, d.partition, 2, kind=k)
  2457. assert_raises(ValueError, d.argpartition, 2, kind=k)
  2458. def test_partition_fuzz(self):
  2459. # a few rounds of random data testing
  2460. for j in range(10, 30):
  2461. for i in range(1, j - 2):
  2462. d = np.arange(j)
  2463. np.random.shuffle(d)
  2464. d = d % np.random.randint(2, 30)
  2465. idx = np.random.randint(d.size)
  2466. kth = [0, idx, i, i + 1]
  2467. tgt = np.sort(d)[kth]
  2468. assert_array_equal(np.partition(d, kth)[kth], tgt,
  2469. err_msg="data: %r\n kth: %r" % (d, kth))
  2470. def test_argpartition_gh5524(self):
  2471. # A test for functionality of argpartition on lists.
  2472. d = [6,7,3,2,9,0]
  2473. p = np.argpartition(d,1)
  2474. self.assert_partitioned(np.array(d)[p],[1])
  2475. def test_flatten(self):
  2476. x0 = np.array([[1, 2, 3], [4, 5, 6]], np.int32)
  2477. x1 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], np.int32)
  2478. y0 = np.array([1, 2, 3, 4, 5, 6], np.int32)
  2479. y0f = np.array([1, 4, 2, 5, 3, 6], np.int32)
  2480. y1 = np.array([1, 2, 3, 4, 5, 6, 7, 8], np.int32)
  2481. y1f = np.array([1, 5, 3, 7, 2, 6, 4, 8], np.int32)
  2482. assert_equal(x0.flatten(), y0)
  2483. assert_equal(x0.flatten('F'), y0f)
  2484. assert_equal(x0.flatten('F'), x0.T.flatten())
  2485. assert_equal(x1.flatten(), y1)
  2486. assert_equal(x1.flatten('F'), y1f)
  2487. assert_equal(x1.flatten('F'), x1.T.flatten())
  2488. @pytest.mark.parametrize('func', (np.dot, np.matmul))
  2489. def test_arr_mult(self, func):
  2490. a = np.array([[1, 0], [0, 1]])
  2491. b = np.array([[0, 1], [1, 0]])
  2492. c = np.array([[9, 1], [1, -9]])
  2493. d = np.arange(24).reshape(4, 6)
  2494. ddt = np.array(
  2495. [[ 55, 145, 235, 325],
  2496. [ 145, 451, 757, 1063],
  2497. [ 235, 757, 1279, 1801],
  2498. [ 325, 1063, 1801, 2539]]
  2499. )
  2500. dtd = np.array(
  2501. [[504, 540, 576, 612, 648, 684],
  2502. [540, 580, 620, 660, 700, 740],
  2503. [576, 620, 664, 708, 752, 796],
  2504. [612, 660, 708, 756, 804, 852],
  2505. [648, 700, 752, 804, 856, 908],
  2506. [684, 740, 796, 852, 908, 964]]
  2507. )
  2508. # gemm vs syrk optimizations
  2509. for et in [np.float32, np.float64, np.complex64, np.complex128]:
  2510. eaf = a.astype(et)
  2511. assert_equal(func(eaf, eaf), eaf)
  2512. assert_equal(func(eaf.T, eaf), eaf)
  2513. assert_equal(func(eaf, eaf.T), eaf)
  2514. assert_equal(func(eaf.T, eaf.T), eaf)
  2515. assert_equal(func(eaf.T.copy(), eaf), eaf)
  2516. assert_equal(func(eaf, eaf.T.copy()), eaf)
  2517. assert_equal(func(eaf.T.copy(), eaf.T.copy()), eaf)
  2518. # syrk validations
  2519. for et in [np.float32, np.float64, np.complex64, np.complex128]:
  2520. eaf = a.astype(et)
  2521. ebf = b.astype(et)
  2522. assert_equal(func(ebf, ebf), eaf)
  2523. assert_equal(func(ebf.T, ebf), eaf)
  2524. assert_equal(func(ebf, ebf.T), eaf)
  2525. assert_equal(func(ebf.T, ebf.T), eaf)
  2526. # syrk - different shape, stride, and view validations
  2527. for et in [np.float32, np.float64, np.complex64, np.complex128]:
  2528. edf = d.astype(et)
  2529. assert_equal(
  2530. func(edf[::-1, :], edf.T),
  2531. func(edf[::-1, :].copy(), edf.T.copy())
  2532. )
  2533. assert_equal(
  2534. func(edf[:, ::-1], edf.T),
  2535. func(edf[:, ::-1].copy(), edf.T.copy())
  2536. )
  2537. assert_equal(
  2538. func(edf, edf[::-1, :].T),
  2539. func(edf, edf[::-1, :].T.copy())
  2540. )
  2541. assert_equal(
  2542. func(edf, edf[:, ::-1].T),
  2543. func(edf, edf[:, ::-1].T.copy())
  2544. )
  2545. assert_equal(
  2546. func(edf[:edf.shape[0] // 2, :], edf[::2, :].T),
  2547. func(edf[:edf.shape[0] // 2, :].copy(), edf[::2, :].T.copy())
  2548. )
  2549. assert_equal(
  2550. func(edf[::2, :], edf[:edf.shape[0] // 2, :].T),
  2551. func(edf[::2, :].copy(), edf[:edf.shape[0] // 2, :].T.copy())
  2552. )
  2553. # syrk - different shape
  2554. for et in [np.float32, np.float64, np.complex64, np.complex128]:
  2555. edf = d.astype(et)
  2556. eddtf = ddt.astype(et)
  2557. edtdf = dtd.astype(et)
  2558. assert_equal(func(edf, edf.T), eddtf)
  2559. assert_equal(func(edf.T, edf), edtdf)
  2560. @pytest.mark.parametrize('func', (np.dot, np.matmul))
  2561. @pytest.mark.parametrize('dtype', 'ifdFD')
  2562. def test_no_dgemv(self, func, dtype):
  2563. # check vector arg for contiguous before gemv
  2564. # gh-12156
  2565. a = np.arange(8.0, dtype=dtype).reshape(2, 4)
  2566. b = np.broadcast_to(1., (4, 1))
  2567. ret1 = func(a, b)
  2568. ret2 = func(a, b.copy())
  2569. assert_equal(ret1, ret2)
  2570. ret1 = func(b.T, a.T)
  2571. ret2 = func(b.T.copy(), a.T)
  2572. assert_equal(ret1, ret2)
  2573. # check for unaligned data
  2574. dt = np.dtype(dtype)
  2575. a = np.zeros(8 * dt.itemsize // 2 + 1, dtype='int16')[1:].view(dtype)
  2576. a = a.reshape(2, 4)
  2577. b = a[0]
  2578. # make sure it is not aligned
  2579. assert_(a.__array_interface__['data'][0] % dt.itemsize != 0)
  2580. ret1 = func(a, b)
  2581. ret2 = func(a.copy(), b.copy())
  2582. assert_equal(ret1, ret2)
  2583. ret1 = func(b.T, a.T)
  2584. ret2 = func(b.T.copy(), a.T.copy())
  2585. assert_equal(ret1, ret2)
  2586. def test_dot(self):
  2587. a = np.array([[1, 0], [0, 1]])
  2588. b = np.array([[0, 1], [1, 0]])
  2589. c = np.array([[9, 1], [1, -9]])
  2590. # function versus methods
  2591. assert_equal(np.dot(a, b), a.dot(b))
  2592. assert_equal(np.dot(np.dot(a, b), c), a.dot(b).dot(c))
  2593. # test passing in an output array
  2594. c = np.zeros_like(a)
  2595. a.dot(b, c)
  2596. assert_equal(c, np.dot(a, b))
  2597. # test keyword args
  2598. c = np.zeros_like(a)
  2599. a.dot(b=b, out=c)
  2600. assert_equal(c, np.dot(a, b))
  2601. def test_dot_type_mismatch(self):
  2602. c = 1.
  2603. A = np.array((1,1), dtype='i,i')
  2604. assert_raises(TypeError, np.dot, c, A)
  2605. assert_raises(TypeError, np.dot, A, c)
  2606. def test_dot_out_mem_overlap(self):
  2607. np.random.seed(1)
  2608. # Test BLAS and non-BLAS code paths, including all dtypes
  2609. # that dot() supports
  2610. dtypes = [np.dtype(code) for code in np.typecodes['All']
  2611. if code not in 'USVM']
  2612. for dtype in dtypes:
  2613. a = np.random.rand(3, 3).astype(dtype)
  2614. # Valid dot() output arrays must be aligned
  2615. b = _aligned_zeros((3, 3), dtype=dtype)
  2616. b[...] = np.random.rand(3, 3)
  2617. y = np.dot(a, b)
  2618. x = np.dot(a, b, out=b)
  2619. assert_equal(x, y, err_msg=repr(dtype))
  2620. # Check invalid output array
  2621. assert_raises(ValueError, np.dot, a, b, out=b[::2])
  2622. assert_raises(ValueError, np.dot, a, b, out=b.T)
  2623. def test_dot_matmul_out(self):
  2624. # gh-9641
  2625. class Sub(np.ndarray):
  2626. pass
  2627. a = np.ones((2, 2)).view(Sub)
  2628. b = np.ones((2, 2)).view(Sub)
  2629. out = np.ones((2, 2))
  2630. # make sure out can be any ndarray (not only subclass of inputs)
  2631. np.dot(a, b, out=out)
  2632. np.matmul(a, b, out=out)
  2633. def test_dot_matmul_inner_array_casting_fails(self):
  2634. class A:
  2635. def __array__(self, *args, **kwargs):
  2636. raise NotImplementedError
  2637. # Don't override the error from calling __array__()
  2638. assert_raises(NotImplementedError, np.dot, A(), A())
  2639. assert_raises(NotImplementedError, np.matmul, A(), A())
  2640. assert_raises(NotImplementedError, np.inner, A(), A())
  2641. def test_matmul_out(self):
  2642. # overlapping memory
  2643. a = np.arange(18).reshape(2, 3, 3)
  2644. b = np.matmul(a, a)
  2645. c = np.matmul(a, a, out=a)
  2646. assert_(c is a)
  2647. assert_equal(c, b)
  2648. a = np.arange(18).reshape(2, 3, 3)
  2649. c = np.matmul(a, a, out=a[::-1, ...])
  2650. assert_(c.base is a.base)
  2651. assert_equal(c, b)
  2652. def test_diagonal(self):
  2653. a = np.arange(12).reshape((3, 4))
  2654. assert_equal(a.diagonal(), [0, 5, 10])
  2655. assert_equal(a.diagonal(0), [0, 5, 10])
  2656. assert_equal(a.diagonal(1), [1, 6, 11])
  2657. assert_equal(a.diagonal(-1), [4, 9])
  2658. assert_raises(np.AxisError, a.diagonal, axis1=0, axis2=5)
  2659. assert_raises(np.AxisError, a.diagonal, axis1=5, axis2=0)
  2660. assert_raises(np.AxisError, a.diagonal, axis1=5, axis2=5)
  2661. assert_raises(ValueError, a.diagonal, axis1=1, axis2=1)
  2662. b = np.arange(8).reshape((2, 2, 2))
  2663. assert_equal(b.diagonal(), [[0, 6], [1, 7]])
  2664. assert_equal(b.diagonal(0), [[0, 6], [1, 7]])
  2665. assert_equal(b.diagonal(1), [[2], [3]])
  2666. assert_equal(b.diagonal(-1), [[4], [5]])
  2667. assert_raises(ValueError, b.diagonal, axis1=0, axis2=0)
  2668. assert_equal(b.diagonal(0, 1, 2), [[0, 3], [4, 7]])
  2669. assert_equal(b.diagonal(0, 0, 1), [[0, 6], [1, 7]])
  2670. assert_equal(b.diagonal(offset=1, axis1=0, axis2=2), [[1], [3]])
  2671. # Order of axis argument doesn't matter:
  2672. assert_equal(b.diagonal(0, 2, 1), [[0, 3], [4, 7]])
  2673. def test_diagonal_view_notwriteable(self):
  2674. a = np.eye(3).diagonal()
  2675. assert_(not a.flags.writeable)
  2676. assert_(not a.flags.owndata)
  2677. a = np.diagonal(np.eye(3))
  2678. assert_(not a.flags.writeable)
  2679. assert_(not a.flags.owndata)
  2680. a = np.diag(np.eye(3))
  2681. assert_(not a.flags.writeable)
  2682. assert_(not a.flags.owndata)
  2683. def test_diagonal_memleak(self):
  2684. # Regression test for a bug that crept in at one point
  2685. a = np.zeros((100, 100))
  2686. if HAS_REFCOUNT:
  2687. assert_(sys.getrefcount(a) < 50)
  2688. for i in range(100):
  2689. a.diagonal()
  2690. if HAS_REFCOUNT:
  2691. assert_(sys.getrefcount(a) < 50)
  2692. def test_size_zero_memleak(self):
  2693. # Regression test for issue 9615
  2694. # Exercises a special-case code path for dot products of length
  2695. # zero in cblasfuncs (making it is specific to floating dtypes).
  2696. a = np.array([], dtype=np.float64)
  2697. x = np.array(2.0)
  2698. for _ in range(100):
  2699. np.dot(a, a, out=x)
  2700. if HAS_REFCOUNT:
  2701. assert_(sys.getrefcount(x) < 50)
  2702. def test_trace(self):
  2703. a = np.arange(12).reshape((3, 4))
  2704. assert_equal(a.trace(), 15)
  2705. assert_equal(a.trace(0), 15)
  2706. assert_equal(a.trace(1), 18)
  2707. assert_equal(a.trace(-1), 13)
  2708. b = np.arange(8).reshape((2, 2, 2))
  2709. assert_equal(b.trace(), [6, 8])
  2710. assert_equal(b.trace(0), [6, 8])
  2711. assert_equal(b.trace(1), [2, 3])
  2712. assert_equal(b.trace(-1), [4, 5])
  2713. assert_equal(b.trace(0, 0, 1), [6, 8])
  2714. assert_equal(b.trace(0, 0, 2), [5, 9])
  2715. assert_equal(b.trace(0, 1, 2), [3, 11])
  2716. assert_equal(b.trace(offset=1, axis1=0, axis2=2), [1, 3])
  2717. out = np.array(1)
  2718. ret = a.trace(out=out)
  2719. assert ret is out
  2720. def test_trace_subclass(self):
  2721. # The class would need to overwrite trace to ensure single-element
  2722. # output also has the right subclass.
  2723. class MyArray(np.ndarray):
  2724. pass
  2725. b = np.arange(8).reshape((2, 2, 2)).view(MyArray)
  2726. t = b.trace()
  2727. assert_(isinstance(t, MyArray))
  2728. def test_put(self):
  2729. icodes = np.typecodes['AllInteger']
  2730. fcodes = np.typecodes['AllFloat']
  2731. for dt in icodes + fcodes + 'O':
  2732. tgt = np.array([0, 1, 0, 3, 0, 5], dtype=dt)
  2733. # test 1-d
  2734. a = np.zeros(6, dtype=dt)
  2735. a.put([1, 3, 5], [1, 3, 5])
  2736. assert_equal(a, tgt)
  2737. # test 2-d
  2738. a = np.zeros((2, 3), dtype=dt)
  2739. a.put([1, 3, 5], [1, 3, 5])
  2740. assert_equal(a, tgt.reshape(2, 3))
  2741. for dt in '?':
  2742. tgt = np.array([False, True, False, True, False, True], dtype=dt)
  2743. # test 1-d
  2744. a = np.zeros(6, dtype=dt)
  2745. a.put([1, 3, 5], [True]*3)
  2746. assert_equal(a, tgt)
  2747. # test 2-d
  2748. a = np.zeros((2, 3), dtype=dt)
  2749. a.put([1, 3, 5], [True]*3)
  2750. assert_equal(a, tgt.reshape(2, 3))
  2751. # check must be writeable
  2752. a = np.zeros(6)
  2753. a.flags.writeable = False
  2754. assert_raises(ValueError, a.put, [1, 3, 5], [1, 3, 5])
  2755. # when calling np.put, make sure a
  2756. # TypeError is raised if the object
  2757. # isn't an ndarray
  2758. bad_array = [1, 2, 3]
  2759. assert_raises(TypeError, np.put, bad_array, [0, 2], 5)
  2760. def test_ravel(self):
  2761. a = np.array([[0, 1], [2, 3]])
  2762. assert_equal(a.ravel(), [0, 1, 2, 3])
  2763. assert_(not a.ravel().flags.owndata)
  2764. assert_equal(a.ravel('F'), [0, 2, 1, 3])
  2765. assert_equal(a.ravel(order='C'), [0, 1, 2, 3])
  2766. assert_equal(a.ravel(order='F'), [0, 2, 1, 3])
  2767. assert_equal(a.ravel(order='A'), [0, 1, 2, 3])
  2768. assert_(not a.ravel(order='A').flags.owndata)
  2769. assert_equal(a.ravel(order='K'), [0, 1, 2, 3])
  2770. assert_(not a.ravel(order='K').flags.owndata)
  2771. assert_equal(a.ravel(), a.reshape(-1))
  2772. a = np.array([[0, 1], [2, 3]], order='F')
  2773. assert_equal(a.ravel(), [0, 1, 2, 3])
  2774. assert_equal(a.ravel(order='A'), [0, 2, 1, 3])
  2775. assert_equal(a.ravel(order='K'), [0, 2, 1, 3])
  2776. assert_(not a.ravel(order='A').flags.owndata)
  2777. assert_(not a.ravel(order='K').flags.owndata)
  2778. assert_equal(a.ravel(), a.reshape(-1))
  2779. assert_equal(a.ravel(order='A'), a.reshape(-1, order='A'))
  2780. a = np.array([[0, 1], [2, 3]])[::-1, :]
  2781. assert_equal(a.ravel(), [2, 3, 0, 1])
  2782. assert_equal(a.ravel(order='C'), [2, 3, 0, 1])
  2783. assert_equal(a.ravel(order='F'), [2, 0, 3, 1])
  2784. assert_equal(a.ravel(order='A'), [2, 3, 0, 1])
  2785. # 'K' doesn't reverse the axes of negative strides
  2786. assert_equal(a.ravel(order='K'), [2, 3, 0, 1])
  2787. assert_(a.ravel(order='K').flags.owndata)
  2788. # Test simple 1-d copy behaviour:
  2789. a = np.arange(10)[::2]
  2790. assert_(a.ravel('K').flags.owndata)
  2791. assert_(a.ravel('C').flags.owndata)
  2792. assert_(a.ravel('F').flags.owndata)
  2793. # Not contiguous and 1-sized axis with non matching stride
  2794. a = np.arange(2**3 * 2)[::2]
  2795. a = a.reshape(2, 1, 2, 2).swapaxes(-1, -2)
  2796. strides = list(a.strides)
  2797. strides[1] = 123
  2798. a.strides = strides
  2799. assert_(a.ravel(order='K').flags.owndata)
  2800. assert_equal(a.ravel('K'), np.arange(0, 15, 2))
  2801. # contiguous and 1-sized axis with non matching stride works:
  2802. a = np.arange(2**3)
  2803. a = a.reshape(2, 1, 2, 2).swapaxes(-1, -2)
  2804. strides = list(a.strides)
  2805. strides[1] = 123
  2806. a.strides = strides
  2807. assert_(np.may_share_memory(a.ravel(order='K'), a))
  2808. assert_equal(a.ravel(order='K'), np.arange(2**3))
  2809. # Test negative strides (not very interesting since non-contiguous):
  2810. a = np.arange(4)[::-1].reshape(2, 2)
  2811. assert_(a.ravel(order='C').flags.owndata)
  2812. assert_(a.ravel(order='K').flags.owndata)
  2813. assert_equal(a.ravel('C'), [3, 2, 1, 0])
  2814. assert_equal(a.ravel('K'), [3, 2, 1, 0])
  2815. # 1-element tidy strides test (NPY_RELAXED_STRIDES_CHECKING):
  2816. a = np.array([[1]])
  2817. a.strides = (123, 432)
  2818. # If the stride is not 8, NPY_RELAXED_STRIDES_CHECKING is messing
  2819. # them up on purpose:
  2820. if np.ones(1).strides == (8,):
  2821. assert_(np.may_share_memory(a.ravel('K'), a))
  2822. assert_equal(a.ravel('K').strides, (a.dtype.itemsize,))
  2823. for order in ('C', 'F', 'A', 'K'):
  2824. # 0-d corner case:
  2825. a = np.array(0)
  2826. assert_equal(a.ravel(order), [0])
  2827. assert_(np.may_share_memory(a.ravel(order), a))
  2828. # Test that certain non-inplace ravels work right (mostly) for 'K':
  2829. b = np.arange(2**4 * 2)[::2].reshape(2, 2, 2, 2)
  2830. a = b[..., ::2]
  2831. assert_equal(a.ravel('K'), [0, 4, 8, 12, 16, 20, 24, 28])
  2832. assert_equal(a.ravel('C'), [0, 4, 8, 12, 16, 20, 24, 28])
  2833. assert_equal(a.ravel('A'), [0, 4, 8, 12, 16, 20, 24, 28])
  2834. assert_equal(a.ravel('F'), [0, 16, 8, 24, 4, 20, 12, 28])
  2835. a = b[::2, ...]
  2836. assert_equal(a.ravel('K'), [0, 2, 4, 6, 8, 10, 12, 14])
  2837. assert_equal(a.ravel('C'), [0, 2, 4, 6, 8, 10, 12, 14])
  2838. assert_equal(a.ravel('A'), [0, 2, 4, 6, 8, 10, 12, 14])
  2839. assert_equal(a.ravel('F'), [0, 8, 4, 12, 2, 10, 6, 14])
  2840. def test_ravel_subclass(self):
  2841. class ArraySubclass(np.ndarray):
  2842. pass
  2843. a = np.arange(10).view(ArraySubclass)
  2844. assert_(isinstance(a.ravel('C'), ArraySubclass))
  2845. assert_(isinstance(a.ravel('F'), ArraySubclass))
  2846. assert_(isinstance(a.ravel('A'), ArraySubclass))
  2847. assert_(isinstance(a.ravel('K'), ArraySubclass))
  2848. a = np.arange(10)[::2].view(ArraySubclass)
  2849. assert_(isinstance(a.ravel('C'), ArraySubclass))
  2850. assert_(isinstance(a.ravel('F'), ArraySubclass))
  2851. assert_(isinstance(a.ravel('A'), ArraySubclass))
  2852. assert_(isinstance(a.ravel('K'), ArraySubclass))
  2853. def test_swapaxes(self):
  2854. a = np.arange(1*2*3*4).reshape(1, 2, 3, 4).copy()
  2855. idx = np.indices(a.shape)
  2856. assert_(a.flags['OWNDATA'])
  2857. b = a.copy()
  2858. # check exceptions
  2859. assert_raises(np.AxisError, a.swapaxes, -5, 0)
  2860. assert_raises(np.AxisError, a.swapaxes, 4, 0)
  2861. assert_raises(np.AxisError, a.swapaxes, 0, -5)
  2862. assert_raises(np.AxisError, a.swapaxes, 0, 4)
  2863. for i in range(-4, 4):
  2864. for j in range(-4, 4):
  2865. for k, src in enumerate((a, b)):
  2866. c = src.swapaxes(i, j)
  2867. # check shape
  2868. shape = list(src.shape)
  2869. shape[i] = src.shape[j]
  2870. shape[j] = src.shape[i]
  2871. assert_equal(c.shape, shape, str((i, j, k)))
  2872. # check array contents
  2873. i0, i1, i2, i3 = [dim-1 for dim in c.shape]
  2874. j0, j1, j2, j3 = [dim-1 for dim in src.shape]
  2875. assert_equal(src[idx[j0], idx[j1], idx[j2], idx[j3]],
  2876. c[idx[i0], idx[i1], idx[i2], idx[i3]],
  2877. str((i, j, k)))
  2878. # check a view is always returned, gh-5260
  2879. assert_(not c.flags['OWNDATA'], str((i, j, k)))
  2880. # check on non-contiguous input array
  2881. if k == 1:
  2882. b = c
  2883. def test_conjugate(self):
  2884. a = np.array([1-1j, 1+1j, 23+23.0j])
  2885. ac = a.conj()
  2886. assert_equal(a.real, ac.real)
  2887. assert_equal(a.imag, -ac.imag)
  2888. assert_equal(ac, a.conjugate())
  2889. assert_equal(ac, np.conjugate(a))
  2890. a = np.array([1-1j, 1+1j, 23+23.0j], 'F')
  2891. ac = a.conj()
  2892. assert_equal(a.real, ac.real)
  2893. assert_equal(a.imag, -ac.imag)
  2894. assert_equal(ac, a.conjugate())
  2895. assert_equal(ac, np.conjugate(a))
  2896. a = np.array([1, 2, 3])
  2897. ac = a.conj()
  2898. assert_equal(a, ac)
  2899. assert_equal(ac, a.conjugate())
  2900. assert_equal(ac, np.conjugate(a))
  2901. a = np.array([1.0, 2.0, 3.0])
  2902. ac = a.conj()
  2903. assert_equal(a, ac)
  2904. assert_equal(ac, a.conjugate())
  2905. assert_equal(ac, np.conjugate(a))
  2906. a = np.array([1-1j, 1+1j, 1, 2.0], object)
  2907. ac = a.conj()
  2908. assert_equal(ac, [k.conjugate() for k in a])
  2909. assert_equal(ac, a.conjugate())
  2910. assert_equal(ac, np.conjugate(a))
  2911. a = np.array([1-1j, 1, 2.0, 'f'], object)
  2912. assert_raises(TypeError, lambda: a.conj())
  2913. assert_raises(TypeError, lambda: a.conjugate())
  2914. def test__complex__(self):
  2915. dtypes = ['i1', 'i2', 'i4', 'i8',
  2916. 'u1', 'u2', 'u4', 'u8',
  2917. 'f', 'd', 'g', 'F', 'D', 'G',
  2918. '?', 'O']
  2919. for dt in dtypes:
  2920. a = np.array(7, dtype=dt)
  2921. b = np.array([7], dtype=dt)
  2922. c = np.array([[[[[7]]]]], dtype=dt)
  2923. msg = 'dtype: {0}'.format(dt)
  2924. ap = complex(a)
  2925. assert_equal(ap, a, msg)
  2926. bp = complex(b)
  2927. assert_equal(bp, b, msg)
  2928. cp = complex(c)
  2929. assert_equal(cp, c, msg)
  2930. def test__complex__should_not_work(self):
  2931. dtypes = ['i1', 'i2', 'i4', 'i8',
  2932. 'u1', 'u2', 'u4', 'u8',
  2933. 'f', 'd', 'g', 'F', 'D', 'G',
  2934. '?', 'O']
  2935. for dt in dtypes:
  2936. a = np.array([1, 2, 3], dtype=dt)
  2937. assert_raises(TypeError, complex, a)
  2938. dt = np.dtype([('a', 'f8'), ('b', 'i1')])
  2939. b = np.array((1.0, 3), dtype=dt)
  2940. assert_raises(TypeError, complex, b)
  2941. c = np.array([(1.0, 3), (2e-3, 7)], dtype=dt)
  2942. assert_raises(TypeError, complex, c)
  2943. d = np.array('1+1j')
  2944. assert_raises(TypeError, complex, d)
  2945. e = np.array(['1+1j'], 'U')
  2946. assert_raises(TypeError, complex, e)
  2947. class TestCequenceMethods:
  2948. def test_array_contains(self):
  2949. assert_(4.0 in np.arange(16.).reshape(4,4))
  2950. assert_(20.0 not in np.arange(16.).reshape(4,4))
  2951. class TestBinop:
  2952. def test_inplace(self):
  2953. # test refcount 1 inplace conversion
  2954. assert_array_almost_equal(np.array([0.5]) * np.array([1.0, 2.0]),
  2955. [0.5, 1.0])
  2956. d = np.array([0.5, 0.5])[::2]
  2957. assert_array_almost_equal(d * (d * np.array([1.0, 2.0])),
  2958. [0.25, 0.5])
  2959. a = np.array([0.5])
  2960. b = np.array([0.5])
  2961. c = a + b
  2962. c = a - b
  2963. c = a * b
  2964. c = a / b
  2965. assert_equal(a, b)
  2966. assert_almost_equal(c, 1.)
  2967. c = a + b * 2. / b * a - a / b
  2968. assert_equal(a, b)
  2969. assert_equal(c, 0.5)
  2970. # true divide
  2971. a = np.array([5])
  2972. b = np.array([3])
  2973. c = (a * a) / b
  2974. assert_almost_equal(c, 25 / 3)
  2975. assert_equal(a, 5)
  2976. assert_equal(b, 3)
  2977. # ndarray.__rop__ always calls ufunc
  2978. # ndarray.__iop__ always calls ufunc
  2979. # ndarray.__op__, __rop__:
  2980. # - defer if other has __array_ufunc__ and it is None
  2981. # or other is not a subclass and has higher array priority
  2982. # - else, call ufunc
  2983. def test_ufunc_binop_interaction(self):
  2984. # Python method name (without underscores)
  2985. # -> (numpy ufunc, has_in_place_version, preferred_dtype)
  2986. ops = {
  2987. 'add': (np.add, True, float),
  2988. 'sub': (np.subtract, True, float),
  2989. 'mul': (np.multiply, True, float),
  2990. 'truediv': (np.true_divide, True, float),
  2991. 'floordiv': (np.floor_divide, True, float),
  2992. 'mod': (np.remainder, True, float),
  2993. 'divmod': (np.divmod, False, float),
  2994. 'pow': (np.power, True, int),
  2995. 'lshift': (np.left_shift, True, int),
  2996. 'rshift': (np.right_shift, True, int),
  2997. 'and': (np.bitwise_and, True, int),
  2998. 'xor': (np.bitwise_xor, True, int),
  2999. 'or': (np.bitwise_or, True, int),
  3000. 'matmul': (np.matmul, False, float),
  3001. # 'ge': (np.less_equal, False),
  3002. # 'gt': (np.less, False),
  3003. # 'le': (np.greater_equal, False),
  3004. # 'lt': (np.greater, False),
  3005. # 'eq': (np.equal, False),
  3006. # 'ne': (np.not_equal, False),
  3007. }
  3008. class Coerced(Exception):
  3009. pass
  3010. def array_impl(self):
  3011. raise Coerced
  3012. def op_impl(self, other):
  3013. return "forward"
  3014. def rop_impl(self, other):
  3015. return "reverse"
  3016. def iop_impl(self, other):
  3017. return "in-place"
  3018. def array_ufunc_impl(self, ufunc, method, *args, **kwargs):
  3019. return ("__array_ufunc__", ufunc, method, args, kwargs)
  3020. # Create an object with the given base, in the given module, with a
  3021. # bunch of placeholder __op__ methods, and optionally a
  3022. # __array_ufunc__ and __array_priority__.
  3023. def make_obj(base, array_priority=False, array_ufunc=False,
  3024. alleged_module="__main__"):
  3025. class_namespace = {"__array__": array_impl}
  3026. if array_priority is not False:
  3027. class_namespace["__array_priority__"] = array_priority
  3028. for op in ops:
  3029. class_namespace["__{0}__".format(op)] = op_impl
  3030. class_namespace["__r{0}__".format(op)] = rop_impl
  3031. class_namespace["__i{0}__".format(op)] = iop_impl
  3032. if array_ufunc is not False:
  3033. class_namespace["__array_ufunc__"] = array_ufunc
  3034. eval_namespace = {"base": base,
  3035. "class_namespace": class_namespace,
  3036. "__name__": alleged_module,
  3037. }
  3038. MyType = eval("type('MyType', (base,), class_namespace)",
  3039. eval_namespace)
  3040. if issubclass(MyType, np.ndarray):
  3041. # Use this range to avoid special case weirdnesses around
  3042. # divide-by-0, pow(x, 2), overflow due to pow(big, big), etc.
  3043. return np.arange(3, 7).reshape(2, 2).view(MyType)
  3044. else:
  3045. return MyType()
  3046. def check(obj, binop_override_expected, ufunc_override_expected,
  3047. inplace_override_expected, check_scalar=True):
  3048. for op, (ufunc, has_inplace, dtype) in ops.items():
  3049. err_msg = ('op: %s, ufunc: %s, has_inplace: %s, dtype: %s'
  3050. % (op, ufunc, has_inplace, dtype))
  3051. check_objs = [np.arange(3, 7, dtype=dtype).reshape(2, 2)]
  3052. if check_scalar:
  3053. check_objs.append(check_objs[0][0])
  3054. for arr in check_objs:
  3055. arr_method = getattr(arr, "__{0}__".format(op))
  3056. def first_out_arg(result):
  3057. if op == "divmod":
  3058. assert_(isinstance(result, tuple))
  3059. return result[0]
  3060. else:
  3061. return result
  3062. # arr __op__ obj
  3063. if binop_override_expected:
  3064. assert_equal(arr_method(obj), NotImplemented, err_msg)
  3065. elif ufunc_override_expected:
  3066. assert_equal(arr_method(obj)[0], "__array_ufunc__",
  3067. err_msg)
  3068. else:
  3069. if (isinstance(obj, np.ndarray) and
  3070. (type(obj).__array_ufunc__ is
  3071. np.ndarray.__array_ufunc__)):
  3072. # __array__ gets ignored
  3073. res = first_out_arg(arr_method(obj))
  3074. assert_(res.__class__ is obj.__class__, err_msg)
  3075. else:
  3076. assert_raises((TypeError, Coerced),
  3077. arr_method, obj, err_msg=err_msg)
  3078. # obj __op__ arr
  3079. arr_rmethod = getattr(arr, "__r{0}__".format(op))
  3080. if ufunc_override_expected:
  3081. res = arr_rmethod(obj)
  3082. assert_equal(res[0], "__array_ufunc__",
  3083. err_msg=err_msg)
  3084. assert_equal(res[1], ufunc, err_msg=err_msg)
  3085. else:
  3086. if (isinstance(obj, np.ndarray) and
  3087. (type(obj).__array_ufunc__ is
  3088. np.ndarray.__array_ufunc__)):
  3089. # __array__ gets ignored
  3090. res = first_out_arg(arr_rmethod(obj))
  3091. assert_(res.__class__ is obj.__class__, err_msg)
  3092. else:
  3093. # __array_ufunc__ = "asdf" creates a TypeError
  3094. assert_raises((TypeError, Coerced),
  3095. arr_rmethod, obj, err_msg=err_msg)
  3096. # arr __iop__ obj
  3097. # array scalars don't have in-place operators
  3098. if has_inplace and isinstance(arr, np.ndarray):
  3099. arr_imethod = getattr(arr, "__i{0}__".format(op))
  3100. if inplace_override_expected:
  3101. assert_equal(arr_method(obj), NotImplemented,
  3102. err_msg=err_msg)
  3103. elif ufunc_override_expected:
  3104. res = arr_imethod(obj)
  3105. assert_equal(res[0], "__array_ufunc__", err_msg)
  3106. assert_equal(res[1], ufunc, err_msg)
  3107. assert_(type(res[-1]["out"]) is tuple, err_msg)
  3108. assert_(res[-1]["out"][0] is arr, err_msg)
  3109. else:
  3110. if (isinstance(obj, np.ndarray) and
  3111. (type(obj).__array_ufunc__ is
  3112. np.ndarray.__array_ufunc__)):
  3113. # __array__ gets ignored
  3114. assert_(arr_imethod(obj) is arr, err_msg)
  3115. else:
  3116. assert_raises((TypeError, Coerced),
  3117. arr_imethod, obj,
  3118. err_msg=err_msg)
  3119. op_fn = getattr(operator, op, None)
  3120. if op_fn is None:
  3121. op_fn = getattr(operator, op + "_", None)
  3122. if op_fn is None:
  3123. op_fn = getattr(builtins, op)
  3124. assert_equal(op_fn(obj, arr), "forward", err_msg)
  3125. if not isinstance(obj, np.ndarray):
  3126. if binop_override_expected:
  3127. assert_equal(op_fn(arr, obj), "reverse", err_msg)
  3128. elif ufunc_override_expected:
  3129. assert_equal(op_fn(arr, obj)[0], "__array_ufunc__",
  3130. err_msg)
  3131. if ufunc_override_expected:
  3132. assert_equal(ufunc(obj, arr)[0], "__array_ufunc__",
  3133. err_msg)
  3134. # No array priority, no array_ufunc -> nothing called
  3135. check(make_obj(object), False, False, False)
  3136. # Negative array priority, no array_ufunc -> nothing called
  3137. # (has to be very negative, because scalar priority is -1000000.0)
  3138. check(make_obj(object, array_priority=-2**30), False, False, False)
  3139. # Positive array priority, no array_ufunc -> binops and iops only
  3140. check(make_obj(object, array_priority=1), True, False, True)
  3141. # ndarray ignores array_priority for ndarray subclasses
  3142. check(make_obj(np.ndarray, array_priority=1), False, False, False,
  3143. check_scalar=False)
  3144. # Positive array_priority and array_ufunc -> array_ufunc only
  3145. check(make_obj(object, array_priority=1,
  3146. array_ufunc=array_ufunc_impl), False, True, False)
  3147. check(make_obj(np.ndarray, array_priority=1,
  3148. array_ufunc=array_ufunc_impl), False, True, False)
  3149. # array_ufunc set to None -> defer binops only
  3150. check(make_obj(object, array_ufunc=None), True, False, False)
  3151. check(make_obj(np.ndarray, array_ufunc=None), True, False, False,
  3152. check_scalar=False)
  3153. def test_ufunc_override_normalize_signature(self):
  3154. # gh-5674
  3155. class SomeClass:
  3156. def __array_ufunc__(self, ufunc, method, *inputs, **kw):
  3157. return kw
  3158. a = SomeClass()
  3159. kw = np.add(a, [1])
  3160. assert_('sig' not in kw and 'signature' not in kw)
  3161. kw = np.add(a, [1], sig='ii->i')
  3162. assert_('sig' not in kw and 'signature' in kw)
  3163. assert_equal(kw['signature'], 'ii->i')
  3164. kw = np.add(a, [1], signature='ii->i')
  3165. assert_('sig' not in kw and 'signature' in kw)
  3166. assert_equal(kw['signature'], 'ii->i')
  3167. def test_array_ufunc_index(self):
  3168. # Check that index is set appropriately, also if only an output
  3169. # is passed on (latter is another regression tests for github bug 4753)
  3170. # This also checks implicitly that 'out' is always a tuple.
  3171. class CheckIndex:
  3172. def __array_ufunc__(self, ufunc, method, *inputs, **kw):
  3173. for i, a in enumerate(inputs):
  3174. if a is self:
  3175. return i
  3176. # calls below mean we must be in an output.
  3177. for j, a in enumerate(kw['out']):
  3178. if a is self:
  3179. return (j,)
  3180. a = CheckIndex()
  3181. dummy = np.arange(2.)
  3182. # 1 input, 1 output
  3183. assert_equal(np.sin(a), 0)
  3184. assert_equal(np.sin(dummy, a), (0,))
  3185. assert_equal(np.sin(dummy, out=a), (0,))
  3186. assert_equal(np.sin(dummy, out=(a,)), (0,))
  3187. assert_equal(np.sin(a, a), 0)
  3188. assert_equal(np.sin(a, out=a), 0)
  3189. assert_equal(np.sin(a, out=(a,)), 0)
  3190. # 1 input, 2 outputs
  3191. assert_equal(np.modf(dummy, a), (0,))
  3192. assert_equal(np.modf(dummy, None, a), (1,))
  3193. assert_equal(np.modf(dummy, dummy, a), (1,))
  3194. assert_equal(np.modf(dummy, out=(a, None)), (0,))
  3195. assert_equal(np.modf(dummy, out=(a, dummy)), (0,))
  3196. assert_equal(np.modf(dummy, out=(None, a)), (1,))
  3197. assert_equal(np.modf(dummy, out=(dummy, a)), (1,))
  3198. assert_equal(np.modf(a, out=(dummy, a)), 0)
  3199. with assert_raises(TypeError):
  3200. # Out argument must be tuple, since there are multiple outputs
  3201. np.modf(dummy, out=a)
  3202. assert_raises(ValueError, np.modf, dummy, out=(a,))
  3203. # 2 inputs, 1 output
  3204. assert_equal(np.add(a, dummy), 0)
  3205. assert_equal(np.add(dummy, a), 1)
  3206. assert_equal(np.add(dummy, dummy, a), (0,))
  3207. assert_equal(np.add(dummy, a, a), 1)
  3208. assert_equal(np.add(dummy, dummy, out=a), (0,))
  3209. assert_equal(np.add(dummy, dummy, out=(a,)), (0,))
  3210. assert_equal(np.add(a, dummy, out=a), 0)
  3211. def test_out_override(self):
  3212. # regression test for github bug 4753
  3213. class OutClass(np.ndarray):
  3214. def __array_ufunc__(self, ufunc, method, *inputs, **kw):
  3215. if 'out' in kw:
  3216. tmp_kw = kw.copy()
  3217. tmp_kw.pop('out')
  3218. func = getattr(ufunc, method)
  3219. kw['out'][0][...] = func(*inputs, **tmp_kw)
  3220. A = np.array([0]).view(OutClass)
  3221. B = np.array([5])
  3222. C = np.array([6])
  3223. np.multiply(C, B, A)
  3224. assert_equal(A[0], 30)
  3225. assert_(isinstance(A, OutClass))
  3226. A[0] = 0
  3227. np.multiply(C, B, out=A)
  3228. assert_equal(A[0], 30)
  3229. assert_(isinstance(A, OutClass))
  3230. def test_pow_override_with_errors(self):
  3231. # regression test for gh-9112
  3232. class PowerOnly(np.ndarray):
  3233. def __array_ufunc__(self, ufunc, method, *inputs, **kw):
  3234. if ufunc is not np.power:
  3235. raise NotImplementedError
  3236. return "POWER!"
  3237. # explicit cast to float, to ensure the fast power path is taken.
  3238. a = np.array(5., dtype=np.float64).view(PowerOnly)
  3239. assert_equal(a ** 2.5, "POWER!")
  3240. with assert_raises(NotImplementedError):
  3241. a ** 0.5
  3242. with assert_raises(NotImplementedError):
  3243. a ** 0
  3244. with assert_raises(NotImplementedError):
  3245. a ** 1
  3246. with assert_raises(NotImplementedError):
  3247. a ** -1
  3248. with assert_raises(NotImplementedError):
  3249. a ** 2
  3250. def test_pow_array_object_dtype(self):
  3251. # test pow on arrays of object dtype
  3252. class SomeClass:
  3253. def __init__(self, num=None):
  3254. self.num = num
  3255. # want to ensure a fast pow path is not taken
  3256. def __mul__(self, other):
  3257. raise AssertionError('__mul__ should not be called')
  3258. def __div__(self, other):
  3259. raise AssertionError('__div__ should not be called')
  3260. def __pow__(self, exp):
  3261. return SomeClass(num=self.num ** exp)
  3262. def __eq__(self, other):
  3263. if isinstance(other, SomeClass):
  3264. return self.num == other.num
  3265. __rpow__ = __pow__
  3266. def pow_for(exp, arr):
  3267. return np.array([x ** exp for x in arr])
  3268. obj_arr = np.array([SomeClass(1), SomeClass(2), SomeClass(3)])
  3269. assert_equal(obj_arr ** 0.5, pow_for(0.5, obj_arr))
  3270. assert_equal(obj_arr ** 0, pow_for(0, obj_arr))
  3271. assert_equal(obj_arr ** 1, pow_for(1, obj_arr))
  3272. assert_equal(obj_arr ** -1, pow_for(-1, obj_arr))
  3273. assert_equal(obj_arr ** 2, pow_for(2, obj_arr))
  3274. def test_pos_array_ufunc_override(self):
  3275. class A(np.ndarray):
  3276. def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
  3277. return getattr(ufunc, method)(*[i.view(np.ndarray) for
  3278. i in inputs], **kwargs)
  3279. tst = np.array('foo').view(A)
  3280. with assert_raises(TypeError):
  3281. +tst
  3282. class TestTemporaryElide:
  3283. # elision is only triggered on relatively large arrays
  3284. def test_extension_incref_elide(self):
  3285. # test extension (e.g. cython) calling PyNumber_* slots without
  3286. # increasing the reference counts
  3287. #
  3288. # def incref_elide(a):
  3289. # d = input.copy() # refcount 1
  3290. # return d, d + d # PyNumber_Add without increasing refcount
  3291. from numpy.core._multiarray_tests import incref_elide
  3292. d = np.ones(100000)
  3293. orig, res = incref_elide(d)
  3294. d + d
  3295. # the return original should not be changed to an inplace operation
  3296. assert_array_equal(orig, d)
  3297. assert_array_equal(res, d + d)
  3298. def test_extension_incref_elide_stack(self):
  3299. # scanning if the refcount == 1 object is on the python stack to check
  3300. # that we are called directly from python is flawed as object may still
  3301. # be above the stack pointer and we have no access to the top of it
  3302. #
  3303. # def incref_elide_l(d):
  3304. # return l[4] + l[4] # PyNumber_Add without increasing refcount
  3305. from numpy.core._multiarray_tests import incref_elide_l
  3306. # padding with 1 makes sure the object on the stack is not overwritten
  3307. l = [1, 1, 1, 1, np.ones(100000)]
  3308. res = incref_elide_l(l)
  3309. # the return original should not be changed to an inplace operation
  3310. assert_array_equal(l[4], np.ones(100000))
  3311. assert_array_equal(res, l[4] + l[4])
  3312. def test_temporary_with_cast(self):
  3313. # check that we don't elide into a temporary which would need casting
  3314. d = np.ones(200000, dtype=np.int64)
  3315. assert_equal(((d + d) + 2**222).dtype, np.dtype('O'))
  3316. r = ((d + d) / 2)
  3317. assert_equal(r.dtype, np.dtype('f8'))
  3318. r = np.true_divide((d + d), 2)
  3319. assert_equal(r.dtype, np.dtype('f8'))
  3320. r = ((d + d) / 2.)
  3321. assert_equal(r.dtype, np.dtype('f8'))
  3322. r = ((d + d) // 2)
  3323. assert_equal(r.dtype, np.dtype(np.int64))
  3324. # commutative elision into the astype result
  3325. f = np.ones(100000, dtype=np.float32)
  3326. assert_equal(((f + f) + f.astype(np.float64)).dtype, np.dtype('f8'))
  3327. # no elision into lower type
  3328. d = f.astype(np.float64)
  3329. assert_equal(((f + f) + d).dtype, d.dtype)
  3330. l = np.ones(100000, dtype=np.longdouble)
  3331. assert_equal(((d + d) + l).dtype, l.dtype)
  3332. # test unary abs with different output dtype
  3333. for dt in (np.complex64, np.complex128, np.clongdouble):
  3334. c = np.ones(100000, dtype=dt)
  3335. r = abs(c * 2.0)
  3336. assert_equal(r.dtype, np.dtype('f%d' % (c.itemsize // 2)))
  3337. def test_elide_broadcast(self):
  3338. # test no elision on broadcast to higher dimension
  3339. # only triggers elision code path in debug mode as triggering it in
  3340. # normal mode needs 256kb large matching dimension, so a lot of memory
  3341. d = np.ones((2000, 1), dtype=int)
  3342. b = np.ones((2000), dtype=bool)
  3343. r = (1 - d) + b
  3344. assert_equal(r, 1)
  3345. assert_equal(r.shape, (2000, 2000))
  3346. def test_elide_scalar(self):
  3347. # check inplace op does not create ndarray from scalars
  3348. a = np.bool_()
  3349. assert_(type(~(a & a)) is np.bool_)
  3350. def test_elide_scalar_readonly(self):
  3351. # The imaginary part of a real array is readonly. This needs to go
  3352. # through fast_scalar_power which is only called for powers of
  3353. # +1, -1, 0, 0.5, and 2, so use 2. Also need valid refcount for
  3354. # elision which can be gotten for the imaginary part of a real
  3355. # array. Should not error.
  3356. a = np.empty(100000, dtype=np.float64)
  3357. a.imag ** 2
  3358. def test_elide_readonly(self):
  3359. # don't try to elide readonly temporaries
  3360. r = np.asarray(np.broadcast_to(np.zeros(1), 100000).flat) * 0.0
  3361. assert_equal(r, 0)
  3362. def test_elide_updateifcopy(self):
  3363. a = np.ones(2**20)[::2]
  3364. b = a.flat.__array__() + 1
  3365. del b
  3366. assert_equal(a, 1)
  3367. class TestCAPI:
  3368. def test_IsPythonScalar(self):
  3369. from numpy.core._multiarray_tests import IsPythonScalar
  3370. assert_(IsPythonScalar(b'foobar'))
  3371. assert_(IsPythonScalar(1))
  3372. assert_(IsPythonScalar(2**80))
  3373. assert_(IsPythonScalar(2.))
  3374. assert_(IsPythonScalar("a"))
  3375. class TestSubscripting:
  3376. def test_test_zero_rank(self):
  3377. x = np.array([1, 2, 3])
  3378. assert_(isinstance(x[0], np.int_))
  3379. assert_(type(x[0, ...]) is np.ndarray)
  3380. class TestPickling:
  3381. @pytest.mark.skipif(pickle.HIGHEST_PROTOCOL >= 5,
  3382. reason=('this tests the error messages when trying to'
  3383. 'protocol 5 although it is not available'))
  3384. def test_correct_protocol5_error_message(self):
  3385. array = np.arange(10)
  3386. if sys.version_info[:2] in ((3, 6), (3, 7)):
  3387. # For the specific case of python3.6 and 3.7, raise a clear import
  3388. # error about the pickle5 backport when trying to use protocol=5
  3389. # without the pickle5 package
  3390. with pytest.raises(ImportError):
  3391. array.__reduce_ex__(5)
  3392. def test_record_array_with_object_dtype(self):
  3393. my_object = object()
  3394. arr_with_object = np.array(
  3395. [(my_object, 1, 2.0)],
  3396. dtype=[('a', object), ('b', int), ('c', float)])
  3397. arr_without_object = np.array(
  3398. [('xxx', 1, 2.0)],
  3399. dtype=[('a', str), ('b', int), ('c', float)])
  3400. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  3401. depickled_arr_with_object = pickle.loads(
  3402. pickle.dumps(arr_with_object, protocol=proto))
  3403. depickled_arr_without_object = pickle.loads(
  3404. pickle.dumps(arr_without_object, protocol=proto))
  3405. assert_equal(arr_with_object.dtype,
  3406. depickled_arr_with_object.dtype)
  3407. assert_equal(arr_without_object.dtype,
  3408. depickled_arr_without_object.dtype)
  3409. @pytest.mark.skipif(pickle.HIGHEST_PROTOCOL < 5,
  3410. reason="requires pickle protocol 5")
  3411. def test_f_contiguous_array(self):
  3412. f_contiguous_array = np.array([[1, 2, 3], [4, 5, 6]], order='F')
  3413. buffers = []
  3414. # When using pickle protocol 5, Fortran-contiguous arrays can be
  3415. # serialized using out-of-band buffers
  3416. bytes_string = pickle.dumps(f_contiguous_array, protocol=5,
  3417. buffer_callback=buffers.append)
  3418. assert len(buffers) > 0
  3419. depickled_f_contiguous_array = pickle.loads(bytes_string,
  3420. buffers=buffers)
  3421. assert_equal(f_contiguous_array, depickled_f_contiguous_array)
  3422. def test_non_contiguous_array(self):
  3423. non_contiguous_array = np.arange(12).reshape(3, 4)[:, :2]
  3424. assert not non_contiguous_array.flags.c_contiguous
  3425. assert not non_contiguous_array.flags.f_contiguous
  3426. # make sure non-contiguous arrays can be pickled-depickled
  3427. # using any protocol
  3428. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  3429. depickled_non_contiguous_array = pickle.loads(
  3430. pickle.dumps(non_contiguous_array, protocol=proto))
  3431. assert_equal(non_contiguous_array, depickled_non_contiguous_array)
  3432. def test_roundtrip(self):
  3433. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  3434. carray = np.array([[2, 9], [7, 0], [3, 8]])
  3435. DATA = [
  3436. carray,
  3437. np.transpose(carray),
  3438. np.array([('xxx', 1, 2.0)], dtype=[('a', (str, 3)), ('b', int),
  3439. ('c', float)])
  3440. ]
  3441. refs = [weakref.ref(a) for a in DATA]
  3442. for a in DATA:
  3443. assert_equal(
  3444. a, pickle.loads(pickle.dumps(a, protocol=proto)),
  3445. err_msg="%r" % a)
  3446. del a, DATA, carray
  3447. break_cycles()
  3448. # check for reference leaks (gh-12793)
  3449. for ref in refs:
  3450. assert ref() is None
  3451. def _loads(self, obj):
  3452. return pickle.loads(obj, encoding='latin1')
  3453. # version 0 pickles, using protocol=2 to pickle
  3454. # version 0 doesn't have a version field
  3455. def test_version0_int8(self):
  3456. s = b'\x80\x02cnumpy.core._internal\n_reconstruct\nq\x01cnumpy\nndarray\nq\x02K\x00\x85U\x01b\x87Rq\x03(K\x04\x85cnumpy\ndtype\nq\x04U\x02i1K\x00K\x01\x87Rq\x05(U\x01|NNJ\xff\xff\xff\xffJ\xff\xff\xff\xfftb\x89U\x04\x01\x02\x03\x04tb.'
  3457. a = np.array([1, 2, 3, 4], dtype=np.int8)
  3458. p = self._loads(s)
  3459. assert_equal(a, p)
  3460. def test_version0_float32(self):
  3461. s = b'\x80\x02cnumpy.core._internal\n_reconstruct\nq\x01cnumpy\nndarray\nq\x02K\x00\x85U\x01b\x87Rq\x03(K\x04\x85cnumpy\ndtype\nq\x04U\x02f4K\x00K\x01\x87Rq\x05(U\x01<NNJ\xff\xff\xff\xffJ\xff\xff\xff\xfftb\x89U\x10\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@tb.'
  3462. a = np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float32)
  3463. p = self._loads(s)
  3464. assert_equal(a, p)
  3465. def test_version0_object(self):
  3466. s = b'\x80\x02cnumpy.core._internal\n_reconstruct\nq\x01cnumpy\nndarray\nq\x02K\x00\x85U\x01b\x87Rq\x03(K\x02\x85cnumpy\ndtype\nq\x04U\x02O8K\x00K\x01\x87Rq\x05(U\x01|NNJ\xff\xff\xff\xffJ\xff\xff\xff\xfftb\x89]q\x06(}q\x07U\x01aK\x01s}q\x08U\x01bK\x02setb.'
  3467. a = np.array([{'a': 1}, {'b': 2}])
  3468. p = self._loads(s)
  3469. assert_equal(a, p)
  3470. # version 1 pickles, using protocol=2 to pickle
  3471. def test_version1_int8(self):
  3472. s = b'\x80\x02cnumpy.core._internal\n_reconstruct\nq\x01cnumpy\nndarray\nq\x02K\x00\x85U\x01b\x87Rq\x03(K\x01K\x04\x85cnumpy\ndtype\nq\x04U\x02i1K\x00K\x01\x87Rq\x05(K\x01U\x01|NNJ\xff\xff\xff\xffJ\xff\xff\xff\xfftb\x89U\x04\x01\x02\x03\x04tb.'
  3473. a = np.array([1, 2, 3, 4], dtype=np.int8)
  3474. p = self._loads(s)
  3475. assert_equal(a, p)
  3476. def test_version1_float32(self):
  3477. s = b'\x80\x02cnumpy.core._internal\n_reconstruct\nq\x01cnumpy\nndarray\nq\x02K\x00\x85U\x01b\x87Rq\x03(K\x01K\x04\x85cnumpy\ndtype\nq\x04U\x02f4K\x00K\x01\x87Rq\x05(K\x01U\x01<NNJ\xff\xff\xff\xffJ\xff\xff\xff\xfftb\x89U\x10\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@tb.'
  3478. a = np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float32)
  3479. p = self._loads(s)
  3480. assert_equal(a, p)
  3481. def test_version1_object(self):
  3482. s = b'\x80\x02cnumpy.core._internal\n_reconstruct\nq\x01cnumpy\nndarray\nq\x02K\x00\x85U\x01b\x87Rq\x03(K\x01K\x02\x85cnumpy\ndtype\nq\x04U\x02O8K\x00K\x01\x87Rq\x05(K\x01U\x01|NNJ\xff\xff\xff\xffJ\xff\xff\xff\xfftb\x89]q\x06(}q\x07U\x01aK\x01s}q\x08U\x01bK\x02setb.'
  3483. a = np.array([{'a': 1}, {'b': 2}])
  3484. p = self._loads(s)
  3485. assert_equal(a, p)
  3486. def test_subarray_int_shape(self):
  3487. s = b"cnumpy.core.multiarray\n_reconstruct\np0\n(cnumpy\nndarray\np1\n(I0\ntp2\nS'b'\np3\ntp4\nRp5\n(I1\n(I1\ntp6\ncnumpy\ndtype\np7\n(S'V6'\np8\nI0\nI1\ntp9\nRp10\n(I3\nS'|'\np11\nN(S'a'\np12\ng3\ntp13\n(dp14\ng12\n(g7\n(S'V4'\np15\nI0\nI1\ntp16\nRp17\n(I3\nS'|'\np18\n(g7\n(S'i1'\np19\nI0\nI1\ntp20\nRp21\n(I3\nS'|'\np22\nNNNI-1\nI-1\nI0\ntp23\nb(I2\nI2\ntp24\ntp25\nNNI4\nI1\nI0\ntp26\nbI0\ntp27\nsg3\n(g7\n(S'V2'\np28\nI0\nI1\ntp29\nRp30\n(I3\nS'|'\np31\n(g21\nI2\ntp32\nNNI2\nI1\nI0\ntp33\nbI4\ntp34\nsI6\nI1\nI0\ntp35\nbI00\nS'\\x01\\x01\\x01\\x01\\x01\\x02'\np36\ntp37\nb."
  3488. a = np.array([(1, (1, 2))], dtype=[('a', 'i1', (2, 2)), ('b', 'i1', 2)])
  3489. p = self._loads(s)
  3490. assert_equal(a, p)
  3491. def test_datetime64_byteorder(self):
  3492. original = np.array([['2015-02-24T00:00:00.000000000']], dtype='datetime64[ns]')
  3493. original_byte_reversed = original.copy(order='K')
  3494. original_byte_reversed.dtype = original_byte_reversed.dtype.newbyteorder('S')
  3495. original_byte_reversed.byteswap(inplace=True)
  3496. new = pickle.loads(pickle.dumps(original_byte_reversed))
  3497. assert_equal(original.dtype, new.dtype)
  3498. class TestFancyIndexing:
  3499. def test_list(self):
  3500. x = np.ones((1, 1))
  3501. x[:, [0]] = 2.0
  3502. assert_array_equal(x, np.array([[2.0]]))
  3503. x = np.ones((1, 1, 1))
  3504. x[:, :, [0]] = 2.0
  3505. assert_array_equal(x, np.array([[[2.0]]]))
  3506. def test_tuple(self):
  3507. x = np.ones((1, 1))
  3508. x[:, (0,)] = 2.0
  3509. assert_array_equal(x, np.array([[2.0]]))
  3510. x = np.ones((1, 1, 1))
  3511. x[:, :, (0,)] = 2.0
  3512. assert_array_equal(x, np.array([[[2.0]]]))
  3513. def test_mask(self):
  3514. x = np.array([1, 2, 3, 4])
  3515. m = np.array([0, 1, 0, 0], bool)
  3516. assert_array_equal(x[m], np.array([2]))
  3517. def test_mask2(self):
  3518. x = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
  3519. m = np.array([0, 1], bool)
  3520. m2 = np.array([[0, 1, 0, 0], [1, 0, 0, 0]], bool)
  3521. m3 = np.array([[0, 1, 0, 0], [0, 0, 0, 0]], bool)
  3522. assert_array_equal(x[m], np.array([[5, 6, 7, 8]]))
  3523. assert_array_equal(x[m2], np.array([2, 5]))
  3524. assert_array_equal(x[m3], np.array([2]))
  3525. def test_assign_mask(self):
  3526. x = np.array([1, 2, 3, 4])
  3527. m = np.array([0, 1, 0, 0], bool)
  3528. x[m] = 5
  3529. assert_array_equal(x, np.array([1, 5, 3, 4]))
  3530. def test_assign_mask2(self):
  3531. xorig = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
  3532. m = np.array([0, 1], bool)
  3533. m2 = np.array([[0, 1, 0, 0], [1, 0, 0, 0]], bool)
  3534. m3 = np.array([[0, 1, 0, 0], [0, 0, 0, 0]], bool)
  3535. x = xorig.copy()
  3536. x[m] = 10
  3537. assert_array_equal(x, np.array([[1, 2, 3, 4], [10, 10, 10, 10]]))
  3538. x = xorig.copy()
  3539. x[m2] = 10
  3540. assert_array_equal(x, np.array([[1, 10, 3, 4], [10, 6, 7, 8]]))
  3541. x = xorig.copy()
  3542. x[m3] = 10
  3543. assert_array_equal(x, np.array([[1, 10, 3, 4], [5, 6, 7, 8]]))
  3544. class TestStringCompare:
  3545. def test_string(self):
  3546. g1 = np.array(["This", "is", "example"])
  3547. g2 = np.array(["This", "was", "example"])
  3548. assert_array_equal(g1 == g2, [g1[i] == g2[i] for i in [0, 1, 2]])
  3549. assert_array_equal(g1 != g2, [g1[i] != g2[i] for i in [0, 1, 2]])
  3550. assert_array_equal(g1 <= g2, [g1[i] <= g2[i] for i in [0, 1, 2]])
  3551. assert_array_equal(g1 >= g2, [g1[i] >= g2[i] for i in [0, 1, 2]])
  3552. assert_array_equal(g1 < g2, [g1[i] < g2[i] for i in [0, 1, 2]])
  3553. assert_array_equal(g1 > g2, [g1[i] > g2[i] for i in [0, 1, 2]])
  3554. def test_mixed(self):
  3555. g1 = np.array(["spam", "spa", "spammer", "and eggs"])
  3556. g2 = "spam"
  3557. assert_array_equal(g1 == g2, [x == g2 for x in g1])
  3558. assert_array_equal(g1 != g2, [x != g2 for x in g1])
  3559. assert_array_equal(g1 < g2, [x < g2 for x in g1])
  3560. assert_array_equal(g1 > g2, [x > g2 for x in g1])
  3561. assert_array_equal(g1 <= g2, [x <= g2 for x in g1])
  3562. assert_array_equal(g1 >= g2, [x >= g2 for x in g1])
  3563. def test_unicode(self):
  3564. g1 = np.array([u"This", u"is", u"example"])
  3565. g2 = np.array([u"This", u"was", u"example"])
  3566. assert_array_equal(g1 == g2, [g1[i] == g2[i] for i in [0, 1, 2]])
  3567. assert_array_equal(g1 != g2, [g1[i] != g2[i] for i in [0, 1, 2]])
  3568. assert_array_equal(g1 <= g2, [g1[i] <= g2[i] for i in [0, 1, 2]])
  3569. assert_array_equal(g1 >= g2, [g1[i] >= g2[i] for i in [0, 1, 2]])
  3570. assert_array_equal(g1 < g2, [g1[i] < g2[i] for i in [0, 1, 2]])
  3571. assert_array_equal(g1 > g2, [g1[i] > g2[i] for i in [0, 1, 2]])
  3572. class TestArgmax:
  3573. nan_arr = [
  3574. ([0, 1, 2, 3, np.nan], 4),
  3575. ([0, 1, 2, np.nan, 3], 3),
  3576. ([np.nan, 0, 1, 2, 3], 0),
  3577. ([np.nan, 0, np.nan, 2, 3], 0),
  3578. ([0, 1, 2, 3, complex(0, np.nan)], 4),
  3579. ([0, 1, 2, 3, complex(np.nan, 0)], 4),
  3580. ([0, 1, 2, complex(np.nan, 0), 3], 3),
  3581. ([0, 1, 2, complex(0, np.nan), 3], 3),
  3582. ([complex(0, np.nan), 0, 1, 2, 3], 0),
  3583. ([complex(np.nan, np.nan), 0, 1, 2, 3], 0),
  3584. ([complex(np.nan, 0), complex(np.nan, 2), complex(np.nan, 1)], 0),
  3585. ([complex(np.nan, np.nan), complex(np.nan, 2), complex(np.nan, 1)], 0),
  3586. ([complex(np.nan, 0), complex(np.nan, 2), complex(np.nan, np.nan)], 0),
  3587. ([complex(0, 0), complex(0, 2), complex(0, 1)], 1),
  3588. ([complex(1, 0), complex(0, 2), complex(0, 1)], 0),
  3589. ([complex(1, 0), complex(0, 2), complex(1, 1)], 2),
  3590. ([np.datetime64('1923-04-14T12:43:12'),
  3591. np.datetime64('1994-06-21T14:43:15'),
  3592. np.datetime64('2001-10-15T04:10:32'),
  3593. np.datetime64('1995-11-25T16:02:16'),
  3594. np.datetime64('2005-01-04T03:14:12'),
  3595. np.datetime64('2041-12-03T14:05:03')], 5),
  3596. ([np.datetime64('1935-09-14T04:40:11'),
  3597. np.datetime64('1949-10-12T12:32:11'),
  3598. np.datetime64('2010-01-03T05:14:12'),
  3599. np.datetime64('2015-11-20T12:20:59'),
  3600. np.datetime64('1932-09-23T10:10:13'),
  3601. np.datetime64('2014-10-10T03:50:30')], 3),
  3602. # Assorted tests with NaTs
  3603. ([np.datetime64('NaT'),
  3604. np.datetime64('NaT'),
  3605. np.datetime64('2010-01-03T05:14:12'),
  3606. np.datetime64('NaT'),
  3607. np.datetime64('2015-09-23T10:10:13'),
  3608. np.datetime64('1932-10-10T03:50:30')], 0),
  3609. ([np.datetime64('2059-03-14T12:43:12'),
  3610. np.datetime64('1996-09-21T14:43:15'),
  3611. np.datetime64('NaT'),
  3612. np.datetime64('2022-12-25T16:02:16'),
  3613. np.datetime64('1963-10-04T03:14:12'),
  3614. np.datetime64('2013-05-08T18:15:23')], 2),
  3615. ([np.timedelta64(2, 's'),
  3616. np.timedelta64(1, 's'),
  3617. np.timedelta64('NaT', 's'),
  3618. np.timedelta64(3, 's')], 2),
  3619. ([np.timedelta64('NaT', 's')] * 3, 0),
  3620. ([timedelta(days=5, seconds=14), timedelta(days=2, seconds=35),
  3621. timedelta(days=-1, seconds=23)], 0),
  3622. ([timedelta(days=1, seconds=43), timedelta(days=10, seconds=5),
  3623. timedelta(days=5, seconds=14)], 1),
  3624. ([timedelta(days=10, seconds=24), timedelta(days=10, seconds=5),
  3625. timedelta(days=10, seconds=43)], 2),
  3626. ([False, False, False, False, True], 4),
  3627. ([False, False, False, True, False], 3),
  3628. ([True, False, False, False, False], 0),
  3629. ([True, False, True, False, False], 0),
  3630. ]
  3631. def test_all(self):
  3632. a = np.random.normal(0, 1, (4, 5, 6, 7, 8))
  3633. for i in range(a.ndim):
  3634. amax = a.max(i)
  3635. aargmax = a.argmax(i)
  3636. axes = list(range(a.ndim))
  3637. axes.remove(i)
  3638. assert_(np.all(amax == aargmax.choose(*a.transpose(i,*axes))))
  3639. def test_combinations(self):
  3640. for arr, pos in self.nan_arr:
  3641. with suppress_warnings() as sup:
  3642. sup.filter(RuntimeWarning,
  3643. "invalid value encountered in reduce")
  3644. max_val = np.max(arr)
  3645. assert_equal(np.argmax(arr), pos, err_msg="%r" % arr)
  3646. assert_equal(arr[np.argmax(arr)], max_val, err_msg="%r" % arr)
  3647. def test_output_shape(self):
  3648. # see also gh-616
  3649. a = np.ones((10, 5))
  3650. # Check some simple shape mismatches
  3651. out = np.ones(11, dtype=np.int_)
  3652. assert_raises(ValueError, a.argmax, -1, out)
  3653. out = np.ones((2, 5), dtype=np.int_)
  3654. assert_raises(ValueError, a.argmax, -1, out)
  3655. # these could be relaxed possibly (used to allow even the previous)
  3656. out = np.ones((1, 10), dtype=np.int_)
  3657. assert_raises(ValueError, a.argmax, -1, out)
  3658. out = np.ones(10, dtype=np.int_)
  3659. a.argmax(-1, out=out)
  3660. assert_equal(out, a.argmax(-1))
  3661. @pytest.mark.parametrize('ndim', [0, 1])
  3662. def test_ret_is_out(self, ndim):
  3663. a = np.ones((4,) + (3,)*ndim)
  3664. out = np.empty((3,)*ndim, dtype=np.intp)
  3665. ret = a.argmax(axis=0, out=out)
  3666. assert ret is out
  3667. def test_argmax_unicode(self):
  3668. d = np.zeros(6031, dtype='<U9')
  3669. d[5942] = "as"
  3670. assert_equal(d.argmax(), 5942)
  3671. def test_np_vs_ndarray(self):
  3672. # make sure both ndarray.argmax and numpy.argmax support out/axis args
  3673. a = np.random.normal(size=(2,3))
  3674. # check positional args
  3675. out1 = np.zeros(2, dtype=int)
  3676. out2 = np.zeros(2, dtype=int)
  3677. assert_equal(a.argmax(1, out1), np.argmax(a, 1, out2))
  3678. assert_equal(out1, out2)
  3679. # check keyword args
  3680. out1 = np.zeros(3, dtype=int)
  3681. out2 = np.zeros(3, dtype=int)
  3682. assert_equal(a.argmax(out=out1, axis=0), np.argmax(a, out=out2, axis=0))
  3683. assert_equal(out1, out2)
  3684. @pytest.mark.leaks_references(reason="replaces None with NULL.")
  3685. def test_object_argmax_with_NULLs(self):
  3686. # See gh-6032
  3687. a = np.empty(4, dtype='O')
  3688. ctypes.memset(a.ctypes.data, 0, a.nbytes)
  3689. assert_equal(a.argmax(), 0)
  3690. a[3] = 10
  3691. assert_equal(a.argmax(), 3)
  3692. a[1] = 30
  3693. assert_equal(a.argmax(), 1)
  3694. class TestArgmin:
  3695. nan_arr = [
  3696. ([0, 1, 2, 3, np.nan], 4),
  3697. ([0, 1, 2, np.nan, 3], 3),
  3698. ([np.nan, 0, 1, 2, 3], 0),
  3699. ([np.nan, 0, np.nan, 2, 3], 0),
  3700. ([0, 1, 2, 3, complex(0, np.nan)], 4),
  3701. ([0, 1, 2, 3, complex(np.nan, 0)], 4),
  3702. ([0, 1, 2, complex(np.nan, 0), 3], 3),
  3703. ([0, 1, 2, complex(0, np.nan), 3], 3),
  3704. ([complex(0, np.nan), 0, 1, 2, 3], 0),
  3705. ([complex(np.nan, np.nan), 0, 1, 2, 3], 0),
  3706. ([complex(np.nan, 0), complex(np.nan, 2), complex(np.nan, 1)], 0),
  3707. ([complex(np.nan, np.nan), complex(np.nan, 2), complex(np.nan, 1)], 0),
  3708. ([complex(np.nan, 0), complex(np.nan, 2), complex(np.nan, np.nan)], 0),
  3709. ([complex(0, 0), complex(0, 2), complex(0, 1)], 0),
  3710. ([complex(1, 0), complex(0, 2), complex(0, 1)], 2),
  3711. ([complex(1, 0), complex(0, 2), complex(1, 1)], 1),
  3712. ([np.datetime64('1923-04-14T12:43:12'),
  3713. np.datetime64('1994-06-21T14:43:15'),
  3714. np.datetime64('2001-10-15T04:10:32'),
  3715. np.datetime64('1995-11-25T16:02:16'),
  3716. np.datetime64('2005-01-04T03:14:12'),
  3717. np.datetime64('2041-12-03T14:05:03')], 0),
  3718. ([np.datetime64('1935-09-14T04:40:11'),
  3719. np.datetime64('1949-10-12T12:32:11'),
  3720. np.datetime64('2010-01-03T05:14:12'),
  3721. np.datetime64('2014-11-20T12:20:59'),
  3722. np.datetime64('2015-09-23T10:10:13'),
  3723. np.datetime64('1932-10-10T03:50:30')], 5),
  3724. # Assorted tests with NaTs
  3725. ([np.datetime64('NaT'),
  3726. np.datetime64('NaT'),
  3727. np.datetime64('2010-01-03T05:14:12'),
  3728. np.datetime64('NaT'),
  3729. np.datetime64('2015-09-23T10:10:13'),
  3730. np.datetime64('1932-10-10T03:50:30')], 0),
  3731. ([np.datetime64('2059-03-14T12:43:12'),
  3732. np.datetime64('1996-09-21T14:43:15'),
  3733. np.datetime64('NaT'),
  3734. np.datetime64('2022-12-25T16:02:16'),
  3735. np.datetime64('1963-10-04T03:14:12'),
  3736. np.datetime64('2013-05-08T18:15:23')], 2),
  3737. ([np.timedelta64(2, 's'),
  3738. np.timedelta64(1, 's'),
  3739. np.timedelta64('NaT', 's'),
  3740. np.timedelta64(3, 's')], 2),
  3741. ([np.timedelta64('NaT', 's')] * 3, 0),
  3742. ([timedelta(days=5, seconds=14), timedelta(days=2, seconds=35),
  3743. timedelta(days=-1, seconds=23)], 2),
  3744. ([timedelta(days=1, seconds=43), timedelta(days=10, seconds=5),
  3745. timedelta(days=5, seconds=14)], 0),
  3746. ([timedelta(days=10, seconds=24), timedelta(days=10, seconds=5),
  3747. timedelta(days=10, seconds=43)], 1),
  3748. ([True, True, True, True, False], 4),
  3749. ([True, True, True, False, True], 3),
  3750. ([False, True, True, True, True], 0),
  3751. ([False, True, False, True, True], 0),
  3752. ]
  3753. def test_all(self):
  3754. a = np.random.normal(0, 1, (4, 5, 6, 7, 8))
  3755. for i in range(a.ndim):
  3756. amin = a.min(i)
  3757. aargmin = a.argmin(i)
  3758. axes = list(range(a.ndim))
  3759. axes.remove(i)
  3760. assert_(np.all(amin == aargmin.choose(*a.transpose(i,*axes))))
  3761. def test_combinations(self):
  3762. for arr, pos in self.nan_arr:
  3763. with suppress_warnings() as sup:
  3764. sup.filter(RuntimeWarning,
  3765. "invalid value encountered in reduce")
  3766. min_val = np.min(arr)
  3767. assert_equal(np.argmin(arr), pos, err_msg="%r" % arr)
  3768. assert_equal(arr[np.argmin(arr)], min_val, err_msg="%r" % arr)
  3769. def test_minimum_signed_integers(self):
  3770. a = np.array([1, -2**7, -2**7 + 1], dtype=np.int8)
  3771. assert_equal(np.argmin(a), 1)
  3772. a = np.array([1, -2**15, -2**15 + 1], dtype=np.int16)
  3773. assert_equal(np.argmin(a), 1)
  3774. a = np.array([1, -2**31, -2**31 + 1], dtype=np.int32)
  3775. assert_equal(np.argmin(a), 1)
  3776. a = np.array([1, -2**63, -2**63 + 1], dtype=np.int64)
  3777. assert_equal(np.argmin(a), 1)
  3778. def test_output_shape(self):
  3779. # see also gh-616
  3780. a = np.ones((10, 5))
  3781. # Check some simple shape mismatches
  3782. out = np.ones(11, dtype=np.int_)
  3783. assert_raises(ValueError, a.argmin, -1, out)
  3784. out = np.ones((2, 5), dtype=np.int_)
  3785. assert_raises(ValueError, a.argmin, -1, out)
  3786. # these could be relaxed possibly (used to allow even the previous)
  3787. out = np.ones((1, 10), dtype=np.int_)
  3788. assert_raises(ValueError, a.argmin, -1, out)
  3789. out = np.ones(10, dtype=np.int_)
  3790. a.argmin(-1, out=out)
  3791. assert_equal(out, a.argmin(-1))
  3792. @pytest.mark.parametrize('ndim', [0, 1])
  3793. def test_ret_is_out(self, ndim):
  3794. a = np.ones((4,) + (3,)*ndim)
  3795. out = np.empty((3,)*ndim, dtype=np.intp)
  3796. ret = a.argmin(axis=0, out=out)
  3797. assert ret is out
  3798. def test_argmin_unicode(self):
  3799. d = np.ones(6031, dtype='<U9')
  3800. d[6001] = "0"
  3801. assert_equal(d.argmin(), 6001)
  3802. def test_np_vs_ndarray(self):
  3803. # make sure both ndarray.argmin and numpy.argmin support out/axis args
  3804. a = np.random.normal(size=(2, 3))
  3805. # check positional args
  3806. out1 = np.zeros(2, dtype=int)
  3807. out2 = np.ones(2, dtype=int)
  3808. assert_equal(a.argmin(1, out1), np.argmin(a, 1, out2))
  3809. assert_equal(out1, out2)
  3810. # check keyword args
  3811. out1 = np.zeros(3, dtype=int)
  3812. out2 = np.ones(3, dtype=int)
  3813. assert_equal(a.argmin(out=out1, axis=0), np.argmin(a, out=out2, axis=0))
  3814. assert_equal(out1, out2)
  3815. @pytest.mark.leaks_references(reason="replaces None with NULL.")
  3816. def test_object_argmin_with_NULLs(self):
  3817. # See gh-6032
  3818. a = np.empty(4, dtype='O')
  3819. ctypes.memset(a.ctypes.data, 0, a.nbytes)
  3820. assert_equal(a.argmin(), 0)
  3821. a[3] = 30
  3822. assert_equal(a.argmin(), 3)
  3823. a[1] = 10
  3824. assert_equal(a.argmin(), 1)
  3825. class TestMinMax:
  3826. def test_scalar(self):
  3827. assert_raises(np.AxisError, np.amax, 1, 1)
  3828. assert_raises(np.AxisError, np.amin, 1, 1)
  3829. assert_equal(np.amax(1, axis=0), 1)
  3830. assert_equal(np.amin(1, axis=0), 1)
  3831. assert_equal(np.amax(1, axis=None), 1)
  3832. assert_equal(np.amin(1, axis=None), 1)
  3833. def test_axis(self):
  3834. assert_raises(np.AxisError, np.amax, [1, 2, 3], 1000)
  3835. assert_equal(np.amax([[1, 2, 3]], axis=1), 3)
  3836. def test_datetime(self):
  3837. # Do not ignore NaT
  3838. for dtype in ('m8[s]', 'm8[Y]'):
  3839. a = np.arange(10).astype(dtype)
  3840. assert_equal(np.amin(a), a[0])
  3841. assert_equal(np.amax(a), a[9])
  3842. a[3] = 'NaT'
  3843. assert_equal(np.amin(a), a[3])
  3844. assert_equal(np.amax(a), a[3])
  3845. class TestNewaxis:
  3846. def test_basic(self):
  3847. sk = np.array([0, -0.1, 0.1])
  3848. res = 250*sk[:, np.newaxis]
  3849. assert_almost_equal(res.ravel(), 250*sk)
  3850. class TestClip:
  3851. def _check_range(self, x, cmin, cmax):
  3852. assert_(np.all(x >= cmin))
  3853. assert_(np.all(x <= cmax))
  3854. def _clip_type(self, type_group, array_max,
  3855. clip_min, clip_max, inplace=False,
  3856. expected_min=None, expected_max=None):
  3857. if expected_min is None:
  3858. expected_min = clip_min
  3859. if expected_max is None:
  3860. expected_max = clip_max
  3861. for T in np.sctypes[type_group]:
  3862. if sys.byteorder == 'little':
  3863. byte_orders = ['=', '>']
  3864. else:
  3865. byte_orders = ['<', '=']
  3866. for byteorder in byte_orders:
  3867. dtype = np.dtype(T).newbyteorder(byteorder)
  3868. x = (np.random.random(1000) * array_max).astype(dtype)
  3869. if inplace:
  3870. # The tests that call us pass clip_min and clip_max that
  3871. # might not fit in the destination dtype. They were written
  3872. # assuming the previous unsafe casting, which now must be
  3873. # passed explicitly to avoid a warning.
  3874. x.clip(clip_min, clip_max, x, casting='unsafe')
  3875. else:
  3876. x = x.clip(clip_min, clip_max)
  3877. byteorder = '='
  3878. if x.dtype.byteorder == '|':
  3879. byteorder = '|'
  3880. assert_equal(x.dtype.byteorder, byteorder)
  3881. self._check_range(x, expected_min, expected_max)
  3882. return x
  3883. def test_basic(self):
  3884. for inplace in [False, True]:
  3885. self._clip_type(
  3886. 'float', 1024, -12.8, 100.2, inplace=inplace)
  3887. self._clip_type(
  3888. 'float', 1024, 0, 0, inplace=inplace)
  3889. self._clip_type(
  3890. 'int', 1024, -120, 100, inplace=inplace)
  3891. self._clip_type(
  3892. 'int', 1024, 0, 0, inplace=inplace)
  3893. self._clip_type(
  3894. 'uint', 1024, 0, 0, inplace=inplace)
  3895. self._clip_type(
  3896. 'uint', 1024, -120, 100, inplace=inplace, expected_min=0)
  3897. def test_record_array(self):
  3898. rec = np.array([(-5, 2.0, 3.0), (5.0, 4.0, 3.0)],
  3899. dtype=[('x', '<f8'), ('y', '<f8'), ('z', '<f8')])
  3900. y = rec['x'].clip(-0.3, 0.5)
  3901. self._check_range(y, -0.3, 0.5)
  3902. def test_max_or_min(self):
  3903. val = np.array([0, 1, 2, 3, 4, 5, 6, 7])
  3904. x = val.clip(3)
  3905. assert_(np.all(x >= 3))
  3906. x = val.clip(min=3)
  3907. assert_(np.all(x >= 3))
  3908. x = val.clip(max=4)
  3909. assert_(np.all(x <= 4))
  3910. def test_nan(self):
  3911. input_arr = np.array([-2., np.nan, 0.5, 3., 0.25, np.nan])
  3912. result = input_arr.clip(-1, 1)
  3913. expected = np.array([-1., np.nan, 0.5, 1., 0.25, np.nan])
  3914. assert_array_equal(result, expected)
  3915. class TestCompress:
  3916. def test_axis(self):
  3917. tgt = [[5, 6, 7, 8, 9]]
  3918. arr = np.arange(10).reshape(2, 5)
  3919. out = np.compress([0, 1], arr, axis=0)
  3920. assert_equal(out, tgt)
  3921. tgt = [[1, 3], [6, 8]]
  3922. out = np.compress([0, 1, 0, 1, 0], arr, axis=1)
  3923. assert_equal(out, tgt)
  3924. def test_truncate(self):
  3925. tgt = [[1], [6]]
  3926. arr = np.arange(10).reshape(2, 5)
  3927. out = np.compress([0, 1], arr, axis=1)
  3928. assert_equal(out, tgt)
  3929. def test_flatten(self):
  3930. arr = np.arange(10).reshape(2, 5)
  3931. out = np.compress([0, 1], arr)
  3932. assert_equal(out, 1)
  3933. class TestPutmask:
  3934. def tst_basic(self, x, T, mask, val):
  3935. np.putmask(x, mask, val)
  3936. assert_equal(x[mask], np.array(val, T))
  3937. def test_ip_types(self):
  3938. unchecked_types = [bytes, str, np.void]
  3939. x = np.random.random(1000)*100
  3940. mask = x < 40
  3941. for val in [-100, 0, 15]:
  3942. for types in np.sctypes.values():
  3943. for T in types:
  3944. if T not in unchecked_types:
  3945. self.tst_basic(x.copy().astype(T), T, mask, val)
  3946. # Also test string of a length which uses an untypical length
  3947. dt = np.dtype("S3")
  3948. self.tst_basic(x.astype(dt), dt.type, mask, dt.type(val)[:3])
  3949. def test_mask_size(self):
  3950. assert_raises(ValueError, np.putmask, np.array([1, 2, 3]), [True], 5)
  3951. @pytest.mark.parametrize('dtype', ('>i4', '<i4'))
  3952. def test_byteorder(self, dtype):
  3953. x = np.array([1, 2, 3], dtype)
  3954. np.putmask(x, [True, False, True], -1)
  3955. assert_array_equal(x, [-1, 2, -1])
  3956. def test_record_array(self):
  3957. # Note mixed byteorder.
  3958. rec = np.array([(-5, 2.0, 3.0), (5.0, 4.0, 3.0)],
  3959. dtype=[('x', '<f8'), ('y', '>f8'), ('z', '<f8')])
  3960. np.putmask(rec['x'], [True, False], 10)
  3961. assert_array_equal(rec['x'], [10, 5])
  3962. assert_array_equal(rec['y'], [2, 4])
  3963. assert_array_equal(rec['z'], [3, 3])
  3964. np.putmask(rec['y'], [True, False], 11)
  3965. assert_array_equal(rec['x'], [10, 5])
  3966. assert_array_equal(rec['y'], [11, 4])
  3967. assert_array_equal(rec['z'], [3, 3])
  3968. def test_overlaps(self):
  3969. # gh-6272 check overlap
  3970. x = np.array([True, False, True, False])
  3971. np.putmask(x[1:4], [True, True, True], x[:3])
  3972. assert_equal(x, np.array([True, True, False, True]))
  3973. x = np.array([True, False, True, False])
  3974. np.putmask(x[1:4], x[:3], [True, False, True])
  3975. assert_equal(x, np.array([True, True, True, True]))
  3976. class TestTake:
  3977. def tst_basic(self, x):
  3978. ind = list(range(x.shape[0]))
  3979. assert_array_equal(x.take(ind, axis=0), x)
  3980. def test_ip_types(self):
  3981. unchecked_types = [bytes, str, np.void]
  3982. x = np.random.random(24)*100
  3983. x.shape = 2, 3, 4
  3984. for types in np.sctypes.values():
  3985. for T in types:
  3986. if T not in unchecked_types:
  3987. self.tst_basic(x.copy().astype(T))
  3988. # Also test string of a length which uses an untypical length
  3989. self.tst_basic(x.astype("S3"))
  3990. def test_raise(self):
  3991. x = np.random.random(24)*100
  3992. x.shape = 2, 3, 4
  3993. assert_raises(IndexError, x.take, [0, 1, 2], axis=0)
  3994. assert_raises(IndexError, x.take, [-3], axis=0)
  3995. assert_array_equal(x.take([-1], axis=0)[0], x[1])
  3996. def test_clip(self):
  3997. x = np.random.random(24)*100
  3998. x.shape = 2, 3, 4
  3999. assert_array_equal(x.take([-1], axis=0, mode='clip')[0], x[0])
  4000. assert_array_equal(x.take([2], axis=0, mode='clip')[0], x[1])
  4001. def test_wrap(self):
  4002. x = np.random.random(24)*100
  4003. x.shape = 2, 3, 4
  4004. assert_array_equal(x.take([-1], axis=0, mode='wrap')[0], x[1])
  4005. assert_array_equal(x.take([2], axis=0, mode='wrap')[0], x[0])
  4006. assert_array_equal(x.take([3], axis=0, mode='wrap')[0], x[1])
  4007. @pytest.mark.parametrize('dtype', ('>i4', '<i4'))
  4008. def test_byteorder(self, dtype):
  4009. x = np.array([1, 2, 3], dtype)
  4010. assert_array_equal(x.take([0, 2, 1]), [1, 3, 2])
  4011. def test_record_array(self):
  4012. # Note mixed byteorder.
  4013. rec = np.array([(-5, 2.0, 3.0), (5.0, 4.0, 3.0)],
  4014. dtype=[('x', '<f8'), ('y', '>f8'), ('z', '<f8')])
  4015. rec1 = rec.take([1])
  4016. assert_(rec1['x'] == 5.0 and rec1['y'] == 4.0)
  4017. def test_out_overlap(self):
  4018. # gh-6272 check overlap on out
  4019. x = np.arange(5)
  4020. y = np.take(x, [1, 2, 3], out=x[2:5], mode='wrap')
  4021. assert_equal(y, np.array([1, 2, 3]))
  4022. @pytest.mark.parametrize('shape', [(1, 2), (1,), ()])
  4023. def test_ret_is_out(self, shape):
  4024. # 0d arrays should not be an exception to this rule
  4025. x = np.arange(5)
  4026. inds = np.zeros(shape, dtype=np.intp)
  4027. out = np.zeros(shape, dtype=x.dtype)
  4028. ret = np.take(x, inds, out=out)
  4029. assert ret is out
  4030. class TestLexsort:
  4031. @pytest.mark.parametrize('dtype',[
  4032. np.uint8, np.uint16, np.uint32, np.uint64,
  4033. np.int8, np.int16, np.int32, np.int64,
  4034. np.float16, np.float32, np.float64
  4035. ])
  4036. def test_basic(self, dtype):
  4037. a = np.array([1, 2, 1, 3, 1, 5], dtype=dtype)
  4038. b = np.array([0, 4, 5, 6, 2, 3], dtype=dtype)
  4039. idx = np.lexsort((b, a))
  4040. expected_idx = np.array([0, 4, 2, 1, 3, 5])
  4041. assert_array_equal(idx, expected_idx)
  4042. assert_array_equal(a[idx], np.sort(a))
  4043. def test_mixed(self):
  4044. a = np.array([1, 2, 1, 3, 1, 5])
  4045. b = np.array([0, 4, 5, 6, 2, 3], dtype='datetime64[D]')
  4046. idx = np.lexsort((b, a))
  4047. expected_idx = np.array([0, 4, 2, 1, 3, 5])
  4048. assert_array_equal(idx, expected_idx)
  4049. def test_datetime(self):
  4050. a = np.array([0,0,0], dtype='datetime64[D]')
  4051. b = np.array([2,1,0], dtype='datetime64[D]')
  4052. idx = np.lexsort((b, a))
  4053. expected_idx = np.array([2, 1, 0])
  4054. assert_array_equal(idx, expected_idx)
  4055. a = np.array([0,0,0], dtype='timedelta64[D]')
  4056. b = np.array([2,1,0], dtype='timedelta64[D]')
  4057. idx = np.lexsort((b, a))
  4058. expected_idx = np.array([2, 1, 0])
  4059. assert_array_equal(idx, expected_idx)
  4060. def test_object(self): # gh-6312
  4061. a = np.random.choice(10, 1000)
  4062. b = np.random.choice(['abc', 'xy', 'wz', 'efghi', 'qwst', 'x'], 1000)
  4063. for u in a, b:
  4064. left = np.lexsort((u.astype('O'),))
  4065. right = np.argsort(u, kind='mergesort')
  4066. assert_array_equal(left, right)
  4067. for u, v in (a, b), (b, a):
  4068. idx = np.lexsort((u, v))
  4069. assert_array_equal(idx, np.lexsort((u.astype('O'), v)))
  4070. assert_array_equal(idx, np.lexsort((u, v.astype('O'))))
  4071. u, v = np.array(u, dtype='object'), np.array(v, dtype='object')
  4072. assert_array_equal(idx, np.lexsort((u, v)))
  4073. def test_invalid_axis(self): # gh-7528
  4074. x = np.linspace(0., 1., 42*3).reshape(42, 3)
  4075. assert_raises(np.AxisError, np.lexsort, x, axis=2)
  4076. class TestIO:
  4077. """Test tofile, fromfile, tobytes, and fromstring"""
  4078. def setup(self):
  4079. shape = (2, 4, 3)
  4080. rand = np.random.random
  4081. self.x = rand(shape) + rand(shape).astype(complex)*1j
  4082. self.x[0,:, 1] = [np.nan, np.inf, -np.inf, np.nan]
  4083. self.dtype = self.x.dtype
  4084. self.tempdir = tempfile.mkdtemp()
  4085. self.filename = tempfile.mktemp(dir=self.tempdir)
  4086. def teardown(self):
  4087. shutil.rmtree(self.tempdir)
  4088. def test_nofile(self):
  4089. # this should probably be supported as a file
  4090. # but for now test for proper errors
  4091. b = io.BytesIO()
  4092. assert_raises(IOError, np.fromfile, b, np.uint8, 80)
  4093. d = np.ones(7)
  4094. assert_raises(IOError, lambda x: x.tofile(b), d)
  4095. def test_bool_fromstring(self):
  4096. v = np.array([True, False, True, False], dtype=np.bool_)
  4097. y = np.fromstring('1 0 -2.3 0.0', sep=' ', dtype=np.bool_)
  4098. assert_array_equal(v, y)
  4099. def test_uint64_fromstring(self):
  4100. d = np.fromstring("9923372036854775807 104783749223640",
  4101. dtype=np.uint64, sep=' ')
  4102. e = np.array([9923372036854775807, 104783749223640], dtype=np.uint64)
  4103. assert_array_equal(d, e)
  4104. def test_int64_fromstring(self):
  4105. d = np.fromstring("-25041670086757 104783749223640",
  4106. dtype=np.int64, sep=' ')
  4107. e = np.array([-25041670086757, 104783749223640], dtype=np.int64)
  4108. assert_array_equal(d, e)
  4109. def test_fromstring_count0(self):
  4110. d = np.fromstring("1,2", sep=",", dtype=np.int64, count=0)
  4111. assert d.shape == (0,)
  4112. def test_empty_files_binary(self):
  4113. with open(self.filename, 'w') as f:
  4114. pass
  4115. y = np.fromfile(self.filename)
  4116. assert_(y.size == 0, "Array not empty")
  4117. def test_empty_files_text(self):
  4118. with open(self.filename, 'wb') as f:
  4119. pass
  4120. y = np.fromfile(self.filename, sep=" ")
  4121. assert_(y.size == 0, "Array not empty")
  4122. def test_roundtrip_file(self):
  4123. with open(self.filename, 'wb') as f:
  4124. self.x.tofile(f)
  4125. # NB. doesn't work with flush+seek, due to use of C stdio
  4126. with open(self.filename, 'rb') as f:
  4127. y = np.fromfile(f, dtype=self.dtype)
  4128. assert_array_equal(y, self.x.flat)
  4129. def test_roundtrip_filename(self):
  4130. self.x.tofile(self.filename)
  4131. y = np.fromfile(self.filename, dtype=self.dtype)
  4132. assert_array_equal(y, self.x.flat)
  4133. def test_roundtrip_pathlib(self):
  4134. p = pathlib.Path(self.filename)
  4135. self.x.tofile(p)
  4136. y = np.fromfile(p, dtype=self.dtype)
  4137. assert_array_equal(y, self.x.flat)
  4138. def test_roundtrip_dump_pathlib(self):
  4139. p = pathlib.Path(self.filename)
  4140. self.x.dump(p)
  4141. y = np.load(p, allow_pickle=True)
  4142. assert_array_equal(y, self.x)
  4143. def test_roundtrip_binary_str(self):
  4144. s = self.x.tobytes()
  4145. y = np.frombuffer(s, dtype=self.dtype)
  4146. assert_array_equal(y, self.x.flat)
  4147. s = self.x.tobytes('F')
  4148. y = np.frombuffer(s, dtype=self.dtype)
  4149. assert_array_equal(y, self.x.flatten('F'))
  4150. def test_roundtrip_str(self):
  4151. x = self.x.real.ravel()
  4152. s = "@".join(map(str, x))
  4153. y = np.fromstring(s, sep="@")
  4154. # NB. str imbues less precision
  4155. nan_mask = ~np.isfinite(x)
  4156. assert_array_equal(x[nan_mask], y[nan_mask])
  4157. assert_array_almost_equal(x[~nan_mask], y[~nan_mask], decimal=5)
  4158. def test_roundtrip_repr(self):
  4159. x = self.x.real.ravel()
  4160. s = "@".join(map(repr, x))
  4161. y = np.fromstring(s, sep="@")
  4162. assert_array_equal(x, y)
  4163. def test_unseekable_fromfile(self):
  4164. # gh-6246
  4165. self.x.tofile(self.filename)
  4166. def fail(*args, **kwargs):
  4167. raise IOError('Can not tell or seek')
  4168. with io.open(self.filename, 'rb', buffering=0) as f:
  4169. f.seek = fail
  4170. f.tell = fail
  4171. assert_raises(IOError, np.fromfile, f, dtype=self.dtype)
  4172. def test_io_open_unbuffered_fromfile(self):
  4173. # gh-6632
  4174. self.x.tofile(self.filename)
  4175. with io.open(self.filename, 'rb', buffering=0) as f:
  4176. y = np.fromfile(f, dtype=self.dtype)
  4177. assert_array_equal(y, self.x.flat)
  4178. def test_largish_file(self):
  4179. # check the fallocate path on files > 16MB
  4180. d = np.zeros(4 * 1024 ** 2)
  4181. d.tofile(self.filename)
  4182. assert_equal(os.path.getsize(self.filename), d.nbytes)
  4183. assert_array_equal(d, np.fromfile(self.filename))
  4184. # check offset
  4185. with open(self.filename, "r+b") as f:
  4186. f.seek(d.nbytes)
  4187. d.tofile(f)
  4188. assert_equal(os.path.getsize(self.filename), d.nbytes * 2)
  4189. # check append mode (gh-8329)
  4190. open(self.filename, "w").close() # delete file contents
  4191. with open(self.filename, "ab") as f:
  4192. d.tofile(f)
  4193. assert_array_equal(d, np.fromfile(self.filename))
  4194. with open(self.filename, "ab") as f:
  4195. d.tofile(f)
  4196. assert_equal(os.path.getsize(self.filename), d.nbytes * 2)
  4197. def test_io_open_buffered_fromfile(self):
  4198. # gh-6632
  4199. self.x.tofile(self.filename)
  4200. with io.open(self.filename, 'rb', buffering=-1) as f:
  4201. y = np.fromfile(f, dtype=self.dtype)
  4202. assert_array_equal(y, self.x.flat)
  4203. def test_file_position_after_fromfile(self):
  4204. # gh-4118
  4205. sizes = [io.DEFAULT_BUFFER_SIZE//8,
  4206. io.DEFAULT_BUFFER_SIZE,
  4207. io.DEFAULT_BUFFER_SIZE*8]
  4208. for size in sizes:
  4209. with open(self.filename, 'wb') as f:
  4210. f.seek(size-1)
  4211. f.write(b'\0')
  4212. for mode in ['rb', 'r+b']:
  4213. err_msg = "%d %s" % (size, mode)
  4214. with open(self.filename, mode) as f:
  4215. f.read(2)
  4216. np.fromfile(f, dtype=np.float64, count=1)
  4217. pos = f.tell()
  4218. assert_equal(pos, 10, err_msg=err_msg)
  4219. def test_file_position_after_tofile(self):
  4220. # gh-4118
  4221. sizes = [io.DEFAULT_BUFFER_SIZE//8,
  4222. io.DEFAULT_BUFFER_SIZE,
  4223. io.DEFAULT_BUFFER_SIZE*8]
  4224. for size in sizes:
  4225. err_msg = "%d" % (size,)
  4226. with open(self.filename, 'wb') as f:
  4227. f.seek(size-1)
  4228. f.write(b'\0')
  4229. f.seek(10)
  4230. f.write(b'12')
  4231. np.array([0], dtype=np.float64).tofile(f)
  4232. pos = f.tell()
  4233. assert_equal(pos, 10 + 2 + 8, err_msg=err_msg)
  4234. with open(self.filename, 'r+b') as f:
  4235. f.read(2)
  4236. f.seek(0, 1) # seek between read&write required by ANSI C
  4237. np.array([0], dtype=np.float64).tofile(f)
  4238. pos = f.tell()
  4239. assert_equal(pos, 10, err_msg=err_msg)
  4240. def test_load_object_array_fromfile(self):
  4241. # gh-12300
  4242. with open(self.filename, 'w') as f:
  4243. # Ensure we have a file with consistent contents
  4244. pass
  4245. with open(self.filename, 'rb') as f:
  4246. assert_raises_regex(ValueError, "Cannot read into object array",
  4247. np.fromfile, f, dtype=object)
  4248. assert_raises_regex(ValueError, "Cannot read into object array",
  4249. np.fromfile, self.filename, dtype=object)
  4250. def test_fromfile_offset(self):
  4251. with open(self.filename, 'wb') as f:
  4252. self.x.tofile(f)
  4253. with open(self.filename, 'rb') as f:
  4254. y = np.fromfile(f, dtype=self.dtype, offset=0)
  4255. assert_array_equal(y, self.x.flat)
  4256. with open(self.filename, 'rb') as f:
  4257. count_items = len(self.x.flat) // 8
  4258. offset_items = len(self.x.flat) // 4
  4259. offset_bytes = self.dtype.itemsize * offset_items
  4260. y = np.fromfile(f, dtype=self.dtype, count=count_items, offset=offset_bytes)
  4261. assert_array_equal(y, self.x.flat[offset_items:offset_items+count_items])
  4262. # subsequent seeks should stack
  4263. offset_bytes = self.dtype.itemsize
  4264. z = np.fromfile(f, dtype=self.dtype, offset=offset_bytes)
  4265. assert_array_equal(z, self.x.flat[offset_items+count_items+1:])
  4266. with open(self.filename, 'wb') as f:
  4267. self.x.tofile(f, sep=",")
  4268. with open(self.filename, 'rb') as f:
  4269. assert_raises_regex(
  4270. TypeError,
  4271. "'offset' argument only permitted for binary files",
  4272. np.fromfile, self.filename, dtype=self.dtype,
  4273. sep=",", offset=1)
  4274. def _check_from(self, s, value, **kw):
  4275. if 'sep' not in kw:
  4276. y = np.frombuffer(s, **kw)
  4277. else:
  4278. y = np.fromstring(s, **kw)
  4279. assert_array_equal(y, value)
  4280. with open(self.filename, 'wb') as f:
  4281. f.write(s)
  4282. y = np.fromfile(self.filename, **kw)
  4283. assert_array_equal(y, value)
  4284. def test_nan(self):
  4285. self._check_from(
  4286. b"nan +nan -nan NaN nan(foo) +NaN(BAR) -NAN(q_u_u_x_)",
  4287. [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan],
  4288. sep=' ')
  4289. def test_inf(self):
  4290. self._check_from(
  4291. b"inf +inf -inf infinity -Infinity iNfInItY -inF",
  4292. [np.inf, np.inf, -np.inf, np.inf, -np.inf, np.inf, -np.inf],
  4293. sep=' ')
  4294. def test_numbers(self):
  4295. self._check_from(b"1.234 -1.234 .3 .3e55 -123133.1231e+133",
  4296. [1.234, -1.234, .3, .3e55, -123133.1231e+133], sep=' ')
  4297. def test_binary(self):
  4298. self._check_from(b'\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@',
  4299. np.array([1, 2, 3, 4]),
  4300. dtype='<f4')
  4301. @pytest.mark.slow # takes > 1 minute on mechanical hard drive
  4302. def test_big_binary(self):
  4303. """Test workarounds for 32-bit limited fwrite, fseek, and ftell
  4304. calls in windows. These normally would hang doing something like this.
  4305. See http://projects.scipy.org/numpy/ticket/1660"""
  4306. if sys.platform != 'win32':
  4307. return
  4308. try:
  4309. # before workarounds, only up to 2**32-1 worked
  4310. fourgbplus = 2**32 + 2**16
  4311. testbytes = np.arange(8, dtype=np.int8)
  4312. n = len(testbytes)
  4313. flike = tempfile.NamedTemporaryFile()
  4314. f = flike.file
  4315. np.tile(testbytes, fourgbplus // testbytes.nbytes).tofile(f)
  4316. flike.seek(0)
  4317. a = np.fromfile(f, dtype=np.int8)
  4318. flike.close()
  4319. assert_(len(a) == fourgbplus)
  4320. # check only start and end for speed:
  4321. assert_((a[:n] == testbytes).all())
  4322. assert_((a[-n:] == testbytes).all())
  4323. except (MemoryError, ValueError):
  4324. pass
  4325. def test_string(self):
  4326. self._check_from(b'1,2,3,4', [1., 2., 3., 4.], sep=',')
  4327. def test_counted_string(self):
  4328. self._check_from(b'1,2,3,4', [1., 2., 3., 4.], count=4, sep=',')
  4329. self._check_from(b'1,2,3,4', [1., 2., 3.], count=3, sep=',')
  4330. self._check_from(b'1,2,3,4', [1., 2., 3., 4.], count=-1, sep=',')
  4331. def test_string_with_ws(self):
  4332. self._check_from(b'1 2 3 4 ', [1, 2, 3, 4], dtype=int, sep=' ')
  4333. def test_counted_string_with_ws(self):
  4334. self._check_from(b'1 2 3 4 ', [1, 2, 3], count=3, dtype=int,
  4335. sep=' ')
  4336. def test_ascii(self):
  4337. self._check_from(b'1 , 2 , 3 , 4', [1., 2., 3., 4.], sep=',')
  4338. self._check_from(b'1,2,3,4', [1., 2., 3., 4.], dtype=float, sep=',')
  4339. def test_malformed(self):
  4340. with assert_warns(DeprecationWarning):
  4341. self._check_from(b'1.234 1,234', [1.234, 1.], sep=' ')
  4342. def test_long_sep(self):
  4343. self._check_from(b'1_x_3_x_4_x_5', [1, 3, 4, 5], sep='_x_')
  4344. def test_dtype(self):
  4345. v = np.array([1, 2, 3, 4], dtype=np.int_)
  4346. self._check_from(b'1,2,3,4', v, sep=',', dtype=np.int_)
  4347. def test_dtype_bool(self):
  4348. # can't use _check_from because fromstring can't handle True/False
  4349. v = np.array([True, False, True, False], dtype=np.bool_)
  4350. s = b'1,0,-2.3,0'
  4351. with open(self.filename, 'wb') as f:
  4352. f.write(s)
  4353. y = np.fromfile(self.filename, sep=',', dtype=np.bool_)
  4354. assert_(y.dtype == '?')
  4355. assert_array_equal(y, v)
  4356. def test_tofile_sep(self):
  4357. x = np.array([1.51, 2, 3.51, 4], dtype=float)
  4358. with open(self.filename, 'w') as f:
  4359. x.tofile(f, sep=',')
  4360. with open(self.filename, 'r') as f:
  4361. s = f.read()
  4362. #assert_equal(s, '1.51,2.0,3.51,4.0')
  4363. y = np.array([float(p) for p in s.split(',')])
  4364. assert_array_equal(x,y)
  4365. def test_tofile_format(self):
  4366. x = np.array([1.51, 2, 3.51, 4], dtype=float)
  4367. with open(self.filename, 'w') as f:
  4368. x.tofile(f, sep=',', format='%.2f')
  4369. with open(self.filename, 'r') as f:
  4370. s = f.read()
  4371. assert_equal(s, '1.51,2.00,3.51,4.00')
  4372. def test_tofile_cleanup(self):
  4373. x = np.zeros((10), dtype=object)
  4374. with open(self.filename, 'wb') as f:
  4375. assert_raises(IOError, lambda: x.tofile(f, sep=''))
  4376. # Dup-ed file handle should be closed or remove will fail on Windows OS
  4377. os.remove(self.filename)
  4378. # Also make sure that we close the Python handle
  4379. assert_raises(IOError, lambda: x.tofile(self.filename))
  4380. os.remove(self.filename)
  4381. def test_locale(self):
  4382. with CommaDecimalPointLocale():
  4383. self.test_numbers()
  4384. self.test_nan()
  4385. self.test_inf()
  4386. self.test_counted_string()
  4387. self.test_ascii()
  4388. self.test_malformed()
  4389. self.test_tofile_sep()
  4390. self.test_tofile_format()
  4391. def test_fromfile_subarray_binary(self):
  4392. # Test subarray dtypes which are absorbed into the shape
  4393. x = np.arange(24, dtype="i4").reshape(2, 3, 4)
  4394. x.tofile(self.filename)
  4395. res = np.fromfile(self.filename, dtype="(3,4)i4")
  4396. assert_array_equal(x, res)
  4397. x_str = x.tobytes()
  4398. with assert_warns(DeprecationWarning):
  4399. # binary fromstring is deprecated
  4400. res = np.fromstring(x_str, dtype="(3,4)i4")
  4401. assert_array_equal(x, res)
  4402. def test_parsing_subarray_unsupported(self):
  4403. # We currently do not support parsing subarray dtypes
  4404. data = "12,42,13," * 50
  4405. with pytest.raises(ValueError):
  4406. expected = np.fromstring(data, dtype="(3,)i", sep=",")
  4407. with open(self.filename, "w") as f:
  4408. f.write(data)
  4409. with pytest.raises(ValueError):
  4410. np.fromfile(self.filename, dtype="(3,)i", sep=",")
  4411. def test_read_shorter_than_count_subarray(self):
  4412. # Test that requesting more values does not cause any problems
  4413. # in conjuction with subarray dimensions being absored into the
  4414. # array dimension.
  4415. expected = np.arange(511 * 10, dtype="i").reshape(-1, 10)
  4416. binary = expected.tobytes()
  4417. with pytest.raises(ValueError):
  4418. with pytest.warns(DeprecationWarning):
  4419. np.fromstring(binary, dtype="(10,)i", count=10000)
  4420. expected.tofile(self.filename)
  4421. res = np.fromfile(self.filename, dtype="(10,)i", count=10000)
  4422. assert_array_equal(res, expected)
  4423. class TestFromBuffer:
  4424. @pytest.mark.parametrize('byteorder', ['<', '>'])
  4425. @pytest.mark.parametrize('dtype', [float, int, complex])
  4426. def test_basic(self, byteorder, dtype):
  4427. dt = np.dtype(dtype).newbyteorder(byteorder)
  4428. x = (np.random.random((4, 7)) * 5).astype(dt)
  4429. buf = x.tobytes()
  4430. assert_array_equal(np.frombuffer(buf, dtype=dt), x.flat)
  4431. def test_empty(self):
  4432. assert_array_equal(np.frombuffer(b''), np.array([]))
  4433. class TestFlat:
  4434. def setup(self):
  4435. a0 = np.arange(20.0)
  4436. a = a0.reshape(4, 5)
  4437. a0.shape = (4, 5)
  4438. a.flags.writeable = False
  4439. self.a = a
  4440. self.b = a[::2, ::2]
  4441. self.a0 = a0
  4442. self.b0 = a0[::2, ::2]
  4443. def test_contiguous(self):
  4444. testpassed = False
  4445. try:
  4446. self.a.flat[12] = 100.0
  4447. except ValueError:
  4448. testpassed = True
  4449. assert_(testpassed)
  4450. assert_(self.a.flat[12] == 12.0)
  4451. def test_discontiguous(self):
  4452. testpassed = False
  4453. try:
  4454. self.b.flat[4] = 100.0
  4455. except ValueError:
  4456. testpassed = True
  4457. assert_(testpassed)
  4458. assert_(self.b.flat[4] == 12.0)
  4459. def test___array__(self):
  4460. c = self.a.flat.__array__()
  4461. d = self.b.flat.__array__()
  4462. e = self.a0.flat.__array__()
  4463. f = self.b0.flat.__array__()
  4464. assert_(c.flags.writeable is False)
  4465. assert_(d.flags.writeable is False)
  4466. # for 1.14 all are set to non-writeable on the way to replacing the
  4467. # UPDATEIFCOPY array returned for non-contiguous arrays.
  4468. assert_(e.flags.writeable is True)
  4469. assert_(f.flags.writeable is False)
  4470. with assert_warns(DeprecationWarning):
  4471. assert_(c.flags.updateifcopy is False)
  4472. with assert_warns(DeprecationWarning):
  4473. assert_(d.flags.updateifcopy is False)
  4474. with assert_warns(DeprecationWarning):
  4475. assert_(e.flags.updateifcopy is False)
  4476. with assert_warns(DeprecationWarning):
  4477. # UPDATEIFCOPY is removed.
  4478. assert_(f.flags.updateifcopy is False)
  4479. assert_(c.flags.writebackifcopy is False)
  4480. assert_(d.flags.writebackifcopy is False)
  4481. assert_(e.flags.writebackifcopy is False)
  4482. assert_(f.flags.writebackifcopy is False)
  4483. @pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
  4484. def test_refcount(self):
  4485. # includes regression test for reference count error gh-13165
  4486. inds = [np.intp(0), np.array([True]*self.a.size), np.array([0]), None]
  4487. indtype = np.dtype(np.intp)
  4488. rc_indtype = sys.getrefcount(indtype)
  4489. for ind in inds:
  4490. rc_ind = sys.getrefcount(ind)
  4491. for _ in range(100):
  4492. try:
  4493. self.a.flat[ind]
  4494. except IndexError:
  4495. pass
  4496. assert_(abs(sys.getrefcount(ind) - rc_ind) < 50)
  4497. assert_(abs(sys.getrefcount(indtype) - rc_indtype) < 50)
  4498. class TestResize:
  4499. @_no_tracing
  4500. def test_basic(self):
  4501. x = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
  4502. if IS_PYPY:
  4503. x.resize((5, 5), refcheck=False)
  4504. else:
  4505. x.resize((5, 5))
  4506. assert_array_equal(x.flat[:9],
  4507. np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]).flat)
  4508. assert_array_equal(x[9:].flat, 0)
  4509. def test_check_reference(self):
  4510. x = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
  4511. y = x
  4512. assert_raises(ValueError, x.resize, (5, 1))
  4513. del y # avoid pyflakes unused variable warning.
  4514. @_no_tracing
  4515. def test_int_shape(self):
  4516. x = np.eye(3)
  4517. if IS_PYPY:
  4518. x.resize(3, refcheck=False)
  4519. else:
  4520. x.resize(3)
  4521. assert_array_equal(x, np.eye(3)[0,:])
  4522. def test_none_shape(self):
  4523. x = np.eye(3)
  4524. x.resize(None)
  4525. assert_array_equal(x, np.eye(3))
  4526. x.resize()
  4527. assert_array_equal(x, np.eye(3))
  4528. def test_0d_shape(self):
  4529. # to it multiple times to test it does not break alloc cache gh-9216
  4530. for i in range(10):
  4531. x = np.empty((1,))
  4532. x.resize(())
  4533. assert_equal(x.shape, ())
  4534. assert_equal(x.size, 1)
  4535. x = np.empty(())
  4536. x.resize((1,))
  4537. assert_equal(x.shape, (1,))
  4538. assert_equal(x.size, 1)
  4539. def test_invalid_arguments(self):
  4540. assert_raises(TypeError, np.eye(3).resize, 'hi')
  4541. assert_raises(ValueError, np.eye(3).resize, -1)
  4542. assert_raises(TypeError, np.eye(3).resize, order=1)
  4543. assert_raises(TypeError, np.eye(3).resize, refcheck='hi')
  4544. @_no_tracing
  4545. def test_freeform_shape(self):
  4546. x = np.eye(3)
  4547. if IS_PYPY:
  4548. x.resize(3, 2, 1, refcheck=False)
  4549. else:
  4550. x.resize(3, 2, 1)
  4551. assert_(x.shape == (3, 2, 1))
  4552. @_no_tracing
  4553. def test_zeros_appended(self):
  4554. x = np.eye(3)
  4555. if IS_PYPY:
  4556. x.resize(2, 3, 3, refcheck=False)
  4557. else:
  4558. x.resize(2, 3, 3)
  4559. assert_array_equal(x[0], np.eye(3))
  4560. assert_array_equal(x[1], np.zeros((3, 3)))
  4561. @_no_tracing
  4562. def test_obj_obj(self):
  4563. # check memory is initialized on resize, gh-4857
  4564. a = np.ones(10, dtype=[('k', object, 2)])
  4565. if IS_PYPY:
  4566. a.resize(15, refcheck=False)
  4567. else:
  4568. a.resize(15,)
  4569. assert_equal(a.shape, (15,))
  4570. assert_array_equal(a['k'][-5:], 0)
  4571. assert_array_equal(a['k'][:-5], 1)
  4572. def test_empty_view(self):
  4573. # check that sizes containing a zero don't trigger a reallocate for
  4574. # already empty arrays
  4575. x = np.zeros((10, 0), int)
  4576. x_view = x[...]
  4577. x_view.resize((0, 10))
  4578. x_view.resize((0, 100))
  4579. def test_check_weakref(self):
  4580. x = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
  4581. xref = weakref.ref(x)
  4582. assert_raises(ValueError, x.resize, (5, 1))
  4583. del xref # avoid pyflakes unused variable warning.
  4584. class TestRecord:
  4585. def test_field_rename(self):
  4586. dt = np.dtype([('f', float), ('i', int)])
  4587. dt.names = ['p', 'q']
  4588. assert_equal(dt.names, ['p', 'q'])
  4589. def test_multiple_field_name_occurrence(self):
  4590. def test_dtype_init():
  4591. np.dtype([("A", "f8"), ("B", "f8"), ("A", "f8")])
  4592. # Error raised when multiple fields have the same name
  4593. assert_raises(ValueError, test_dtype_init)
  4594. def test_bytes_fields(self):
  4595. # Bytes are not allowed in field names and not recognized in titles
  4596. # on Py3
  4597. assert_raises(TypeError, np.dtype, [(b'a', int)])
  4598. assert_raises(TypeError, np.dtype, [(('b', b'a'), int)])
  4599. dt = np.dtype([((b'a', 'b'), int)])
  4600. assert_raises(TypeError, dt.__getitem__, b'a')
  4601. x = np.array([(1,), (2,), (3,)], dtype=dt)
  4602. assert_raises(IndexError, x.__getitem__, b'a')
  4603. y = x[0]
  4604. assert_raises(IndexError, y.__getitem__, b'a')
  4605. def test_multiple_field_name_unicode(self):
  4606. def test_dtype_unicode():
  4607. np.dtype([("\u20B9", "f8"), ("B", "f8"), ("\u20B9", "f8")])
  4608. # Error raised when multiple fields have the same name(unicode included)
  4609. assert_raises(ValueError, test_dtype_unicode)
  4610. def test_fromarrays_unicode(self):
  4611. # A single name string provided to fromarrays() is allowed to be unicode
  4612. # on both Python 2 and 3:
  4613. x = np.core.records.fromarrays([[0], [1]], names=u'a,b', formats=u'i4,i4')
  4614. assert_equal(x['a'][0], 0)
  4615. assert_equal(x['b'][0], 1)
  4616. def test_unicode_order(self):
  4617. # Test that we can sort with order as a unicode field name in both Python 2 and
  4618. # 3:
  4619. name = u'b'
  4620. x = np.array([1, 3, 2], dtype=[(name, int)])
  4621. x.sort(order=name)
  4622. assert_equal(x[u'b'], np.array([1, 2, 3]))
  4623. def test_field_names(self):
  4624. # Test unicode and 8-bit / byte strings can be used
  4625. a = np.zeros((1,), dtype=[('f1', 'i4'),
  4626. ('f2', 'i4'),
  4627. ('f3', [('sf1', 'i4')])])
  4628. # byte string indexing fails gracefully
  4629. assert_raises(IndexError, a.__setitem__, b'f1', 1)
  4630. assert_raises(IndexError, a.__getitem__, b'f1')
  4631. assert_raises(IndexError, a['f1'].__setitem__, b'sf1', 1)
  4632. assert_raises(IndexError, a['f1'].__getitem__, b'sf1')
  4633. b = a.copy()
  4634. fn1 = str('f1')
  4635. b[fn1] = 1
  4636. assert_equal(b[fn1], 1)
  4637. fnn = str('not at all')
  4638. assert_raises(ValueError, b.__setitem__, fnn, 1)
  4639. assert_raises(ValueError, b.__getitem__, fnn)
  4640. b[0][fn1] = 2
  4641. assert_equal(b[fn1], 2)
  4642. # Subfield
  4643. assert_raises(ValueError, b[0].__setitem__, fnn, 1)
  4644. assert_raises(ValueError, b[0].__getitem__, fnn)
  4645. # Subfield
  4646. fn3 = str('f3')
  4647. sfn1 = str('sf1')
  4648. b[fn3][sfn1] = 1
  4649. assert_equal(b[fn3][sfn1], 1)
  4650. assert_raises(ValueError, b[fn3].__setitem__, fnn, 1)
  4651. assert_raises(ValueError, b[fn3].__getitem__, fnn)
  4652. # multiple subfields
  4653. fn2 = str('f2')
  4654. b[fn2] = 3
  4655. assert_equal(b[['f1', 'f2']][0].tolist(), (2, 3))
  4656. assert_equal(b[['f2', 'f1']][0].tolist(), (3, 2))
  4657. assert_equal(b[['f1', 'f3']][0].tolist(), (2, (1,)))
  4658. # non-ascii unicode field indexing is well behaved
  4659. assert_raises(ValueError, a.__setitem__, u'\u03e0', 1)
  4660. assert_raises(ValueError, a.__getitem__, u'\u03e0')
  4661. def test_record_hash(self):
  4662. a = np.array([(1, 2), (1, 2)], dtype='i1,i2')
  4663. a.flags.writeable = False
  4664. b = np.array([(1, 2), (3, 4)], dtype=[('num1', 'i1'), ('num2', 'i2')])
  4665. b.flags.writeable = False
  4666. c = np.array([(1, 2), (3, 4)], dtype='i1,i2')
  4667. c.flags.writeable = False
  4668. assert_(hash(a[0]) == hash(a[1]))
  4669. assert_(hash(a[0]) == hash(b[0]))
  4670. assert_(hash(a[0]) != hash(b[1]))
  4671. assert_(hash(c[0]) == hash(a[0]) and c[0] == a[0])
  4672. def test_record_no_hash(self):
  4673. a = np.array([(1, 2), (1, 2)], dtype='i1,i2')
  4674. assert_raises(TypeError, hash, a[0])
  4675. def test_empty_structure_creation(self):
  4676. # make sure these do not raise errors (gh-5631)
  4677. np.array([()], dtype={'names': [], 'formats': [],
  4678. 'offsets': [], 'itemsize': 12})
  4679. np.array([(), (), (), (), ()], dtype={'names': [], 'formats': [],
  4680. 'offsets': [], 'itemsize': 12})
  4681. def test_multifield_indexing_view(self):
  4682. a = np.ones(3, dtype=[('a', 'i4'), ('b', 'f4'), ('c', 'u4')])
  4683. v = a[['a', 'c']]
  4684. assert_(v.base is a)
  4685. assert_(v.dtype == np.dtype({'names': ['a', 'c'],
  4686. 'formats': ['i4', 'u4'],
  4687. 'offsets': [0, 8]}))
  4688. v[:] = (4,5)
  4689. assert_equal(a[0].item(), (4, 1, 5))
  4690. class TestView:
  4691. def test_basic(self):
  4692. x = np.array([(1, 2, 3, 4), (5, 6, 7, 8)],
  4693. dtype=[('r', np.int8), ('g', np.int8),
  4694. ('b', np.int8), ('a', np.int8)])
  4695. # We must be specific about the endianness here:
  4696. y = x.view(dtype='<i4')
  4697. # ... and again without the keyword.
  4698. z = x.view('<i4')
  4699. assert_array_equal(y, z)
  4700. assert_array_equal(y, [67305985, 134678021])
  4701. def _mean(a, **args):
  4702. return a.mean(**args)
  4703. def _var(a, **args):
  4704. return a.var(**args)
  4705. def _std(a, **args):
  4706. return a.std(**args)
  4707. class TestStats:
  4708. funcs = [_mean, _var, _std]
  4709. def setup(self):
  4710. np.random.seed(range(3))
  4711. self.rmat = np.random.random((4, 5))
  4712. self.cmat = self.rmat + 1j * self.rmat
  4713. self.omat = np.array([Decimal(repr(r)) for r in self.rmat.flat])
  4714. self.omat = self.omat.reshape(4, 5)
  4715. def test_python_type(self):
  4716. for x in (np.float16(1.), 1, 1., 1+0j):
  4717. assert_equal(np.mean([x]), 1.)
  4718. assert_equal(np.std([x]), 0.)
  4719. assert_equal(np.var([x]), 0.)
  4720. def test_keepdims(self):
  4721. mat = np.eye(3)
  4722. for f in self.funcs:
  4723. for axis in [0, 1]:
  4724. res = f(mat, axis=axis, keepdims=True)
  4725. assert_(res.ndim == mat.ndim)
  4726. assert_(res.shape[axis] == 1)
  4727. for axis in [None]:
  4728. res = f(mat, axis=axis, keepdims=True)
  4729. assert_(res.shape == (1, 1))
  4730. def test_out(self):
  4731. mat = np.eye(3)
  4732. for f in self.funcs:
  4733. out = np.zeros(3)
  4734. tgt = f(mat, axis=1)
  4735. res = f(mat, axis=1, out=out)
  4736. assert_almost_equal(res, out)
  4737. assert_almost_equal(res, tgt)
  4738. out = np.empty(2)
  4739. assert_raises(ValueError, f, mat, axis=1, out=out)
  4740. out = np.empty((2, 2))
  4741. assert_raises(ValueError, f, mat, axis=1, out=out)
  4742. def test_dtype_from_input(self):
  4743. icodes = np.typecodes['AllInteger']
  4744. fcodes = np.typecodes['AllFloat']
  4745. # object type
  4746. for f in self.funcs:
  4747. mat = np.array([[Decimal(1)]*3]*3)
  4748. tgt = mat.dtype.type
  4749. res = f(mat, axis=1).dtype.type
  4750. assert_(res is tgt)
  4751. # scalar case
  4752. res = type(f(mat, axis=None))
  4753. assert_(res is Decimal)
  4754. # integer types
  4755. for f in self.funcs:
  4756. for c in icodes:
  4757. mat = np.eye(3, dtype=c)
  4758. tgt = np.float64
  4759. res = f(mat, axis=1).dtype.type
  4760. assert_(res is tgt)
  4761. # scalar case
  4762. res = f(mat, axis=None).dtype.type
  4763. assert_(res is tgt)
  4764. # mean for float types
  4765. for f in [_mean]:
  4766. for c in fcodes:
  4767. mat = np.eye(3, dtype=c)
  4768. tgt = mat.dtype.type
  4769. res = f(mat, axis=1).dtype.type
  4770. assert_(res is tgt)
  4771. # scalar case
  4772. res = f(mat, axis=None).dtype.type
  4773. assert_(res is tgt)
  4774. # var, std for float types
  4775. for f in [_var, _std]:
  4776. for c in fcodes:
  4777. mat = np.eye(3, dtype=c)
  4778. # deal with complex types
  4779. tgt = mat.real.dtype.type
  4780. res = f(mat, axis=1).dtype.type
  4781. assert_(res is tgt)
  4782. # scalar case
  4783. res = f(mat, axis=None).dtype.type
  4784. assert_(res is tgt)
  4785. def test_dtype_from_dtype(self):
  4786. mat = np.eye(3)
  4787. # stats for integer types
  4788. # FIXME:
  4789. # this needs definition as there are lots places along the line
  4790. # where type casting may take place.
  4791. # for f in self.funcs:
  4792. # for c in np.typecodes['AllInteger']:
  4793. # tgt = np.dtype(c).type
  4794. # res = f(mat, axis=1, dtype=c).dtype.type
  4795. # assert_(res is tgt)
  4796. # # scalar case
  4797. # res = f(mat, axis=None, dtype=c).dtype.type
  4798. # assert_(res is tgt)
  4799. # stats for float types
  4800. for f in self.funcs:
  4801. for c in np.typecodes['AllFloat']:
  4802. tgt = np.dtype(c).type
  4803. res = f(mat, axis=1, dtype=c).dtype.type
  4804. assert_(res is tgt)
  4805. # scalar case
  4806. res = f(mat, axis=None, dtype=c).dtype.type
  4807. assert_(res is tgt)
  4808. def test_ddof(self):
  4809. for f in [_var]:
  4810. for ddof in range(3):
  4811. dim = self.rmat.shape[1]
  4812. tgt = f(self.rmat, axis=1) * dim
  4813. res = f(self.rmat, axis=1, ddof=ddof) * (dim - ddof)
  4814. for f in [_std]:
  4815. for ddof in range(3):
  4816. dim = self.rmat.shape[1]
  4817. tgt = f(self.rmat, axis=1) * np.sqrt(dim)
  4818. res = f(self.rmat, axis=1, ddof=ddof) * np.sqrt(dim - ddof)
  4819. assert_almost_equal(res, tgt)
  4820. assert_almost_equal(res, tgt)
  4821. def test_ddof_too_big(self):
  4822. dim = self.rmat.shape[1]
  4823. for f in [_var, _std]:
  4824. for ddof in range(dim, dim + 2):
  4825. with warnings.catch_warnings(record=True) as w:
  4826. warnings.simplefilter('always')
  4827. res = f(self.rmat, axis=1, ddof=ddof)
  4828. assert_(not (res < 0).any())
  4829. assert_(len(w) > 0)
  4830. assert_(issubclass(w[0].category, RuntimeWarning))
  4831. def test_empty(self):
  4832. A = np.zeros((0, 3))
  4833. for f in self.funcs:
  4834. for axis in [0, None]:
  4835. with warnings.catch_warnings(record=True) as w:
  4836. warnings.simplefilter('always')
  4837. assert_(np.isnan(f(A, axis=axis)).all())
  4838. assert_(len(w) > 0)
  4839. assert_(issubclass(w[0].category, RuntimeWarning))
  4840. for axis in [1]:
  4841. with warnings.catch_warnings(record=True) as w:
  4842. warnings.simplefilter('always')
  4843. assert_equal(f(A, axis=axis), np.zeros([]))
  4844. def test_mean_values(self):
  4845. for mat in [self.rmat, self.cmat, self.omat]:
  4846. for axis in [0, 1]:
  4847. tgt = mat.sum(axis=axis)
  4848. res = _mean(mat, axis=axis) * mat.shape[axis]
  4849. assert_almost_equal(res, tgt)
  4850. for axis in [None]:
  4851. tgt = mat.sum(axis=axis)
  4852. res = _mean(mat, axis=axis) * np.prod(mat.shape)
  4853. assert_almost_equal(res, tgt)
  4854. def test_mean_float16(self):
  4855. # This fail if the sum inside mean is done in float16 instead
  4856. # of float32.
  4857. assert_(_mean(np.ones(100000, dtype='float16')) == 1)
  4858. def test_mean_axis_error(self):
  4859. # Ensure that AxisError is raised instead of IndexError when axis is
  4860. # out of bounds, see gh-15817.
  4861. with assert_raises(np.core._exceptions.AxisError):
  4862. np.arange(10).mean(axis=2)
  4863. def test_mean_where(self):
  4864. a = np.arange(16).reshape((4, 4))
  4865. wh_full = np.array([[False, True, False, True],
  4866. [True, False, True, False],
  4867. [True, True, False, False],
  4868. [False, False, True, True]])
  4869. wh_partial = np.array([[False],
  4870. [True],
  4871. [True],
  4872. [False]])
  4873. _cases = [(1, True, [1.5, 5.5, 9.5, 13.5]),
  4874. (0, wh_full, [6., 5., 10., 9.]),
  4875. (1, wh_full, [2., 5., 8.5, 14.5]),
  4876. (0, wh_partial, [6., 7., 8., 9.])]
  4877. for _ax, _wh, _res in _cases:
  4878. assert_allclose(a.mean(axis=_ax, where=_wh),
  4879. np.array(_res))
  4880. assert_allclose(np.mean(a, axis=_ax, where=_wh),
  4881. np.array(_res))
  4882. a3d = np.arange(16).reshape((2, 2, 4))
  4883. _wh_partial = np.array([False, True, True, False])
  4884. _res = [[1.5, 5.5], [9.5, 13.5]]
  4885. assert_allclose(a3d.mean(axis=2, where=_wh_partial),
  4886. np.array(_res))
  4887. assert_allclose(np.mean(a3d, axis=2, where=_wh_partial),
  4888. np.array(_res))
  4889. with pytest.warns(RuntimeWarning) as w:
  4890. assert_allclose(a.mean(axis=1, where=wh_partial),
  4891. np.array([np.nan, 5.5, 9.5, np.nan]))
  4892. with pytest.warns(RuntimeWarning) as w:
  4893. assert_equal(a.mean(where=False), np.nan)
  4894. with pytest.warns(RuntimeWarning) as w:
  4895. assert_equal(np.mean(a, where=False), np.nan)
  4896. def test_var_values(self):
  4897. for mat in [self.rmat, self.cmat, self.omat]:
  4898. for axis in [0, 1, None]:
  4899. msqr = _mean(mat * mat.conj(), axis=axis)
  4900. mean = _mean(mat, axis=axis)
  4901. tgt = msqr - mean * mean.conjugate()
  4902. res = _var(mat, axis=axis)
  4903. assert_almost_equal(res, tgt)
  4904. @pytest.mark.parametrize(('complex_dtype', 'ndec'), (
  4905. ('complex64', 6),
  4906. ('complex128', 7),
  4907. ('clongdouble', 7),
  4908. ))
  4909. def test_var_complex_values(self, complex_dtype, ndec):
  4910. # Test fast-paths for every builtin complex type
  4911. for axis in [0, 1, None]:
  4912. mat = self.cmat.copy().astype(complex_dtype)
  4913. msqr = _mean(mat * mat.conj(), axis=axis)
  4914. mean = _mean(mat, axis=axis)
  4915. tgt = msqr - mean * mean.conjugate()
  4916. res = _var(mat, axis=axis)
  4917. assert_almost_equal(res, tgt, decimal=ndec)
  4918. def test_var_dimensions(self):
  4919. # _var paths for complex number introduce additions on views that
  4920. # increase dimensions. Ensure this generalizes to higher dims
  4921. mat = np.stack([self.cmat]*3)
  4922. for axis in [0, 1, 2, -1, None]:
  4923. msqr = _mean(mat * mat.conj(), axis=axis)
  4924. mean = _mean(mat, axis=axis)
  4925. tgt = msqr - mean * mean.conjugate()
  4926. res = _var(mat, axis=axis)
  4927. assert_almost_equal(res, tgt)
  4928. def test_var_complex_byteorder(self):
  4929. # Test that var fast-path does not cause failures for complex arrays
  4930. # with non-native byteorder
  4931. cmat = self.cmat.copy().astype('complex128')
  4932. cmat_swapped = cmat.astype(cmat.dtype.newbyteorder())
  4933. assert_almost_equal(cmat.var(), cmat_swapped.var())
  4934. def test_var_axis_error(self):
  4935. # Ensure that AxisError is raised instead of IndexError when axis is
  4936. # out of bounds, see gh-15817.
  4937. with assert_raises(np.core._exceptions.AxisError):
  4938. np.arange(10).var(axis=2)
  4939. def test_var_where(self):
  4940. a = np.arange(25).reshape((5, 5))
  4941. wh_full = np.array([[False, True, False, True, True],
  4942. [True, False, True, True, False],
  4943. [True, True, False, False, True],
  4944. [False, True, True, False, True],
  4945. [True, False, True, True, False]])
  4946. wh_partial = np.array([[False],
  4947. [True],
  4948. [True],
  4949. [False],
  4950. [True]])
  4951. _cases = [(0, True, [50., 50., 50., 50., 50.]),
  4952. (1, True, [2., 2., 2., 2., 2.])]
  4953. for _ax, _wh, _res in _cases:
  4954. assert_allclose(a.var(axis=_ax, where=_wh),
  4955. np.array(_res))
  4956. assert_allclose(np.var(a, axis=_ax, where=_wh),
  4957. np.array(_res))
  4958. a3d = np.arange(16).reshape((2, 2, 4))
  4959. _wh_partial = np.array([False, True, True, False])
  4960. _res = [[0.25, 0.25], [0.25, 0.25]]
  4961. assert_allclose(a3d.var(axis=2, where=_wh_partial),
  4962. np.array(_res))
  4963. assert_allclose(np.var(a3d, axis=2, where=_wh_partial),
  4964. np.array(_res))
  4965. assert_allclose(np.var(a, axis=1, where=wh_full),
  4966. np.var(a[wh_full].reshape((5, 3)), axis=1))
  4967. assert_allclose(np.var(a, axis=0, where=wh_partial),
  4968. np.var(a[wh_partial[:,0]], axis=0))
  4969. with pytest.warns(RuntimeWarning) as w:
  4970. assert_equal(a.var(where=False), np.nan)
  4971. with pytest.warns(RuntimeWarning) as w:
  4972. assert_equal(np.var(a, where=False), np.nan)
  4973. def test_std_values(self):
  4974. for mat in [self.rmat, self.cmat, self.omat]:
  4975. for axis in [0, 1, None]:
  4976. tgt = np.sqrt(_var(mat, axis=axis))
  4977. res = _std(mat, axis=axis)
  4978. assert_almost_equal(res, tgt)
  4979. def test_std_where(self):
  4980. a = np.arange(25).reshape((5,5))[::-1]
  4981. whf = np.array([[False, True, False, True, True],
  4982. [True, False, True, False, True],
  4983. [True, True, False, True, False],
  4984. [True, False, True, True, False],
  4985. [False, True, False, True, True]])
  4986. whp = np.array([[False],
  4987. [False],
  4988. [True],
  4989. [True],
  4990. [False]])
  4991. _cases = [
  4992. (0, True, 7.07106781*np.ones((5))),
  4993. (1, True, 1.41421356*np.ones((5))),
  4994. (0, whf,
  4995. np.array([4.0824829 , 8.16496581, 5., 7.39509973, 8.49836586])),
  4996. (0, whp, 2.5*np.ones((5)))
  4997. ]
  4998. for _ax, _wh, _res in _cases:
  4999. assert_allclose(a.std(axis=_ax, where=_wh), _res)
  5000. assert_allclose(np.std(a, axis=_ax, where=_wh), _res)
  5001. a3d = np.arange(16).reshape((2, 2, 4))
  5002. _wh_partial = np.array([False, True, True, False])
  5003. _res = [[0.5, 0.5], [0.5, 0.5]]
  5004. assert_allclose(a3d.std(axis=2, where=_wh_partial),
  5005. np.array(_res))
  5006. assert_allclose(np.std(a3d, axis=2, where=_wh_partial),
  5007. np.array(_res))
  5008. assert_allclose(a.std(axis=1, where=whf),
  5009. np.std(a[whf].reshape((5,3)), axis=1))
  5010. assert_allclose(np.std(a, axis=1, where=whf),
  5011. (a[whf].reshape((5,3))).std(axis=1))
  5012. assert_allclose(a.std(axis=0, where=whp),
  5013. np.std(a[whp[:,0]], axis=0))
  5014. assert_allclose(np.std(a, axis=0, where=whp),
  5015. (a[whp[:,0]]).std(axis=0))
  5016. with pytest.warns(RuntimeWarning) as w:
  5017. assert_equal(a.std(where=False), np.nan)
  5018. with pytest.warns(RuntimeWarning) as w:
  5019. assert_equal(np.std(a, where=False), np.nan)
  5020. def test_subclass(self):
  5021. class TestArray(np.ndarray):
  5022. def __new__(cls, data, info):
  5023. result = np.array(data)
  5024. result = result.view(cls)
  5025. result.info = info
  5026. return result
  5027. def __array_finalize__(self, obj):
  5028. self.info = getattr(obj, "info", '')
  5029. dat = TestArray([[1, 2, 3, 4], [5, 6, 7, 8]], 'jubba')
  5030. res = dat.mean(1)
  5031. assert_(res.info == dat.info)
  5032. res = dat.std(1)
  5033. assert_(res.info == dat.info)
  5034. res = dat.var(1)
  5035. assert_(res.info == dat.info)
  5036. class TestVdot:
  5037. def test_basic(self):
  5038. dt_numeric = np.typecodes['AllFloat'] + np.typecodes['AllInteger']
  5039. dt_complex = np.typecodes['Complex']
  5040. # test real
  5041. a = np.eye(3)
  5042. for dt in dt_numeric + 'O':
  5043. b = a.astype(dt)
  5044. res = np.vdot(b, b)
  5045. assert_(np.isscalar(res))
  5046. assert_equal(np.vdot(b, b), 3)
  5047. # test complex
  5048. a = np.eye(3) * 1j
  5049. for dt in dt_complex + 'O':
  5050. b = a.astype(dt)
  5051. res = np.vdot(b, b)
  5052. assert_(np.isscalar(res))
  5053. assert_equal(np.vdot(b, b), 3)
  5054. # test boolean
  5055. b = np.eye(3, dtype=bool)
  5056. res = np.vdot(b, b)
  5057. assert_(np.isscalar(res))
  5058. assert_equal(np.vdot(b, b), True)
  5059. def test_vdot_array_order(self):
  5060. a = np.array([[1, 2], [3, 4]], order='C')
  5061. b = np.array([[1, 2], [3, 4]], order='F')
  5062. res = np.vdot(a, a)
  5063. # integer arrays are exact
  5064. assert_equal(np.vdot(a, b), res)
  5065. assert_equal(np.vdot(b, a), res)
  5066. assert_equal(np.vdot(b, b), res)
  5067. def test_vdot_uncontiguous(self):
  5068. for size in [2, 1000]:
  5069. # Different sizes match different branches in vdot.
  5070. a = np.zeros((size, 2, 2))
  5071. b = np.zeros((size, 2, 2))
  5072. a[:, 0, 0] = np.arange(size)
  5073. b[:, 0, 0] = np.arange(size) + 1
  5074. # Make a and b uncontiguous:
  5075. a = a[..., 0]
  5076. b = b[..., 0]
  5077. assert_equal(np.vdot(a, b),
  5078. np.vdot(a.flatten(), b.flatten()))
  5079. assert_equal(np.vdot(a, b.copy()),
  5080. np.vdot(a.flatten(), b.flatten()))
  5081. assert_equal(np.vdot(a.copy(), b),
  5082. np.vdot(a.flatten(), b.flatten()))
  5083. assert_equal(np.vdot(a.copy('F'), b),
  5084. np.vdot(a.flatten(), b.flatten()))
  5085. assert_equal(np.vdot(a, b.copy('F')),
  5086. np.vdot(a.flatten(), b.flatten()))
  5087. class TestDot:
  5088. def setup(self):
  5089. np.random.seed(128)
  5090. self.A = np.random.rand(4, 2)
  5091. self.b1 = np.random.rand(2, 1)
  5092. self.b2 = np.random.rand(2)
  5093. self.b3 = np.random.rand(1, 2)
  5094. self.b4 = np.random.rand(4)
  5095. self.N = 7
  5096. def test_dotmatmat(self):
  5097. A = self.A
  5098. res = np.dot(A.transpose(), A)
  5099. tgt = np.array([[1.45046013, 0.86323640],
  5100. [0.86323640, 0.84934569]])
  5101. assert_almost_equal(res, tgt, decimal=self.N)
  5102. def test_dotmatvec(self):
  5103. A, b1 = self.A, self.b1
  5104. res = np.dot(A, b1)
  5105. tgt = np.array([[0.32114320], [0.04889721],
  5106. [0.15696029], [0.33612621]])
  5107. assert_almost_equal(res, tgt, decimal=self.N)
  5108. def test_dotmatvec2(self):
  5109. A, b2 = self.A, self.b2
  5110. res = np.dot(A, b2)
  5111. tgt = np.array([0.29677940, 0.04518649, 0.14468333, 0.31039293])
  5112. assert_almost_equal(res, tgt, decimal=self.N)
  5113. def test_dotvecmat(self):
  5114. A, b4 = self.A, self.b4
  5115. res = np.dot(b4, A)
  5116. tgt = np.array([1.23495091, 1.12222648])
  5117. assert_almost_equal(res, tgt, decimal=self.N)
  5118. def test_dotvecmat2(self):
  5119. b3, A = self.b3, self.A
  5120. res = np.dot(b3, A.transpose())
  5121. tgt = np.array([[0.58793804, 0.08957460, 0.30605758, 0.62716383]])
  5122. assert_almost_equal(res, tgt, decimal=self.N)
  5123. def test_dotvecmat3(self):
  5124. A, b4 = self.A, self.b4
  5125. res = np.dot(A.transpose(), b4)
  5126. tgt = np.array([1.23495091, 1.12222648])
  5127. assert_almost_equal(res, tgt, decimal=self.N)
  5128. def test_dotvecvecouter(self):
  5129. b1, b3 = self.b1, self.b3
  5130. res = np.dot(b1, b3)
  5131. tgt = np.array([[0.20128610, 0.08400440], [0.07190947, 0.03001058]])
  5132. assert_almost_equal(res, tgt, decimal=self.N)
  5133. def test_dotvecvecinner(self):
  5134. b1, b3 = self.b1, self.b3
  5135. res = np.dot(b3, b1)
  5136. tgt = np.array([[ 0.23129668]])
  5137. assert_almost_equal(res, tgt, decimal=self.N)
  5138. def test_dotcolumnvect1(self):
  5139. b1 = np.ones((3, 1))
  5140. b2 = [5.3]
  5141. res = np.dot(b1, b2)
  5142. tgt = np.array([5.3, 5.3, 5.3])
  5143. assert_almost_equal(res, tgt, decimal=self.N)
  5144. def test_dotcolumnvect2(self):
  5145. b1 = np.ones((3, 1)).transpose()
  5146. b2 = [6.2]
  5147. res = np.dot(b2, b1)
  5148. tgt = np.array([6.2, 6.2, 6.2])
  5149. assert_almost_equal(res, tgt, decimal=self.N)
  5150. def test_dotvecscalar(self):
  5151. np.random.seed(100)
  5152. b1 = np.random.rand(1, 1)
  5153. b2 = np.random.rand(1, 4)
  5154. res = np.dot(b1, b2)
  5155. tgt = np.array([[0.15126730, 0.23068496, 0.45905553, 0.00256425]])
  5156. assert_almost_equal(res, tgt, decimal=self.N)
  5157. def test_dotvecscalar2(self):
  5158. np.random.seed(100)
  5159. b1 = np.random.rand(4, 1)
  5160. b2 = np.random.rand(1, 1)
  5161. res = np.dot(b1, b2)
  5162. tgt = np.array([[0.00256425],[0.00131359],[0.00200324],[ 0.00398638]])
  5163. assert_almost_equal(res, tgt, decimal=self.N)
  5164. def test_all(self):
  5165. dims = [(), (1,), (1, 1)]
  5166. dout = [(), (1,), (1, 1), (1,), (), (1,), (1, 1), (1,), (1, 1)]
  5167. for dim, (dim1, dim2) in zip(dout, itertools.product(dims, dims)):
  5168. b1 = np.zeros(dim1)
  5169. b2 = np.zeros(dim2)
  5170. res = np.dot(b1, b2)
  5171. tgt = np.zeros(dim)
  5172. assert_(res.shape == tgt.shape)
  5173. assert_almost_equal(res, tgt, decimal=self.N)
  5174. def test_vecobject(self):
  5175. class Vec:
  5176. def __init__(self, sequence=None):
  5177. if sequence is None:
  5178. sequence = []
  5179. self.array = np.array(sequence)
  5180. def __add__(self, other):
  5181. out = Vec()
  5182. out.array = self.array + other.array
  5183. return out
  5184. def __sub__(self, other):
  5185. out = Vec()
  5186. out.array = self.array - other.array
  5187. return out
  5188. def __mul__(self, other): # with scalar
  5189. out = Vec(self.array.copy())
  5190. out.array *= other
  5191. return out
  5192. def __rmul__(self, other):
  5193. return self*other
  5194. U_non_cont = np.transpose([[1., 1.], [1., 2.]])
  5195. U_cont = np.ascontiguousarray(U_non_cont)
  5196. x = np.array([Vec([1., 0.]), Vec([0., 1.])])
  5197. zeros = np.array([Vec([0., 0.]), Vec([0., 0.])])
  5198. zeros_test = np.dot(U_cont, x) - np.dot(U_non_cont, x)
  5199. assert_equal(zeros[0].array, zeros_test[0].array)
  5200. assert_equal(zeros[1].array, zeros_test[1].array)
  5201. def test_dot_2args(self):
  5202. from numpy.core.multiarray import dot
  5203. a = np.array([[1, 2], [3, 4]], dtype=float)
  5204. b = np.array([[1, 0], [1, 1]], dtype=float)
  5205. c = np.array([[3, 2], [7, 4]], dtype=float)
  5206. d = dot(a, b)
  5207. assert_allclose(c, d)
  5208. def test_dot_3args(self):
  5209. from numpy.core.multiarray import dot
  5210. np.random.seed(22)
  5211. f = np.random.random_sample((1024, 16))
  5212. v = np.random.random_sample((16, 32))
  5213. r = np.empty((1024, 32))
  5214. for i in range(12):
  5215. dot(f, v, r)
  5216. if HAS_REFCOUNT:
  5217. assert_equal(sys.getrefcount(r), 2)
  5218. r2 = dot(f, v, out=None)
  5219. assert_array_equal(r2, r)
  5220. assert_(r is dot(f, v, out=r))
  5221. v = v[:, 0].copy() # v.shape == (16,)
  5222. r = r[:, 0].copy() # r.shape == (1024,)
  5223. r2 = dot(f, v)
  5224. assert_(r is dot(f, v, r))
  5225. assert_array_equal(r2, r)
  5226. def test_dot_3args_errors(self):
  5227. from numpy.core.multiarray import dot
  5228. np.random.seed(22)
  5229. f = np.random.random_sample((1024, 16))
  5230. v = np.random.random_sample((16, 32))
  5231. r = np.empty((1024, 31))
  5232. assert_raises(ValueError, dot, f, v, r)
  5233. r = np.empty((1024,))
  5234. assert_raises(ValueError, dot, f, v, r)
  5235. r = np.empty((32,))
  5236. assert_raises(ValueError, dot, f, v, r)
  5237. r = np.empty((32, 1024))
  5238. assert_raises(ValueError, dot, f, v, r)
  5239. assert_raises(ValueError, dot, f, v, r.T)
  5240. r = np.empty((1024, 64))
  5241. assert_raises(ValueError, dot, f, v, r[:, ::2])
  5242. assert_raises(ValueError, dot, f, v, r[:, :32])
  5243. r = np.empty((1024, 32), dtype=np.float32)
  5244. assert_raises(ValueError, dot, f, v, r)
  5245. r = np.empty((1024, 32), dtype=int)
  5246. assert_raises(ValueError, dot, f, v, r)
  5247. def test_dot_array_order(self):
  5248. a = np.array([[1, 2], [3, 4]], order='C')
  5249. b = np.array([[1, 2], [3, 4]], order='F')
  5250. res = np.dot(a, a)
  5251. # integer arrays are exact
  5252. assert_equal(np.dot(a, b), res)
  5253. assert_equal(np.dot(b, a), res)
  5254. assert_equal(np.dot(b, b), res)
  5255. class MatmulCommon:
  5256. """Common tests for '@' operator and numpy.matmul.
  5257. """
  5258. # Should work with these types. Will want to add
  5259. # "O" at some point
  5260. types = "?bhilqBHILQefdgFDGO"
  5261. def test_exceptions(self):
  5262. dims = [
  5263. ((1,), (2,)), # mismatched vector vector
  5264. ((2, 1,), (2,)), # mismatched matrix vector
  5265. ((2,), (1, 2)), # mismatched vector matrix
  5266. ((1, 2), (3, 1)), # mismatched matrix matrix
  5267. ((1,), ()), # vector scalar
  5268. ((), (1)), # scalar vector
  5269. ((1, 1), ()), # matrix scalar
  5270. ((), (1, 1)), # scalar matrix
  5271. ((2, 2, 1), (3, 1, 2)), # cannot broadcast
  5272. ]
  5273. for dt, (dm1, dm2) in itertools.product(self.types, dims):
  5274. a = np.ones(dm1, dtype=dt)
  5275. b = np.ones(dm2, dtype=dt)
  5276. assert_raises(ValueError, self.matmul, a, b)
  5277. def test_shapes(self):
  5278. dims = [
  5279. ((1, 1), (2, 1, 1)), # broadcast first argument
  5280. ((2, 1, 1), (1, 1)), # broadcast second argument
  5281. ((2, 1, 1), (2, 1, 1)), # matrix stack sizes match
  5282. ]
  5283. for dt, (dm1, dm2) in itertools.product(self.types, dims):
  5284. a = np.ones(dm1, dtype=dt)
  5285. b = np.ones(dm2, dtype=dt)
  5286. res = self.matmul(a, b)
  5287. assert_(res.shape == (2, 1, 1))
  5288. # vector vector returns scalars.
  5289. for dt in self.types:
  5290. a = np.ones((2,), dtype=dt)
  5291. b = np.ones((2,), dtype=dt)
  5292. c = self.matmul(a, b)
  5293. assert_(np.array(c).shape == ())
  5294. def test_result_types(self):
  5295. mat = np.ones((1,1))
  5296. vec = np.ones((1,))
  5297. for dt in self.types:
  5298. m = mat.astype(dt)
  5299. v = vec.astype(dt)
  5300. for arg in [(m, v), (v, m), (m, m)]:
  5301. res = self.matmul(*arg)
  5302. assert_(res.dtype == dt)
  5303. # vector vector returns scalars
  5304. if dt != "O":
  5305. res = self.matmul(v, v)
  5306. assert_(type(res) is np.dtype(dt).type)
  5307. def test_scalar_output(self):
  5308. vec1 = np.array([2])
  5309. vec2 = np.array([3, 4]).reshape(1, -1)
  5310. tgt = np.array([6, 8])
  5311. for dt in self.types[1:]:
  5312. v1 = vec1.astype(dt)
  5313. v2 = vec2.astype(dt)
  5314. res = self.matmul(v1, v2)
  5315. assert_equal(res, tgt)
  5316. res = self.matmul(v2.T, v1)
  5317. assert_equal(res, tgt)
  5318. # boolean type
  5319. vec = np.array([True, True], dtype='?').reshape(1, -1)
  5320. res = self.matmul(vec[:, 0], vec)
  5321. assert_equal(res, True)
  5322. def test_vector_vector_values(self):
  5323. vec1 = np.array([1, 2])
  5324. vec2 = np.array([3, 4]).reshape(-1, 1)
  5325. tgt1 = np.array([11])
  5326. tgt2 = np.array([[3, 6], [4, 8]])
  5327. for dt in self.types[1:]:
  5328. v1 = vec1.astype(dt)
  5329. v2 = vec2.astype(dt)
  5330. res = self.matmul(v1, v2)
  5331. assert_equal(res, tgt1)
  5332. # no broadcast, we must make v1 into a 2d ndarray
  5333. res = self.matmul(v2, v1.reshape(1, -1))
  5334. assert_equal(res, tgt2)
  5335. # boolean type
  5336. vec = np.array([True, True], dtype='?')
  5337. res = self.matmul(vec, vec)
  5338. assert_equal(res, True)
  5339. def test_vector_matrix_values(self):
  5340. vec = np.array([1, 2])
  5341. mat1 = np.array([[1, 2], [3, 4]])
  5342. mat2 = np.stack([mat1]*2, axis=0)
  5343. tgt1 = np.array([7, 10])
  5344. tgt2 = np.stack([tgt1]*2, axis=0)
  5345. for dt in self.types[1:]:
  5346. v = vec.astype(dt)
  5347. m1 = mat1.astype(dt)
  5348. m2 = mat2.astype(dt)
  5349. res = self.matmul(v, m1)
  5350. assert_equal(res, tgt1)
  5351. res = self.matmul(v, m2)
  5352. assert_equal(res, tgt2)
  5353. # boolean type
  5354. vec = np.array([True, False])
  5355. mat1 = np.array([[True, False], [False, True]])
  5356. mat2 = np.stack([mat1]*2, axis=0)
  5357. tgt1 = np.array([True, False])
  5358. tgt2 = np.stack([tgt1]*2, axis=0)
  5359. res = self.matmul(vec, mat1)
  5360. assert_equal(res, tgt1)
  5361. res = self.matmul(vec, mat2)
  5362. assert_equal(res, tgt2)
  5363. def test_matrix_vector_values(self):
  5364. vec = np.array([1, 2])
  5365. mat1 = np.array([[1, 2], [3, 4]])
  5366. mat2 = np.stack([mat1]*2, axis=0)
  5367. tgt1 = np.array([5, 11])
  5368. tgt2 = np.stack([tgt1]*2, axis=0)
  5369. for dt in self.types[1:]:
  5370. v = vec.astype(dt)
  5371. m1 = mat1.astype(dt)
  5372. m2 = mat2.astype(dt)
  5373. res = self.matmul(m1, v)
  5374. assert_equal(res, tgt1)
  5375. res = self.matmul(m2, v)
  5376. assert_equal(res, tgt2)
  5377. # boolean type
  5378. vec = np.array([True, False])
  5379. mat1 = np.array([[True, False], [False, True]])
  5380. mat2 = np.stack([mat1]*2, axis=0)
  5381. tgt1 = np.array([True, False])
  5382. tgt2 = np.stack([tgt1]*2, axis=0)
  5383. res = self.matmul(vec, mat1)
  5384. assert_equal(res, tgt1)
  5385. res = self.matmul(vec, mat2)
  5386. assert_equal(res, tgt2)
  5387. def test_matrix_matrix_values(self):
  5388. mat1 = np.array([[1, 2], [3, 4]])
  5389. mat2 = np.array([[1, 0], [1, 1]])
  5390. mat12 = np.stack([mat1, mat2], axis=0)
  5391. mat21 = np.stack([mat2, mat1], axis=0)
  5392. tgt11 = np.array([[7, 10], [15, 22]])
  5393. tgt12 = np.array([[3, 2], [7, 4]])
  5394. tgt21 = np.array([[1, 2], [4, 6]])
  5395. tgt12_21 = np.stack([tgt12, tgt21], axis=0)
  5396. tgt11_12 = np.stack((tgt11, tgt12), axis=0)
  5397. tgt11_21 = np.stack((tgt11, tgt21), axis=0)
  5398. for dt in self.types[1:]:
  5399. m1 = mat1.astype(dt)
  5400. m2 = mat2.astype(dt)
  5401. m12 = mat12.astype(dt)
  5402. m21 = mat21.astype(dt)
  5403. # matrix @ matrix
  5404. res = self.matmul(m1, m2)
  5405. assert_equal(res, tgt12)
  5406. res = self.matmul(m2, m1)
  5407. assert_equal(res, tgt21)
  5408. # stacked @ matrix
  5409. res = self.matmul(m12, m1)
  5410. assert_equal(res, tgt11_21)
  5411. # matrix @ stacked
  5412. res = self.matmul(m1, m12)
  5413. assert_equal(res, tgt11_12)
  5414. # stacked @ stacked
  5415. res = self.matmul(m12, m21)
  5416. assert_equal(res, tgt12_21)
  5417. # boolean type
  5418. m1 = np.array([[1, 1], [0, 0]], dtype=np.bool_)
  5419. m2 = np.array([[1, 0], [1, 1]], dtype=np.bool_)
  5420. m12 = np.stack([m1, m2], axis=0)
  5421. m21 = np.stack([m2, m1], axis=0)
  5422. tgt11 = m1
  5423. tgt12 = m1
  5424. tgt21 = np.array([[1, 1], [1, 1]], dtype=np.bool_)
  5425. tgt12_21 = np.stack([tgt12, tgt21], axis=0)
  5426. tgt11_12 = np.stack((tgt11, tgt12), axis=0)
  5427. tgt11_21 = np.stack((tgt11, tgt21), axis=0)
  5428. # matrix @ matrix
  5429. res = self.matmul(m1, m2)
  5430. assert_equal(res, tgt12)
  5431. res = self.matmul(m2, m1)
  5432. assert_equal(res, tgt21)
  5433. # stacked @ matrix
  5434. res = self.matmul(m12, m1)
  5435. assert_equal(res, tgt11_21)
  5436. # matrix @ stacked
  5437. res = self.matmul(m1, m12)
  5438. assert_equal(res, tgt11_12)
  5439. # stacked @ stacked
  5440. res = self.matmul(m12, m21)
  5441. assert_equal(res, tgt12_21)
  5442. class TestMatmul(MatmulCommon):
  5443. matmul = np.matmul
  5444. def test_out_arg(self):
  5445. a = np.ones((5, 2), dtype=float)
  5446. b = np.array([[1, 3], [5, 7]], dtype=float)
  5447. tgt = np.dot(a, b)
  5448. # test as positional argument
  5449. msg = "out positional argument"
  5450. out = np.zeros((5, 2), dtype=float)
  5451. self.matmul(a, b, out)
  5452. assert_array_equal(out, tgt, err_msg=msg)
  5453. # test as keyword argument
  5454. msg = "out keyword argument"
  5455. out = np.zeros((5, 2), dtype=float)
  5456. self.matmul(a, b, out=out)
  5457. assert_array_equal(out, tgt, err_msg=msg)
  5458. # test out with not allowed type cast (safe casting)
  5459. msg = "Cannot cast ufunc .* output"
  5460. out = np.zeros((5, 2), dtype=np.int32)
  5461. assert_raises_regex(TypeError, msg, self.matmul, a, b, out=out)
  5462. # test out with type upcast to complex
  5463. out = np.zeros((5, 2), dtype=np.complex128)
  5464. c = self.matmul(a, b, out=out)
  5465. assert_(c is out)
  5466. with suppress_warnings() as sup:
  5467. sup.filter(np.ComplexWarning, '')
  5468. c = c.astype(tgt.dtype)
  5469. assert_array_equal(c, tgt)
  5470. def test_out_contiguous(self):
  5471. a = np.ones((5, 2), dtype=float)
  5472. b = np.array([[1, 3], [5, 7]], dtype=float)
  5473. v = np.array([1, 3], dtype=float)
  5474. tgt = np.dot(a, b)
  5475. tgt_mv = np.dot(a, v)
  5476. # test out non-contiguous
  5477. out = np.ones((5, 2, 2), dtype=float)
  5478. c = self.matmul(a, b, out=out[..., 0])
  5479. assert c.base is out
  5480. assert_array_equal(c, tgt)
  5481. c = self.matmul(a, v, out=out[:, 0, 0])
  5482. assert_array_equal(c, tgt_mv)
  5483. c = self.matmul(v, a.T, out=out[:, 0, 0])
  5484. assert_array_equal(c, tgt_mv)
  5485. # test out contiguous in only last dim
  5486. out = np.ones((10, 2), dtype=float)
  5487. c = self.matmul(a, b, out=out[::2, :])
  5488. assert_array_equal(c, tgt)
  5489. # test transposes of out, args
  5490. out = np.ones((5, 2), dtype=float)
  5491. c = self.matmul(b.T, a.T, out=out.T)
  5492. assert_array_equal(out, tgt)
  5493. m1 = np.arange(15.).reshape(5, 3)
  5494. m2 = np.arange(21.).reshape(3, 7)
  5495. m3 = np.arange(30.).reshape(5, 6)[:, ::2] # non-contiguous
  5496. vc = np.arange(10.)
  5497. vr = np.arange(6.)
  5498. m0 = np.zeros((3, 0))
  5499. @pytest.mark.parametrize('args', (
  5500. # matrix-matrix
  5501. (m1, m2), (m2.T, m1.T), (m2.T.copy(), m1.T), (m2.T, m1.T.copy()),
  5502. # matrix-matrix-transpose, contiguous and non
  5503. (m1, m1.T), (m1.T, m1), (m1, m3.T), (m3, m1.T),
  5504. (m3, m3.T), (m3.T, m3),
  5505. # matrix-matrix non-contiguous
  5506. (m3, m2), (m2.T, m3.T), (m2.T.copy(), m3.T),
  5507. # vector-matrix, matrix-vector, contiguous
  5508. (m1, vr[:3]), (vc[:5], m1), (m1.T, vc[:5]), (vr[:3], m1.T),
  5509. # vector-matrix, matrix-vector, vector non-contiguous
  5510. (m1, vr[::2]), (vc[::2], m1), (m1.T, vc[::2]), (vr[::2], m1.T),
  5511. # vector-matrix, matrix-vector, matrix non-contiguous
  5512. (m3, vr[:3]), (vc[:5], m3), (m3.T, vc[:5]), (vr[:3], m3.T),
  5513. # vector-matrix, matrix-vector, both non-contiguous
  5514. (m3, vr[::2]), (vc[::2], m3), (m3.T, vc[::2]), (vr[::2], m3.T),
  5515. # size == 0
  5516. (m0, m0.T), (m0.T, m0), (m1, m0), (m0.T, m1.T),
  5517. ))
  5518. def test_dot_equivalent(self, args):
  5519. r1 = np.matmul(*args)
  5520. r2 = np.dot(*args)
  5521. assert_equal(r1, r2)
  5522. r3 = np.matmul(args[0].copy(), args[1].copy())
  5523. assert_equal(r1, r3)
  5524. def test_matmul_object(self):
  5525. import fractions
  5526. f = np.vectorize(fractions.Fraction)
  5527. def random_ints():
  5528. return np.random.randint(1, 1000, size=(10, 3, 3))
  5529. M1 = f(random_ints(), random_ints())
  5530. M2 = f(random_ints(), random_ints())
  5531. M3 = self.matmul(M1, M2)
  5532. [N1, N2, N3] = [a.astype(float) for a in [M1, M2, M3]]
  5533. assert_allclose(N3, self.matmul(N1, N2))
  5534. def test_matmul_object_type_scalar(self):
  5535. from fractions import Fraction as F
  5536. v = np.array([F(2,3), F(5,7)])
  5537. res = self.matmul(v, v)
  5538. assert_(type(res) is F)
  5539. def test_matmul_empty(self):
  5540. a = np.empty((3, 0), dtype=object)
  5541. b = np.empty((0, 3), dtype=object)
  5542. c = np.zeros((3, 3))
  5543. assert_array_equal(np.matmul(a, b), c)
  5544. def test_matmul_exception_multiply(self):
  5545. # test that matmul fails if `__mul__` is missing
  5546. class add_not_multiply():
  5547. def __add__(self, other):
  5548. return self
  5549. a = np.full((3,3), add_not_multiply())
  5550. with assert_raises(TypeError):
  5551. b = np.matmul(a, a)
  5552. def test_matmul_exception_add(self):
  5553. # test that matmul fails if `__add__` is missing
  5554. class multiply_not_add():
  5555. def __mul__(self, other):
  5556. return self
  5557. a = np.full((3,3), multiply_not_add())
  5558. with assert_raises(TypeError):
  5559. b = np.matmul(a, a)
  5560. def test_matmul_bool(self):
  5561. # gh-14439
  5562. a = np.array([[1, 0],[1, 1]], dtype=bool)
  5563. assert np.max(a.view(np.uint8)) == 1
  5564. b = np.matmul(a, a)
  5565. # matmul with boolean output should always be 0, 1
  5566. assert np.max(b.view(np.uint8)) == 1
  5567. rg = np.random.default_rng(np.random.PCG64(43))
  5568. d = rg.integers(2, size=4*5, dtype=np.int8)
  5569. d = d.reshape(4, 5) > 0
  5570. out1 = np.matmul(d, d.reshape(5, 4))
  5571. out2 = np.dot(d, d.reshape(5, 4))
  5572. assert_equal(out1, out2)
  5573. c = np.matmul(np.zeros((2, 0), dtype=bool), np.zeros(0, dtype=bool))
  5574. assert not np.any(c)
  5575. class TestMatmulOperator(MatmulCommon):
  5576. import operator
  5577. matmul = operator.matmul
  5578. def test_array_priority_override(self):
  5579. class A:
  5580. __array_priority__ = 1000
  5581. def __matmul__(self, other):
  5582. return "A"
  5583. def __rmatmul__(self, other):
  5584. return "A"
  5585. a = A()
  5586. b = np.ones(2)
  5587. assert_equal(self.matmul(a, b), "A")
  5588. assert_equal(self.matmul(b, a), "A")
  5589. def test_matmul_raises(self):
  5590. assert_raises(TypeError, self.matmul, np.int8(5), np.int8(5))
  5591. assert_raises(TypeError, self.matmul, np.void(b'abc'), np.void(b'abc'))
  5592. assert_raises(ValueError, self.matmul, np.arange(10), np.void(b'abc'))
  5593. def test_matmul_inplace():
  5594. # It would be nice to support in-place matmul eventually, but for now
  5595. # we don't have a working implementation, so better just to error out
  5596. # and nudge people to writing "a = a @ b".
  5597. a = np.eye(3)
  5598. b = np.eye(3)
  5599. assert_raises(TypeError, a.__imatmul__, b)
  5600. import operator
  5601. assert_raises(TypeError, operator.imatmul, a, b)
  5602. assert_raises(TypeError, exec, "a @= b", globals(), locals())
  5603. def test_matmul_axes():
  5604. a = np.arange(3*4*5).reshape(3, 4, 5)
  5605. c = np.matmul(a, a, axes=[(-2, -1), (-1, -2), (1, 2)])
  5606. assert c.shape == (3, 4, 4)
  5607. d = np.matmul(a, a, axes=[(-2, -1), (-1, -2), (0, 1)])
  5608. assert d.shape == (4, 4, 3)
  5609. e = np.swapaxes(d, 0, 2)
  5610. assert_array_equal(e, c)
  5611. f = np.matmul(a, np.arange(3), axes=[(1, 0), (0), (0)])
  5612. assert f.shape == (4, 5)
  5613. class TestInner:
  5614. def test_inner_type_mismatch(self):
  5615. c = 1.
  5616. A = np.array((1,1), dtype='i,i')
  5617. assert_raises(TypeError, np.inner, c, A)
  5618. assert_raises(TypeError, np.inner, A, c)
  5619. def test_inner_scalar_and_vector(self):
  5620. for dt in np.typecodes['AllInteger'] + np.typecodes['AllFloat'] + '?':
  5621. sca = np.array(3, dtype=dt)[()]
  5622. vec = np.array([1, 2], dtype=dt)
  5623. desired = np.array([3, 6], dtype=dt)
  5624. assert_equal(np.inner(vec, sca), desired)
  5625. assert_equal(np.inner(sca, vec), desired)
  5626. def test_vecself(self):
  5627. # Ticket 844.
  5628. # Inner product of a vector with itself segfaults or give
  5629. # meaningless result
  5630. a = np.zeros(shape=(1, 80), dtype=np.float64)
  5631. p = np.inner(a, a)
  5632. assert_almost_equal(p, 0, decimal=14)
  5633. def test_inner_product_with_various_contiguities(self):
  5634. # github issue 6532
  5635. for dt in np.typecodes['AllInteger'] + np.typecodes['AllFloat'] + '?':
  5636. # check an inner product involving a matrix transpose
  5637. A = np.array([[1, 2], [3, 4]], dtype=dt)
  5638. B = np.array([[1, 3], [2, 4]], dtype=dt)
  5639. C = np.array([1, 1], dtype=dt)
  5640. desired = np.array([4, 6], dtype=dt)
  5641. assert_equal(np.inner(A.T, C), desired)
  5642. assert_equal(np.inner(C, A.T), desired)
  5643. assert_equal(np.inner(B, C), desired)
  5644. assert_equal(np.inner(C, B), desired)
  5645. # check a matrix product
  5646. desired = np.array([[7, 10], [15, 22]], dtype=dt)
  5647. assert_equal(np.inner(A, B), desired)
  5648. # check the syrk vs. gemm paths
  5649. desired = np.array([[5, 11], [11, 25]], dtype=dt)
  5650. assert_equal(np.inner(A, A), desired)
  5651. assert_equal(np.inner(A, A.copy()), desired)
  5652. # check an inner product involving an aliased and reversed view
  5653. a = np.arange(5).astype(dt)
  5654. b = a[::-1]
  5655. desired = np.array(10, dtype=dt).item()
  5656. assert_equal(np.inner(b, a), desired)
  5657. def test_3d_tensor(self):
  5658. for dt in np.typecodes['AllInteger'] + np.typecodes['AllFloat'] + '?':
  5659. a = np.arange(24).reshape(2,3,4).astype(dt)
  5660. b = np.arange(24, 48).reshape(2,3,4).astype(dt)
  5661. desired = np.array(
  5662. [[[[ 158, 182, 206],
  5663. [ 230, 254, 278]],
  5664. [[ 566, 654, 742],
  5665. [ 830, 918, 1006]],
  5666. [[ 974, 1126, 1278],
  5667. [1430, 1582, 1734]]],
  5668. [[[1382, 1598, 1814],
  5669. [2030, 2246, 2462]],
  5670. [[1790, 2070, 2350],
  5671. [2630, 2910, 3190]],
  5672. [[2198, 2542, 2886],
  5673. [3230, 3574, 3918]]]],
  5674. dtype=dt
  5675. )
  5676. assert_equal(np.inner(a, b), desired)
  5677. assert_equal(np.inner(b, a).transpose(2,3,0,1), desired)
  5678. class TestAlen:
  5679. def test_basic(self):
  5680. with pytest.warns(DeprecationWarning):
  5681. m = np.array([1, 2, 3])
  5682. assert_equal(np.alen(m), 3)
  5683. m = np.array([[1, 2, 3], [4, 5, 7]])
  5684. assert_equal(np.alen(m), 2)
  5685. m = [1, 2, 3]
  5686. assert_equal(np.alen(m), 3)
  5687. m = [[1, 2, 3], [4, 5, 7]]
  5688. assert_equal(np.alen(m), 2)
  5689. def test_singleton(self):
  5690. with pytest.warns(DeprecationWarning):
  5691. assert_equal(np.alen(5), 1)
  5692. class TestChoose:
  5693. def setup(self):
  5694. self.x = 2*np.ones((3,), dtype=int)
  5695. self.y = 3*np.ones((3,), dtype=int)
  5696. self.x2 = 2*np.ones((2, 3), dtype=int)
  5697. self.y2 = 3*np.ones((2, 3), dtype=int)
  5698. self.ind = [0, 0, 1]
  5699. def test_basic(self):
  5700. A = np.choose(self.ind, (self.x, self.y))
  5701. assert_equal(A, [2, 2, 3])
  5702. def test_broadcast1(self):
  5703. A = np.choose(self.ind, (self.x2, self.y2))
  5704. assert_equal(A, [[2, 2, 3], [2, 2, 3]])
  5705. def test_broadcast2(self):
  5706. A = np.choose(self.ind, (self.x, self.y2))
  5707. assert_equal(A, [[2, 2, 3], [2, 2, 3]])
  5708. @pytest.mark.parametrize("ops",
  5709. [(1000, np.array([1], dtype=np.uint8)),
  5710. (-1, np.array([1], dtype=np.uint8)),
  5711. (1., np.float32(3)),
  5712. (1., np.array([3], dtype=np.float32))],)
  5713. def test_output_dtype(self, ops):
  5714. expected_dt = np.result_type(*ops)
  5715. assert(np.choose([0], ops).dtype == expected_dt)
  5716. class TestRepeat:
  5717. def setup(self):
  5718. self.m = np.array([1, 2, 3, 4, 5, 6])
  5719. self.m_rect = self.m.reshape((2, 3))
  5720. def test_basic(self):
  5721. A = np.repeat(self.m, [1, 3, 2, 1, 1, 2])
  5722. assert_equal(A, [1, 2, 2, 2, 3,
  5723. 3, 4, 5, 6, 6])
  5724. def test_broadcast1(self):
  5725. A = np.repeat(self.m, 2)
  5726. assert_equal(A, [1, 1, 2, 2, 3, 3,
  5727. 4, 4, 5, 5, 6, 6])
  5728. def test_axis_spec(self):
  5729. A = np.repeat(self.m_rect, [2, 1], axis=0)
  5730. assert_equal(A, [[1, 2, 3],
  5731. [1, 2, 3],
  5732. [4, 5, 6]])
  5733. A = np.repeat(self.m_rect, [1, 3, 2], axis=1)
  5734. assert_equal(A, [[1, 2, 2, 2, 3, 3],
  5735. [4, 5, 5, 5, 6, 6]])
  5736. def test_broadcast2(self):
  5737. A = np.repeat(self.m_rect, 2, axis=0)
  5738. assert_equal(A, [[1, 2, 3],
  5739. [1, 2, 3],
  5740. [4, 5, 6],
  5741. [4, 5, 6]])
  5742. A = np.repeat(self.m_rect, 2, axis=1)
  5743. assert_equal(A, [[1, 1, 2, 2, 3, 3],
  5744. [4, 4, 5, 5, 6, 6]])
  5745. # TODO: test for multidimensional
  5746. NEIGH_MODE = {'zero': 0, 'one': 1, 'constant': 2, 'circular': 3, 'mirror': 4}
  5747. @pytest.mark.parametrize('dt', [float, Decimal], ids=['float', 'object'])
  5748. class TestNeighborhoodIter:
  5749. # Simple, 2d tests
  5750. def test_simple2d(self, dt):
  5751. # Test zero and one padding for simple data type
  5752. x = np.array([[0, 1], [2, 3]], dtype=dt)
  5753. r = [np.array([[0, 0, 0], [0, 0, 1]], dtype=dt),
  5754. np.array([[0, 0, 0], [0, 1, 0]], dtype=dt),
  5755. np.array([[0, 0, 1], [0, 2, 3]], dtype=dt),
  5756. np.array([[0, 1, 0], [2, 3, 0]], dtype=dt)]
  5757. l = _multiarray_tests.test_neighborhood_iterator(
  5758. x, [-1, 0, -1, 1], x[0], NEIGH_MODE['zero'])
  5759. assert_array_equal(l, r)
  5760. r = [np.array([[1, 1, 1], [1, 0, 1]], dtype=dt),
  5761. np.array([[1, 1, 1], [0, 1, 1]], dtype=dt),
  5762. np.array([[1, 0, 1], [1, 2, 3]], dtype=dt),
  5763. np.array([[0, 1, 1], [2, 3, 1]], dtype=dt)]
  5764. l = _multiarray_tests.test_neighborhood_iterator(
  5765. x, [-1, 0, -1, 1], x[0], NEIGH_MODE['one'])
  5766. assert_array_equal(l, r)
  5767. r = [np.array([[4, 4, 4], [4, 0, 1]], dtype=dt),
  5768. np.array([[4, 4, 4], [0, 1, 4]], dtype=dt),
  5769. np.array([[4, 0, 1], [4, 2, 3]], dtype=dt),
  5770. np.array([[0, 1, 4], [2, 3, 4]], dtype=dt)]
  5771. l = _multiarray_tests.test_neighborhood_iterator(
  5772. x, [-1, 0, -1, 1], 4, NEIGH_MODE['constant'])
  5773. assert_array_equal(l, r)
  5774. def test_mirror2d(self, dt):
  5775. x = np.array([[0, 1], [2, 3]], dtype=dt)
  5776. r = [np.array([[0, 0, 1], [0, 0, 1]], dtype=dt),
  5777. np.array([[0, 1, 1], [0, 1, 1]], dtype=dt),
  5778. np.array([[0, 0, 1], [2, 2, 3]], dtype=dt),
  5779. np.array([[0, 1, 1], [2, 3, 3]], dtype=dt)]
  5780. l = _multiarray_tests.test_neighborhood_iterator(
  5781. x, [-1, 0, -1, 1], x[0], NEIGH_MODE['mirror'])
  5782. assert_array_equal(l, r)
  5783. # Simple, 1d tests
  5784. def test_simple(self, dt):
  5785. # Test padding with constant values
  5786. x = np.linspace(1, 5, 5).astype(dt)
  5787. r = [[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 0]]
  5788. l = _multiarray_tests.test_neighborhood_iterator(
  5789. x, [-1, 1], x[0], NEIGH_MODE['zero'])
  5790. assert_array_equal(l, r)
  5791. r = [[1, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 1]]
  5792. l = _multiarray_tests.test_neighborhood_iterator(
  5793. x, [-1, 1], x[0], NEIGH_MODE['one'])
  5794. assert_array_equal(l, r)
  5795. r = [[x[4], 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, x[4]]]
  5796. l = _multiarray_tests.test_neighborhood_iterator(
  5797. x, [-1, 1], x[4], NEIGH_MODE['constant'])
  5798. assert_array_equal(l, r)
  5799. # Test mirror modes
  5800. def test_mirror(self, dt):
  5801. x = np.linspace(1, 5, 5).astype(dt)
  5802. r = np.array([[2, 1, 1, 2, 3], [1, 1, 2, 3, 4], [1, 2, 3, 4, 5],
  5803. [2, 3, 4, 5, 5], [3, 4, 5, 5, 4]], dtype=dt)
  5804. l = _multiarray_tests.test_neighborhood_iterator(
  5805. x, [-2, 2], x[1], NEIGH_MODE['mirror'])
  5806. assert_([i.dtype == dt for i in l])
  5807. assert_array_equal(l, r)
  5808. # Circular mode
  5809. def test_circular(self, dt):
  5810. x = np.linspace(1, 5, 5).astype(dt)
  5811. r = np.array([[4, 5, 1, 2, 3], [5, 1, 2, 3, 4], [1, 2, 3, 4, 5],
  5812. [2, 3, 4, 5, 1], [3, 4, 5, 1, 2]], dtype=dt)
  5813. l = _multiarray_tests.test_neighborhood_iterator(
  5814. x, [-2, 2], x[0], NEIGH_MODE['circular'])
  5815. assert_array_equal(l, r)
  5816. # Test stacking neighborhood iterators
  5817. class TestStackedNeighborhoodIter:
  5818. # Simple, 1d test: stacking 2 constant-padded neigh iterators
  5819. def test_simple_const(self):
  5820. dt = np.float64
  5821. # Test zero and one padding for simple data type
  5822. x = np.array([1, 2, 3], dtype=dt)
  5823. r = [np.array([0], dtype=dt),
  5824. np.array([0], dtype=dt),
  5825. np.array([1], dtype=dt),
  5826. np.array([2], dtype=dt),
  5827. np.array([3], dtype=dt),
  5828. np.array([0], dtype=dt),
  5829. np.array([0], dtype=dt)]
  5830. l = _multiarray_tests.test_neighborhood_iterator_oob(
  5831. x, [-2, 4], NEIGH_MODE['zero'], [0, 0], NEIGH_MODE['zero'])
  5832. assert_array_equal(l, r)
  5833. r = [np.array([1, 0, 1], dtype=dt),
  5834. np.array([0, 1, 2], dtype=dt),
  5835. np.array([1, 2, 3], dtype=dt),
  5836. np.array([2, 3, 0], dtype=dt),
  5837. np.array([3, 0, 1], dtype=dt)]
  5838. l = _multiarray_tests.test_neighborhood_iterator_oob(
  5839. x, [-1, 3], NEIGH_MODE['zero'], [-1, 1], NEIGH_MODE['one'])
  5840. assert_array_equal(l, r)
  5841. # 2nd simple, 1d test: stacking 2 neigh iterators, mixing const padding and
  5842. # mirror padding
  5843. def test_simple_mirror(self):
  5844. dt = np.float64
  5845. # Stacking zero on top of mirror
  5846. x = np.array([1, 2, 3], dtype=dt)
  5847. r = [np.array([0, 1, 1], dtype=dt),
  5848. np.array([1, 1, 2], dtype=dt),
  5849. np.array([1, 2, 3], dtype=dt),
  5850. np.array([2, 3, 3], dtype=dt),
  5851. np.array([3, 3, 0], dtype=dt)]
  5852. l = _multiarray_tests.test_neighborhood_iterator_oob(
  5853. x, [-1, 3], NEIGH_MODE['mirror'], [-1, 1], NEIGH_MODE['zero'])
  5854. assert_array_equal(l, r)
  5855. # Stacking mirror on top of zero
  5856. x = np.array([1, 2, 3], dtype=dt)
  5857. r = [np.array([1, 0, 0], dtype=dt),
  5858. np.array([0, 0, 1], dtype=dt),
  5859. np.array([0, 1, 2], dtype=dt),
  5860. np.array([1, 2, 3], dtype=dt),
  5861. np.array([2, 3, 0], dtype=dt)]
  5862. l = _multiarray_tests.test_neighborhood_iterator_oob(
  5863. x, [-1, 3], NEIGH_MODE['zero'], [-2, 0], NEIGH_MODE['mirror'])
  5864. assert_array_equal(l, r)
  5865. # Stacking mirror on top of zero: 2nd
  5866. x = np.array([1, 2, 3], dtype=dt)
  5867. r = [np.array([0, 1, 2], dtype=dt),
  5868. np.array([1, 2, 3], dtype=dt),
  5869. np.array([2, 3, 0], dtype=dt),
  5870. np.array([3, 0, 0], dtype=dt),
  5871. np.array([0, 0, 3], dtype=dt)]
  5872. l = _multiarray_tests.test_neighborhood_iterator_oob(
  5873. x, [-1, 3], NEIGH_MODE['zero'], [0, 2], NEIGH_MODE['mirror'])
  5874. assert_array_equal(l, r)
  5875. # Stacking mirror on top of zero: 3rd
  5876. x = np.array([1, 2, 3], dtype=dt)
  5877. r = [np.array([1, 0, 0, 1, 2], dtype=dt),
  5878. np.array([0, 0, 1, 2, 3], dtype=dt),
  5879. np.array([0, 1, 2, 3, 0], dtype=dt),
  5880. np.array([1, 2, 3, 0, 0], dtype=dt),
  5881. np.array([2, 3, 0, 0, 3], dtype=dt)]
  5882. l = _multiarray_tests.test_neighborhood_iterator_oob(
  5883. x, [-1, 3], NEIGH_MODE['zero'], [-2, 2], NEIGH_MODE['mirror'])
  5884. assert_array_equal(l, r)
  5885. # 3rd simple, 1d test: stacking 2 neigh iterators, mixing const padding and
  5886. # circular padding
  5887. def test_simple_circular(self):
  5888. dt = np.float64
  5889. # Stacking zero on top of mirror
  5890. x = np.array([1, 2, 3], dtype=dt)
  5891. r = [np.array([0, 3, 1], dtype=dt),
  5892. np.array([3, 1, 2], dtype=dt),
  5893. np.array([1, 2, 3], dtype=dt),
  5894. np.array([2, 3, 1], dtype=dt),
  5895. np.array([3, 1, 0], dtype=dt)]
  5896. l = _multiarray_tests.test_neighborhood_iterator_oob(
  5897. x, [-1, 3], NEIGH_MODE['circular'], [-1, 1], NEIGH_MODE['zero'])
  5898. assert_array_equal(l, r)
  5899. # Stacking mirror on top of zero
  5900. x = np.array([1, 2, 3], dtype=dt)
  5901. r = [np.array([3, 0, 0], dtype=dt),
  5902. np.array([0, 0, 1], dtype=dt),
  5903. np.array([0, 1, 2], dtype=dt),
  5904. np.array([1, 2, 3], dtype=dt),
  5905. np.array([2, 3, 0], dtype=dt)]
  5906. l = _multiarray_tests.test_neighborhood_iterator_oob(
  5907. x, [-1, 3], NEIGH_MODE['zero'], [-2, 0], NEIGH_MODE['circular'])
  5908. assert_array_equal(l, r)
  5909. # Stacking mirror on top of zero: 2nd
  5910. x = np.array([1, 2, 3], dtype=dt)
  5911. r = [np.array([0, 1, 2], dtype=dt),
  5912. np.array([1, 2, 3], dtype=dt),
  5913. np.array([2, 3, 0], dtype=dt),
  5914. np.array([3, 0, 0], dtype=dt),
  5915. np.array([0, 0, 1], dtype=dt)]
  5916. l = _multiarray_tests.test_neighborhood_iterator_oob(
  5917. x, [-1, 3], NEIGH_MODE['zero'], [0, 2], NEIGH_MODE['circular'])
  5918. assert_array_equal(l, r)
  5919. # Stacking mirror on top of zero: 3rd
  5920. x = np.array([1, 2, 3], dtype=dt)
  5921. r = [np.array([3, 0, 0, 1, 2], dtype=dt),
  5922. np.array([0, 0, 1, 2, 3], dtype=dt),
  5923. np.array([0, 1, 2, 3, 0], dtype=dt),
  5924. np.array([1, 2, 3, 0, 0], dtype=dt),
  5925. np.array([2, 3, 0, 0, 1], dtype=dt)]
  5926. l = _multiarray_tests.test_neighborhood_iterator_oob(
  5927. x, [-1, 3], NEIGH_MODE['zero'], [-2, 2], NEIGH_MODE['circular'])
  5928. assert_array_equal(l, r)
  5929. # 4th simple, 1d test: stacking 2 neigh iterators, but with lower iterator
  5930. # being strictly within the array
  5931. def test_simple_strict_within(self):
  5932. dt = np.float64
  5933. # Stacking zero on top of zero, first neighborhood strictly inside the
  5934. # array
  5935. x = np.array([1, 2, 3], dtype=dt)
  5936. r = [np.array([1, 2, 3, 0], dtype=dt)]
  5937. l = _multiarray_tests.test_neighborhood_iterator_oob(
  5938. x, [1, 1], NEIGH_MODE['zero'], [-1, 2], NEIGH_MODE['zero'])
  5939. assert_array_equal(l, r)
  5940. # Stacking mirror on top of zero, first neighborhood strictly inside the
  5941. # array
  5942. x = np.array([1, 2, 3], dtype=dt)
  5943. r = [np.array([1, 2, 3, 3], dtype=dt)]
  5944. l = _multiarray_tests.test_neighborhood_iterator_oob(
  5945. x, [1, 1], NEIGH_MODE['zero'], [-1, 2], NEIGH_MODE['mirror'])
  5946. assert_array_equal(l, r)
  5947. # Stacking mirror on top of zero, first neighborhood strictly inside the
  5948. # array
  5949. x = np.array([1, 2, 3], dtype=dt)
  5950. r = [np.array([1, 2, 3, 1], dtype=dt)]
  5951. l = _multiarray_tests.test_neighborhood_iterator_oob(
  5952. x, [1, 1], NEIGH_MODE['zero'], [-1, 2], NEIGH_MODE['circular'])
  5953. assert_array_equal(l, r)
  5954. class TestWarnings:
  5955. def test_complex_warning(self):
  5956. x = np.array([1, 2])
  5957. y = np.array([1-2j, 1+2j])
  5958. with warnings.catch_warnings():
  5959. warnings.simplefilter("error", np.ComplexWarning)
  5960. assert_raises(np.ComplexWarning, x.__setitem__, slice(None), y)
  5961. assert_equal(x, [1, 2])
  5962. class TestMinScalarType:
  5963. def test_usigned_shortshort(self):
  5964. dt = np.min_scalar_type(2**8-1)
  5965. wanted = np.dtype('uint8')
  5966. assert_equal(wanted, dt)
  5967. def test_usigned_short(self):
  5968. dt = np.min_scalar_type(2**16-1)
  5969. wanted = np.dtype('uint16')
  5970. assert_equal(wanted, dt)
  5971. def test_usigned_int(self):
  5972. dt = np.min_scalar_type(2**32-1)
  5973. wanted = np.dtype('uint32')
  5974. assert_equal(wanted, dt)
  5975. def test_usigned_longlong(self):
  5976. dt = np.min_scalar_type(2**63-1)
  5977. wanted = np.dtype('uint64')
  5978. assert_equal(wanted, dt)
  5979. def test_object(self):
  5980. dt = np.min_scalar_type(2**64)
  5981. wanted = np.dtype('O')
  5982. assert_equal(wanted, dt)
  5983. from numpy.core._internal import _dtype_from_pep3118
  5984. class TestPEP3118Dtype:
  5985. def _check(self, spec, wanted):
  5986. dt = np.dtype(wanted)
  5987. actual = _dtype_from_pep3118(spec)
  5988. assert_equal(actual, dt,
  5989. err_msg="spec %r != dtype %r" % (spec, wanted))
  5990. def test_native_padding(self):
  5991. align = np.dtype('i').alignment
  5992. for j in range(8):
  5993. if j == 0:
  5994. s = 'bi'
  5995. else:
  5996. s = 'b%dxi' % j
  5997. self._check('@'+s, {'f0': ('i1', 0),
  5998. 'f1': ('i', align*(1 + j//align))})
  5999. self._check('='+s, {'f0': ('i1', 0),
  6000. 'f1': ('i', 1+j)})
  6001. def test_native_padding_2(self):
  6002. # Native padding should work also for structs and sub-arrays
  6003. self._check('x3T{xi}', {'f0': (({'f0': ('i', 4)}, (3,)), 4)})
  6004. self._check('^x3T{xi}', {'f0': (({'f0': ('i', 1)}, (3,)), 1)})
  6005. def test_trailing_padding(self):
  6006. # Trailing padding should be included, *and*, the item size
  6007. # should match the alignment if in aligned mode
  6008. align = np.dtype('i').alignment
  6009. size = np.dtype('i').itemsize
  6010. def aligned(n):
  6011. return align*(1 + (n-1)//align)
  6012. base = dict(formats=['i'], names=['f0'])
  6013. self._check('ix', dict(itemsize=aligned(size + 1), **base))
  6014. self._check('ixx', dict(itemsize=aligned(size + 2), **base))
  6015. self._check('ixxx', dict(itemsize=aligned(size + 3), **base))
  6016. self._check('ixxxx', dict(itemsize=aligned(size + 4), **base))
  6017. self._check('i7x', dict(itemsize=aligned(size + 7), **base))
  6018. self._check('^ix', dict(itemsize=size + 1, **base))
  6019. self._check('^ixx', dict(itemsize=size + 2, **base))
  6020. self._check('^ixxx', dict(itemsize=size + 3, **base))
  6021. self._check('^ixxxx', dict(itemsize=size + 4, **base))
  6022. self._check('^i7x', dict(itemsize=size + 7, **base))
  6023. def test_native_padding_3(self):
  6024. dt = np.dtype(
  6025. [('a', 'b'), ('b', 'i'),
  6026. ('sub', np.dtype('b,i')), ('c', 'i')],
  6027. align=True)
  6028. self._check("T{b:a:xxxi:b:T{b:f0:=i:f1:}:sub:xxxi:c:}", dt)
  6029. dt = np.dtype(
  6030. [('a', 'b'), ('b', 'i'), ('c', 'b'), ('d', 'b'),
  6031. ('e', 'b'), ('sub', np.dtype('b,i', align=True))])
  6032. self._check("T{b:a:=i:b:b:c:b:d:b:e:T{b:f0:xxxi:f1:}:sub:}", dt)
  6033. def test_padding_with_array_inside_struct(self):
  6034. dt = np.dtype(
  6035. [('a', 'b'), ('b', 'i'), ('c', 'b', (3,)),
  6036. ('d', 'i')],
  6037. align=True)
  6038. self._check("T{b:a:xxxi:b:3b:c:xi:d:}", dt)
  6039. def test_byteorder_inside_struct(self):
  6040. # The byte order after @T{=i} should be '=', not '@'.
  6041. # Check this by noting the absence of native alignment.
  6042. self._check('@T{^i}xi', {'f0': ({'f0': ('i', 0)}, 0),
  6043. 'f1': ('i', 5)})
  6044. def test_intra_padding(self):
  6045. # Natively aligned sub-arrays may require some internal padding
  6046. align = np.dtype('i').alignment
  6047. size = np.dtype('i').itemsize
  6048. def aligned(n):
  6049. return (align*(1 + (n-1)//align))
  6050. self._check('(3)T{ix}', (dict(
  6051. names=['f0'],
  6052. formats=['i'],
  6053. offsets=[0],
  6054. itemsize=aligned(size + 1)
  6055. ), (3,)))
  6056. def test_char_vs_string(self):
  6057. dt = np.dtype('c')
  6058. self._check('c', dt)
  6059. dt = np.dtype([('f0', 'S1', (4,)), ('f1', 'S4')])
  6060. self._check('4c4s', dt)
  6061. def test_field_order(self):
  6062. # gh-9053 - previously, we relied on dictionary key order
  6063. self._check("(0)I:a:f:b:", [('a', 'I', (0,)), ('b', 'f')])
  6064. self._check("(0)I:b:f:a:", [('b', 'I', (0,)), ('a', 'f')])
  6065. def test_unnamed_fields(self):
  6066. self._check('ii', [('f0', 'i'), ('f1', 'i')])
  6067. self._check('ii:f0:', [('f1', 'i'), ('f0', 'i')])
  6068. self._check('i', 'i')
  6069. self._check('i:f0:', [('f0', 'i')])
  6070. class TestNewBufferProtocol:
  6071. """ Test PEP3118 buffers """
  6072. def _check_roundtrip(self, obj):
  6073. obj = np.asarray(obj)
  6074. x = memoryview(obj)
  6075. y = np.asarray(x)
  6076. y2 = np.array(x)
  6077. assert_(not y.flags.owndata)
  6078. assert_(y2.flags.owndata)
  6079. assert_equal(y.dtype, obj.dtype)
  6080. assert_equal(y.shape, obj.shape)
  6081. assert_array_equal(obj, y)
  6082. assert_equal(y2.dtype, obj.dtype)
  6083. assert_equal(y2.shape, obj.shape)
  6084. assert_array_equal(obj, y2)
  6085. def test_roundtrip(self):
  6086. x = np.array([1, 2, 3, 4, 5], dtype='i4')
  6087. self._check_roundtrip(x)
  6088. x = np.array([[1, 2], [3, 4]], dtype=np.float64)
  6089. self._check_roundtrip(x)
  6090. x = np.zeros((3, 3, 3), dtype=np.float32)[:, 0,:]
  6091. self._check_roundtrip(x)
  6092. dt = [('a', 'b'),
  6093. ('b', 'h'),
  6094. ('c', 'i'),
  6095. ('d', 'l'),
  6096. ('dx', 'q'),
  6097. ('e', 'B'),
  6098. ('f', 'H'),
  6099. ('g', 'I'),
  6100. ('h', 'L'),
  6101. ('hx', 'Q'),
  6102. ('i', np.single),
  6103. ('j', np.double),
  6104. ('k', np.longdouble),
  6105. ('ix', np.csingle),
  6106. ('jx', np.cdouble),
  6107. ('kx', np.clongdouble),
  6108. ('l', 'S4'),
  6109. ('m', 'U4'),
  6110. ('n', 'V3'),
  6111. ('o', '?'),
  6112. ('p', np.half),
  6113. ]
  6114. x = np.array(
  6115. [(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6116. b'aaaa', 'bbbb', b'xxx', True, 1.0)],
  6117. dtype=dt)
  6118. self._check_roundtrip(x)
  6119. x = np.array(([[1, 2], [3, 4]],), dtype=[('a', (int, (2, 2)))])
  6120. self._check_roundtrip(x)
  6121. x = np.array([1, 2, 3], dtype='>i2')
  6122. self._check_roundtrip(x)
  6123. x = np.array([1, 2, 3], dtype='<i2')
  6124. self._check_roundtrip(x)
  6125. x = np.array([1, 2, 3], dtype='>i4')
  6126. self._check_roundtrip(x)
  6127. x = np.array([1, 2, 3], dtype='<i4')
  6128. self._check_roundtrip(x)
  6129. # check long long can be represented as non-native
  6130. x = np.array([1, 2, 3], dtype='>q')
  6131. self._check_roundtrip(x)
  6132. # Native-only data types can be passed through the buffer interface
  6133. # only in native byte order
  6134. if sys.byteorder == 'little':
  6135. x = np.array([1, 2, 3], dtype='>g')
  6136. assert_raises(ValueError, self._check_roundtrip, x)
  6137. x = np.array([1, 2, 3], dtype='<g')
  6138. self._check_roundtrip(x)
  6139. else:
  6140. x = np.array([1, 2, 3], dtype='>g')
  6141. self._check_roundtrip(x)
  6142. x = np.array([1, 2, 3], dtype='<g')
  6143. assert_raises(ValueError, self._check_roundtrip, x)
  6144. def test_roundtrip_half(self):
  6145. half_list = [
  6146. 1.0,
  6147. -2.0,
  6148. 6.5504 * 10**4, # (max half precision)
  6149. 2**-14, # ~= 6.10352 * 10**-5 (minimum positive normal)
  6150. 2**-24, # ~= 5.96046 * 10**-8 (minimum strictly positive subnormal)
  6151. 0.0,
  6152. -0.0,
  6153. float('+inf'),
  6154. float('-inf'),
  6155. 0.333251953125, # ~= 1/3
  6156. ]
  6157. x = np.array(half_list, dtype='>e')
  6158. self._check_roundtrip(x)
  6159. x = np.array(half_list, dtype='<e')
  6160. self._check_roundtrip(x)
  6161. def test_roundtrip_single_types(self):
  6162. for typ in np.typeDict.values():
  6163. dtype = np.dtype(typ)
  6164. if dtype.char in 'Mm':
  6165. # datetimes cannot be used in buffers
  6166. continue
  6167. if dtype.char == 'V':
  6168. # skip void
  6169. continue
  6170. x = np.zeros(4, dtype=dtype)
  6171. self._check_roundtrip(x)
  6172. if dtype.char not in 'qQgG':
  6173. dt = dtype.newbyteorder('<')
  6174. x = np.zeros(4, dtype=dt)
  6175. self._check_roundtrip(x)
  6176. dt = dtype.newbyteorder('>')
  6177. x = np.zeros(4, dtype=dt)
  6178. self._check_roundtrip(x)
  6179. def test_roundtrip_scalar(self):
  6180. # Issue #4015.
  6181. self._check_roundtrip(0)
  6182. def test_invalid_buffer_format(self):
  6183. # datetime64 cannot be used fully in a buffer yet
  6184. # Should be fixed in the next Numpy major release
  6185. dt = np.dtype([('a', 'uint16'), ('b', 'M8[s]')])
  6186. a = np.empty(3, dt)
  6187. assert_raises((ValueError, BufferError), memoryview, a)
  6188. assert_raises((ValueError, BufferError), memoryview, np.array((3), 'M8[D]'))
  6189. def test_export_simple_1d(self):
  6190. x = np.array([1, 2, 3, 4, 5], dtype='i')
  6191. y = memoryview(x)
  6192. assert_equal(y.format, 'i')
  6193. assert_equal(y.shape, (5,))
  6194. assert_equal(y.ndim, 1)
  6195. assert_equal(y.strides, (4,))
  6196. assert_equal(y.suboffsets, ())
  6197. assert_equal(y.itemsize, 4)
  6198. def test_export_simple_nd(self):
  6199. x = np.array([[1, 2], [3, 4]], dtype=np.float64)
  6200. y = memoryview(x)
  6201. assert_equal(y.format, 'd')
  6202. assert_equal(y.shape, (2, 2))
  6203. assert_equal(y.ndim, 2)
  6204. assert_equal(y.strides, (16, 8))
  6205. assert_equal(y.suboffsets, ())
  6206. assert_equal(y.itemsize, 8)
  6207. def test_export_discontiguous(self):
  6208. x = np.zeros((3, 3, 3), dtype=np.float32)[:, 0,:]
  6209. y = memoryview(x)
  6210. assert_equal(y.format, 'f')
  6211. assert_equal(y.shape, (3, 3))
  6212. assert_equal(y.ndim, 2)
  6213. assert_equal(y.strides, (36, 4))
  6214. assert_equal(y.suboffsets, ())
  6215. assert_equal(y.itemsize, 4)
  6216. def test_export_record(self):
  6217. dt = [('a', 'b'),
  6218. ('b', 'h'),
  6219. ('c', 'i'),
  6220. ('d', 'l'),
  6221. ('dx', 'q'),
  6222. ('e', 'B'),
  6223. ('f', 'H'),
  6224. ('g', 'I'),
  6225. ('h', 'L'),
  6226. ('hx', 'Q'),
  6227. ('i', np.single),
  6228. ('j', np.double),
  6229. ('k', np.longdouble),
  6230. ('ix', np.csingle),
  6231. ('jx', np.cdouble),
  6232. ('kx', np.clongdouble),
  6233. ('l', 'S4'),
  6234. ('m', 'U4'),
  6235. ('n', 'V3'),
  6236. ('o', '?'),
  6237. ('p', np.half),
  6238. ]
  6239. x = np.array(
  6240. [(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6241. b'aaaa', 'bbbb', b' ', True, 1.0)],
  6242. dtype=dt)
  6243. y = memoryview(x)
  6244. assert_equal(y.shape, (1,))
  6245. assert_equal(y.ndim, 1)
  6246. assert_equal(y.suboffsets, ())
  6247. sz = sum([np.dtype(b).itemsize for a, b in dt])
  6248. if np.dtype('l').itemsize == 4:
  6249. assert_equal(y.format, 'T{b:a:=h:b:i:c:l:d:q:dx:B:e:@H:f:=I:g:L:h:Q:hx:f:i:d:j:^g:k:=Zf:ix:Zd:jx:^Zg:kx:4s:l:=4w:m:3x:n:?:o:@e:p:}')
  6250. else:
  6251. assert_equal(y.format, 'T{b:a:=h:b:i:c:q:d:q:dx:B:e:@H:f:=I:g:Q:h:Q:hx:f:i:d:j:^g:k:=Zf:ix:Zd:jx:^Zg:kx:4s:l:=4w:m:3x:n:?:o:@e:p:}')
  6252. # Cannot test if NPY_RELAXED_STRIDES_CHECKING changes the strides
  6253. if not (np.ones(1).strides[0] == np.iinfo(np.intp).max):
  6254. assert_equal(y.strides, (sz,))
  6255. assert_equal(y.itemsize, sz)
  6256. def test_export_subarray(self):
  6257. x = np.array(([[1, 2], [3, 4]],), dtype=[('a', ('i', (2, 2)))])
  6258. y = memoryview(x)
  6259. assert_equal(y.format, 'T{(2,2)i:a:}')
  6260. assert_equal(y.shape, ())
  6261. assert_equal(y.ndim, 0)
  6262. assert_equal(y.strides, ())
  6263. assert_equal(y.suboffsets, ())
  6264. assert_equal(y.itemsize, 16)
  6265. def test_export_endian(self):
  6266. x = np.array([1, 2, 3], dtype='>i')
  6267. y = memoryview(x)
  6268. if sys.byteorder == 'little':
  6269. assert_equal(y.format, '>i')
  6270. else:
  6271. assert_equal(y.format, 'i')
  6272. x = np.array([1, 2, 3], dtype='<i')
  6273. y = memoryview(x)
  6274. if sys.byteorder == 'little':
  6275. assert_equal(y.format, 'i')
  6276. else:
  6277. assert_equal(y.format, '<i')
  6278. def test_export_flags(self):
  6279. # Check SIMPLE flag, see also gh-3613 (exception should be BufferError)
  6280. assert_raises(ValueError,
  6281. _multiarray_tests.get_buffer_info,
  6282. np.arange(5)[::2], ('SIMPLE',))
  6283. @pytest.mark.parametrize(["obj", "error"], [
  6284. pytest.param(np.array([1, 2], dtype=rational), ValueError, id="array"),
  6285. pytest.param(rational(1, 2), TypeError, id="scalar")])
  6286. def test_export_and_pickle_user_dtype(self, obj, error):
  6287. # User dtypes should export successfully when FORMAT was not requested.
  6288. with pytest.raises(error):
  6289. _multiarray_tests.get_buffer_info(obj, ("STRIDED_RO", "FORMAT"))
  6290. _multiarray_tests.get_buffer_info(obj, ("STRIDED_RO",))
  6291. # This is currently also necessary to implement pickling:
  6292. pickle_obj = pickle.dumps(obj)
  6293. res = pickle.loads(pickle_obj)
  6294. assert_array_equal(res, obj)
  6295. def test_padding(self):
  6296. for j in range(8):
  6297. x = np.array([(1,), (2,)], dtype={'f0': (int, j)})
  6298. self._check_roundtrip(x)
  6299. def test_reference_leak(self):
  6300. if HAS_REFCOUNT:
  6301. count_1 = sys.getrefcount(np.core._internal)
  6302. a = np.zeros(4)
  6303. b = memoryview(a)
  6304. c = np.asarray(b)
  6305. if HAS_REFCOUNT:
  6306. count_2 = sys.getrefcount(np.core._internal)
  6307. assert_equal(count_1, count_2)
  6308. del c # avoid pyflakes unused variable warning.
  6309. def test_padded_struct_array(self):
  6310. dt1 = np.dtype(
  6311. [('a', 'b'), ('b', 'i'), ('sub', np.dtype('b,i')), ('c', 'i')],
  6312. align=True)
  6313. x1 = np.arange(dt1.itemsize, dtype=np.int8).view(dt1)
  6314. self._check_roundtrip(x1)
  6315. dt2 = np.dtype(
  6316. [('a', 'b'), ('b', 'i'), ('c', 'b', (3,)), ('d', 'i')],
  6317. align=True)
  6318. x2 = np.arange(dt2.itemsize, dtype=np.int8).view(dt2)
  6319. self._check_roundtrip(x2)
  6320. dt3 = np.dtype(
  6321. [('a', 'b'), ('b', 'i'), ('c', 'b'), ('d', 'b'),
  6322. ('e', 'b'), ('sub', np.dtype('b,i', align=True))])
  6323. x3 = np.arange(dt3.itemsize, dtype=np.int8).view(dt3)
  6324. self._check_roundtrip(x3)
  6325. @pytest.mark.valgrind_error(reason="leaks buffer info cache temporarily.")
  6326. def test_relaxed_strides(self, c=np.ones((1, 10, 10), dtype='i8')):
  6327. # Note: c defined as parameter so that it is persistent and leak
  6328. # checks will notice gh-16934 (buffer info cache leak).
  6329. # Check for NPY_RELAXED_STRIDES_CHECKING:
  6330. if np.ones((10, 1), order="C").flags.f_contiguous:
  6331. c.strides = (-1, 80, 8)
  6332. assert_(memoryview(c).strides == (800, 80, 8))
  6333. # Writing C-contiguous data to a BytesIO buffer should work
  6334. fd = io.BytesIO()
  6335. fd.write(c.data)
  6336. fortran = c.T
  6337. assert_(memoryview(fortran).strides == (8, 80, 800))
  6338. arr = np.ones((1, 10))
  6339. if arr.flags.f_contiguous:
  6340. shape, strides = _multiarray_tests.get_buffer_info(
  6341. arr, ['F_CONTIGUOUS'])
  6342. assert_(strides[0] == 8)
  6343. arr = np.ones((10, 1), order='F')
  6344. shape, strides = _multiarray_tests.get_buffer_info(
  6345. arr, ['C_CONTIGUOUS'])
  6346. assert_(strides[-1] == 8)
  6347. @pytest.mark.valgrind_error(reason="leaks buffer info cache temporarily.")
  6348. @pytest.mark.skipif(not np.ones((10, 1), order="C").flags.f_contiguous,
  6349. reason="Test is unnecessary (but fails) without relaxed strides.")
  6350. def test_relaxed_strides_buffer_info_leak(self, arr=np.ones((1, 10))):
  6351. """Test that alternating export of C- and F-order buffers from
  6352. an array which is both C- and F-order when relaxed strides is
  6353. active works.
  6354. This test defines array in the signature to ensure leaking more
  6355. references every time the test is run (catching the leak with
  6356. pytest-leaks).
  6357. """
  6358. for i in range(10):
  6359. _, s = _multiarray_tests.get_buffer_info(arr, ['F_CONTIGUOUS'])
  6360. assert s == (8, 8)
  6361. _, s = _multiarray_tests.get_buffer_info(arr, ['C_CONTIGUOUS'])
  6362. assert s == (80, 8)
  6363. def test_out_of_order_fields(self):
  6364. dt = np.dtype(dict(
  6365. formats=['<i4', '<i4'],
  6366. names=['one', 'two'],
  6367. offsets=[4, 0],
  6368. itemsize=8
  6369. ))
  6370. # overlapping fields cannot be represented by PEP3118
  6371. arr = np.empty(1, dt)
  6372. with assert_raises(ValueError):
  6373. memoryview(arr)
  6374. def test_max_dims(self):
  6375. a = np.ones((1,) * 32)
  6376. self._check_roundtrip(a)
  6377. @pytest.mark.slow
  6378. def test_error_too_many_dims(self):
  6379. def make_ctype(shape, scalar_type):
  6380. t = scalar_type
  6381. for dim in shape[::-1]:
  6382. t = dim * t
  6383. return t
  6384. # construct a memoryview with 33 dimensions
  6385. c_u8_33d = make_ctype((1,)*33, ctypes.c_uint8)
  6386. m = memoryview(c_u8_33d())
  6387. assert_equal(m.ndim, 33)
  6388. assert_raises_regex(
  6389. RuntimeError, "ndim",
  6390. np.array, m)
  6391. # The above seems to create some deep cycles, clean them up for
  6392. # easier reference count debugging:
  6393. del c_u8_33d, m
  6394. for i in range(33):
  6395. if gc.collect() == 0:
  6396. break
  6397. def test_error_pointer_type(self):
  6398. # gh-6741
  6399. m = memoryview(ctypes.pointer(ctypes.c_uint8()))
  6400. assert_('&' in m.format)
  6401. assert_raises_regex(
  6402. ValueError, "format string",
  6403. np.array, m)
  6404. def test_error_message_unsupported(self):
  6405. # wchar has no corresponding numpy type - if this changes in future, we
  6406. # need a better way to construct an invalid memoryview format.
  6407. t = ctypes.c_wchar * 4
  6408. with assert_raises(ValueError) as cm:
  6409. np.array(t())
  6410. exc = cm.exception
  6411. with assert_raises_regex(
  6412. NotImplementedError,
  6413. r"Unrepresentable .* 'u' \(UCS-2 strings\)"
  6414. ):
  6415. raise exc.__cause__
  6416. def test_ctypes_integer_via_memoryview(self):
  6417. # gh-11150, due to bpo-10746
  6418. for c_integer in {ctypes.c_int, ctypes.c_long, ctypes.c_longlong}:
  6419. value = c_integer(42)
  6420. with warnings.catch_warnings(record=True):
  6421. warnings.filterwarnings('always', r'.*\bctypes\b', RuntimeWarning)
  6422. np.asarray(value)
  6423. def test_ctypes_struct_via_memoryview(self):
  6424. # gh-10528
  6425. class foo(ctypes.Structure):
  6426. _fields_ = [('a', ctypes.c_uint8), ('b', ctypes.c_uint32)]
  6427. f = foo(a=1, b=2)
  6428. with warnings.catch_warnings(record=True):
  6429. warnings.filterwarnings('always', r'.*\bctypes\b', RuntimeWarning)
  6430. arr = np.asarray(f)
  6431. assert_equal(arr['a'], 1)
  6432. assert_equal(arr['b'], 2)
  6433. f.a = 3
  6434. assert_equal(arr['a'], 3)
  6435. @pytest.mark.parametrize("obj", [np.ones(3), np.ones(1, dtype="i,i")[()]])
  6436. def test_error_if_stored_buffer_info_is_corrupted(self, obj):
  6437. """
  6438. If a user extends a NumPy array before 1.20 and then runs it
  6439. on NumPy 1.20+. A C-subclassed array might in theory modify
  6440. the new buffer-info field. This checks that an error is raised
  6441. if this happens (for buffer export), an error is written on delete.
  6442. This is a sanity check to help users transition to safe code, it
  6443. may be deleted at any point.
  6444. """
  6445. # corrupt buffer info:
  6446. _multiarray_tests.corrupt_or_fix_bufferinfo(obj)
  6447. name = type(obj)
  6448. with pytest.raises(RuntimeError,
  6449. match=f".*{name} appears to be C subclassed"):
  6450. memoryview(obj)
  6451. # Fix buffer info again before we delete (or we lose the memory)
  6452. _multiarray_tests.corrupt_or_fix_bufferinfo(obj)
  6453. class TestArrayAttributeDeletion:
  6454. def test_multiarray_writable_attributes_deletion(self):
  6455. # ticket #2046, should not seqfault, raise AttributeError
  6456. a = np.ones(2)
  6457. attr = ['shape', 'strides', 'data', 'dtype', 'real', 'imag', 'flat']
  6458. with suppress_warnings() as sup:
  6459. sup.filter(DeprecationWarning, "Assigning the 'data' attribute")
  6460. for s in attr:
  6461. assert_raises(AttributeError, delattr, a, s)
  6462. def test_multiarray_not_writable_attributes_deletion(self):
  6463. a = np.ones(2)
  6464. attr = ["ndim", "flags", "itemsize", "size", "nbytes", "base",
  6465. "ctypes", "T", "__array_interface__", "__array_struct__",
  6466. "__array_priority__", "__array_finalize__"]
  6467. for s in attr:
  6468. assert_raises(AttributeError, delattr, a, s)
  6469. def test_multiarray_flags_writable_attribute_deletion(self):
  6470. a = np.ones(2).flags
  6471. attr = ['writebackifcopy', 'updateifcopy', 'aligned', 'writeable']
  6472. for s in attr:
  6473. assert_raises(AttributeError, delattr, a, s)
  6474. def test_multiarray_flags_not_writable_attribute_deletion(self):
  6475. a = np.ones(2).flags
  6476. attr = ["contiguous", "c_contiguous", "f_contiguous", "fortran",
  6477. "owndata", "fnc", "forc", "behaved", "carray", "farray",
  6478. "num"]
  6479. for s in attr:
  6480. assert_raises(AttributeError, delattr, a, s)
  6481. class TestArrayInterface():
  6482. class Foo:
  6483. def __init__(self, value):
  6484. self.value = value
  6485. self.iface = {'typestr': 'f8'}
  6486. def __float__(self):
  6487. return float(self.value)
  6488. @property
  6489. def __array_interface__(self):
  6490. return self.iface
  6491. f = Foo(0.5)
  6492. @pytest.mark.parametrize('val, iface, expected', [
  6493. (f, {}, 0.5),
  6494. ([f], {}, [0.5]),
  6495. ([f, f], {}, [0.5, 0.5]),
  6496. (f, {'shape': ()}, 0.5),
  6497. (f, {'shape': None}, TypeError),
  6498. (f, {'shape': (1, 1)}, [[0.5]]),
  6499. (f, {'shape': (2,)}, ValueError),
  6500. (f, {'strides': ()}, 0.5),
  6501. (f, {'strides': (2,)}, ValueError),
  6502. (f, {'strides': 16}, TypeError),
  6503. ])
  6504. def test_scalar_interface(self, val, iface, expected):
  6505. # Test scalar coercion within the array interface
  6506. self.f.iface = {'typestr': 'f8'}
  6507. self.f.iface.update(iface)
  6508. if HAS_REFCOUNT:
  6509. pre_cnt = sys.getrefcount(np.dtype('f8'))
  6510. if isinstance(expected, type):
  6511. assert_raises(expected, np.array, val)
  6512. else:
  6513. result = np.array(val)
  6514. assert_equal(np.array(val), expected)
  6515. assert result.dtype == 'f8'
  6516. del result
  6517. if HAS_REFCOUNT:
  6518. post_cnt = sys.getrefcount(np.dtype('f8'))
  6519. assert_equal(pre_cnt, post_cnt)
  6520. def test_interface_no_shape():
  6521. class ArrayLike:
  6522. array = np.array(1)
  6523. __array_interface__ = array.__array_interface__
  6524. assert_equal(np.array(ArrayLike()), 1)
  6525. def test_array_interface_itemsize():
  6526. # See gh-6361
  6527. my_dtype = np.dtype({'names': ['A', 'B'], 'formats': ['f4', 'f4'],
  6528. 'offsets': [0, 8], 'itemsize': 16})
  6529. a = np.ones(10, dtype=my_dtype)
  6530. descr_t = np.dtype(a.__array_interface__['descr'])
  6531. typestr_t = np.dtype(a.__array_interface__['typestr'])
  6532. assert_equal(descr_t.itemsize, typestr_t.itemsize)
  6533. def test_array_interface_empty_shape():
  6534. # See gh-7994
  6535. arr = np.array([1, 2, 3])
  6536. interface1 = dict(arr.__array_interface__)
  6537. interface1['shape'] = ()
  6538. class DummyArray1:
  6539. __array_interface__ = interface1
  6540. # NOTE: Because Py2 str/Py3 bytes supports the buffer interface, setting
  6541. # the interface data to bytes would invoke the bug this tests for, that
  6542. # __array_interface__ with shape=() is not allowed if the data is an object
  6543. # exposing the buffer interface
  6544. interface2 = dict(interface1)
  6545. interface2['data'] = arr[0].tobytes()
  6546. class DummyArray2:
  6547. __array_interface__ = interface2
  6548. arr1 = np.asarray(DummyArray1())
  6549. arr2 = np.asarray(DummyArray2())
  6550. arr3 = arr[:1].reshape(())
  6551. assert_equal(arr1, arr2)
  6552. assert_equal(arr1, arr3)
  6553. def test_array_interface_offset():
  6554. arr = np.array([1, 2, 3], dtype='int32')
  6555. interface = dict(arr.__array_interface__)
  6556. interface['data'] = memoryview(arr)
  6557. interface['shape'] = (2,)
  6558. interface['offset'] = 4
  6559. class DummyArray:
  6560. __array_interface__ = interface
  6561. arr1 = np.asarray(DummyArray())
  6562. assert_equal(arr1, arr[1:])
  6563. def test_array_interface_unicode_typestr():
  6564. arr = np.array([1, 2, 3], dtype='int32')
  6565. interface = dict(arr.__array_interface__)
  6566. interface['typestr'] = '\N{check mark}'
  6567. class DummyArray:
  6568. __array_interface__ = interface
  6569. # should not be UnicodeEncodeError
  6570. with pytest.raises(TypeError):
  6571. np.asarray(DummyArray())
  6572. def test_flat_element_deletion():
  6573. it = np.ones(3).flat
  6574. try:
  6575. del it[1]
  6576. del it[1:2]
  6577. except TypeError:
  6578. pass
  6579. except Exception:
  6580. raise AssertionError
  6581. def test_scalar_element_deletion():
  6582. a = np.zeros(2, dtype=[('x', 'int'), ('y', 'int')])
  6583. assert_raises(ValueError, a[0].__delitem__, 'x')
  6584. class TestMemEventHook:
  6585. def test_mem_seteventhook(self):
  6586. # The actual tests are within the C code in
  6587. # multiarray/_multiarray_tests.c.src
  6588. _multiarray_tests.test_pydatamem_seteventhook_start()
  6589. # force an allocation and free of a numpy array
  6590. # needs to be larger then limit of small memory cacher in ctors.c
  6591. a = np.zeros(1000)
  6592. del a
  6593. break_cycles()
  6594. _multiarray_tests.test_pydatamem_seteventhook_end()
  6595. class TestMapIter:
  6596. def test_mapiter(self):
  6597. # The actual tests are within the C code in
  6598. # multiarray/_multiarray_tests.c.src
  6599. a = np.arange(12).reshape((3, 4)).astype(float)
  6600. index = ([1, 1, 2, 0],
  6601. [0, 0, 2, 3])
  6602. vals = [50, 50, 30, 16]
  6603. _multiarray_tests.test_inplace_increment(a, index, vals)
  6604. assert_equal(a, [[0.00, 1., 2.0, 19.],
  6605. [104., 5., 6.0, 7.0],
  6606. [8.00, 9., 40., 11.]])
  6607. b = np.arange(6).astype(float)
  6608. index = (np.array([1, 2, 0]),)
  6609. vals = [50, 4, 100.1]
  6610. _multiarray_tests.test_inplace_increment(b, index, vals)
  6611. assert_equal(b, [100.1, 51., 6., 3., 4., 5.])
  6612. class TestAsCArray:
  6613. def test_1darray(self):
  6614. array = np.arange(24, dtype=np.double)
  6615. from_c = _multiarray_tests.test_as_c_array(array, 3)
  6616. assert_equal(array[3], from_c)
  6617. def test_2darray(self):
  6618. array = np.arange(24, dtype=np.double).reshape(3, 8)
  6619. from_c = _multiarray_tests.test_as_c_array(array, 2, 4)
  6620. assert_equal(array[2, 4], from_c)
  6621. def test_3darray(self):
  6622. array = np.arange(24, dtype=np.double).reshape(2, 3, 4)
  6623. from_c = _multiarray_tests.test_as_c_array(array, 1, 2, 3)
  6624. assert_equal(array[1, 2, 3], from_c)
  6625. class TestConversion:
  6626. def test_array_scalar_relational_operation(self):
  6627. # All integer
  6628. for dt1 in np.typecodes['AllInteger']:
  6629. assert_(1 > np.array(0, dtype=dt1), "type %s failed" % (dt1,))
  6630. assert_(not 1 < np.array(0, dtype=dt1), "type %s failed" % (dt1,))
  6631. for dt2 in np.typecodes['AllInteger']:
  6632. assert_(np.array(1, dtype=dt1) > np.array(0, dtype=dt2),
  6633. "type %s and %s failed" % (dt1, dt2))
  6634. assert_(not np.array(1, dtype=dt1) < np.array(0, dtype=dt2),
  6635. "type %s and %s failed" % (dt1, dt2))
  6636. # Unsigned integers
  6637. for dt1 in 'BHILQP':
  6638. assert_(-1 < np.array(1, dtype=dt1), "type %s failed" % (dt1,))
  6639. assert_(not -1 > np.array(1, dtype=dt1), "type %s failed" % (dt1,))
  6640. assert_(-1 != np.array(1, dtype=dt1), "type %s failed" % (dt1,))
  6641. # Unsigned vs signed
  6642. for dt2 in 'bhilqp':
  6643. assert_(np.array(1, dtype=dt1) > np.array(-1, dtype=dt2),
  6644. "type %s and %s failed" % (dt1, dt2))
  6645. assert_(not np.array(1, dtype=dt1) < np.array(-1, dtype=dt2),
  6646. "type %s and %s failed" % (dt1, dt2))
  6647. assert_(np.array(1, dtype=dt1) != np.array(-1, dtype=dt2),
  6648. "type %s and %s failed" % (dt1, dt2))
  6649. # Signed integers and floats
  6650. for dt1 in 'bhlqp' + np.typecodes['Float']:
  6651. assert_(1 > np.array(-1, dtype=dt1), "type %s failed" % (dt1,))
  6652. assert_(not 1 < np.array(-1, dtype=dt1), "type %s failed" % (dt1,))
  6653. assert_(-1 == np.array(-1, dtype=dt1), "type %s failed" % (dt1,))
  6654. for dt2 in 'bhlqp' + np.typecodes['Float']:
  6655. assert_(np.array(1, dtype=dt1) > np.array(-1, dtype=dt2),
  6656. "type %s and %s failed" % (dt1, dt2))
  6657. assert_(not np.array(1, dtype=dt1) < np.array(-1, dtype=dt2),
  6658. "type %s and %s failed" % (dt1, dt2))
  6659. assert_(np.array(-1, dtype=dt1) == np.array(-1, dtype=dt2),
  6660. "type %s and %s failed" % (dt1, dt2))
  6661. def test_to_bool_scalar(self):
  6662. assert_equal(bool(np.array([False])), False)
  6663. assert_equal(bool(np.array([True])), True)
  6664. assert_equal(bool(np.array([[42]])), True)
  6665. assert_raises(ValueError, bool, np.array([1, 2]))
  6666. class NotConvertible:
  6667. def __bool__(self):
  6668. raise NotImplementedError
  6669. assert_raises(NotImplementedError, bool, np.array(NotConvertible()))
  6670. assert_raises(NotImplementedError, bool, np.array([NotConvertible()]))
  6671. self_containing = np.array([None])
  6672. self_containing[0] = self_containing
  6673. try:
  6674. Error = RecursionError
  6675. except NameError:
  6676. Error = RuntimeError # python < 3.5
  6677. assert_raises(Error, bool, self_containing) # previously stack overflow
  6678. self_containing[0] = None # resolve circular reference
  6679. def test_to_int_scalar(self):
  6680. # gh-9972 means that these aren't always the same
  6681. int_funcs = (int, lambda x: x.__int__())
  6682. for int_func in int_funcs:
  6683. assert_equal(int_func(np.array(0)), 0)
  6684. assert_equal(int_func(np.array([1])), 1)
  6685. assert_equal(int_func(np.array([[42]])), 42)
  6686. assert_raises(TypeError, int_func, np.array([1, 2]))
  6687. # gh-9972
  6688. assert_equal(4, int_func(np.array('4')))
  6689. assert_equal(5, int_func(np.bytes_(b'5')))
  6690. assert_equal(6, int_func(np.unicode_(u'6')))
  6691. class HasTrunc:
  6692. def __trunc__(self):
  6693. return 3
  6694. assert_equal(3, int_func(np.array(HasTrunc())))
  6695. assert_equal(3, int_func(np.array([HasTrunc()])))
  6696. class NotConvertible:
  6697. def __int__(self):
  6698. raise NotImplementedError
  6699. assert_raises(NotImplementedError,
  6700. int_func, np.array(NotConvertible()))
  6701. assert_raises(NotImplementedError,
  6702. int_func, np.array([NotConvertible()]))
  6703. class TestWhere:
  6704. def test_basic(self):
  6705. dts = [bool, np.int16, np.int32, np.int64, np.double, np.complex128,
  6706. np.longdouble, np.clongdouble]
  6707. for dt in dts:
  6708. c = np.ones(53, dtype=bool)
  6709. assert_equal(np.where( c, dt(0), dt(1)), dt(0))
  6710. assert_equal(np.where(~c, dt(0), dt(1)), dt(1))
  6711. assert_equal(np.where(True, dt(0), dt(1)), dt(0))
  6712. assert_equal(np.where(False, dt(0), dt(1)), dt(1))
  6713. d = np.ones_like(c).astype(dt)
  6714. e = np.zeros_like(d)
  6715. r = d.astype(dt)
  6716. c[7] = False
  6717. r[7] = e[7]
  6718. assert_equal(np.where(c, e, e), e)
  6719. assert_equal(np.where(c, d, e), r)
  6720. assert_equal(np.where(c, d, e[0]), r)
  6721. assert_equal(np.where(c, d[0], e), r)
  6722. assert_equal(np.where(c[::2], d[::2], e[::2]), r[::2])
  6723. assert_equal(np.where(c[1::2], d[1::2], e[1::2]), r[1::2])
  6724. assert_equal(np.where(c[::3], d[::3], e[::3]), r[::3])
  6725. assert_equal(np.where(c[1::3], d[1::3], e[1::3]), r[1::3])
  6726. assert_equal(np.where(c[::-2], d[::-2], e[::-2]), r[::-2])
  6727. assert_equal(np.where(c[::-3], d[::-3], e[::-3]), r[::-3])
  6728. assert_equal(np.where(c[1::-3], d[1::-3], e[1::-3]), r[1::-3])
  6729. def test_exotic(self):
  6730. # object
  6731. assert_array_equal(np.where(True, None, None), np.array(None))
  6732. # zero sized
  6733. m = np.array([], dtype=bool).reshape(0, 3)
  6734. b = np.array([], dtype=np.float64).reshape(0, 3)
  6735. assert_array_equal(np.where(m, 0, b), np.array([]).reshape(0, 3))
  6736. # object cast
  6737. d = np.array([-1.34, -0.16, -0.54, -0.31, -0.08, -0.95, 0.000, 0.313,
  6738. 0.547, -0.18, 0.876, 0.236, 1.969, 0.310, 0.699, 1.013,
  6739. 1.267, 0.229, -1.39, 0.487])
  6740. nan = float('NaN')
  6741. e = np.array(['5z', '0l', nan, 'Wz', nan, nan, 'Xq', 'cs', nan, nan,
  6742. 'QN', nan, nan, 'Fd', nan, nan, 'kp', nan, '36', 'i1'],
  6743. dtype=object)
  6744. m = np.array([0, 0, 1, 0, 1, 1, 0, 0, 1, 1,
  6745. 0, 1, 1, 0, 1, 1, 0, 1, 0, 0], dtype=bool)
  6746. r = e[:]
  6747. r[np.where(m)] = d[np.where(m)]
  6748. assert_array_equal(np.where(m, d, e), r)
  6749. r = e[:]
  6750. r[np.where(~m)] = d[np.where(~m)]
  6751. assert_array_equal(np.where(m, e, d), r)
  6752. assert_array_equal(np.where(m, e, e), e)
  6753. # minimal dtype result with NaN scalar (e.g required by pandas)
  6754. d = np.array([1., 2.], dtype=np.float32)
  6755. e = float('NaN')
  6756. assert_equal(np.where(True, d, e).dtype, np.float32)
  6757. e = float('Infinity')
  6758. assert_equal(np.where(True, d, e).dtype, np.float32)
  6759. e = float('-Infinity')
  6760. assert_equal(np.where(True, d, e).dtype, np.float32)
  6761. # also check upcast
  6762. e = float(1e150)
  6763. assert_equal(np.where(True, d, e).dtype, np.float64)
  6764. def test_ndim(self):
  6765. c = [True, False]
  6766. a = np.zeros((2, 25))
  6767. b = np.ones((2, 25))
  6768. r = np.where(np.array(c)[:,np.newaxis], a, b)
  6769. assert_array_equal(r[0], a[0])
  6770. assert_array_equal(r[1], b[0])
  6771. a = a.T
  6772. b = b.T
  6773. r = np.where(c, a, b)
  6774. assert_array_equal(r[:,0], a[:,0])
  6775. assert_array_equal(r[:,1], b[:,0])
  6776. def test_dtype_mix(self):
  6777. c = np.array([False, True, False, False, False, False, True, False,
  6778. False, False, True, False])
  6779. a = np.uint32(1)
  6780. b = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.],
  6781. dtype=np.float64)
  6782. r = np.array([5., 1., 3., 2., -1., -4., 1., -10., 10., 1., 1., 3.],
  6783. dtype=np.float64)
  6784. assert_equal(np.where(c, a, b), r)
  6785. a = a.astype(np.float32)
  6786. b = b.astype(np.int64)
  6787. assert_equal(np.where(c, a, b), r)
  6788. # non bool mask
  6789. c = c.astype(int)
  6790. c[c != 0] = 34242324
  6791. assert_equal(np.where(c, a, b), r)
  6792. # invert
  6793. tmpmask = c != 0
  6794. c[c == 0] = 41247212
  6795. c[tmpmask] = 0
  6796. assert_equal(np.where(c, b, a), r)
  6797. def test_foreign(self):
  6798. c = np.array([False, True, False, False, False, False, True, False,
  6799. False, False, True, False])
  6800. r = np.array([5., 1., 3., 2., -1., -4., 1., -10., 10., 1., 1., 3.],
  6801. dtype=np.float64)
  6802. a = np.ones(1, dtype='>i4')
  6803. b = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.],
  6804. dtype=np.float64)
  6805. assert_equal(np.where(c, a, b), r)
  6806. b = b.astype('>f8')
  6807. assert_equal(np.where(c, a, b), r)
  6808. a = a.astype('<i4')
  6809. assert_equal(np.where(c, a, b), r)
  6810. c = c.astype('>i4')
  6811. assert_equal(np.where(c, a, b), r)
  6812. def test_error(self):
  6813. c = [True, True]
  6814. a = np.ones((4, 5))
  6815. b = np.ones((5, 5))
  6816. assert_raises(ValueError, np.where, c, a, a)
  6817. assert_raises(ValueError, np.where, c[0], a, b)
  6818. def test_string(self):
  6819. # gh-4778 check strings are properly filled with nulls
  6820. a = np.array("abc")
  6821. b = np.array("x" * 753)
  6822. assert_equal(np.where(True, a, b), "abc")
  6823. assert_equal(np.where(False, b, a), "abc")
  6824. # check native datatype sized strings
  6825. a = np.array("abcd")
  6826. b = np.array("x" * 8)
  6827. assert_equal(np.where(True, a, b), "abcd")
  6828. assert_equal(np.where(False, b, a), "abcd")
  6829. def test_empty_result(self):
  6830. # pass empty where result through an assignment which reads the data of
  6831. # empty arrays, error detectable with valgrind, see gh-8922
  6832. x = np.zeros((1, 1))
  6833. ibad = np.vstack(np.where(x == 99.))
  6834. assert_array_equal(ibad,
  6835. np.atleast_2d(np.array([[],[]], dtype=np.intp)))
  6836. def test_largedim(self):
  6837. # invalid read regression gh-9304
  6838. shape = [10, 2, 3, 4, 5, 6]
  6839. np.random.seed(2)
  6840. array = np.random.rand(*shape)
  6841. for i in range(10):
  6842. benchmark = array.nonzero()
  6843. result = array.nonzero()
  6844. assert_array_equal(benchmark, result)
  6845. if not IS_PYPY:
  6846. # sys.getsizeof() is not valid on PyPy
  6847. class TestSizeOf:
  6848. def test_empty_array(self):
  6849. x = np.array([])
  6850. assert_(sys.getsizeof(x) > 0)
  6851. def check_array(self, dtype):
  6852. elem_size = dtype(0).itemsize
  6853. for length in [10, 50, 100, 500]:
  6854. x = np.arange(length, dtype=dtype)
  6855. assert_(sys.getsizeof(x) > length * elem_size)
  6856. def test_array_int32(self):
  6857. self.check_array(np.int32)
  6858. def test_array_int64(self):
  6859. self.check_array(np.int64)
  6860. def test_array_float32(self):
  6861. self.check_array(np.float32)
  6862. def test_array_float64(self):
  6863. self.check_array(np.float64)
  6864. def test_view(self):
  6865. d = np.ones(100)
  6866. assert_(sys.getsizeof(d[...]) < sys.getsizeof(d))
  6867. def test_reshape(self):
  6868. d = np.ones(100)
  6869. assert_(sys.getsizeof(d) < sys.getsizeof(d.reshape(100, 1, 1).copy()))
  6870. @_no_tracing
  6871. def test_resize(self):
  6872. d = np.ones(100)
  6873. old = sys.getsizeof(d)
  6874. d.resize(50)
  6875. assert_(old > sys.getsizeof(d))
  6876. d.resize(150)
  6877. assert_(old < sys.getsizeof(d))
  6878. def test_error(self):
  6879. d = np.ones(100)
  6880. assert_raises(TypeError, d.__sizeof__, "a")
  6881. class TestHashing:
  6882. def test_arrays_not_hashable(self):
  6883. x = np.ones(3)
  6884. assert_raises(TypeError, hash, x)
  6885. def test_collections_hashable(self):
  6886. x = np.array([])
  6887. assert_(not isinstance(x, collections.abc.Hashable))
  6888. class TestArrayPriority:
  6889. # This will go away when __array_priority__ is settled, meanwhile
  6890. # it serves to check unintended changes.
  6891. op = operator
  6892. binary_ops = [
  6893. op.pow, op.add, op.sub, op.mul, op.floordiv, op.truediv, op.mod,
  6894. op.and_, op.or_, op.xor, op.lshift, op.rshift, op.mod, op.gt,
  6895. op.ge, op.lt, op.le, op.ne, op.eq
  6896. ]
  6897. class Foo(np.ndarray):
  6898. __array_priority__ = 100.
  6899. def __new__(cls, *args, **kwargs):
  6900. return np.array(*args, **kwargs).view(cls)
  6901. class Bar(np.ndarray):
  6902. __array_priority__ = 101.
  6903. def __new__(cls, *args, **kwargs):
  6904. return np.array(*args, **kwargs).view(cls)
  6905. class Other:
  6906. __array_priority__ = 1000.
  6907. def _all(self, other):
  6908. return self.__class__()
  6909. __add__ = __radd__ = _all
  6910. __sub__ = __rsub__ = _all
  6911. __mul__ = __rmul__ = _all
  6912. __pow__ = __rpow__ = _all
  6913. __div__ = __rdiv__ = _all
  6914. __mod__ = __rmod__ = _all
  6915. __truediv__ = __rtruediv__ = _all
  6916. __floordiv__ = __rfloordiv__ = _all
  6917. __and__ = __rand__ = _all
  6918. __xor__ = __rxor__ = _all
  6919. __or__ = __ror__ = _all
  6920. __lshift__ = __rlshift__ = _all
  6921. __rshift__ = __rrshift__ = _all
  6922. __eq__ = _all
  6923. __ne__ = _all
  6924. __gt__ = _all
  6925. __ge__ = _all
  6926. __lt__ = _all
  6927. __le__ = _all
  6928. def test_ndarray_subclass(self):
  6929. a = np.array([1, 2])
  6930. b = self.Bar([1, 2])
  6931. for f in self.binary_ops:
  6932. msg = repr(f)
  6933. assert_(isinstance(f(a, b), self.Bar), msg)
  6934. assert_(isinstance(f(b, a), self.Bar), msg)
  6935. def test_ndarray_other(self):
  6936. a = np.array([1, 2])
  6937. b = self.Other()
  6938. for f in self.binary_ops:
  6939. msg = repr(f)
  6940. assert_(isinstance(f(a, b), self.Other), msg)
  6941. assert_(isinstance(f(b, a), self.Other), msg)
  6942. def test_subclass_subclass(self):
  6943. a = self.Foo([1, 2])
  6944. b = self.Bar([1, 2])
  6945. for f in self.binary_ops:
  6946. msg = repr(f)
  6947. assert_(isinstance(f(a, b), self.Bar), msg)
  6948. assert_(isinstance(f(b, a), self.Bar), msg)
  6949. def test_subclass_other(self):
  6950. a = self.Foo([1, 2])
  6951. b = self.Other()
  6952. for f in self.binary_ops:
  6953. msg = repr(f)
  6954. assert_(isinstance(f(a, b), self.Other), msg)
  6955. assert_(isinstance(f(b, a), self.Other), msg)
  6956. class TestBytestringArrayNonzero:
  6957. def test_empty_bstring_array_is_falsey(self):
  6958. assert_(not np.array([''], dtype=str))
  6959. def test_whitespace_bstring_array_is_falsey(self):
  6960. a = np.array(['spam'], dtype=str)
  6961. a[0] = ' \0\0'
  6962. assert_(not a)
  6963. def test_all_null_bstring_array_is_falsey(self):
  6964. a = np.array(['spam'], dtype=str)
  6965. a[0] = '\0\0\0\0'
  6966. assert_(not a)
  6967. def test_null_inside_bstring_array_is_truthy(self):
  6968. a = np.array(['spam'], dtype=str)
  6969. a[0] = ' \0 \0'
  6970. assert_(a)
  6971. class TestUnicodeEncoding:
  6972. """
  6973. Tests for encoding related bugs, such as UCS2 vs UCS4, round-tripping
  6974. issues, etc
  6975. """
  6976. def test_round_trip(self):
  6977. """ Tests that GETITEM, SETITEM, and PyArray_Scalar roundtrip """
  6978. # gh-15363
  6979. arr = np.zeros(shape=(), dtype="U1")
  6980. for i in range(1, sys.maxunicode + 1):
  6981. expected = chr(i)
  6982. arr[()] = expected
  6983. assert arr[()] == expected
  6984. assert arr.item() == expected
  6985. def test_assign_scalar(self):
  6986. # gh-3258
  6987. l = np.array(['aa', 'bb'])
  6988. l[:] = np.unicode_('cc')
  6989. assert_equal(l, ['cc', 'cc'])
  6990. def test_fill_scalar(self):
  6991. # gh-7227
  6992. l = np.array(['aa', 'bb'])
  6993. l.fill(np.unicode_('cc'))
  6994. assert_equal(l, ['cc', 'cc'])
  6995. class TestUnicodeArrayNonzero:
  6996. def test_empty_ustring_array_is_falsey(self):
  6997. assert_(not np.array([''], dtype=np.unicode_))
  6998. def test_whitespace_ustring_array_is_falsey(self):
  6999. a = np.array(['eggs'], dtype=np.unicode_)
  7000. a[0] = ' \0\0'
  7001. assert_(not a)
  7002. def test_all_null_ustring_array_is_falsey(self):
  7003. a = np.array(['eggs'], dtype=np.unicode_)
  7004. a[0] = '\0\0\0\0'
  7005. assert_(not a)
  7006. def test_null_inside_ustring_array_is_truthy(self):
  7007. a = np.array(['eggs'], dtype=np.unicode_)
  7008. a[0] = ' \0 \0'
  7009. assert_(a)
  7010. class TestFormat:
  7011. def test_0d(self):
  7012. a = np.array(np.pi)
  7013. assert_equal('{:0.3g}'.format(a), '3.14')
  7014. assert_equal('{:0.3g}'.format(a[()]), '3.14')
  7015. def test_1d_no_format(self):
  7016. a = np.array([np.pi])
  7017. assert_equal('{}'.format(a), str(a))
  7018. def test_1d_format(self):
  7019. # until gh-5543, ensure that the behaviour matches what it used to be
  7020. a = np.array([np.pi])
  7021. assert_raises(TypeError, '{:30}'.format, a)
  7022. from numpy.testing import IS_PYPY
  7023. class TestCTypes:
  7024. def test_ctypes_is_available(self):
  7025. test_arr = np.array([[1, 2, 3], [4, 5, 6]])
  7026. assert_equal(ctypes, test_arr.ctypes._ctypes)
  7027. assert_equal(tuple(test_arr.ctypes.shape), (2, 3))
  7028. def test_ctypes_is_not_available(self):
  7029. from numpy.core import _internal
  7030. _internal.ctypes = None
  7031. try:
  7032. test_arr = np.array([[1, 2, 3], [4, 5, 6]])
  7033. assert_(isinstance(test_arr.ctypes._ctypes,
  7034. _internal._missing_ctypes))
  7035. assert_equal(tuple(test_arr.ctypes.shape), (2, 3))
  7036. finally:
  7037. _internal.ctypes = ctypes
  7038. def _make_readonly(x):
  7039. x.flags.writeable = False
  7040. return x
  7041. @pytest.mark.parametrize('arr', [
  7042. np.array([1, 2, 3]),
  7043. np.array([['one', 'two'], ['three', 'four']]),
  7044. np.array((1, 2), dtype='i4,i4'),
  7045. np.zeros((2,), dtype=
  7046. np.dtype(dict(
  7047. formats=['<i4', '<i4'],
  7048. names=['a', 'b'],
  7049. offsets=[0, 2],
  7050. itemsize=6
  7051. ))
  7052. ),
  7053. np.array([None], dtype=object),
  7054. np.array([]),
  7055. np.empty((0, 0)),
  7056. _make_readonly(np.array([1, 2, 3])),
  7057. ], ids=[
  7058. '1d',
  7059. '2d',
  7060. 'structured',
  7061. 'overlapping',
  7062. 'object',
  7063. 'empty',
  7064. 'empty-2d',
  7065. 'readonly'
  7066. ])
  7067. def test_ctypes_data_as_holds_reference(self, arr):
  7068. # gh-9647
  7069. # create a copy to ensure that pytest does not mess with the refcounts
  7070. arr = arr.copy()
  7071. arr_ref = weakref.ref(arr)
  7072. ctypes_ptr = arr.ctypes.data_as(ctypes.c_void_p)
  7073. # `ctypes_ptr` should hold onto `arr`
  7074. del arr
  7075. break_cycles()
  7076. assert_(arr_ref() is not None, "ctypes pointer did not hold onto a reference")
  7077. # but when the `ctypes_ptr` object dies, so should `arr`
  7078. del ctypes_ptr
  7079. if IS_PYPY:
  7080. # Pypy does not recycle arr objects immediately. Trigger gc to
  7081. # release arr. Cpython uses refcounts. An explicit call to gc
  7082. # should not be needed here.
  7083. break_cycles()
  7084. assert_(arr_ref() is None, "unknowable whether ctypes pointer holds a reference")
  7085. def test_ctypes_as_parameter_holds_reference(self):
  7086. arr = np.array([None]).copy()
  7087. arr_ref = weakref.ref(arr)
  7088. ctypes_ptr = arr.ctypes._as_parameter_
  7089. # `ctypes_ptr` should hold onto `arr`
  7090. del arr
  7091. break_cycles()
  7092. assert_(arr_ref() is not None, "ctypes pointer did not hold onto a reference")
  7093. # but when the `ctypes_ptr` object dies, so should `arr`
  7094. del ctypes_ptr
  7095. if IS_PYPY:
  7096. break_cycles()
  7097. assert_(arr_ref() is None, "unknowable whether ctypes pointer holds a reference")
  7098. class TestWritebackIfCopy:
  7099. # all these tests use the WRITEBACKIFCOPY mechanism
  7100. def test_argmax_with_out(self):
  7101. mat = np.eye(5)
  7102. out = np.empty(5, dtype='i2')
  7103. res = np.argmax(mat, 0, out=out)
  7104. assert_equal(res, range(5))
  7105. def test_argmin_with_out(self):
  7106. mat = -np.eye(5)
  7107. out = np.empty(5, dtype='i2')
  7108. res = np.argmin(mat, 0, out=out)
  7109. assert_equal(res, range(5))
  7110. def test_insert_noncontiguous(self):
  7111. a = np.arange(6).reshape(2,3).T # force non-c-contiguous
  7112. # uses arr_insert
  7113. np.place(a, a>2, [44, 55])
  7114. assert_equal(a, np.array([[0, 44], [1, 55], [2, 44]]))
  7115. # hit one of the failing paths
  7116. assert_raises(ValueError, np.place, a, a>20, [])
  7117. def test_put_noncontiguous(self):
  7118. a = np.arange(6).reshape(2,3).T # force non-c-contiguous
  7119. np.put(a, [0, 2], [44, 55])
  7120. assert_equal(a, np.array([[44, 3], [55, 4], [2, 5]]))
  7121. def test_putmask_noncontiguous(self):
  7122. a = np.arange(6).reshape(2,3).T # force non-c-contiguous
  7123. # uses arr_putmask
  7124. np.putmask(a, a>2, a**2)
  7125. assert_equal(a, np.array([[0, 9], [1, 16], [2, 25]]))
  7126. def test_take_mode_raise(self):
  7127. a = np.arange(6, dtype='int')
  7128. out = np.empty(2, dtype='int')
  7129. np.take(a, [0, 2], out=out, mode='raise')
  7130. assert_equal(out, np.array([0, 2]))
  7131. def test_choose_mod_raise(self):
  7132. a = np.array([[1, 0, 1], [0, 1, 0], [1, 0, 1]])
  7133. out = np.empty((3,3), dtype='int')
  7134. choices = [-10, 10]
  7135. np.choose(a, choices, out=out, mode='raise')
  7136. assert_equal(out, np.array([[ 10, -10, 10],
  7137. [-10, 10, -10],
  7138. [ 10, -10, 10]]))
  7139. def test_flatiter__array__(self):
  7140. a = np.arange(9).reshape(3,3)
  7141. b = a.T.flat
  7142. c = b.__array__()
  7143. # triggers the WRITEBACKIFCOPY resolution, assuming refcount semantics
  7144. del c
  7145. def test_dot_out(self):
  7146. # if HAVE_CBLAS, will use WRITEBACKIFCOPY
  7147. a = np.arange(9, dtype=float).reshape(3,3)
  7148. b = np.dot(a, a, out=a)
  7149. assert_equal(b, np.array([[15, 18, 21], [42, 54, 66], [69, 90, 111]]))
  7150. def test_view_assign(self):
  7151. from numpy.core._multiarray_tests import npy_create_writebackifcopy, npy_resolve
  7152. arr = np.arange(9).reshape(3, 3).T
  7153. arr_wb = npy_create_writebackifcopy(arr)
  7154. assert_(arr_wb.flags.writebackifcopy)
  7155. assert_(arr_wb.base is arr)
  7156. arr_wb[...] = -100
  7157. npy_resolve(arr_wb)
  7158. # arr changes after resolve, even though we assigned to arr_wb
  7159. assert_equal(arr, -100)
  7160. # after resolve, the two arrays no longer reference each other
  7161. assert_(arr_wb.ctypes.data != 0)
  7162. assert_equal(arr_wb.base, None)
  7163. # assigning to arr_wb does not get transferred to arr
  7164. arr_wb[...] = 100
  7165. assert_equal(arr, -100)
  7166. @pytest.mark.leaks_references(
  7167. reason="increments self in dealloc; ignore since deprecated path.")
  7168. def test_dealloc_warning(self):
  7169. with suppress_warnings() as sup:
  7170. sup.record(RuntimeWarning)
  7171. arr = np.arange(9).reshape(3, 3)
  7172. v = arr.T
  7173. _multiarray_tests.npy_abuse_writebackifcopy(v)
  7174. assert len(sup.log) == 1
  7175. def test_view_discard_refcount(self):
  7176. from numpy.core._multiarray_tests import npy_create_writebackifcopy, npy_discard
  7177. arr = np.arange(9).reshape(3, 3).T
  7178. orig = arr.copy()
  7179. if HAS_REFCOUNT:
  7180. arr_cnt = sys.getrefcount(arr)
  7181. arr_wb = npy_create_writebackifcopy(arr)
  7182. assert_(arr_wb.flags.writebackifcopy)
  7183. assert_(arr_wb.base is arr)
  7184. arr_wb[...] = -100
  7185. npy_discard(arr_wb)
  7186. # arr remains unchanged after discard
  7187. assert_equal(arr, orig)
  7188. # after discard, the two arrays no longer reference each other
  7189. assert_(arr_wb.ctypes.data != 0)
  7190. assert_equal(arr_wb.base, None)
  7191. if HAS_REFCOUNT:
  7192. assert_equal(arr_cnt, sys.getrefcount(arr))
  7193. # assigning to arr_wb does not get transferred to arr
  7194. arr_wb[...] = 100
  7195. assert_equal(arr, orig)
  7196. class TestArange:
  7197. def test_infinite(self):
  7198. assert_raises_regex(
  7199. ValueError, "size exceeded",
  7200. np.arange, 0, np.inf
  7201. )
  7202. def test_nan_step(self):
  7203. assert_raises_regex(
  7204. ValueError, "cannot compute length",
  7205. np.arange, 0, 1, np.nan
  7206. )
  7207. def test_zero_step(self):
  7208. assert_raises(ZeroDivisionError, np.arange, 0, 10, 0)
  7209. assert_raises(ZeroDivisionError, np.arange, 0.0, 10.0, 0.0)
  7210. # empty range
  7211. assert_raises(ZeroDivisionError, np.arange, 0, 0, 0)
  7212. assert_raises(ZeroDivisionError, np.arange, 0.0, 0.0, 0.0)
  7213. class TestArrayFinalize:
  7214. """ Tests __array_finalize__ """
  7215. def test_receives_base(self):
  7216. # gh-11237
  7217. class SavesBase(np.ndarray):
  7218. def __array_finalize__(self, obj):
  7219. self.saved_base = self.base
  7220. a = np.array(1).view(SavesBase)
  7221. assert_(a.saved_base is a.base)
  7222. def test_lifetime_on_error(self):
  7223. # gh-11237
  7224. class RaisesInFinalize(np.ndarray):
  7225. def __array_finalize__(self, obj):
  7226. # crash, but keep this object alive
  7227. raise Exception(self)
  7228. # a plain object can't be weakref'd
  7229. class Dummy: pass
  7230. # get a weak reference to an object within an array
  7231. obj_arr = np.array(Dummy())
  7232. obj_ref = weakref.ref(obj_arr[()])
  7233. # get an array that crashed in __array_finalize__
  7234. with assert_raises(Exception) as e:
  7235. obj_arr.view(RaisesInFinalize)
  7236. obj_subarray = e.exception.args[0]
  7237. del e
  7238. assert_(isinstance(obj_subarray, RaisesInFinalize))
  7239. # reference should still be held by obj_arr
  7240. break_cycles()
  7241. assert_(obj_ref() is not None, "object should not already be dead")
  7242. del obj_arr
  7243. break_cycles()
  7244. assert_(obj_ref() is not None, "obj_arr should not hold the last reference")
  7245. del obj_subarray
  7246. break_cycles()
  7247. assert_(obj_ref() is None, "no references should remain")
  7248. def test_orderconverter_with_nonASCII_unicode_ordering():
  7249. # gh-7475
  7250. a = np.arange(5)
  7251. assert_raises(ValueError, a.flatten, order=u'\xe2')
  7252. def test_equal_override():
  7253. # gh-9153: ndarray.__eq__ uses special logic for structured arrays, which
  7254. # did not respect overrides with __array_priority__ or __array_ufunc__.
  7255. # The PR fixed this for __array_priority__ and __array_ufunc__ = None.
  7256. class MyAlwaysEqual:
  7257. def __eq__(self, other):
  7258. return "eq"
  7259. def __ne__(self, other):
  7260. return "ne"
  7261. class MyAlwaysEqualOld(MyAlwaysEqual):
  7262. __array_priority__ = 10000
  7263. class MyAlwaysEqualNew(MyAlwaysEqual):
  7264. __array_ufunc__ = None
  7265. array = np.array([(0, 1), (2, 3)], dtype='i4,i4')
  7266. for my_always_equal_cls in MyAlwaysEqualOld, MyAlwaysEqualNew:
  7267. my_always_equal = my_always_equal_cls()
  7268. assert_equal(my_always_equal == array, 'eq')
  7269. assert_equal(array == my_always_equal, 'eq')
  7270. assert_equal(my_always_equal != array, 'ne')
  7271. assert_equal(array != my_always_equal, 'ne')
  7272. def test_npymath_complex():
  7273. # Smoketest npymath functions
  7274. from numpy.core._multiarray_tests import (
  7275. npy_cabs, npy_carg)
  7276. funcs = {npy_cabs: np.absolute,
  7277. npy_carg: np.angle}
  7278. vals = (1, np.inf, -np.inf, np.nan)
  7279. types = (np.complex64, np.complex128, np.clongdouble)
  7280. for fun, npfun in funcs.items():
  7281. for x, y in itertools.product(vals, vals):
  7282. for t in types:
  7283. z = t(complex(x, y))
  7284. got = fun(z)
  7285. expected = npfun(z)
  7286. assert_allclose(got, expected)
  7287. def test_npymath_real():
  7288. # Smoketest npymath functions
  7289. from numpy.core._multiarray_tests import (
  7290. npy_log10, npy_cosh, npy_sinh, npy_tan, npy_tanh)
  7291. funcs = {npy_log10: np.log10,
  7292. npy_cosh: np.cosh,
  7293. npy_sinh: np.sinh,
  7294. npy_tan: np.tan,
  7295. npy_tanh: np.tanh}
  7296. vals = (1, np.inf, -np.inf, np.nan)
  7297. types = (np.float32, np.float64, np.longdouble)
  7298. with np.errstate(all='ignore'):
  7299. for fun, npfun in funcs.items():
  7300. for x, t in itertools.product(vals, types):
  7301. z = t(x)
  7302. got = fun(z)
  7303. expected = npfun(z)
  7304. assert_allclose(got, expected)
  7305. def test_uintalignment_and_alignment():
  7306. # alignment code needs to satisfy these requirements:
  7307. # 1. numpy structs match C struct layout
  7308. # 2. ufuncs/casting is safe wrt to aligned access
  7309. # 3. copy code is safe wrt to "uint alidned" access
  7310. #
  7311. # Complex types are the main problem, whose alignment may not be the same
  7312. # as their "uint alignment".
  7313. #
  7314. # This test might only fail on certain platforms, where uint64 alignment is
  7315. # not equal to complex64 alignment. The second 2 tests will only fail
  7316. # for DEBUG=1.
  7317. d1 = np.dtype('u1,c8', align=True)
  7318. d2 = np.dtype('u4,c8', align=True)
  7319. d3 = np.dtype({'names': ['a', 'b'], 'formats': ['u1', d1]}, align=True)
  7320. assert_equal(np.zeros(1, dtype=d1)['f1'].flags['ALIGNED'], True)
  7321. assert_equal(np.zeros(1, dtype=d2)['f1'].flags['ALIGNED'], True)
  7322. assert_equal(np.zeros(1, dtype='u1,c8')['f1'].flags['ALIGNED'], False)
  7323. # check that C struct matches numpy struct size
  7324. s = _multiarray_tests.get_struct_alignments()
  7325. for d, (alignment, size) in zip([d1,d2,d3], s):
  7326. assert_equal(d.alignment, alignment)
  7327. assert_equal(d.itemsize, size)
  7328. # check that ufuncs don't complain in debug mode
  7329. # (this is probably OK if the aligned flag is true above)
  7330. src = np.zeros((2,2), dtype=d1)['f1'] # 4-byte aligned, often
  7331. np.exp(src) # assert fails?
  7332. # check that copy code doesn't complain in debug mode
  7333. dst = np.zeros((2,2), dtype='c8')
  7334. dst[:,1] = src[:,1] # assert in lowlevel_strided_loops fails?
  7335. class TestAlignment:
  7336. # adapted from scipy._lib.tests.test__util.test__aligned_zeros
  7337. # Checks that unusual memory alignments don't trip up numpy.
  7338. # In particular, check RELAXED_STRIDES don't trip alignment assertions in
  7339. # NDEBUG mode for size-0 arrays (gh-12503)
  7340. def check(self, shape, dtype, order, align):
  7341. err_msg = repr((shape, dtype, order, align))
  7342. x = _aligned_zeros(shape, dtype, order, align=align)
  7343. if align is None:
  7344. align = np.dtype(dtype).alignment
  7345. assert_equal(x.__array_interface__['data'][0] % align, 0)
  7346. if hasattr(shape, '__len__'):
  7347. assert_equal(x.shape, shape, err_msg)
  7348. else:
  7349. assert_equal(x.shape, (shape,), err_msg)
  7350. assert_equal(x.dtype, dtype)
  7351. if order == "C":
  7352. assert_(x.flags.c_contiguous, err_msg)
  7353. elif order == "F":
  7354. if x.size > 0:
  7355. assert_(x.flags.f_contiguous, err_msg)
  7356. elif order is None:
  7357. assert_(x.flags.c_contiguous, err_msg)
  7358. else:
  7359. raise ValueError()
  7360. def test_various_alignments(self):
  7361. for align in [1, 2, 3, 4, 8, 12, 16, 32, 64, None]:
  7362. for n in [0, 1, 3, 11]:
  7363. for order in ["C", "F", None]:
  7364. for dtype in list(np.typecodes["All"]) + ['i4,i4,i4']:
  7365. if dtype == 'O':
  7366. # object dtype can't be misaligned
  7367. continue
  7368. for shape in [n, (1, 2, 3, n)]:
  7369. self.check(shape, np.dtype(dtype), order, align)
  7370. def test_strided_loop_alignments(self):
  7371. # particularly test that complex64 and float128 use right alignment
  7372. # code-paths, since these are particularly problematic. It is useful to
  7373. # turn on USE_DEBUG for this test, so lowlevel-loop asserts are run.
  7374. for align in [1, 2, 4, 8, 12, 16, None]:
  7375. xf64 = _aligned_zeros(3, np.float64)
  7376. xc64 = _aligned_zeros(3, np.complex64, align=align)
  7377. xf128 = _aligned_zeros(3, np.longdouble, align=align)
  7378. # test casting, both to and from misaligned
  7379. with suppress_warnings() as sup:
  7380. sup.filter(np.ComplexWarning, "Casting complex values")
  7381. xc64.astype('f8')
  7382. xf64.astype(np.complex64)
  7383. test = xc64 + xf64
  7384. xf128.astype('f8')
  7385. xf64.astype(np.longdouble)
  7386. test = xf128 + xf64
  7387. test = xf128 + xc64
  7388. # test copy, both to and from misaligned
  7389. # contig copy
  7390. xf64[:] = xf64.copy()
  7391. xc64[:] = xc64.copy()
  7392. xf128[:] = xf128.copy()
  7393. # strided copy
  7394. xf64[::2] = xf64[::2].copy()
  7395. xc64[::2] = xc64[::2].copy()
  7396. xf128[::2] = xf128[::2].copy()
  7397. def test_getfield():
  7398. a = np.arange(32, dtype='uint16')
  7399. if sys.byteorder == 'little':
  7400. i = 0
  7401. j = 1
  7402. else:
  7403. i = 1
  7404. j = 0
  7405. b = a.getfield('int8', i)
  7406. assert_equal(b, a)
  7407. b = a.getfield('int8', j)
  7408. assert_equal(b, 0)
  7409. pytest.raises(ValueError, a.getfield, 'uint8', -1)
  7410. pytest.raises(ValueError, a.getfield, 'uint8', 16)
  7411. pytest.raises(ValueError, a.getfield, 'uint64', 0)