test_multiarray.py 362 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775
  1. import collections.abc
  2. import tempfile
  3. import sys
  4. import warnings
  5. import operator
  6. import io
  7. import itertools
  8. import functools
  9. import ctypes
  10. import os
  11. import gc
  12. import re
  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 mmap
  21. import numpy as np
  22. import numpy.core._multiarray_tests as _multiarray_tests
  23. from numpy.core._rational_tests import rational
  24. from numpy.testing import (
  25. assert_, assert_raises, assert_warns, assert_equal, assert_almost_equal,
  26. assert_array_equal, assert_raises_regex, assert_array_almost_equal,
  27. assert_allclose, IS_PYPY, IS_PYSTON, HAS_REFCOUNT, assert_array_less,
  28. runstring, temppath, suppress_warnings, break_cycles,
  29. )
  30. from numpy.testing._private.utils import requires_memory, _no_tracing
  31. from numpy.core.tests._locales import CommaDecimalPointLocale
  32. from numpy.lib.recfunctions import repack_fields
  33. # Need to test an object that does not fully implement math interface
  34. from datetime import timedelta, datetime
  35. def _aligned_zeros(shape, dtype=float, order="C", align=None):
  36. """
  37. Allocate a new ndarray with aligned memory.
  38. The ndarray is guaranteed *not* aligned to twice the requested alignment.
  39. Eg, if align=4, guarantees it is not aligned to 8. If align=None uses
  40. dtype.alignment."""
  41. dtype = np.dtype(dtype)
  42. if dtype == np.dtype(object):
  43. # Can't do this, fall back to standard allocation (which
  44. # should always be sufficiently aligned)
  45. if align is not None:
  46. raise ValueError("object array alignment not supported")
  47. return np.zeros(shape, dtype=dtype, order=order)
  48. if align is None:
  49. align = dtype.alignment
  50. if not hasattr(shape, '__len__'):
  51. shape = (shape,)
  52. size = functools.reduce(operator.mul, shape) * dtype.itemsize
  53. buf = np.empty(size + 2*align + 1, np.uint8)
  54. ptr = buf.__array_interface__['data'][0]
  55. offset = ptr % align
  56. if offset != 0:
  57. offset = align - offset
  58. if (ptr % (2*align)) == 0:
  59. offset += align
  60. # Note: slices producing 0-size arrays do not necessarily change
  61. # data pointer --- so we use and allocate size+1
  62. buf = buf[offset:offset+size+1][:-1]
  63. buf.fill(0)
  64. data = np.ndarray(shape, dtype, buf, order=order)
  65. return data
  66. class TestFlags:
  67. def setup_method(self):
  68. self.a = np.arange(10)
  69. def test_writeable(self):
  70. mydict = locals()
  71. self.a.flags.writeable = False
  72. assert_raises(ValueError, runstring, 'self.a[0] = 3', mydict)
  73. assert_raises(ValueError, runstring, 'self.a[0:1].itemset(3)', mydict)
  74. self.a.flags.writeable = True
  75. self.a[0] = 5
  76. self.a[0] = 0
  77. def test_writeable_any_base(self):
  78. # Ensure that any base being writeable is sufficient to change flag;
  79. # this is especially interesting for arrays from an array interface.
  80. arr = np.arange(10)
  81. class subclass(np.ndarray):
  82. pass
  83. # Create subclass so base will not be collapsed, this is OK to change
  84. view1 = arr.view(subclass)
  85. view2 = view1[...]
  86. arr.flags.writeable = False
  87. view2.flags.writeable = False
  88. view2.flags.writeable = True # Can be set to True again.
  89. arr = np.arange(10)
  90. class frominterface:
  91. def __init__(self, arr):
  92. self.arr = arr
  93. self.__array_interface__ = arr.__array_interface__
  94. view1 = np.asarray(frominterface)
  95. view2 = view1[...]
  96. view2.flags.writeable = False
  97. view2.flags.writeable = True
  98. view1.flags.writeable = False
  99. view2.flags.writeable = False
  100. with assert_raises(ValueError):
  101. # Must assume not writeable, since only base is not:
  102. view2.flags.writeable = True
  103. def test_writeable_from_readonly(self):
  104. # gh-9440 - make sure fromstring, from buffer on readonly buffers
  105. # set writeable False
  106. data = b'\x00' * 100
  107. vals = np.frombuffer(data, 'B')
  108. assert_raises(ValueError, vals.setflags, write=True)
  109. types = np.dtype( [('vals', 'u1'), ('res3', 'S4')] )
  110. values = np.core.records.fromstring(data, types)
  111. vals = values['vals']
  112. assert_raises(ValueError, vals.setflags, write=True)
  113. def test_writeable_from_buffer(self):
  114. data = bytearray(b'\x00' * 100)
  115. vals = np.frombuffer(data, 'B')
  116. assert_(vals.flags.writeable)
  117. vals.setflags(write=False)
  118. assert_(vals.flags.writeable is False)
  119. vals.setflags(write=True)
  120. assert_(vals.flags.writeable)
  121. types = np.dtype( [('vals', 'u1'), ('res3', 'S4')] )
  122. values = np.core.records.fromstring(data, types)
  123. vals = values['vals']
  124. assert_(vals.flags.writeable)
  125. vals.setflags(write=False)
  126. assert_(vals.flags.writeable is False)
  127. vals.setflags(write=True)
  128. assert_(vals.flags.writeable)
  129. @pytest.mark.skipif(IS_PYPY, reason="PyPy always copies")
  130. def test_writeable_pickle(self):
  131. import pickle
  132. # Small arrays will be copied without setting base.
  133. # See condition for using PyArray_SetBaseObject in
  134. # array_setstate.
  135. a = np.arange(1000)
  136. for v in range(pickle.HIGHEST_PROTOCOL):
  137. vals = pickle.loads(pickle.dumps(a, v))
  138. assert_(vals.flags.writeable)
  139. assert_(isinstance(vals.base, bytes))
  140. def test_writeable_from_c_data(self):
  141. # Test that the writeable flag can be changed for an array wrapping
  142. # low level C-data, but not owning its data.
  143. # Also see that this is deprecated to change from python.
  144. from numpy.core._multiarray_tests import get_c_wrapping_array
  145. arr_writeable = get_c_wrapping_array(True)
  146. assert not arr_writeable.flags.owndata
  147. assert arr_writeable.flags.writeable
  148. view = arr_writeable[...]
  149. # Toggling the writeable flag works on the view:
  150. view.flags.writeable = False
  151. assert not view.flags.writeable
  152. view.flags.writeable = True
  153. assert view.flags.writeable
  154. # Flag can be unset on the arr_writeable:
  155. arr_writeable.flags.writeable = False
  156. arr_readonly = get_c_wrapping_array(False)
  157. assert not arr_readonly.flags.owndata
  158. assert not arr_readonly.flags.writeable
  159. for arr in [arr_writeable, arr_readonly]:
  160. view = arr[...]
  161. view.flags.writeable = False # make sure it is readonly
  162. arr.flags.writeable = False
  163. assert not arr.flags.writeable
  164. with assert_raises(ValueError):
  165. view.flags.writeable = True
  166. with warnings.catch_warnings():
  167. warnings.simplefilter("error", DeprecationWarning)
  168. with assert_raises(DeprecationWarning):
  169. arr.flags.writeable = True
  170. with assert_warns(DeprecationWarning):
  171. arr.flags.writeable = True
  172. def test_warnonwrite(self):
  173. a = np.arange(10)
  174. a.flags._warn_on_write = True
  175. with warnings.catch_warnings(record=True) as w:
  176. warnings.filterwarnings('always')
  177. a[1] = 10
  178. a[2] = 10
  179. # only warn once
  180. assert_(len(w) == 1)
  181. @pytest.mark.parametrize(["flag", "flag_value", "writeable"],
  182. [("writeable", True, True),
  183. # Delete _warn_on_write after deprecation and simplify
  184. # the parameterization:
  185. ("_warn_on_write", True, False),
  186. ("writeable", False, False)])
  187. def test_readonly_flag_protocols(self, flag, flag_value, writeable):
  188. a = np.arange(10)
  189. setattr(a.flags, flag, flag_value)
  190. class MyArr():
  191. __array_struct__ = a.__array_struct__
  192. assert memoryview(a).readonly is not writeable
  193. assert a.__array_interface__['data'][1] is not writeable
  194. assert np.asarray(MyArr()).flags.writeable is writeable
  195. def test_otherflags(self):
  196. assert_equal(self.a.flags.carray, True)
  197. assert_equal(self.a.flags['C'], True)
  198. assert_equal(self.a.flags.farray, False)
  199. assert_equal(self.a.flags.behaved, True)
  200. assert_equal(self.a.flags.fnc, False)
  201. assert_equal(self.a.flags.forc, True)
  202. assert_equal(self.a.flags.owndata, True)
  203. assert_equal(self.a.flags.writeable, True)
  204. assert_equal(self.a.flags.aligned, True)
  205. assert_equal(self.a.flags.writebackifcopy, False)
  206. assert_equal(self.a.flags['X'], False)
  207. assert_equal(self.a.flags['WRITEBACKIFCOPY'], False)
  208. def test_string_align(self):
  209. a = np.zeros(4, dtype=np.dtype('|S4'))
  210. assert_(a.flags.aligned)
  211. # not power of two are accessed byte-wise and thus considered aligned
  212. a = np.zeros(5, dtype=np.dtype('|S4'))
  213. assert_(a.flags.aligned)
  214. def test_void_align(self):
  215. a = np.zeros(4, dtype=np.dtype([("a", "i4"), ("b", "i4")]))
  216. assert_(a.flags.aligned)
  217. class TestHash:
  218. # see #3793
  219. def test_int(self):
  220. for st, ut, s in [(np.int8, np.uint8, 8),
  221. (np.int16, np.uint16, 16),
  222. (np.int32, np.uint32, 32),
  223. (np.int64, np.uint64, 64)]:
  224. for i in range(1, s):
  225. assert_equal(hash(st(-2**i)), hash(-2**i),
  226. err_msg="%r: -2**%d" % (st, i))
  227. assert_equal(hash(st(2**(i - 1))), hash(2**(i - 1)),
  228. err_msg="%r: 2**%d" % (st, i - 1))
  229. assert_equal(hash(st(2**i - 1)), hash(2**i - 1),
  230. err_msg="%r: 2**%d - 1" % (st, i))
  231. i = max(i - 1, 1)
  232. assert_equal(hash(ut(2**(i - 1))), hash(2**(i - 1)),
  233. err_msg="%r: 2**%d" % (ut, i - 1))
  234. assert_equal(hash(ut(2**i - 1)), hash(2**i - 1),
  235. err_msg="%r: 2**%d - 1" % (ut, i))
  236. class TestAttributes:
  237. def setup_method(self):
  238. self.one = np.arange(10)
  239. self.two = np.arange(20).reshape(4, 5)
  240. self.three = np.arange(60, dtype=np.float64).reshape(2, 5, 6)
  241. def test_attributes(self):
  242. assert_equal(self.one.shape, (10,))
  243. assert_equal(self.two.shape, (4, 5))
  244. assert_equal(self.three.shape, (2, 5, 6))
  245. self.three.shape = (10, 3, 2)
  246. assert_equal(self.three.shape, (10, 3, 2))
  247. self.three.shape = (2, 5, 6)
  248. assert_equal(self.one.strides, (self.one.itemsize,))
  249. num = self.two.itemsize
  250. assert_equal(self.two.strides, (5*num, num))
  251. num = self.three.itemsize
  252. assert_equal(self.three.strides, (30*num, 6*num, num))
  253. assert_equal(self.one.ndim, 1)
  254. assert_equal(self.two.ndim, 2)
  255. assert_equal(self.three.ndim, 3)
  256. num = self.two.itemsize
  257. assert_equal(self.two.size, 20)
  258. assert_equal(self.two.nbytes, 20*num)
  259. assert_equal(self.two.itemsize, self.two.dtype.itemsize)
  260. assert_equal(self.two.base, np.arange(20))
  261. def test_dtypeattr(self):
  262. assert_equal(self.one.dtype, np.dtype(np.int_))
  263. assert_equal(self.three.dtype, np.dtype(np.float_))
  264. assert_equal(self.one.dtype.char, 'l')
  265. assert_equal(self.three.dtype.char, 'd')
  266. assert_(self.three.dtype.str[0] in '<>')
  267. assert_equal(self.one.dtype.str[1], 'i')
  268. assert_equal(self.three.dtype.str[1], 'f')
  269. def test_int_subclassing(self):
  270. # Regression test for https://github.com/numpy/numpy/pull/3526
  271. numpy_int = np.int_(0)
  272. # int_ doesn't inherit from Python int, because it's not fixed-width
  273. assert_(not isinstance(numpy_int, int))
  274. def test_stridesattr(self):
  275. x = self.one
  276. def make_array(size, offset, strides):
  277. return np.ndarray(size, buffer=x, dtype=int,
  278. offset=offset*x.itemsize,
  279. strides=strides*x.itemsize)
  280. assert_equal(make_array(4, 4, -1), np.array([4, 3, 2, 1]))
  281. assert_raises(ValueError, make_array, 4, 4, -2)
  282. assert_raises(ValueError, make_array, 4, 2, -1)
  283. assert_raises(ValueError, make_array, 8, 3, 1)
  284. assert_equal(make_array(8, 3, 0), np.array([3]*8))
  285. # Check behavior reported in gh-2503:
  286. assert_raises(ValueError, make_array, (2, 3), 5, np.array([-2, -3]))
  287. make_array(0, 0, 10)
  288. def test_set_stridesattr(self):
  289. x = self.one
  290. def make_array(size, offset, strides):
  291. try:
  292. r = np.ndarray([size], dtype=int, buffer=x,
  293. offset=offset*x.itemsize)
  294. except Exception as e:
  295. raise RuntimeError(e)
  296. r.strides = strides = strides*x.itemsize
  297. return r
  298. assert_equal(make_array(4, 4, -1), np.array([4, 3, 2, 1]))
  299. assert_equal(make_array(7, 3, 1), np.array([3, 4, 5, 6, 7, 8, 9]))
  300. assert_raises(ValueError, make_array, 4, 4, -2)
  301. assert_raises(ValueError, make_array, 4, 2, -1)
  302. assert_raises(RuntimeError, make_array, 8, 3, 1)
  303. # Check that the true extent of the array is used.
  304. # Test relies on as_strided base not exposing a buffer.
  305. x = np.lib.stride_tricks.as_strided(np.arange(1), (10, 10), (0, 0))
  306. def set_strides(arr, strides):
  307. arr.strides = strides
  308. assert_raises(ValueError, set_strides, x, (10*x.itemsize, x.itemsize))
  309. # Test for offset calculations:
  310. x = np.lib.stride_tricks.as_strided(np.arange(10, dtype=np.int8)[-1],
  311. shape=(10,), strides=(-1,))
  312. assert_raises(ValueError, set_strides, x[::-1], -1)
  313. a = x[::-1]
  314. a.strides = 1
  315. a[::2].strides = 2
  316. # test 0d
  317. arr_0d = np.array(0)
  318. arr_0d.strides = ()
  319. assert_raises(TypeError, set_strides, arr_0d, None)
  320. def test_fill(self):
  321. for t in "?bhilqpBHILQPfdgFDGO":
  322. x = np.empty((3, 2, 1), t)
  323. y = np.empty((3, 2, 1), t)
  324. x.fill(1)
  325. y[...] = 1
  326. assert_equal(x, y)
  327. def test_fill_max_uint64(self):
  328. x = np.empty((3, 2, 1), dtype=np.uint64)
  329. y = np.empty((3, 2, 1), dtype=np.uint64)
  330. value = 2**64 - 1
  331. y[...] = value
  332. x.fill(value)
  333. assert_array_equal(x, y)
  334. def test_fill_struct_array(self):
  335. # Filling from a scalar
  336. x = np.array([(0, 0.0), (1, 1.0)], dtype='i4,f8')
  337. x.fill(x[0])
  338. assert_equal(x['f1'][1], x['f1'][0])
  339. # Filling from a tuple that can be converted
  340. # to a scalar
  341. x = np.zeros(2, dtype=[('a', 'f8'), ('b', 'i4')])
  342. x.fill((3.5, -2))
  343. assert_array_equal(x['a'], [3.5, 3.5])
  344. assert_array_equal(x['b'], [-2, -2])
  345. def test_fill_readonly(self):
  346. # gh-22922
  347. a = np.zeros(11)
  348. a.setflags(write=False)
  349. with pytest.raises(ValueError, match=".*read-only"):
  350. a.fill(0)
  351. class TestArrayConstruction:
  352. def test_array(self):
  353. d = np.ones(6)
  354. r = np.array([d, d])
  355. assert_equal(r, np.ones((2, 6)))
  356. d = np.ones(6)
  357. tgt = np.ones((2, 6))
  358. r = np.array([d, d])
  359. assert_equal(r, tgt)
  360. tgt[1] = 2
  361. r = np.array([d, d + 1])
  362. assert_equal(r, tgt)
  363. d = np.ones(6)
  364. r = np.array([[d, d]])
  365. assert_equal(r, np.ones((1, 2, 6)))
  366. d = np.ones(6)
  367. r = np.array([[d, d], [d, d]])
  368. assert_equal(r, np.ones((2, 2, 6)))
  369. d = np.ones((6, 6))
  370. r = np.array([d, d])
  371. assert_equal(r, np.ones((2, 6, 6)))
  372. d = np.ones((6, ))
  373. r = np.array([[d, d + 1], d + 2], dtype=object)
  374. assert_equal(len(r), 2)
  375. assert_equal(r[0], [d, d + 1])
  376. assert_equal(r[1], d + 2)
  377. tgt = np.ones((2, 3), dtype=bool)
  378. tgt[0, 2] = False
  379. tgt[1, 0:2] = False
  380. r = np.array([[True, True, False], [False, False, True]])
  381. assert_equal(r, tgt)
  382. r = np.array([[True, False], [True, False], [False, True]])
  383. assert_equal(r, tgt.T)
  384. def test_array_empty(self):
  385. assert_raises(TypeError, np.array)
  386. def test_0d_array_shape(self):
  387. assert np.ones(np.array(3)).shape == (3,)
  388. def test_array_copy_false(self):
  389. d = np.array([1, 2, 3])
  390. e = np.array(d, copy=False)
  391. d[1] = 3
  392. assert_array_equal(e, [1, 3, 3])
  393. e = np.array(d, copy=False, order='F')
  394. d[1] = 4
  395. assert_array_equal(e, [1, 4, 3])
  396. e[2] = 7
  397. assert_array_equal(d, [1, 4, 7])
  398. def test_array_copy_true(self):
  399. d = np.array([[1,2,3], [1, 2, 3]])
  400. e = np.array(d, copy=True)
  401. d[0, 1] = 3
  402. e[0, 2] = -7
  403. assert_array_equal(e, [[1, 2, -7], [1, 2, 3]])
  404. assert_array_equal(d, [[1, 3, 3], [1, 2, 3]])
  405. e = np.array(d, copy=True, order='F')
  406. d[0, 1] = 5
  407. e[0, 2] = 7
  408. assert_array_equal(e, [[1, 3, 7], [1, 2, 3]])
  409. assert_array_equal(d, [[1, 5, 3], [1,2,3]])
  410. def test_array_cont(self):
  411. d = np.ones(10)[::2]
  412. assert_(np.ascontiguousarray(d).flags.c_contiguous)
  413. assert_(np.ascontiguousarray(d).flags.f_contiguous)
  414. assert_(np.asfortranarray(d).flags.c_contiguous)
  415. assert_(np.asfortranarray(d).flags.f_contiguous)
  416. d = np.ones((10, 10))[::2,::2]
  417. assert_(np.ascontiguousarray(d).flags.c_contiguous)
  418. assert_(np.asfortranarray(d).flags.f_contiguous)
  419. @pytest.mark.parametrize("func",
  420. [np.array,
  421. np.asarray,
  422. np.asanyarray,
  423. np.ascontiguousarray,
  424. np.asfortranarray])
  425. def test_bad_arguments_error(self, func):
  426. with pytest.raises(TypeError):
  427. func(3, dtype="bad dtype")
  428. with pytest.raises(TypeError):
  429. func() # missing arguments
  430. with pytest.raises(TypeError):
  431. func(1, 2, 3, 4, 5, 6, 7, 8) # too many arguments
  432. @pytest.mark.parametrize("func",
  433. [np.array,
  434. np.asarray,
  435. np.asanyarray,
  436. np.ascontiguousarray,
  437. np.asfortranarray])
  438. def test_array_as_keyword(self, func):
  439. # This should likely be made positional only, but do not change
  440. # the name accidentally.
  441. if func is np.array:
  442. func(object=3)
  443. else:
  444. func(a=3)
  445. class TestAssignment:
  446. def test_assignment_broadcasting(self):
  447. a = np.arange(6).reshape(2, 3)
  448. # Broadcasting the input to the output
  449. a[...] = np.arange(3)
  450. assert_equal(a, [[0, 1, 2], [0, 1, 2]])
  451. a[...] = np.arange(2).reshape(2, 1)
  452. assert_equal(a, [[0, 0, 0], [1, 1, 1]])
  453. # For compatibility with <= 1.5, a limited version of broadcasting
  454. # the output to the input.
  455. #
  456. # This behavior is inconsistent with NumPy broadcasting
  457. # in general, because it only uses one of the two broadcasting
  458. # rules (adding a new "1" dimension to the left of the shape),
  459. # applied to the output instead of an input. In NumPy 2.0, this kind
  460. # of broadcasting assignment will likely be disallowed.
  461. a[...] = np.arange(6)[::-1].reshape(1, 2, 3)
  462. assert_equal(a, [[5, 4, 3], [2, 1, 0]])
  463. # The other type of broadcasting would require a reduction operation.
  464. def assign(a, b):
  465. a[...] = b
  466. assert_raises(ValueError, assign, a, np.arange(12).reshape(2, 2, 3))
  467. def test_assignment_errors(self):
  468. # Address issue #2276
  469. class C:
  470. pass
  471. a = np.zeros(1)
  472. def assign(v):
  473. a[0] = v
  474. assert_raises((AttributeError, TypeError), assign, C())
  475. assert_raises(ValueError, assign, [1])
  476. def test_unicode_assignment(self):
  477. # gh-5049
  478. from numpy.core.numeric import set_string_function
  479. @contextmanager
  480. def inject_str(s):
  481. """ replace ndarray.__str__ temporarily """
  482. set_string_function(lambda x: s, repr=False)
  483. try:
  484. yield
  485. finally:
  486. set_string_function(None, repr=False)
  487. a1d = np.array(['test'])
  488. a0d = np.array('done')
  489. with inject_str('bad'):
  490. a1d[0] = a0d # previously this would invoke __str__
  491. assert_equal(a1d[0], 'done')
  492. # this would crash for the same reason
  493. np.array([np.array('\xe5\xe4\xf6')])
  494. def test_stringlike_empty_list(self):
  495. # gh-8902
  496. u = np.array(['done'])
  497. b = np.array([b'done'])
  498. class bad_sequence:
  499. def __getitem__(self): pass
  500. def __len__(self): raise RuntimeError
  501. assert_raises(ValueError, operator.setitem, u, 0, [])
  502. assert_raises(ValueError, operator.setitem, b, 0, [])
  503. assert_raises(ValueError, operator.setitem, u, 0, bad_sequence())
  504. assert_raises(ValueError, operator.setitem, b, 0, bad_sequence())
  505. def test_longdouble_assignment(self):
  506. # only relevant if longdouble is larger than float
  507. # we're looking for loss of precision
  508. for dtype in (np.longdouble, np.longcomplex):
  509. # gh-8902
  510. tinyb = np.nextafter(np.longdouble(0), 1).astype(dtype)
  511. tinya = np.nextafter(np.longdouble(0), -1).astype(dtype)
  512. # construction
  513. tiny1d = np.array([tinya])
  514. assert_equal(tiny1d[0], tinya)
  515. # scalar = scalar
  516. tiny1d[0] = tinyb
  517. assert_equal(tiny1d[0], tinyb)
  518. # 0d = scalar
  519. tiny1d[0, ...] = tinya
  520. assert_equal(tiny1d[0], tinya)
  521. # 0d = 0d
  522. tiny1d[0, ...] = tinyb[...]
  523. assert_equal(tiny1d[0], tinyb)
  524. # scalar = 0d
  525. tiny1d[0] = tinyb[...]
  526. assert_equal(tiny1d[0], tinyb)
  527. arr = np.array([np.array(tinya)])
  528. assert_equal(arr[0], tinya)
  529. def test_cast_to_string(self):
  530. # cast to str should do "str(scalar)", not "str(scalar.item())"
  531. # Example: In python2, str(float) is truncated, so we want to avoid
  532. # str(np.float64(...).item()) as this would incorrectly truncate.
  533. a = np.zeros(1, dtype='S20')
  534. a[:] = np.array(['1.12345678901234567890'], dtype='f8')
  535. assert_equal(a[0], b"1.1234567890123457")
  536. class TestDtypedescr:
  537. def test_construction(self):
  538. d1 = np.dtype('i4')
  539. assert_equal(d1, np.dtype(np.int32))
  540. d2 = np.dtype('f8')
  541. assert_equal(d2, np.dtype(np.float64))
  542. def test_byteorders(self):
  543. assert_(np.dtype('<i4') != np.dtype('>i4'))
  544. assert_(np.dtype([('a', '<i4')]) != np.dtype([('a', '>i4')]))
  545. def test_structured_non_void(self):
  546. fields = [('a', '<i2'), ('b', '<i2')]
  547. dt_int = np.dtype(('i4', fields))
  548. assert_equal(str(dt_int), "(numpy.int32, [('a', '<i2'), ('b', '<i2')])")
  549. # gh-9821
  550. arr_int = np.zeros(4, dt_int)
  551. assert_equal(repr(arr_int),
  552. "array([0, 0, 0, 0], dtype=(numpy.int32, [('a', '<i2'), ('b', '<i2')]))")
  553. class TestZeroRank:
  554. def setup_method(self):
  555. self.d = np.array(0), np.array('x', object)
  556. def test_ellipsis_subscript(self):
  557. a, b = self.d
  558. assert_equal(a[...], 0)
  559. assert_equal(b[...], 'x')
  560. assert_(a[...].base is a) # `a[...] is a` in numpy <1.9.
  561. assert_(b[...].base is b) # `b[...] is b` in numpy <1.9.
  562. def test_empty_subscript(self):
  563. a, b = self.d
  564. assert_equal(a[()], 0)
  565. assert_equal(b[()], 'x')
  566. assert_(type(a[()]) is a.dtype.type)
  567. assert_(type(b[()]) is str)
  568. def test_invalid_subscript(self):
  569. a, b = self.d
  570. assert_raises(IndexError, lambda x: x[0], a)
  571. assert_raises(IndexError, lambda x: x[0], b)
  572. assert_raises(IndexError, lambda x: x[np.array([], int)], a)
  573. assert_raises(IndexError, lambda x: x[np.array([], int)], b)
  574. def test_ellipsis_subscript_assignment(self):
  575. a, b = self.d
  576. a[...] = 42
  577. assert_equal(a, 42)
  578. b[...] = ''
  579. assert_equal(b.item(), '')
  580. def test_empty_subscript_assignment(self):
  581. a, b = self.d
  582. a[()] = 42
  583. assert_equal(a, 42)
  584. b[()] = ''
  585. assert_equal(b.item(), '')
  586. def test_invalid_subscript_assignment(self):
  587. a, b = self.d
  588. def assign(x, i, v):
  589. x[i] = v
  590. assert_raises(IndexError, assign, a, 0, 42)
  591. assert_raises(IndexError, assign, b, 0, '')
  592. assert_raises(ValueError, assign, a, (), '')
  593. def test_newaxis(self):
  594. a, b = self.d
  595. assert_equal(a[np.newaxis].shape, (1,))
  596. assert_equal(a[..., np.newaxis].shape, (1,))
  597. assert_equal(a[np.newaxis, ...].shape, (1,))
  598. assert_equal(a[..., np.newaxis].shape, (1,))
  599. assert_equal(a[np.newaxis, ..., np.newaxis].shape, (1, 1))
  600. assert_equal(a[..., np.newaxis, np.newaxis].shape, (1, 1))
  601. assert_equal(a[np.newaxis, np.newaxis, ...].shape, (1, 1))
  602. assert_equal(a[(np.newaxis,)*10].shape, (1,)*10)
  603. def test_invalid_newaxis(self):
  604. a, b = self.d
  605. def subscript(x, i):
  606. x[i]
  607. assert_raises(IndexError, subscript, a, (np.newaxis, 0))
  608. assert_raises(IndexError, subscript, a, (np.newaxis,)*50)
  609. def test_constructor(self):
  610. x = np.ndarray(())
  611. x[()] = 5
  612. assert_equal(x[()], 5)
  613. y = np.ndarray((), buffer=x)
  614. y[()] = 6
  615. assert_equal(x[()], 6)
  616. # strides and shape must be the same length
  617. with pytest.raises(ValueError):
  618. np.ndarray((2,), strides=())
  619. with pytest.raises(ValueError):
  620. np.ndarray((), strides=(2,))
  621. def test_output(self):
  622. x = np.array(2)
  623. assert_raises(ValueError, np.add, x, [1], x)
  624. def test_real_imag(self):
  625. # contiguity checks are for gh-11245
  626. x = np.array(1j)
  627. xr = x.real
  628. xi = x.imag
  629. assert_equal(xr, np.array(0))
  630. assert_(type(xr) is np.ndarray)
  631. assert_equal(xr.flags.contiguous, True)
  632. assert_equal(xr.flags.f_contiguous, True)
  633. assert_equal(xi, np.array(1))
  634. assert_(type(xi) is np.ndarray)
  635. assert_equal(xi.flags.contiguous, True)
  636. assert_equal(xi.flags.f_contiguous, True)
  637. class TestScalarIndexing:
  638. def setup_method(self):
  639. self.d = np.array([0, 1])[0]
  640. def test_ellipsis_subscript(self):
  641. a = self.d
  642. assert_equal(a[...], 0)
  643. assert_equal(a[...].shape, ())
  644. def test_empty_subscript(self):
  645. a = self.d
  646. assert_equal(a[()], 0)
  647. assert_equal(a[()].shape, ())
  648. def test_invalid_subscript(self):
  649. a = self.d
  650. assert_raises(IndexError, lambda x: x[0], a)
  651. assert_raises(IndexError, lambda x: x[np.array([], int)], a)
  652. def test_invalid_subscript_assignment(self):
  653. a = self.d
  654. def assign(x, i, v):
  655. x[i] = v
  656. assert_raises(TypeError, assign, a, 0, 42)
  657. def test_newaxis(self):
  658. a = self.d
  659. assert_equal(a[np.newaxis].shape, (1,))
  660. assert_equal(a[..., np.newaxis].shape, (1,))
  661. assert_equal(a[np.newaxis, ...].shape, (1,))
  662. assert_equal(a[..., np.newaxis].shape, (1,))
  663. assert_equal(a[np.newaxis, ..., np.newaxis].shape, (1, 1))
  664. assert_equal(a[..., np.newaxis, np.newaxis].shape, (1, 1))
  665. assert_equal(a[np.newaxis, np.newaxis, ...].shape, (1, 1))
  666. assert_equal(a[(np.newaxis,)*10].shape, (1,)*10)
  667. def test_invalid_newaxis(self):
  668. a = self.d
  669. def subscript(x, i):
  670. x[i]
  671. assert_raises(IndexError, subscript, a, (np.newaxis, 0))
  672. assert_raises(IndexError, subscript, a, (np.newaxis,)*50)
  673. def test_overlapping_assignment(self):
  674. # With positive strides
  675. a = np.arange(4)
  676. a[:-1] = a[1:]
  677. assert_equal(a, [1, 2, 3, 3])
  678. a = np.arange(4)
  679. a[1:] = a[:-1]
  680. assert_equal(a, [0, 0, 1, 2])
  681. # With positive and negative strides
  682. a = np.arange(4)
  683. a[:] = a[::-1]
  684. assert_equal(a, [3, 2, 1, 0])
  685. a = np.arange(6).reshape(2, 3)
  686. a[::-1,:] = a[:, ::-1]
  687. assert_equal(a, [[5, 4, 3], [2, 1, 0]])
  688. a = np.arange(6).reshape(2, 3)
  689. a[::-1, ::-1] = a[:, ::-1]
  690. assert_equal(a, [[3, 4, 5], [0, 1, 2]])
  691. # With just one element overlapping
  692. a = np.arange(5)
  693. a[:3] = a[2:]
  694. assert_equal(a, [2, 3, 4, 3, 4])
  695. a = np.arange(5)
  696. a[2:] = a[:3]
  697. assert_equal(a, [0, 1, 0, 1, 2])
  698. a = np.arange(5)
  699. a[2::-1] = a[2:]
  700. assert_equal(a, [4, 3, 2, 3, 4])
  701. a = np.arange(5)
  702. a[2:] = a[2::-1]
  703. assert_equal(a, [0, 1, 2, 1, 0])
  704. a = np.arange(5)
  705. a[2::-1] = a[:1:-1]
  706. assert_equal(a, [2, 3, 4, 3, 4])
  707. a = np.arange(5)
  708. a[:1:-1] = a[2::-1]
  709. assert_equal(a, [0, 1, 0, 1, 2])
  710. class TestCreation:
  711. """
  712. Test the np.array constructor
  713. """
  714. def test_from_attribute(self):
  715. class x:
  716. def __array__(self, dtype=None):
  717. pass
  718. assert_raises(ValueError, np.array, x())
  719. def test_from_string(self):
  720. types = np.typecodes['AllInteger'] + np.typecodes['Float']
  721. nstr = ['123', '123']
  722. result = np.array([123, 123], dtype=int)
  723. for type in types:
  724. msg = 'String conversion for %s' % type
  725. assert_equal(np.array(nstr, dtype=type), result, err_msg=msg)
  726. def test_void(self):
  727. arr = np.array([], dtype='V')
  728. assert arr.dtype == 'V8' # current default
  729. # Same length scalars (those that go to the same void) work:
  730. arr = np.array([b"1234", b"1234"], dtype="V")
  731. assert arr.dtype == "V4"
  732. # Promoting different lengths will fail (pre 1.20 this worked)
  733. # by going via S5 and casting to V5.
  734. with pytest.raises(TypeError):
  735. np.array([b"1234", b"12345"], dtype="V")
  736. with pytest.raises(TypeError):
  737. np.array([b"12345", b"1234"], dtype="V")
  738. # Check the same for the casting path:
  739. arr = np.array([b"1234", b"1234"], dtype="O").astype("V")
  740. assert arr.dtype == "V4"
  741. with pytest.raises(TypeError):
  742. np.array([b"1234", b"12345"], dtype="O").astype("V")
  743. @pytest.mark.parametrize("idx",
  744. [pytest.param(Ellipsis, id="arr"), pytest.param((), id="scalar")])
  745. def test_structured_void_promotion(self, idx):
  746. arr = np.array(
  747. [np.array(1, dtype="i,i")[idx], np.array(2, dtype='i,i')[idx]],
  748. dtype="V")
  749. assert_array_equal(arr, np.array([(1, 1), (2, 2)], dtype="i,i"))
  750. # The following fails to promote the two dtypes, resulting in an error
  751. with pytest.raises(TypeError):
  752. np.array(
  753. [np.array(1, dtype="i,i")[idx], np.array(2, dtype='i,i,i')[idx]],
  754. dtype="V")
  755. def test_too_big_error(self):
  756. # 45341 is the smallest integer greater than sqrt(2**31 - 1).
  757. # 3037000500 is the smallest integer greater than sqrt(2**63 - 1).
  758. # We want to make sure that the square byte array with those dimensions
  759. # is too big on 32 or 64 bit systems respectively.
  760. if np.iinfo('intp').max == 2**31 - 1:
  761. shape = (46341, 46341)
  762. elif np.iinfo('intp').max == 2**63 - 1:
  763. shape = (3037000500, 3037000500)
  764. else:
  765. return
  766. assert_raises(ValueError, np.empty, shape, dtype=np.int8)
  767. assert_raises(ValueError, np.zeros, shape, dtype=np.int8)
  768. assert_raises(ValueError, np.ones, shape, dtype=np.int8)
  769. @pytest.mark.skipif(np.dtype(np.intp).itemsize != 8,
  770. reason="malloc may not fail on 32 bit systems")
  771. def test_malloc_fails(self):
  772. # This test is guaranteed to fail due to a too large allocation
  773. with assert_raises(np.core._exceptions._ArrayMemoryError):
  774. np.empty(np.iinfo(np.intp).max, dtype=np.uint8)
  775. def test_zeros(self):
  776. types = np.typecodes['AllInteger'] + np.typecodes['AllFloat']
  777. for dt in types:
  778. d = np.zeros((13,), dtype=dt)
  779. assert_equal(np.count_nonzero(d), 0)
  780. # true for ieee floats
  781. assert_equal(d.sum(), 0)
  782. assert_(not d.any())
  783. d = np.zeros(2, dtype='(2,4)i4')
  784. assert_equal(np.count_nonzero(d), 0)
  785. assert_equal(d.sum(), 0)
  786. assert_(not d.any())
  787. d = np.zeros(2, dtype='4i4')
  788. assert_equal(np.count_nonzero(d), 0)
  789. assert_equal(d.sum(), 0)
  790. assert_(not d.any())
  791. d = np.zeros(2, dtype='(2,4)i4, (2,4)i4')
  792. assert_equal(np.count_nonzero(d), 0)
  793. @pytest.mark.slow
  794. def test_zeros_big(self):
  795. # test big array as they might be allocated different by the system
  796. types = np.typecodes['AllInteger'] + np.typecodes['AllFloat']
  797. for dt in types:
  798. d = np.zeros((30 * 1024**2,), dtype=dt)
  799. assert_(not d.any())
  800. # This test can fail on 32-bit systems due to insufficient
  801. # contiguous memory. Deallocating the previous array increases the
  802. # chance of success.
  803. del(d)
  804. def test_zeros_obj(self):
  805. # test initialization from PyLong(0)
  806. d = np.zeros((13,), dtype=object)
  807. assert_array_equal(d, [0] * 13)
  808. assert_equal(np.count_nonzero(d), 0)
  809. def test_zeros_obj_obj(self):
  810. d = np.zeros(10, dtype=[('k', object, 2)])
  811. assert_array_equal(d['k'], 0)
  812. def test_zeros_like_like_zeros(self):
  813. # test zeros_like returns the same as zeros
  814. for c in np.typecodes['All']:
  815. if c == 'V':
  816. continue
  817. d = np.zeros((3,3), dtype=c)
  818. assert_array_equal(np.zeros_like(d), d)
  819. assert_equal(np.zeros_like(d).dtype, d.dtype)
  820. # explicitly check some special cases
  821. d = np.zeros((3,3), dtype='S5')
  822. assert_array_equal(np.zeros_like(d), d)
  823. assert_equal(np.zeros_like(d).dtype, d.dtype)
  824. d = np.zeros((3,3), dtype='U5')
  825. assert_array_equal(np.zeros_like(d), d)
  826. assert_equal(np.zeros_like(d).dtype, d.dtype)
  827. d = np.zeros((3,3), dtype='<i4')
  828. assert_array_equal(np.zeros_like(d), d)
  829. assert_equal(np.zeros_like(d).dtype, d.dtype)
  830. d = np.zeros((3,3), dtype='>i4')
  831. assert_array_equal(np.zeros_like(d), d)
  832. assert_equal(np.zeros_like(d).dtype, d.dtype)
  833. d = np.zeros((3,3), dtype='<M8[s]')
  834. assert_array_equal(np.zeros_like(d), d)
  835. assert_equal(np.zeros_like(d).dtype, d.dtype)
  836. d = np.zeros((3,3), dtype='>M8[s]')
  837. assert_array_equal(np.zeros_like(d), d)
  838. assert_equal(np.zeros_like(d).dtype, d.dtype)
  839. d = np.zeros((3,3), dtype='f4,f4')
  840. assert_array_equal(np.zeros_like(d), d)
  841. assert_equal(np.zeros_like(d).dtype, d.dtype)
  842. def test_empty_unicode(self):
  843. # don't throw decode errors on garbage memory
  844. for i in range(5, 100, 5):
  845. d = np.empty(i, dtype='U')
  846. str(d)
  847. def test_sequence_non_homogeneous(self):
  848. assert_equal(np.array([4, 2**80]).dtype, object)
  849. assert_equal(np.array([4, 2**80, 4]).dtype, object)
  850. assert_equal(np.array([2**80, 4]).dtype, object)
  851. assert_equal(np.array([2**80] * 3).dtype, object)
  852. assert_equal(np.array([[1, 1],[1j, 1j]]).dtype, complex)
  853. assert_equal(np.array([[1j, 1j],[1, 1]]).dtype, complex)
  854. assert_equal(np.array([[1, 1, 1],[1, 1j, 1.], [1, 1, 1]]).dtype, complex)
  855. def test_non_sequence_sequence(self):
  856. """Should not segfault.
  857. Class Fail breaks the sequence protocol for new style classes, i.e.,
  858. those derived from object. Class Map is a mapping type indicated by
  859. raising a ValueError. At some point we may raise a warning instead
  860. of an error in the Fail case.
  861. """
  862. class Fail:
  863. def __len__(self):
  864. return 1
  865. def __getitem__(self, index):
  866. raise ValueError()
  867. class Map:
  868. def __len__(self):
  869. return 1
  870. def __getitem__(self, index):
  871. raise KeyError()
  872. a = np.array([Map()])
  873. assert_(a.shape == (1,))
  874. assert_(a.dtype == np.dtype(object))
  875. assert_raises(ValueError, np.array, [Fail()])
  876. def test_no_len_object_type(self):
  877. # gh-5100, want object array from iterable object without len()
  878. class Point2:
  879. def __init__(self):
  880. pass
  881. def __getitem__(self, ind):
  882. if ind in [0, 1]:
  883. return ind
  884. else:
  885. raise IndexError()
  886. d = np.array([Point2(), Point2(), Point2()])
  887. assert_equal(d.dtype, np.dtype(object))
  888. def test_false_len_sequence(self):
  889. # gh-7264, segfault for this example
  890. class C:
  891. def __getitem__(self, i):
  892. raise IndexError
  893. def __len__(self):
  894. return 42
  895. a = np.array(C()) # segfault?
  896. assert_equal(len(a), 0)
  897. def test_false_len_iterable(self):
  898. # Special case where a bad __getitem__ makes us fall back on __iter__:
  899. class C:
  900. def __getitem__(self, x):
  901. raise Exception
  902. def __iter__(self):
  903. return iter(())
  904. def __len__(self):
  905. return 2
  906. a = np.empty(2)
  907. with assert_raises(ValueError):
  908. a[:] = C() # Segfault!
  909. np.array(C()) == list(C())
  910. def test_failed_len_sequence(self):
  911. # gh-7393
  912. class A:
  913. def __init__(self, data):
  914. self._data = data
  915. def __getitem__(self, item):
  916. return type(self)(self._data[item])
  917. def __len__(self):
  918. return len(self._data)
  919. # len(d) should give 3, but len(d[0]) will fail
  920. d = A([1,2,3])
  921. assert_equal(len(np.array(d)), 3)
  922. def test_array_too_big(self):
  923. # Test that array creation succeeds for arrays addressable by intp
  924. # on the byte level and fails for too large arrays.
  925. buf = np.zeros(100)
  926. max_bytes = np.iinfo(np.intp).max
  927. for dtype in ["intp", "S20", "b"]:
  928. dtype = np.dtype(dtype)
  929. itemsize = dtype.itemsize
  930. np.ndarray(buffer=buf, strides=(0,),
  931. shape=(max_bytes//itemsize,), dtype=dtype)
  932. assert_raises(ValueError, np.ndarray, buffer=buf, strides=(0,),
  933. shape=(max_bytes//itemsize + 1,), dtype=dtype)
  934. def _ragged_creation(self, seq):
  935. # without dtype=object, the ragged object raises
  936. with pytest.raises(ValueError, match=".*detected shape was"):
  937. a = np.array(seq)
  938. return np.array(seq, dtype=object)
  939. def test_ragged_ndim_object(self):
  940. # Lists of mismatching depths are treated as object arrays
  941. a = self._ragged_creation([[1], 2, 3])
  942. assert_equal(a.shape, (3,))
  943. assert_equal(a.dtype, object)
  944. a = self._ragged_creation([1, [2], 3])
  945. assert_equal(a.shape, (3,))
  946. assert_equal(a.dtype, object)
  947. a = self._ragged_creation([1, 2, [3]])
  948. assert_equal(a.shape, (3,))
  949. assert_equal(a.dtype, object)
  950. def test_ragged_shape_object(self):
  951. # The ragged dimension of a list is turned into an object array
  952. a = self._ragged_creation([[1, 1], [2], [3]])
  953. assert_equal(a.shape, (3,))
  954. assert_equal(a.dtype, object)
  955. a = self._ragged_creation([[1], [2, 2], [3]])
  956. assert_equal(a.shape, (3,))
  957. assert_equal(a.dtype, object)
  958. a = self._ragged_creation([[1], [2], [3, 3]])
  959. assert a.shape == (3,)
  960. assert a.dtype == object
  961. def test_array_of_ragged_array(self):
  962. outer = np.array([None, None])
  963. outer[0] = outer[1] = np.array([1, 2, 3])
  964. assert np.array(outer).shape == (2,)
  965. assert np.array([outer]).shape == (1, 2)
  966. outer_ragged = np.array([None, None])
  967. outer_ragged[0] = np.array([1, 2, 3])
  968. outer_ragged[1] = np.array([1, 2, 3, 4])
  969. # should both of these emit deprecation warnings?
  970. assert np.array(outer_ragged).shape == (2,)
  971. assert np.array([outer_ragged]).shape == (1, 2,)
  972. def test_deep_nonragged_object(self):
  973. # None of these should raise, even though they are missing dtype=object
  974. a = np.array([[[Decimal(1)]]])
  975. a = np.array([1, Decimal(1)])
  976. a = np.array([[1], [Decimal(1)]])
  977. @pytest.mark.parametrize("dtype", [object, "O,O", "O,(3)O", "(2,3)O"])
  978. @pytest.mark.parametrize("function", [
  979. np.ndarray, np.empty,
  980. lambda shape, dtype: np.empty_like(np.empty(shape, dtype=dtype))])
  981. def test_object_initialized_to_None(self, function, dtype):
  982. # NumPy has support for object fields to be NULL (meaning None)
  983. # but generally, we should always fill with the proper None, and
  984. # downstream may rely on that. (For fully initialized arrays!)
  985. arr = function(3, dtype=dtype)
  986. # We expect a fill value of None, which is not NULL:
  987. expected = np.array(None).tobytes()
  988. expected = expected * (arr.nbytes // len(expected))
  989. assert arr.tobytes() == expected
  990. class TestStructured:
  991. def test_subarray_field_access(self):
  992. a = np.zeros((3, 5), dtype=[('a', ('i4', (2, 2)))])
  993. a['a'] = np.arange(60).reshape(3, 5, 2, 2)
  994. # Since the subarray is always in C-order, a transpose
  995. # does not swap the subarray:
  996. assert_array_equal(a.T['a'], a['a'].transpose(1, 0, 2, 3))
  997. # In Fortran order, the subarray gets appended
  998. # like in all other cases, not prepended as a special case
  999. b = a.copy(order='F')
  1000. assert_equal(a['a'].shape, b['a'].shape)
  1001. assert_equal(a.T['a'].shape, a.T.copy()['a'].shape)
  1002. def test_subarray_comparison(self):
  1003. # Check that comparisons between record arrays with
  1004. # multi-dimensional field types work properly
  1005. a = np.rec.fromrecords(
  1006. [([1, 2, 3], 'a', [[1, 2], [3, 4]]), ([3, 3, 3], 'b', [[0, 0], [0, 0]])],
  1007. dtype=[('a', ('f4', 3)), ('b', object), ('c', ('i4', (2, 2)))])
  1008. b = a.copy()
  1009. assert_equal(a == b, [True, True])
  1010. assert_equal(a != b, [False, False])
  1011. b[1].b = 'c'
  1012. assert_equal(a == b, [True, False])
  1013. assert_equal(a != b, [False, True])
  1014. for i in range(3):
  1015. b[0].a = a[0].a
  1016. b[0].a[i] = 5
  1017. assert_equal(a == b, [False, False])
  1018. assert_equal(a != b, [True, True])
  1019. for i in range(2):
  1020. for j in range(2):
  1021. b = a.copy()
  1022. b[0].c[i, j] = 10
  1023. assert_equal(a == b, [False, True])
  1024. assert_equal(a != b, [True, False])
  1025. # Check that broadcasting with a subarray works, including cases that
  1026. # require promotion to work:
  1027. a = np.array([[(0,)], [(1,)]], dtype=[('a', 'f8')])
  1028. b = np.array([(0,), (0,), (1,)], dtype=[('a', 'f8')])
  1029. assert_equal(a == b, [[True, True, False], [False, False, True]])
  1030. assert_equal(b == a, [[True, True, False], [False, False, True]])
  1031. a = np.array([[(0,)], [(1,)]], dtype=[('a', 'f8', (1,))])
  1032. b = np.array([(0,), (0,), (1,)], dtype=[('a', 'f8', (1,))])
  1033. assert_equal(a == b, [[True, True, False], [False, False, True]])
  1034. assert_equal(b == a, [[True, True, False], [False, False, True]])
  1035. a = np.array([[([0, 0],)], [([1, 1],)]], dtype=[('a', 'f8', (2,))])
  1036. b = np.array([([0, 0],), ([0, 1],), ([1, 1],)], dtype=[('a', 'f8', (2,))])
  1037. assert_equal(a == b, [[True, False, False], [False, False, True]])
  1038. assert_equal(b == a, [[True, False, False], [False, False, True]])
  1039. # Check that broadcasting Fortran-style arrays with a subarray work
  1040. a = np.array([[([0, 0],)], [([1, 1],)]], dtype=[('a', 'f8', (2,))], order='F')
  1041. b = np.array([([0, 0],), ([0, 1],), ([1, 1],)], dtype=[('a', 'f8', (2,))])
  1042. assert_equal(a == b, [[True, False, False], [False, False, True]])
  1043. assert_equal(b == a, [[True, False, False], [False, False, True]])
  1044. # Check that incompatible sub-array shapes don't result to broadcasting
  1045. x = np.zeros((1,), dtype=[('a', ('f4', (1, 2))), ('b', 'i1')])
  1046. y = np.zeros((1,), dtype=[('a', ('f4', (2,))), ('b', 'i1')])
  1047. # The main importance is that it does not return True:
  1048. with pytest.raises(TypeError):
  1049. x == y
  1050. x = np.zeros((1,), dtype=[('a', ('f4', (2, 1))), ('b', 'i1')])
  1051. y = np.zeros((1,), dtype=[('a', ('f4', (2,))), ('b', 'i1')])
  1052. # The main importance is that it does not return True:
  1053. with pytest.raises(TypeError):
  1054. x == y
  1055. def test_empty_structured_array_comparison(self):
  1056. # Check that comparison works on empty arrays with nontrivially
  1057. # shaped fields
  1058. a = np.zeros(0, [('a', '<f8', (1, 1))])
  1059. assert_equal(a, a)
  1060. a = np.zeros(0, [('a', '<f8', (1,))])
  1061. assert_equal(a, a)
  1062. a = np.zeros((0, 0), [('a', '<f8', (1, 1))])
  1063. assert_equal(a, a)
  1064. a = np.zeros((1, 0, 1), [('a', '<f8', (1, 1))])
  1065. assert_equal(a, a)
  1066. def test_structured_comparisons_with_promotion(self):
  1067. # Check that structured arrays can be compared so long as their
  1068. # dtypes promote fine:
  1069. a = np.array([(5, 42), (10, 1)], dtype=[('a', '>i8'), ('b', '<f8')])
  1070. b = np.array([(5, 43), (10, 1)], dtype=[('a', '<i8'), ('b', '>f8')])
  1071. assert_equal(a == b, [False, True])
  1072. assert_equal(a != b, [True, False])
  1073. a = np.array([(5, 42), (10, 1)], dtype=[('a', '>f8'), ('b', '<f8')])
  1074. b = np.array([(5, 43), (10, 1)], dtype=[('a', '<i8'), ('b', '>i8')])
  1075. assert_equal(a == b, [False, True])
  1076. assert_equal(a != b, [True, False])
  1077. # Including with embedded subarray dtype (although subarray comparison
  1078. # itself may still be a bit weird and compare the raw data)
  1079. a = np.array([(5, 42), (10, 1)], dtype=[('a', '10>f8'), ('b', '5<f8')])
  1080. b = np.array([(5, 43), (10, 1)], dtype=[('a', '10<i8'), ('b', '5>i8')])
  1081. assert_equal(a == b, [False, True])
  1082. assert_equal(a != b, [True, False])
  1083. def test_void_comparison_failures(self):
  1084. # In principle, one could decide to return an array of False for some
  1085. # if comparisons are impossible. But right now we return TypeError
  1086. # when "void" dtype are involved.
  1087. x = np.zeros(3, dtype=[('a', 'i1')])
  1088. y = np.zeros(3)
  1089. # Cannot compare non-structured to structured:
  1090. with pytest.raises(TypeError):
  1091. x == y
  1092. # Added title prevents promotion, but casts are OK:
  1093. y = np.zeros(3, dtype=[(('title', 'a'), 'i1')])
  1094. assert np.can_cast(y.dtype, x.dtype)
  1095. with pytest.raises(TypeError):
  1096. x == y
  1097. x = np.zeros(3, dtype="V7")
  1098. y = np.zeros(3, dtype="V8")
  1099. with pytest.raises(TypeError):
  1100. x == y
  1101. def test_casting(self):
  1102. # Check that casting a structured array to change its byte order
  1103. # works
  1104. a = np.array([(1,)], dtype=[('a', '<i4')])
  1105. assert_(np.can_cast(a.dtype, [('a', '>i4')], casting='unsafe'))
  1106. b = a.astype([('a', '>i4')])
  1107. assert_equal(b, a.byteswap().newbyteorder())
  1108. assert_equal(a['a'][0], b['a'][0])
  1109. # Check that equality comparison works on structured arrays if
  1110. # they are 'equiv'-castable
  1111. a = np.array([(5, 42), (10, 1)], dtype=[('a', '>i4'), ('b', '<f8')])
  1112. b = np.array([(5, 42), (10, 1)], dtype=[('a', '<i4'), ('b', '>f8')])
  1113. assert_(np.can_cast(a.dtype, b.dtype, casting='equiv'))
  1114. assert_equal(a == b, [True, True])
  1115. # Check that 'equiv' casting can change byte order
  1116. assert_(np.can_cast(a.dtype, b.dtype, casting='equiv'))
  1117. c = a.astype(b.dtype, casting='equiv')
  1118. assert_equal(a == c, [True, True])
  1119. # Check that 'safe' casting can change byte order and up-cast
  1120. # fields
  1121. t = [('a', '<i8'), ('b', '>f8')]
  1122. assert_(np.can_cast(a.dtype, t, casting='safe'))
  1123. c = a.astype(t, casting='safe')
  1124. assert_equal((c == np.array([(5, 42), (10, 1)], dtype=t)),
  1125. [True, True])
  1126. # Check that 'same_kind' casting can change byte order and
  1127. # change field widths within a "kind"
  1128. t = [('a', '<i4'), ('b', '>f4')]
  1129. assert_(np.can_cast(a.dtype, t, casting='same_kind'))
  1130. c = a.astype(t, casting='same_kind')
  1131. assert_equal((c == np.array([(5, 42), (10, 1)], dtype=t)),
  1132. [True, True])
  1133. # Check that casting fails if the casting rule should fail on
  1134. # any of the fields
  1135. t = [('a', '>i8'), ('b', '<f4')]
  1136. assert_(not np.can_cast(a.dtype, t, casting='safe'))
  1137. assert_raises(TypeError, a.astype, t, casting='safe')
  1138. t = [('a', '>i2'), ('b', '<f8')]
  1139. assert_(not np.can_cast(a.dtype, t, casting='equiv'))
  1140. assert_raises(TypeError, a.astype, t, casting='equiv')
  1141. t = [('a', '>i8'), ('b', '<i2')]
  1142. assert_(not np.can_cast(a.dtype, t, casting='same_kind'))
  1143. assert_raises(TypeError, a.astype, t, casting='same_kind')
  1144. assert_(not np.can_cast(a.dtype, b.dtype, casting='no'))
  1145. assert_raises(TypeError, a.astype, b.dtype, casting='no')
  1146. # Check that non-'unsafe' casting can't change the set of field names
  1147. for casting in ['no', 'safe', 'equiv', 'same_kind']:
  1148. t = [('a', '>i4')]
  1149. assert_(not np.can_cast(a.dtype, t, casting=casting))
  1150. t = [('a', '>i4'), ('b', '<f8'), ('c', 'i4')]
  1151. assert_(not np.can_cast(a.dtype, t, casting=casting))
  1152. def test_objview(self):
  1153. # https://github.com/numpy/numpy/issues/3286
  1154. a = np.array([], dtype=[('a', 'f'), ('b', 'f'), ('c', 'O')])
  1155. a[['a', 'b']] # TypeError?
  1156. # https://github.com/numpy/numpy/issues/3253
  1157. dat2 = np.zeros(3, [('A', 'i'), ('B', '|O')])
  1158. dat2[['B', 'A']] # TypeError?
  1159. def test_setfield(self):
  1160. # https://github.com/numpy/numpy/issues/3126
  1161. struct_dt = np.dtype([('elem', 'i4', 5),])
  1162. dt = np.dtype([('field', 'i4', 10),('struct', struct_dt)])
  1163. x = np.zeros(1, dt)
  1164. x[0]['field'] = np.ones(10, dtype='i4')
  1165. x[0]['struct'] = np.ones(1, dtype=struct_dt)
  1166. assert_equal(x[0]['field'], np.ones(10, dtype='i4'))
  1167. def test_setfield_object(self):
  1168. # make sure object field assignment with ndarray value
  1169. # on void scalar mimics setitem behavior
  1170. b = np.zeros(1, dtype=[('x', 'O')])
  1171. # next line should work identically to b['x'][0] = np.arange(3)
  1172. b[0]['x'] = np.arange(3)
  1173. assert_equal(b[0]['x'], np.arange(3))
  1174. # check that broadcasting check still works
  1175. c = np.zeros(1, dtype=[('x', 'O', 5)])
  1176. def testassign():
  1177. c[0]['x'] = np.arange(3)
  1178. assert_raises(ValueError, testassign)
  1179. def test_zero_width_string(self):
  1180. # Test for PR #6430 / issues #473, #4955, #2585
  1181. dt = np.dtype([('I', int), ('S', 'S0')])
  1182. x = np.zeros(4, dtype=dt)
  1183. assert_equal(x['S'], [b'', b'', b'', b''])
  1184. assert_equal(x['S'].itemsize, 0)
  1185. x['S'] = ['a', 'b', 'c', 'd']
  1186. assert_equal(x['S'], [b'', b'', b'', b''])
  1187. assert_equal(x['I'], [0, 0, 0, 0])
  1188. # Variation on test case from #4955
  1189. x['S'][x['I'] == 0] = 'hello'
  1190. assert_equal(x['S'], [b'', b'', b'', b''])
  1191. assert_equal(x['I'], [0, 0, 0, 0])
  1192. # Variation on test case from #2585
  1193. x['S'] = 'A'
  1194. assert_equal(x['S'], [b'', b'', b'', b''])
  1195. assert_equal(x['I'], [0, 0, 0, 0])
  1196. # Allow zero-width dtypes in ndarray constructor
  1197. y = np.ndarray(4, dtype=x['S'].dtype)
  1198. assert_equal(y.itemsize, 0)
  1199. assert_equal(x['S'], y)
  1200. # More tests for indexing an array with zero-width fields
  1201. assert_equal(np.zeros(4, dtype=[('a', 'S0,S0'),
  1202. ('b', 'u1')])['a'].itemsize, 0)
  1203. assert_equal(np.empty(3, dtype='S0,S0').itemsize, 0)
  1204. assert_equal(np.zeros(4, dtype='S0,u1')['f0'].itemsize, 0)
  1205. xx = x['S'].reshape((2, 2))
  1206. assert_equal(xx.itemsize, 0)
  1207. assert_equal(xx, [[b'', b''], [b'', b'']])
  1208. # check for no uninitialized memory due to viewing S0 array
  1209. assert_equal(xx[:].dtype, xx.dtype)
  1210. assert_array_equal(eval(repr(xx), dict(array=np.array)), xx)
  1211. b = io.BytesIO()
  1212. np.save(b, xx)
  1213. b.seek(0)
  1214. yy = np.load(b)
  1215. assert_equal(yy.itemsize, 0)
  1216. assert_equal(xx, yy)
  1217. with temppath(suffix='.npy') as tmp:
  1218. np.save(tmp, xx)
  1219. yy = np.load(tmp)
  1220. assert_equal(yy.itemsize, 0)
  1221. assert_equal(xx, yy)
  1222. def test_base_attr(self):
  1223. a = np.zeros(3, dtype='i4,f4')
  1224. b = a[0]
  1225. assert_(b.base is a)
  1226. def test_assignment(self):
  1227. def testassign(arr, v):
  1228. c = arr.copy()
  1229. c[0] = v # assign using setitem
  1230. c[1:] = v # assign using "dtype_transfer" code paths
  1231. return c
  1232. dt = np.dtype([('foo', 'i8'), ('bar', 'i8')])
  1233. arr = np.ones(2, dt)
  1234. v1 = np.array([(2,3)], dtype=[('foo', 'i8'), ('bar', 'i8')])
  1235. v2 = np.array([(2,3)], dtype=[('bar', 'i8'), ('foo', 'i8')])
  1236. v3 = np.array([(2,3)], dtype=[('bar', 'i8'), ('baz', 'i8')])
  1237. v4 = np.array([(2,)], dtype=[('bar', 'i8')])
  1238. v5 = np.array([(2,3)], dtype=[('foo', 'f8'), ('bar', 'f8')])
  1239. w = arr.view({'names': ['bar'], 'formats': ['i8'], 'offsets': [8]})
  1240. ans = np.array([(2,3),(2,3)], dtype=dt)
  1241. assert_equal(testassign(arr, v1), ans)
  1242. assert_equal(testassign(arr, v2), ans)
  1243. assert_equal(testassign(arr, v3), ans)
  1244. assert_raises(TypeError, lambda: testassign(arr, v4))
  1245. assert_equal(testassign(arr, v5), ans)
  1246. w[:] = 4
  1247. assert_equal(arr, np.array([(1,4),(1,4)], dtype=dt))
  1248. # test field-reordering, assignment by position, and self-assignment
  1249. a = np.array([(1,2,3)],
  1250. dtype=[('foo', 'i8'), ('bar', 'i8'), ('baz', 'f4')])
  1251. a[['foo', 'bar']] = a[['bar', 'foo']]
  1252. assert_equal(a[0].item(), (2,1,3))
  1253. # test that this works even for 'simple_unaligned' structs
  1254. # (ie, that PyArray_EquivTypes cares about field order too)
  1255. a = np.array([(1,2)], dtype=[('a', 'i4'), ('b', 'i4')])
  1256. a[['a', 'b']] = a[['b', 'a']]
  1257. assert_equal(a[0].item(), (2,1))
  1258. def test_scalar_assignment(self):
  1259. with assert_raises(ValueError):
  1260. arr = np.arange(25).reshape(5, 5)
  1261. arr.itemset(3)
  1262. def test_structuredscalar_indexing(self):
  1263. # test gh-7262
  1264. x = np.empty(shape=1, dtype="(2)3S,(2)3U")
  1265. assert_equal(x[["f0","f1"]][0], x[0][["f0","f1"]])
  1266. assert_equal(x[0], x[0][()])
  1267. def test_multiindex_titles(self):
  1268. a = np.zeros(4, dtype=[(('a', 'b'), 'i'), ('c', 'i'), ('d', 'i')])
  1269. assert_raises(KeyError, lambda : a[['a','c']])
  1270. assert_raises(KeyError, lambda : a[['a','a']])
  1271. assert_raises(ValueError, lambda : a[['b','b']]) # field exists, but repeated
  1272. a[['b','c']] # no exception
  1273. def test_structured_cast_promotion_fieldorder(self):
  1274. # gh-15494
  1275. # dtypes with different field names are not promotable
  1276. A = ("a", "<i8")
  1277. B = ("b", ">i8")
  1278. ab = np.array([(1, 2)], dtype=[A, B])
  1279. ba = np.array([(1, 2)], dtype=[B, A])
  1280. assert_raises(TypeError, np.concatenate, ab, ba)
  1281. assert_raises(TypeError, np.result_type, ab.dtype, ba.dtype)
  1282. assert_raises(TypeError, np.promote_types, ab.dtype, ba.dtype)
  1283. # dtypes with same field names/order but different memory offsets
  1284. # and byte-order are promotable to packed nbo.
  1285. assert_equal(np.promote_types(ab.dtype, ba[['a', 'b']].dtype),
  1286. repack_fields(ab.dtype.newbyteorder('N')))
  1287. # gh-13667
  1288. # dtypes with different fieldnames but castable field types are castable
  1289. assert_equal(np.can_cast(ab.dtype, ba.dtype), True)
  1290. assert_equal(ab.astype(ba.dtype).dtype, ba.dtype)
  1291. assert_equal(np.can_cast('f8,i8', [('f0', 'f8'), ('f1', 'i8')]), True)
  1292. assert_equal(np.can_cast('f8,i8', [('f1', 'f8'), ('f0', 'i8')]), True)
  1293. assert_equal(np.can_cast('f8,i8', [('f1', 'i8'), ('f0', 'f8')]), False)
  1294. assert_equal(np.can_cast('f8,i8', [('f1', 'i8'), ('f0', 'f8')],
  1295. casting='unsafe'), True)
  1296. ab[:] = ba # make sure assignment still works
  1297. # tests of type-promotion of corresponding fields
  1298. dt1 = np.dtype([("", "i4")])
  1299. dt2 = np.dtype([("", "i8")])
  1300. assert_equal(np.promote_types(dt1, dt2), np.dtype([('f0', 'i8')]))
  1301. assert_equal(np.promote_types(dt2, dt1), np.dtype([('f0', 'i8')]))
  1302. assert_raises(TypeError, np.promote_types, dt1, np.dtype([("", "V3")]))
  1303. assert_equal(np.promote_types('i4,f8', 'i8,f4'),
  1304. np.dtype([('f0', 'i8'), ('f1', 'f8')]))
  1305. # test nested case
  1306. dt1nest = np.dtype([("", dt1)])
  1307. dt2nest = np.dtype([("", dt2)])
  1308. assert_equal(np.promote_types(dt1nest, dt2nest),
  1309. np.dtype([('f0', np.dtype([('f0', 'i8')]))]))
  1310. # note that offsets are lost when promoting:
  1311. dt = np.dtype({'names': ['x'], 'formats': ['i4'], 'offsets': [8]})
  1312. a = np.ones(3, dtype=dt)
  1313. assert_equal(np.concatenate([a, a]).dtype, np.dtype([('x', 'i4')]))
  1314. @pytest.mark.parametrize("dtype_dict", [
  1315. dict(names=["a", "b"], formats=["i4", "f"], itemsize=100),
  1316. dict(names=["a", "b"], formats=["i4", "f"],
  1317. offsets=[0, 12])])
  1318. @pytest.mark.parametrize("align", [True, False])
  1319. def test_structured_promotion_packs(self, dtype_dict, align):
  1320. # Structured dtypes are packed when promoted (we consider the packed
  1321. # form to be "canonical"), so tere is no extra padding.
  1322. dtype = np.dtype(dtype_dict, align=align)
  1323. # Remove non "canonical" dtype options:
  1324. dtype_dict.pop("itemsize", None)
  1325. dtype_dict.pop("offsets", None)
  1326. expected = np.dtype(dtype_dict, align=align)
  1327. res = np.promote_types(dtype, dtype)
  1328. assert res.itemsize == expected.itemsize
  1329. assert res.fields == expected.fields
  1330. # But the "expected" one, should just be returned unchanged:
  1331. res = np.promote_types(expected, expected)
  1332. assert res is expected
  1333. def test_structured_asarray_is_view(self):
  1334. # A scalar viewing an array preserves its view even when creating a
  1335. # new array. This test documents behaviour, it may not be the best
  1336. # desired behaviour.
  1337. arr = np.array([1], dtype="i,i")
  1338. scalar = arr[0]
  1339. assert not scalar.flags.owndata # view into the array
  1340. assert np.asarray(scalar).base is scalar
  1341. # But never when a dtype is passed in:
  1342. assert np.asarray(scalar, dtype=scalar.dtype).base is None
  1343. # A scalar which owns its data does not have this property.
  1344. # It is not easy to create one, one method is to use pickle:
  1345. scalar = pickle.loads(pickle.dumps(scalar))
  1346. assert scalar.flags.owndata
  1347. assert np.asarray(scalar).base is None
  1348. class TestBool:
  1349. def test_test_interning(self):
  1350. a0 = np.bool_(0)
  1351. b0 = np.bool_(False)
  1352. assert_(a0 is b0)
  1353. a1 = np.bool_(1)
  1354. b1 = np.bool_(True)
  1355. assert_(a1 is b1)
  1356. assert_(np.array([True])[0] is a1)
  1357. assert_(np.array(True)[()] is a1)
  1358. def test_sum(self):
  1359. d = np.ones(101, dtype=bool)
  1360. assert_equal(d.sum(), d.size)
  1361. assert_equal(d[::2].sum(), d[::2].size)
  1362. assert_equal(d[::-2].sum(), d[::-2].size)
  1363. d = np.frombuffer(b'\xff\xff' * 100, dtype=bool)
  1364. assert_equal(d.sum(), d.size)
  1365. assert_equal(d[::2].sum(), d[::2].size)
  1366. assert_equal(d[::-2].sum(), d[::-2].size)
  1367. def check_count_nonzero(self, power, length):
  1368. powers = [2 ** i for i in range(length)]
  1369. for i in range(2**power):
  1370. l = [(i & x) != 0 for x in powers]
  1371. a = np.array(l, dtype=bool)
  1372. c = builtins.sum(l)
  1373. assert_equal(np.count_nonzero(a), c)
  1374. av = a.view(np.uint8)
  1375. av *= 3
  1376. assert_equal(np.count_nonzero(a), c)
  1377. av *= 4
  1378. assert_equal(np.count_nonzero(a), c)
  1379. av[av != 0] = 0xFF
  1380. assert_equal(np.count_nonzero(a), c)
  1381. def test_count_nonzero(self):
  1382. # check all 12 bit combinations in a length 17 array
  1383. # covers most cases of the 16 byte unrolled code
  1384. self.check_count_nonzero(12, 17)
  1385. @pytest.mark.slow
  1386. def test_count_nonzero_all(self):
  1387. # check all combinations in a length 17 array
  1388. # covers all cases of the 16 byte unrolled code
  1389. self.check_count_nonzero(17, 17)
  1390. def test_count_nonzero_unaligned(self):
  1391. # prevent mistakes as e.g. gh-4060
  1392. for o in range(7):
  1393. a = np.zeros((18,), dtype=bool)[o+1:]
  1394. a[:o] = True
  1395. assert_equal(np.count_nonzero(a), builtins.sum(a.tolist()))
  1396. a = np.ones((18,), dtype=bool)[o+1:]
  1397. a[:o] = False
  1398. assert_equal(np.count_nonzero(a), builtins.sum(a.tolist()))
  1399. def _test_cast_from_flexible(self, dtype):
  1400. # empty string -> false
  1401. for n in range(3):
  1402. v = np.array(b'', (dtype, n))
  1403. assert_equal(bool(v), False)
  1404. assert_equal(bool(v[()]), False)
  1405. assert_equal(v.astype(bool), False)
  1406. assert_(isinstance(v.astype(bool), np.ndarray))
  1407. assert_(v[()].astype(bool) is np.False_)
  1408. # anything else -> true
  1409. for n in range(1, 4):
  1410. for val in [b'a', b'0', b' ']:
  1411. v = np.array(val, (dtype, n))
  1412. assert_equal(bool(v), True)
  1413. assert_equal(bool(v[()]), True)
  1414. assert_equal(v.astype(bool), True)
  1415. assert_(isinstance(v.astype(bool), np.ndarray))
  1416. assert_(v[()].astype(bool) is np.True_)
  1417. def test_cast_from_void(self):
  1418. self._test_cast_from_flexible(np.void)
  1419. @pytest.mark.xfail(reason="See gh-9847")
  1420. def test_cast_from_unicode(self):
  1421. self._test_cast_from_flexible(np.unicode_)
  1422. @pytest.mark.xfail(reason="See gh-9847")
  1423. def test_cast_from_bytes(self):
  1424. self._test_cast_from_flexible(np.bytes_)
  1425. class TestZeroSizeFlexible:
  1426. @staticmethod
  1427. def _zeros(shape, dtype=str):
  1428. dtype = np.dtype(dtype)
  1429. if dtype == np.void:
  1430. return np.zeros(shape, dtype=(dtype, 0))
  1431. # not constructable directly
  1432. dtype = np.dtype([('x', dtype, 0)])
  1433. return np.zeros(shape, dtype=dtype)['x']
  1434. def test_create(self):
  1435. zs = self._zeros(10, bytes)
  1436. assert_equal(zs.itemsize, 0)
  1437. zs = self._zeros(10, np.void)
  1438. assert_equal(zs.itemsize, 0)
  1439. zs = self._zeros(10, str)
  1440. assert_equal(zs.itemsize, 0)
  1441. def _test_sort_partition(self, name, kinds, **kwargs):
  1442. # Previously, these would all hang
  1443. for dt in [bytes, np.void, str]:
  1444. zs = self._zeros(10, dt)
  1445. sort_method = getattr(zs, name)
  1446. sort_func = getattr(np, name)
  1447. for kind in kinds:
  1448. sort_method(kind=kind, **kwargs)
  1449. sort_func(zs, kind=kind, **kwargs)
  1450. def test_sort(self):
  1451. self._test_sort_partition('sort', kinds='qhs')
  1452. def test_argsort(self):
  1453. self._test_sort_partition('argsort', kinds='qhs')
  1454. def test_partition(self):
  1455. self._test_sort_partition('partition', kinds=['introselect'], kth=2)
  1456. def test_argpartition(self):
  1457. self._test_sort_partition('argpartition', kinds=['introselect'], kth=2)
  1458. def test_resize(self):
  1459. # previously an error
  1460. for dt in [bytes, np.void, str]:
  1461. zs = self._zeros(10, dt)
  1462. zs.resize(25)
  1463. zs.resize((10, 10))
  1464. def test_view(self):
  1465. for dt in [bytes, np.void, str]:
  1466. zs = self._zeros(10, dt)
  1467. # viewing as itself should be allowed
  1468. assert_equal(zs.view(dt).dtype, np.dtype(dt))
  1469. # viewing as any non-empty type gives an empty result
  1470. assert_equal(zs.view((dt, 1)).shape, (0,))
  1471. def test_dumps(self):
  1472. zs = self._zeros(10, int)
  1473. assert_equal(zs, pickle.loads(zs.dumps()))
  1474. def test_pickle(self):
  1475. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  1476. for dt in [bytes, np.void, str]:
  1477. zs = self._zeros(10, dt)
  1478. p = pickle.dumps(zs, protocol=proto)
  1479. zs2 = pickle.loads(p)
  1480. assert_equal(zs.dtype, zs2.dtype)
  1481. def test_pickle_empty(self):
  1482. """Checking if an empty array pickled and un-pickled will not cause a
  1483. segmentation fault"""
  1484. arr = np.array([]).reshape(999999, 0)
  1485. pk_dmp = pickle.dumps(arr)
  1486. pk_load = pickle.loads(pk_dmp)
  1487. assert pk_load.size == 0
  1488. @pytest.mark.skipif(pickle.HIGHEST_PROTOCOL < 5,
  1489. reason="requires pickle protocol 5")
  1490. def test_pickle_with_buffercallback(self):
  1491. array = np.arange(10)
  1492. buffers = []
  1493. bytes_string = pickle.dumps(array, buffer_callback=buffers.append,
  1494. protocol=5)
  1495. array_from_buffer = pickle.loads(bytes_string, buffers=buffers)
  1496. # when using pickle protocol 5 with buffer callbacks,
  1497. # array_from_buffer is reconstructed from a buffer holding a view
  1498. # to the initial array's data, so modifying an element in array
  1499. # should modify it in array_from_buffer too.
  1500. array[0] = -1
  1501. assert array_from_buffer[0] == -1, array_from_buffer[0]
  1502. class TestMethods:
  1503. sort_kinds = ['quicksort', 'heapsort', 'stable']
  1504. def test_all_where(self):
  1505. a = np.array([[True, False, True],
  1506. [False, False, False],
  1507. [True, True, True]])
  1508. wh_full = np.array([[True, False, True],
  1509. [False, False, False],
  1510. [True, False, True]])
  1511. wh_lower = np.array([[False],
  1512. [False],
  1513. [True]])
  1514. for _ax in [0, None]:
  1515. assert_equal(a.all(axis=_ax, where=wh_lower),
  1516. np.all(a[wh_lower[:,0],:], axis=_ax))
  1517. assert_equal(np.all(a, axis=_ax, where=wh_lower),
  1518. a[wh_lower[:,0],:].all(axis=_ax))
  1519. assert_equal(a.all(where=wh_full), True)
  1520. assert_equal(np.all(a, where=wh_full), True)
  1521. assert_equal(a.all(where=False), True)
  1522. assert_equal(np.all(a, where=False), True)
  1523. def test_any_where(self):
  1524. a = np.array([[True, False, True],
  1525. [False, False, False],
  1526. [True, True, True]])
  1527. wh_full = np.array([[False, True, False],
  1528. [True, True, True],
  1529. [False, False, False]])
  1530. wh_middle = np.array([[False],
  1531. [True],
  1532. [False]])
  1533. for _ax in [0, None]:
  1534. assert_equal(a.any(axis=_ax, where=wh_middle),
  1535. np.any(a[wh_middle[:,0],:], axis=_ax))
  1536. assert_equal(np.any(a, axis=_ax, where=wh_middle),
  1537. a[wh_middle[:,0],:].any(axis=_ax))
  1538. assert_equal(a.any(where=wh_full), False)
  1539. assert_equal(np.any(a, where=wh_full), False)
  1540. assert_equal(a.any(where=False), False)
  1541. assert_equal(np.any(a, where=False), False)
  1542. def test_compress(self):
  1543. tgt = [[5, 6, 7, 8, 9]]
  1544. arr = np.arange(10).reshape(2, 5)
  1545. out = arr.compress([0, 1], axis=0)
  1546. assert_equal(out, tgt)
  1547. tgt = [[1, 3], [6, 8]]
  1548. out = arr.compress([0, 1, 0, 1, 0], axis=1)
  1549. assert_equal(out, tgt)
  1550. tgt = [[1], [6]]
  1551. arr = np.arange(10).reshape(2, 5)
  1552. out = arr.compress([0, 1], axis=1)
  1553. assert_equal(out, tgt)
  1554. arr = np.arange(10).reshape(2, 5)
  1555. out = arr.compress([0, 1])
  1556. assert_equal(out, 1)
  1557. def test_choose(self):
  1558. x = 2*np.ones((3,), dtype=int)
  1559. y = 3*np.ones((3,), dtype=int)
  1560. x2 = 2*np.ones((2, 3), dtype=int)
  1561. y2 = 3*np.ones((2, 3), dtype=int)
  1562. ind = np.array([0, 0, 1])
  1563. A = ind.choose((x, y))
  1564. assert_equal(A, [2, 2, 3])
  1565. A = ind.choose((x2, y2))
  1566. assert_equal(A, [[2, 2, 3], [2, 2, 3]])
  1567. A = ind.choose((x, y2))
  1568. assert_equal(A, [[2, 2, 3], [2, 2, 3]])
  1569. oned = np.ones(1)
  1570. # gh-12031, caused SEGFAULT
  1571. assert_raises(TypeError, oned.choose,np.void(0), [oned])
  1572. out = np.array(0)
  1573. ret = np.choose(np.array(1), [10, 20, 30], out=out)
  1574. assert out is ret
  1575. assert_equal(out[()], 20)
  1576. # gh-6272 check overlap on out
  1577. x = np.arange(5)
  1578. y = np.choose([0,0,0], [x[:3], x[:3], x[:3]], out=x[1:4], mode='wrap')
  1579. assert_equal(y, np.array([0, 1, 2]))
  1580. def test_prod(self):
  1581. ba = [1, 2, 10, 11, 6, 5, 4]
  1582. ba2 = [[1, 2, 3, 4], [5, 6, 7, 9], [10, 3, 4, 5]]
  1583. for ctype in [np.int16, np.uint16, np.int32, np.uint32,
  1584. np.float32, np.float64, np.complex64, np.complex128]:
  1585. a = np.array(ba, ctype)
  1586. a2 = np.array(ba2, ctype)
  1587. if ctype in ['1', 'b']:
  1588. assert_raises(ArithmeticError, a.prod)
  1589. assert_raises(ArithmeticError, a2.prod, axis=1)
  1590. else:
  1591. assert_equal(a.prod(axis=0), 26400)
  1592. assert_array_equal(a2.prod(axis=0),
  1593. np.array([50, 36, 84, 180], ctype))
  1594. assert_array_equal(a2.prod(axis=-1),
  1595. np.array([24, 1890, 600], ctype))
  1596. def test_repeat(self):
  1597. m = np.array([1, 2, 3, 4, 5, 6])
  1598. m_rect = m.reshape((2, 3))
  1599. A = m.repeat([1, 3, 2, 1, 1, 2])
  1600. assert_equal(A, [1, 2, 2, 2, 3,
  1601. 3, 4, 5, 6, 6])
  1602. A = m.repeat(2)
  1603. assert_equal(A, [1, 1, 2, 2, 3, 3,
  1604. 4, 4, 5, 5, 6, 6])
  1605. A = m_rect.repeat([2, 1], axis=0)
  1606. assert_equal(A, [[1, 2, 3],
  1607. [1, 2, 3],
  1608. [4, 5, 6]])
  1609. A = m_rect.repeat([1, 3, 2], axis=1)
  1610. assert_equal(A, [[1, 2, 2, 2, 3, 3],
  1611. [4, 5, 5, 5, 6, 6]])
  1612. A = m_rect.repeat(2, axis=0)
  1613. assert_equal(A, [[1, 2, 3],
  1614. [1, 2, 3],
  1615. [4, 5, 6],
  1616. [4, 5, 6]])
  1617. A = m_rect.repeat(2, axis=1)
  1618. assert_equal(A, [[1, 1, 2, 2, 3, 3],
  1619. [4, 4, 5, 5, 6, 6]])
  1620. def test_reshape(self):
  1621. arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
  1622. tgt = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]
  1623. assert_equal(arr.reshape(2, 6), tgt)
  1624. tgt = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
  1625. assert_equal(arr.reshape(3, 4), tgt)
  1626. tgt = [[1, 10, 8, 6], [4, 2, 11, 9], [7, 5, 3, 12]]
  1627. assert_equal(arr.reshape((3, 4), order='F'), tgt)
  1628. tgt = [[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]
  1629. assert_equal(arr.T.reshape((3, 4), order='C'), tgt)
  1630. def test_round(self):
  1631. def check_round(arr, expected, *round_args):
  1632. assert_equal(arr.round(*round_args), expected)
  1633. # With output array
  1634. out = np.zeros_like(arr)
  1635. res = arr.round(*round_args, out=out)
  1636. assert_equal(out, expected)
  1637. assert out is res
  1638. check_round(np.array([1.2, 1.5]), [1, 2])
  1639. check_round(np.array(1.5), 2)
  1640. check_round(np.array([12.2, 15.5]), [10, 20], -1)
  1641. check_round(np.array([12.15, 15.51]), [12.2, 15.5], 1)
  1642. # Complex rounding
  1643. check_round(np.array([4.5 + 1.5j]), [4 + 2j])
  1644. check_round(np.array([12.5 + 15.5j]), [10 + 20j], -1)
  1645. def test_squeeze(self):
  1646. a = np.array([[[1], [2], [3]]])
  1647. assert_equal(a.squeeze(), [1, 2, 3])
  1648. assert_equal(a.squeeze(axis=(0,)), [[1], [2], [3]])
  1649. assert_raises(ValueError, a.squeeze, axis=(1,))
  1650. assert_equal(a.squeeze(axis=(2,)), [[1, 2, 3]])
  1651. def test_transpose(self):
  1652. a = np.array([[1, 2], [3, 4]])
  1653. assert_equal(a.transpose(), [[1, 3], [2, 4]])
  1654. assert_raises(ValueError, lambda: a.transpose(0))
  1655. assert_raises(ValueError, lambda: a.transpose(0, 0))
  1656. assert_raises(ValueError, lambda: a.transpose(0, 1, 2))
  1657. def test_sort(self):
  1658. # test ordering for floats and complex containing nans. It is only
  1659. # necessary to check the less-than comparison, so sorts that
  1660. # only follow the insertion sort path are sufficient. We only
  1661. # test doubles and complex doubles as the logic is the same.
  1662. # check doubles
  1663. msg = "Test real sort order with nans"
  1664. a = np.array([np.nan, 1, 0])
  1665. b = np.sort(a)
  1666. assert_equal(b, a[::-1], msg)
  1667. # check complex
  1668. msg = "Test complex sort order with nans"
  1669. a = np.zeros(9, dtype=np.complex128)
  1670. a.real += [np.nan, np.nan, np.nan, 1, 0, 1, 1, 0, 0]
  1671. a.imag += [np.nan, 1, 0, np.nan, np.nan, 1, 0, 1, 0]
  1672. b = np.sort(a)
  1673. assert_equal(b, a[::-1], msg)
  1674. # all c scalar sorts use the same code with different types
  1675. # so it suffices to run a quick check with one type. The number
  1676. # of sorted items must be greater than ~50 to check the actual
  1677. # algorithm because quick and merge sort fall over to insertion
  1678. # sort for small arrays.
  1679. @pytest.mark.parametrize('dtype', [np.uint8, np.uint16, np.uint32, np.uint64,
  1680. np.float16, np.float32, np.float64,
  1681. np.longdouble])
  1682. def test_sort_unsigned(self, dtype):
  1683. a = np.arange(101, dtype=dtype)
  1684. b = a[::-1].copy()
  1685. for kind in self.sort_kinds:
  1686. msg = "scalar sort, kind=%s" % kind
  1687. c = a.copy()
  1688. c.sort(kind=kind)
  1689. assert_equal(c, a, msg)
  1690. c = b.copy()
  1691. c.sort(kind=kind)
  1692. assert_equal(c, a, msg)
  1693. @pytest.mark.parametrize('dtype',
  1694. [np.int8, np.int16, np.int32, np.int64, np.float16,
  1695. np.float32, np.float64, np.longdouble])
  1696. def test_sort_signed(self, dtype):
  1697. a = np.arange(-50, 51, dtype=dtype)
  1698. b = a[::-1].copy()
  1699. for kind in self.sort_kinds:
  1700. msg = "scalar sort, kind=%s" % (kind)
  1701. c = a.copy()
  1702. c.sort(kind=kind)
  1703. assert_equal(c, a, msg)
  1704. c = b.copy()
  1705. c.sort(kind=kind)
  1706. assert_equal(c, a, msg)
  1707. @pytest.mark.parametrize('dtype', [np.float32, np.float64, np.longdouble])
  1708. @pytest.mark.parametrize('part', ['real', 'imag'])
  1709. def test_sort_complex(self, part, dtype):
  1710. # test complex sorts. These use the same code as the scalars
  1711. # but the compare function differs.
  1712. cdtype = {
  1713. np.single: np.csingle,
  1714. np.double: np.cdouble,
  1715. np.longdouble: np.clongdouble,
  1716. }[dtype]
  1717. a = np.arange(-50, 51, dtype=dtype)
  1718. b = a[::-1].copy()
  1719. ai = (a * (1+1j)).astype(cdtype)
  1720. bi = (b * (1+1j)).astype(cdtype)
  1721. setattr(ai, part, 1)
  1722. setattr(bi, part, 1)
  1723. for kind in self.sort_kinds:
  1724. msg = "complex sort, %s part == 1, kind=%s" % (part, kind)
  1725. c = ai.copy()
  1726. c.sort(kind=kind)
  1727. assert_equal(c, ai, msg)
  1728. c = bi.copy()
  1729. c.sort(kind=kind)
  1730. assert_equal(c, ai, msg)
  1731. def test_sort_complex_byte_swapping(self):
  1732. # test sorting of complex arrays requiring byte-swapping, gh-5441
  1733. for endianness in '<>':
  1734. for dt in np.typecodes['Complex']:
  1735. arr = np.array([1+3.j, 2+2.j, 3+1.j], dtype=endianness + dt)
  1736. c = arr.copy()
  1737. c.sort()
  1738. msg = 'byte-swapped complex sort, dtype={0}'.format(dt)
  1739. assert_equal(c, arr, msg)
  1740. @pytest.mark.parametrize('dtype', [np.bytes_, np.unicode_])
  1741. def test_sort_string(self, dtype):
  1742. # np.array will perform the encoding to bytes for us in the bytes test
  1743. a = np.array(['aaaaaaaa' + chr(i) for i in range(101)], dtype=dtype)
  1744. b = a[::-1].copy()
  1745. for kind in self.sort_kinds:
  1746. msg = "kind=%s" % kind
  1747. c = a.copy()
  1748. c.sort(kind=kind)
  1749. assert_equal(c, a, msg)
  1750. c = b.copy()
  1751. c.sort(kind=kind)
  1752. assert_equal(c, a, msg)
  1753. def test_sort_object(self):
  1754. # test object array sorts.
  1755. a = np.empty((101,), dtype=object)
  1756. a[:] = list(range(101))
  1757. b = a[::-1]
  1758. for kind in ['q', 'h', 'm']:
  1759. msg = "kind=%s" % kind
  1760. c = a.copy()
  1761. c.sort(kind=kind)
  1762. assert_equal(c, a, msg)
  1763. c = b.copy()
  1764. c.sort(kind=kind)
  1765. assert_equal(c, a, msg)
  1766. def test_sort_structured(self):
  1767. # test record array sorts.
  1768. dt = np.dtype([('f', float), ('i', int)])
  1769. a = np.array([(i, i) for i in range(101)], dtype=dt)
  1770. b = a[::-1]
  1771. for kind in ['q', 'h', 'm']:
  1772. msg = "kind=%s" % kind
  1773. c = a.copy()
  1774. c.sort(kind=kind)
  1775. assert_equal(c, a, msg)
  1776. c = b.copy()
  1777. c.sort(kind=kind)
  1778. assert_equal(c, a, msg)
  1779. @pytest.mark.parametrize('dtype', ['datetime64[D]', 'timedelta64[D]'])
  1780. def test_sort_time(self, dtype):
  1781. # test datetime64 and timedelta64 sorts.
  1782. a = np.arange(0, 101, dtype=dtype)
  1783. b = a[::-1]
  1784. for kind in ['q', 'h', 'm']:
  1785. msg = "kind=%s" % kind
  1786. c = a.copy()
  1787. c.sort(kind=kind)
  1788. assert_equal(c, a, msg)
  1789. c = b.copy()
  1790. c.sort(kind=kind)
  1791. assert_equal(c, a, msg)
  1792. def test_sort_axis(self):
  1793. # check axis handling. This should be the same for all type
  1794. # specific sorts, so we only check it for one type and one kind
  1795. a = np.array([[3, 2], [1, 0]])
  1796. b = np.array([[1, 0], [3, 2]])
  1797. c = np.array([[2, 3], [0, 1]])
  1798. d = a.copy()
  1799. d.sort(axis=0)
  1800. assert_equal(d, b, "test sort with axis=0")
  1801. d = a.copy()
  1802. d.sort(axis=1)
  1803. assert_equal(d, c, "test sort with axis=1")
  1804. d = a.copy()
  1805. d.sort()
  1806. assert_equal(d, c, "test sort with default axis")
  1807. def test_sort_size_0(self):
  1808. # check axis handling for multidimensional empty arrays
  1809. a = np.array([])
  1810. a.shape = (3, 2, 1, 0)
  1811. for axis in range(-a.ndim, a.ndim):
  1812. msg = 'test empty array sort with axis={0}'.format(axis)
  1813. assert_equal(np.sort(a, axis=axis), a, msg)
  1814. msg = 'test empty array sort with axis=None'
  1815. assert_equal(np.sort(a, axis=None), a.ravel(), msg)
  1816. def test_sort_bad_ordering(self):
  1817. # test generic class with bogus ordering,
  1818. # should not segfault.
  1819. class Boom:
  1820. def __lt__(self, other):
  1821. return True
  1822. a = np.array([Boom()] * 100, dtype=object)
  1823. for kind in self.sort_kinds:
  1824. msg = "kind=%s" % kind
  1825. c = a.copy()
  1826. c.sort(kind=kind)
  1827. assert_equal(c, a, msg)
  1828. def test_void_sort(self):
  1829. # gh-8210 - previously segfaulted
  1830. for i in range(4):
  1831. rand = np.random.randint(256, size=4000, dtype=np.uint8)
  1832. arr = rand.view('V4')
  1833. arr[::-1].sort()
  1834. dt = np.dtype([('val', 'i4', (1,))])
  1835. for i in range(4):
  1836. rand = np.random.randint(256, size=4000, dtype=np.uint8)
  1837. arr = rand.view(dt)
  1838. arr[::-1].sort()
  1839. def test_sort_raises(self):
  1840. #gh-9404
  1841. arr = np.array([0, datetime.now(), 1], dtype=object)
  1842. for kind in self.sort_kinds:
  1843. assert_raises(TypeError, arr.sort, kind=kind)
  1844. #gh-3879
  1845. class Raiser:
  1846. def raises_anything(*args, **kwargs):
  1847. raise TypeError("SOMETHING ERRORED")
  1848. __eq__ = __ne__ = __lt__ = __gt__ = __ge__ = __le__ = raises_anything
  1849. arr = np.array([[Raiser(), n] for n in range(10)]).reshape(-1)
  1850. np.random.shuffle(arr)
  1851. for kind in self.sort_kinds:
  1852. assert_raises(TypeError, arr.sort, kind=kind)
  1853. def test_sort_degraded(self):
  1854. # test degraded dataset would take minutes to run with normal qsort
  1855. d = np.arange(1000000)
  1856. do = d.copy()
  1857. x = d
  1858. # create a median of 3 killer where each median is the sorted second
  1859. # last element of the quicksort partition
  1860. while x.size > 3:
  1861. mid = x.size // 2
  1862. x[mid], x[-2] = x[-2], x[mid]
  1863. x = x[:-2]
  1864. assert_equal(np.sort(d), do)
  1865. assert_equal(d[np.argsort(d)], do)
  1866. def test_copy(self):
  1867. def assert_fortran(arr):
  1868. assert_(arr.flags.fortran)
  1869. assert_(arr.flags.f_contiguous)
  1870. assert_(not arr.flags.c_contiguous)
  1871. def assert_c(arr):
  1872. assert_(not arr.flags.fortran)
  1873. assert_(not arr.flags.f_contiguous)
  1874. assert_(arr.flags.c_contiguous)
  1875. a = np.empty((2, 2), order='F')
  1876. # Test copying a Fortran array
  1877. assert_c(a.copy())
  1878. assert_c(a.copy('C'))
  1879. assert_fortran(a.copy('F'))
  1880. assert_fortran(a.copy('A'))
  1881. # Now test starting with a C array.
  1882. a = np.empty((2, 2), order='C')
  1883. assert_c(a.copy())
  1884. assert_c(a.copy('C'))
  1885. assert_fortran(a.copy('F'))
  1886. assert_c(a.copy('A'))
  1887. @pytest.mark.parametrize("dtype", ['O', np.int32, 'i,O'])
  1888. def test__deepcopy__(self, dtype):
  1889. # Force the entry of NULLs into array
  1890. a = np.empty(4, dtype=dtype)
  1891. ctypes.memset(a.ctypes.data, 0, a.nbytes)
  1892. # Ensure no error is raised, see gh-21833
  1893. b = a.__deepcopy__({})
  1894. a[0] = 42
  1895. with pytest.raises(AssertionError):
  1896. assert_array_equal(a, b)
  1897. def test__deepcopy__catches_failure(self):
  1898. class MyObj:
  1899. def __deepcopy__(self, *args, **kwargs):
  1900. raise RuntimeError
  1901. arr = np.array([1, MyObj(), 3], dtype='O')
  1902. with pytest.raises(RuntimeError):
  1903. arr.__deepcopy__({})
  1904. def test_sort_order(self):
  1905. # Test sorting an array with fields
  1906. x1 = np.array([21, 32, 14])
  1907. x2 = np.array(['my', 'first', 'name'])
  1908. x3 = np.array([3.1, 4.5, 6.2])
  1909. r = np.rec.fromarrays([x1, x2, x3], names='id,word,number')
  1910. r.sort(order=['id'])
  1911. assert_equal(r.id, np.array([14, 21, 32]))
  1912. assert_equal(r.word, np.array(['name', 'my', 'first']))
  1913. assert_equal(r.number, np.array([6.2, 3.1, 4.5]))
  1914. r.sort(order=['word'])
  1915. assert_equal(r.id, np.array([32, 21, 14]))
  1916. assert_equal(r.word, np.array(['first', 'my', 'name']))
  1917. assert_equal(r.number, np.array([4.5, 3.1, 6.2]))
  1918. r.sort(order=['number'])
  1919. assert_equal(r.id, np.array([21, 32, 14]))
  1920. assert_equal(r.word, np.array(['my', 'first', 'name']))
  1921. assert_equal(r.number, np.array([3.1, 4.5, 6.2]))
  1922. assert_raises_regex(ValueError, 'duplicate',
  1923. lambda: r.sort(order=['id', 'id']))
  1924. if sys.byteorder == 'little':
  1925. strtype = '>i2'
  1926. else:
  1927. strtype = '<i2'
  1928. mydtype = [('name', 'U5'), ('col2', strtype)]
  1929. r = np.array([('a', 1), ('b', 255), ('c', 3), ('d', 258)],
  1930. dtype=mydtype)
  1931. r.sort(order='col2')
  1932. assert_equal(r['col2'], [1, 3, 255, 258])
  1933. assert_equal(r, np.array([('a', 1), ('c', 3), ('b', 255), ('d', 258)],
  1934. dtype=mydtype))
  1935. def test_argsort(self):
  1936. # all c scalar argsorts use the same code with different types
  1937. # so it suffices to run a quick check with one type. The number
  1938. # of sorted items must be greater than ~50 to check the actual
  1939. # algorithm because quick and merge sort fall over to insertion
  1940. # sort for small arrays.
  1941. for dtype in [np.int32, np.uint32, np.float32]:
  1942. a = np.arange(101, dtype=dtype)
  1943. b = a[::-1].copy()
  1944. for kind in self.sort_kinds:
  1945. msg = "scalar argsort, kind=%s, dtype=%s" % (kind, dtype)
  1946. assert_equal(a.copy().argsort(kind=kind), a, msg)
  1947. assert_equal(b.copy().argsort(kind=kind), b, msg)
  1948. # test complex argsorts. These use the same code as the scalars
  1949. # but the compare function differs.
  1950. ai = a*1j + 1
  1951. bi = b*1j + 1
  1952. for kind in self.sort_kinds:
  1953. msg = "complex argsort, kind=%s" % kind
  1954. assert_equal(ai.copy().argsort(kind=kind), a, msg)
  1955. assert_equal(bi.copy().argsort(kind=kind), b, msg)
  1956. ai = a + 1j
  1957. bi = b + 1j
  1958. for kind in self.sort_kinds:
  1959. msg = "complex argsort, kind=%s" % kind
  1960. assert_equal(ai.copy().argsort(kind=kind), a, msg)
  1961. assert_equal(bi.copy().argsort(kind=kind), b, msg)
  1962. # test argsort of complex arrays requiring byte-swapping, gh-5441
  1963. for endianness in '<>':
  1964. for dt in np.typecodes['Complex']:
  1965. arr = np.array([1+3.j, 2+2.j, 3+1.j], dtype=endianness + dt)
  1966. msg = 'byte-swapped complex argsort, dtype={0}'.format(dt)
  1967. assert_equal(arr.argsort(),
  1968. np.arange(len(arr), dtype=np.intp), msg)
  1969. # test string argsorts.
  1970. s = 'aaaaaaaa'
  1971. a = np.array([s + chr(i) for i in range(101)])
  1972. b = a[::-1].copy()
  1973. r = np.arange(101)
  1974. rr = r[::-1]
  1975. for kind in self.sort_kinds:
  1976. msg = "string argsort, kind=%s" % kind
  1977. assert_equal(a.copy().argsort(kind=kind), r, msg)
  1978. assert_equal(b.copy().argsort(kind=kind), rr, msg)
  1979. # test unicode argsorts.
  1980. s = 'aaaaaaaa'
  1981. a = np.array([s + chr(i) for i in range(101)], dtype=np.unicode_)
  1982. b = a[::-1]
  1983. r = np.arange(101)
  1984. rr = r[::-1]
  1985. for kind in self.sort_kinds:
  1986. msg = "unicode argsort, kind=%s" % kind
  1987. assert_equal(a.copy().argsort(kind=kind), r, msg)
  1988. assert_equal(b.copy().argsort(kind=kind), rr, msg)
  1989. # test object array argsorts.
  1990. a = np.empty((101,), dtype=object)
  1991. a[:] = list(range(101))
  1992. b = a[::-1]
  1993. r = np.arange(101)
  1994. rr = r[::-1]
  1995. for kind in self.sort_kinds:
  1996. msg = "object argsort, kind=%s" % kind
  1997. assert_equal(a.copy().argsort(kind=kind), r, msg)
  1998. assert_equal(b.copy().argsort(kind=kind), rr, msg)
  1999. # test structured array argsorts.
  2000. dt = np.dtype([('f', float), ('i', int)])
  2001. a = np.array([(i, i) for i in range(101)], dtype=dt)
  2002. b = a[::-1]
  2003. r = np.arange(101)
  2004. rr = r[::-1]
  2005. for kind in self.sort_kinds:
  2006. msg = "structured array argsort, kind=%s" % kind
  2007. assert_equal(a.copy().argsort(kind=kind), r, msg)
  2008. assert_equal(b.copy().argsort(kind=kind), rr, msg)
  2009. # test datetime64 argsorts.
  2010. a = np.arange(0, 101, dtype='datetime64[D]')
  2011. b = a[::-1]
  2012. r = np.arange(101)
  2013. rr = r[::-1]
  2014. for kind in ['q', 'h', 'm']:
  2015. msg = "datetime64 argsort, kind=%s" % kind
  2016. assert_equal(a.copy().argsort(kind=kind), r, msg)
  2017. assert_equal(b.copy().argsort(kind=kind), rr, msg)
  2018. # test timedelta64 argsorts.
  2019. a = np.arange(0, 101, dtype='timedelta64[D]')
  2020. b = a[::-1]
  2021. r = np.arange(101)
  2022. rr = r[::-1]
  2023. for kind in ['q', 'h', 'm']:
  2024. msg = "timedelta64 argsort, kind=%s" % kind
  2025. assert_equal(a.copy().argsort(kind=kind), r, msg)
  2026. assert_equal(b.copy().argsort(kind=kind), rr, msg)
  2027. # check axis handling. This should be the same for all type
  2028. # specific argsorts, so we only check it for one type and one kind
  2029. a = np.array([[3, 2], [1, 0]])
  2030. b = np.array([[1, 1], [0, 0]])
  2031. c = np.array([[1, 0], [1, 0]])
  2032. assert_equal(a.copy().argsort(axis=0), b)
  2033. assert_equal(a.copy().argsort(axis=1), c)
  2034. assert_equal(a.copy().argsort(), c)
  2035. # check axis handling for multidimensional empty arrays
  2036. a = np.array([])
  2037. a.shape = (3, 2, 1, 0)
  2038. for axis in range(-a.ndim, a.ndim):
  2039. msg = 'test empty array argsort with axis={0}'.format(axis)
  2040. assert_equal(np.argsort(a, axis=axis),
  2041. np.zeros_like(a, dtype=np.intp), msg)
  2042. msg = 'test empty array argsort with axis=None'
  2043. assert_equal(np.argsort(a, axis=None),
  2044. np.zeros_like(a.ravel(), dtype=np.intp), msg)
  2045. # check that stable argsorts are stable
  2046. r = np.arange(100)
  2047. # scalars
  2048. a = np.zeros(100)
  2049. assert_equal(a.argsort(kind='m'), r)
  2050. # complex
  2051. a = np.zeros(100, dtype=complex)
  2052. assert_equal(a.argsort(kind='m'), r)
  2053. # string
  2054. a = np.array(['aaaaaaaaa' for i in range(100)])
  2055. assert_equal(a.argsort(kind='m'), r)
  2056. # unicode
  2057. a = np.array(['aaaaaaaaa' for i in range(100)], dtype=np.unicode_)
  2058. assert_equal(a.argsort(kind='m'), r)
  2059. def test_sort_unicode_kind(self):
  2060. d = np.arange(10)
  2061. k = b'\xc3\xa4'.decode("UTF8")
  2062. assert_raises(ValueError, d.sort, kind=k)
  2063. assert_raises(ValueError, d.argsort, kind=k)
  2064. @pytest.mark.parametrize('a', [
  2065. np.array([0, 1, np.nan], dtype=np.float16),
  2066. np.array([0, 1, np.nan], dtype=np.float32),
  2067. np.array([0, 1, np.nan]),
  2068. ])
  2069. def test_searchsorted_floats(self, a):
  2070. # test for floats arrays containing nans. Explicitly test
  2071. # half, single, and double precision floats to verify that
  2072. # the NaN-handling is correct.
  2073. msg = "Test real (%s) searchsorted with nans, side='l'" % a.dtype
  2074. b = a.searchsorted(a, side='left')
  2075. assert_equal(b, np.arange(3), msg)
  2076. msg = "Test real (%s) searchsorted with nans, side='r'" % a.dtype
  2077. b = a.searchsorted(a, side='right')
  2078. assert_equal(b, np.arange(1, 4), msg)
  2079. # check keyword arguments
  2080. a.searchsorted(v=1)
  2081. x = np.array([0, 1, np.nan], dtype='float32')
  2082. y = np.searchsorted(x, x[-1])
  2083. assert_equal(y, 2)
  2084. def test_searchsorted_complex(self):
  2085. # test for complex arrays containing nans.
  2086. # The search sorted routines use the compare functions for the
  2087. # array type, so this checks if that is consistent with the sort
  2088. # order.
  2089. # check double complex
  2090. a = np.zeros(9, dtype=np.complex128)
  2091. a.real += [0, 0, 1, 1, 0, 1, np.nan, np.nan, np.nan]
  2092. a.imag += [0, 1, 0, 1, np.nan, np.nan, 0, 1, np.nan]
  2093. msg = "Test complex searchsorted with nans, side='l'"
  2094. b = a.searchsorted(a, side='left')
  2095. assert_equal(b, np.arange(9), msg)
  2096. msg = "Test complex searchsorted with nans, side='r'"
  2097. b = a.searchsorted(a, side='right')
  2098. assert_equal(b, np.arange(1, 10), msg)
  2099. msg = "Test searchsorted with little endian, side='l'"
  2100. a = np.array([0, 128], dtype='<i4')
  2101. b = a.searchsorted(np.array(128, dtype='<i4'))
  2102. assert_equal(b, 1, msg)
  2103. msg = "Test searchsorted with big endian, side='l'"
  2104. a = np.array([0, 128], dtype='>i4')
  2105. b = a.searchsorted(np.array(128, dtype='>i4'))
  2106. assert_equal(b, 1, msg)
  2107. def test_searchsorted_n_elements(self):
  2108. # Check 0 elements
  2109. a = np.ones(0)
  2110. b = a.searchsorted([0, 1, 2], 'left')
  2111. assert_equal(b, [0, 0, 0])
  2112. b = a.searchsorted([0, 1, 2], 'right')
  2113. assert_equal(b, [0, 0, 0])
  2114. a = np.ones(1)
  2115. # Check 1 element
  2116. b = a.searchsorted([0, 1, 2], 'left')
  2117. assert_equal(b, [0, 0, 1])
  2118. b = a.searchsorted([0, 1, 2], 'right')
  2119. assert_equal(b, [0, 1, 1])
  2120. # Check all elements equal
  2121. a = np.ones(2)
  2122. b = a.searchsorted([0, 1, 2], 'left')
  2123. assert_equal(b, [0, 0, 2])
  2124. b = a.searchsorted([0, 1, 2], 'right')
  2125. assert_equal(b, [0, 2, 2])
  2126. def test_searchsorted_unaligned_array(self):
  2127. # Test searching unaligned array
  2128. a = np.arange(10)
  2129. aligned = np.empty(a.itemsize * a.size + 1, 'uint8')
  2130. unaligned = aligned[1:].view(a.dtype)
  2131. unaligned[:] = a
  2132. # Test searching unaligned array
  2133. b = unaligned.searchsorted(a, 'left')
  2134. assert_equal(b, a)
  2135. b = unaligned.searchsorted(a, 'right')
  2136. assert_equal(b, a + 1)
  2137. # Test searching for unaligned keys
  2138. b = a.searchsorted(unaligned, 'left')
  2139. assert_equal(b, a)
  2140. b = a.searchsorted(unaligned, 'right')
  2141. assert_equal(b, a + 1)
  2142. def test_searchsorted_resetting(self):
  2143. # Test smart resetting of binsearch indices
  2144. a = np.arange(5)
  2145. b = a.searchsorted([6, 5, 4], 'left')
  2146. assert_equal(b, [5, 5, 4])
  2147. b = a.searchsorted([6, 5, 4], 'right')
  2148. assert_equal(b, [5, 5, 5])
  2149. def test_searchsorted_type_specific(self):
  2150. # Test all type specific binary search functions
  2151. types = ''.join((np.typecodes['AllInteger'], np.typecodes['AllFloat'],
  2152. np.typecodes['Datetime'], '?O'))
  2153. for dt in types:
  2154. if dt == 'M':
  2155. dt = 'M8[D]'
  2156. if dt == '?':
  2157. a = np.arange(2, dtype=dt)
  2158. out = np.arange(2)
  2159. else:
  2160. a = np.arange(0, 5, dtype=dt)
  2161. out = np.arange(5)
  2162. b = a.searchsorted(a, 'left')
  2163. assert_equal(b, out)
  2164. b = a.searchsorted(a, 'right')
  2165. assert_equal(b, out + 1)
  2166. # Test empty array, use a fresh array to get warnings in
  2167. # valgrind if access happens.
  2168. e = np.ndarray(shape=0, buffer=b'', dtype=dt)
  2169. b = e.searchsorted(a, 'left')
  2170. assert_array_equal(b, np.zeros(len(a), dtype=np.intp))
  2171. b = a.searchsorted(e, 'left')
  2172. assert_array_equal(b, np.zeros(0, dtype=np.intp))
  2173. def test_searchsorted_unicode(self):
  2174. # Test searchsorted on unicode strings.
  2175. # 1.6.1 contained a string length miscalculation in
  2176. # arraytypes.c.src:UNICODE_compare() which manifested as
  2177. # incorrect/inconsistent results from searchsorted.
  2178. a = np.array(['P:\\20x_dapi_cy3\\20x_dapi_cy3_20100185_1',
  2179. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100186_1',
  2180. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100187_1',
  2181. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100189_1',
  2182. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100190_1',
  2183. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100191_1',
  2184. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100192_1',
  2185. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100193_1',
  2186. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100194_1',
  2187. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100195_1',
  2188. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100196_1',
  2189. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100197_1',
  2190. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100198_1',
  2191. 'P:\\20x_dapi_cy3\\20x_dapi_cy3_20100199_1'],
  2192. dtype=np.unicode_)
  2193. ind = np.arange(len(a))
  2194. assert_equal([a.searchsorted(v, 'left') for v in a], ind)
  2195. assert_equal([a.searchsorted(v, 'right') for v in a], ind + 1)
  2196. assert_equal([a.searchsorted(a[i], 'left') for i in ind], ind)
  2197. assert_equal([a.searchsorted(a[i], 'right') for i in ind], ind + 1)
  2198. def test_searchsorted_with_invalid_sorter(self):
  2199. a = np.array([5, 2, 1, 3, 4])
  2200. s = np.argsort(a)
  2201. assert_raises(TypeError, np.searchsorted, a, 0,
  2202. sorter=np.array((1, (2, 3)), dtype=object))
  2203. assert_raises(TypeError, np.searchsorted, a, 0, sorter=[1.1])
  2204. assert_raises(ValueError, np.searchsorted, a, 0, sorter=[1, 2, 3, 4])
  2205. assert_raises(ValueError, np.searchsorted, a, 0, sorter=[1, 2, 3, 4, 5, 6])
  2206. # bounds check
  2207. assert_raises(ValueError, np.searchsorted, a, 4, sorter=[0, 1, 2, 3, 5])
  2208. assert_raises(ValueError, np.searchsorted, a, 0, sorter=[-1, 0, 1, 2, 3])
  2209. assert_raises(ValueError, np.searchsorted, a, 0, sorter=[4, 0, -1, 2, 3])
  2210. def test_searchsorted_with_sorter(self):
  2211. a = np.random.rand(300)
  2212. s = a.argsort()
  2213. b = np.sort(a)
  2214. k = np.linspace(0, 1, 20)
  2215. assert_equal(b.searchsorted(k), a.searchsorted(k, sorter=s))
  2216. a = np.array([0, 1, 2, 3, 5]*20)
  2217. s = a.argsort()
  2218. k = [0, 1, 2, 3, 5]
  2219. expected = [0, 20, 40, 60, 80]
  2220. assert_equal(a.searchsorted(k, side='left', sorter=s), expected)
  2221. expected = [20, 40, 60, 80, 100]
  2222. assert_equal(a.searchsorted(k, side='right', sorter=s), expected)
  2223. # Test searching unaligned array
  2224. keys = np.arange(10)
  2225. a = keys.copy()
  2226. np.random.shuffle(s)
  2227. s = a.argsort()
  2228. aligned = np.empty(a.itemsize * a.size + 1, 'uint8')
  2229. unaligned = aligned[1:].view(a.dtype)
  2230. # Test searching unaligned array
  2231. unaligned[:] = a
  2232. b = unaligned.searchsorted(keys, 'left', s)
  2233. assert_equal(b, keys)
  2234. b = unaligned.searchsorted(keys, 'right', s)
  2235. assert_equal(b, keys + 1)
  2236. # Test searching for unaligned keys
  2237. unaligned[:] = keys
  2238. b = a.searchsorted(unaligned, 'left', s)
  2239. assert_equal(b, keys)
  2240. b = a.searchsorted(unaligned, 'right', s)
  2241. assert_equal(b, keys + 1)
  2242. # Test all type specific indirect binary search functions
  2243. types = ''.join((np.typecodes['AllInteger'], np.typecodes['AllFloat'],
  2244. np.typecodes['Datetime'], '?O'))
  2245. for dt in types:
  2246. if dt == 'M':
  2247. dt = 'M8[D]'
  2248. if dt == '?':
  2249. a = np.array([1, 0], dtype=dt)
  2250. # We want the sorter array to be of a type that is different
  2251. # from np.intp in all platforms, to check for #4698
  2252. s = np.array([1, 0], dtype=np.int16)
  2253. out = np.array([1, 0])
  2254. else:
  2255. a = np.array([3, 4, 1, 2, 0], dtype=dt)
  2256. # We want the sorter array to be of a type that is different
  2257. # from np.intp in all platforms, to check for #4698
  2258. s = np.array([4, 2, 3, 0, 1], dtype=np.int16)
  2259. out = np.array([3, 4, 1, 2, 0], dtype=np.intp)
  2260. b = a.searchsorted(a, 'left', s)
  2261. assert_equal(b, out)
  2262. b = a.searchsorted(a, 'right', s)
  2263. assert_equal(b, out + 1)
  2264. # Test empty array, use a fresh array to get warnings in
  2265. # valgrind if access happens.
  2266. e = np.ndarray(shape=0, buffer=b'', dtype=dt)
  2267. b = e.searchsorted(a, 'left', s[:0])
  2268. assert_array_equal(b, np.zeros(len(a), dtype=np.intp))
  2269. b = a.searchsorted(e, 'left', s)
  2270. assert_array_equal(b, np.zeros(0, dtype=np.intp))
  2271. # Test non-contiguous sorter array
  2272. a = np.array([3, 4, 1, 2, 0])
  2273. srt = np.empty((10,), dtype=np.intp)
  2274. srt[1::2] = -1
  2275. srt[::2] = [4, 2, 3, 0, 1]
  2276. s = srt[::2]
  2277. out = np.array([3, 4, 1, 2, 0], dtype=np.intp)
  2278. b = a.searchsorted(a, 'left', s)
  2279. assert_equal(b, out)
  2280. b = a.searchsorted(a, 'right', s)
  2281. assert_equal(b, out + 1)
  2282. def test_searchsorted_return_type(self):
  2283. # Functions returning indices should always return base ndarrays
  2284. class A(np.ndarray):
  2285. pass
  2286. a = np.arange(5).view(A)
  2287. b = np.arange(1, 3).view(A)
  2288. s = np.arange(5).view(A)
  2289. assert_(not isinstance(a.searchsorted(b, 'left'), A))
  2290. assert_(not isinstance(a.searchsorted(b, 'right'), A))
  2291. assert_(not isinstance(a.searchsorted(b, 'left', s), A))
  2292. assert_(not isinstance(a.searchsorted(b, 'right', s), A))
  2293. @pytest.mark.parametrize("dtype", np.typecodes["All"])
  2294. def test_argpartition_out_of_range(self, dtype):
  2295. # Test out of range values in kth raise an error, gh-5469
  2296. d = np.arange(10).astype(dtype=dtype)
  2297. assert_raises(ValueError, d.argpartition, 10)
  2298. assert_raises(ValueError, d.argpartition, -11)
  2299. @pytest.mark.parametrize("dtype", np.typecodes["All"])
  2300. def test_partition_out_of_range(self, dtype):
  2301. # Test out of range values in kth raise an error, gh-5469
  2302. d = np.arange(10).astype(dtype=dtype)
  2303. assert_raises(ValueError, d.partition, 10)
  2304. assert_raises(ValueError, d.partition, -11)
  2305. def test_argpartition_integer(self):
  2306. # Test non-integer values in kth raise an error/
  2307. d = np.arange(10)
  2308. assert_raises(TypeError, d.argpartition, 9.)
  2309. # Test also for generic type argpartition, which uses sorting
  2310. # and used to not bound check kth
  2311. d_obj = np.arange(10, dtype=object)
  2312. assert_raises(TypeError, d_obj.argpartition, 9.)
  2313. def test_partition_integer(self):
  2314. # Test out of range values in kth raise an error, gh-5469
  2315. d = np.arange(10)
  2316. assert_raises(TypeError, d.partition, 9.)
  2317. # Test also for generic type partition, which uses sorting
  2318. # and used to not bound check kth
  2319. d_obj = np.arange(10, dtype=object)
  2320. assert_raises(TypeError, d_obj.partition, 9.)
  2321. @pytest.mark.parametrize("kth_dtype", np.typecodes["AllInteger"])
  2322. def test_partition_empty_array(self, kth_dtype):
  2323. # check axis handling for multidimensional empty arrays
  2324. kth = np.array(0, dtype=kth_dtype)[()]
  2325. a = np.array([])
  2326. a.shape = (3, 2, 1, 0)
  2327. for axis in range(-a.ndim, a.ndim):
  2328. msg = 'test empty array partition with axis={0}'.format(axis)
  2329. assert_equal(np.partition(a, kth, axis=axis), a, msg)
  2330. msg = 'test empty array partition with axis=None'
  2331. assert_equal(np.partition(a, kth, axis=None), a.ravel(), msg)
  2332. @pytest.mark.parametrize("kth_dtype", np.typecodes["AllInteger"])
  2333. def test_argpartition_empty_array(self, kth_dtype):
  2334. # check axis handling for multidimensional empty arrays
  2335. kth = np.array(0, dtype=kth_dtype)[()]
  2336. a = np.array([])
  2337. a.shape = (3, 2, 1, 0)
  2338. for axis in range(-a.ndim, a.ndim):
  2339. msg = 'test empty array argpartition with axis={0}'.format(axis)
  2340. assert_equal(np.partition(a, kth, axis=axis),
  2341. np.zeros_like(a, dtype=np.intp), msg)
  2342. msg = 'test empty array argpartition with axis=None'
  2343. assert_equal(np.partition(a, kth, axis=None),
  2344. np.zeros_like(a.ravel(), dtype=np.intp), msg)
  2345. def test_partition(self):
  2346. d = np.arange(10)
  2347. assert_raises(TypeError, np.partition, d, 2, kind=1)
  2348. assert_raises(ValueError, np.partition, d, 2, kind="nonsense")
  2349. assert_raises(ValueError, np.argpartition, d, 2, kind="nonsense")
  2350. assert_raises(ValueError, d.partition, 2, axis=0, kind="nonsense")
  2351. assert_raises(ValueError, d.argpartition, 2, axis=0, kind="nonsense")
  2352. for k in ("introselect",):
  2353. d = np.array([])
  2354. assert_array_equal(np.partition(d, 0, kind=k), d)
  2355. assert_array_equal(np.argpartition(d, 0, kind=k), d)
  2356. d = np.ones(1)
  2357. assert_array_equal(np.partition(d, 0, kind=k)[0], d)
  2358. assert_array_equal(d[np.argpartition(d, 0, kind=k)],
  2359. np.partition(d, 0, kind=k))
  2360. # kth not modified
  2361. kth = np.array([30, 15, 5])
  2362. okth = kth.copy()
  2363. np.partition(np.arange(40), kth)
  2364. assert_array_equal(kth, okth)
  2365. for r in ([2, 1], [1, 2], [1, 1]):
  2366. d = np.array(r)
  2367. tgt = np.sort(d)
  2368. assert_array_equal(np.partition(d, 0, kind=k)[0], tgt[0])
  2369. assert_array_equal(np.partition(d, 1, kind=k)[1], tgt[1])
  2370. assert_array_equal(d[np.argpartition(d, 0, kind=k)],
  2371. np.partition(d, 0, kind=k))
  2372. assert_array_equal(d[np.argpartition(d, 1, kind=k)],
  2373. np.partition(d, 1, kind=k))
  2374. for i in range(d.size):
  2375. d[i:].partition(0, kind=k)
  2376. assert_array_equal(d, tgt)
  2377. for r in ([3, 2, 1], [1, 2, 3], [2, 1, 3], [2, 3, 1],
  2378. [1, 1, 1], [1, 2, 2], [2, 2, 1], [1, 2, 1]):
  2379. d = np.array(r)
  2380. tgt = np.sort(d)
  2381. assert_array_equal(np.partition(d, 0, kind=k)[0], tgt[0])
  2382. assert_array_equal(np.partition(d, 1, kind=k)[1], tgt[1])
  2383. assert_array_equal(np.partition(d, 2, kind=k)[2], tgt[2])
  2384. assert_array_equal(d[np.argpartition(d, 0, kind=k)],
  2385. np.partition(d, 0, kind=k))
  2386. assert_array_equal(d[np.argpartition(d, 1, kind=k)],
  2387. np.partition(d, 1, kind=k))
  2388. assert_array_equal(d[np.argpartition(d, 2, kind=k)],
  2389. np.partition(d, 2, kind=k))
  2390. for i in range(d.size):
  2391. d[i:].partition(0, kind=k)
  2392. assert_array_equal(d, tgt)
  2393. d = np.ones(50)
  2394. assert_array_equal(np.partition(d, 0, kind=k), d)
  2395. assert_array_equal(d[np.argpartition(d, 0, kind=k)],
  2396. np.partition(d, 0, kind=k))
  2397. # sorted
  2398. d = np.arange(49)
  2399. assert_equal(np.partition(d, 5, kind=k)[5], 5)
  2400. assert_equal(np.partition(d, 15, kind=k)[15], 15)
  2401. assert_array_equal(d[np.argpartition(d, 5, kind=k)],
  2402. np.partition(d, 5, kind=k))
  2403. assert_array_equal(d[np.argpartition(d, 15, kind=k)],
  2404. np.partition(d, 15, kind=k))
  2405. # rsorted
  2406. d = np.arange(47)[::-1]
  2407. assert_equal(np.partition(d, 6, kind=k)[6], 6)
  2408. assert_equal(np.partition(d, 16, kind=k)[16], 16)
  2409. assert_array_equal(d[np.argpartition(d, 6, kind=k)],
  2410. np.partition(d, 6, kind=k))
  2411. assert_array_equal(d[np.argpartition(d, 16, kind=k)],
  2412. np.partition(d, 16, kind=k))
  2413. assert_array_equal(np.partition(d, -6, kind=k),
  2414. np.partition(d, 41, kind=k))
  2415. assert_array_equal(np.partition(d, -16, kind=k),
  2416. np.partition(d, 31, kind=k))
  2417. assert_array_equal(d[np.argpartition(d, -6, kind=k)],
  2418. np.partition(d, 41, kind=k))
  2419. # median of 3 killer, O(n^2) on pure median 3 pivot quickselect
  2420. # exercises the median of median of 5 code used to keep O(n)
  2421. d = np.arange(1000000)
  2422. x = np.roll(d, d.size // 2)
  2423. mid = x.size // 2 + 1
  2424. assert_equal(np.partition(x, mid)[mid], mid)
  2425. d = np.arange(1000001)
  2426. x = np.roll(d, d.size // 2 + 1)
  2427. mid = x.size // 2 + 1
  2428. assert_equal(np.partition(x, mid)[mid], mid)
  2429. # max
  2430. d = np.ones(10)
  2431. d[1] = 4
  2432. assert_equal(np.partition(d, (2, -1))[-1], 4)
  2433. assert_equal(np.partition(d, (2, -1))[2], 1)
  2434. assert_equal(d[np.argpartition(d, (2, -1))][-1], 4)
  2435. assert_equal(d[np.argpartition(d, (2, -1))][2], 1)
  2436. d[1] = np.nan
  2437. assert_(np.isnan(d[np.argpartition(d, (2, -1))][-1]))
  2438. assert_(np.isnan(np.partition(d, (2, -1))[-1]))
  2439. # equal elements
  2440. d = np.arange(47) % 7
  2441. tgt = np.sort(np.arange(47) % 7)
  2442. np.random.shuffle(d)
  2443. for i in range(d.size):
  2444. assert_equal(np.partition(d, i, kind=k)[i], tgt[i])
  2445. assert_array_equal(d[np.argpartition(d, 6, kind=k)],
  2446. np.partition(d, 6, kind=k))
  2447. assert_array_equal(d[np.argpartition(d, 16, kind=k)],
  2448. np.partition(d, 16, kind=k))
  2449. for i in range(d.size):
  2450. d[i:].partition(0, kind=k)
  2451. assert_array_equal(d, tgt)
  2452. d = np.array([0, 1, 2, 3, 4, 5, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  2453. 7, 7, 7, 7, 7, 9])
  2454. kth = [0, 3, 19, 20]
  2455. assert_equal(np.partition(d, kth, kind=k)[kth], (0, 3, 7, 7))
  2456. assert_equal(d[np.argpartition(d, kth, kind=k)][kth], (0, 3, 7, 7))
  2457. d = np.array([2, 1])
  2458. d.partition(0, kind=k)
  2459. assert_raises(ValueError, d.partition, 2)
  2460. assert_raises(np.AxisError, d.partition, 3, axis=1)
  2461. assert_raises(ValueError, np.partition, d, 2)
  2462. assert_raises(np.AxisError, np.partition, d, 2, axis=1)
  2463. assert_raises(ValueError, d.argpartition, 2)
  2464. assert_raises(np.AxisError, d.argpartition, 3, axis=1)
  2465. assert_raises(ValueError, np.argpartition, d, 2)
  2466. assert_raises(np.AxisError, np.argpartition, d, 2, axis=1)
  2467. d = np.arange(10).reshape((2, 5))
  2468. d.partition(1, axis=0, kind=k)
  2469. d.partition(4, axis=1, kind=k)
  2470. np.partition(d, 1, axis=0, kind=k)
  2471. np.partition(d, 4, axis=1, kind=k)
  2472. np.partition(d, 1, axis=None, kind=k)
  2473. np.partition(d, 9, axis=None, kind=k)
  2474. d.argpartition(1, axis=0, kind=k)
  2475. d.argpartition(4, axis=1, kind=k)
  2476. np.argpartition(d, 1, axis=0, kind=k)
  2477. np.argpartition(d, 4, axis=1, kind=k)
  2478. np.argpartition(d, 1, axis=None, kind=k)
  2479. np.argpartition(d, 9, axis=None, kind=k)
  2480. assert_raises(ValueError, d.partition, 2, axis=0)
  2481. assert_raises(ValueError, d.partition, 11, axis=1)
  2482. assert_raises(TypeError, d.partition, 2, axis=None)
  2483. assert_raises(ValueError, np.partition, d, 9, axis=1)
  2484. assert_raises(ValueError, np.partition, d, 11, axis=None)
  2485. assert_raises(ValueError, d.argpartition, 2, axis=0)
  2486. assert_raises(ValueError, d.argpartition, 11, axis=1)
  2487. assert_raises(ValueError, np.argpartition, d, 9, axis=1)
  2488. assert_raises(ValueError, np.argpartition, d, 11, axis=None)
  2489. td = [(dt, s) for dt in [np.int32, np.float32, np.complex64]
  2490. for s in (9, 16)]
  2491. for dt, s in td:
  2492. aae = assert_array_equal
  2493. at = assert_
  2494. d = np.arange(s, dtype=dt)
  2495. np.random.shuffle(d)
  2496. d1 = np.tile(np.arange(s, dtype=dt), (4, 1))
  2497. map(np.random.shuffle, d1)
  2498. d0 = np.transpose(d1)
  2499. for i in range(d.size):
  2500. p = np.partition(d, i, kind=k)
  2501. assert_equal(p[i], i)
  2502. # all before are smaller
  2503. assert_array_less(p[:i], p[i])
  2504. # all after are larger
  2505. assert_array_less(p[i], p[i + 1:])
  2506. aae(p, d[np.argpartition(d, i, kind=k)])
  2507. p = np.partition(d1, i, axis=1, kind=k)
  2508. aae(p[:, i], np.array([i] * d1.shape[0], dtype=dt))
  2509. # array_less does not seem to work right
  2510. at((p[:, :i].T <= p[:, i]).all(),
  2511. msg="%d: %r <= %r" % (i, p[:, i], p[:, :i].T))
  2512. at((p[:, i + 1:].T > p[:, i]).all(),
  2513. msg="%d: %r < %r" % (i, p[:, i], p[:, i + 1:].T))
  2514. aae(p, d1[np.arange(d1.shape[0])[:, None],
  2515. np.argpartition(d1, i, axis=1, kind=k)])
  2516. p = np.partition(d0, i, axis=0, kind=k)
  2517. aae(p[i, :], np.array([i] * d1.shape[0], dtype=dt))
  2518. # array_less does not seem to work right
  2519. at((p[:i, :] <= p[i, :]).all(),
  2520. msg="%d: %r <= %r" % (i, p[i, :], p[:i, :]))
  2521. at((p[i + 1:, :] > p[i, :]).all(),
  2522. msg="%d: %r < %r" % (i, p[i, :], p[:, i + 1:]))
  2523. aae(p, d0[np.argpartition(d0, i, axis=0, kind=k),
  2524. np.arange(d0.shape[1])[None, :]])
  2525. # check inplace
  2526. dc = d.copy()
  2527. dc.partition(i, kind=k)
  2528. assert_equal(dc, np.partition(d, i, kind=k))
  2529. dc = d0.copy()
  2530. dc.partition(i, axis=0, kind=k)
  2531. assert_equal(dc, np.partition(d0, i, axis=0, kind=k))
  2532. dc = d1.copy()
  2533. dc.partition(i, axis=1, kind=k)
  2534. assert_equal(dc, np.partition(d1, i, axis=1, kind=k))
  2535. def assert_partitioned(self, d, kth):
  2536. prev = 0
  2537. for k in np.sort(kth):
  2538. assert_array_less(d[prev:k], d[k], err_msg='kth %d' % k)
  2539. assert_((d[k:] >= d[k]).all(),
  2540. msg="kth %d, %r not greater equal %d" % (k, d[k:], d[k]))
  2541. prev = k + 1
  2542. def test_partition_iterative(self):
  2543. d = np.arange(17)
  2544. kth = (0, 1, 2, 429, 231)
  2545. assert_raises(ValueError, d.partition, kth)
  2546. assert_raises(ValueError, d.argpartition, kth)
  2547. d = np.arange(10).reshape((2, 5))
  2548. assert_raises(ValueError, d.partition, kth, axis=0)
  2549. assert_raises(ValueError, d.partition, kth, axis=1)
  2550. assert_raises(ValueError, np.partition, d, kth, axis=1)
  2551. assert_raises(ValueError, np.partition, d, kth, axis=None)
  2552. d = np.array([3, 4, 2, 1])
  2553. p = np.partition(d, (0, 3))
  2554. self.assert_partitioned(p, (0, 3))
  2555. self.assert_partitioned(d[np.argpartition(d, (0, 3))], (0, 3))
  2556. assert_array_equal(p, np.partition(d, (-3, -1)))
  2557. assert_array_equal(p, d[np.argpartition(d, (-3, -1))])
  2558. d = np.arange(17)
  2559. np.random.shuffle(d)
  2560. d.partition(range(d.size))
  2561. assert_array_equal(np.arange(17), d)
  2562. np.random.shuffle(d)
  2563. assert_array_equal(np.arange(17), d[d.argpartition(range(d.size))])
  2564. # test unsorted kth
  2565. d = np.arange(17)
  2566. np.random.shuffle(d)
  2567. keys = np.array([1, 3, 8, -2])
  2568. np.random.shuffle(d)
  2569. p = np.partition(d, keys)
  2570. self.assert_partitioned(p, keys)
  2571. p = d[np.argpartition(d, keys)]
  2572. self.assert_partitioned(p, keys)
  2573. np.random.shuffle(keys)
  2574. assert_array_equal(np.partition(d, keys), p)
  2575. assert_array_equal(d[np.argpartition(d, keys)], p)
  2576. # equal kth
  2577. d = np.arange(20)[::-1]
  2578. self.assert_partitioned(np.partition(d, [5]*4), [5])
  2579. self.assert_partitioned(np.partition(d, [5]*4 + [6, 13]),
  2580. [5]*4 + [6, 13])
  2581. self.assert_partitioned(d[np.argpartition(d, [5]*4)], [5])
  2582. self.assert_partitioned(d[np.argpartition(d, [5]*4 + [6, 13])],
  2583. [5]*4 + [6, 13])
  2584. d = np.arange(12)
  2585. np.random.shuffle(d)
  2586. d1 = np.tile(np.arange(12), (4, 1))
  2587. map(np.random.shuffle, d1)
  2588. d0 = np.transpose(d1)
  2589. kth = (1, 6, 7, -1)
  2590. p = np.partition(d1, kth, axis=1)
  2591. pa = d1[np.arange(d1.shape[0])[:, None],
  2592. d1.argpartition(kth, axis=1)]
  2593. assert_array_equal(p, pa)
  2594. for i in range(d1.shape[0]):
  2595. self.assert_partitioned(p[i,:], kth)
  2596. p = np.partition(d0, kth, axis=0)
  2597. pa = d0[np.argpartition(d0, kth, axis=0),
  2598. np.arange(d0.shape[1])[None,:]]
  2599. assert_array_equal(p, pa)
  2600. for i in range(d0.shape[1]):
  2601. self.assert_partitioned(p[:, i], kth)
  2602. def test_partition_cdtype(self):
  2603. d = np.array([('Galahad', 1.7, 38), ('Arthur', 1.8, 41),
  2604. ('Lancelot', 1.9, 38)],
  2605. dtype=[('name', '|S10'), ('height', '<f8'), ('age', '<i4')])
  2606. tgt = np.sort(d, order=['age', 'height'])
  2607. assert_array_equal(np.partition(d, range(d.size),
  2608. order=['age', 'height']),
  2609. tgt)
  2610. assert_array_equal(d[np.argpartition(d, range(d.size),
  2611. order=['age', 'height'])],
  2612. tgt)
  2613. for k in range(d.size):
  2614. assert_equal(np.partition(d, k, order=['age', 'height'])[k],
  2615. tgt[k])
  2616. assert_equal(d[np.argpartition(d, k, order=['age', 'height'])][k],
  2617. tgt[k])
  2618. d = np.array(['Galahad', 'Arthur', 'zebra', 'Lancelot'])
  2619. tgt = np.sort(d)
  2620. assert_array_equal(np.partition(d, range(d.size)), tgt)
  2621. for k in range(d.size):
  2622. assert_equal(np.partition(d, k)[k], tgt[k])
  2623. assert_equal(d[np.argpartition(d, k)][k], tgt[k])
  2624. def test_partition_unicode_kind(self):
  2625. d = np.arange(10)
  2626. k = b'\xc3\xa4'.decode("UTF8")
  2627. assert_raises(ValueError, d.partition, 2, kind=k)
  2628. assert_raises(ValueError, d.argpartition, 2, kind=k)
  2629. def test_partition_fuzz(self):
  2630. # a few rounds of random data testing
  2631. for j in range(10, 30):
  2632. for i in range(1, j - 2):
  2633. d = np.arange(j)
  2634. np.random.shuffle(d)
  2635. d = d % np.random.randint(2, 30)
  2636. idx = np.random.randint(d.size)
  2637. kth = [0, idx, i, i + 1]
  2638. tgt = np.sort(d)[kth]
  2639. assert_array_equal(np.partition(d, kth)[kth], tgt,
  2640. err_msg="data: %r\n kth: %r" % (d, kth))
  2641. @pytest.mark.parametrize("kth_dtype", np.typecodes["AllInteger"])
  2642. def test_argpartition_gh5524(self, kth_dtype):
  2643. # A test for functionality of argpartition on lists.
  2644. kth = np.array(1, dtype=kth_dtype)[()]
  2645. d = [6, 7, 3, 2, 9, 0]
  2646. p = np.argpartition(d, kth)
  2647. self.assert_partitioned(np.array(d)[p],[1])
  2648. def test_flatten(self):
  2649. x0 = np.array([[1, 2, 3], [4, 5, 6]], np.int32)
  2650. x1 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], np.int32)
  2651. y0 = np.array([1, 2, 3, 4, 5, 6], np.int32)
  2652. y0f = np.array([1, 4, 2, 5, 3, 6], np.int32)
  2653. y1 = np.array([1, 2, 3, 4, 5, 6, 7, 8], np.int32)
  2654. y1f = np.array([1, 5, 3, 7, 2, 6, 4, 8], np.int32)
  2655. assert_equal(x0.flatten(), y0)
  2656. assert_equal(x0.flatten('F'), y0f)
  2657. assert_equal(x0.flatten('F'), x0.T.flatten())
  2658. assert_equal(x1.flatten(), y1)
  2659. assert_equal(x1.flatten('F'), y1f)
  2660. assert_equal(x1.flatten('F'), x1.T.flatten())
  2661. @pytest.mark.parametrize('func', (np.dot, np.matmul))
  2662. def test_arr_mult(self, func):
  2663. a = np.array([[1, 0], [0, 1]])
  2664. b = np.array([[0, 1], [1, 0]])
  2665. c = np.array([[9, 1], [1, -9]])
  2666. d = np.arange(24).reshape(4, 6)
  2667. ddt = np.array(
  2668. [[ 55, 145, 235, 325],
  2669. [ 145, 451, 757, 1063],
  2670. [ 235, 757, 1279, 1801],
  2671. [ 325, 1063, 1801, 2539]]
  2672. )
  2673. dtd = np.array(
  2674. [[504, 540, 576, 612, 648, 684],
  2675. [540, 580, 620, 660, 700, 740],
  2676. [576, 620, 664, 708, 752, 796],
  2677. [612, 660, 708, 756, 804, 852],
  2678. [648, 700, 752, 804, 856, 908],
  2679. [684, 740, 796, 852, 908, 964]]
  2680. )
  2681. # gemm vs syrk optimizations
  2682. for et in [np.float32, np.float64, np.complex64, np.complex128]:
  2683. eaf = a.astype(et)
  2684. assert_equal(func(eaf, eaf), eaf)
  2685. assert_equal(func(eaf.T, eaf), eaf)
  2686. assert_equal(func(eaf, eaf.T), eaf)
  2687. assert_equal(func(eaf.T, eaf.T), eaf)
  2688. assert_equal(func(eaf.T.copy(), eaf), eaf)
  2689. assert_equal(func(eaf, eaf.T.copy()), eaf)
  2690. assert_equal(func(eaf.T.copy(), eaf.T.copy()), eaf)
  2691. # syrk validations
  2692. for et in [np.float32, np.float64, np.complex64, np.complex128]:
  2693. eaf = a.astype(et)
  2694. ebf = b.astype(et)
  2695. assert_equal(func(ebf, ebf), eaf)
  2696. assert_equal(func(ebf.T, ebf), eaf)
  2697. assert_equal(func(ebf, ebf.T), eaf)
  2698. assert_equal(func(ebf.T, ebf.T), eaf)
  2699. # syrk - different shape, stride, and view validations
  2700. for et in [np.float32, np.float64, np.complex64, np.complex128]:
  2701. edf = d.astype(et)
  2702. assert_equal(
  2703. func(edf[::-1, :], edf.T),
  2704. func(edf[::-1, :].copy(), edf.T.copy())
  2705. )
  2706. assert_equal(
  2707. func(edf[:, ::-1], edf.T),
  2708. func(edf[:, ::-1].copy(), edf.T.copy())
  2709. )
  2710. assert_equal(
  2711. func(edf, edf[::-1, :].T),
  2712. func(edf, edf[::-1, :].T.copy())
  2713. )
  2714. assert_equal(
  2715. func(edf, edf[:, ::-1].T),
  2716. func(edf, edf[:, ::-1].T.copy())
  2717. )
  2718. assert_equal(
  2719. func(edf[:edf.shape[0] // 2, :], edf[::2, :].T),
  2720. func(edf[:edf.shape[0] // 2, :].copy(), edf[::2, :].T.copy())
  2721. )
  2722. assert_equal(
  2723. func(edf[::2, :], edf[:edf.shape[0] // 2, :].T),
  2724. func(edf[::2, :].copy(), edf[:edf.shape[0] // 2, :].T.copy())
  2725. )
  2726. # syrk - different shape
  2727. for et in [np.float32, np.float64, np.complex64, np.complex128]:
  2728. edf = d.astype(et)
  2729. eddtf = ddt.astype(et)
  2730. edtdf = dtd.astype(et)
  2731. assert_equal(func(edf, edf.T), eddtf)
  2732. assert_equal(func(edf.T, edf), edtdf)
  2733. @pytest.mark.parametrize('func', (np.dot, np.matmul))
  2734. @pytest.mark.parametrize('dtype', 'ifdFD')
  2735. def test_no_dgemv(self, func, dtype):
  2736. # check vector arg for contiguous before gemv
  2737. # gh-12156
  2738. a = np.arange(8.0, dtype=dtype).reshape(2, 4)
  2739. b = np.broadcast_to(1., (4, 1))
  2740. ret1 = func(a, b)
  2741. ret2 = func(a, b.copy())
  2742. assert_equal(ret1, ret2)
  2743. ret1 = func(b.T, a.T)
  2744. ret2 = func(b.T.copy(), a.T)
  2745. assert_equal(ret1, ret2)
  2746. # check for unaligned data
  2747. dt = np.dtype(dtype)
  2748. a = np.zeros(8 * dt.itemsize // 2 + 1, dtype='int16')[1:].view(dtype)
  2749. a = a.reshape(2, 4)
  2750. b = a[0]
  2751. # make sure it is not aligned
  2752. assert_(a.__array_interface__['data'][0] % dt.itemsize != 0)
  2753. ret1 = func(a, b)
  2754. ret2 = func(a.copy(), b.copy())
  2755. assert_equal(ret1, ret2)
  2756. ret1 = func(b.T, a.T)
  2757. ret2 = func(b.T.copy(), a.T.copy())
  2758. assert_equal(ret1, ret2)
  2759. def test_dot(self):
  2760. a = np.array([[1, 0], [0, 1]])
  2761. b = np.array([[0, 1], [1, 0]])
  2762. c = np.array([[9, 1], [1, -9]])
  2763. # function versus methods
  2764. assert_equal(np.dot(a, b), a.dot(b))
  2765. assert_equal(np.dot(np.dot(a, b), c), a.dot(b).dot(c))
  2766. # test passing in an output array
  2767. c = np.zeros_like(a)
  2768. a.dot(b, c)
  2769. assert_equal(c, np.dot(a, b))
  2770. # test keyword args
  2771. c = np.zeros_like(a)
  2772. a.dot(b=b, out=c)
  2773. assert_equal(c, np.dot(a, b))
  2774. def test_dot_type_mismatch(self):
  2775. c = 1.
  2776. A = np.array((1,1), dtype='i,i')
  2777. assert_raises(TypeError, np.dot, c, A)
  2778. assert_raises(TypeError, np.dot, A, c)
  2779. def test_dot_out_mem_overlap(self):
  2780. np.random.seed(1)
  2781. # Test BLAS and non-BLAS code paths, including all dtypes
  2782. # that dot() supports
  2783. dtypes = [np.dtype(code) for code in np.typecodes['All']
  2784. if code not in 'USVM']
  2785. for dtype in dtypes:
  2786. a = np.random.rand(3, 3).astype(dtype)
  2787. # Valid dot() output arrays must be aligned
  2788. b = _aligned_zeros((3, 3), dtype=dtype)
  2789. b[...] = np.random.rand(3, 3)
  2790. y = np.dot(a, b)
  2791. x = np.dot(a, b, out=b)
  2792. assert_equal(x, y, err_msg=repr(dtype))
  2793. # Check invalid output array
  2794. assert_raises(ValueError, np.dot, a, b, out=b[::2])
  2795. assert_raises(ValueError, np.dot, a, b, out=b.T)
  2796. def test_dot_matmul_out(self):
  2797. # gh-9641
  2798. class Sub(np.ndarray):
  2799. pass
  2800. a = np.ones((2, 2)).view(Sub)
  2801. b = np.ones((2, 2)).view(Sub)
  2802. out = np.ones((2, 2))
  2803. # make sure out can be any ndarray (not only subclass of inputs)
  2804. np.dot(a, b, out=out)
  2805. np.matmul(a, b, out=out)
  2806. def test_dot_matmul_inner_array_casting_fails(self):
  2807. class A:
  2808. def __array__(self, *args, **kwargs):
  2809. raise NotImplementedError
  2810. # Don't override the error from calling __array__()
  2811. assert_raises(NotImplementedError, np.dot, A(), A())
  2812. assert_raises(NotImplementedError, np.matmul, A(), A())
  2813. assert_raises(NotImplementedError, np.inner, A(), A())
  2814. def test_matmul_out(self):
  2815. # overlapping memory
  2816. a = np.arange(18).reshape(2, 3, 3)
  2817. b = np.matmul(a, a)
  2818. c = np.matmul(a, a, out=a)
  2819. assert_(c is a)
  2820. assert_equal(c, b)
  2821. a = np.arange(18).reshape(2, 3, 3)
  2822. c = np.matmul(a, a, out=a[::-1, ...])
  2823. assert_(c.base is a.base)
  2824. assert_equal(c, b)
  2825. def test_diagonal(self):
  2826. a = np.arange(12).reshape((3, 4))
  2827. assert_equal(a.diagonal(), [0, 5, 10])
  2828. assert_equal(a.diagonal(0), [0, 5, 10])
  2829. assert_equal(a.diagonal(1), [1, 6, 11])
  2830. assert_equal(a.diagonal(-1), [4, 9])
  2831. assert_raises(np.AxisError, a.diagonal, axis1=0, axis2=5)
  2832. assert_raises(np.AxisError, a.diagonal, axis1=5, axis2=0)
  2833. assert_raises(np.AxisError, a.diagonal, axis1=5, axis2=5)
  2834. assert_raises(ValueError, a.diagonal, axis1=1, axis2=1)
  2835. b = np.arange(8).reshape((2, 2, 2))
  2836. assert_equal(b.diagonal(), [[0, 6], [1, 7]])
  2837. assert_equal(b.diagonal(0), [[0, 6], [1, 7]])
  2838. assert_equal(b.diagonal(1), [[2], [3]])
  2839. assert_equal(b.diagonal(-1), [[4], [5]])
  2840. assert_raises(ValueError, b.diagonal, axis1=0, axis2=0)
  2841. assert_equal(b.diagonal(0, 1, 2), [[0, 3], [4, 7]])
  2842. assert_equal(b.diagonal(0, 0, 1), [[0, 6], [1, 7]])
  2843. assert_equal(b.diagonal(offset=1, axis1=0, axis2=2), [[1], [3]])
  2844. # Order of axis argument doesn't matter:
  2845. assert_equal(b.diagonal(0, 2, 1), [[0, 3], [4, 7]])
  2846. def test_diagonal_view_notwriteable(self):
  2847. a = np.eye(3).diagonal()
  2848. assert_(not a.flags.writeable)
  2849. assert_(not a.flags.owndata)
  2850. a = np.diagonal(np.eye(3))
  2851. assert_(not a.flags.writeable)
  2852. assert_(not a.flags.owndata)
  2853. a = np.diag(np.eye(3))
  2854. assert_(not a.flags.writeable)
  2855. assert_(not a.flags.owndata)
  2856. def test_diagonal_memleak(self):
  2857. # Regression test for a bug that crept in at one point
  2858. a = np.zeros((100, 100))
  2859. if HAS_REFCOUNT:
  2860. assert_(sys.getrefcount(a) < 50)
  2861. for i in range(100):
  2862. a.diagonal()
  2863. if HAS_REFCOUNT:
  2864. assert_(sys.getrefcount(a) < 50)
  2865. def test_size_zero_memleak(self):
  2866. # Regression test for issue 9615
  2867. # Exercises a special-case code path for dot products of length
  2868. # zero in cblasfuncs (making it is specific to floating dtypes).
  2869. a = np.array([], dtype=np.float64)
  2870. x = np.array(2.0)
  2871. for _ in range(100):
  2872. np.dot(a, a, out=x)
  2873. if HAS_REFCOUNT:
  2874. assert_(sys.getrefcount(x) < 50)
  2875. def test_trace(self):
  2876. a = np.arange(12).reshape((3, 4))
  2877. assert_equal(a.trace(), 15)
  2878. assert_equal(a.trace(0), 15)
  2879. assert_equal(a.trace(1), 18)
  2880. assert_equal(a.trace(-1), 13)
  2881. b = np.arange(8).reshape((2, 2, 2))
  2882. assert_equal(b.trace(), [6, 8])
  2883. assert_equal(b.trace(0), [6, 8])
  2884. assert_equal(b.trace(1), [2, 3])
  2885. assert_equal(b.trace(-1), [4, 5])
  2886. assert_equal(b.trace(0, 0, 1), [6, 8])
  2887. assert_equal(b.trace(0, 0, 2), [5, 9])
  2888. assert_equal(b.trace(0, 1, 2), [3, 11])
  2889. assert_equal(b.trace(offset=1, axis1=0, axis2=2), [1, 3])
  2890. out = np.array(1)
  2891. ret = a.trace(out=out)
  2892. assert ret is out
  2893. def test_trace_subclass(self):
  2894. # The class would need to overwrite trace to ensure single-element
  2895. # output also has the right subclass.
  2896. class MyArray(np.ndarray):
  2897. pass
  2898. b = np.arange(8).reshape((2, 2, 2)).view(MyArray)
  2899. t = b.trace()
  2900. assert_(isinstance(t, MyArray))
  2901. def test_put(self):
  2902. icodes = np.typecodes['AllInteger']
  2903. fcodes = np.typecodes['AllFloat']
  2904. for dt in icodes + fcodes + 'O':
  2905. tgt = np.array([0, 1, 0, 3, 0, 5], dtype=dt)
  2906. # test 1-d
  2907. a = np.zeros(6, dtype=dt)
  2908. a.put([1, 3, 5], [1, 3, 5])
  2909. assert_equal(a, tgt)
  2910. # test 2-d
  2911. a = np.zeros((2, 3), dtype=dt)
  2912. a.put([1, 3, 5], [1, 3, 5])
  2913. assert_equal(a, tgt.reshape(2, 3))
  2914. for dt in '?':
  2915. tgt = np.array([False, True, False, True, False, True], dtype=dt)
  2916. # test 1-d
  2917. a = np.zeros(6, dtype=dt)
  2918. a.put([1, 3, 5], [True]*3)
  2919. assert_equal(a, tgt)
  2920. # test 2-d
  2921. a = np.zeros((2, 3), dtype=dt)
  2922. a.put([1, 3, 5], [True]*3)
  2923. assert_equal(a, tgt.reshape(2, 3))
  2924. # check must be writeable
  2925. a = np.zeros(6)
  2926. a.flags.writeable = False
  2927. assert_raises(ValueError, a.put, [1, 3, 5], [1, 3, 5])
  2928. # when calling np.put, make sure a
  2929. # TypeError is raised if the object
  2930. # isn't an ndarray
  2931. bad_array = [1, 2, 3]
  2932. assert_raises(TypeError, np.put, bad_array, [0, 2], 5)
  2933. def test_ravel(self):
  2934. a = np.array([[0, 1], [2, 3]])
  2935. assert_equal(a.ravel(), [0, 1, 2, 3])
  2936. assert_(not a.ravel().flags.owndata)
  2937. assert_equal(a.ravel('F'), [0, 2, 1, 3])
  2938. assert_equal(a.ravel(order='C'), [0, 1, 2, 3])
  2939. assert_equal(a.ravel(order='F'), [0, 2, 1, 3])
  2940. assert_equal(a.ravel(order='A'), [0, 1, 2, 3])
  2941. assert_(not a.ravel(order='A').flags.owndata)
  2942. assert_equal(a.ravel(order='K'), [0, 1, 2, 3])
  2943. assert_(not a.ravel(order='K').flags.owndata)
  2944. assert_equal(a.ravel(), a.reshape(-1))
  2945. a = np.array([[0, 1], [2, 3]], order='F')
  2946. assert_equal(a.ravel(), [0, 1, 2, 3])
  2947. assert_equal(a.ravel(order='A'), [0, 2, 1, 3])
  2948. assert_equal(a.ravel(order='K'), [0, 2, 1, 3])
  2949. assert_(not a.ravel(order='A').flags.owndata)
  2950. assert_(not a.ravel(order='K').flags.owndata)
  2951. assert_equal(a.ravel(), a.reshape(-1))
  2952. assert_equal(a.ravel(order='A'), a.reshape(-1, order='A'))
  2953. a = np.array([[0, 1], [2, 3]])[::-1, :]
  2954. assert_equal(a.ravel(), [2, 3, 0, 1])
  2955. assert_equal(a.ravel(order='C'), [2, 3, 0, 1])
  2956. assert_equal(a.ravel(order='F'), [2, 0, 3, 1])
  2957. assert_equal(a.ravel(order='A'), [2, 3, 0, 1])
  2958. # 'K' doesn't reverse the axes of negative strides
  2959. assert_equal(a.ravel(order='K'), [2, 3, 0, 1])
  2960. assert_(a.ravel(order='K').flags.owndata)
  2961. # Test simple 1-d copy behaviour:
  2962. a = np.arange(10)[::2]
  2963. assert_(a.ravel('K').flags.owndata)
  2964. assert_(a.ravel('C').flags.owndata)
  2965. assert_(a.ravel('F').flags.owndata)
  2966. # Not contiguous and 1-sized axis with non matching stride
  2967. a = np.arange(2**3 * 2)[::2]
  2968. a = a.reshape(2, 1, 2, 2).swapaxes(-1, -2)
  2969. strides = list(a.strides)
  2970. strides[1] = 123
  2971. a.strides = strides
  2972. assert_(a.ravel(order='K').flags.owndata)
  2973. assert_equal(a.ravel('K'), np.arange(0, 15, 2))
  2974. # contiguous and 1-sized axis with non matching stride works:
  2975. a = np.arange(2**3)
  2976. a = a.reshape(2, 1, 2, 2).swapaxes(-1, -2)
  2977. strides = list(a.strides)
  2978. strides[1] = 123
  2979. a.strides = strides
  2980. assert_(np.may_share_memory(a.ravel(order='K'), a))
  2981. assert_equal(a.ravel(order='K'), np.arange(2**3))
  2982. # Test negative strides (not very interesting since non-contiguous):
  2983. a = np.arange(4)[::-1].reshape(2, 2)
  2984. assert_(a.ravel(order='C').flags.owndata)
  2985. assert_(a.ravel(order='K').flags.owndata)
  2986. assert_equal(a.ravel('C'), [3, 2, 1, 0])
  2987. assert_equal(a.ravel('K'), [3, 2, 1, 0])
  2988. # 1-element tidy strides test:
  2989. a = np.array([[1]])
  2990. a.strides = (123, 432)
  2991. # If the following stride is not 8, NPY_RELAXED_STRIDES_DEBUG is
  2992. # messing them up on purpose:
  2993. if np.ones(1).strides == (8,):
  2994. assert_(np.may_share_memory(a.ravel('K'), a))
  2995. assert_equal(a.ravel('K').strides, (a.dtype.itemsize,))
  2996. for order in ('C', 'F', 'A', 'K'):
  2997. # 0-d corner case:
  2998. a = np.array(0)
  2999. assert_equal(a.ravel(order), [0])
  3000. assert_(np.may_share_memory(a.ravel(order), a))
  3001. # Test that certain non-inplace ravels work right (mostly) for 'K':
  3002. b = np.arange(2**4 * 2)[::2].reshape(2, 2, 2, 2)
  3003. a = b[..., ::2]
  3004. assert_equal(a.ravel('K'), [0, 4, 8, 12, 16, 20, 24, 28])
  3005. assert_equal(a.ravel('C'), [0, 4, 8, 12, 16, 20, 24, 28])
  3006. assert_equal(a.ravel('A'), [0, 4, 8, 12, 16, 20, 24, 28])
  3007. assert_equal(a.ravel('F'), [0, 16, 8, 24, 4, 20, 12, 28])
  3008. a = b[::2, ...]
  3009. assert_equal(a.ravel('K'), [0, 2, 4, 6, 8, 10, 12, 14])
  3010. assert_equal(a.ravel('C'), [0, 2, 4, 6, 8, 10, 12, 14])
  3011. assert_equal(a.ravel('A'), [0, 2, 4, 6, 8, 10, 12, 14])
  3012. assert_equal(a.ravel('F'), [0, 8, 4, 12, 2, 10, 6, 14])
  3013. def test_ravel_subclass(self):
  3014. class ArraySubclass(np.ndarray):
  3015. pass
  3016. a = np.arange(10).view(ArraySubclass)
  3017. assert_(isinstance(a.ravel('C'), ArraySubclass))
  3018. assert_(isinstance(a.ravel('F'), ArraySubclass))
  3019. assert_(isinstance(a.ravel('A'), ArraySubclass))
  3020. assert_(isinstance(a.ravel('K'), ArraySubclass))
  3021. a = np.arange(10)[::2].view(ArraySubclass)
  3022. assert_(isinstance(a.ravel('C'), ArraySubclass))
  3023. assert_(isinstance(a.ravel('F'), ArraySubclass))
  3024. assert_(isinstance(a.ravel('A'), ArraySubclass))
  3025. assert_(isinstance(a.ravel('K'), ArraySubclass))
  3026. def test_swapaxes(self):
  3027. a = np.arange(1*2*3*4).reshape(1, 2, 3, 4).copy()
  3028. idx = np.indices(a.shape)
  3029. assert_(a.flags['OWNDATA'])
  3030. b = a.copy()
  3031. # check exceptions
  3032. assert_raises(np.AxisError, a.swapaxes, -5, 0)
  3033. assert_raises(np.AxisError, a.swapaxes, 4, 0)
  3034. assert_raises(np.AxisError, a.swapaxes, 0, -5)
  3035. assert_raises(np.AxisError, a.swapaxes, 0, 4)
  3036. for i in range(-4, 4):
  3037. for j in range(-4, 4):
  3038. for k, src in enumerate((a, b)):
  3039. c = src.swapaxes(i, j)
  3040. # check shape
  3041. shape = list(src.shape)
  3042. shape[i] = src.shape[j]
  3043. shape[j] = src.shape[i]
  3044. assert_equal(c.shape, shape, str((i, j, k)))
  3045. # check array contents
  3046. i0, i1, i2, i3 = [dim-1 for dim in c.shape]
  3047. j0, j1, j2, j3 = [dim-1 for dim in src.shape]
  3048. assert_equal(src[idx[j0], idx[j1], idx[j2], idx[j3]],
  3049. c[idx[i0], idx[i1], idx[i2], idx[i3]],
  3050. str((i, j, k)))
  3051. # check a view is always returned, gh-5260
  3052. assert_(not c.flags['OWNDATA'], str((i, j, k)))
  3053. # check on non-contiguous input array
  3054. if k == 1:
  3055. b = c
  3056. def test_conjugate(self):
  3057. a = np.array([1-1j, 1+1j, 23+23.0j])
  3058. ac = a.conj()
  3059. assert_equal(a.real, ac.real)
  3060. assert_equal(a.imag, -ac.imag)
  3061. assert_equal(ac, a.conjugate())
  3062. assert_equal(ac, np.conjugate(a))
  3063. a = np.array([1-1j, 1+1j, 23+23.0j], 'F')
  3064. ac = a.conj()
  3065. assert_equal(a.real, ac.real)
  3066. assert_equal(a.imag, -ac.imag)
  3067. assert_equal(ac, a.conjugate())
  3068. assert_equal(ac, np.conjugate(a))
  3069. a = np.array([1, 2, 3])
  3070. ac = a.conj()
  3071. assert_equal(a, ac)
  3072. assert_equal(ac, a.conjugate())
  3073. assert_equal(ac, np.conjugate(a))
  3074. a = np.array([1.0, 2.0, 3.0])
  3075. ac = a.conj()
  3076. assert_equal(a, ac)
  3077. assert_equal(ac, a.conjugate())
  3078. assert_equal(ac, np.conjugate(a))
  3079. a = np.array([1-1j, 1+1j, 1, 2.0], object)
  3080. ac = a.conj()
  3081. assert_equal(ac, [k.conjugate() for k in a])
  3082. assert_equal(ac, a.conjugate())
  3083. assert_equal(ac, np.conjugate(a))
  3084. a = np.array([1-1j, 1, 2.0, 'f'], object)
  3085. assert_raises(TypeError, lambda: a.conj())
  3086. assert_raises(TypeError, lambda: a.conjugate())
  3087. def test_conjugate_out(self):
  3088. # Minimal test for the out argument being passed on correctly
  3089. # NOTE: The ability to pass `out` is currently undocumented!
  3090. a = np.array([1-1j, 1+1j, 23+23.0j])
  3091. out = np.empty_like(a)
  3092. res = a.conjugate(out)
  3093. assert res is out
  3094. assert_array_equal(out, a.conjugate())
  3095. def test__complex__(self):
  3096. dtypes = ['i1', 'i2', 'i4', 'i8',
  3097. 'u1', 'u2', 'u4', 'u8',
  3098. 'f', 'd', 'g', 'F', 'D', 'G',
  3099. '?', 'O']
  3100. for dt in dtypes:
  3101. a = np.array(7, dtype=dt)
  3102. b = np.array([7], dtype=dt)
  3103. c = np.array([[[[[7]]]]], dtype=dt)
  3104. msg = 'dtype: {0}'.format(dt)
  3105. ap = complex(a)
  3106. assert_equal(ap, a, msg)
  3107. bp = complex(b)
  3108. assert_equal(bp, b, msg)
  3109. cp = complex(c)
  3110. assert_equal(cp, c, msg)
  3111. def test__complex__should_not_work(self):
  3112. dtypes = ['i1', 'i2', 'i4', 'i8',
  3113. 'u1', 'u2', 'u4', 'u8',
  3114. 'f', 'd', 'g', 'F', 'D', 'G',
  3115. '?', 'O']
  3116. for dt in dtypes:
  3117. a = np.array([1, 2, 3], dtype=dt)
  3118. assert_raises(TypeError, complex, a)
  3119. dt = np.dtype([('a', 'f8'), ('b', 'i1')])
  3120. b = np.array((1.0, 3), dtype=dt)
  3121. assert_raises(TypeError, complex, b)
  3122. c = np.array([(1.0, 3), (2e-3, 7)], dtype=dt)
  3123. assert_raises(TypeError, complex, c)
  3124. d = np.array('1+1j')
  3125. assert_raises(TypeError, complex, d)
  3126. e = np.array(['1+1j'], 'U')
  3127. assert_raises(TypeError, complex, e)
  3128. class TestCequenceMethods:
  3129. def test_array_contains(self):
  3130. assert_(4.0 in np.arange(16.).reshape(4,4))
  3131. assert_(20.0 not in np.arange(16.).reshape(4,4))
  3132. class TestBinop:
  3133. def test_inplace(self):
  3134. # test refcount 1 inplace conversion
  3135. assert_array_almost_equal(np.array([0.5]) * np.array([1.0, 2.0]),
  3136. [0.5, 1.0])
  3137. d = np.array([0.5, 0.5])[::2]
  3138. assert_array_almost_equal(d * (d * np.array([1.0, 2.0])),
  3139. [0.25, 0.5])
  3140. a = np.array([0.5])
  3141. b = np.array([0.5])
  3142. c = a + b
  3143. c = a - b
  3144. c = a * b
  3145. c = a / b
  3146. assert_equal(a, b)
  3147. assert_almost_equal(c, 1.)
  3148. c = a + b * 2. / b * a - a / b
  3149. assert_equal(a, b)
  3150. assert_equal(c, 0.5)
  3151. # true divide
  3152. a = np.array([5])
  3153. b = np.array([3])
  3154. c = (a * a) / b
  3155. assert_almost_equal(c, 25 / 3)
  3156. assert_equal(a, 5)
  3157. assert_equal(b, 3)
  3158. # ndarray.__rop__ always calls ufunc
  3159. # ndarray.__iop__ always calls ufunc
  3160. # ndarray.__op__, __rop__:
  3161. # - defer if other has __array_ufunc__ and it is None
  3162. # or other is not a subclass and has higher array priority
  3163. # - else, call ufunc
  3164. def test_ufunc_binop_interaction(self):
  3165. # Python method name (without underscores)
  3166. # -> (numpy ufunc, has_in_place_version, preferred_dtype)
  3167. ops = {
  3168. 'add': (np.add, True, float),
  3169. 'sub': (np.subtract, True, float),
  3170. 'mul': (np.multiply, True, float),
  3171. 'truediv': (np.true_divide, True, float),
  3172. 'floordiv': (np.floor_divide, True, float),
  3173. 'mod': (np.remainder, True, float),
  3174. 'divmod': (np.divmod, False, float),
  3175. 'pow': (np.power, True, int),
  3176. 'lshift': (np.left_shift, True, int),
  3177. 'rshift': (np.right_shift, True, int),
  3178. 'and': (np.bitwise_and, True, int),
  3179. 'xor': (np.bitwise_xor, True, int),
  3180. 'or': (np.bitwise_or, True, int),
  3181. 'matmul': (np.matmul, False, float),
  3182. # 'ge': (np.less_equal, False),
  3183. # 'gt': (np.less, False),
  3184. # 'le': (np.greater_equal, False),
  3185. # 'lt': (np.greater, False),
  3186. # 'eq': (np.equal, False),
  3187. # 'ne': (np.not_equal, False),
  3188. }
  3189. class Coerced(Exception):
  3190. pass
  3191. def array_impl(self):
  3192. raise Coerced
  3193. def op_impl(self, other):
  3194. return "forward"
  3195. def rop_impl(self, other):
  3196. return "reverse"
  3197. def iop_impl(self, other):
  3198. return "in-place"
  3199. def array_ufunc_impl(self, ufunc, method, *args, **kwargs):
  3200. return ("__array_ufunc__", ufunc, method, args, kwargs)
  3201. # Create an object with the given base, in the given module, with a
  3202. # bunch of placeholder __op__ methods, and optionally a
  3203. # __array_ufunc__ and __array_priority__.
  3204. def make_obj(base, array_priority=False, array_ufunc=False,
  3205. alleged_module="__main__"):
  3206. class_namespace = {"__array__": array_impl}
  3207. if array_priority is not False:
  3208. class_namespace["__array_priority__"] = array_priority
  3209. for op in ops:
  3210. class_namespace["__{0}__".format(op)] = op_impl
  3211. class_namespace["__r{0}__".format(op)] = rop_impl
  3212. class_namespace["__i{0}__".format(op)] = iop_impl
  3213. if array_ufunc is not False:
  3214. class_namespace["__array_ufunc__"] = array_ufunc
  3215. eval_namespace = {"base": base,
  3216. "class_namespace": class_namespace,
  3217. "__name__": alleged_module,
  3218. }
  3219. MyType = eval("type('MyType', (base,), class_namespace)",
  3220. eval_namespace)
  3221. if issubclass(MyType, np.ndarray):
  3222. # Use this range to avoid special case weirdnesses around
  3223. # divide-by-0, pow(x, 2), overflow due to pow(big, big), etc.
  3224. return np.arange(3, 7).reshape(2, 2).view(MyType)
  3225. else:
  3226. return MyType()
  3227. def check(obj, binop_override_expected, ufunc_override_expected,
  3228. inplace_override_expected, check_scalar=True):
  3229. for op, (ufunc, has_inplace, dtype) in ops.items():
  3230. err_msg = ('op: %s, ufunc: %s, has_inplace: %s, dtype: %s'
  3231. % (op, ufunc, has_inplace, dtype))
  3232. check_objs = [np.arange(3, 7, dtype=dtype).reshape(2, 2)]
  3233. if check_scalar:
  3234. check_objs.append(check_objs[0][0])
  3235. for arr in check_objs:
  3236. arr_method = getattr(arr, "__{0}__".format(op))
  3237. def first_out_arg(result):
  3238. if op == "divmod":
  3239. assert_(isinstance(result, tuple))
  3240. return result[0]
  3241. else:
  3242. return result
  3243. # arr __op__ obj
  3244. if binop_override_expected:
  3245. assert_equal(arr_method(obj), NotImplemented, err_msg)
  3246. elif ufunc_override_expected:
  3247. assert_equal(arr_method(obj)[0], "__array_ufunc__",
  3248. err_msg)
  3249. else:
  3250. if (isinstance(obj, np.ndarray) and
  3251. (type(obj).__array_ufunc__ is
  3252. np.ndarray.__array_ufunc__)):
  3253. # __array__ gets ignored
  3254. res = first_out_arg(arr_method(obj))
  3255. assert_(res.__class__ is obj.__class__, err_msg)
  3256. else:
  3257. assert_raises((TypeError, Coerced),
  3258. arr_method, obj, err_msg=err_msg)
  3259. # obj __op__ arr
  3260. arr_rmethod = getattr(arr, "__r{0}__".format(op))
  3261. if ufunc_override_expected:
  3262. res = arr_rmethod(obj)
  3263. assert_equal(res[0], "__array_ufunc__",
  3264. err_msg=err_msg)
  3265. assert_equal(res[1], ufunc, err_msg=err_msg)
  3266. else:
  3267. if (isinstance(obj, np.ndarray) and
  3268. (type(obj).__array_ufunc__ is
  3269. np.ndarray.__array_ufunc__)):
  3270. # __array__ gets ignored
  3271. res = first_out_arg(arr_rmethod(obj))
  3272. assert_(res.__class__ is obj.__class__, err_msg)
  3273. else:
  3274. # __array_ufunc__ = "asdf" creates a TypeError
  3275. assert_raises((TypeError, Coerced),
  3276. arr_rmethod, obj, err_msg=err_msg)
  3277. # arr __iop__ obj
  3278. # array scalars don't have in-place operators
  3279. if has_inplace and isinstance(arr, np.ndarray):
  3280. arr_imethod = getattr(arr, "__i{0}__".format(op))
  3281. if inplace_override_expected:
  3282. assert_equal(arr_method(obj), NotImplemented,
  3283. err_msg=err_msg)
  3284. elif ufunc_override_expected:
  3285. res = arr_imethod(obj)
  3286. assert_equal(res[0], "__array_ufunc__", err_msg)
  3287. assert_equal(res[1], ufunc, err_msg)
  3288. assert_(type(res[-1]["out"]) is tuple, err_msg)
  3289. assert_(res[-1]["out"][0] is arr, err_msg)
  3290. else:
  3291. if (isinstance(obj, np.ndarray) and
  3292. (type(obj).__array_ufunc__ is
  3293. np.ndarray.__array_ufunc__)):
  3294. # __array__ gets ignored
  3295. assert_(arr_imethod(obj) is arr, err_msg)
  3296. else:
  3297. assert_raises((TypeError, Coerced),
  3298. arr_imethod, obj,
  3299. err_msg=err_msg)
  3300. op_fn = getattr(operator, op, None)
  3301. if op_fn is None:
  3302. op_fn = getattr(operator, op + "_", None)
  3303. if op_fn is None:
  3304. op_fn = getattr(builtins, op)
  3305. assert_equal(op_fn(obj, arr), "forward", err_msg)
  3306. if not isinstance(obj, np.ndarray):
  3307. if binop_override_expected:
  3308. assert_equal(op_fn(arr, obj), "reverse", err_msg)
  3309. elif ufunc_override_expected:
  3310. assert_equal(op_fn(arr, obj)[0], "__array_ufunc__",
  3311. err_msg)
  3312. if ufunc_override_expected:
  3313. assert_equal(ufunc(obj, arr)[0], "__array_ufunc__",
  3314. err_msg)
  3315. # No array priority, no array_ufunc -> nothing called
  3316. check(make_obj(object), False, False, False)
  3317. # Negative array priority, no array_ufunc -> nothing called
  3318. # (has to be very negative, because scalar priority is -1000000.0)
  3319. check(make_obj(object, array_priority=-2**30), False, False, False)
  3320. # Positive array priority, no array_ufunc -> binops and iops only
  3321. check(make_obj(object, array_priority=1), True, False, True)
  3322. # ndarray ignores array_priority for ndarray subclasses
  3323. check(make_obj(np.ndarray, array_priority=1), False, False, False,
  3324. check_scalar=False)
  3325. # Positive array_priority and array_ufunc -> array_ufunc only
  3326. check(make_obj(object, array_priority=1,
  3327. array_ufunc=array_ufunc_impl), False, True, False)
  3328. check(make_obj(np.ndarray, array_priority=1,
  3329. array_ufunc=array_ufunc_impl), False, True, False)
  3330. # array_ufunc set to None -> defer binops only
  3331. check(make_obj(object, array_ufunc=None), True, False, False)
  3332. check(make_obj(np.ndarray, array_ufunc=None), True, False, False,
  3333. check_scalar=False)
  3334. @pytest.mark.parametrize("priority", [None, "runtime error"])
  3335. def test_ufunc_binop_bad_array_priority(self, priority):
  3336. # Mainly checks that this does not crash. The second array has a lower
  3337. # priority than -1 ("error value"). If the __radd__ actually exists,
  3338. # bad things can happen (I think via the scalar paths).
  3339. # In principle both of these can probably just be errors in the future.
  3340. class BadPriority:
  3341. @property
  3342. def __array_priority__(self):
  3343. if priority == "runtime error":
  3344. raise RuntimeError("RuntimeError in __array_priority__!")
  3345. return priority
  3346. def __radd__(self, other):
  3347. return "result"
  3348. class LowPriority(np.ndarray):
  3349. __array_priority__ = -1000
  3350. # Priority failure uses the same as scalars (smaller -1000). So the
  3351. # LowPriority wins with 'result' for each element (inner operation).
  3352. res = np.arange(3).view(LowPriority) + BadPriority()
  3353. assert res.shape == (3,)
  3354. assert res[0] == 'result'
  3355. def test_ufunc_override_normalize_signature(self):
  3356. # gh-5674
  3357. class SomeClass:
  3358. def __array_ufunc__(self, ufunc, method, *inputs, **kw):
  3359. return kw
  3360. a = SomeClass()
  3361. kw = np.add(a, [1])
  3362. assert_('sig' not in kw and 'signature' not in kw)
  3363. kw = np.add(a, [1], sig='ii->i')
  3364. assert_('sig' not in kw and 'signature' in kw)
  3365. assert_equal(kw['signature'], 'ii->i')
  3366. kw = np.add(a, [1], signature='ii->i')
  3367. assert_('sig' not in kw and 'signature' in kw)
  3368. assert_equal(kw['signature'], 'ii->i')
  3369. def test_array_ufunc_index(self):
  3370. # Check that index is set appropriately, also if only an output
  3371. # is passed on (latter is another regression tests for github bug 4753)
  3372. # This also checks implicitly that 'out' is always a tuple.
  3373. class CheckIndex:
  3374. def __array_ufunc__(self, ufunc, method, *inputs, **kw):
  3375. for i, a in enumerate(inputs):
  3376. if a is self:
  3377. return i
  3378. # calls below mean we must be in an output.
  3379. for j, a in enumerate(kw['out']):
  3380. if a is self:
  3381. return (j,)
  3382. a = CheckIndex()
  3383. dummy = np.arange(2.)
  3384. # 1 input, 1 output
  3385. assert_equal(np.sin(a), 0)
  3386. assert_equal(np.sin(dummy, a), (0,))
  3387. assert_equal(np.sin(dummy, out=a), (0,))
  3388. assert_equal(np.sin(dummy, out=(a,)), (0,))
  3389. assert_equal(np.sin(a, a), 0)
  3390. assert_equal(np.sin(a, out=a), 0)
  3391. assert_equal(np.sin(a, out=(a,)), 0)
  3392. # 1 input, 2 outputs
  3393. assert_equal(np.modf(dummy, a), (0,))
  3394. assert_equal(np.modf(dummy, None, a), (1,))
  3395. assert_equal(np.modf(dummy, dummy, a), (1,))
  3396. assert_equal(np.modf(dummy, out=(a, None)), (0,))
  3397. assert_equal(np.modf(dummy, out=(a, dummy)), (0,))
  3398. assert_equal(np.modf(dummy, out=(None, a)), (1,))
  3399. assert_equal(np.modf(dummy, out=(dummy, a)), (1,))
  3400. assert_equal(np.modf(a, out=(dummy, a)), 0)
  3401. with assert_raises(TypeError):
  3402. # Out argument must be tuple, since there are multiple outputs
  3403. np.modf(dummy, out=a)
  3404. assert_raises(ValueError, np.modf, dummy, out=(a,))
  3405. # 2 inputs, 1 output
  3406. assert_equal(np.add(a, dummy), 0)
  3407. assert_equal(np.add(dummy, a), 1)
  3408. assert_equal(np.add(dummy, dummy, a), (0,))
  3409. assert_equal(np.add(dummy, a, a), 1)
  3410. assert_equal(np.add(dummy, dummy, out=a), (0,))
  3411. assert_equal(np.add(dummy, dummy, out=(a,)), (0,))
  3412. assert_equal(np.add(a, dummy, out=a), 0)
  3413. def test_out_override(self):
  3414. # regression test for github bug 4753
  3415. class OutClass(np.ndarray):
  3416. def __array_ufunc__(self, ufunc, method, *inputs, **kw):
  3417. if 'out' in kw:
  3418. tmp_kw = kw.copy()
  3419. tmp_kw.pop('out')
  3420. func = getattr(ufunc, method)
  3421. kw['out'][0][...] = func(*inputs, **tmp_kw)
  3422. A = np.array([0]).view(OutClass)
  3423. B = np.array([5])
  3424. C = np.array([6])
  3425. np.multiply(C, B, A)
  3426. assert_equal(A[0], 30)
  3427. assert_(isinstance(A, OutClass))
  3428. A[0] = 0
  3429. np.multiply(C, B, out=A)
  3430. assert_equal(A[0], 30)
  3431. assert_(isinstance(A, OutClass))
  3432. def test_pow_override_with_errors(self):
  3433. # regression test for gh-9112
  3434. class PowerOnly(np.ndarray):
  3435. def __array_ufunc__(self, ufunc, method, *inputs, **kw):
  3436. if ufunc is not np.power:
  3437. raise NotImplementedError
  3438. return "POWER!"
  3439. # explicit cast to float, to ensure the fast power path is taken.
  3440. a = np.array(5., dtype=np.float64).view(PowerOnly)
  3441. assert_equal(a ** 2.5, "POWER!")
  3442. with assert_raises(NotImplementedError):
  3443. a ** 0.5
  3444. with assert_raises(NotImplementedError):
  3445. a ** 0
  3446. with assert_raises(NotImplementedError):
  3447. a ** 1
  3448. with assert_raises(NotImplementedError):
  3449. a ** -1
  3450. with assert_raises(NotImplementedError):
  3451. a ** 2
  3452. def test_pow_array_object_dtype(self):
  3453. # test pow on arrays of object dtype
  3454. class SomeClass:
  3455. def __init__(self, num=None):
  3456. self.num = num
  3457. # want to ensure a fast pow path is not taken
  3458. def __mul__(self, other):
  3459. raise AssertionError('__mul__ should not be called')
  3460. def __div__(self, other):
  3461. raise AssertionError('__div__ should not be called')
  3462. def __pow__(self, exp):
  3463. return SomeClass(num=self.num ** exp)
  3464. def __eq__(self, other):
  3465. if isinstance(other, SomeClass):
  3466. return self.num == other.num
  3467. __rpow__ = __pow__
  3468. def pow_for(exp, arr):
  3469. return np.array([x ** exp for x in arr])
  3470. obj_arr = np.array([SomeClass(1), SomeClass(2), SomeClass(3)])
  3471. assert_equal(obj_arr ** 0.5, pow_for(0.5, obj_arr))
  3472. assert_equal(obj_arr ** 0, pow_for(0, obj_arr))
  3473. assert_equal(obj_arr ** 1, pow_for(1, obj_arr))
  3474. assert_equal(obj_arr ** -1, pow_for(-1, obj_arr))
  3475. assert_equal(obj_arr ** 2, pow_for(2, obj_arr))
  3476. def test_pos_array_ufunc_override(self):
  3477. class A(np.ndarray):
  3478. def __array_ufunc__(self, ufunc, method, *inputs, **kwargs):
  3479. return getattr(ufunc, method)(*[i.view(np.ndarray) for
  3480. i in inputs], **kwargs)
  3481. tst = np.array('foo').view(A)
  3482. with assert_raises(TypeError):
  3483. +tst
  3484. class TestTemporaryElide:
  3485. # elision is only triggered on relatively large arrays
  3486. def test_extension_incref_elide(self):
  3487. # test extension (e.g. cython) calling PyNumber_* slots without
  3488. # increasing the reference counts
  3489. #
  3490. # def incref_elide(a):
  3491. # d = input.copy() # refcount 1
  3492. # return d, d + d # PyNumber_Add without increasing refcount
  3493. from numpy.core._multiarray_tests import incref_elide
  3494. d = np.ones(100000)
  3495. orig, res = incref_elide(d)
  3496. d + d
  3497. # the return original should not be changed to an inplace operation
  3498. assert_array_equal(orig, d)
  3499. assert_array_equal(res, d + d)
  3500. def test_extension_incref_elide_stack(self):
  3501. # scanning if the refcount == 1 object is on the python stack to check
  3502. # that we are called directly from python is flawed as object may still
  3503. # be above the stack pointer and we have no access to the top of it
  3504. #
  3505. # def incref_elide_l(d):
  3506. # return l[4] + l[4] # PyNumber_Add without increasing refcount
  3507. from numpy.core._multiarray_tests import incref_elide_l
  3508. # padding with 1 makes sure the object on the stack is not overwritten
  3509. l = [1, 1, 1, 1, np.ones(100000)]
  3510. res = incref_elide_l(l)
  3511. # the return original should not be changed to an inplace operation
  3512. assert_array_equal(l[4], np.ones(100000))
  3513. assert_array_equal(res, l[4] + l[4])
  3514. def test_temporary_with_cast(self):
  3515. # check that we don't elide into a temporary which would need casting
  3516. d = np.ones(200000, dtype=np.int64)
  3517. assert_equal(((d + d) + 2**222).dtype, np.dtype('O'))
  3518. r = ((d + d) / 2)
  3519. assert_equal(r.dtype, np.dtype('f8'))
  3520. r = np.true_divide((d + d), 2)
  3521. assert_equal(r.dtype, np.dtype('f8'))
  3522. r = ((d + d) / 2.)
  3523. assert_equal(r.dtype, np.dtype('f8'))
  3524. r = ((d + d) // 2)
  3525. assert_equal(r.dtype, np.dtype(np.int64))
  3526. # commutative elision into the astype result
  3527. f = np.ones(100000, dtype=np.float32)
  3528. assert_equal(((f + f) + f.astype(np.float64)).dtype, np.dtype('f8'))
  3529. # no elision into lower type
  3530. d = f.astype(np.float64)
  3531. assert_equal(((f + f) + d).dtype, d.dtype)
  3532. l = np.ones(100000, dtype=np.longdouble)
  3533. assert_equal(((d + d) + l).dtype, l.dtype)
  3534. # test unary abs with different output dtype
  3535. for dt in (np.complex64, np.complex128, np.clongdouble):
  3536. c = np.ones(100000, dtype=dt)
  3537. r = abs(c * 2.0)
  3538. assert_equal(r.dtype, np.dtype('f%d' % (c.itemsize // 2)))
  3539. def test_elide_broadcast(self):
  3540. # test no elision on broadcast to higher dimension
  3541. # only triggers elision code path in debug mode as triggering it in
  3542. # normal mode needs 256kb large matching dimension, so a lot of memory
  3543. d = np.ones((2000, 1), dtype=int)
  3544. b = np.ones((2000), dtype=bool)
  3545. r = (1 - d) + b
  3546. assert_equal(r, 1)
  3547. assert_equal(r.shape, (2000, 2000))
  3548. def test_elide_scalar(self):
  3549. # check inplace op does not create ndarray from scalars
  3550. a = np.bool_()
  3551. assert_(type(~(a & a)) is np.bool_)
  3552. def test_elide_scalar_readonly(self):
  3553. # The imaginary part of a real array is readonly. This needs to go
  3554. # through fast_scalar_power which is only called for powers of
  3555. # +1, -1, 0, 0.5, and 2, so use 2. Also need valid refcount for
  3556. # elision which can be gotten for the imaginary part of a real
  3557. # array. Should not error.
  3558. a = np.empty(100000, dtype=np.float64)
  3559. a.imag ** 2
  3560. def test_elide_readonly(self):
  3561. # don't try to elide readonly temporaries
  3562. r = np.asarray(np.broadcast_to(np.zeros(1), 100000).flat) * 0.0
  3563. assert_equal(r, 0)
  3564. def test_elide_updateifcopy(self):
  3565. a = np.ones(2**20)[::2]
  3566. b = a.flat.__array__() + 1
  3567. del b
  3568. assert_equal(a, 1)
  3569. class TestCAPI:
  3570. def test_IsPythonScalar(self):
  3571. from numpy.core._multiarray_tests import IsPythonScalar
  3572. assert_(IsPythonScalar(b'foobar'))
  3573. assert_(IsPythonScalar(1))
  3574. assert_(IsPythonScalar(2**80))
  3575. assert_(IsPythonScalar(2.))
  3576. assert_(IsPythonScalar("a"))
  3577. @pytest.mark.parametrize("converter",
  3578. [_multiarray_tests.run_scalar_intp_converter,
  3579. _multiarray_tests.run_scalar_intp_from_sequence])
  3580. def test_intp_sequence_converters(self, converter):
  3581. # Test simple values (-1 is special for error return paths)
  3582. assert converter(10) == (10,)
  3583. assert converter(-1) == (-1,)
  3584. # A 0-D array looks a bit like a sequence but must take the integer
  3585. # path:
  3586. assert converter(np.array(123)) == (123,)
  3587. # Test simple sequences (intp_from_sequence only supports length 1):
  3588. assert converter((10,)) == (10,)
  3589. assert converter(np.array([11])) == (11,)
  3590. @pytest.mark.parametrize("converter",
  3591. [_multiarray_tests.run_scalar_intp_converter,
  3592. _multiarray_tests.run_scalar_intp_from_sequence])
  3593. @pytest.mark.skipif(IS_PYPY and sys.implementation.version <= (7, 3, 8),
  3594. reason="PyPy bug in error formatting")
  3595. def test_intp_sequence_converters_errors(self, converter):
  3596. with pytest.raises(TypeError,
  3597. match="expected a sequence of integers or a single integer, "):
  3598. converter(object())
  3599. with pytest.raises(TypeError,
  3600. match="expected a sequence of integers or a single integer, "
  3601. "got '32.0'"):
  3602. converter(32.)
  3603. with pytest.raises(TypeError,
  3604. match="'float' object cannot be interpreted as an integer"):
  3605. converter([32.])
  3606. with pytest.raises(ValueError,
  3607. match="Maximum allowed dimension"):
  3608. # These converters currently convert overflows to a ValueError
  3609. converter(2**64)
  3610. class TestSubscripting:
  3611. def test_test_zero_rank(self):
  3612. x = np.array([1, 2, 3])
  3613. assert_(isinstance(x[0], np.int_))
  3614. assert_(type(x[0, ...]) is np.ndarray)
  3615. class TestPickling:
  3616. @pytest.mark.skipif(pickle.HIGHEST_PROTOCOL >= 5,
  3617. reason=('this tests the error messages when trying to'
  3618. 'protocol 5 although it is not available'))
  3619. def test_correct_protocol5_error_message(self):
  3620. array = np.arange(10)
  3621. def test_record_array_with_object_dtype(self):
  3622. my_object = object()
  3623. arr_with_object = np.array(
  3624. [(my_object, 1, 2.0)],
  3625. dtype=[('a', object), ('b', int), ('c', float)])
  3626. arr_without_object = np.array(
  3627. [('xxx', 1, 2.0)],
  3628. dtype=[('a', str), ('b', int), ('c', float)])
  3629. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  3630. depickled_arr_with_object = pickle.loads(
  3631. pickle.dumps(arr_with_object, protocol=proto))
  3632. depickled_arr_without_object = pickle.loads(
  3633. pickle.dumps(arr_without_object, protocol=proto))
  3634. assert_equal(arr_with_object.dtype,
  3635. depickled_arr_with_object.dtype)
  3636. assert_equal(arr_without_object.dtype,
  3637. depickled_arr_without_object.dtype)
  3638. @pytest.mark.skipif(pickle.HIGHEST_PROTOCOL < 5,
  3639. reason="requires pickle protocol 5")
  3640. def test_f_contiguous_array(self):
  3641. f_contiguous_array = np.array([[1, 2, 3], [4, 5, 6]], order='F')
  3642. buffers = []
  3643. # When using pickle protocol 5, Fortran-contiguous arrays can be
  3644. # serialized using out-of-band buffers
  3645. bytes_string = pickle.dumps(f_contiguous_array, protocol=5,
  3646. buffer_callback=buffers.append)
  3647. assert len(buffers) > 0
  3648. depickled_f_contiguous_array = pickle.loads(bytes_string,
  3649. buffers=buffers)
  3650. assert_equal(f_contiguous_array, depickled_f_contiguous_array)
  3651. def test_non_contiguous_array(self):
  3652. non_contiguous_array = np.arange(12).reshape(3, 4)[:, :2]
  3653. assert not non_contiguous_array.flags.c_contiguous
  3654. assert not non_contiguous_array.flags.f_contiguous
  3655. # make sure non-contiguous arrays can be pickled-depickled
  3656. # using any protocol
  3657. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  3658. depickled_non_contiguous_array = pickle.loads(
  3659. pickle.dumps(non_contiguous_array, protocol=proto))
  3660. assert_equal(non_contiguous_array, depickled_non_contiguous_array)
  3661. def test_roundtrip(self):
  3662. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  3663. carray = np.array([[2, 9], [7, 0], [3, 8]])
  3664. DATA = [
  3665. carray,
  3666. np.transpose(carray),
  3667. np.array([('xxx', 1, 2.0)], dtype=[('a', (str, 3)), ('b', int),
  3668. ('c', float)])
  3669. ]
  3670. refs = [weakref.ref(a) for a in DATA]
  3671. for a in DATA:
  3672. assert_equal(
  3673. a, pickle.loads(pickle.dumps(a, protocol=proto)),
  3674. err_msg="%r" % a)
  3675. del a, DATA, carray
  3676. break_cycles()
  3677. # check for reference leaks (gh-12793)
  3678. for ref in refs:
  3679. assert ref() is None
  3680. def _loads(self, obj):
  3681. return pickle.loads(obj, encoding='latin1')
  3682. # version 0 pickles, using protocol=2 to pickle
  3683. # version 0 doesn't have a version field
  3684. def test_version0_int8(self):
  3685. 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.'
  3686. a = np.array([1, 2, 3, 4], dtype=np.int8)
  3687. p = self._loads(s)
  3688. assert_equal(a, p)
  3689. def test_version0_float32(self):
  3690. 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.'
  3691. a = np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float32)
  3692. p = self._loads(s)
  3693. assert_equal(a, p)
  3694. def test_version0_object(self):
  3695. 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.'
  3696. a = np.array([{'a': 1}, {'b': 2}])
  3697. p = self._loads(s)
  3698. assert_equal(a, p)
  3699. # version 1 pickles, using protocol=2 to pickle
  3700. def test_version1_int8(self):
  3701. 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.'
  3702. a = np.array([1, 2, 3, 4], dtype=np.int8)
  3703. p = self._loads(s)
  3704. assert_equal(a, p)
  3705. def test_version1_float32(self):
  3706. 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.'
  3707. a = np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float32)
  3708. p = self._loads(s)
  3709. assert_equal(a, p)
  3710. def test_version1_object(self):
  3711. 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.'
  3712. a = np.array([{'a': 1}, {'b': 2}])
  3713. p = self._loads(s)
  3714. assert_equal(a, p)
  3715. def test_subarray_int_shape(self):
  3716. 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."
  3717. a = np.array([(1, (1, 2))], dtype=[('a', 'i1', (2, 2)), ('b', 'i1', 2)])
  3718. p = self._loads(s)
  3719. assert_equal(a, p)
  3720. def test_datetime64_byteorder(self):
  3721. original = np.array([['2015-02-24T00:00:00.000000000']], dtype='datetime64[ns]')
  3722. original_byte_reversed = original.copy(order='K')
  3723. original_byte_reversed.dtype = original_byte_reversed.dtype.newbyteorder('S')
  3724. original_byte_reversed.byteswap(inplace=True)
  3725. new = pickle.loads(pickle.dumps(original_byte_reversed))
  3726. assert_equal(original.dtype, new.dtype)
  3727. class TestFancyIndexing:
  3728. def test_list(self):
  3729. x = np.ones((1, 1))
  3730. x[:, [0]] = 2.0
  3731. assert_array_equal(x, np.array([[2.0]]))
  3732. x = np.ones((1, 1, 1))
  3733. x[:, :, [0]] = 2.0
  3734. assert_array_equal(x, np.array([[[2.0]]]))
  3735. def test_tuple(self):
  3736. x = np.ones((1, 1))
  3737. x[:, (0,)] = 2.0
  3738. assert_array_equal(x, np.array([[2.0]]))
  3739. x = np.ones((1, 1, 1))
  3740. x[:, :, (0,)] = 2.0
  3741. assert_array_equal(x, np.array([[[2.0]]]))
  3742. def test_mask(self):
  3743. x = np.array([1, 2, 3, 4])
  3744. m = np.array([0, 1, 0, 0], bool)
  3745. assert_array_equal(x[m], np.array([2]))
  3746. def test_mask2(self):
  3747. x = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
  3748. m = np.array([0, 1], bool)
  3749. m2 = np.array([[0, 1, 0, 0], [1, 0, 0, 0]], bool)
  3750. m3 = np.array([[0, 1, 0, 0], [0, 0, 0, 0]], bool)
  3751. assert_array_equal(x[m], np.array([[5, 6, 7, 8]]))
  3752. assert_array_equal(x[m2], np.array([2, 5]))
  3753. assert_array_equal(x[m3], np.array([2]))
  3754. def test_assign_mask(self):
  3755. x = np.array([1, 2, 3, 4])
  3756. m = np.array([0, 1, 0, 0], bool)
  3757. x[m] = 5
  3758. assert_array_equal(x, np.array([1, 5, 3, 4]))
  3759. def test_assign_mask2(self):
  3760. xorig = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
  3761. m = np.array([0, 1], bool)
  3762. m2 = np.array([[0, 1, 0, 0], [1, 0, 0, 0]], bool)
  3763. m3 = np.array([[0, 1, 0, 0], [0, 0, 0, 0]], bool)
  3764. x = xorig.copy()
  3765. x[m] = 10
  3766. assert_array_equal(x, np.array([[1, 2, 3, 4], [10, 10, 10, 10]]))
  3767. x = xorig.copy()
  3768. x[m2] = 10
  3769. assert_array_equal(x, np.array([[1, 10, 3, 4], [10, 6, 7, 8]]))
  3770. x = xorig.copy()
  3771. x[m3] = 10
  3772. assert_array_equal(x, np.array([[1, 10, 3, 4], [5, 6, 7, 8]]))
  3773. class TestStringCompare:
  3774. def test_string(self):
  3775. g1 = np.array(["This", "is", "example"])
  3776. g2 = np.array(["This", "was", "example"])
  3777. assert_array_equal(g1 == g2, [g1[i] == g2[i] for i in [0, 1, 2]])
  3778. assert_array_equal(g1 != g2, [g1[i] != g2[i] for i in [0, 1, 2]])
  3779. assert_array_equal(g1 <= g2, [g1[i] <= g2[i] for i in [0, 1, 2]])
  3780. assert_array_equal(g1 >= g2, [g1[i] >= g2[i] for i in [0, 1, 2]])
  3781. assert_array_equal(g1 < g2, [g1[i] < g2[i] for i in [0, 1, 2]])
  3782. assert_array_equal(g1 > g2, [g1[i] > g2[i] for i in [0, 1, 2]])
  3783. def test_mixed(self):
  3784. g1 = np.array(["spam", "spa", "spammer", "and eggs"])
  3785. g2 = "spam"
  3786. assert_array_equal(g1 == g2, [x == g2 for x in g1])
  3787. assert_array_equal(g1 != g2, [x != g2 for x in g1])
  3788. assert_array_equal(g1 < g2, [x < g2 for x in g1])
  3789. assert_array_equal(g1 > g2, [x > g2 for x in g1])
  3790. assert_array_equal(g1 <= g2, [x <= g2 for x in g1])
  3791. assert_array_equal(g1 >= g2, [x >= g2 for x in g1])
  3792. def test_unicode(self):
  3793. g1 = np.array(["This", "is", "example"])
  3794. g2 = np.array(["This", "was", "example"])
  3795. assert_array_equal(g1 == g2, [g1[i] == g2[i] for i in [0, 1, 2]])
  3796. assert_array_equal(g1 != g2, [g1[i] != g2[i] for i in [0, 1, 2]])
  3797. assert_array_equal(g1 <= g2, [g1[i] <= g2[i] for i in [0, 1, 2]])
  3798. assert_array_equal(g1 >= g2, [g1[i] >= g2[i] for i in [0, 1, 2]])
  3799. assert_array_equal(g1 < g2, [g1[i] < g2[i] for i in [0, 1, 2]])
  3800. assert_array_equal(g1 > g2, [g1[i] > g2[i] for i in [0, 1, 2]])
  3801. class TestArgmaxArgminCommon:
  3802. sizes = [(), (3,), (3, 2), (2, 3),
  3803. (3, 3), (2, 3, 4), (4, 3, 2),
  3804. (1, 2, 3, 4), (2, 3, 4, 1),
  3805. (3, 4, 1, 2), (4, 1, 2, 3),
  3806. (64,), (128,), (256,)]
  3807. @pytest.mark.parametrize("size, axis", itertools.chain(*[[(size, axis)
  3808. for axis in list(range(-len(size), len(size))) + [None]]
  3809. for size in sizes]))
  3810. @pytest.mark.parametrize('method', [np.argmax, np.argmin])
  3811. def test_np_argmin_argmax_keepdims(self, size, axis, method):
  3812. arr = np.random.normal(size=size)
  3813. # contiguous arrays
  3814. if axis is None:
  3815. new_shape = [1 for _ in range(len(size))]
  3816. else:
  3817. new_shape = list(size)
  3818. new_shape[axis] = 1
  3819. new_shape = tuple(new_shape)
  3820. _res_orig = method(arr, axis=axis)
  3821. res_orig = _res_orig.reshape(new_shape)
  3822. res = method(arr, axis=axis, keepdims=True)
  3823. assert_equal(res, res_orig)
  3824. assert_(res.shape == new_shape)
  3825. outarray = np.empty(res.shape, dtype=res.dtype)
  3826. res1 = method(arr, axis=axis, out=outarray,
  3827. keepdims=True)
  3828. assert_(res1 is outarray)
  3829. assert_equal(res, outarray)
  3830. if len(size) > 0:
  3831. wrong_shape = list(new_shape)
  3832. if axis is not None:
  3833. wrong_shape[axis] = 2
  3834. else:
  3835. wrong_shape[0] = 2
  3836. wrong_outarray = np.empty(wrong_shape, dtype=res.dtype)
  3837. with pytest.raises(ValueError):
  3838. method(arr.T, axis=axis,
  3839. out=wrong_outarray, keepdims=True)
  3840. # non-contiguous arrays
  3841. if axis is None:
  3842. new_shape = [1 for _ in range(len(size))]
  3843. else:
  3844. new_shape = list(size)[::-1]
  3845. new_shape[axis] = 1
  3846. new_shape = tuple(new_shape)
  3847. _res_orig = method(arr.T, axis=axis)
  3848. res_orig = _res_orig.reshape(new_shape)
  3849. res = method(arr.T, axis=axis, keepdims=True)
  3850. assert_equal(res, res_orig)
  3851. assert_(res.shape == new_shape)
  3852. outarray = np.empty(new_shape[::-1], dtype=res.dtype)
  3853. outarray = outarray.T
  3854. res1 = method(arr.T, axis=axis, out=outarray,
  3855. keepdims=True)
  3856. assert_(res1 is outarray)
  3857. assert_equal(res, outarray)
  3858. if len(size) > 0:
  3859. # one dimension lesser for non-zero sized
  3860. # array should raise an error
  3861. with pytest.raises(ValueError):
  3862. method(arr[0], axis=axis,
  3863. out=outarray, keepdims=True)
  3864. if len(size) > 0:
  3865. wrong_shape = list(new_shape)
  3866. if axis is not None:
  3867. wrong_shape[axis] = 2
  3868. else:
  3869. wrong_shape[0] = 2
  3870. wrong_outarray = np.empty(wrong_shape, dtype=res.dtype)
  3871. with pytest.raises(ValueError):
  3872. method(arr.T, axis=axis,
  3873. out=wrong_outarray, keepdims=True)
  3874. @pytest.mark.parametrize('method', ['max', 'min'])
  3875. def test_all(self, method):
  3876. a = np.random.normal(0, 1, (4, 5, 6, 7, 8))
  3877. arg_method = getattr(a, 'arg' + method)
  3878. val_method = getattr(a, method)
  3879. for i in range(a.ndim):
  3880. a_maxmin = val_method(i)
  3881. aarg_maxmin = arg_method(i)
  3882. axes = list(range(a.ndim))
  3883. axes.remove(i)
  3884. assert_(np.all(a_maxmin == aarg_maxmin.choose(
  3885. *a.transpose(i, *axes))))
  3886. @pytest.mark.parametrize('method', ['argmax', 'argmin'])
  3887. def test_output_shape(self, method):
  3888. # see also gh-616
  3889. a = np.ones((10, 5))
  3890. arg_method = getattr(a, method)
  3891. # Check some simple shape mismatches
  3892. out = np.ones(11, dtype=np.int_)
  3893. assert_raises(ValueError, arg_method, -1, out)
  3894. out = np.ones((2, 5), dtype=np.int_)
  3895. assert_raises(ValueError, arg_method, -1, out)
  3896. # these could be relaxed possibly (used to allow even the previous)
  3897. out = np.ones((1, 10), dtype=np.int_)
  3898. assert_raises(ValueError, arg_method, -1, out)
  3899. out = np.ones(10, dtype=np.int_)
  3900. arg_method(-1, out=out)
  3901. assert_equal(out, arg_method(-1))
  3902. @pytest.mark.parametrize('ndim', [0, 1])
  3903. @pytest.mark.parametrize('method', ['argmax', 'argmin'])
  3904. def test_ret_is_out(self, ndim, method):
  3905. a = np.ones((4,) + (256,)*ndim)
  3906. arg_method = getattr(a, method)
  3907. out = np.empty((256,)*ndim, dtype=np.intp)
  3908. ret = arg_method(axis=0, out=out)
  3909. assert ret is out
  3910. @pytest.mark.parametrize('np_array, method, idx, val',
  3911. [(np.zeros, 'argmax', 5942, "as"),
  3912. (np.ones, 'argmin', 6001, "0")])
  3913. def test_unicode(self, np_array, method, idx, val):
  3914. d = np_array(6031, dtype='<U9')
  3915. arg_method = getattr(d, method)
  3916. d[idx] = val
  3917. assert_equal(arg_method(), idx)
  3918. @pytest.mark.parametrize('arr_method, np_method',
  3919. [('argmax', np.argmax),
  3920. ('argmin', np.argmin)])
  3921. def test_np_vs_ndarray(self, arr_method, np_method):
  3922. # make sure both ndarray.argmax/argmin and
  3923. # numpy.argmax/argmin support out/axis args
  3924. a = np.random.normal(size=(2, 3))
  3925. arg_method = getattr(a, arr_method)
  3926. # check positional args
  3927. out1 = np.zeros(2, dtype=int)
  3928. out2 = np.zeros(2, dtype=int)
  3929. assert_equal(arg_method(1, out1), np_method(a, 1, out2))
  3930. assert_equal(out1, out2)
  3931. # check keyword args
  3932. out1 = np.zeros(3, dtype=int)
  3933. out2 = np.zeros(3, dtype=int)
  3934. assert_equal(arg_method(out=out1, axis=0),
  3935. np_method(a, out=out2, axis=0))
  3936. assert_equal(out1, out2)
  3937. @pytest.mark.leaks_references(reason="replaces None with NULL.")
  3938. @pytest.mark.parametrize('method, vals',
  3939. [('argmax', (10, 30)),
  3940. ('argmin', (30, 10))])
  3941. def test_object_with_NULLs(self, method, vals):
  3942. # See gh-6032
  3943. a = np.empty(4, dtype='O')
  3944. arg_method = getattr(a, method)
  3945. ctypes.memset(a.ctypes.data, 0, a.nbytes)
  3946. assert_equal(arg_method(), 0)
  3947. a[3] = vals[0]
  3948. assert_equal(arg_method(), 3)
  3949. a[1] = vals[1]
  3950. assert_equal(arg_method(), 1)
  3951. class TestArgmax:
  3952. usg_data = [
  3953. ([1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], 0),
  3954. ([3, 3, 3, 3, 2, 2, 2, 2], 0),
  3955. ([0, 1, 2, 3, 4, 5, 6, 7], 7),
  3956. ([7, 6, 5, 4, 3, 2, 1, 0], 0)
  3957. ]
  3958. sg_data = usg_data + [
  3959. ([1, 2, 3, 4, -4, -3, -2, -1], 3),
  3960. ([1, 2, 3, 4, -1, -2, -3, -4], 3)
  3961. ]
  3962. darr = [(np.array(d[0], dtype=t), d[1]) for d, t in (
  3963. itertools.product(usg_data, (
  3964. np.uint8, np.uint16, np.uint32, np.uint64
  3965. ))
  3966. )]
  3967. darr = darr + [(np.array(d[0], dtype=t), d[1]) for d, t in (
  3968. itertools.product(sg_data, (
  3969. np.int8, np.int16, np.int32, np.int64, np.float32, np.float64
  3970. ))
  3971. )]
  3972. darr = darr + [(np.array(d[0], dtype=t), d[1]) for d, t in (
  3973. itertools.product((
  3974. ([0, 1, 2, 3, np.nan], 4),
  3975. ([0, 1, 2, np.nan, 3], 3),
  3976. ([np.nan, 0, 1, 2, 3], 0),
  3977. ([np.nan, 0, np.nan, 2, 3], 0),
  3978. # To hit the tail of SIMD multi-level(x4, x1) inner loops
  3979. # on variant SIMD widthes
  3980. ([1] * (2*5-1) + [np.nan], 2*5-1),
  3981. ([1] * (4*5-1) + [np.nan], 4*5-1),
  3982. ([1] * (8*5-1) + [np.nan], 8*5-1),
  3983. ([1] * (16*5-1) + [np.nan], 16*5-1),
  3984. ([1] * (32*5-1) + [np.nan], 32*5-1)
  3985. ), (
  3986. np.float32, np.float64
  3987. ))
  3988. )]
  3989. nan_arr = darr + [
  3990. ([0, 1, 2, 3, complex(0, np.nan)], 4),
  3991. ([0, 1, 2, 3, complex(np.nan, 0)], 4),
  3992. ([0, 1, 2, complex(np.nan, 0), 3], 3),
  3993. ([0, 1, 2, complex(0, np.nan), 3], 3),
  3994. ([complex(0, np.nan), 0, 1, 2, 3], 0),
  3995. ([complex(np.nan, np.nan), 0, 1, 2, 3], 0),
  3996. ([complex(np.nan, 0), complex(np.nan, 2), complex(np.nan, 1)], 0),
  3997. ([complex(np.nan, np.nan), complex(np.nan, 2), complex(np.nan, 1)], 0),
  3998. ([complex(np.nan, 0), complex(np.nan, 2), complex(np.nan, np.nan)], 0),
  3999. ([complex(0, 0), complex(0, 2), complex(0, 1)], 1),
  4000. ([complex(1, 0), complex(0, 2), complex(0, 1)], 0),
  4001. ([complex(1, 0), complex(0, 2), complex(1, 1)], 2),
  4002. ([np.datetime64('1923-04-14T12:43:12'),
  4003. np.datetime64('1994-06-21T14:43:15'),
  4004. np.datetime64('2001-10-15T04:10:32'),
  4005. np.datetime64('1995-11-25T16:02:16'),
  4006. np.datetime64('2005-01-04T03:14:12'),
  4007. np.datetime64('2041-12-03T14:05:03')], 5),
  4008. ([np.datetime64('1935-09-14T04:40:11'),
  4009. np.datetime64('1949-10-12T12:32:11'),
  4010. np.datetime64('2010-01-03T05:14:12'),
  4011. np.datetime64('2015-11-20T12:20:59'),
  4012. np.datetime64('1932-09-23T10:10:13'),
  4013. np.datetime64('2014-10-10T03:50:30')], 3),
  4014. # Assorted tests with NaTs
  4015. ([np.datetime64('NaT'),
  4016. np.datetime64('NaT'),
  4017. np.datetime64('2010-01-03T05:14:12'),
  4018. np.datetime64('NaT'),
  4019. np.datetime64('2015-09-23T10:10:13'),
  4020. np.datetime64('1932-10-10T03:50:30')], 0),
  4021. ([np.datetime64('2059-03-14T12:43:12'),
  4022. np.datetime64('1996-09-21T14:43:15'),
  4023. np.datetime64('NaT'),
  4024. np.datetime64('2022-12-25T16:02:16'),
  4025. np.datetime64('1963-10-04T03:14:12'),
  4026. np.datetime64('2013-05-08T18:15:23')], 2),
  4027. ([np.timedelta64(2, 's'),
  4028. np.timedelta64(1, 's'),
  4029. np.timedelta64('NaT', 's'),
  4030. np.timedelta64(3, 's')], 2),
  4031. ([np.timedelta64('NaT', 's')] * 3, 0),
  4032. ([timedelta(days=5, seconds=14), timedelta(days=2, seconds=35),
  4033. timedelta(days=-1, seconds=23)], 0),
  4034. ([timedelta(days=1, seconds=43), timedelta(days=10, seconds=5),
  4035. timedelta(days=5, seconds=14)], 1),
  4036. ([timedelta(days=10, seconds=24), timedelta(days=10, seconds=5),
  4037. timedelta(days=10, seconds=43)], 2),
  4038. ([False, False, False, False, True], 4),
  4039. ([False, False, False, True, False], 3),
  4040. ([True, False, False, False, False], 0),
  4041. ([True, False, True, False, False], 0),
  4042. ]
  4043. @pytest.mark.parametrize('data', nan_arr)
  4044. def test_combinations(self, data):
  4045. arr, pos = data
  4046. with suppress_warnings() as sup:
  4047. sup.filter(RuntimeWarning,
  4048. "invalid value encountered in reduce")
  4049. val = np.max(arr)
  4050. assert_equal(np.argmax(arr), pos, err_msg="%r" % arr)
  4051. assert_equal(arr[np.argmax(arr)], val, err_msg="%r" % arr)
  4052. # add padding to test SIMD loops
  4053. rarr = np.repeat(arr, 129)
  4054. rpos = pos * 129
  4055. assert_equal(np.argmax(rarr), rpos, err_msg="%r" % rarr)
  4056. assert_equal(rarr[np.argmax(rarr)], val, err_msg="%r" % rarr)
  4057. padd = np.repeat(np.min(arr), 513)
  4058. rarr = np.concatenate((arr, padd))
  4059. rpos = pos
  4060. assert_equal(np.argmax(rarr), rpos, err_msg="%r" % rarr)
  4061. assert_equal(rarr[np.argmax(rarr)], val, err_msg="%r" % rarr)
  4062. def test_maximum_signed_integers(self):
  4063. a = np.array([1, 2**7 - 1, -2**7], dtype=np.int8)
  4064. assert_equal(np.argmax(a), 1)
  4065. a.repeat(129)
  4066. assert_equal(np.argmax(a), 1)
  4067. a = np.array([1, 2**15 - 1, -2**15], dtype=np.int16)
  4068. assert_equal(np.argmax(a), 1)
  4069. a.repeat(129)
  4070. assert_equal(np.argmax(a), 1)
  4071. a = np.array([1, 2**31 - 1, -2**31], dtype=np.int32)
  4072. assert_equal(np.argmax(a), 1)
  4073. a.repeat(129)
  4074. assert_equal(np.argmax(a), 1)
  4075. a = np.array([1, 2**63 - 1, -2**63], dtype=np.int64)
  4076. assert_equal(np.argmax(a), 1)
  4077. a.repeat(129)
  4078. assert_equal(np.argmax(a), 1)
  4079. class TestArgmin:
  4080. usg_data = [
  4081. ([1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], 8),
  4082. ([3, 3, 3, 3, 2, 2, 2, 2], 4),
  4083. ([0, 1, 2, 3, 4, 5, 6, 7], 0),
  4084. ([7, 6, 5, 4, 3, 2, 1, 0], 7)
  4085. ]
  4086. sg_data = usg_data + [
  4087. ([1, 2, 3, 4, -4, -3, -2, -1], 4),
  4088. ([1, 2, 3, 4, -1, -2, -3, -4], 7)
  4089. ]
  4090. darr = [(np.array(d[0], dtype=t), d[1]) for d, t in (
  4091. itertools.product(usg_data, (
  4092. np.uint8, np.uint16, np.uint32, np.uint64
  4093. ))
  4094. )]
  4095. darr = darr + [(np.array(d[0], dtype=t), d[1]) for d, t in (
  4096. itertools.product(sg_data, (
  4097. np.int8, np.int16, np.int32, np.int64, np.float32, np.float64
  4098. ))
  4099. )]
  4100. darr = darr + [(np.array(d[0], dtype=t), d[1]) for d, t in (
  4101. itertools.product((
  4102. ([0, 1, 2, 3, np.nan], 4),
  4103. ([0, 1, 2, np.nan, 3], 3),
  4104. ([np.nan, 0, 1, 2, 3], 0),
  4105. ([np.nan, 0, np.nan, 2, 3], 0),
  4106. # To hit the tail of SIMD multi-level(x4, x1) inner loops
  4107. # on variant SIMD widthes
  4108. ([1] * (2*5-1) + [np.nan], 2*5-1),
  4109. ([1] * (4*5-1) + [np.nan], 4*5-1),
  4110. ([1] * (8*5-1) + [np.nan], 8*5-1),
  4111. ([1] * (16*5-1) + [np.nan], 16*5-1),
  4112. ([1] * (32*5-1) + [np.nan], 32*5-1)
  4113. ), (
  4114. np.float32, np.float64
  4115. ))
  4116. )]
  4117. nan_arr = darr + [
  4118. ([0, 1, 2, 3, complex(0, np.nan)], 4),
  4119. ([0, 1, 2, 3, complex(np.nan, 0)], 4),
  4120. ([0, 1, 2, complex(np.nan, 0), 3], 3),
  4121. ([0, 1, 2, complex(0, np.nan), 3], 3),
  4122. ([complex(0, np.nan), 0, 1, 2, 3], 0),
  4123. ([complex(np.nan, np.nan), 0, 1, 2, 3], 0),
  4124. ([complex(np.nan, 0), complex(np.nan, 2), complex(np.nan, 1)], 0),
  4125. ([complex(np.nan, np.nan), complex(np.nan, 2), complex(np.nan, 1)], 0),
  4126. ([complex(np.nan, 0), complex(np.nan, 2), complex(np.nan, np.nan)], 0),
  4127. ([complex(0, 0), complex(0, 2), complex(0, 1)], 0),
  4128. ([complex(1, 0), complex(0, 2), complex(0, 1)], 2),
  4129. ([complex(1, 0), complex(0, 2), complex(1, 1)], 1),
  4130. ([np.datetime64('1923-04-14T12:43:12'),
  4131. np.datetime64('1994-06-21T14:43:15'),
  4132. np.datetime64('2001-10-15T04:10:32'),
  4133. np.datetime64('1995-11-25T16:02:16'),
  4134. np.datetime64('2005-01-04T03:14:12'),
  4135. np.datetime64('2041-12-03T14:05:03')], 0),
  4136. ([np.datetime64('1935-09-14T04:40:11'),
  4137. np.datetime64('1949-10-12T12:32:11'),
  4138. np.datetime64('2010-01-03T05:14:12'),
  4139. np.datetime64('2014-11-20T12:20:59'),
  4140. np.datetime64('2015-09-23T10:10:13'),
  4141. np.datetime64('1932-10-10T03:50:30')], 5),
  4142. # Assorted tests with NaTs
  4143. ([np.datetime64('NaT'),
  4144. np.datetime64('NaT'),
  4145. np.datetime64('2010-01-03T05:14:12'),
  4146. np.datetime64('NaT'),
  4147. np.datetime64('2015-09-23T10:10:13'),
  4148. np.datetime64('1932-10-10T03:50:30')], 0),
  4149. ([np.datetime64('2059-03-14T12:43:12'),
  4150. np.datetime64('1996-09-21T14:43:15'),
  4151. np.datetime64('NaT'),
  4152. np.datetime64('2022-12-25T16:02:16'),
  4153. np.datetime64('1963-10-04T03:14:12'),
  4154. np.datetime64('2013-05-08T18:15:23')], 2),
  4155. ([np.timedelta64(2, 's'),
  4156. np.timedelta64(1, 's'),
  4157. np.timedelta64('NaT', 's'),
  4158. np.timedelta64(3, 's')], 2),
  4159. ([np.timedelta64('NaT', 's')] * 3, 0),
  4160. ([timedelta(days=5, seconds=14), timedelta(days=2, seconds=35),
  4161. timedelta(days=-1, seconds=23)], 2),
  4162. ([timedelta(days=1, seconds=43), timedelta(days=10, seconds=5),
  4163. timedelta(days=5, seconds=14)], 0),
  4164. ([timedelta(days=10, seconds=24), timedelta(days=10, seconds=5),
  4165. timedelta(days=10, seconds=43)], 1),
  4166. ([True, True, True, True, False], 4),
  4167. ([True, True, True, False, True], 3),
  4168. ([False, True, True, True, True], 0),
  4169. ([False, True, False, True, True], 0),
  4170. ]
  4171. @pytest.mark.parametrize('data', nan_arr)
  4172. def test_combinations(self, data):
  4173. arr, pos = data
  4174. with suppress_warnings() as sup:
  4175. sup.filter(RuntimeWarning,
  4176. "invalid value encountered in reduce")
  4177. min_val = np.min(arr)
  4178. assert_equal(np.argmin(arr), pos, err_msg="%r" % arr)
  4179. assert_equal(arr[np.argmin(arr)], min_val, err_msg="%r" % arr)
  4180. # add padding to test SIMD loops
  4181. rarr = np.repeat(arr, 129)
  4182. rpos = pos * 129
  4183. assert_equal(np.argmin(rarr), rpos, err_msg="%r" % rarr)
  4184. assert_equal(rarr[np.argmin(rarr)], min_val, err_msg="%r" % rarr)
  4185. padd = np.repeat(np.max(arr), 513)
  4186. rarr = np.concatenate((arr, padd))
  4187. rpos = pos
  4188. assert_equal(np.argmin(rarr), rpos, err_msg="%r" % rarr)
  4189. assert_equal(rarr[np.argmin(rarr)], min_val, err_msg="%r" % rarr)
  4190. def test_minimum_signed_integers(self):
  4191. a = np.array([1, -2**7, -2**7 + 1, 2**7 - 1], dtype=np.int8)
  4192. assert_equal(np.argmin(a), 1)
  4193. a.repeat(129)
  4194. assert_equal(np.argmin(a), 1)
  4195. a = np.array([1, -2**15, -2**15 + 1, 2**15 - 1], dtype=np.int16)
  4196. assert_equal(np.argmin(a), 1)
  4197. a.repeat(129)
  4198. assert_equal(np.argmin(a), 1)
  4199. a = np.array([1, -2**31, -2**31 + 1, 2**31 - 1], dtype=np.int32)
  4200. assert_equal(np.argmin(a), 1)
  4201. a.repeat(129)
  4202. assert_equal(np.argmin(a), 1)
  4203. a = np.array([1, -2**63, -2**63 + 1, 2**63 - 1], dtype=np.int64)
  4204. assert_equal(np.argmin(a), 1)
  4205. a.repeat(129)
  4206. assert_equal(np.argmin(a), 1)
  4207. class TestMinMax:
  4208. def test_scalar(self):
  4209. assert_raises(np.AxisError, np.amax, 1, 1)
  4210. assert_raises(np.AxisError, np.amin, 1, 1)
  4211. assert_equal(np.amax(1, axis=0), 1)
  4212. assert_equal(np.amin(1, axis=0), 1)
  4213. assert_equal(np.amax(1, axis=None), 1)
  4214. assert_equal(np.amin(1, axis=None), 1)
  4215. def test_axis(self):
  4216. assert_raises(np.AxisError, np.amax, [1, 2, 3], 1000)
  4217. assert_equal(np.amax([[1, 2, 3]], axis=1), 3)
  4218. def test_datetime(self):
  4219. # Do not ignore NaT
  4220. for dtype in ('m8[s]', 'm8[Y]'):
  4221. a = np.arange(10).astype(dtype)
  4222. assert_equal(np.amin(a), a[0])
  4223. assert_equal(np.amax(a), a[9])
  4224. a[3] = 'NaT'
  4225. assert_equal(np.amin(a), a[3])
  4226. assert_equal(np.amax(a), a[3])
  4227. class TestNewaxis:
  4228. def test_basic(self):
  4229. sk = np.array([0, -0.1, 0.1])
  4230. res = 250*sk[:, np.newaxis]
  4231. assert_almost_equal(res.ravel(), 250*sk)
  4232. class TestClip:
  4233. def _check_range(self, x, cmin, cmax):
  4234. assert_(np.all(x >= cmin))
  4235. assert_(np.all(x <= cmax))
  4236. def _clip_type(self, type_group, array_max,
  4237. clip_min, clip_max, inplace=False,
  4238. expected_min=None, expected_max=None):
  4239. if expected_min is None:
  4240. expected_min = clip_min
  4241. if expected_max is None:
  4242. expected_max = clip_max
  4243. for T in np.sctypes[type_group]:
  4244. if sys.byteorder == 'little':
  4245. byte_orders = ['=', '>']
  4246. else:
  4247. byte_orders = ['<', '=']
  4248. for byteorder in byte_orders:
  4249. dtype = np.dtype(T).newbyteorder(byteorder)
  4250. x = (np.random.random(1000) * array_max).astype(dtype)
  4251. if inplace:
  4252. # The tests that call us pass clip_min and clip_max that
  4253. # might not fit in the destination dtype. They were written
  4254. # assuming the previous unsafe casting, which now must be
  4255. # passed explicitly to avoid a warning.
  4256. x.clip(clip_min, clip_max, x, casting='unsafe')
  4257. else:
  4258. x = x.clip(clip_min, clip_max)
  4259. byteorder = '='
  4260. if x.dtype.byteorder == '|':
  4261. byteorder = '|'
  4262. assert_equal(x.dtype.byteorder, byteorder)
  4263. self._check_range(x, expected_min, expected_max)
  4264. return x
  4265. def test_basic(self):
  4266. for inplace in [False, True]:
  4267. self._clip_type(
  4268. 'float', 1024, -12.8, 100.2, inplace=inplace)
  4269. self._clip_type(
  4270. 'float', 1024, 0, 0, inplace=inplace)
  4271. self._clip_type(
  4272. 'int', 1024, -120, 100, inplace=inplace)
  4273. self._clip_type(
  4274. 'int', 1024, 0, 0, inplace=inplace)
  4275. self._clip_type(
  4276. 'uint', 1024, 0, 0, inplace=inplace)
  4277. self._clip_type(
  4278. 'uint', 1024, -120, 100, inplace=inplace, expected_min=0)
  4279. def test_record_array(self):
  4280. rec = np.array([(-5, 2.0, 3.0), (5.0, 4.0, 3.0)],
  4281. dtype=[('x', '<f8'), ('y', '<f8'), ('z', '<f8')])
  4282. y = rec['x'].clip(-0.3, 0.5)
  4283. self._check_range(y, -0.3, 0.5)
  4284. def test_max_or_min(self):
  4285. val = np.array([0, 1, 2, 3, 4, 5, 6, 7])
  4286. x = val.clip(3)
  4287. assert_(np.all(x >= 3))
  4288. x = val.clip(min=3)
  4289. assert_(np.all(x >= 3))
  4290. x = val.clip(max=4)
  4291. assert_(np.all(x <= 4))
  4292. def test_nan(self):
  4293. input_arr = np.array([-2., np.nan, 0.5, 3., 0.25, np.nan])
  4294. result = input_arr.clip(-1, 1)
  4295. expected = np.array([-1., np.nan, 0.5, 1., 0.25, np.nan])
  4296. assert_array_equal(result, expected)
  4297. class TestCompress:
  4298. def test_axis(self):
  4299. tgt = [[5, 6, 7, 8, 9]]
  4300. arr = np.arange(10).reshape(2, 5)
  4301. out = np.compress([0, 1], arr, axis=0)
  4302. assert_equal(out, tgt)
  4303. tgt = [[1, 3], [6, 8]]
  4304. out = np.compress([0, 1, 0, 1, 0], arr, axis=1)
  4305. assert_equal(out, tgt)
  4306. def test_truncate(self):
  4307. tgt = [[1], [6]]
  4308. arr = np.arange(10).reshape(2, 5)
  4309. out = np.compress([0, 1], arr, axis=1)
  4310. assert_equal(out, tgt)
  4311. def test_flatten(self):
  4312. arr = np.arange(10).reshape(2, 5)
  4313. out = np.compress([0, 1], arr)
  4314. assert_equal(out, 1)
  4315. class TestPutmask:
  4316. def tst_basic(self, x, T, mask, val):
  4317. np.putmask(x, mask, val)
  4318. assert_equal(x[mask], np.array(val, T))
  4319. def test_ip_types(self):
  4320. unchecked_types = [bytes, str, np.void]
  4321. x = np.random.random(1000)*100
  4322. mask = x < 40
  4323. for val in [-100, 0, 15]:
  4324. for types in np.sctypes.values():
  4325. for T in types:
  4326. if T not in unchecked_types:
  4327. if val < 0 and np.dtype(T).kind == "u":
  4328. val = np.iinfo(T).max - 99
  4329. self.tst_basic(x.copy().astype(T), T, mask, val)
  4330. # Also test string of a length which uses an untypical length
  4331. dt = np.dtype("S3")
  4332. self.tst_basic(x.astype(dt), dt.type, mask, dt.type(val)[:3])
  4333. def test_mask_size(self):
  4334. assert_raises(ValueError, np.putmask, np.array([1, 2, 3]), [True], 5)
  4335. @pytest.mark.parametrize('dtype', ('>i4', '<i4'))
  4336. def test_byteorder(self, dtype):
  4337. x = np.array([1, 2, 3], dtype)
  4338. np.putmask(x, [True, False, True], -1)
  4339. assert_array_equal(x, [-1, 2, -1])
  4340. def test_record_array(self):
  4341. # Note mixed byteorder.
  4342. rec = np.array([(-5, 2.0, 3.0), (5.0, 4.0, 3.0)],
  4343. dtype=[('x', '<f8'), ('y', '>f8'), ('z', '<f8')])
  4344. np.putmask(rec['x'], [True, False], 10)
  4345. assert_array_equal(rec['x'], [10, 5])
  4346. assert_array_equal(rec['y'], [2, 4])
  4347. assert_array_equal(rec['z'], [3, 3])
  4348. np.putmask(rec['y'], [True, False], 11)
  4349. assert_array_equal(rec['x'], [10, 5])
  4350. assert_array_equal(rec['y'], [11, 4])
  4351. assert_array_equal(rec['z'], [3, 3])
  4352. def test_overlaps(self):
  4353. # gh-6272 check overlap
  4354. x = np.array([True, False, True, False])
  4355. np.putmask(x[1:4], [True, True, True], x[:3])
  4356. assert_equal(x, np.array([True, True, False, True]))
  4357. x = np.array([True, False, True, False])
  4358. np.putmask(x[1:4], x[:3], [True, False, True])
  4359. assert_equal(x, np.array([True, True, True, True]))
  4360. def test_writeable(self):
  4361. a = np.arange(5)
  4362. a.flags.writeable = False
  4363. with pytest.raises(ValueError):
  4364. np.putmask(a, a >= 2, 3)
  4365. class TestTake:
  4366. def tst_basic(self, x):
  4367. ind = list(range(x.shape[0]))
  4368. assert_array_equal(x.take(ind, axis=0), x)
  4369. def test_ip_types(self):
  4370. unchecked_types = [bytes, str, np.void]
  4371. x = np.random.random(24)*100
  4372. x.shape = 2, 3, 4
  4373. for types in np.sctypes.values():
  4374. for T in types:
  4375. if T not in unchecked_types:
  4376. self.tst_basic(x.copy().astype(T))
  4377. # Also test string of a length which uses an untypical length
  4378. self.tst_basic(x.astype("S3"))
  4379. def test_raise(self):
  4380. x = np.random.random(24)*100
  4381. x.shape = 2, 3, 4
  4382. assert_raises(IndexError, x.take, [0, 1, 2], axis=0)
  4383. assert_raises(IndexError, x.take, [-3], axis=0)
  4384. assert_array_equal(x.take([-1], axis=0)[0], x[1])
  4385. def test_clip(self):
  4386. x = np.random.random(24)*100
  4387. x.shape = 2, 3, 4
  4388. assert_array_equal(x.take([-1], axis=0, mode='clip')[0], x[0])
  4389. assert_array_equal(x.take([2], axis=0, mode='clip')[0], x[1])
  4390. def test_wrap(self):
  4391. x = np.random.random(24)*100
  4392. x.shape = 2, 3, 4
  4393. assert_array_equal(x.take([-1], axis=0, mode='wrap')[0], x[1])
  4394. assert_array_equal(x.take([2], axis=0, mode='wrap')[0], x[0])
  4395. assert_array_equal(x.take([3], axis=0, mode='wrap')[0], x[1])
  4396. @pytest.mark.parametrize('dtype', ('>i4', '<i4'))
  4397. def test_byteorder(self, dtype):
  4398. x = np.array([1, 2, 3], dtype)
  4399. assert_array_equal(x.take([0, 2, 1]), [1, 3, 2])
  4400. def test_record_array(self):
  4401. # Note mixed byteorder.
  4402. rec = np.array([(-5, 2.0, 3.0), (5.0, 4.0, 3.0)],
  4403. dtype=[('x', '<f8'), ('y', '>f8'), ('z', '<f8')])
  4404. rec1 = rec.take([1])
  4405. assert_(rec1['x'] == 5.0 and rec1['y'] == 4.0)
  4406. def test_out_overlap(self):
  4407. # gh-6272 check overlap on out
  4408. x = np.arange(5)
  4409. y = np.take(x, [1, 2, 3], out=x[2:5], mode='wrap')
  4410. assert_equal(y, np.array([1, 2, 3]))
  4411. @pytest.mark.parametrize('shape', [(1, 2), (1,), ()])
  4412. def test_ret_is_out(self, shape):
  4413. # 0d arrays should not be an exception to this rule
  4414. x = np.arange(5)
  4415. inds = np.zeros(shape, dtype=np.intp)
  4416. out = np.zeros(shape, dtype=x.dtype)
  4417. ret = np.take(x, inds, out=out)
  4418. assert ret is out
  4419. class TestLexsort:
  4420. @pytest.mark.parametrize('dtype',[
  4421. np.uint8, np.uint16, np.uint32, np.uint64,
  4422. np.int8, np.int16, np.int32, np.int64,
  4423. np.float16, np.float32, np.float64
  4424. ])
  4425. def test_basic(self, dtype):
  4426. a = np.array([1, 2, 1, 3, 1, 5], dtype=dtype)
  4427. b = np.array([0, 4, 5, 6, 2, 3], dtype=dtype)
  4428. idx = np.lexsort((b, a))
  4429. expected_idx = np.array([0, 4, 2, 1, 3, 5])
  4430. assert_array_equal(idx, expected_idx)
  4431. assert_array_equal(a[idx], np.sort(a))
  4432. def test_mixed(self):
  4433. a = np.array([1, 2, 1, 3, 1, 5])
  4434. b = np.array([0, 4, 5, 6, 2, 3], dtype='datetime64[D]')
  4435. idx = np.lexsort((b, a))
  4436. expected_idx = np.array([0, 4, 2, 1, 3, 5])
  4437. assert_array_equal(idx, expected_idx)
  4438. def test_datetime(self):
  4439. a = np.array([0,0,0], dtype='datetime64[D]')
  4440. b = np.array([2,1,0], dtype='datetime64[D]')
  4441. idx = np.lexsort((b, a))
  4442. expected_idx = np.array([2, 1, 0])
  4443. assert_array_equal(idx, expected_idx)
  4444. a = np.array([0,0,0], dtype='timedelta64[D]')
  4445. b = np.array([2,1,0], dtype='timedelta64[D]')
  4446. idx = np.lexsort((b, a))
  4447. expected_idx = np.array([2, 1, 0])
  4448. assert_array_equal(idx, expected_idx)
  4449. def test_object(self): # gh-6312
  4450. a = np.random.choice(10, 1000)
  4451. b = np.random.choice(['abc', 'xy', 'wz', 'efghi', 'qwst', 'x'], 1000)
  4452. for u in a, b:
  4453. left = np.lexsort((u.astype('O'),))
  4454. right = np.argsort(u, kind='mergesort')
  4455. assert_array_equal(left, right)
  4456. for u, v in (a, b), (b, a):
  4457. idx = np.lexsort((u, v))
  4458. assert_array_equal(idx, np.lexsort((u.astype('O'), v)))
  4459. assert_array_equal(idx, np.lexsort((u, v.astype('O'))))
  4460. u, v = np.array(u, dtype='object'), np.array(v, dtype='object')
  4461. assert_array_equal(idx, np.lexsort((u, v)))
  4462. def test_invalid_axis(self): # gh-7528
  4463. x = np.linspace(0., 1., 42*3).reshape(42, 3)
  4464. assert_raises(np.AxisError, np.lexsort, x, axis=2)
  4465. class TestIO:
  4466. """Test tofile, fromfile, tobytes, and fromstring"""
  4467. @pytest.fixture()
  4468. def x(self):
  4469. shape = (2, 4, 3)
  4470. rand = np.random.random
  4471. x = rand(shape) + rand(shape).astype(complex) * 1j
  4472. x[0, :, 1] = [np.nan, np.inf, -np.inf, np.nan]
  4473. return x
  4474. @pytest.fixture(params=["string", "path_obj"])
  4475. def tmp_filename(self, tmp_path, request):
  4476. # This fixture covers two cases:
  4477. # one where the filename is a string and
  4478. # another where it is a pathlib object
  4479. filename = tmp_path / "file"
  4480. if request.param == "string":
  4481. filename = str(filename)
  4482. yield filename
  4483. def test_nofile(self):
  4484. # this should probably be supported as a file
  4485. # but for now test for proper errors
  4486. b = io.BytesIO()
  4487. assert_raises(OSError, np.fromfile, b, np.uint8, 80)
  4488. d = np.ones(7)
  4489. assert_raises(OSError, lambda x: x.tofile(b), d)
  4490. def test_bool_fromstring(self):
  4491. v = np.array([True, False, True, False], dtype=np.bool_)
  4492. y = np.fromstring('1 0 -2.3 0.0', sep=' ', dtype=np.bool_)
  4493. assert_array_equal(v, y)
  4494. def test_uint64_fromstring(self):
  4495. d = np.fromstring("9923372036854775807 104783749223640",
  4496. dtype=np.uint64, sep=' ')
  4497. e = np.array([9923372036854775807, 104783749223640], dtype=np.uint64)
  4498. assert_array_equal(d, e)
  4499. def test_int64_fromstring(self):
  4500. d = np.fromstring("-25041670086757 104783749223640",
  4501. dtype=np.int64, sep=' ')
  4502. e = np.array([-25041670086757, 104783749223640], dtype=np.int64)
  4503. assert_array_equal(d, e)
  4504. def test_fromstring_count0(self):
  4505. d = np.fromstring("1,2", sep=",", dtype=np.int64, count=0)
  4506. assert d.shape == (0,)
  4507. def test_empty_files_text(self, tmp_filename):
  4508. with open(tmp_filename, 'w') as f:
  4509. pass
  4510. y = np.fromfile(tmp_filename)
  4511. assert_(y.size == 0, "Array not empty")
  4512. def test_empty_files_binary(self, tmp_filename):
  4513. with open(tmp_filename, 'wb') as f:
  4514. pass
  4515. y = np.fromfile(tmp_filename, sep=" ")
  4516. assert_(y.size == 0, "Array not empty")
  4517. def test_roundtrip_file(self, x, tmp_filename):
  4518. with open(tmp_filename, 'wb') as f:
  4519. x.tofile(f)
  4520. # NB. doesn't work with flush+seek, due to use of C stdio
  4521. with open(tmp_filename, 'rb') as f:
  4522. y = np.fromfile(f, dtype=x.dtype)
  4523. assert_array_equal(y, x.flat)
  4524. def test_roundtrip(self, x, tmp_filename):
  4525. x.tofile(tmp_filename)
  4526. y = np.fromfile(tmp_filename, dtype=x.dtype)
  4527. assert_array_equal(y, x.flat)
  4528. def test_roundtrip_dump_pathlib(self, x, tmp_filename):
  4529. p = pathlib.Path(tmp_filename)
  4530. x.dump(p)
  4531. y = np.load(p, allow_pickle=True)
  4532. assert_array_equal(y, x)
  4533. def test_roundtrip_binary_str(self, x):
  4534. s = x.tobytes()
  4535. y = np.frombuffer(s, dtype=x.dtype)
  4536. assert_array_equal(y, x.flat)
  4537. s = x.tobytes('F')
  4538. y = np.frombuffer(s, dtype=x.dtype)
  4539. assert_array_equal(y, x.flatten('F'))
  4540. def test_roundtrip_str(self, x):
  4541. x = x.real.ravel()
  4542. s = "@".join(map(str, x))
  4543. y = np.fromstring(s, sep="@")
  4544. # NB. str imbues less precision
  4545. nan_mask = ~np.isfinite(x)
  4546. assert_array_equal(x[nan_mask], y[nan_mask])
  4547. assert_array_almost_equal(x[~nan_mask], y[~nan_mask], decimal=5)
  4548. def test_roundtrip_repr(self, x):
  4549. x = x.real.ravel()
  4550. s = "@".join(map(repr, x))
  4551. y = np.fromstring(s, sep="@")
  4552. assert_array_equal(x, y)
  4553. def test_unseekable_fromfile(self, x, tmp_filename):
  4554. # gh-6246
  4555. x.tofile(tmp_filename)
  4556. def fail(*args, **kwargs):
  4557. raise OSError('Can not tell or seek')
  4558. with io.open(tmp_filename, 'rb', buffering=0) as f:
  4559. f.seek = fail
  4560. f.tell = fail
  4561. assert_raises(OSError, np.fromfile, f, dtype=x.dtype)
  4562. def test_io_open_unbuffered_fromfile(self, x, tmp_filename):
  4563. # gh-6632
  4564. x.tofile(tmp_filename)
  4565. with io.open(tmp_filename, 'rb', buffering=0) as f:
  4566. y = np.fromfile(f, dtype=x.dtype)
  4567. assert_array_equal(y, x.flat)
  4568. def test_largish_file(self, tmp_filename):
  4569. # check the fallocate path on files > 16MB
  4570. d = np.zeros(4 * 1024 ** 2)
  4571. d.tofile(tmp_filename)
  4572. assert_equal(os.path.getsize(tmp_filename), d.nbytes)
  4573. assert_array_equal(d, np.fromfile(tmp_filename))
  4574. # check offset
  4575. with open(tmp_filename, "r+b") as f:
  4576. f.seek(d.nbytes)
  4577. d.tofile(f)
  4578. assert_equal(os.path.getsize(tmp_filename), d.nbytes * 2)
  4579. # check append mode (gh-8329)
  4580. open(tmp_filename, "w").close() # delete file contents
  4581. with open(tmp_filename, "ab") as f:
  4582. d.tofile(f)
  4583. assert_array_equal(d, np.fromfile(tmp_filename))
  4584. with open(tmp_filename, "ab") as f:
  4585. d.tofile(f)
  4586. assert_equal(os.path.getsize(tmp_filename), d.nbytes * 2)
  4587. def test_io_open_buffered_fromfile(self, x, tmp_filename):
  4588. # gh-6632
  4589. x.tofile(tmp_filename)
  4590. with io.open(tmp_filename, 'rb', buffering=-1) as f:
  4591. y = np.fromfile(f, dtype=x.dtype)
  4592. assert_array_equal(y, x.flat)
  4593. def test_file_position_after_fromfile(self, tmp_filename):
  4594. # gh-4118
  4595. sizes = [io.DEFAULT_BUFFER_SIZE//8,
  4596. io.DEFAULT_BUFFER_SIZE,
  4597. io.DEFAULT_BUFFER_SIZE*8]
  4598. for size in sizes:
  4599. with open(tmp_filename, 'wb') as f:
  4600. f.seek(size-1)
  4601. f.write(b'\0')
  4602. for mode in ['rb', 'r+b']:
  4603. err_msg = "%d %s" % (size, mode)
  4604. with open(tmp_filename, mode) as f:
  4605. f.read(2)
  4606. np.fromfile(f, dtype=np.float64, count=1)
  4607. pos = f.tell()
  4608. assert_equal(pos, 10, err_msg=err_msg)
  4609. def test_file_position_after_tofile(self, tmp_filename):
  4610. # gh-4118
  4611. sizes = [io.DEFAULT_BUFFER_SIZE//8,
  4612. io.DEFAULT_BUFFER_SIZE,
  4613. io.DEFAULT_BUFFER_SIZE*8]
  4614. for size in sizes:
  4615. err_msg = "%d" % (size,)
  4616. with open(tmp_filename, 'wb') as f:
  4617. f.seek(size-1)
  4618. f.write(b'\0')
  4619. f.seek(10)
  4620. f.write(b'12')
  4621. np.array([0], dtype=np.float64).tofile(f)
  4622. pos = f.tell()
  4623. assert_equal(pos, 10 + 2 + 8, err_msg=err_msg)
  4624. with open(tmp_filename, 'r+b') as f:
  4625. f.read(2)
  4626. f.seek(0, 1) # seek between read&write required by ANSI C
  4627. np.array([0], dtype=np.float64).tofile(f)
  4628. pos = f.tell()
  4629. assert_equal(pos, 10, err_msg=err_msg)
  4630. def test_load_object_array_fromfile(self, tmp_filename):
  4631. # gh-12300
  4632. with open(tmp_filename, 'w') as f:
  4633. # Ensure we have a file with consistent contents
  4634. pass
  4635. with open(tmp_filename, 'rb') as f:
  4636. assert_raises_regex(ValueError, "Cannot read into object array",
  4637. np.fromfile, f, dtype=object)
  4638. assert_raises_regex(ValueError, "Cannot read into object array",
  4639. np.fromfile, tmp_filename, dtype=object)
  4640. def test_fromfile_offset(self, x, tmp_filename):
  4641. with open(tmp_filename, 'wb') as f:
  4642. x.tofile(f)
  4643. with open(tmp_filename, 'rb') as f:
  4644. y = np.fromfile(f, dtype=x.dtype, offset=0)
  4645. assert_array_equal(y, x.flat)
  4646. with open(tmp_filename, 'rb') as f:
  4647. count_items = len(x.flat) // 8
  4648. offset_items = len(x.flat) // 4
  4649. offset_bytes = x.dtype.itemsize * offset_items
  4650. y = np.fromfile(
  4651. f, dtype=x.dtype, count=count_items, offset=offset_bytes
  4652. )
  4653. assert_array_equal(
  4654. y, x.flat[offset_items:offset_items+count_items]
  4655. )
  4656. # subsequent seeks should stack
  4657. offset_bytes = x.dtype.itemsize
  4658. z = np.fromfile(f, dtype=x.dtype, offset=offset_bytes)
  4659. assert_array_equal(z, x.flat[offset_items+count_items+1:])
  4660. with open(tmp_filename, 'wb') as f:
  4661. x.tofile(f, sep=",")
  4662. with open(tmp_filename, 'rb') as f:
  4663. assert_raises_regex(
  4664. TypeError,
  4665. "'offset' argument only permitted for binary files",
  4666. np.fromfile, tmp_filename, dtype=x.dtype,
  4667. sep=",", offset=1)
  4668. @pytest.mark.skipif(IS_PYPY, reason="bug in PyPy's PyNumber_AsSsize_t")
  4669. def test_fromfile_bad_dup(self, x, tmp_filename):
  4670. def dup_str(fd):
  4671. return 'abc'
  4672. def dup_bigint(fd):
  4673. return 2**68
  4674. old_dup = os.dup
  4675. try:
  4676. with open(tmp_filename, 'wb') as f:
  4677. x.tofile(f)
  4678. for dup, exc in ((dup_str, TypeError), (dup_bigint, OSError)):
  4679. os.dup = dup
  4680. assert_raises(exc, np.fromfile, f)
  4681. finally:
  4682. os.dup = old_dup
  4683. def _check_from(self, s, value, filename, **kw):
  4684. if 'sep' not in kw:
  4685. y = np.frombuffer(s, **kw)
  4686. else:
  4687. y = np.fromstring(s, **kw)
  4688. assert_array_equal(y, value)
  4689. with open(filename, 'wb') as f:
  4690. f.write(s)
  4691. y = np.fromfile(filename, **kw)
  4692. assert_array_equal(y, value)
  4693. @pytest.fixture(params=["period", "comma"])
  4694. def decimal_sep_localization(self, request):
  4695. """
  4696. Including this fixture in a test will automatically
  4697. execute it with both types of decimal separator.
  4698. So::
  4699. def test_decimal(decimal_sep_localization):
  4700. pass
  4701. is equivalent to the following two tests::
  4702. def test_decimal_period_separator():
  4703. pass
  4704. def test_decimal_comma_separator():
  4705. with CommaDecimalPointLocale():
  4706. pass
  4707. """
  4708. if request.param == "period":
  4709. yield
  4710. elif request.param == "comma":
  4711. with CommaDecimalPointLocale():
  4712. yield
  4713. else:
  4714. assert False, request.param
  4715. def test_nan(self, tmp_filename, decimal_sep_localization):
  4716. self._check_from(
  4717. b"nan +nan -nan NaN nan(foo) +NaN(BAR) -NAN(q_u_u_x_)",
  4718. [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan],
  4719. tmp_filename,
  4720. sep=' ')
  4721. def test_inf(self, tmp_filename, decimal_sep_localization):
  4722. self._check_from(
  4723. b"inf +inf -inf infinity -Infinity iNfInItY -inF",
  4724. [np.inf, np.inf, -np.inf, np.inf, -np.inf, np.inf, -np.inf],
  4725. tmp_filename,
  4726. sep=' ')
  4727. def test_numbers(self, tmp_filename, decimal_sep_localization):
  4728. self._check_from(
  4729. b"1.234 -1.234 .3 .3e55 -123133.1231e+133",
  4730. [1.234, -1.234, .3, .3e55, -123133.1231e+133],
  4731. tmp_filename,
  4732. sep=' ')
  4733. def test_binary(self, tmp_filename):
  4734. self._check_from(
  4735. b'\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@',
  4736. np.array([1, 2, 3, 4]),
  4737. tmp_filename,
  4738. dtype='<f4')
  4739. @pytest.mark.slow # takes > 1 minute on mechanical hard drive
  4740. def test_big_binary(self):
  4741. """Test workarounds for 32-bit limit for MSVC fwrite, fseek, and ftell
  4742. These normally would hang doing something like this.
  4743. See : https://github.com/numpy/numpy/issues/2256
  4744. """
  4745. if sys.platform != 'win32' or '[GCC ' in sys.version:
  4746. return
  4747. try:
  4748. # before workarounds, only up to 2**32-1 worked
  4749. fourgbplus = 2**32 + 2**16
  4750. testbytes = np.arange(8, dtype=np.int8)
  4751. n = len(testbytes)
  4752. flike = tempfile.NamedTemporaryFile()
  4753. f = flike.file
  4754. np.tile(testbytes, fourgbplus // testbytes.nbytes).tofile(f)
  4755. flike.seek(0)
  4756. a = np.fromfile(f, dtype=np.int8)
  4757. flike.close()
  4758. assert_(len(a) == fourgbplus)
  4759. # check only start and end for speed:
  4760. assert_((a[:n] == testbytes).all())
  4761. assert_((a[-n:] == testbytes).all())
  4762. except (MemoryError, ValueError):
  4763. pass
  4764. def test_string(self, tmp_filename):
  4765. self._check_from(b'1,2,3,4', [1., 2., 3., 4.], tmp_filename, sep=',')
  4766. def test_counted_string(self, tmp_filename, decimal_sep_localization):
  4767. self._check_from(
  4768. b'1,2,3,4', [1., 2., 3., 4.], tmp_filename, count=4, sep=',')
  4769. self._check_from(
  4770. b'1,2,3,4', [1., 2., 3.], tmp_filename, count=3, sep=',')
  4771. self._check_from(
  4772. b'1,2,3,4', [1., 2., 3., 4.], tmp_filename, count=-1, sep=',')
  4773. def test_string_with_ws(self, tmp_filename):
  4774. self._check_from(
  4775. b'1 2 3 4 ', [1, 2, 3, 4], tmp_filename, dtype=int, sep=' ')
  4776. def test_counted_string_with_ws(self, tmp_filename):
  4777. self._check_from(
  4778. b'1 2 3 4 ', [1, 2, 3], tmp_filename, count=3, dtype=int,
  4779. sep=' ')
  4780. def test_ascii(self, tmp_filename, decimal_sep_localization):
  4781. self._check_from(
  4782. b'1 , 2 , 3 , 4', [1., 2., 3., 4.], tmp_filename, sep=',')
  4783. self._check_from(
  4784. b'1,2,3,4', [1., 2., 3., 4.], tmp_filename, dtype=float, sep=',')
  4785. def test_malformed(self, tmp_filename, decimal_sep_localization):
  4786. with assert_warns(DeprecationWarning):
  4787. self._check_from(
  4788. b'1.234 1,234', [1.234, 1.], tmp_filename, sep=' ')
  4789. def test_long_sep(self, tmp_filename):
  4790. self._check_from(
  4791. b'1_x_3_x_4_x_5', [1, 3, 4, 5], tmp_filename, sep='_x_')
  4792. def test_dtype(self, tmp_filename):
  4793. v = np.array([1, 2, 3, 4], dtype=np.int_)
  4794. self._check_from(b'1,2,3,4', v, tmp_filename, sep=',', dtype=np.int_)
  4795. def test_dtype_bool(self, tmp_filename):
  4796. # can't use _check_from because fromstring can't handle True/False
  4797. v = np.array([True, False, True, False], dtype=np.bool_)
  4798. s = b'1,0,-2.3,0'
  4799. with open(tmp_filename, 'wb') as f:
  4800. f.write(s)
  4801. y = np.fromfile(tmp_filename, sep=',', dtype=np.bool_)
  4802. assert_(y.dtype == '?')
  4803. assert_array_equal(y, v)
  4804. def test_tofile_sep(self, tmp_filename, decimal_sep_localization):
  4805. x = np.array([1.51, 2, 3.51, 4], dtype=float)
  4806. with open(tmp_filename, 'w') as f:
  4807. x.tofile(f, sep=',')
  4808. with open(tmp_filename, 'r') as f:
  4809. s = f.read()
  4810. #assert_equal(s, '1.51,2.0,3.51,4.0')
  4811. y = np.array([float(p) for p in s.split(',')])
  4812. assert_array_equal(x,y)
  4813. def test_tofile_format(self, tmp_filename, decimal_sep_localization):
  4814. x = np.array([1.51, 2, 3.51, 4], dtype=float)
  4815. with open(tmp_filename, 'w') as f:
  4816. x.tofile(f, sep=',', format='%.2f')
  4817. with open(tmp_filename, 'r') as f:
  4818. s = f.read()
  4819. assert_equal(s, '1.51,2.00,3.51,4.00')
  4820. def test_tofile_cleanup(self, tmp_filename):
  4821. x = np.zeros((10), dtype=object)
  4822. with open(tmp_filename, 'wb') as f:
  4823. assert_raises(OSError, lambda: x.tofile(f, sep=''))
  4824. # Dup-ed file handle should be closed or remove will fail on Windows OS
  4825. os.remove(tmp_filename)
  4826. # Also make sure that we close the Python handle
  4827. assert_raises(OSError, lambda: x.tofile(tmp_filename))
  4828. os.remove(tmp_filename)
  4829. def test_fromfile_subarray_binary(self, tmp_filename):
  4830. # Test subarray dtypes which are absorbed into the shape
  4831. x = np.arange(24, dtype="i4").reshape(2, 3, 4)
  4832. x.tofile(tmp_filename)
  4833. res = np.fromfile(tmp_filename, dtype="(3,4)i4")
  4834. assert_array_equal(x, res)
  4835. x_str = x.tobytes()
  4836. with assert_warns(DeprecationWarning):
  4837. # binary fromstring is deprecated
  4838. res = np.fromstring(x_str, dtype="(3,4)i4")
  4839. assert_array_equal(x, res)
  4840. def test_parsing_subarray_unsupported(self, tmp_filename):
  4841. # We currently do not support parsing subarray dtypes
  4842. data = "12,42,13," * 50
  4843. with pytest.raises(ValueError):
  4844. expected = np.fromstring(data, dtype="(3,)i", sep=",")
  4845. with open(tmp_filename, "w") as f:
  4846. f.write(data)
  4847. with pytest.raises(ValueError):
  4848. np.fromfile(tmp_filename, dtype="(3,)i", sep=",")
  4849. def test_read_shorter_than_count_subarray(self, tmp_filename):
  4850. # Test that requesting more values does not cause any problems
  4851. # in conjunction with subarray dimensions being absorbed into the
  4852. # array dimension.
  4853. expected = np.arange(511 * 10, dtype="i").reshape(-1, 10)
  4854. binary = expected.tobytes()
  4855. with pytest.raises(ValueError):
  4856. with pytest.warns(DeprecationWarning):
  4857. np.fromstring(binary, dtype="(10,)i", count=10000)
  4858. expected.tofile(tmp_filename)
  4859. res = np.fromfile(tmp_filename, dtype="(10,)i", count=10000)
  4860. assert_array_equal(res, expected)
  4861. class TestFromBuffer:
  4862. @pytest.mark.parametrize('byteorder', ['<', '>'])
  4863. @pytest.mark.parametrize('dtype', [float, int, complex])
  4864. def test_basic(self, byteorder, dtype):
  4865. dt = np.dtype(dtype).newbyteorder(byteorder)
  4866. x = (np.random.random((4, 7)) * 5).astype(dt)
  4867. buf = x.tobytes()
  4868. assert_array_equal(np.frombuffer(buf, dtype=dt), x.flat)
  4869. @pytest.mark.parametrize("obj", [np.arange(10), b"12345678"])
  4870. def test_array_base(self, obj):
  4871. # Objects (including NumPy arrays), which do not use the
  4872. # `release_buffer` slot should be directly used as a base object.
  4873. # See also gh-21612
  4874. new = np.frombuffer(obj)
  4875. assert new.base is obj
  4876. def test_empty(self):
  4877. assert_array_equal(np.frombuffer(b''), np.array([]))
  4878. @pytest.mark.skipif(IS_PYPY,
  4879. reason="PyPy's memoryview currently does not track exports. See: "
  4880. "https://foss.heptapod.net/pypy/pypy/-/issues/3724")
  4881. def test_mmap_close(self):
  4882. # The old buffer protocol was not safe for some things that the new
  4883. # one is. But `frombuffer` always used the old one for a long time.
  4884. # Checks that it is safe with the new one (using memoryviews)
  4885. with tempfile.TemporaryFile(mode='wb') as tmp:
  4886. tmp.write(b"asdf")
  4887. tmp.flush()
  4888. mm = mmap.mmap(tmp.fileno(), 0)
  4889. arr = np.frombuffer(mm, dtype=np.uint8)
  4890. with pytest.raises(BufferError):
  4891. mm.close() # cannot close while array uses the buffer
  4892. del arr
  4893. mm.close()
  4894. class TestFlat:
  4895. def setup_method(self):
  4896. a0 = np.arange(20.0)
  4897. a = a0.reshape(4, 5)
  4898. a0.shape = (4, 5)
  4899. a.flags.writeable = False
  4900. self.a = a
  4901. self.b = a[::2, ::2]
  4902. self.a0 = a0
  4903. self.b0 = a0[::2, ::2]
  4904. def test_contiguous(self):
  4905. testpassed = False
  4906. try:
  4907. self.a.flat[12] = 100.0
  4908. except ValueError:
  4909. testpassed = True
  4910. assert_(testpassed)
  4911. assert_(self.a.flat[12] == 12.0)
  4912. def test_discontiguous(self):
  4913. testpassed = False
  4914. try:
  4915. self.b.flat[4] = 100.0
  4916. except ValueError:
  4917. testpassed = True
  4918. assert_(testpassed)
  4919. assert_(self.b.flat[4] == 12.0)
  4920. def test___array__(self):
  4921. c = self.a.flat.__array__()
  4922. d = self.b.flat.__array__()
  4923. e = self.a0.flat.__array__()
  4924. f = self.b0.flat.__array__()
  4925. assert_(c.flags.writeable is False)
  4926. assert_(d.flags.writeable is False)
  4927. assert_(e.flags.writeable is True)
  4928. assert_(f.flags.writeable is False)
  4929. assert_(c.flags.writebackifcopy is False)
  4930. assert_(d.flags.writebackifcopy is False)
  4931. assert_(e.flags.writebackifcopy is False)
  4932. assert_(f.flags.writebackifcopy is False)
  4933. @pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
  4934. def test_refcount(self):
  4935. # includes regression test for reference count error gh-13165
  4936. inds = [np.intp(0), np.array([True]*self.a.size), np.array([0]), None]
  4937. indtype = np.dtype(np.intp)
  4938. rc_indtype = sys.getrefcount(indtype)
  4939. for ind in inds:
  4940. rc_ind = sys.getrefcount(ind)
  4941. for _ in range(100):
  4942. try:
  4943. self.a.flat[ind]
  4944. except IndexError:
  4945. pass
  4946. assert_(abs(sys.getrefcount(ind) - rc_ind) < 50)
  4947. assert_(abs(sys.getrefcount(indtype) - rc_indtype) < 50)
  4948. def test_index_getset(self):
  4949. it = np.arange(10).reshape(2, 1, 5).flat
  4950. with pytest.raises(AttributeError):
  4951. it.index = 10
  4952. for _ in it:
  4953. pass
  4954. # Check the value of `.index` is updated correctly (see also gh-19153)
  4955. # If the type was incorrect, this would show up on big-endian machines
  4956. assert it.index == it.base.size
  4957. class TestResize:
  4958. @_no_tracing
  4959. def test_basic(self):
  4960. x = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
  4961. if IS_PYPY:
  4962. x.resize((5, 5), refcheck=False)
  4963. else:
  4964. x.resize((5, 5))
  4965. assert_array_equal(x.flat[:9],
  4966. np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]).flat)
  4967. assert_array_equal(x[9:].flat, 0)
  4968. def test_check_reference(self):
  4969. x = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
  4970. y = x
  4971. assert_raises(ValueError, x.resize, (5, 1))
  4972. del y # avoid pyflakes unused variable warning.
  4973. @_no_tracing
  4974. def test_int_shape(self):
  4975. x = np.eye(3)
  4976. if IS_PYPY:
  4977. x.resize(3, refcheck=False)
  4978. else:
  4979. x.resize(3)
  4980. assert_array_equal(x, np.eye(3)[0,:])
  4981. def test_none_shape(self):
  4982. x = np.eye(3)
  4983. x.resize(None)
  4984. assert_array_equal(x, np.eye(3))
  4985. x.resize()
  4986. assert_array_equal(x, np.eye(3))
  4987. def test_0d_shape(self):
  4988. # to it multiple times to test it does not break alloc cache gh-9216
  4989. for i in range(10):
  4990. x = np.empty((1,))
  4991. x.resize(())
  4992. assert_equal(x.shape, ())
  4993. assert_equal(x.size, 1)
  4994. x = np.empty(())
  4995. x.resize((1,))
  4996. assert_equal(x.shape, (1,))
  4997. assert_equal(x.size, 1)
  4998. def test_invalid_arguments(self):
  4999. assert_raises(TypeError, np.eye(3).resize, 'hi')
  5000. assert_raises(ValueError, np.eye(3).resize, -1)
  5001. assert_raises(TypeError, np.eye(3).resize, order=1)
  5002. assert_raises(TypeError, np.eye(3).resize, refcheck='hi')
  5003. @_no_tracing
  5004. def test_freeform_shape(self):
  5005. x = np.eye(3)
  5006. if IS_PYPY:
  5007. x.resize(3, 2, 1, refcheck=False)
  5008. else:
  5009. x.resize(3, 2, 1)
  5010. assert_(x.shape == (3, 2, 1))
  5011. @_no_tracing
  5012. def test_zeros_appended(self):
  5013. x = np.eye(3)
  5014. if IS_PYPY:
  5015. x.resize(2, 3, 3, refcheck=False)
  5016. else:
  5017. x.resize(2, 3, 3)
  5018. assert_array_equal(x[0], np.eye(3))
  5019. assert_array_equal(x[1], np.zeros((3, 3)))
  5020. @_no_tracing
  5021. def test_obj_obj(self):
  5022. # check memory is initialized on resize, gh-4857
  5023. a = np.ones(10, dtype=[('k', object, 2)])
  5024. if IS_PYPY:
  5025. a.resize(15, refcheck=False)
  5026. else:
  5027. a.resize(15,)
  5028. assert_equal(a.shape, (15,))
  5029. assert_array_equal(a['k'][-5:], 0)
  5030. assert_array_equal(a['k'][:-5], 1)
  5031. def test_empty_view(self):
  5032. # check that sizes containing a zero don't trigger a reallocate for
  5033. # already empty arrays
  5034. x = np.zeros((10, 0), int)
  5035. x_view = x[...]
  5036. x_view.resize((0, 10))
  5037. x_view.resize((0, 100))
  5038. def test_check_weakref(self):
  5039. x = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
  5040. xref = weakref.ref(x)
  5041. assert_raises(ValueError, x.resize, (5, 1))
  5042. del xref # avoid pyflakes unused variable warning.
  5043. class TestRecord:
  5044. def test_field_rename(self):
  5045. dt = np.dtype([('f', float), ('i', int)])
  5046. dt.names = ['p', 'q']
  5047. assert_equal(dt.names, ['p', 'q'])
  5048. def test_multiple_field_name_occurrence(self):
  5049. def test_dtype_init():
  5050. np.dtype([("A", "f8"), ("B", "f8"), ("A", "f8")])
  5051. # Error raised when multiple fields have the same name
  5052. assert_raises(ValueError, test_dtype_init)
  5053. def test_bytes_fields(self):
  5054. # Bytes are not allowed in field names and not recognized in titles
  5055. # on Py3
  5056. assert_raises(TypeError, np.dtype, [(b'a', int)])
  5057. assert_raises(TypeError, np.dtype, [(('b', b'a'), int)])
  5058. dt = np.dtype([((b'a', 'b'), int)])
  5059. assert_raises(TypeError, dt.__getitem__, b'a')
  5060. x = np.array([(1,), (2,), (3,)], dtype=dt)
  5061. assert_raises(IndexError, x.__getitem__, b'a')
  5062. y = x[0]
  5063. assert_raises(IndexError, y.__getitem__, b'a')
  5064. def test_multiple_field_name_unicode(self):
  5065. def test_dtype_unicode():
  5066. np.dtype([("\u20B9", "f8"), ("B", "f8"), ("\u20B9", "f8")])
  5067. # Error raised when multiple fields have the same name(unicode included)
  5068. assert_raises(ValueError, test_dtype_unicode)
  5069. def test_fromarrays_unicode(self):
  5070. # A single name string provided to fromarrays() is allowed to be unicode
  5071. # on both Python 2 and 3:
  5072. x = np.core.records.fromarrays(
  5073. [[0], [1]], names='a,b', formats='i4,i4')
  5074. assert_equal(x['a'][0], 0)
  5075. assert_equal(x['b'][0], 1)
  5076. def test_unicode_order(self):
  5077. # Test that we can sort with order as a unicode field name in both Python 2 and
  5078. # 3:
  5079. name = 'b'
  5080. x = np.array([1, 3, 2], dtype=[(name, int)])
  5081. x.sort(order=name)
  5082. assert_equal(x['b'], np.array([1, 2, 3]))
  5083. def test_field_names(self):
  5084. # Test unicode and 8-bit / byte strings can be used
  5085. a = np.zeros((1,), dtype=[('f1', 'i4'),
  5086. ('f2', 'i4'),
  5087. ('f3', [('sf1', 'i4')])])
  5088. # byte string indexing fails gracefully
  5089. assert_raises(IndexError, a.__setitem__, b'f1', 1)
  5090. assert_raises(IndexError, a.__getitem__, b'f1')
  5091. assert_raises(IndexError, a['f1'].__setitem__, b'sf1', 1)
  5092. assert_raises(IndexError, a['f1'].__getitem__, b'sf1')
  5093. b = a.copy()
  5094. fn1 = str('f1')
  5095. b[fn1] = 1
  5096. assert_equal(b[fn1], 1)
  5097. fnn = str('not at all')
  5098. assert_raises(ValueError, b.__setitem__, fnn, 1)
  5099. assert_raises(ValueError, b.__getitem__, fnn)
  5100. b[0][fn1] = 2
  5101. assert_equal(b[fn1], 2)
  5102. # Subfield
  5103. assert_raises(ValueError, b[0].__setitem__, fnn, 1)
  5104. assert_raises(ValueError, b[0].__getitem__, fnn)
  5105. # Subfield
  5106. fn3 = str('f3')
  5107. sfn1 = str('sf1')
  5108. b[fn3][sfn1] = 1
  5109. assert_equal(b[fn3][sfn1], 1)
  5110. assert_raises(ValueError, b[fn3].__setitem__, fnn, 1)
  5111. assert_raises(ValueError, b[fn3].__getitem__, fnn)
  5112. # multiple subfields
  5113. fn2 = str('f2')
  5114. b[fn2] = 3
  5115. assert_equal(b[['f1', 'f2']][0].tolist(), (2, 3))
  5116. assert_equal(b[['f2', 'f1']][0].tolist(), (3, 2))
  5117. assert_equal(b[['f1', 'f3']][0].tolist(), (2, (1,)))
  5118. # non-ascii unicode field indexing is well behaved
  5119. assert_raises(ValueError, a.__setitem__, '\u03e0', 1)
  5120. assert_raises(ValueError, a.__getitem__, '\u03e0')
  5121. def test_record_hash(self):
  5122. a = np.array([(1, 2), (1, 2)], dtype='i1,i2')
  5123. a.flags.writeable = False
  5124. b = np.array([(1, 2), (3, 4)], dtype=[('num1', 'i1'), ('num2', 'i2')])
  5125. b.flags.writeable = False
  5126. c = np.array([(1, 2), (3, 4)], dtype='i1,i2')
  5127. c.flags.writeable = False
  5128. assert_(hash(a[0]) == hash(a[1]))
  5129. assert_(hash(a[0]) == hash(b[0]))
  5130. assert_(hash(a[0]) != hash(b[1]))
  5131. assert_(hash(c[0]) == hash(a[0]) and c[0] == a[0])
  5132. def test_record_no_hash(self):
  5133. a = np.array([(1, 2), (1, 2)], dtype='i1,i2')
  5134. assert_raises(TypeError, hash, a[0])
  5135. def test_empty_structure_creation(self):
  5136. # make sure these do not raise errors (gh-5631)
  5137. np.array([()], dtype={'names': [], 'formats': [],
  5138. 'offsets': [], 'itemsize': 12})
  5139. np.array([(), (), (), (), ()], dtype={'names': [], 'formats': [],
  5140. 'offsets': [], 'itemsize': 12})
  5141. def test_multifield_indexing_view(self):
  5142. a = np.ones(3, dtype=[('a', 'i4'), ('b', 'f4'), ('c', 'u4')])
  5143. v = a[['a', 'c']]
  5144. assert_(v.base is a)
  5145. assert_(v.dtype == np.dtype({'names': ['a', 'c'],
  5146. 'formats': ['i4', 'u4'],
  5147. 'offsets': [0, 8]}))
  5148. v[:] = (4,5)
  5149. assert_equal(a[0].item(), (4, 1, 5))
  5150. class TestView:
  5151. def test_basic(self):
  5152. x = np.array([(1, 2, 3, 4), (5, 6, 7, 8)],
  5153. dtype=[('r', np.int8), ('g', np.int8),
  5154. ('b', np.int8), ('a', np.int8)])
  5155. # We must be specific about the endianness here:
  5156. y = x.view(dtype='<i4')
  5157. # ... and again without the keyword.
  5158. z = x.view('<i4')
  5159. assert_array_equal(y, z)
  5160. assert_array_equal(y, [67305985, 134678021])
  5161. def _mean(a, **args):
  5162. return a.mean(**args)
  5163. def _var(a, **args):
  5164. return a.var(**args)
  5165. def _std(a, **args):
  5166. return a.std(**args)
  5167. class TestStats:
  5168. funcs = [_mean, _var, _std]
  5169. def setup_method(self):
  5170. np.random.seed(range(3))
  5171. self.rmat = np.random.random((4, 5))
  5172. self.cmat = self.rmat + 1j * self.rmat
  5173. self.omat = np.array([Decimal(repr(r)) for r in self.rmat.flat])
  5174. self.omat = self.omat.reshape(4, 5)
  5175. def test_python_type(self):
  5176. for x in (np.float16(1.), 1, 1., 1+0j):
  5177. assert_equal(np.mean([x]), 1.)
  5178. assert_equal(np.std([x]), 0.)
  5179. assert_equal(np.var([x]), 0.)
  5180. def test_keepdims(self):
  5181. mat = np.eye(3)
  5182. for f in self.funcs:
  5183. for axis in [0, 1]:
  5184. res = f(mat, axis=axis, keepdims=True)
  5185. assert_(res.ndim == mat.ndim)
  5186. assert_(res.shape[axis] == 1)
  5187. for axis in [None]:
  5188. res = f(mat, axis=axis, keepdims=True)
  5189. assert_(res.shape == (1, 1))
  5190. def test_out(self):
  5191. mat = np.eye(3)
  5192. for f in self.funcs:
  5193. out = np.zeros(3)
  5194. tgt = f(mat, axis=1)
  5195. res = f(mat, axis=1, out=out)
  5196. assert_almost_equal(res, out)
  5197. assert_almost_equal(res, tgt)
  5198. out = np.empty(2)
  5199. assert_raises(ValueError, f, mat, axis=1, out=out)
  5200. out = np.empty((2, 2))
  5201. assert_raises(ValueError, f, mat, axis=1, out=out)
  5202. def test_dtype_from_input(self):
  5203. icodes = np.typecodes['AllInteger']
  5204. fcodes = np.typecodes['AllFloat']
  5205. # object type
  5206. for f in self.funcs:
  5207. mat = np.array([[Decimal(1)]*3]*3)
  5208. tgt = mat.dtype.type
  5209. res = f(mat, axis=1).dtype.type
  5210. assert_(res is tgt)
  5211. # scalar case
  5212. res = type(f(mat, axis=None))
  5213. assert_(res is Decimal)
  5214. # integer types
  5215. for f in self.funcs:
  5216. for c in icodes:
  5217. mat = np.eye(3, dtype=c)
  5218. tgt = np.float64
  5219. res = f(mat, axis=1).dtype.type
  5220. assert_(res is tgt)
  5221. # scalar case
  5222. res = f(mat, axis=None).dtype.type
  5223. assert_(res is tgt)
  5224. # mean for float types
  5225. for f in [_mean]:
  5226. for c in fcodes:
  5227. mat = np.eye(3, dtype=c)
  5228. tgt = mat.dtype.type
  5229. res = f(mat, axis=1).dtype.type
  5230. assert_(res is tgt)
  5231. # scalar case
  5232. res = f(mat, axis=None).dtype.type
  5233. assert_(res is tgt)
  5234. # var, std for float types
  5235. for f in [_var, _std]:
  5236. for c in fcodes:
  5237. mat = np.eye(3, dtype=c)
  5238. # deal with complex types
  5239. tgt = mat.real.dtype.type
  5240. res = f(mat, axis=1).dtype.type
  5241. assert_(res is tgt)
  5242. # scalar case
  5243. res = f(mat, axis=None).dtype.type
  5244. assert_(res is tgt)
  5245. def test_dtype_from_dtype(self):
  5246. mat = np.eye(3)
  5247. # stats for integer types
  5248. # FIXME:
  5249. # this needs definition as there are lots places along the line
  5250. # where type casting may take place.
  5251. # for f in self.funcs:
  5252. # for c in np.typecodes['AllInteger']:
  5253. # tgt = np.dtype(c).type
  5254. # res = f(mat, axis=1, dtype=c).dtype.type
  5255. # assert_(res is tgt)
  5256. # # scalar case
  5257. # res = f(mat, axis=None, dtype=c).dtype.type
  5258. # assert_(res is tgt)
  5259. # stats for float types
  5260. for f in self.funcs:
  5261. for c in np.typecodes['AllFloat']:
  5262. tgt = np.dtype(c).type
  5263. res = f(mat, axis=1, dtype=c).dtype.type
  5264. assert_(res is tgt)
  5265. # scalar case
  5266. res = f(mat, axis=None, dtype=c).dtype.type
  5267. assert_(res is tgt)
  5268. def test_ddof(self):
  5269. for f in [_var]:
  5270. for ddof in range(3):
  5271. dim = self.rmat.shape[1]
  5272. tgt = f(self.rmat, axis=1) * dim
  5273. res = f(self.rmat, axis=1, ddof=ddof) * (dim - ddof)
  5274. for f in [_std]:
  5275. for ddof in range(3):
  5276. dim = self.rmat.shape[1]
  5277. tgt = f(self.rmat, axis=1) * np.sqrt(dim)
  5278. res = f(self.rmat, axis=1, ddof=ddof) * np.sqrt(dim - ddof)
  5279. assert_almost_equal(res, tgt)
  5280. assert_almost_equal(res, tgt)
  5281. def test_ddof_too_big(self):
  5282. dim = self.rmat.shape[1]
  5283. for f in [_var, _std]:
  5284. for ddof in range(dim, dim + 2):
  5285. with warnings.catch_warnings(record=True) as w:
  5286. warnings.simplefilter('always')
  5287. res = f(self.rmat, axis=1, ddof=ddof)
  5288. assert_(not (res < 0).any())
  5289. assert_(len(w) > 0)
  5290. assert_(issubclass(w[0].category, RuntimeWarning))
  5291. def test_empty(self):
  5292. A = np.zeros((0, 3))
  5293. for f in self.funcs:
  5294. for axis in [0, None]:
  5295. with warnings.catch_warnings(record=True) as w:
  5296. warnings.simplefilter('always')
  5297. assert_(np.isnan(f(A, axis=axis)).all())
  5298. assert_(len(w) > 0)
  5299. assert_(issubclass(w[0].category, RuntimeWarning))
  5300. for axis in [1]:
  5301. with warnings.catch_warnings(record=True) as w:
  5302. warnings.simplefilter('always')
  5303. assert_equal(f(A, axis=axis), np.zeros([]))
  5304. def test_mean_values(self):
  5305. for mat in [self.rmat, self.cmat, self.omat]:
  5306. for axis in [0, 1]:
  5307. tgt = mat.sum(axis=axis)
  5308. res = _mean(mat, axis=axis) * mat.shape[axis]
  5309. assert_almost_equal(res, tgt)
  5310. for axis in [None]:
  5311. tgt = mat.sum(axis=axis)
  5312. res = _mean(mat, axis=axis) * np.prod(mat.shape)
  5313. assert_almost_equal(res, tgt)
  5314. def test_mean_float16(self):
  5315. # This fail if the sum inside mean is done in float16 instead
  5316. # of float32.
  5317. assert_(_mean(np.ones(100000, dtype='float16')) == 1)
  5318. def test_mean_axis_error(self):
  5319. # Ensure that AxisError is raised instead of IndexError when axis is
  5320. # out of bounds, see gh-15817.
  5321. with assert_raises(np.core._exceptions.AxisError):
  5322. np.arange(10).mean(axis=2)
  5323. def test_mean_where(self):
  5324. a = np.arange(16).reshape((4, 4))
  5325. wh_full = np.array([[False, True, False, True],
  5326. [True, False, True, False],
  5327. [True, True, False, False],
  5328. [False, False, True, True]])
  5329. wh_partial = np.array([[False],
  5330. [True],
  5331. [True],
  5332. [False]])
  5333. _cases = [(1, True, [1.5, 5.5, 9.5, 13.5]),
  5334. (0, wh_full, [6., 5., 10., 9.]),
  5335. (1, wh_full, [2., 5., 8.5, 14.5]),
  5336. (0, wh_partial, [6., 7., 8., 9.])]
  5337. for _ax, _wh, _res in _cases:
  5338. assert_allclose(a.mean(axis=_ax, where=_wh),
  5339. np.array(_res))
  5340. assert_allclose(np.mean(a, axis=_ax, where=_wh),
  5341. np.array(_res))
  5342. a3d = np.arange(16).reshape((2, 2, 4))
  5343. _wh_partial = np.array([False, True, True, False])
  5344. _res = [[1.5, 5.5], [9.5, 13.5]]
  5345. assert_allclose(a3d.mean(axis=2, where=_wh_partial),
  5346. np.array(_res))
  5347. assert_allclose(np.mean(a3d, axis=2, where=_wh_partial),
  5348. np.array(_res))
  5349. with pytest.warns(RuntimeWarning) as w:
  5350. assert_allclose(a.mean(axis=1, where=wh_partial),
  5351. np.array([np.nan, 5.5, 9.5, np.nan]))
  5352. with pytest.warns(RuntimeWarning) as w:
  5353. assert_equal(a.mean(where=False), np.nan)
  5354. with pytest.warns(RuntimeWarning) as w:
  5355. assert_equal(np.mean(a, where=False), np.nan)
  5356. def test_var_values(self):
  5357. for mat in [self.rmat, self.cmat, self.omat]:
  5358. for axis in [0, 1, None]:
  5359. msqr = _mean(mat * mat.conj(), axis=axis)
  5360. mean = _mean(mat, axis=axis)
  5361. tgt = msqr - mean * mean.conjugate()
  5362. res = _var(mat, axis=axis)
  5363. assert_almost_equal(res, tgt)
  5364. @pytest.mark.parametrize(('complex_dtype', 'ndec'), (
  5365. ('complex64', 6),
  5366. ('complex128', 7),
  5367. ('clongdouble', 7),
  5368. ))
  5369. def test_var_complex_values(self, complex_dtype, ndec):
  5370. # Test fast-paths for every builtin complex type
  5371. for axis in [0, 1, None]:
  5372. mat = self.cmat.copy().astype(complex_dtype)
  5373. msqr = _mean(mat * mat.conj(), axis=axis)
  5374. mean = _mean(mat, axis=axis)
  5375. tgt = msqr - mean * mean.conjugate()
  5376. res = _var(mat, axis=axis)
  5377. assert_almost_equal(res, tgt, decimal=ndec)
  5378. def test_var_dimensions(self):
  5379. # _var paths for complex number introduce additions on views that
  5380. # increase dimensions. Ensure this generalizes to higher dims
  5381. mat = np.stack([self.cmat]*3)
  5382. for axis in [0, 1, 2, -1, None]:
  5383. msqr = _mean(mat * mat.conj(), axis=axis)
  5384. mean = _mean(mat, axis=axis)
  5385. tgt = msqr - mean * mean.conjugate()
  5386. res = _var(mat, axis=axis)
  5387. assert_almost_equal(res, tgt)
  5388. def test_var_complex_byteorder(self):
  5389. # Test that var fast-path does not cause failures for complex arrays
  5390. # with non-native byteorder
  5391. cmat = self.cmat.copy().astype('complex128')
  5392. cmat_swapped = cmat.astype(cmat.dtype.newbyteorder())
  5393. assert_almost_equal(cmat.var(), cmat_swapped.var())
  5394. def test_var_axis_error(self):
  5395. # Ensure that AxisError is raised instead of IndexError when axis is
  5396. # out of bounds, see gh-15817.
  5397. with assert_raises(np.core._exceptions.AxisError):
  5398. np.arange(10).var(axis=2)
  5399. def test_var_where(self):
  5400. a = np.arange(25).reshape((5, 5))
  5401. wh_full = np.array([[False, True, False, True, True],
  5402. [True, False, True, True, False],
  5403. [True, True, False, False, True],
  5404. [False, True, True, False, True],
  5405. [True, False, True, True, False]])
  5406. wh_partial = np.array([[False],
  5407. [True],
  5408. [True],
  5409. [False],
  5410. [True]])
  5411. _cases = [(0, True, [50., 50., 50., 50., 50.]),
  5412. (1, True, [2., 2., 2., 2., 2.])]
  5413. for _ax, _wh, _res in _cases:
  5414. assert_allclose(a.var(axis=_ax, where=_wh),
  5415. np.array(_res))
  5416. assert_allclose(np.var(a, axis=_ax, where=_wh),
  5417. np.array(_res))
  5418. a3d = np.arange(16).reshape((2, 2, 4))
  5419. _wh_partial = np.array([False, True, True, False])
  5420. _res = [[0.25, 0.25], [0.25, 0.25]]
  5421. assert_allclose(a3d.var(axis=2, where=_wh_partial),
  5422. np.array(_res))
  5423. assert_allclose(np.var(a3d, axis=2, where=_wh_partial),
  5424. np.array(_res))
  5425. assert_allclose(np.var(a, axis=1, where=wh_full),
  5426. np.var(a[wh_full].reshape((5, 3)), axis=1))
  5427. assert_allclose(np.var(a, axis=0, where=wh_partial),
  5428. np.var(a[wh_partial[:,0]], axis=0))
  5429. with pytest.warns(RuntimeWarning) as w:
  5430. assert_equal(a.var(where=False), np.nan)
  5431. with pytest.warns(RuntimeWarning) as w:
  5432. assert_equal(np.var(a, where=False), np.nan)
  5433. def test_std_values(self):
  5434. for mat in [self.rmat, self.cmat, self.omat]:
  5435. for axis in [0, 1, None]:
  5436. tgt = np.sqrt(_var(mat, axis=axis))
  5437. res = _std(mat, axis=axis)
  5438. assert_almost_equal(res, tgt)
  5439. def test_std_where(self):
  5440. a = np.arange(25).reshape((5,5))[::-1]
  5441. whf = np.array([[False, True, False, True, True],
  5442. [True, False, True, False, True],
  5443. [True, True, False, True, False],
  5444. [True, False, True, True, False],
  5445. [False, True, False, True, True]])
  5446. whp = np.array([[False],
  5447. [False],
  5448. [True],
  5449. [True],
  5450. [False]])
  5451. _cases = [
  5452. (0, True, 7.07106781*np.ones((5))),
  5453. (1, True, 1.41421356*np.ones((5))),
  5454. (0, whf,
  5455. np.array([4.0824829 , 8.16496581, 5., 7.39509973, 8.49836586])),
  5456. (0, whp, 2.5*np.ones((5)))
  5457. ]
  5458. for _ax, _wh, _res in _cases:
  5459. assert_allclose(a.std(axis=_ax, where=_wh), _res)
  5460. assert_allclose(np.std(a, axis=_ax, where=_wh), _res)
  5461. a3d = np.arange(16).reshape((2, 2, 4))
  5462. _wh_partial = np.array([False, True, True, False])
  5463. _res = [[0.5, 0.5], [0.5, 0.5]]
  5464. assert_allclose(a3d.std(axis=2, where=_wh_partial),
  5465. np.array(_res))
  5466. assert_allclose(np.std(a3d, axis=2, where=_wh_partial),
  5467. np.array(_res))
  5468. assert_allclose(a.std(axis=1, where=whf),
  5469. np.std(a[whf].reshape((5,3)), axis=1))
  5470. assert_allclose(np.std(a, axis=1, where=whf),
  5471. (a[whf].reshape((5,3))).std(axis=1))
  5472. assert_allclose(a.std(axis=0, where=whp),
  5473. np.std(a[whp[:,0]], axis=0))
  5474. assert_allclose(np.std(a, axis=0, where=whp),
  5475. (a[whp[:,0]]).std(axis=0))
  5476. with pytest.warns(RuntimeWarning) as w:
  5477. assert_equal(a.std(where=False), np.nan)
  5478. with pytest.warns(RuntimeWarning) as w:
  5479. assert_equal(np.std(a, where=False), np.nan)
  5480. def test_subclass(self):
  5481. class TestArray(np.ndarray):
  5482. def __new__(cls, data, info):
  5483. result = np.array(data)
  5484. result = result.view(cls)
  5485. result.info = info
  5486. return result
  5487. def __array_finalize__(self, obj):
  5488. self.info = getattr(obj, "info", '')
  5489. dat = TestArray([[1, 2, 3, 4], [5, 6, 7, 8]], 'jubba')
  5490. res = dat.mean(1)
  5491. assert_(res.info == dat.info)
  5492. res = dat.std(1)
  5493. assert_(res.info == dat.info)
  5494. res = dat.var(1)
  5495. assert_(res.info == dat.info)
  5496. class TestVdot:
  5497. def test_basic(self):
  5498. dt_numeric = np.typecodes['AllFloat'] + np.typecodes['AllInteger']
  5499. dt_complex = np.typecodes['Complex']
  5500. # test real
  5501. a = np.eye(3)
  5502. for dt in dt_numeric + 'O':
  5503. b = a.astype(dt)
  5504. res = np.vdot(b, b)
  5505. assert_(np.isscalar(res))
  5506. assert_equal(np.vdot(b, b), 3)
  5507. # test complex
  5508. a = np.eye(3) * 1j
  5509. for dt in dt_complex + 'O':
  5510. b = a.astype(dt)
  5511. res = np.vdot(b, b)
  5512. assert_(np.isscalar(res))
  5513. assert_equal(np.vdot(b, b), 3)
  5514. # test boolean
  5515. b = np.eye(3, dtype=bool)
  5516. res = np.vdot(b, b)
  5517. assert_(np.isscalar(res))
  5518. assert_equal(np.vdot(b, b), True)
  5519. def test_vdot_array_order(self):
  5520. a = np.array([[1, 2], [3, 4]], order='C')
  5521. b = np.array([[1, 2], [3, 4]], order='F')
  5522. res = np.vdot(a, a)
  5523. # integer arrays are exact
  5524. assert_equal(np.vdot(a, b), res)
  5525. assert_equal(np.vdot(b, a), res)
  5526. assert_equal(np.vdot(b, b), res)
  5527. def test_vdot_uncontiguous(self):
  5528. for size in [2, 1000]:
  5529. # Different sizes match different branches in vdot.
  5530. a = np.zeros((size, 2, 2))
  5531. b = np.zeros((size, 2, 2))
  5532. a[:, 0, 0] = np.arange(size)
  5533. b[:, 0, 0] = np.arange(size) + 1
  5534. # Make a and b uncontiguous:
  5535. a = a[..., 0]
  5536. b = b[..., 0]
  5537. assert_equal(np.vdot(a, b),
  5538. np.vdot(a.flatten(), b.flatten()))
  5539. assert_equal(np.vdot(a, b.copy()),
  5540. np.vdot(a.flatten(), b.flatten()))
  5541. assert_equal(np.vdot(a.copy(), b),
  5542. np.vdot(a.flatten(), b.flatten()))
  5543. assert_equal(np.vdot(a.copy('F'), b),
  5544. np.vdot(a.flatten(), b.flatten()))
  5545. assert_equal(np.vdot(a, b.copy('F')),
  5546. np.vdot(a.flatten(), b.flatten()))
  5547. class TestDot:
  5548. def setup_method(self):
  5549. np.random.seed(128)
  5550. self.A = np.random.rand(4, 2)
  5551. self.b1 = np.random.rand(2, 1)
  5552. self.b2 = np.random.rand(2)
  5553. self.b3 = np.random.rand(1, 2)
  5554. self.b4 = np.random.rand(4)
  5555. self.N = 7
  5556. def test_dotmatmat(self):
  5557. A = self.A
  5558. res = np.dot(A.transpose(), A)
  5559. tgt = np.array([[1.45046013, 0.86323640],
  5560. [0.86323640, 0.84934569]])
  5561. assert_almost_equal(res, tgt, decimal=self.N)
  5562. def test_dotmatvec(self):
  5563. A, b1 = self.A, self.b1
  5564. res = np.dot(A, b1)
  5565. tgt = np.array([[0.32114320], [0.04889721],
  5566. [0.15696029], [0.33612621]])
  5567. assert_almost_equal(res, tgt, decimal=self.N)
  5568. def test_dotmatvec2(self):
  5569. A, b2 = self.A, self.b2
  5570. res = np.dot(A, b2)
  5571. tgt = np.array([0.29677940, 0.04518649, 0.14468333, 0.31039293])
  5572. assert_almost_equal(res, tgt, decimal=self.N)
  5573. def test_dotvecmat(self):
  5574. A, b4 = self.A, self.b4
  5575. res = np.dot(b4, A)
  5576. tgt = np.array([1.23495091, 1.12222648])
  5577. assert_almost_equal(res, tgt, decimal=self.N)
  5578. def test_dotvecmat2(self):
  5579. b3, A = self.b3, self.A
  5580. res = np.dot(b3, A.transpose())
  5581. tgt = np.array([[0.58793804, 0.08957460, 0.30605758, 0.62716383]])
  5582. assert_almost_equal(res, tgt, decimal=self.N)
  5583. def test_dotvecmat3(self):
  5584. A, b4 = self.A, self.b4
  5585. res = np.dot(A.transpose(), b4)
  5586. tgt = np.array([1.23495091, 1.12222648])
  5587. assert_almost_equal(res, tgt, decimal=self.N)
  5588. def test_dotvecvecouter(self):
  5589. b1, b3 = self.b1, self.b3
  5590. res = np.dot(b1, b3)
  5591. tgt = np.array([[0.20128610, 0.08400440], [0.07190947, 0.03001058]])
  5592. assert_almost_equal(res, tgt, decimal=self.N)
  5593. def test_dotvecvecinner(self):
  5594. b1, b3 = self.b1, self.b3
  5595. res = np.dot(b3, b1)
  5596. tgt = np.array([[ 0.23129668]])
  5597. assert_almost_equal(res, tgt, decimal=self.N)
  5598. def test_dotcolumnvect1(self):
  5599. b1 = np.ones((3, 1))
  5600. b2 = [5.3]
  5601. res = np.dot(b1, b2)
  5602. tgt = np.array([5.3, 5.3, 5.3])
  5603. assert_almost_equal(res, tgt, decimal=self.N)
  5604. def test_dotcolumnvect2(self):
  5605. b1 = np.ones((3, 1)).transpose()
  5606. b2 = [6.2]
  5607. res = np.dot(b2, b1)
  5608. tgt = np.array([6.2, 6.2, 6.2])
  5609. assert_almost_equal(res, tgt, decimal=self.N)
  5610. def test_dotvecscalar(self):
  5611. np.random.seed(100)
  5612. b1 = np.random.rand(1, 1)
  5613. b2 = np.random.rand(1, 4)
  5614. res = np.dot(b1, b2)
  5615. tgt = np.array([[0.15126730, 0.23068496, 0.45905553, 0.00256425]])
  5616. assert_almost_equal(res, tgt, decimal=self.N)
  5617. def test_dotvecscalar2(self):
  5618. np.random.seed(100)
  5619. b1 = np.random.rand(4, 1)
  5620. b2 = np.random.rand(1, 1)
  5621. res = np.dot(b1, b2)
  5622. tgt = np.array([[0.00256425],[0.00131359],[0.00200324],[ 0.00398638]])
  5623. assert_almost_equal(res, tgt, decimal=self.N)
  5624. def test_all(self):
  5625. dims = [(), (1,), (1, 1)]
  5626. dout = [(), (1,), (1, 1), (1,), (), (1,), (1, 1), (1,), (1, 1)]
  5627. for dim, (dim1, dim2) in zip(dout, itertools.product(dims, dims)):
  5628. b1 = np.zeros(dim1)
  5629. b2 = np.zeros(dim2)
  5630. res = np.dot(b1, b2)
  5631. tgt = np.zeros(dim)
  5632. assert_(res.shape == tgt.shape)
  5633. assert_almost_equal(res, tgt, decimal=self.N)
  5634. def test_vecobject(self):
  5635. class Vec:
  5636. def __init__(self, sequence=None):
  5637. if sequence is None:
  5638. sequence = []
  5639. self.array = np.array(sequence)
  5640. def __add__(self, other):
  5641. out = Vec()
  5642. out.array = self.array + other.array
  5643. return out
  5644. def __sub__(self, other):
  5645. out = Vec()
  5646. out.array = self.array - other.array
  5647. return out
  5648. def __mul__(self, other): # with scalar
  5649. out = Vec(self.array.copy())
  5650. out.array *= other
  5651. return out
  5652. def __rmul__(self, other):
  5653. return self*other
  5654. U_non_cont = np.transpose([[1., 1.], [1., 2.]])
  5655. U_cont = np.ascontiguousarray(U_non_cont)
  5656. x = np.array([Vec([1., 0.]), Vec([0., 1.])])
  5657. zeros = np.array([Vec([0., 0.]), Vec([0., 0.])])
  5658. zeros_test = np.dot(U_cont, x) - np.dot(U_non_cont, x)
  5659. assert_equal(zeros[0].array, zeros_test[0].array)
  5660. assert_equal(zeros[1].array, zeros_test[1].array)
  5661. def test_dot_2args(self):
  5662. from numpy.core.multiarray import dot
  5663. a = np.array([[1, 2], [3, 4]], dtype=float)
  5664. b = np.array([[1, 0], [1, 1]], dtype=float)
  5665. c = np.array([[3, 2], [7, 4]], dtype=float)
  5666. d = dot(a, b)
  5667. assert_allclose(c, d)
  5668. def test_dot_3args(self):
  5669. from numpy.core.multiarray import dot
  5670. np.random.seed(22)
  5671. f = np.random.random_sample((1024, 16))
  5672. v = np.random.random_sample((16, 32))
  5673. r = np.empty((1024, 32))
  5674. for i in range(12):
  5675. dot(f, v, r)
  5676. if HAS_REFCOUNT:
  5677. assert_equal(sys.getrefcount(r), 2)
  5678. r2 = dot(f, v, out=None)
  5679. assert_array_equal(r2, r)
  5680. assert_(r is dot(f, v, out=r))
  5681. v = v[:, 0].copy() # v.shape == (16,)
  5682. r = r[:, 0].copy() # r.shape == (1024,)
  5683. r2 = dot(f, v)
  5684. assert_(r is dot(f, v, r))
  5685. assert_array_equal(r2, r)
  5686. def test_dot_3args_errors(self):
  5687. from numpy.core.multiarray import dot
  5688. np.random.seed(22)
  5689. f = np.random.random_sample((1024, 16))
  5690. v = np.random.random_sample((16, 32))
  5691. r = np.empty((1024, 31))
  5692. assert_raises(ValueError, dot, f, v, r)
  5693. r = np.empty((1024,))
  5694. assert_raises(ValueError, dot, f, v, r)
  5695. r = np.empty((32,))
  5696. assert_raises(ValueError, dot, f, v, r)
  5697. r = np.empty((32, 1024))
  5698. assert_raises(ValueError, dot, f, v, r)
  5699. assert_raises(ValueError, dot, f, v, r.T)
  5700. r = np.empty((1024, 64))
  5701. assert_raises(ValueError, dot, f, v, r[:, ::2])
  5702. assert_raises(ValueError, dot, f, v, r[:, :32])
  5703. r = np.empty((1024, 32), dtype=np.float32)
  5704. assert_raises(ValueError, dot, f, v, r)
  5705. r = np.empty((1024, 32), dtype=int)
  5706. assert_raises(ValueError, dot, f, v, r)
  5707. def test_dot_array_order(self):
  5708. a = np.array([[1, 2], [3, 4]], order='C')
  5709. b = np.array([[1, 2], [3, 4]], order='F')
  5710. res = np.dot(a, a)
  5711. # integer arrays are exact
  5712. assert_equal(np.dot(a, b), res)
  5713. assert_equal(np.dot(b, a), res)
  5714. assert_equal(np.dot(b, b), res)
  5715. def test_accelerate_framework_sgemv_fix(self):
  5716. def aligned_array(shape, align, dtype, order='C'):
  5717. d = dtype(0)
  5718. N = np.prod(shape)
  5719. tmp = np.zeros(N * d.nbytes + align, dtype=np.uint8)
  5720. address = tmp.__array_interface__["data"][0]
  5721. for offset in range(align):
  5722. if (address + offset) % align == 0:
  5723. break
  5724. tmp = tmp[offset:offset+N*d.nbytes].view(dtype=dtype)
  5725. return tmp.reshape(shape, order=order)
  5726. def as_aligned(arr, align, dtype, order='C'):
  5727. aligned = aligned_array(arr.shape, align, dtype, order)
  5728. aligned[:] = arr[:]
  5729. return aligned
  5730. def assert_dot_close(A, X, desired):
  5731. assert_allclose(np.dot(A, X), desired, rtol=1e-5, atol=1e-7)
  5732. m = aligned_array(100, 15, np.float32)
  5733. s = aligned_array((100, 100), 15, np.float32)
  5734. np.dot(s, m) # this will always segfault if the bug is present
  5735. testdata = itertools.product((15, 32), (10000,), (200, 89), ('C', 'F'))
  5736. for align, m, n, a_order in testdata:
  5737. # Calculation in double precision
  5738. A_d = np.random.rand(m, n)
  5739. X_d = np.random.rand(n)
  5740. desired = np.dot(A_d, X_d)
  5741. # Calculation with aligned single precision
  5742. A_f = as_aligned(A_d, align, np.float32, order=a_order)
  5743. X_f = as_aligned(X_d, align, np.float32)
  5744. assert_dot_close(A_f, X_f, desired)
  5745. # Strided A rows
  5746. A_d_2 = A_d[::2]
  5747. desired = np.dot(A_d_2, X_d)
  5748. A_f_2 = A_f[::2]
  5749. assert_dot_close(A_f_2, X_f, desired)
  5750. # Strided A columns, strided X vector
  5751. A_d_22 = A_d_2[:, ::2]
  5752. X_d_2 = X_d[::2]
  5753. desired = np.dot(A_d_22, X_d_2)
  5754. A_f_22 = A_f_2[:, ::2]
  5755. X_f_2 = X_f[::2]
  5756. assert_dot_close(A_f_22, X_f_2, desired)
  5757. # Check the strides are as expected
  5758. if a_order == 'F':
  5759. assert_equal(A_f_22.strides, (8, 8 * m))
  5760. else:
  5761. assert_equal(A_f_22.strides, (8 * n, 8))
  5762. assert_equal(X_f_2.strides, (8,))
  5763. # Strides in A rows + cols only
  5764. X_f_2c = as_aligned(X_f_2, align, np.float32)
  5765. assert_dot_close(A_f_22, X_f_2c, desired)
  5766. # Strides just in A cols
  5767. A_d_12 = A_d[:, ::2]
  5768. desired = np.dot(A_d_12, X_d_2)
  5769. A_f_12 = A_f[:, ::2]
  5770. assert_dot_close(A_f_12, X_f_2c, desired)
  5771. # Strides in A cols and X
  5772. assert_dot_close(A_f_12, X_f_2, desired)
  5773. @pytest.mark.slow
  5774. @pytest.mark.parametrize("dtype", [np.float64, np.complex128])
  5775. @requires_memory(free_bytes=18e9) # complex case needs 18GiB+
  5776. def test_huge_vectordot(self, dtype):
  5777. # Large vector multiplications are chunked with 32bit BLAS
  5778. # Test that the chunking does the right thing, see also gh-22262
  5779. data = np.ones(2**30+100, dtype=dtype)
  5780. res = np.dot(data, data)
  5781. assert res == 2**30+100
  5782. def test_dtype_discovery_fails(self):
  5783. # See gh-14247, error checking was missing for failed dtype discovery
  5784. class BadObject(object):
  5785. def __array__(self):
  5786. raise TypeError("just this tiny mint leaf")
  5787. with pytest.raises(TypeError):
  5788. np.dot(BadObject(), BadObject())
  5789. with pytest.raises(TypeError):
  5790. np.dot(3.0, BadObject())
  5791. class MatmulCommon:
  5792. """Common tests for '@' operator and numpy.matmul.
  5793. """
  5794. # Should work with these types. Will want to add
  5795. # "O" at some point
  5796. types = "?bhilqBHILQefdgFDGO"
  5797. def test_exceptions(self):
  5798. dims = [
  5799. ((1,), (2,)), # mismatched vector vector
  5800. ((2, 1,), (2,)), # mismatched matrix vector
  5801. ((2,), (1, 2)), # mismatched vector matrix
  5802. ((1, 2), (3, 1)), # mismatched matrix matrix
  5803. ((1,), ()), # vector scalar
  5804. ((), (1)), # scalar vector
  5805. ((1, 1), ()), # matrix scalar
  5806. ((), (1, 1)), # scalar matrix
  5807. ((2, 2, 1), (3, 1, 2)), # cannot broadcast
  5808. ]
  5809. for dt, (dm1, dm2) in itertools.product(self.types, dims):
  5810. a = np.ones(dm1, dtype=dt)
  5811. b = np.ones(dm2, dtype=dt)
  5812. assert_raises(ValueError, self.matmul, a, b)
  5813. def test_shapes(self):
  5814. dims = [
  5815. ((1, 1), (2, 1, 1)), # broadcast first argument
  5816. ((2, 1, 1), (1, 1)), # broadcast second argument
  5817. ((2, 1, 1), (2, 1, 1)), # matrix stack sizes match
  5818. ]
  5819. for dt, (dm1, dm2) in itertools.product(self.types, dims):
  5820. a = np.ones(dm1, dtype=dt)
  5821. b = np.ones(dm2, dtype=dt)
  5822. res = self.matmul(a, b)
  5823. assert_(res.shape == (2, 1, 1))
  5824. # vector vector returns scalars.
  5825. for dt in self.types:
  5826. a = np.ones((2,), dtype=dt)
  5827. b = np.ones((2,), dtype=dt)
  5828. c = self.matmul(a, b)
  5829. assert_(np.array(c).shape == ())
  5830. def test_result_types(self):
  5831. mat = np.ones((1,1))
  5832. vec = np.ones((1,))
  5833. for dt in self.types:
  5834. m = mat.astype(dt)
  5835. v = vec.astype(dt)
  5836. for arg in [(m, v), (v, m), (m, m)]:
  5837. res = self.matmul(*arg)
  5838. assert_(res.dtype == dt)
  5839. # vector vector returns scalars
  5840. if dt != "O":
  5841. res = self.matmul(v, v)
  5842. assert_(type(res) is np.dtype(dt).type)
  5843. def test_scalar_output(self):
  5844. vec1 = np.array([2])
  5845. vec2 = np.array([3, 4]).reshape(1, -1)
  5846. tgt = np.array([6, 8])
  5847. for dt in self.types[1:]:
  5848. v1 = vec1.astype(dt)
  5849. v2 = vec2.astype(dt)
  5850. res = self.matmul(v1, v2)
  5851. assert_equal(res, tgt)
  5852. res = self.matmul(v2.T, v1)
  5853. assert_equal(res, tgt)
  5854. # boolean type
  5855. vec = np.array([True, True], dtype='?').reshape(1, -1)
  5856. res = self.matmul(vec[:, 0], vec)
  5857. assert_equal(res, True)
  5858. def test_vector_vector_values(self):
  5859. vec1 = np.array([1, 2])
  5860. vec2 = np.array([3, 4]).reshape(-1, 1)
  5861. tgt1 = np.array([11])
  5862. tgt2 = np.array([[3, 6], [4, 8]])
  5863. for dt in self.types[1:]:
  5864. v1 = vec1.astype(dt)
  5865. v2 = vec2.astype(dt)
  5866. res = self.matmul(v1, v2)
  5867. assert_equal(res, tgt1)
  5868. # no broadcast, we must make v1 into a 2d ndarray
  5869. res = self.matmul(v2, v1.reshape(1, -1))
  5870. assert_equal(res, tgt2)
  5871. # boolean type
  5872. vec = np.array([True, True], dtype='?')
  5873. res = self.matmul(vec, vec)
  5874. assert_equal(res, True)
  5875. def test_vector_matrix_values(self):
  5876. vec = np.array([1, 2])
  5877. mat1 = np.array([[1, 2], [3, 4]])
  5878. mat2 = np.stack([mat1]*2, axis=0)
  5879. tgt1 = np.array([7, 10])
  5880. tgt2 = np.stack([tgt1]*2, axis=0)
  5881. for dt in self.types[1:]:
  5882. v = vec.astype(dt)
  5883. m1 = mat1.astype(dt)
  5884. m2 = mat2.astype(dt)
  5885. res = self.matmul(v, m1)
  5886. assert_equal(res, tgt1)
  5887. res = self.matmul(v, m2)
  5888. assert_equal(res, tgt2)
  5889. # boolean type
  5890. vec = np.array([True, False])
  5891. mat1 = np.array([[True, False], [False, True]])
  5892. mat2 = np.stack([mat1]*2, axis=0)
  5893. tgt1 = np.array([True, False])
  5894. tgt2 = np.stack([tgt1]*2, axis=0)
  5895. res = self.matmul(vec, mat1)
  5896. assert_equal(res, tgt1)
  5897. res = self.matmul(vec, mat2)
  5898. assert_equal(res, tgt2)
  5899. def test_matrix_vector_values(self):
  5900. vec = np.array([1, 2])
  5901. mat1 = np.array([[1, 2], [3, 4]])
  5902. mat2 = np.stack([mat1]*2, axis=0)
  5903. tgt1 = np.array([5, 11])
  5904. tgt2 = np.stack([tgt1]*2, axis=0)
  5905. for dt in self.types[1:]:
  5906. v = vec.astype(dt)
  5907. m1 = mat1.astype(dt)
  5908. m2 = mat2.astype(dt)
  5909. res = self.matmul(m1, v)
  5910. assert_equal(res, tgt1)
  5911. res = self.matmul(m2, v)
  5912. assert_equal(res, tgt2)
  5913. # boolean type
  5914. vec = np.array([True, False])
  5915. mat1 = np.array([[True, False], [False, True]])
  5916. mat2 = np.stack([mat1]*2, axis=0)
  5917. tgt1 = np.array([True, False])
  5918. tgt2 = np.stack([tgt1]*2, axis=0)
  5919. res = self.matmul(vec, mat1)
  5920. assert_equal(res, tgt1)
  5921. res = self.matmul(vec, mat2)
  5922. assert_equal(res, tgt2)
  5923. def test_matrix_matrix_values(self):
  5924. mat1 = np.array([[1, 2], [3, 4]])
  5925. mat2 = np.array([[1, 0], [1, 1]])
  5926. mat12 = np.stack([mat1, mat2], axis=0)
  5927. mat21 = np.stack([mat2, mat1], axis=0)
  5928. tgt11 = np.array([[7, 10], [15, 22]])
  5929. tgt12 = np.array([[3, 2], [7, 4]])
  5930. tgt21 = np.array([[1, 2], [4, 6]])
  5931. tgt12_21 = np.stack([tgt12, tgt21], axis=0)
  5932. tgt11_12 = np.stack((tgt11, tgt12), axis=0)
  5933. tgt11_21 = np.stack((tgt11, tgt21), axis=0)
  5934. for dt in self.types[1:]:
  5935. m1 = mat1.astype(dt)
  5936. m2 = mat2.astype(dt)
  5937. m12 = mat12.astype(dt)
  5938. m21 = mat21.astype(dt)
  5939. # matrix @ matrix
  5940. res = self.matmul(m1, m2)
  5941. assert_equal(res, tgt12)
  5942. res = self.matmul(m2, m1)
  5943. assert_equal(res, tgt21)
  5944. # stacked @ matrix
  5945. res = self.matmul(m12, m1)
  5946. assert_equal(res, tgt11_21)
  5947. # matrix @ stacked
  5948. res = self.matmul(m1, m12)
  5949. assert_equal(res, tgt11_12)
  5950. # stacked @ stacked
  5951. res = self.matmul(m12, m21)
  5952. assert_equal(res, tgt12_21)
  5953. # boolean type
  5954. m1 = np.array([[1, 1], [0, 0]], dtype=np.bool_)
  5955. m2 = np.array([[1, 0], [1, 1]], dtype=np.bool_)
  5956. m12 = np.stack([m1, m2], axis=0)
  5957. m21 = np.stack([m2, m1], axis=0)
  5958. tgt11 = m1
  5959. tgt12 = m1
  5960. tgt21 = np.array([[1, 1], [1, 1]], dtype=np.bool_)
  5961. tgt12_21 = np.stack([tgt12, tgt21], axis=0)
  5962. tgt11_12 = np.stack((tgt11, tgt12), axis=0)
  5963. tgt11_21 = np.stack((tgt11, tgt21), axis=0)
  5964. # matrix @ matrix
  5965. res = self.matmul(m1, m2)
  5966. assert_equal(res, tgt12)
  5967. res = self.matmul(m2, m1)
  5968. assert_equal(res, tgt21)
  5969. # stacked @ matrix
  5970. res = self.matmul(m12, m1)
  5971. assert_equal(res, tgt11_21)
  5972. # matrix @ stacked
  5973. res = self.matmul(m1, m12)
  5974. assert_equal(res, tgt11_12)
  5975. # stacked @ stacked
  5976. res = self.matmul(m12, m21)
  5977. assert_equal(res, tgt12_21)
  5978. class TestMatmul(MatmulCommon):
  5979. matmul = np.matmul
  5980. def test_out_arg(self):
  5981. a = np.ones((5, 2), dtype=float)
  5982. b = np.array([[1, 3], [5, 7]], dtype=float)
  5983. tgt = np.dot(a, b)
  5984. # test as positional argument
  5985. msg = "out positional argument"
  5986. out = np.zeros((5, 2), dtype=float)
  5987. self.matmul(a, b, out)
  5988. assert_array_equal(out, tgt, err_msg=msg)
  5989. # test as keyword argument
  5990. msg = "out keyword argument"
  5991. out = np.zeros((5, 2), dtype=float)
  5992. self.matmul(a, b, out=out)
  5993. assert_array_equal(out, tgt, err_msg=msg)
  5994. # test out with not allowed type cast (safe casting)
  5995. msg = "Cannot cast ufunc .* output"
  5996. out = np.zeros((5, 2), dtype=np.int32)
  5997. assert_raises_regex(TypeError, msg, self.matmul, a, b, out=out)
  5998. # test out with type upcast to complex
  5999. out = np.zeros((5, 2), dtype=np.complex128)
  6000. c = self.matmul(a, b, out=out)
  6001. assert_(c is out)
  6002. with suppress_warnings() as sup:
  6003. sup.filter(np.ComplexWarning, '')
  6004. c = c.astype(tgt.dtype)
  6005. assert_array_equal(c, tgt)
  6006. def test_empty_out(self):
  6007. # Check that the output cannot be broadcast, so that it cannot be
  6008. # size zero when the outer dimensions (iterator size) has size zero.
  6009. arr = np.ones((0, 1, 1))
  6010. out = np.ones((1, 1, 1))
  6011. assert self.matmul(arr, arr).shape == (0, 1, 1)
  6012. with pytest.raises(ValueError, match=r"non-broadcastable"):
  6013. self.matmul(arr, arr, out=out)
  6014. def test_out_contiguous(self):
  6015. a = np.ones((5, 2), dtype=float)
  6016. b = np.array([[1, 3], [5, 7]], dtype=float)
  6017. v = np.array([1, 3], dtype=float)
  6018. tgt = np.dot(a, b)
  6019. tgt_mv = np.dot(a, v)
  6020. # test out non-contiguous
  6021. out = np.ones((5, 2, 2), dtype=float)
  6022. c = self.matmul(a, b, out=out[..., 0])
  6023. assert c.base is out
  6024. assert_array_equal(c, tgt)
  6025. c = self.matmul(a, v, out=out[:, 0, 0])
  6026. assert_array_equal(c, tgt_mv)
  6027. c = self.matmul(v, a.T, out=out[:, 0, 0])
  6028. assert_array_equal(c, tgt_mv)
  6029. # test out contiguous in only last dim
  6030. out = np.ones((10, 2), dtype=float)
  6031. c = self.matmul(a, b, out=out[::2, :])
  6032. assert_array_equal(c, tgt)
  6033. # test transposes of out, args
  6034. out = np.ones((5, 2), dtype=float)
  6035. c = self.matmul(b.T, a.T, out=out.T)
  6036. assert_array_equal(out, tgt)
  6037. m1 = np.arange(15.).reshape(5, 3)
  6038. m2 = np.arange(21.).reshape(3, 7)
  6039. m3 = np.arange(30.).reshape(5, 6)[:, ::2] # non-contiguous
  6040. vc = np.arange(10.)
  6041. vr = np.arange(6.)
  6042. m0 = np.zeros((3, 0))
  6043. @pytest.mark.parametrize('args', (
  6044. # matrix-matrix
  6045. (m1, m2), (m2.T, m1.T), (m2.T.copy(), m1.T), (m2.T, m1.T.copy()),
  6046. # matrix-matrix-transpose, contiguous and non
  6047. (m1, m1.T), (m1.T, m1), (m1, m3.T), (m3, m1.T),
  6048. (m3, m3.T), (m3.T, m3),
  6049. # matrix-matrix non-contiguous
  6050. (m3, m2), (m2.T, m3.T), (m2.T.copy(), m3.T),
  6051. # vector-matrix, matrix-vector, contiguous
  6052. (m1, vr[:3]), (vc[:5], m1), (m1.T, vc[:5]), (vr[:3], m1.T),
  6053. # vector-matrix, matrix-vector, vector non-contiguous
  6054. (m1, vr[::2]), (vc[::2], m1), (m1.T, vc[::2]), (vr[::2], m1.T),
  6055. # vector-matrix, matrix-vector, matrix non-contiguous
  6056. (m3, vr[:3]), (vc[:5], m3), (m3.T, vc[:5]), (vr[:3], m3.T),
  6057. # vector-matrix, matrix-vector, both non-contiguous
  6058. (m3, vr[::2]), (vc[::2], m3), (m3.T, vc[::2]), (vr[::2], m3.T),
  6059. # size == 0
  6060. (m0, m0.T), (m0.T, m0), (m1, m0), (m0.T, m1.T),
  6061. ))
  6062. def test_dot_equivalent(self, args):
  6063. r1 = np.matmul(*args)
  6064. r2 = np.dot(*args)
  6065. assert_equal(r1, r2)
  6066. r3 = np.matmul(args[0].copy(), args[1].copy())
  6067. assert_equal(r1, r3)
  6068. def test_matmul_object(self):
  6069. import fractions
  6070. f = np.vectorize(fractions.Fraction)
  6071. def random_ints():
  6072. return np.random.randint(1, 1000, size=(10, 3, 3))
  6073. M1 = f(random_ints(), random_ints())
  6074. M2 = f(random_ints(), random_ints())
  6075. M3 = self.matmul(M1, M2)
  6076. [N1, N2, N3] = [a.astype(float) for a in [M1, M2, M3]]
  6077. assert_allclose(N3, self.matmul(N1, N2))
  6078. def test_matmul_object_type_scalar(self):
  6079. from fractions import Fraction as F
  6080. v = np.array([F(2,3), F(5,7)])
  6081. res = self.matmul(v, v)
  6082. assert_(type(res) is F)
  6083. def test_matmul_empty(self):
  6084. a = np.empty((3, 0), dtype=object)
  6085. b = np.empty((0, 3), dtype=object)
  6086. c = np.zeros((3, 3))
  6087. assert_array_equal(np.matmul(a, b), c)
  6088. def test_matmul_exception_multiply(self):
  6089. # test that matmul fails if `__mul__` is missing
  6090. class add_not_multiply():
  6091. def __add__(self, other):
  6092. return self
  6093. a = np.full((3,3), add_not_multiply())
  6094. with assert_raises(TypeError):
  6095. b = np.matmul(a, a)
  6096. def test_matmul_exception_add(self):
  6097. # test that matmul fails if `__add__` is missing
  6098. class multiply_not_add():
  6099. def __mul__(self, other):
  6100. return self
  6101. a = np.full((3,3), multiply_not_add())
  6102. with assert_raises(TypeError):
  6103. b = np.matmul(a, a)
  6104. def test_matmul_bool(self):
  6105. # gh-14439
  6106. a = np.array([[1, 0],[1, 1]], dtype=bool)
  6107. assert np.max(a.view(np.uint8)) == 1
  6108. b = np.matmul(a, a)
  6109. # matmul with boolean output should always be 0, 1
  6110. assert np.max(b.view(np.uint8)) == 1
  6111. rg = np.random.default_rng(np.random.PCG64(43))
  6112. d = rg.integers(2, size=4*5, dtype=np.int8)
  6113. d = d.reshape(4, 5) > 0
  6114. out1 = np.matmul(d, d.reshape(5, 4))
  6115. out2 = np.dot(d, d.reshape(5, 4))
  6116. assert_equal(out1, out2)
  6117. c = np.matmul(np.zeros((2, 0), dtype=bool), np.zeros(0, dtype=bool))
  6118. assert not np.any(c)
  6119. class TestMatmulOperator(MatmulCommon):
  6120. import operator
  6121. matmul = operator.matmul
  6122. def test_array_priority_override(self):
  6123. class A:
  6124. __array_priority__ = 1000
  6125. def __matmul__(self, other):
  6126. return "A"
  6127. def __rmatmul__(self, other):
  6128. return "A"
  6129. a = A()
  6130. b = np.ones(2)
  6131. assert_equal(self.matmul(a, b), "A")
  6132. assert_equal(self.matmul(b, a), "A")
  6133. def test_matmul_raises(self):
  6134. assert_raises(TypeError, self.matmul, np.int8(5), np.int8(5))
  6135. assert_raises(TypeError, self.matmul, np.void(b'abc'), np.void(b'abc'))
  6136. assert_raises(TypeError, self.matmul, np.arange(10), np.void(b'abc'))
  6137. def test_matmul_inplace():
  6138. # It would be nice to support in-place matmul eventually, but for now
  6139. # we don't have a working implementation, so better just to error out
  6140. # and nudge people to writing "a = a @ b".
  6141. a = np.eye(3)
  6142. b = np.eye(3)
  6143. assert_raises(TypeError, a.__imatmul__, b)
  6144. import operator
  6145. assert_raises(TypeError, operator.imatmul, a, b)
  6146. assert_raises(TypeError, exec, "a @= b", globals(), locals())
  6147. def test_matmul_axes():
  6148. a = np.arange(3*4*5).reshape(3, 4, 5)
  6149. c = np.matmul(a, a, axes=[(-2, -1), (-1, -2), (1, 2)])
  6150. assert c.shape == (3, 4, 4)
  6151. d = np.matmul(a, a, axes=[(-2, -1), (-1, -2), (0, 1)])
  6152. assert d.shape == (4, 4, 3)
  6153. e = np.swapaxes(d, 0, 2)
  6154. assert_array_equal(e, c)
  6155. f = np.matmul(a, np.arange(3), axes=[(1, 0), (0), (0)])
  6156. assert f.shape == (4, 5)
  6157. class TestInner:
  6158. def test_inner_type_mismatch(self):
  6159. c = 1.
  6160. A = np.array((1,1), dtype='i,i')
  6161. assert_raises(TypeError, np.inner, c, A)
  6162. assert_raises(TypeError, np.inner, A, c)
  6163. def test_inner_scalar_and_vector(self):
  6164. for dt in np.typecodes['AllInteger'] + np.typecodes['AllFloat'] + '?':
  6165. sca = np.array(3, dtype=dt)[()]
  6166. vec = np.array([1, 2], dtype=dt)
  6167. desired = np.array([3, 6], dtype=dt)
  6168. assert_equal(np.inner(vec, sca), desired)
  6169. assert_equal(np.inner(sca, vec), desired)
  6170. def test_vecself(self):
  6171. # Ticket 844.
  6172. # Inner product of a vector with itself segfaults or give
  6173. # meaningless result
  6174. a = np.zeros(shape=(1, 80), dtype=np.float64)
  6175. p = np.inner(a, a)
  6176. assert_almost_equal(p, 0, decimal=14)
  6177. def test_inner_product_with_various_contiguities(self):
  6178. # github issue 6532
  6179. for dt in np.typecodes['AllInteger'] + np.typecodes['AllFloat'] + '?':
  6180. # check an inner product involving a matrix transpose
  6181. A = np.array([[1, 2], [3, 4]], dtype=dt)
  6182. B = np.array([[1, 3], [2, 4]], dtype=dt)
  6183. C = np.array([1, 1], dtype=dt)
  6184. desired = np.array([4, 6], dtype=dt)
  6185. assert_equal(np.inner(A.T, C), desired)
  6186. assert_equal(np.inner(C, A.T), desired)
  6187. assert_equal(np.inner(B, C), desired)
  6188. assert_equal(np.inner(C, B), desired)
  6189. # check a matrix product
  6190. desired = np.array([[7, 10], [15, 22]], dtype=dt)
  6191. assert_equal(np.inner(A, B), desired)
  6192. # check the syrk vs. gemm paths
  6193. desired = np.array([[5, 11], [11, 25]], dtype=dt)
  6194. assert_equal(np.inner(A, A), desired)
  6195. assert_equal(np.inner(A, A.copy()), desired)
  6196. # check an inner product involving an aliased and reversed view
  6197. a = np.arange(5).astype(dt)
  6198. b = a[::-1]
  6199. desired = np.array(10, dtype=dt).item()
  6200. assert_equal(np.inner(b, a), desired)
  6201. def test_3d_tensor(self):
  6202. for dt in np.typecodes['AllInteger'] + np.typecodes['AllFloat'] + '?':
  6203. a = np.arange(24).reshape(2,3,4).astype(dt)
  6204. b = np.arange(24, 48).reshape(2,3,4).astype(dt)
  6205. desired = np.array(
  6206. [[[[ 158, 182, 206],
  6207. [ 230, 254, 278]],
  6208. [[ 566, 654, 742],
  6209. [ 830, 918, 1006]],
  6210. [[ 974, 1126, 1278],
  6211. [1430, 1582, 1734]]],
  6212. [[[1382, 1598, 1814],
  6213. [2030, 2246, 2462]],
  6214. [[1790, 2070, 2350],
  6215. [2630, 2910, 3190]],
  6216. [[2198, 2542, 2886],
  6217. [3230, 3574, 3918]]]]
  6218. ).astype(dt)
  6219. assert_equal(np.inner(a, b), desired)
  6220. assert_equal(np.inner(b, a).transpose(2,3,0,1), desired)
  6221. class TestChoose:
  6222. def setup_method(self):
  6223. self.x = 2*np.ones((3,), dtype=int)
  6224. self.y = 3*np.ones((3,), dtype=int)
  6225. self.x2 = 2*np.ones((2, 3), dtype=int)
  6226. self.y2 = 3*np.ones((2, 3), dtype=int)
  6227. self.ind = [0, 0, 1]
  6228. def test_basic(self):
  6229. A = np.choose(self.ind, (self.x, self.y))
  6230. assert_equal(A, [2, 2, 3])
  6231. def test_broadcast1(self):
  6232. A = np.choose(self.ind, (self.x2, self.y2))
  6233. assert_equal(A, [[2, 2, 3], [2, 2, 3]])
  6234. def test_broadcast2(self):
  6235. A = np.choose(self.ind, (self.x, self.y2))
  6236. assert_equal(A, [[2, 2, 3], [2, 2, 3]])
  6237. @pytest.mark.parametrize("ops",
  6238. [(1000, np.array([1], dtype=np.uint8)),
  6239. (-1, np.array([1], dtype=np.uint8)),
  6240. (1., np.float32(3)),
  6241. (1., np.array([3], dtype=np.float32))],)
  6242. def test_output_dtype(self, ops):
  6243. expected_dt = np.result_type(*ops)
  6244. assert(np.choose([0], ops).dtype == expected_dt)
  6245. class TestRepeat:
  6246. def setup_method(self):
  6247. self.m = np.array([1, 2, 3, 4, 5, 6])
  6248. self.m_rect = self.m.reshape((2, 3))
  6249. def test_basic(self):
  6250. A = np.repeat(self.m, [1, 3, 2, 1, 1, 2])
  6251. assert_equal(A, [1, 2, 2, 2, 3,
  6252. 3, 4, 5, 6, 6])
  6253. def test_broadcast1(self):
  6254. A = np.repeat(self.m, 2)
  6255. assert_equal(A, [1, 1, 2, 2, 3, 3,
  6256. 4, 4, 5, 5, 6, 6])
  6257. def test_axis_spec(self):
  6258. A = np.repeat(self.m_rect, [2, 1], axis=0)
  6259. assert_equal(A, [[1, 2, 3],
  6260. [1, 2, 3],
  6261. [4, 5, 6]])
  6262. A = np.repeat(self.m_rect, [1, 3, 2], axis=1)
  6263. assert_equal(A, [[1, 2, 2, 2, 3, 3],
  6264. [4, 5, 5, 5, 6, 6]])
  6265. def test_broadcast2(self):
  6266. A = np.repeat(self.m_rect, 2, axis=0)
  6267. assert_equal(A, [[1, 2, 3],
  6268. [1, 2, 3],
  6269. [4, 5, 6],
  6270. [4, 5, 6]])
  6271. A = np.repeat(self.m_rect, 2, axis=1)
  6272. assert_equal(A, [[1, 1, 2, 2, 3, 3],
  6273. [4, 4, 5, 5, 6, 6]])
  6274. # TODO: test for multidimensional
  6275. NEIGH_MODE = {'zero': 0, 'one': 1, 'constant': 2, 'circular': 3, 'mirror': 4}
  6276. @pytest.mark.parametrize('dt', [float, Decimal], ids=['float', 'object'])
  6277. class TestNeighborhoodIter:
  6278. # Simple, 2d tests
  6279. def test_simple2d(self, dt):
  6280. # Test zero and one padding for simple data type
  6281. x = np.array([[0, 1], [2, 3]], dtype=dt)
  6282. r = [np.array([[0, 0, 0], [0, 0, 1]], dtype=dt),
  6283. np.array([[0, 0, 0], [0, 1, 0]], dtype=dt),
  6284. np.array([[0, 0, 1], [0, 2, 3]], dtype=dt),
  6285. np.array([[0, 1, 0], [2, 3, 0]], dtype=dt)]
  6286. l = _multiarray_tests.test_neighborhood_iterator(
  6287. x, [-1, 0, -1, 1], x[0], NEIGH_MODE['zero'])
  6288. assert_array_equal(l, r)
  6289. r = [np.array([[1, 1, 1], [1, 0, 1]], dtype=dt),
  6290. np.array([[1, 1, 1], [0, 1, 1]], dtype=dt),
  6291. np.array([[1, 0, 1], [1, 2, 3]], dtype=dt),
  6292. np.array([[0, 1, 1], [2, 3, 1]], dtype=dt)]
  6293. l = _multiarray_tests.test_neighborhood_iterator(
  6294. x, [-1, 0, -1, 1], x[0], NEIGH_MODE['one'])
  6295. assert_array_equal(l, r)
  6296. r = [np.array([[4, 4, 4], [4, 0, 1]], dtype=dt),
  6297. np.array([[4, 4, 4], [0, 1, 4]], dtype=dt),
  6298. np.array([[4, 0, 1], [4, 2, 3]], dtype=dt),
  6299. np.array([[0, 1, 4], [2, 3, 4]], dtype=dt)]
  6300. l = _multiarray_tests.test_neighborhood_iterator(
  6301. x, [-1, 0, -1, 1], 4, NEIGH_MODE['constant'])
  6302. assert_array_equal(l, r)
  6303. # Test with start in the middle
  6304. r = [np.array([[4, 0, 1], [4, 2, 3]], dtype=dt),
  6305. np.array([[0, 1, 4], [2, 3, 4]], dtype=dt)]
  6306. l = _multiarray_tests.test_neighborhood_iterator(
  6307. x, [-1, 0, -1, 1], 4, NEIGH_MODE['constant'], 2)
  6308. assert_array_equal(l, r)
  6309. def test_mirror2d(self, dt):
  6310. x = np.array([[0, 1], [2, 3]], dtype=dt)
  6311. r = [np.array([[0, 0, 1], [0, 0, 1]], dtype=dt),
  6312. np.array([[0, 1, 1], [0, 1, 1]], dtype=dt),
  6313. np.array([[0, 0, 1], [2, 2, 3]], dtype=dt),
  6314. np.array([[0, 1, 1], [2, 3, 3]], dtype=dt)]
  6315. l = _multiarray_tests.test_neighborhood_iterator(
  6316. x, [-1, 0, -1, 1], x[0], NEIGH_MODE['mirror'])
  6317. assert_array_equal(l, r)
  6318. # Simple, 1d tests
  6319. def test_simple(self, dt):
  6320. # Test padding with constant values
  6321. x = np.linspace(1, 5, 5).astype(dt)
  6322. r = [[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 0]]
  6323. l = _multiarray_tests.test_neighborhood_iterator(
  6324. x, [-1, 1], x[0], NEIGH_MODE['zero'])
  6325. assert_array_equal(l, r)
  6326. r = [[1, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 1]]
  6327. l = _multiarray_tests.test_neighborhood_iterator(
  6328. x, [-1, 1], x[0], NEIGH_MODE['one'])
  6329. assert_array_equal(l, r)
  6330. r = [[x[4], 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, x[4]]]
  6331. l = _multiarray_tests.test_neighborhood_iterator(
  6332. x, [-1, 1], x[4], NEIGH_MODE['constant'])
  6333. assert_array_equal(l, r)
  6334. # Test mirror modes
  6335. def test_mirror(self, dt):
  6336. x = np.linspace(1, 5, 5).astype(dt)
  6337. r = np.array([[2, 1, 1, 2, 3], [1, 1, 2, 3, 4], [1, 2, 3, 4, 5],
  6338. [2, 3, 4, 5, 5], [3, 4, 5, 5, 4]], dtype=dt)
  6339. l = _multiarray_tests.test_neighborhood_iterator(
  6340. x, [-2, 2], x[1], NEIGH_MODE['mirror'])
  6341. assert_([i.dtype == dt for i in l])
  6342. assert_array_equal(l, r)
  6343. # Circular mode
  6344. def test_circular(self, dt):
  6345. x = np.linspace(1, 5, 5).astype(dt)
  6346. r = np.array([[4, 5, 1, 2, 3], [5, 1, 2, 3, 4], [1, 2, 3, 4, 5],
  6347. [2, 3, 4, 5, 1], [3, 4, 5, 1, 2]], dtype=dt)
  6348. l = _multiarray_tests.test_neighborhood_iterator(
  6349. x, [-2, 2], x[0], NEIGH_MODE['circular'])
  6350. assert_array_equal(l, r)
  6351. # Test stacking neighborhood iterators
  6352. class TestStackedNeighborhoodIter:
  6353. # Simple, 1d test: stacking 2 constant-padded neigh iterators
  6354. def test_simple_const(self):
  6355. dt = np.float64
  6356. # Test zero and one padding for simple data type
  6357. x = np.array([1, 2, 3], dtype=dt)
  6358. r = [np.array([0], dtype=dt),
  6359. np.array([0], dtype=dt),
  6360. np.array([1], dtype=dt),
  6361. np.array([2], dtype=dt),
  6362. np.array([3], dtype=dt),
  6363. np.array([0], dtype=dt),
  6364. np.array([0], dtype=dt)]
  6365. l = _multiarray_tests.test_neighborhood_iterator_oob(
  6366. x, [-2, 4], NEIGH_MODE['zero'], [0, 0], NEIGH_MODE['zero'])
  6367. assert_array_equal(l, r)
  6368. r = [np.array([1, 0, 1], dtype=dt),
  6369. np.array([0, 1, 2], dtype=dt),
  6370. np.array([1, 2, 3], dtype=dt),
  6371. np.array([2, 3, 0], dtype=dt),
  6372. np.array([3, 0, 1], dtype=dt)]
  6373. l = _multiarray_tests.test_neighborhood_iterator_oob(
  6374. x, [-1, 3], NEIGH_MODE['zero'], [-1, 1], NEIGH_MODE['one'])
  6375. assert_array_equal(l, r)
  6376. # 2nd simple, 1d test: stacking 2 neigh iterators, mixing const padding and
  6377. # mirror padding
  6378. def test_simple_mirror(self):
  6379. dt = np.float64
  6380. # Stacking zero on top of mirror
  6381. x = np.array([1, 2, 3], dtype=dt)
  6382. r = [np.array([0, 1, 1], dtype=dt),
  6383. np.array([1, 1, 2], dtype=dt),
  6384. np.array([1, 2, 3], dtype=dt),
  6385. np.array([2, 3, 3], dtype=dt),
  6386. np.array([3, 3, 0], dtype=dt)]
  6387. l = _multiarray_tests.test_neighborhood_iterator_oob(
  6388. x, [-1, 3], NEIGH_MODE['mirror'], [-1, 1], NEIGH_MODE['zero'])
  6389. assert_array_equal(l, r)
  6390. # Stacking mirror on top of zero
  6391. x = np.array([1, 2, 3], dtype=dt)
  6392. r = [np.array([1, 0, 0], dtype=dt),
  6393. np.array([0, 0, 1], dtype=dt),
  6394. np.array([0, 1, 2], dtype=dt),
  6395. np.array([1, 2, 3], dtype=dt),
  6396. np.array([2, 3, 0], dtype=dt)]
  6397. l = _multiarray_tests.test_neighborhood_iterator_oob(
  6398. x, [-1, 3], NEIGH_MODE['zero'], [-2, 0], NEIGH_MODE['mirror'])
  6399. assert_array_equal(l, r)
  6400. # Stacking mirror on top of zero: 2nd
  6401. x = np.array([1, 2, 3], dtype=dt)
  6402. r = [np.array([0, 1, 2], dtype=dt),
  6403. np.array([1, 2, 3], dtype=dt),
  6404. np.array([2, 3, 0], dtype=dt),
  6405. np.array([3, 0, 0], dtype=dt),
  6406. np.array([0, 0, 3], dtype=dt)]
  6407. l = _multiarray_tests.test_neighborhood_iterator_oob(
  6408. x, [-1, 3], NEIGH_MODE['zero'], [0, 2], NEIGH_MODE['mirror'])
  6409. assert_array_equal(l, r)
  6410. # Stacking mirror on top of zero: 3rd
  6411. x = np.array([1, 2, 3], dtype=dt)
  6412. r = [np.array([1, 0, 0, 1, 2], dtype=dt),
  6413. np.array([0, 0, 1, 2, 3], dtype=dt),
  6414. np.array([0, 1, 2, 3, 0], dtype=dt),
  6415. np.array([1, 2, 3, 0, 0], dtype=dt),
  6416. np.array([2, 3, 0, 0, 3], dtype=dt)]
  6417. l = _multiarray_tests.test_neighborhood_iterator_oob(
  6418. x, [-1, 3], NEIGH_MODE['zero'], [-2, 2], NEIGH_MODE['mirror'])
  6419. assert_array_equal(l, r)
  6420. # 3rd simple, 1d test: stacking 2 neigh iterators, mixing const padding and
  6421. # circular padding
  6422. def test_simple_circular(self):
  6423. dt = np.float64
  6424. # Stacking zero on top of mirror
  6425. x = np.array([1, 2, 3], dtype=dt)
  6426. r = [np.array([0, 3, 1], dtype=dt),
  6427. np.array([3, 1, 2], dtype=dt),
  6428. np.array([1, 2, 3], dtype=dt),
  6429. np.array([2, 3, 1], dtype=dt),
  6430. np.array([3, 1, 0], dtype=dt)]
  6431. l = _multiarray_tests.test_neighborhood_iterator_oob(
  6432. x, [-1, 3], NEIGH_MODE['circular'], [-1, 1], NEIGH_MODE['zero'])
  6433. assert_array_equal(l, r)
  6434. # Stacking mirror on top of zero
  6435. x = np.array([1, 2, 3], dtype=dt)
  6436. r = [np.array([3, 0, 0], dtype=dt),
  6437. np.array([0, 0, 1], dtype=dt),
  6438. np.array([0, 1, 2], dtype=dt),
  6439. np.array([1, 2, 3], dtype=dt),
  6440. np.array([2, 3, 0], dtype=dt)]
  6441. l = _multiarray_tests.test_neighborhood_iterator_oob(
  6442. x, [-1, 3], NEIGH_MODE['zero'], [-2, 0], NEIGH_MODE['circular'])
  6443. assert_array_equal(l, r)
  6444. # Stacking mirror on top of zero: 2nd
  6445. x = np.array([1, 2, 3], dtype=dt)
  6446. r = [np.array([0, 1, 2], dtype=dt),
  6447. np.array([1, 2, 3], dtype=dt),
  6448. np.array([2, 3, 0], dtype=dt),
  6449. np.array([3, 0, 0], dtype=dt),
  6450. np.array([0, 0, 1], dtype=dt)]
  6451. l = _multiarray_tests.test_neighborhood_iterator_oob(
  6452. x, [-1, 3], NEIGH_MODE['zero'], [0, 2], NEIGH_MODE['circular'])
  6453. assert_array_equal(l, r)
  6454. # Stacking mirror on top of zero: 3rd
  6455. x = np.array([1, 2, 3], dtype=dt)
  6456. r = [np.array([3, 0, 0, 1, 2], dtype=dt),
  6457. np.array([0, 0, 1, 2, 3], dtype=dt),
  6458. np.array([0, 1, 2, 3, 0], dtype=dt),
  6459. np.array([1, 2, 3, 0, 0], dtype=dt),
  6460. np.array([2, 3, 0, 0, 1], dtype=dt)]
  6461. l = _multiarray_tests.test_neighborhood_iterator_oob(
  6462. x, [-1, 3], NEIGH_MODE['zero'], [-2, 2], NEIGH_MODE['circular'])
  6463. assert_array_equal(l, r)
  6464. # 4th simple, 1d test: stacking 2 neigh iterators, but with lower iterator
  6465. # being strictly within the array
  6466. def test_simple_strict_within(self):
  6467. dt = np.float64
  6468. # Stacking zero on top of zero, first neighborhood strictly inside the
  6469. # array
  6470. x = np.array([1, 2, 3], dtype=dt)
  6471. r = [np.array([1, 2, 3, 0], dtype=dt)]
  6472. l = _multiarray_tests.test_neighborhood_iterator_oob(
  6473. x, [1, 1], NEIGH_MODE['zero'], [-1, 2], NEIGH_MODE['zero'])
  6474. assert_array_equal(l, r)
  6475. # Stacking mirror on top of zero, first neighborhood strictly inside the
  6476. # array
  6477. x = np.array([1, 2, 3], dtype=dt)
  6478. r = [np.array([1, 2, 3, 3], dtype=dt)]
  6479. l = _multiarray_tests.test_neighborhood_iterator_oob(
  6480. x, [1, 1], NEIGH_MODE['zero'], [-1, 2], NEIGH_MODE['mirror'])
  6481. assert_array_equal(l, r)
  6482. # Stacking mirror on top of zero, first neighborhood strictly inside the
  6483. # array
  6484. x = np.array([1, 2, 3], dtype=dt)
  6485. r = [np.array([1, 2, 3, 1], dtype=dt)]
  6486. l = _multiarray_tests.test_neighborhood_iterator_oob(
  6487. x, [1, 1], NEIGH_MODE['zero'], [-1, 2], NEIGH_MODE['circular'])
  6488. assert_array_equal(l, r)
  6489. class TestWarnings:
  6490. def test_complex_warning(self):
  6491. x = np.array([1, 2])
  6492. y = np.array([1-2j, 1+2j])
  6493. with warnings.catch_warnings():
  6494. warnings.simplefilter("error", np.ComplexWarning)
  6495. assert_raises(np.ComplexWarning, x.__setitem__, slice(None), y)
  6496. assert_equal(x, [1, 2])
  6497. class TestMinScalarType:
  6498. def test_usigned_shortshort(self):
  6499. dt = np.min_scalar_type(2**8-1)
  6500. wanted = np.dtype('uint8')
  6501. assert_equal(wanted, dt)
  6502. def test_usigned_short(self):
  6503. dt = np.min_scalar_type(2**16-1)
  6504. wanted = np.dtype('uint16')
  6505. assert_equal(wanted, dt)
  6506. def test_usigned_int(self):
  6507. dt = np.min_scalar_type(2**32-1)
  6508. wanted = np.dtype('uint32')
  6509. assert_equal(wanted, dt)
  6510. def test_usigned_longlong(self):
  6511. dt = np.min_scalar_type(2**63-1)
  6512. wanted = np.dtype('uint64')
  6513. assert_equal(wanted, dt)
  6514. def test_object(self):
  6515. dt = np.min_scalar_type(2**64)
  6516. wanted = np.dtype('O')
  6517. assert_equal(wanted, dt)
  6518. from numpy.core._internal import _dtype_from_pep3118
  6519. class TestPEP3118Dtype:
  6520. def _check(self, spec, wanted):
  6521. dt = np.dtype(wanted)
  6522. actual = _dtype_from_pep3118(spec)
  6523. assert_equal(actual, dt,
  6524. err_msg="spec %r != dtype %r" % (spec, wanted))
  6525. def test_native_padding(self):
  6526. align = np.dtype('i').alignment
  6527. for j in range(8):
  6528. if j == 0:
  6529. s = 'bi'
  6530. else:
  6531. s = 'b%dxi' % j
  6532. self._check('@'+s, {'f0': ('i1', 0),
  6533. 'f1': ('i', align*(1 + j//align))})
  6534. self._check('='+s, {'f0': ('i1', 0),
  6535. 'f1': ('i', 1+j)})
  6536. def test_native_padding_2(self):
  6537. # Native padding should work also for structs and sub-arrays
  6538. self._check('x3T{xi}', {'f0': (({'f0': ('i', 4)}, (3,)), 4)})
  6539. self._check('^x3T{xi}', {'f0': (({'f0': ('i', 1)}, (3,)), 1)})
  6540. def test_trailing_padding(self):
  6541. # Trailing padding should be included, *and*, the item size
  6542. # should match the alignment if in aligned mode
  6543. align = np.dtype('i').alignment
  6544. size = np.dtype('i').itemsize
  6545. def aligned(n):
  6546. return align*(1 + (n-1)//align)
  6547. base = dict(formats=['i'], names=['f0'])
  6548. self._check('ix', dict(itemsize=aligned(size + 1), **base))
  6549. self._check('ixx', dict(itemsize=aligned(size + 2), **base))
  6550. self._check('ixxx', dict(itemsize=aligned(size + 3), **base))
  6551. self._check('ixxxx', dict(itemsize=aligned(size + 4), **base))
  6552. self._check('i7x', dict(itemsize=aligned(size + 7), **base))
  6553. self._check('^ix', dict(itemsize=size + 1, **base))
  6554. self._check('^ixx', dict(itemsize=size + 2, **base))
  6555. self._check('^ixxx', dict(itemsize=size + 3, **base))
  6556. self._check('^ixxxx', dict(itemsize=size + 4, **base))
  6557. self._check('^i7x', dict(itemsize=size + 7, **base))
  6558. def test_native_padding_3(self):
  6559. dt = np.dtype(
  6560. [('a', 'b'), ('b', 'i'),
  6561. ('sub', np.dtype('b,i')), ('c', 'i')],
  6562. align=True)
  6563. self._check("T{b:a:xxxi:b:T{b:f0:=i:f1:}:sub:xxxi:c:}", dt)
  6564. dt = np.dtype(
  6565. [('a', 'b'), ('b', 'i'), ('c', 'b'), ('d', 'b'),
  6566. ('e', 'b'), ('sub', np.dtype('b,i', align=True))])
  6567. self._check("T{b:a:=i:b:b:c:b:d:b:e:T{b:f0:xxxi:f1:}:sub:}", dt)
  6568. def test_padding_with_array_inside_struct(self):
  6569. dt = np.dtype(
  6570. [('a', 'b'), ('b', 'i'), ('c', 'b', (3,)),
  6571. ('d', 'i')],
  6572. align=True)
  6573. self._check("T{b:a:xxxi:b:3b:c:xi:d:}", dt)
  6574. def test_byteorder_inside_struct(self):
  6575. # The byte order after @T{=i} should be '=', not '@'.
  6576. # Check this by noting the absence of native alignment.
  6577. self._check('@T{^i}xi', {'f0': ({'f0': ('i', 0)}, 0),
  6578. 'f1': ('i', 5)})
  6579. def test_intra_padding(self):
  6580. # Natively aligned sub-arrays may require some internal padding
  6581. align = np.dtype('i').alignment
  6582. size = np.dtype('i').itemsize
  6583. def aligned(n):
  6584. return (align*(1 + (n-1)//align))
  6585. self._check('(3)T{ix}', (dict(
  6586. names=['f0'],
  6587. formats=['i'],
  6588. offsets=[0],
  6589. itemsize=aligned(size + 1)
  6590. ), (3,)))
  6591. def test_char_vs_string(self):
  6592. dt = np.dtype('c')
  6593. self._check('c', dt)
  6594. dt = np.dtype([('f0', 'S1', (4,)), ('f1', 'S4')])
  6595. self._check('4c4s', dt)
  6596. def test_field_order(self):
  6597. # gh-9053 - previously, we relied on dictionary key order
  6598. self._check("(0)I:a:f:b:", [('a', 'I', (0,)), ('b', 'f')])
  6599. self._check("(0)I:b:f:a:", [('b', 'I', (0,)), ('a', 'f')])
  6600. def test_unnamed_fields(self):
  6601. self._check('ii', [('f0', 'i'), ('f1', 'i')])
  6602. self._check('ii:f0:', [('f1', 'i'), ('f0', 'i')])
  6603. self._check('i', 'i')
  6604. self._check('i:f0:', [('f0', 'i')])
  6605. class TestNewBufferProtocol:
  6606. """ Test PEP3118 buffers """
  6607. def _check_roundtrip(self, obj):
  6608. obj = np.asarray(obj)
  6609. x = memoryview(obj)
  6610. y = np.asarray(x)
  6611. y2 = np.array(x)
  6612. assert_(not y.flags.owndata)
  6613. assert_(y2.flags.owndata)
  6614. assert_equal(y.dtype, obj.dtype)
  6615. assert_equal(y.shape, obj.shape)
  6616. assert_array_equal(obj, y)
  6617. assert_equal(y2.dtype, obj.dtype)
  6618. assert_equal(y2.shape, obj.shape)
  6619. assert_array_equal(obj, y2)
  6620. def test_roundtrip(self):
  6621. x = np.array([1, 2, 3, 4, 5], dtype='i4')
  6622. self._check_roundtrip(x)
  6623. x = np.array([[1, 2], [3, 4]], dtype=np.float64)
  6624. self._check_roundtrip(x)
  6625. x = np.zeros((3, 3, 3), dtype=np.float32)[:, 0,:]
  6626. self._check_roundtrip(x)
  6627. dt = [('a', 'b'),
  6628. ('b', 'h'),
  6629. ('c', 'i'),
  6630. ('d', 'l'),
  6631. ('dx', 'q'),
  6632. ('e', 'B'),
  6633. ('f', 'H'),
  6634. ('g', 'I'),
  6635. ('h', 'L'),
  6636. ('hx', 'Q'),
  6637. ('i', np.single),
  6638. ('j', np.double),
  6639. ('k', np.longdouble),
  6640. ('ix', np.csingle),
  6641. ('jx', np.cdouble),
  6642. ('kx', np.clongdouble),
  6643. ('l', 'S4'),
  6644. ('m', 'U4'),
  6645. ('n', 'V3'),
  6646. ('o', '?'),
  6647. ('p', np.half),
  6648. ]
  6649. x = np.array(
  6650. [(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6651. b'aaaa', 'bbbb', b'xxx', True, 1.0)],
  6652. dtype=dt)
  6653. self._check_roundtrip(x)
  6654. x = np.array(([[1, 2], [3, 4]],), dtype=[('a', (int, (2, 2)))])
  6655. self._check_roundtrip(x)
  6656. x = np.array([1, 2, 3], dtype='>i2')
  6657. self._check_roundtrip(x)
  6658. x = np.array([1, 2, 3], dtype='<i2')
  6659. self._check_roundtrip(x)
  6660. x = np.array([1, 2, 3], dtype='>i4')
  6661. self._check_roundtrip(x)
  6662. x = np.array([1, 2, 3], dtype='<i4')
  6663. self._check_roundtrip(x)
  6664. # check long long can be represented as non-native
  6665. x = np.array([1, 2, 3], dtype='>q')
  6666. self._check_roundtrip(x)
  6667. # Native-only data types can be passed through the buffer interface
  6668. # only in native byte order
  6669. if sys.byteorder == 'little':
  6670. x = np.array([1, 2, 3], dtype='>g')
  6671. assert_raises(ValueError, self._check_roundtrip, x)
  6672. x = np.array([1, 2, 3], dtype='<g')
  6673. self._check_roundtrip(x)
  6674. else:
  6675. x = np.array([1, 2, 3], dtype='>g')
  6676. self._check_roundtrip(x)
  6677. x = np.array([1, 2, 3], dtype='<g')
  6678. assert_raises(ValueError, self._check_roundtrip, x)
  6679. def test_roundtrip_half(self):
  6680. half_list = [
  6681. 1.0,
  6682. -2.0,
  6683. 6.5504 * 10**4, # (max half precision)
  6684. 2**-14, # ~= 6.10352 * 10**-5 (minimum positive normal)
  6685. 2**-24, # ~= 5.96046 * 10**-8 (minimum strictly positive subnormal)
  6686. 0.0,
  6687. -0.0,
  6688. float('+inf'),
  6689. float('-inf'),
  6690. 0.333251953125, # ~= 1/3
  6691. ]
  6692. x = np.array(half_list, dtype='>e')
  6693. self._check_roundtrip(x)
  6694. x = np.array(half_list, dtype='<e')
  6695. self._check_roundtrip(x)
  6696. def test_roundtrip_single_types(self):
  6697. for typ in np.sctypeDict.values():
  6698. dtype = np.dtype(typ)
  6699. if dtype.char in 'Mm':
  6700. # datetimes cannot be used in buffers
  6701. continue
  6702. if dtype.char == 'V':
  6703. # skip void
  6704. continue
  6705. x = np.zeros(4, dtype=dtype)
  6706. self._check_roundtrip(x)
  6707. if dtype.char not in 'qQgG':
  6708. dt = dtype.newbyteorder('<')
  6709. x = np.zeros(4, dtype=dt)
  6710. self._check_roundtrip(x)
  6711. dt = dtype.newbyteorder('>')
  6712. x = np.zeros(4, dtype=dt)
  6713. self._check_roundtrip(x)
  6714. def test_roundtrip_scalar(self):
  6715. # Issue #4015.
  6716. self._check_roundtrip(0)
  6717. def test_invalid_buffer_format(self):
  6718. # datetime64 cannot be used fully in a buffer yet
  6719. # Should be fixed in the next Numpy major release
  6720. dt = np.dtype([('a', 'uint16'), ('b', 'M8[s]')])
  6721. a = np.empty(3, dt)
  6722. assert_raises((ValueError, BufferError), memoryview, a)
  6723. assert_raises((ValueError, BufferError), memoryview, np.array((3), 'M8[D]'))
  6724. def test_export_simple_1d(self):
  6725. x = np.array([1, 2, 3, 4, 5], dtype='i')
  6726. y = memoryview(x)
  6727. assert_equal(y.format, 'i')
  6728. assert_equal(y.shape, (5,))
  6729. assert_equal(y.ndim, 1)
  6730. assert_equal(y.strides, (4,))
  6731. assert_equal(y.suboffsets, ())
  6732. assert_equal(y.itemsize, 4)
  6733. def test_export_simple_nd(self):
  6734. x = np.array([[1, 2], [3, 4]], dtype=np.float64)
  6735. y = memoryview(x)
  6736. assert_equal(y.format, 'd')
  6737. assert_equal(y.shape, (2, 2))
  6738. assert_equal(y.ndim, 2)
  6739. assert_equal(y.strides, (16, 8))
  6740. assert_equal(y.suboffsets, ())
  6741. assert_equal(y.itemsize, 8)
  6742. def test_export_discontiguous(self):
  6743. x = np.zeros((3, 3, 3), dtype=np.float32)[:, 0,:]
  6744. y = memoryview(x)
  6745. assert_equal(y.format, 'f')
  6746. assert_equal(y.shape, (3, 3))
  6747. assert_equal(y.ndim, 2)
  6748. assert_equal(y.strides, (36, 4))
  6749. assert_equal(y.suboffsets, ())
  6750. assert_equal(y.itemsize, 4)
  6751. def test_export_record(self):
  6752. dt = [('a', 'b'),
  6753. ('b', 'h'),
  6754. ('c', 'i'),
  6755. ('d', 'l'),
  6756. ('dx', 'q'),
  6757. ('e', 'B'),
  6758. ('f', 'H'),
  6759. ('g', 'I'),
  6760. ('h', 'L'),
  6761. ('hx', 'Q'),
  6762. ('i', np.single),
  6763. ('j', np.double),
  6764. ('k', np.longdouble),
  6765. ('ix', np.csingle),
  6766. ('jx', np.cdouble),
  6767. ('kx', np.clongdouble),
  6768. ('l', 'S4'),
  6769. ('m', 'U4'),
  6770. ('n', 'V3'),
  6771. ('o', '?'),
  6772. ('p', np.half),
  6773. ]
  6774. x = np.array(
  6775. [(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6776. b'aaaa', 'bbbb', b' ', True, 1.0)],
  6777. dtype=dt)
  6778. y = memoryview(x)
  6779. assert_equal(y.shape, (1,))
  6780. assert_equal(y.ndim, 1)
  6781. assert_equal(y.suboffsets, ())
  6782. sz = sum([np.dtype(b).itemsize for a, b in dt])
  6783. if np.dtype('l').itemsize == 4:
  6784. 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:}')
  6785. else:
  6786. 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:}')
  6787. # Cannot test if NPY_RELAXED_STRIDES_DEBUG changes the strides
  6788. if not (np.ones(1).strides[0] == np.iinfo(np.intp).max):
  6789. assert_equal(y.strides, (sz,))
  6790. assert_equal(y.itemsize, sz)
  6791. def test_export_subarray(self):
  6792. x = np.array(([[1, 2], [3, 4]],), dtype=[('a', ('i', (2, 2)))])
  6793. y = memoryview(x)
  6794. assert_equal(y.format, 'T{(2,2)i:a:}')
  6795. assert_equal(y.shape, ())
  6796. assert_equal(y.ndim, 0)
  6797. assert_equal(y.strides, ())
  6798. assert_equal(y.suboffsets, ())
  6799. assert_equal(y.itemsize, 16)
  6800. def test_export_endian(self):
  6801. x = np.array([1, 2, 3], dtype='>i')
  6802. y = memoryview(x)
  6803. if sys.byteorder == 'little':
  6804. assert_equal(y.format, '>i')
  6805. else:
  6806. assert_equal(y.format, 'i')
  6807. x = np.array([1, 2, 3], dtype='<i')
  6808. y = memoryview(x)
  6809. if sys.byteorder == 'little':
  6810. assert_equal(y.format, 'i')
  6811. else:
  6812. assert_equal(y.format, '<i')
  6813. def test_export_flags(self):
  6814. # Check SIMPLE flag, see also gh-3613 (exception should be BufferError)
  6815. assert_raises(ValueError,
  6816. _multiarray_tests.get_buffer_info,
  6817. np.arange(5)[::2], ('SIMPLE',))
  6818. @pytest.mark.parametrize(["obj", "error"], [
  6819. pytest.param(np.array([1, 2], dtype=rational), ValueError, id="array"),
  6820. pytest.param(rational(1, 2), TypeError, id="scalar")])
  6821. def test_export_and_pickle_user_dtype(self, obj, error):
  6822. # User dtypes should export successfully when FORMAT was not requested.
  6823. with pytest.raises(error):
  6824. _multiarray_tests.get_buffer_info(obj, ("STRIDED_RO", "FORMAT"))
  6825. _multiarray_tests.get_buffer_info(obj, ("STRIDED_RO",))
  6826. # This is currently also necessary to implement pickling:
  6827. pickle_obj = pickle.dumps(obj)
  6828. res = pickle.loads(pickle_obj)
  6829. assert_array_equal(res, obj)
  6830. def test_padding(self):
  6831. for j in range(8):
  6832. x = np.array([(1,), (2,)], dtype={'f0': (int, j)})
  6833. self._check_roundtrip(x)
  6834. def test_reference_leak(self):
  6835. if HAS_REFCOUNT:
  6836. count_1 = sys.getrefcount(np.core._internal)
  6837. a = np.zeros(4)
  6838. b = memoryview(a)
  6839. c = np.asarray(b)
  6840. if HAS_REFCOUNT:
  6841. count_2 = sys.getrefcount(np.core._internal)
  6842. assert_equal(count_1, count_2)
  6843. del c # avoid pyflakes unused variable warning.
  6844. def test_padded_struct_array(self):
  6845. dt1 = np.dtype(
  6846. [('a', 'b'), ('b', 'i'), ('sub', np.dtype('b,i')), ('c', 'i')],
  6847. align=True)
  6848. x1 = np.arange(dt1.itemsize, dtype=np.int8).view(dt1)
  6849. self._check_roundtrip(x1)
  6850. dt2 = np.dtype(
  6851. [('a', 'b'), ('b', 'i'), ('c', 'b', (3,)), ('d', 'i')],
  6852. align=True)
  6853. x2 = np.arange(dt2.itemsize, dtype=np.int8).view(dt2)
  6854. self._check_roundtrip(x2)
  6855. dt3 = np.dtype(
  6856. [('a', 'b'), ('b', 'i'), ('c', 'b'), ('d', 'b'),
  6857. ('e', 'b'), ('sub', np.dtype('b,i', align=True))])
  6858. x3 = np.arange(dt3.itemsize, dtype=np.int8).view(dt3)
  6859. self._check_roundtrip(x3)
  6860. @pytest.mark.valgrind_error(reason="leaks buffer info cache temporarily.")
  6861. def test_relaxed_strides(self, c=np.ones((1, 10, 10), dtype='i8')):
  6862. # Note: c defined as parameter so that it is persistent and leak
  6863. # checks will notice gh-16934 (buffer info cache leak).
  6864. c.strides = (-1, 80, 8) # strides need to be fixed at export
  6865. assert_(memoryview(c).strides == (800, 80, 8))
  6866. # Writing C-contiguous data to a BytesIO buffer should work
  6867. fd = io.BytesIO()
  6868. fd.write(c.data)
  6869. fortran = c.T
  6870. assert_(memoryview(fortran).strides == (8, 80, 800))
  6871. arr = np.ones((1, 10))
  6872. if arr.flags.f_contiguous:
  6873. shape, strides = _multiarray_tests.get_buffer_info(
  6874. arr, ['F_CONTIGUOUS'])
  6875. assert_(strides[0] == 8)
  6876. arr = np.ones((10, 1), order='F')
  6877. shape, strides = _multiarray_tests.get_buffer_info(
  6878. arr, ['C_CONTIGUOUS'])
  6879. assert_(strides[-1] == 8)
  6880. @pytest.mark.valgrind_error(reason="leaks buffer info cache temporarily.")
  6881. @pytest.mark.skipif(not np.ones((10, 1), order="C").flags.f_contiguous,
  6882. reason="Test is unnecessary (but fails) without relaxed strides.")
  6883. def test_relaxed_strides_buffer_info_leak(self, arr=np.ones((1, 10))):
  6884. """Test that alternating export of C- and F-order buffers from
  6885. an array which is both C- and F-order when relaxed strides is
  6886. active works.
  6887. This test defines array in the signature to ensure leaking more
  6888. references every time the test is run (catching the leak with
  6889. pytest-leaks).
  6890. """
  6891. for i in range(10):
  6892. _, s = _multiarray_tests.get_buffer_info(arr, ['F_CONTIGUOUS'])
  6893. assert s == (8, 8)
  6894. _, s = _multiarray_tests.get_buffer_info(arr, ['C_CONTIGUOUS'])
  6895. assert s == (80, 8)
  6896. def test_out_of_order_fields(self):
  6897. dt = np.dtype(dict(
  6898. formats=['<i4', '<i4'],
  6899. names=['one', 'two'],
  6900. offsets=[4, 0],
  6901. itemsize=8
  6902. ))
  6903. # overlapping fields cannot be represented by PEP3118
  6904. arr = np.empty(1, dt)
  6905. with assert_raises(ValueError):
  6906. memoryview(arr)
  6907. def test_max_dims(self):
  6908. a = np.ones((1,) * 32)
  6909. self._check_roundtrip(a)
  6910. @pytest.mark.slow
  6911. def test_error_too_many_dims(self):
  6912. def make_ctype(shape, scalar_type):
  6913. t = scalar_type
  6914. for dim in shape[::-1]:
  6915. t = dim * t
  6916. return t
  6917. # construct a memoryview with 33 dimensions
  6918. c_u8_33d = make_ctype((1,)*33, ctypes.c_uint8)
  6919. m = memoryview(c_u8_33d())
  6920. assert_equal(m.ndim, 33)
  6921. assert_raises_regex(
  6922. RuntimeError, "ndim",
  6923. np.array, m)
  6924. # The above seems to create some deep cycles, clean them up for
  6925. # easier reference count debugging:
  6926. del c_u8_33d, m
  6927. for i in range(33):
  6928. if gc.collect() == 0:
  6929. break
  6930. def test_error_pointer_type(self):
  6931. # gh-6741
  6932. m = memoryview(ctypes.pointer(ctypes.c_uint8()))
  6933. assert_('&' in m.format)
  6934. assert_raises_regex(
  6935. ValueError, "format string",
  6936. np.array, m)
  6937. def test_error_message_unsupported(self):
  6938. # wchar has no corresponding numpy type - if this changes in future, we
  6939. # need a better way to construct an invalid memoryview format.
  6940. t = ctypes.c_wchar * 4
  6941. with assert_raises(ValueError) as cm:
  6942. np.array(t())
  6943. exc = cm.exception
  6944. with assert_raises_regex(
  6945. NotImplementedError,
  6946. r"Unrepresentable .* 'u' \(UCS-2 strings\)"
  6947. ):
  6948. raise exc.__cause__
  6949. def test_ctypes_integer_via_memoryview(self):
  6950. # gh-11150, due to bpo-10746
  6951. for c_integer in {ctypes.c_int, ctypes.c_long, ctypes.c_longlong}:
  6952. value = c_integer(42)
  6953. with warnings.catch_warnings(record=True):
  6954. warnings.filterwarnings('always', r'.*\bctypes\b', RuntimeWarning)
  6955. np.asarray(value)
  6956. def test_ctypes_struct_via_memoryview(self):
  6957. # gh-10528
  6958. class foo(ctypes.Structure):
  6959. _fields_ = [('a', ctypes.c_uint8), ('b', ctypes.c_uint32)]
  6960. f = foo(a=1, b=2)
  6961. with warnings.catch_warnings(record=True):
  6962. warnings.filterwarnings('always', r'.*\bctypes\b', RuntimeWarning)
  6963. arr = np.asarray(f)
  6964. assert_equal(arr['a'], 1)
  6965. assert_equal(arr['b'], 2)
  6966. f.a = 3
  6967. assert_equal(arr['a'], 3)
  6968. @pytest.mark.parametrize("obj", [np.ones(3), np.ones(1, dtype="i,i")[()]])
  6969. def test_error_if_stored_buffer_info_is_corrupted(self, obj):
  6970. """
  6971. If a user extends a NumPy array before 1.20 and then runs it
  6972. on NumPy 1.20+. A C-subclassed array might in theory modify
  6973. the new buffer-info field. This checks that an error is raised
  6974. if this happens (for buffer export), an error is written on delete.
  6975. This is a sanity check to help users transition to safe code, it
  6976. may be deleted at any point.
  6977. """
  6978. # corrupt buffer info:
  6979. _multiarray_tests.corrupt_or_fix_bufferinfo(obj)
  6980. name = type(obj)
  6981. with pytest.raises(RuntimeError,
  6982. match=f".*{name} appears to be C subclassed"):
  6983. memoryview(obj)
  6984. # Fix buffer info again before we delete (or we lose the memory)
  6985. _multiarray_tests.corrupt_or_fix_bufferinfo(obj)
  6986. def test_no_suboffsets(self):
  6987. try:
  6988. import _testbuffer
  6989. except ImportError:
  6990. raise pytest.skip("_testbuffer is not available")
  6991. for shape in [(2, 3), (2, 3, 4)]:
  6992. data = list(range(np.prod(shape)))
  6993. buffer = _testbuffer.ndarray(data, shape, format='i',
  6994. flags=_testbuffer.ND_PIL)
  6995. msg = "NumPy currently does not support.*suboffsets"
  6996. with pytest.raises(BufferError, match=msg):
  6997. np.asarray(buffer)
  6998. with pytest.raises(BufferError, match=msg):
  6999. np.asarray([buffer])
  7000. # Also check (unrelated and more limited but similar) frombuffer:
  7001. with pytest.raises(BufferError):
  7002. np.frombuffer(buffer)
  7003. class TestArrayCreationCopyArgument(object):
  7004. class RaiseOnBool:
  7005. def __bool__(self):
  7006. raise ValueError
  7007. true_vals = [True, np._CopyMode.ALWAYS, np.True_]
  7008. false_vals = [False, np._CopyMode.IF_NEEDED, np.False_]
  7009. def test_scalars(self):
  7010. # Test both numpy and python scalars
  7011. for dtype in np.typecodes["All"]:
  7012. arr = np.zeros((), dtype=dtype)
  7013. scalar = arr[()]
  7014. pyscalar = arr.item(0)
  7015. # Test never-copy raises error:
  7016. assert_raises(ValueError, np.array, scalar,
  7017. copy=np._CopyMode.NEVER)
  7018. assert_raises(ValueError, np.array, pyscalar,
  7019. copy=np._CopyMode.NEVER)
  7020. assert_raises(ValueError, np.array, pyscalar,
  7021. copy=self.RaiseOnBool())
  7022. assert_raises(ValueError, _multiarray_tests.npy_ensurenocopy,
  7023. [1])
  7024. # Casting with a dtype (to unsigned integers) can be special:
  7025. with pytest.raises(ValueError):
  7026. np.array(pyscalar, dtype=np.int64, copy=np._CopyMode.NEVER)
  7027. def test_compatible_cast(self):
  7028. # Some types are compatible even though they are different, no
  7029. # copy is necessary for them. This is mostly true for some integers
  7030. def int_types(byteswap=False):
  7031. int_types = (np.typecodes["Integer"] +
  7032. np.typecodes["UnsignedInteger"])
  7033. for int_type in int_types:
  7034. yield np.dtype(int_type)
  7035. if byteswap:
  7036. yield np.dtype(int_type).newbyteorder()
  7037. for int1 in int_types():
  7038. for int2 in int_types(True):
  7039. arr = np.arange(10, dtype=int1)
  7040. for copy in self.true_vals:
  7041. res = np.array(arr, copy=copy, dtype=int2)
  7042. assert res is not arr and res.flags.owndata
  7043. assert_array_equal(res, arr)
  7044. if int1 == int2:
  7045. # Casting is not necessary, base check is sufficient here
  7046. for copy in self.false_vals:
  7047. res = np.array(arr, copy=copy, dtype=int2)
  7048. assert res is arr or res.base is arr
  7049. res = np.array(arr,
  7050. copy=np._CopyMode.NEVER,
  7051. dtype=int2)
  7052. assert res is arr or res.base is arr
  7053. else:
  7054. # Casting is necessary, assert copy works:
  7055. for copy in self.false_vals:
  7056. res = np.array(arr, copy=copy, dtype=int2)
  7057. assert res is not arr and res.flags.owndata
  7058. assert_array_equal(res, arr)
  7059. assert_raises(ValueError, np.array,
  7060. arr, copy=np._CopyMode.NEVER,
  7061. dtype=int2)
  7062. assert_raises(ValueError, np.array,
  7063. arr, copy=None,
  7064. dtype=int2)
  7065. def test_buffer_interface(self):
  7066. # Buffer interface gives direct memory access (no copy)
  7067. arr = np.arange(10)
  7068. view = memoryview(arr)
  7069. # Checking bases is a bit tricky since numpy creates another
  7070. # memoryview, so use may_share_memory.
  7071. for copy in self.true_vals:
  7072. res = np.array(view, copy=copy)
  7073. assert not np.may_share_memory(arr, res)
  7074. for copy in self.false_vals:
  7075. res = np.array(view, copy=copy)
  7076. assert np.may_share_memory(arr, res)
  7077. res = np.array(view, copy=np._CopyMode.NEVER)
  7078. assert np.may_share_memory(arr, res)
  7079. def test_array_interfaces(self):
  7080. # Array interface gives direct memory access (much like a memoryview)
  7081. base_arr = np.arange(10)
  7082. class ArrayLike:
  7083. __array_interface__ = base_arr.__array_interface__
  7084. arr = ArrayLike()
  7085. for copy, val in [(True, None), (np._CopyMode.ALWAYS, None),
  7086. (False, arr), (np._CopyMode.IF_NEEDED, arr),
  7087. (np._CopyMode.NEVER, arr)]:
  7088. res = np.array(arr, copy=copy)
  7089. assert res.base is val
  7090. def test___array__(self):
  7091. base_arr = np.arange(10)
  7092. class ArrayLike:
  7093. def __array__(self):
  7094. # __array__ should return a copy, numpy cannot know this
  7095. # however.
  7096. return base_arr
  7097. arr = ArrayLike()
  7098. for copy in self.true_vals:
  7099. res = np.array(arr, copy=copy)
  7100. assert_array_equal(res, base_arr)
  7101. # An additional copy is currently forced by numpy in this case,
  7102. # you could argue, numpy does not trust the ArrayLike. This
  7103. # may be open for change:
  7104. assert res is not base_arr
  7105. for copy in self.false_vals:
  7106. res = np.array(arr, copy=False)
  7107. assert_array_equal(res, base_arr)
  7108. assert res is base_arr # numpy trusts the ArrayLike
  7109. with pytest.raises(ValueError):
  7110. np.array(arr, copy=np._CopyMode.NEVER)
  7111. @pytest.mark.parametrize(
  7112. "arr", [np.ones(()), np.arange(81).reshape((9, 9))])
  7113. @pytest.mark.parametrize("order1", ["C", "F", None])
  7114. @pytest.mark.parametrize("order2", ["C", "F", "A", "K"])
  7115. def test_order_mismatch(self, arr, order1, order2):
  7116. # The order is the main (python side) reason that can cause
  7117. # a never-copy to fail.
  7118. # Prepare C-order, F-order and non-contiguous arrays:
  7119. arr = arr.copy(order1)
  7120. if order1 == "C":
  7121. assert arr.flags.c_contiguous
  7122. elif order1 == "F":
  7123. assert arr.flags.f_contiguous
  7124. elif arr.ndim != 0:
  7125. # Make array non-contiguous
  7126. arr = arr[::2, ::2]
  7127. assert not arr.flags.forc
  7128. # Whether a copy is necessary depends on the order of arr:
  7129. if order2 == "C":
  7130. no_copy_necessary = arr.flags.c_contiguous
  7131. elif order2 == "F":
  7132. no_copy_necessary = arr.flags.f_contiguous
  7133. else:
  7134. # Keeporder and Anyorder are OK with non-contiguous output.
  7135. # This is not consistent with the `astype` behaviour which
  7136. # enforces contiguity for "A". It is probably historic from when
  7137. # "K" did not exist.
  7138. no_copy_necessary = True
  7139. # Test it for both the array and a memoryview
  7140. for view in [arr, memoryview(arr)]:
  7141. for copy in self.true_vals:
  7142. res = np.array(view, copy=copy, order=order2)
  7143. assert res is not arr and res.flags.owndata
  7144. assert_array_equal(arr, res)
  7145. if no_copy_necessary:
  7146. for copy in self.false_vals:
  7147. res = np.array(view, copy=copy, order=order2)
  7148. # res.base.obj refers to the memoryview
  7149. if not IS_PYPY:
  7150. assert res is arr or res.base.obj is arr
  7151. res = np.array(view, copy=np._CopyMode.NEVER,
  7152. order=order2)
  7153. if not IS_PYPY:
  7154. assert res is arr or res.base.obj is arr
  7155. else:
  7156. for copy in self.false_vals:
  7157. res = np.array(arr, copy=copy, order=order2)
  7158. assert_array_equal(arr, res)
  7159. assert_raises(ValueError, np.array,
  7160. view, copy=np._CopyMode.NEVER,
  7161. order=order2)
  7162. assert_raises(ValueError, np.array,
  7163. view, copy=None,
  7164. order=order2)
  7165. def test_striding_not_ok(self):
  7166. arr = np.array([[1, 2, 4], [3, 4, 5]])
  7167. assert_raises(ValueError, np.array,
  7168. arr.T, copy=np._CopyMode.NEVER,
  7169. order='C')
  7170. assert_raises(ValueError, np.array,
  7171. arr.T, copy=np._CopyMode.NEVER,
  7172. order='C', dtype=np.int64)
  7173. assert_raises(ValueError, np.array,
  7174. arr, copy=np._CopyMode.NEVER,
  7175. order='F')
  7176. assert_raises(ValueError, np.array,
  7177. arr, copy=np._CopyMode.NEVER,
  7178. order='F', dtype=np.int64)
  7179. class TestArrayAttributeDeletion:
  7180. def test_multiarray_writable_attributes_deletion(self):
  7181. # ticket #2046, should not seqfault, raise AttributeError
  7182. a = np.ones(2)
  7183. attr = ['shape', 'strides', 'data', 'dtype', 'real', 'imag', 'flat']
  7184. with suppress_warnings() as sup:
  7185. sup.filter(DeprecationWarning, "Assigning the 'data' attribute")
  7186. for s in attr:
  7187. assert_raises(AttributeError, delattr, a, s)
  7188. def test_multiarray_not_writable_attributes_deletion(self):
  7189. a = np.ones(2)
  7190. attr = ["ndim", "flags", "itemsize", "size", "nbytes", "base",
  7191. "ctypes", "T", "__array_interface__", "__array_struct__",
  7192. "__array_priority__", "__array_finalize__"]
  7193. for s in attr:
  7194. assert_raises(AttributeError, delattr, a, s)
  7195. def test_multiarray_flags_writable_attribute_deletion(self):
  7196. a = np.ones(2).flags
  7197. attr = ['writebackifcopy', 'updateifcopy', 'aligned', 'writeable']
  7198. for s in attr:
  7199. assert_raises(AttributeError, delattr, a, s)
  7200. def test_multiarray_flags_not_writable_attribute_deletion(self):
  7201. a = np.ones(2).flags
  7202. attr = ["contiguous", "c_contiguous", "f_contiguous", "fortran",
  7203. "owndata", "fnc", "forc", "behaved", "carray", "farray",
  7204. "num"]
  7205. for s in attr:
  7206. assert_raises(AttributeError, delattr, a, s)
  7207. class TestArrayInterface():
  7208. class Foo:
  7209. def __init__(self, value):
  7210. self.value = value
  7211. self.iface = {'typestr': 'f8'}
  7212. def __float__(self):
  7213. return float(self.value)
  7214. @property
  7215. def __array_interface__(self):
  7216. return self.iface
  7217. f = Foo(0.5)
  7218. @pytest.mark.parametrize('val, iface, expected', [
  7219. (f, {}, 0.5),
  7220. ([f], {}, [0.5]),
  7221. ([f, f], {}, [0.5, 0.5]),
  7222. (f, {'shape': ()}, 0.5),
  7223. (f, {'shape': None}, TypeError),
  7224. (f, {'shape': (1, 1)}, [[0.5]]),
  7225. (f, {'shape': (2,)}, ValueError),
  7226. (f, {'strides': ()}, 0.5),
  7227. (f, {'strides': (2,)}, ValueError),
  7228. (f, {'strides': 16}, TypeError),
  7229. ])
  7230. def test_scalar_interface(self, val, iface, expected):
  7231. # Test scalar coercion within the array interface
  7232. self.f.iface = {'typestr': 'f8'}
  7233. self.f.iface.update(iface)
  7234. if HAS_REFCOUNT:
  7235. pre_cnt = sys.getrefcount(np.dtype('f8'))
  7236. if isinstance(expected, type):
  7237. assert_raises(expected, np.array, val)
  7238. else:
  7239. result = np.array(val)
  7240. assert_equal(np.array(val), expected)
  7241. assert result.dtype == 'f8'
  7242. del result
  7243. if HAS_REFCOUNT:
  7244. post_cnt = sys.getrefcount(np.dtype('f8'))
  7245. assert_equal(pre_cnt, post_cnt)
  7246. def test_interface_no_shape():
  7247. class ArrayLike:
  7248. array = np.array(1)
  7249. __array_interface__ = array.__array_interface__
  7250. assert_equal(np.array(ArrayLike()), 1)
  7251. def test_array_interface_itemsize():
  7252. # See gh-6361
  7253. my_dtype = np.dtype({'names': ['A', 'B'], 'formats': ['f4', 'f4'],
  7254. 'offsets': [0, 8], 'itemsize': 16})
  7255. a = np.ones(10, dtype=my_dtype)
  7256. descr_t = np.dtype(a.__array_interface__['descr'])
  7257. typestr_t = np.dtype(a.__array_interface__['typestr'])
  7258. assert_equal(descr_t.itemsize, typestr_t.itemsize)
  7259. def test_array_interface_empty_shape():
  7260. # See gh-7994
  7261. arr = np.array([1, 2, 3])
  7262. interface1 = dict(arr.__array_interface__)
  7263. interface1['shape'] = ()
  7264. class DummyArray1:
  7265. __array_interface__ = interface1
  7266. # NOTE: Because Py2 str/Py3 bytes supports the buffer interface, setting
  7267. # the interface data to bytes would invoke the bug this tests for, that
  7268. # __array_interface__ with shape=() is not allowed if the data is an object
  7269. # exposing the buffer interface
  7270. interface2 = dict(interface1)
  7271. interface2['data'] = arr[0].tobytes()
  7272. class DummyArray2:
  7273. __array_interface__ = interface2
  7274. arr1 = np.asarray(DummyArray1())
  7275. arr2 = np.asarray(DummyArray2())
  7276. arr3 = arr[:1].reshape(())
  7277. assert_equal(arr1, arr2)
  7278. assert_equal(arr1, arr3)
  7279. def test_array_interface_offset():
  7280. arr = np.array([1, 2, 3], dtype='int32')
  7281. interface = dict(arr.__array_interface__)
  7282. interface['data'] = memoryview(arr)
  7283. interface['shape'] = (2,)
  7284. interface['offset'] = 4
  7285. class DummyArray:
  7286. __array_interface__ = interface
  7287. arr1 = np.asarray(DummyArray())
  7288. assert_equal(arr1, arr[1:])
  7289. def test_array_interface_unicode_typestr():
  7290. arr = np.array([1, 2, 3], dtype='int32')
  7291. interface = dict(arr.__array_interface__)
  7292. interface['typestr'] = '\N{check mark}'
  7293. class DummyArray:
  7294. __array_interface__ = interface
  7295. # should not be UnicodeEncodeError
  7296. with pytest.raises(TypeError):
  7297. np.asarray(DummyArray())
  7298. def test_flat_element_deletion():
  7299. it = np.ones(3).flat
  7300. try:
  7301. del it[1]
  7302. del it[1:2]
  7303. except TypeError:
  7304. pass
  7305. except Exception:
  7306. raise AssertionError
  7307. def test_scalar_element_deletion():
  7308. a = np.zeros(2, dtype=[('x', 'int'), ('y', 'int')])
  7309. assert_raises(ValueError, a[0].__delitem__, 'x')
  7310. class TestMapIter:
  7311. def test_mapiter(self):
  7312. # The actual tests are within the C code in
  7313. # multiarray/_multiarray_tests.c.src
  7314. a = np.arange(12).reshape((3, 4)).astype(float)
  7315. index = ([1, 1, 2, 0],
  7316. [0, 0, 2, 3])
  7317. vals = [50, 50, 30, 16]
  7318. _multiarray_tests.test_inplace_increment(a, index, vals)
  7319. assert_equal(a, [[0.00, 1., 2.0, 19.],
  7320. [104., 5., 6.0, 7.0],
  7321. [8.00, 9., 40., 11.]])
  7322. b = np.arange(6).astype(float)
  7323. index = (np.array([1, 2, 0]),)
  7324. vals = [50, 4, 100.1]
  7325. _multiarray_tests.test_inplace_increment(b, index, vals)
  7326. assert_equal(b, [100.1, 51., 6., 3., 4., 5.])
  7327. class TestAsCArray:
  7328. def test_1darray(self):
  7329. array = np.arange(24, dtype=np.double)
  7330. from_c = _multiarray_tests.test_as_c_array(array, 3)
  7331. assert_equal(array[3], from_c)
  7332. def test_2darray(self):
  7333. array = np.arange(24, dtype=np.double).reshape(3, 8)
  7334. from_c = _multiarray_tests.test_as_c_array(array, 2, 4)
  7335. assert_equal(array[2, 4], from_c)
  7336. def test_3darray(self):
  7337. array = np.arange(24, dtype=np.double).reshape(2, 3, 4)
  7338. from_c = _multiarray_tests.test_as_c_array(array, 1, 2, 3)
  7339. assert_equal(array[1, 2, 3], from_c)
  7340. class TestConversion:
  7341. def test_array_scalar_relational_operation(self):
  7342. # All integer
  7343. for dt1 in np.typecodes['AllInteger']:
  7344. assert_(1 > np.array(0, dtype=dt1), "type %s failed" % (dt1,))
  7345. assert_(not 1 < np.array(0, dtype=dt1), "type %s failed" % (dt1,))
  7346. for dt2 in np.typecodes['AllInteger']:
  7347. assert_(np.array(1, dtype=dt1) > np.array(0, dtype=dt2),
  7348. "type %s and %s failed" % (dt1, dt2))
  7349. assert_(not np.array(1, dtype=dt1) < np.array(0, dtype=dt2),
  7350. "type %s and %s failed" % (dt1, dt2))
  7351. # Unsigned integers
  7352. for dt1 in 'BHILQP':
  7353. assert_(-1 < np.array(1, dtype=dt1), "type %s failed" % (dt1,))
  7354. assert_(not -1 > np.array(1, dtype=dt1), "type %s failed" % (dt1,))
  7355. assert_(-1 != np.array(1, dtype=dt1), "type %s failed" % (dt1,))
  7356. # Unsigned vs signed
  7357. for dt2 in 'bhilqp':
  7358. assert_(np.array(1, dtype=dt1) > np.array(-1, dtype=dt2),
  7359. "type %s and %s failed" % (dt1, dt2))
  7360. assert_(not np.array(1, dtype=dt1) < np.array(-1, dtype=dt2),
  7361. "type %s and %s failed" % (dt1, dt2))
  7362. assert_(np.array(1, dtype=dt1) != np.array(-1, dtype=dt2),
  7363. "type %s and %s failed" % (dt1, dt2))
  7364. # Signed integers and floats
  7365. for dt1 in 'bhlqp' + np.typecodes['Float']:
  7366. assert_(1 > np.array(-1, dtype=dt1), "type %s failed" % (dt1,))
  7367. assert_(not 1 < np.array(-1, dtype=dt1), "type %s failed" % (dt1,))
  7368. assert_(-1 == np.array(-1, dtype=dt1), "type %s failed" % (dt1,))
  7369. for dt2 in 'bhlqp' + np.typecodes['Float']:
  7370. assert_(np.array(1, dtype=dt1) > np.array(-1, dtype=dt2),
  7371. "type %s and %s failed" % (dt1, dt2))
  7372. assert_(not np.array(1, dtype=dt1) < np.array(-1, dtype=dt2),
  7373. "type %s and %s failed" % (dt1, dt2))
  7374. assert_(np.array(-1, dtype=dt1) == np.array(-1, dtype=dt2),
  7375. "type %s and %s failed" % (dt1, dt2))
  7376. def test_to_bool_scalar(self):
  7377. assert_equal(bool(np.array([False])), False)
  7378. assert_equal(bool(np.array([True])), True)
  7379. assert_equal(bool(np.array([[42]])), True)
  7380. assert_raises(ValueError, bool, np.array([1, 2]))
  7381. class NotConvertible:
  7382. def __bool__(self):
  7383. raise NotImplementedError
  7384. assert_raises(NotImplementedError, bool, np.array(NotConvertible()))
  7385. assert_raises(NotImplementedError, bool, np.array([NotConvertible()]))
  7386. if IS_PYSTON:
  7387. pytest.skip("Pyston disables recursion checking")
  7388. self_containing = np.array([None])
  7389. self_containing[0] = self_containing
  7390. Error = RecursionError
  7391. assert_raises(Error, bool, self_containing) # previously stack overflow
  7392. self_containing[0] = None # resolve circular reference
  7393. def test_to_int_scalar(self):
  7394. # gh-9972 means that these aren't always the same
  7395. int_funcs = (int, lambda x: x.__int__())
  7396. for int_func in int_funcs:
  7397. assert_equal(int_func(np.array(0)), 0)
  7398. assert_equal(int_func(np.array([1])), 1)
  7399. assert_equal(int_func(np.array([[42]])), 42)
  7400. assert_raises(TypeError, int_func, np.array([1, 2]))
  7401. # gh-9972
  7402. assert_equal(4, int_func(np.array('4')))
  7403. assert_equal(5, int_func(np.bytes_(b'5')))
  7404. assert_equal(6, int_func(np.unicode_('6')))
  7405. # The delegation of int() to __trunc__ was deprecated in
  7406. # Python 3.11.
  7407. if sys.version_info < (3, 11):
  7408. class HasTrunc:
  7409. def __trunc__(self):
  7410. return 3
  7411. assert_equal(3, int_func(np.array(HasTrunc())))
  7412. assert_equal(3, int_func(np.array([HasTrunc()])))
  7413. else:
  7414. pass
  7415. class NotConvertible:
  7416. def __int__(self):
  7417. raise NotImplementedError
  7418. assert_raises(NotImplementedError,
  7419. int_func, np.array(NotConvertible()))
  7420. assert_raises(NotImplementedError,
  7421. int_func, np.array([NotConvertible()]))
  7422. class TestWhere:
  7423. def test_basic(self):
  7424. dts = [bool, np.int16, np.int32, np.int64, np.double, np.complex128,
  7425. np.longdouble, np.clongdouble]
  7426. for dt in dts:
  7427. c = np.ones(53, dtype=bool)
  7428. assert_equal(np.where( c, dt(0), dt(1)), dt(0))
  7429. assert_equal(np.where(~c, dt(0), dt(1)), dt(1))
  7430. assert_equal(np.where(True, dt(0), dt(1)), dt(0))
  7431. assert_equal(np.where(False, dt(0), dt(1)), dt(1))
  7432. d = np.ones_like(c).astype(dt)
  7433. e = np.zeros_like(d)
  7434. r = d.astype(dt)
  7435. c[7] = False
  7436. r[7] = e[7]
  7437. assert_equal(np.where(c, e, e), e)
  7438. assert_equal(np.where(c, d, e), r)
  7439. assert_equal(np.where(c, d, e[0]), r)
  7440. assert_equal(np.where(c, d[0], e), r)
  7441. assert_equal(np.where(c[::2], d[::2], e[::2]), r[::2])
  7442. assert_equal(np.where(c[1::2], d[1::2], e[1::2]), r[1::2])
  7443. assert_equal(np.where(c[::3], d[::3], e[::3]), r[::3])
  7444. assert_equal(np.where(c[1::3], d[1::3], e[1::3]), r[1::3])
  7445. assert_equal(np.where(c[::-2], d[::-2], e[::-2]), r[::-2])
  7446. assert_equal(np.where(c[::-3], d[::-3], e[::-3]), r[::-3])
  7447. assert_equal(np.where(c[1::-3], d[1::-3], e[1::-3]), r[1::-3])
  7448. def test_exotic(self):
  7449. # object
  7450. assert_array_equal(np.where(True, None, None), np.array(None))
  7451. # zero sized
  7452. m = np.array([], dtype=bool).reshape(0, 3)
  7453. b = np.array([], dtype=np.float64).reshape(0, 3)
  7454. assert_array_equal(np.where(m, 0, b), np.array([]).reshape(0, 3))
  7455. # object cast
  7456. d = np.array([-1.34, -0.16, -0.54, -0.31, -0.08, -0.95, 0.000, 0.313,
  7457. 0.547, -0.18, 0.876, 0.236, 1.969, 0.310, 0.699, 1.013,
  7458. 1.267, 0.229, -1.39, 0.487])
  7459. nan = float('NaN')
  7460. e = np.array(['5z', '0l', nan, 'Wz', nan, nan, 'Xq', 'cs', nan, nan,
  7461. 'QN', nan, nan, 'Fd', nan, nan, 'kp', nan, '36', 'i1'],
  7462. dtype=object)
  7463. m = np.array([0, 0, 1, 0, 1, 1, 0, 0, 1, 1,
  7464. 0, 1, 1, 0, 1, 1, 0, 1, 0, 0], dtype=bool)
  7465. r = e[:]
  7466. r[np.where(m)] = d[np.where(m)]
  7467. assert_array_equal(np.where(m, d, e), r)
  7468. r = e[:]
  7469. r[np.where(~m)] = d[np.where(~m)]
  7470. assert_array_equal(np.where(m, e, d), r)
  7471. assert_array_equal(np.where(m, e, e), e)
  7472. # minimal dtype result with NaN scalar (e.g required by pandas)
  7473. d = np.array([1., 2.], dtype=np.float32)
  7474. e = float('NaN')
  7475. assert_equal(np.where(True, d, e).dtype, np.float32)
  7476. e = float('Infinity')
  7477. assert_equal(np.where(True, d, e).dtype, np.float32)
  7478. e = float('-Infinity')
  7479. assert_equal(np.where(True, d, e).dtype, np.float32)
  7480. # also check upcast
  7481. e = float(1e150)
  7482. assert_equal(np.where(True, d, e).dtype, np.float64)
  7483. def test_ndim(self):
  7484. c = [True, False]
  7485. a = np.zeros((2, 25))
  7486. b = np.ones((2, 25))
  7487. r = np.where(np.array(c)[:,np.newaxis], a, b)
  7488. assert_array_equal(r[0], a[0])
  7489. assert_array_equal(r[1], b[0])
  7490. a = a.T
  7491. b = b.T
  7492. r = np.where(c, a, b)
  7493. assert_array_equal(r[:,0], a[:,0])
  7494. assert_array_equal(r[:,1], b[:,0])
  7495. def test_dtype_mix(self):
  7496. c = np.array([False, True, False, False, False, False, True, False,
  7497. False, False, True, False])
  7498. a = np.uint32(1)
  7499. b = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.],
  7500. dtype=np.float64)
  7501. r = np.array([5., 1., 3., 2., -1., -4., 1., -10., 10., 1., 1., 3.],
  7502. dtype=np.float64)
  7503. assert_equal(np.where(c, a, b), r)
  7504. a = a.astype(np.float32)
  7505. b = b.astype(np.int64)
  7506. assert_equal(np.where(c, a, b), r)
  7507. # non bool mask
  7508. c = c.astype(int)
  7509. c[c != 0] = 34242324
  7510. assert_equal(np.where(c, a, b), r)
  7511. # invert
  7512. tmpmask = c != 0
  7513. c[c == 0] = 41247212
  7514. c[tmpmask] = 0
  7515. assert_equal(np.where(c, b, a), r)
  7516. def test_foreign(self):
  7517. c = np.array([False, True, False, False, False, False, True, False,
  7518. False, False, True, False])
  7519. r = np.array([5., 1., 3., 2., -1., -4., 1., -10., 10., 1., 1., 3.],
  7520. dtype=np.float64)
  7521. a = np.ones(1, dtype='>i4')
  7522. b = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.],
  7523. dtype=np.float64)
  7524. assert_equal(np.where(c, a, b), r)
  7525. b = b.astype('>f8')
  7526. assert_equal(np.where(c, a, b), r)
  7527. a = a.astype('<i4')
  7528. assert_equal(np.where(c, a, b), r)
  7529. c = c.astype('>i4')
  7530. assert_equal(np.where(c, a, b), r)
  7531. def test_error(self):
  7532. c = [True, True]
  7533. a = np.ones((4, 5))
  7534. b = np.ones((5, 5))
  7535. assert_raises(ValueError, np.where, c, a, a)
  7536. assert_raises(ValueError, np.where, c[0], a, b)
  7537. def test_string(self):
  7538. # gh-4778 check strings are properly filled with nulls
  7539. a = np.array("abc")
  7540. b = np.array("x" * 753)
  7541. assert_equal(np.where(True, a, b), "abc")
  7542. assert_equal(np.where(False, b, a), "abc")
  7543. # check native datatype sized strings
  7544. a = np.array("abcd")
  7545. b = np.array("x" * 8)
  7546. assert_equal(np.where(True, a, b), "abcd")
  7547. assert_equal(np.where(False, b, a), "abcd")
  7548. def test_empty_result(self):
  7549. # pass empty where result through an assignment which reads the data of
  7550. # empty arrays, error detectable with valgrind, see gh-8922
  7551. x = np.zeros((1, 1))
  7552. ibad = np.vstack(np.where(x == 99.))
  7553. assert_array_equal(ibad,
  7554. np.atleast_2d(np.array([[],[]], dtype=np.intp)))
  7555. def test_largedim(self):
  7556. # invalid read regression gh-9304
  7557. shape = [10, 2, 3, 4, 5, 6]
  7558. np.random.seed(2)
  7559. array = np.random.rand(*shape)
  7560. for i in range(10):
  7561. benchmark = array.nonzero()
  7562. result = array.nonzero()
  7563. assert_array_equal(benchmark, result)
  7564. if not IS_PYPY:
  7565. # sys.getsizeof() is not valid on PyPy
  7566. class TestSizeOf:
  7567. def test_empty_array(self):
  7568. x = np.array([])
  7569. assert_(sys.getsizeof(x) > 0)
  7570. def check_array(self, dtype):
  7571. elem_size = dtype(0).itemsize
  7572. for length in [10, 50, 100, 500]:
  7573. x = np.arange(length, dtype=dtype)
  7574. assert_(sys.getsizeof(x) > length * elem_size)
  7575. def test_array_int32(self):
  7576. self.check_array(np.int32)
  7577. def test_array_int64(self):
  7578. self.check_array(np.int64)
  7579. def test_array_float32(self):
  7580. self.check_array(np.float32)
  7581. def test_array_float64(self):
  7582. self.check_array(np.float64)
  7583. def test_view(self):
  7584. d = np.ones(100)
  7585. assert_(sys.getsizeof(d[...]) < sys.getsizeof(d))
  7586. def test_reshape(self):
  7587. d = np.ones(100)
  7588. assert_(sys.getsizeof(d) < sys.getsizeof(d.reshape(100, 1, 1).copy()))
  7589. @_no_tracing
  7590. def test_resize(self):
  7591. d = np.ones(100)
  7592. old = sys.getsizeof(d)
  7593. d.resize(50)
  7594. assert_(old > sys.getsizeof(d))
  7595. d.resize(150)
  7596. assert_(old < sys.getsizeof(d))
  7597. def test_error(self):
  7598. d = np.ones(100)
  7599. assert_raises(TypeError, d.__sizeof__, "a")
  7600. class TestHashing:
  7601. def test_arrays_not_hashable(self):
  7602. x = np.ones(3)
  7603. assert_raises(TypeError, hash, x)
  7604. def test_collections_hashable(self):
  7605. x = np.array([])
  7606. assert_(not isinstance(x, collections.abc.Hashable))
  7607. class TestArrayPriority:
  7608. # This will go away when __array_priority__ is settled, meanwhile
  7609. # it serves to check unintended changes.
  7610. op = operator
  7611. binary_ops = [
  7612. op.pow, op.add, op.sub, op.mul, op.floordiv, op.truediv, op.mod,
  7613. op.and_, op.or_, op.xor, op.lshift, op.rshift, op.mod, op.gt,
  7614. op.ge, op.lt, op.le, op.ne, op.eq
  7615. ]
  7616. class Foo(np.ndarray):
  7617. __array_priority__ = 100.
  7618. def __new__(cls, *args, **kwargs):
  7619. return np.array(*args, **kwargs).view(cls)
  7620. class Bar(np.ndarray):
  7621. __array_priority__ = 101.
  7622. def __new__(cls, *args, **kwargs):
  7623. return np.array(*args, **kwargs).view(cls)
  7624. class Other:
  7625. __array_priority__ = 1000.
  7626. def _all(self, other):
  7627. return self.__class__()
  7628. __add__ = __radd__ = _all
  7629. __sub__ = __rsub__ = _all
  7630. __mul__ = __rmul__ = _all
  7631. __pow__ = __rpow__ = _all
  7632. __div__ = __rdiv__ = _all
  7633. __mod__ = __rmod__ = _all
  7634. __truediv__ = __rtruediv__ = _all
  7635. __floordiv__ = __rfloordiv__ = _all
  7636. __and__ = __rand__ = _all
  7637. __xor__ = __rxor__ = _all
  7638. __or__ = __ror__ = _all
  7639. __lshift__ = __rlshift__ = _all
  7640. __rshift__ = __rrshift__ = _all
  7641. __eq__ = _all
  7642. __ne__ = _all
  7643. __gt__ = _all
  7644. __ge__ = _all
  7645. __lt__ = _all
  7646. __le__ = _all
  7647. def test_ndarray_subclass(self):
  7648. a = np.array([1, 2])
  7649. b = self.Bar([1, 2])
  7650. for f in self.binary_ops:
  7651. msg = repr(f)
  7652. assert_(isinstance(f(a, b), self.Bar), msg)
  7653. assert_(isinstance(f(b, a), self.Bar), msg)
  7654. def test_ndarray_other(self):
  7655. a = np.array([1, 2])
  7656. b = self.Other()
  7657. for f in self.binary_ops:
  7658. msg = repr(f)
  7659. assert_(isinstance(f(a, b), self.Other), msg)
  7660. assert_(isinstance(f(b, a), self.Other), msg)
  7661. def test_subclass_subclass(self):
  7662. a = self.Foo([1, 2])
  7663. b = self.Bar([1, 2])
  7664. for f in self.binary_ops:
  7665. msg = repr(f)
  7666. assert_(isinstance(f(a, b), self.Bar), msg)
  7667. assert_(isinstance(f(b, a), self.Bar), msg)
  7668. def test_subclass_other(self):
  7669. a = self.Foo([1, 2])
  7670. b = self.Other()
  7671. for f in self.binary_ops:
  7672. msg = repr(f)
  7673. assert_(isinstance(f(a, b), self.Other), msg)
  7674. assert_(isinstance(f(b, a), self.Other), msg)
  7675. class TestBytestringArrayNonzero:
  7676. def test_empty_bstring_array_is_falsey(self):
  7677. assert_(not np.array([''], dtype=str))
  7678. def test_whitespace_bstring_array_is_falsey(self):
  7679. a = np.array(['spam'], dtype=str)
  7680. a[0] = ' \0\0'
  7681. assert_(not a)
  7682. def test_all_null_bstring_array_is_falsey(self):
  7683. a = np.array(['spam'], dtype=str)
  7684. a[0] = '\0\0\0\0'
  7685. assert_(not a)
  7686. def test_null_inside_bstring_array_is_truthy(self):
  7687. a = np.array(['spam'], dtype=str)
  7688. a[0] = ' \0 \0'
  7689. assert_(a)
  7690. class TestUnicodeEncoding:
  7691. """
  7692. Tests for encoding related bugs, such as UCS2 vs UCS4, round-tripping
  7693. issues, etc
  7694. """
  7695. def test_round_trip(self):
  7696. """ Tests that GETITEM, SETITEM, and PyArray_Scalar roundtrip """
  7697. # gh-15363
  7698. arr = np.zeros(shape=(), dtype="U1")
  7699. for i in range(1, sys.maxunicode + 1):
  7700. expected = chr(i)
  7701. arr[()] = expected
  7702. assert arr[()] == expected
  7703. assert arr.item() == expected
  7704. def test_assign_scalar(self):
  7705. # gh-3258
  7706. l = np.array(['aa', 'bb'])
  7707. l[:] = np.unicode_('cc')
  7708. assert_equal(l, ['cc', 'cc'])
  7709. def test_fill_scalar(self):
  7710. # gh-7227
  7711. l = np.array(['aa', 'bb'])
  7712. l.fill(np.unicode_('cc'))
  7713. assert_equal(l, ['cc', 'cc'])
  7714. class TestUnicodeArrayNonzero:
  7715. def test_empty_ustring_array_is_falsey(self):
  7716. assert_(not np.array([''], dtype=np.unicode_))
  7717. def test_whitespace_ustring_array_is_falsey(self):
  7718. a = np.array(['eggs'], dtype=np.unicode_)
  7719. a[0] = ' \0\0'
  7720. assert_(not a)
  7721. def test_all_null_ustring_array_is_falsey(self):
  7722. a = np.array(['eggs'], dtype=np.unicode_)
  7723. a[0] = '\0\0\0\0'
  7724. assert_(not a)
  7725. def test_null_inside_ustring_array_is_truthy(self):
  7726. a = np.array(['eggs'], dtype=np.unicode_)
  7727. a[0] = ' \0 \0'
  7728. assert_(a)
  7729. class TestFormat:
  7730. def test_0d(self):
  7731. a = np.array(np.pi)
  7732. assert_equal('{:0.3g}'.format(a), '3.14')
  7733. assert_equal('{:0.3g}'.format(a[()]), '3.14')
  7734. def test_1d_no_format(self):
  7735. a = np.array([np.pi])
  7736. assert_equal('{}'.format(a), str(a))
  7737. def test_1d_format(self):
  7738. # until gh-5543, ensure that the behaviour matches what it used to be
  7739. a = np.array([np.pi])
  7740. assert_raises(TypeError, '{:30}'.format, a)
  7741. from numpy.testing import IS_PYPY
  7742. class TestCTypes:
  7743. def test_ctypes_is_available(self):
  7744. test_arr = np.array([[1, 2, 3], [4, 5, 6]])
  7745. assert_equal(ctypes, test_arr.ctypes._ctypes)
  7746. assert_equal(tuple(test_arr.ctypes.shape), (2, 3))
  7747. def test_ctypes_is_not_available(self):
  7748. from numpy.core import _internal
  7749. _internal.ctypes = None
  7750. try:
  7751. test_arr = np.array([[1, 2, 3], [4, 5, 6]])
  7752. assert_(isinstance(test_arr.ctypes._ctypes,
  7753. _internal._missing_ctypes))
  7754. assert_equal(tuple(test_arr.ctypes.shape), (2, 3))
  7755. finally:
  7756. _internal.ctypes = ctypes
  7757. def _make_readonly(x):
  7758. x.flags.writeable = False
  7759. return x
  7760. @pytest.mark.parametrize('arr', [
  7761. np.array([1, 2, 3]),
  7762. np.array([['one', 'two'], ['three', 'four']]),
  7763. np.array((1, 2), dtype='i4,i4'),
  7764. np.zeros((2,), dtype=
  7765. np.dtype(dict(
  7766. formats=['<i4', '<i4'],
  7767. names=['a', 'b'],
  7768. offsets=[0, 2],
  7769. itemsize=6
  7770. ))
  7771. ),
  7772. np.array([None], dtype=object),
  7773. np.array([]),
  7774. np.empty((0, 0)),
  7775. _make_readonly(np.array([1, 2, 3])),
  7776. ], ids=[
  7777. '1d',
  7778. '2d',
  7779. 'structured',
  7780. 'overlapping',
  7781. 'object',
  7782. 'empty',
  7783. 'empty-2d',
  7784. 'readonly'
  7785. ])
  7786. def test_ctypes_data_as_holds_reference(self, arr):
  7787. # gh-9647
  7788. # create a copy to ensure that pytest does not mess with the refcounts
  7789. arr = arr.copy()
  7790. arr_ref = weakref.ref(arr)
  7791. ctypes_ptr = arr.ctypes.data_as(ctypes.c_void_p)
  7792. # `ctypes_ptr` should hold onto `arr`
  7793. del arr
  7794. break_cycles()
  7795. assert_(arr_ref() is not None, "ctypes pointer did not hold onto a reference")
  7796. # but when the `ctypes_ptr` object dies, so should `arr`
  7797. del ctypes_ptr
  7798. if IS_PYPY:
  7799. # Pypy does not recycle arr objects immediately. Trigger gc to
  7800. # release arr. Cpython uses refcounts. An explicit call to gc
  7801. # should not be needed here.
  7802. break_cycles()
  7803. assert_(arr_ref() is None, "unknowable whether ctypes pointer holds a reference")
  7804. def test_ctypes_as_parameter_holds_reference(self):
  7805. arr = np.array([None]).copy()
  7806. arr_ref = weakref.ref(arr)
  7807. ctypes_ptr = arr.ctypes._as_parameter_
  7808. # `ctypes_ptr` should hold onto `arr`
  7809. del arr
  7810. break_cycles()
  7811. assert_(arr_ref() is not None, "ctypes pointer did not hold onto a reference")
  7812. # but when the `ctypes_ptr` object dies, so should `arr`
  7813. del ctypes_ptr
  7814. if IS_PYPY:
  7815. break_cycles()
  7816. assert_(arr_ref() is None, "unknowable whether ctypes pointer holds a reference")
  7817. class TestWritebackIfCopy:
  7818. # all these tests use the WRITEBACKIFCOPY mechanism
  7819. def test_argmax_with_out(self):
  7820. mat = np.eye(5)
  7821. out = np.empty(5, dtype='i2')
  7822. res = np.argmax(mat, 0, out=out)
  7823. assert_equal(res, range(5))
  7824. def test_argmin_with_out(self):
  7825. mat = -np.eye(5)
  7826. out = np.empty(5, dtype='i2')
  7827. res = np.argmin(mat, 0, out=out)
  7828. assert_equal(res, range(5))
  7829. def test_insert_noncontiguous(self):
  7830. a = np.arange(6).reshape(2,3).T # force non-c-contiguous
  7831. # uses arr_insert
  7832. np.place(a, a>2, [44, 55])
  7833. assert_equal(a, np.array([[0, 44], [1, 55], [2, 44]]))
  7834. # hit one of the failing paths
  7835. assert_raises(ValueError, np.place, a, a>20, [])
  7836. def test_put_noncontiguous(self):
  7837. a = np.arange(6).reshape(2,3).T # force non-c-contiguous
  7838. np.put(a, [0, 2], [44, 55])
  7839. assert_equal(a, np.array([[44, 3], [55, 4], [2, 5]]))
  7840. def test_putmask_noncontiguous(self):
  7841. a = np.arange(6).reshape(2,3).T # force non-c-contiguous
  7842. # uses arr_putmask
  7843. np.putmask(a, a>2, a**2)
  7844. assert_equal(a, np.array([[0, 9], [1, 16], [2, 25]]))
  7845. def test_take_mode_raise(self):
  7846. a = np.arange(6, dtype='int')
  7847. out = np.empty(2, dtype='int')
  7848. np.take(a, [0, 2], out=out, mode='raise')
  7849. assert_equal(out, np.array([0, 2]))
  7850. def test_choose_mod_raise(self):
  7851. a = np.array([[1, 0, 1], [0, 1, 0], [1, 0, 1]])
  7852. out = np.empty((3,3), dtype='int')
  7853. choices = [-10, 10]
  7854. np.choose(a, choices, out=out, mode='raise')
  7855. assert_equal(out, np.array([[ 10, -10, 10],
  7856. [-10, 10, -10],
  7857. [ 10, -10, 10]]))
  7858. def test_flatiter__array__(self):
  7859. a = np.arange(9).reshape(3,3)
  7860. b = a.T.flat
  7861. c = b.__array__()
  7862. # triggers the WRITEBACKIFCOPY resolution, assuming refcount semantics
  7863. del c
  7864. def test_dot_out(self):
  7865. # if HAVE_CBLAS, will use WRITEBACKIFCOPY
  7866. a = np.arange(9, dtype=float).reshape(3,3)
  7867. b = np.dot(a, a, out=a)
  7868. assert_equal(b, np.array([[15, 18, 21], [42, 54, 66], [69, 90, 111]]))
  7869. def test_view_assign(self):
  7870. from numpy.core._multiarray_tests import npy_create_writebackifcopy, npy_resolve
  7871. arr = np.arange(9).reshape(3, 3).T
  7872. arr_wb = npy_create_writebackifcopy(arr)
  7873. assert_(arr_wb.flags.writebackifcopy)
  7874. assert_(arr_wb.base is arr)
  7875. arr_wb[...] = -100
  7876. npy_resolve(arr_wb)
  7877. # arr changes after resolve, even though we assigned to arr_wb
  7878. assert_equal(arr, -100)
  7879. # after resolve, the two arrays no longer reference each other
  7880. assert_(arr_wb.ctypes.data != 0)
  7881. assert_equal(arr_wb.base, None)
  7882. # assigning to arr_wb does not get transferred to arr
  7883. arr_wb[...] = 100
  7884. assert_equal(arr, -100)
  7885. @pytest.mark.leaks_references(
  7886. reason="increments self in dealloc; ignore since deprecated path.")
  7887. def test_dealloc_warning(self):
  7888. with suppress_warnings() as sup:
  7889. sup.record(RuntimeWarning)
  7890. arr = np.arange(9).reshape(3, 3)
  7891. v = arr.T
  7892. _multiarray_tests.npy_abuse_writebackifcopy(v)
  7893. assert len(sup.log) == 1
  7894. def test_view_discard_refcount(self):
  7895. from numpy.core._multiarray_tests import npy_create_writebackifcopy, npy_discard
  7896. arr = np.arange(9).reshape(3, 3).T
  7897. orig = arr.copy()
  7898. if HAS_REFCOUNT:
  7899. arr_cnt = sys.getrefcount(arr)
  7900. arr_wb = npy_create_writebackifcopy(arr)
  7901. assert_(arr_wb.flags.writebackifcopy)
  7902. assert_(arr_wb.base is arr)
  7903. arr_wb[...] = -100
  7904. npy_discard(arr_wb)
  7905. # arr remains unchanged after discard
  7906. assert_equal(arr, orig)
  7907. # after discard, the two arrays no longer reference each other
  7908. assert_(arr_wb.ctypes.data != 0)
  7909. assert_equal(arr_wb.base, None)
  7910. if HAS_REFCOUNT:
  7911. assert_equal(arr_cnt, sys.getrefcount(arr))
  7912. # assigning to arr_wb does not get transferred to arr
  7913. arr_wb[...] = 100
  7914. assert_equal(arr, orig)
  7915. class TestArange:
  7916. def test_infinite(self):
  7917. assert_raises_regex(
  7918. ValueError, "size exceeded",
  7919. np.arange, 0, np.inf
  7920. )
  7921. def test_nan_step(self):
  7922. assert_raises_regex(
  7923. ValueError, "cannot compute length",
  7924. np.arange, 0, 1, np.nan
  7925. )
  7926. def test_zero_step(self):
  7927. assert_raises(ZeroDivisionError, np.arange, 0, 10, 0)
  7928. assert_raises(ZeroDivisionError, np.arange, 0.0, 10.0, 0.0)
  7929. # empty range
  7930. assert_raises(ZeroDivisionError, np.arange, 0, 0, 0)
  7931. assert_raises(ZeroDivisionError, np.arange, 0.0, 0.0, 0.0)
  7932. def test_require_range(self):
  7933. assert_raises(TypeError, np.arange)
  7934. assert_raises(TypeError, np.arange, step=3)
  7935. assert_raises(TypeError, np.arange, dtype='int64')
  7936. assert_raises(TypeError, np.arange, start=4)
  7937. def test_start_stop_kwarg(self):
  7938. keyword_stop = np.arange(stop=3)
  7939. keyword_zerotostop = np.arange(start=0, stop=3)
  7940. keyword_start_stop = np.arange(start=3, stop=9)
  7941. assert len(keyword_stop) == 3
  7942. assert len(keyword_zerotostop) == 3
  7943. assert len(keyword_start_stop) == 6
  7944. assert_array_equal(keyword_stop, keyword_zerotostop)
  7945. def test_arange_booleans(self):
  7946. # Arange makes some sense for booleans and works up to length 2.
  7947. # But it is weird since `arange(2, 4, dtype=bool)` works.
  7948. # Arguably, much or all of this could be deprecated/removed.
  7949. res = np.arange(False, dtype=bool)
  7950. assert_array_equal(res, np.array([], dtype="bool"))
  7951. res = np.arange(True, dtype="bool")
  7952. assert_array_equal(res, [False])
  7953. res = np.arange(2, dtype="bool")
  7954. assert_array_equal(res, [False, True])
  7955. # This case is especially weird, but drops out without special case:
  7956. res = np.arange(6, 8, dtype="bool")
  7957. assert_array_equal(res, [True, True])
  7958. with pytest.raises(TypeError):
  7959. np.arange(3, dtype="bool")
  7960. @pytest.mark.parametrize("dtype", ["S3", "U", "5i"])
  7961. def test_rejects_bad_dtypes(self, dtype):
  7962. dtype = np.dtype(dtype)
  7963. DType_name = re.escape(str(type(dtype)))
  7964. with pytest.raises(TypeError,
  7965. match=rf"arange\(\) not supported for inputs .* {DType_name}"):
  7966. np.arange(2, dtype=dtype)
  7967. def test_rejects_strings(self):
  7968. # Explicitly test error for strings which may call "b" - "a":
  7969. DType_name = re.escape(str(type(np.array("a").dtype)))
  7970. with pytest.raises(TypeError,
  7971. match=rf"arange\(\) not supported for inputs .* {DType_name}"):
  7972. np.arange("a", "b")
  7973. def test_byteswapped(self):
  7974. res_be = np.arange(1, 1000, dtype=">i4")
  7975. res_le = np.arange(1, 1000, dtype="<i4")
  7976. assert res_be.dtype == ">i4"
  7977. assert res_le.dtype == "<i4"
  7978. assert_array_equal(res_le, res_be)
  7979. @pytest.mark.parametrize("which", [0, 1, 2])
  7980. def test_error_paths_and_promotion(self, which):
  7981. args = [0, 1, 2] # start, stop, and step
  7982. args[which] = np.float64(2.) # should ensure float64 output
  7983. assert np.arange(*args).dtype == np.float64
  7984. # Cover stranger error path, test only to achieve code coverage!
  7985. args[which] = [None, []]
  7986. with pytest.raises(ValueError):
  7987. # Fails discovering start dtype
  7988. np.arange(*args)
  7989. class TestArrayFinalize:
  7990. """ Tests __array_finalize__ """
  7991. def test_receives_base(self):
  7992. # gh-11237
  7993. class SavesBase(np.ndarray):
  7994. def __array_finalize__(self, obj):
  7995. self.saved_base = self.base
  7996. a = np.array(1).view(SavesBase)
  7997. assert_(a.saved_base is a.base)
  7998. def test_bad_finalize1(self):
  7999. class BadAttributeArray(np.ndarray):
  8000. @property
  8001. def __array_finalize__(self):
  8002. raise RuntimeError("boohoo!")
  8003. with pytest.raises(TypeError, match="not callable"):
  8004. np.arange(10).view(BadAttributeArray)
  8005. def test_bad_finalize2(self):
  8006. class BadAttributeArray(np.ndarray):
  8007. def __array_finalize__(self):
  8008. raise RuntimeError("boohoo!")
  8009. with pytest.raises(TypeError, match="takes 1 positional"):
  8010. np.arange(10).view(BadAttributeArray)
  8011. def test_bad_finalize3(self):
  8012. class BadAttributeArray(np.ndarray):
  8013. def __array_finalize__(self, obj):
  8014. raise RuntimeError("boohoo!")
  8015. with pytest.raises(RuntimeError, match="boohoo!"):
  8016. np.arange(10).view(BadAttributeArray)
  8017. def test_lifetime_on_error(self):
  8018. # gh-11237
  8019. class RaisesInFinalize(np.ndarray):
  8020. def __array_finalize__(self, obj):
  8021. # crash, but keep this object alive
  8022. raise Exception(self)
  8023. # a plain object can't be weakref'd
  8024. class Dummy: pass
  8025. # get a weak reference to an object within an array
  8026. obj_arr = np.array(Dummy())
  8027. obj_ref = weakref.ref(obj_arr[()])
  8028. # get an array that crashed in __array_finalize__
  8029. with assert_raises(Exception) as e:
  8030. obj_arr.view(RaisesInFinalize)
  8031. obj_subarray = e.exception.args[0]
  8032. del e
  8033. assert_(isinstance(obj_subarray, RaisesInFinalize))
  8034. # reference should still be held by obj_arr
  8035. break_cycles()
  8036. assert_(obj_ref() is not None, "object should not already be dead")
  8037. del obj_arr
  8038. break_cycles()
  8039. assert_(obj_ref() is not None, "obj_arr should not hold the last reference")
  8040. del obj_subarray
  8041. break_cycles()
  8042. assert_(obj_ref() is None, "no references should remain")
  8043. def test_can_use_super(self):
  8044. class SuperFinalize(np.ndarray):
  8045. def __array_finalize__(self, obj):
  8046. self.saved_result = super().__array_finalize__(obj)
  8047. a = np.array(1).view(SuperFinalize)
  8048. assert_(a.saved_result is None)
  8049. def test_orderconverter_with_nonASCII_unicode_ordering():
  8050. # gh-7475
  8051. a = np.arange(5)
  8052. assert_raises(ValueError, a.flatten, order='\xe2')
  8053. def test_equal_override():
  8054. # gh-9153: ndarray.__eq__ uses special logic for structured arrays, which
  8055. # did not respect overrides with __array_priority__ or __array_ufunc__.
  8056. # The PR fixed this for __array_priority__ and __array_ufunc__ = None.
  8057. class MyAlwaysEqual:
  8058. def __eq__(self, other):
  8059. return "eq"
  8060. def __ne__(self, other):
  8061. return "ne"
  8062. class MyAlwaysEqualOld(MyAlwaysEqual):
  8063. __array_priority__ = 10000
  8064. class MyAlwaysEqualNew(MyAlwaysEqual):
  8065. __array_ufunc__ = None
  8066. array = np.array([(0, 1), (2, 3)], dtype='i4,i4')
  8067. for my_always_equal_cls in MyAlwaysEqualOld, MyAlwaysEqualNew:
  8068. my_always_equal = my_always_equal_cls()
  8069. assert_equal(my_always_equal == array, 'eq')
  8070. assert_equal(array == my_always_equal, 'eq')
  8071. assert_equal(my_always_equal != array, 'ne')
  8072. assert_equal(array != my_always_equal, 'ne')
  8073. @pytest.mark.parametrize(
  8074. ["fun", "npfun"],
  8075. [
  8076. (_multiarray_tests.npy_cabs, np.absolute),
  8077. (_multiarray_tests.npy_carg, np.angle)
  8078. ]
  8079. )
  8080. @pytest.mark.parametrize("x", [1, np.inf, -np.inf, np.nan])
  8081. @pytest.mark.parametrize("y", [1, np.inf, -np.inf, np.nan])
  8082. @pytest.mark.parametrize("test_dtype", np.complexfloating.__subclasses__())
  8083. def test_npymath_complex(fun, npfun, x, y, test_dtype):
  8084. # Smoketest npymath functions
  8085. z = test_dtype(complex(x, y))
  8086. got = fun(z)
  8087. expected = npfun(z)
  8088. assert_allclose(got, expected)
  8089. def test_npymath_real():
  8090. # Smoketest npymath functions
  8091. from numpy.core._multiarray_tests import (
  8092. npy_log10, npy_cosh, npy_sinh, npy_tan, npy_tanh)
  8093. funcs = {npy_log10: np.log10,
  8094. npy_cosh: np.cosh,
  8095. npy_sinh: np.sinh,
  8096. npy_tan: np.tan,
  8097. npy_tanh: np.tanh}
  8098. vals = (1, np.inf, -np.inf, np.nan)
  8099. types = (np.float32, np.float64, np.longdouble)
  8100. with np.errstate(all='ignore'):
  8101. for fun, npfun in funcs.items():
  8102. for x, t in itertools.product(vals, types):
  8103. z = t(x)
  8104. got = fun(z)
  8105. expected = npfun(z)
  8106. assert_allclose(got, expected)
  8107. def test_uintalignment_and_alignment():
  8108. # alignment code needs to satisfy these requirements:
  8109. # 1. numpy structs match C struct layout
  8110. # 2. ufuncs/casting is safe wrt to aligned access
  8111. # 3. copy code is safe wrt to "uint alidned" access
  8112. #
  8113. # Complex types are the main problem, whose alignment may not be the same
  8114. # as their "uint alignment".
  8115. #
  8116. # This test might only fail on certain platforms, where uint64 alignment is
  8117. # not equal to complex64 alignment. The second 2 tests will only fail
  8118. # for DEBUG=1.
  8119. d1 = np.dtype('u1,c8', align=True)
  8120. d2 = np.dtype('u4,c8', align=True)
  8121. d3 = np.dtype({'names': ['a', 'b'], 'formats': ['u1', d1]}, align=True)
  8122. assert_equal(np.zeros(1, dtype=d1)['f1'].flags['ALIGNED'], True)
  8123. assert_equal(np.zeros(1, dtype=d2)['f1'].flags['ALIGNED'], True)
  8124. assert_equal(np.zeros(1, dtype='u1,c8')['f1'].flags['ALIGNED'], False)
  8125. # check that C struct matches numpy struct size
  8126. s = _multiarray_tests.get_struct_alignments()
  8127. for d, (alignment, size) in zip([d1,d2,d3], s):
  8128. assert_equal(d.alignment, alignment)
  8129. assert_equal(d.itemsize, size)
  8130. # check that ufuncs don't complain in debug mode
  8131. # (this is probably OK if the aligned flag is true above)
  8132. src = np.zeros((2,2), dtype=d1)['f1'] # 4-byte aligned, often
  8133. np.exp(src) # assert fails?
  8134. # check that copy code doesn't complain in debug mode
  8135. dst = np.zeros((2,2), dtype='c8')
  8136. dst[:,1] = src[:,1] # assert in lowlevel_strided_loops fails?
  8137. class TestAlignment:
  8138. # adapted from scipy._lib.tests.test__util.test__aligned_zeros
  8139. # Checks that unusual memory alignments don't trip up numpy.
  8140. # In particular, check RELAXED_STRIDES don't trip alignment assertions in
  8141. # NDEBUG mode for size-0 arrays (gh-12503)
  8142. def check(self, shape, dtype, order, align):
  8143. err_msg = repr((shape, dtype, order, align))
  8144. x = _aligned_zeros(shape, dtype, order, align=align)
  8145. if align is None:
  8146. align = np.dtype(dtype).alignment
  8147. assert_equal(x.__array_interface__['data'][0] % align, 0)
  8148. if hasattr(shape, '__len__'):
  8149. assert_equal(x.shape, shape, err_msg)
  8150. else:
  8151. assert_equal(x.shape, (shape,), err_msg)
  8152. assert_equal(x.dtype, dtype)
  8153. if order == "C":
  8154. assert_(x.flags.c_contiguous, err_msg)
  8155. elif order == "F":
  8156. if x.size > 0:
  8157. assert_(x.flags.f_contiguous, err_msg)
  8158. elif order is None:
  8159. assert_(x.flags.c_contiguous, err_msg)
  8160. else:
  8161. raise ValueError()
  8162. def test_various_alignments(self):
  8163. for align in [1, 2, 3, 4, 8, 12, 16, 32, 64, None]:
  8164. for n in [0, 1, 3, 11]:
  8165. for order in ["C", "F", None]:
  8166. for dtype in list(np.typecodes["All"]) + ['i4,i4,i4']:
  8167. if dtype == 'O':
  8168. # object dtype can't be misaligned
  8169. continue
  8170. for shape in [n, (1, 2, 3, n)]:
  8171. self.check(shape, np.dtype(dtype), order, align)
  8172. def test_strided_loop_alignments(self):
  8173. # particularly test that complex64 and float128 use right alignment
  8174. # code-paths, since these are particularly problematic. It is useful to
  8175. # turn on USE_DEBUG for this test, so lowlevel-loop asserts are run.
  8176. for align in [1, 2, 4, 8, 12, 16, None]:
  8177. xf64 = _aligned_zeros(3, np.float64)
  8178. xc64 = _aligned_zeros(3, np.complex64, align=align)
  8179. xf128 = _aligned_zeros(3, np.longdouble, align=align)
  8180. # test casting, both to and from misaligned
  8181. with suppress_warnings() as sup:
  8182. sup.filter(np.ComplexWarning, "Casting complex values")
  8183. xc64.astype('f8')
  8184. xf64.astype(np.complex64)
  8185. test = xc64 + xf64
  8186. xf128.astype('f8')
  8187. xf64.astype(np.longdouble)
  8188. test = xf128 + xf64
  8189. test = xf128 + xc64
  8190. # test copy, both to and from misaligned
  8191. # contig copy
  8192. xf64[:] = xf64.copy()
  8193. xc64[:] = xc64.copy()
  8194. xf128[:] = xf128.copy()
  8195. # strided copy
  8196. xf64[::2] = xf64[::2].copy()
  8197. xc64[::2] = xc64[::2].copy()
  8198. xf128[::2] = xf128[::2].copy()
  8199. def test_getfield():
  8200. a = np.arange(32, dtype='uint16')
  8201. if sys.byteorder == 'little':
  8202. i = 0
  8203. j = 1
  8204. else:
  8205. i = 1
  8206. j = 0
  8207. b = a.getfield('int8', i)
  8208. assert_equal(b, a)
  8209. b = a.getfield('int8', j)
  8210. assert_equal(b, 0)
  8211. pytest.raises(ValueError, a.getfield, 'uint8', -1)
  8212. pytest.raises(ValueError, a.getfield, 'uint8', 16)
  8213. pytest.raises(ValueError, a.getfield, 'uint64', 0)
  8214. class TestViewDtype:
  8215. """
  8216. Verify that making a view of a non-contiguous array works as expected.
  8217. """
  8218. def test_smaller_dtype_multiple(self):
  8219. # x is non-contiguous
  8220. x = np.arange(10, dtype='<i4')[::2]
  8221. with pytest.raises(ValueError,
  8222. match='the last axis must be contiguous'):
  8223. x.view('<i2')
  8224. expected = [[0, 0], [2, 0], [4, 0], [6, 0], [8, 0]]
  8225. assert_array_equal(x[:, np.newaxis].view('<i2'), expected)
  8226. def test_smaller_dtype_not_multiple(self):
  8227. # x is non-contiguous
  8228. x = np.arange(5, dtype='<i4')[::2]
  8229. with pytest.raises(ValueError,
  8230. match='the last axis must be contiguous'):
  8231. x.view('S3')
  8232. with pytest.raises(ValueError,
  8233. match='When changing to a smaller dtype'):
  8234. x[:, np.newaxis].view('S3')
  8235. # Make sure the problem is because of the dtype size
  8236. expected = [[b''], [b'\x02'], [b'\x04']]
  8237. assert_array_equal(x[:, np.newaxis].view('S4'), expected)
  8238. def test_larger_dtype_multiple(self):
  8239. # x is non-contiguous in the first dimension, contiguous in the last
  8240. x = np.arange(20, dtype='<i2').reshape(10, 2)[::2, :]
  8241. expected = np.array([[65536], [327684], [589832],
  8242. [851980], [1114128]], dtype='<i4')
  8243. assert_array_equal(x.view('<i4'), expected)
  8244. def test_larger_dtype_not_multiple(self):
  8245. # x is non-contiguous in the first dimension, contiguous in the last
  8246. x = np.arange(20, dtype='<i2').reshape(10, 2)[::2, :]
  8247. with pytest.raises(ValueError,
  8248. match='When changing to a larger dtype'):
  8249. x.view('S3')
  8250. # Make sure the problem is because of the dtype size
  8251. expected = [[b'\x00\x00\x01'], [b'\x04\x00\x05'], [b'\x08\x00\t'],
  8252. [b'\x0c\x00\r'], [b'\x10\x00\x11']]
  8253. assert_array_equal(x.view('S4'), expected)
  8254. def test_f_contiguous(self):
  8255. # x is F-contiguous
  8256. x = np.arange(4 * 3, dtype='<i4').reshape(4, 3).T
  8257. with pytest.raises(ValueError,
  8258. match='the last axis must be contiguous'):
  8259. x.view('<i2')
  8260. def test_non_c_contiguous(self):
  8261. # x is contiguous in axis=-1, but not C-contiguous in other axes
  8262. x = np.arange(2 * 3 * 4, dtype='i1').\
  8263. reshape(2, 3, 4).transpose(1, 0, 2)
  8264. expected = [[[256, 770], [3340, 3854]],
  8265. [[1284, 1798], [4368, 4882]],
  8266. [[2312, 2826], [5396, 5910]]]
  8267. assert_array_equal(x.view('<i2'), expected)
  8268. # Test various array sizes that hit different code paths in quicksort-avx512
  8269. @pytest.mark.parametrize("N", [8, 16, 24, 32, 48, 64, 96, 128, 151, 191,
  8270. 256, 383, 512, 1023, 2047])
  8271. def test_sort_float(N):
  8272. # Regular data with nan sprinkled
  8273. np.random.seed(42)
  8274. arr = -0.5 + np.random.sample(N).astype('f')
  8275. arr[np.random.choice(arr.shape[0], 3)] = np.nan
  8276. assert_equal(np.sort(arr, kind='quick'), np.sort(arr, kind='heap'))
  8277. # (2) with +INF
  8278. infarr = np.inf*np.ones(N, dtype='f')
  8279. infarr[np.random.choice(infarr.shape[0], 5)] = -1.0
  8280. assert_equal(np.sort(infarr, kind='quick'), np.sort(infarr, kind='heap'))
  8281. # (3) with -INF
  8282. neginfarr = -np.inf*np.ones(N, dtype='f')
  8283. neginfarr[np.random.choice(neginfarr.shape[0], 5)] = 1.0
  8284. assert_equal(np.sort(neginfarr, kind='quick'),
  8285. np.sort(neginfarr, kind='heap'))
  8286. # (4) with +/-INF
  8287. infarr = np.inf*np.ones(N, dtype='f')
  8288. infarr[np.random.choice(infarr.shape[0], (int)(N/2))] = -np.inf
  8289. assert_equal(np.sort(infarr, kind='quick'), np.sort(infarr, kind='heap'))
  8290. def test_sort_int():
  8291. # Random data with NPY_MAX_INT32 and NPY_MIN_INT32 sprinkled
  8292. rng = np.random.default_rng(42)
  8293. N = 2047
  8294. minv = np.iinfo(np.int32).min
  8295. maxv = np.iinfo(np.int32).max
  8296. arr = rng.integers(low=minv, high=maxv, size=N).astype('int32')
  8297. arr[np.random.choice(arr.shape[0], 10)] = minv
  8298. arr[np.random.choice(arr.shape[0], 10)] = maxv
  8299. assert_equal(np.sort(arr, kind='quick'), np.sort(arr, kind='heap'))
  8300. def test_sort_uint():
  8301. # Random data with NPY_MAX_UINT32 sprinkled
  8302. rng = np.random.default_rng(42)
  8303. N = 2047
  8304. maxv = np.iinfo(np.uint32).max
  8305. arr = rng.integers(low=0, high=maxv, size=N).astype('uint32')
  8306. arr[np.random.choice(arr.shape[0], 10)] = maxv
  8307. assert_equal(np.sort(arr, kind='quick'), np.sort(arr, kind='heap'))