test_core.py 197 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355
  1. # pylint: disable-msg=W0400,W0511,W0611,W0612,W0614,R0201,E1102
  2. """Tests suite for MaskedArray & subclassing.
  3. :author: Pierre Gerard-Marchant
  4. :contact: pierregm_at_uga_dot_edu
  5. """
  6. __author__ = "Pierre GF Gerard-Marchant"
  7. import sys
  8. import warnings
  9. import operator
  10. import itertools
  11. import textwrap
  12. import pytest
  13. from functools import reduce
  14. import numpy as np
  15. import numpy.ma.core
  16. import numpy.core.fromnumeric as fromnumeric
  17. import numpy.core.umath as umath
  18. from numpy.testing import (
  19. assert_raises, assert_warns, suppress_warnings
  20. )
  21. from numpy import ndarray
  22. from numpy.compat import asbytes
  23. from numpy.ma.testutils import (
  24. assert_, assert_array_equal, assert_equal, assert_almost_equal,
  25. assert_equal_records, fail_if_equal, assert_not_equal,
  26. assert_mask_equal
  27. )
  28. from numpy.ma.core import (
  29. MAError, MaskError, MaskType, MaskedArray, abs, absolute, add, all,
  30. allclose, allequal, alltrue, angle, anom, arange, arccos, arccosh, arctan2,
  31. arcsin, arctan, argsort, array, asarray, choose, concatenate,
  32. conjugate, cos, cosh, count, default_fill_value, diag, divide, doc_note,
  33. empty, empty_like, equal, exp, flatten_mask, filled, fix_invalid,
  34. flatten_structured_array, fromflex, getmask, getmaskarray, greater,
  35. greater_equal, identity, inner, isMaskedArray, less, less_equal, log,
  36. log10, make_mask, make_mask_descr, mask_or, masked, masked_array,
  37. masked_equal, masked_greater, masked_greater_equal, masked_inside,
  38. masked_less, masked_less_equal, masked_not_equal, masked_outside,
  39. masked_print_option, masked_values, masked_where, max, maximum,
  40. maximum_fill_value, min, minimum, minimum_fill_value, mod, multiply,
  41. mvoid, nomask, not_equal, ones, outer, power, product, put, putmask,
  42. ravel, repeat, reshape, resize, shape, sin, sinh, sometrue, sort, sqrt,
  43. subtract, sum, take, tan, tanh, transpose, where, zeros,
  44. )
  45. from numpy.compat import pickle
  46. pi = np.pi
  47. suppress_copy_mask_on_assignment = suppress_warnings()
  48. suppress_copy_mask_on_assignment.filter(
  49. numpy.ma.core.MaskedArrayFutureWarning,
  50. "setting an item on a masked array which has a shared mask will not copy")
  51. # For parametrized numeric testing
  52. num_dts = [np.dtype(dt_) for dt_ in '?bhilqBHILQefdgFD']
  53. num_ids = [dt_.char for dt_ in num_dts]
  54. class TestMaskedArray:
  55. # Base test class for MaskedArrays.
  56. def setup(self):
  57. # Base data definition.
  58. x = np.array([1., 1., 1., -2., pi/2.0, 4., 5., -10., 10., 1., 2., 3.])
  59. y = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.])
  60. a10 = 10.
  61. m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
  62. m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1]
  63. xm = masked_array(x, mask=m1)
  64. ym = masked_array(y, mask=m2)
  65. z = np.array([-.5, 0., .5, .8])
  66. zm = masked_array(z, mask=[0, 1, 0, 0])
  67. xf = np.where(m1, 1e+20, x)
  68. xm.set_fill_value(1e+20)
  69. self.d = (x, y, a10, m1, m2, xm, ym, z, zm, xf)
  70. def test_basicattributes(self):
  71. # Tests some basic array attributes.
  72. a = array([1, 3, 2])
  73. b = array([1, 3, 2], mask=[1, 0, 1])
  74. assert_equal(a.ndim, 1)
  75. assert_equal(b.ndim, 1)
  76. assert_equal(a.size, 3)
  77. assert_equal(b.size, 3)
  78. assert_equal(a.shape, (3,))
  79. assert_equal(b.shape, (3,))
  80. def test_basic0d(self):
  81. # Checks masking a scalar
  82. x = masked_array(0)
  83. assert_equal(str(x), '0')
  84. x = masked_array(0, mask=True)
  85. assert_equal(str(x), str(masked_print_option))
  86. x = masked_array(0, mask=False)
  87. assert_equal(str(x), '0')
  88. x = array(0, mask=1)
  89. assert_(x.filled().dtype is x._data.dtype)
  90. def test_basic1d(self):
  91. # Test of basic array creation and properties in 1 dimension.
  92. (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
  93. assert_(not isMaskedArray(x))
  94. assert_(isMaskedArray(xm))
  95. assert_((xm - ym).filled(0).any())
  96. fail_if_equal(xm.mask.astype(int), ym.mask.astype(int))
  97. s = x.shape
  98. assert_equal(np.shape(xm), s)
  99. assert_equal(xm.shape, s)
  100. assert_equal(xm.dtype, x.dtype)
  101. assert_equal(zm.dtype, z.dtype)
  102. assert_equal(xm.size, reduce(lambda x, y:x * y, s))
  103. assert_equal(count(xm), len(m1) - reduce(lambda x, y:x + y, m1))
  104. assert_array_equal(xm, xf)
  105. assert_array_equal(filled(xm, 1.e20), xf)
  106. assert_array_equal(x, xm)
  107. def test_basic2d(self):
  108. # Test of basic array creation and properties in 2 dimensions.
  109. (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
  110. for s in [(4, 3), (6, 2)]:
  111. x.shape = s
  112. y.shape = s
  113. xm.shape = s
  114. ym.shape = s
  115. xf.shape = s
  116. assert_(not isMaskedArray(x))
  117. assert_(isMaskedArray(xm))
  118. assert_equal(shape(xm), s)
  119. assert_equal(xm.shape, s)
  120. assert_equal(xm.size, reduce(lambda x, y:x * y, s))
  121. assert_equal(count(xm), len(m1) - reduce(lambda x, y:x + y, m1))
  122. assert_equal(xm, xf)
  123. assert_equal(filled(xm, 1.e20), xf)
  124. assert_equal(x, xm)
  125. def test_concatenate_basic(self):
  126. # Tests concatenations.
  127. (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
  128. # basic concatenation
  129. assert_equal(np.concatenate((x, y)), concatenate((xm, ym)))
  130. assert_equal(np.concatenate((x, y)), concatenate((x, y)))
  131. assert_equal(np.concatenate((x, y)), concatenate((xm, y)))
  132. assert_equal(np.concatenate((x, y, x)), concatenate((x, ym, x)))
  133. def test_concatenate_alongaxis(self):
  134. # Tests concatenations.
  135. (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
  136. # Concatenation along an axis
  137. s = (3, 4)
  138. x.shape = y.shape = xm.shape = ym.shape = s
  139. assert_equal(xm.mask, np.reshape(m1, s))
  140. assert_equal(ym.mask, np.reshape(m2, s))
  141. xmym = concatenate((xm, ym), 1)
  142. assert_equal(np.concatenate((x, y), 1), xmym)
  143. assert_equal(np.concatenate((xm.mask, ym.mask), 1), xmym._mask)
  144. x = zeros(2)
  145. y = array(ones(2), mask=[False, True])
  146. z = concatenate((x, y))
  147. assert_array_equal(z, [0, 0, 1, 1])
  148. assert_array_equal(z.mask, [False, False, False, True])
  149. z = concatenate((y, x))
  150. assert_array_equal(z, [1, 1, 0, 0])
  151. assert_array_equal(z.mask, [False, True, False, False])
  152. def test_concatenate_flexible(self):
  153. # Tests the concatenation on flexible arrays.
  154. data = masked_array(list(zip(np.random.rand(10),
  155. np.arange(10))),
  156. dtype=[('a', float), ('b', int)])
  157. test = concatenate([data[:5], data[5:]])
  158. assert_equal_records(test, data)
  159. def test_creation_ndmin(self):
  160. # Check the use of ndmin
  161. x = array([1, 2, 3], mask=[1, 0, 0], ndmin=2)
  162. assert_equal(x.shape, (1, 3))
  163. assert_equal(x._data, [[1, 2, 3]])
  164. assert_equal(x._mask, [[1, 0, 0]])
  165. def test_creation_ndmin_from_maskedarray(self):
  166. # Make sure we're not losing the original mask w/ ndmin
  167. x = array([1, 2, 3])
  168. x[-1] = masked
  169. xx = array(x, ndmin=2, dtype=float)
  170. assert_equal(x.shape, x._mask.shape)
  171. assert_equal(xx.shape, xx._mask.shape)
  172. def test_creation_maskcreation(self):
  173. # Tests how masks are initialized at the creation of Maskedarrays.
  174. data = arange(24, dtype=float)
  175. data[[3, 6, 15]] = masked
  176. dma_1 = MaskedArray(data)
  177. assert_equal(dma_1.mask, data.mask)
  178. dma_2 = MaskedArray(dma_1)
  179. assert_equal(dma_2.mask, dma_1.mask)
  180. dma_3 = MaskedArray(dma_1, mask=[1, 0, 0, 0] * 6)
  181. fail_if_equal(dma_3.mask, dma_1.mask)
  182. x = array([1, 2, 3], mask=True)
  183. assert_equal(x._mask, [True, True, True])
  184. x = array([1, 2, 3], mask=False)
  185. assert_equal(x._mask, [False, False, False])
  186. y = array([1, 2, 3], mask=x._mask, copy=False)
  187. assert_(np.may_share_memory(x.mask, y.mask))
  188. y = array([1, 2, 3], mask=x._mask, copy=True)
  189. assert_(not np.may_share_memory(x.mask, y.mask))
  190. def test_masked_singleton_array_creation_warns(self):
  191. # The first works, but should not (ideally), there may be no way
  192. # to solve this, however, as long as `np.ma.masked` is an ndarray.
  193. np.array(np.ma.masked)
  194. with pytest.warns(UserWarning):
  195. # Tries to create a float array, using `float(np.ma.masked)`.
  196. # We may want to define this is invalid behaviour in the future!
  197. # (requiring np.ma.masked to be a known NumPy scalar probably
  198. # with a DType.)
  199. np.array([3., np.ma.masked])
  200. def test_creation_with_list_of_maskedarrays(self):
  201. # Tests creating a masked array from a list of masked arrays.
  202. x = array(np.arange(5), mask=[1, 0, 0, 0, 0])
  203. data = array((x, x[::-1]))
  204. assert_equal(data, [[0, 1, 2, 3, 4], [4, 3, 2, 1, 0]])
  205. assert_equal(data._mask, [[1, 0, 0, 0, 0], [0, 0, 0, 0, 1]])
  206. x.mask = nomask
  207. data = array((x, x[::-1]))
  208. assert_equal(data, [[0, 1, 2, 3, 4], [4, 3, 2, 1, 0]])
  209. assert_(data.mask is nomask)
  210. def test_creation_with_list_of_maskedarrays_no_bool_cast(self):
  211. # Tests the regression in gh-18551
  212. masked_str = np.ma.masked_array(['a', 'b'], mask=[True, False])
  213. normal_int = np.arange(2)
  214. res = np.ma.asarray([masked_str, normal_int], dtype="U21")
  215. assert_array_equal(res.mask, [[True, False], [False, False]])
  216. # The above only failed due a long chain of oddity, try also with
  217. # an object array that cannot be converted to bool always:
  218. class NotBool():
  219. def __bool__(self):
  220. raise ValueError("not a bool!")
  221. masked_obj = np.ma.masked_array([NotBool(), 'b'], mask=[True, False])
  222. # Check that the NotBool actually fails like we would expect:
  223. with pytest.raises(ValueError, match="not a bool!"):
  224. np.asarray([masked_obj], dtype=bool)
  225. res = np.ma.asarray([masked_obj, normal_int])
  226. assert_array_equal(res.mask, [[True, False], [False, False]])
  227. def test_creation_from_ndarray_with_padding(self):
  228. x = np.array([('A', 0)], dtype={'names':['f0','f1'],
  229. 'formats':['S4','i8'],
  230. 'offsets':[0,8]})
  231. array(x) # used to fail due to 'V' padding field in x.dtype.descr
  232. def test_unknown_keyword_parameter(self):
  233. with pytest.raises(TypeError, match="unexpected keyword argument"):
  234. MaskedArray([1, 2, 3], maks=[0, 1, 0]) # `mask` is misspelled.
  235. def test_asarray(self):
  236. (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
  237. xm.fill_value = -9999
  238. xm._hardmask = True
  239. xmm = asarray(xm)
  240. assert_equal(xmm._data, xm._data)
  241. assert_equal(xmm._mask, xm._mask)
  242. assert_equal(xmm.fill_value, xm.fill_value)
  243. assert_equal(xmm._hardmask, xm._hardmask)
  244. def test_asarray_default_order(self):
  245. # See Issue #6646
  246. m = np.eye(3).T
  247. assert_(not m.flags.c_contiguous)
  248. new_m = asarray(m)
  249. assert_(new_m.flags.c_contiguous)
  250. def test_asarray_enforce_order(self):
  251. # See Issue #6646
  252. m = np.eye(3).T
  253. assert_(not m.flags.c_contiguous)
  254. new_m = asarray(m, order='C')
  255. assert_(new_m.flags.c_contiguous)
  256. def test_fix_invalid(self):
  257. # Checks fix_invalid.
  258. with np.errstate(invalid='ignore'):
  259. data = masked_array([np.nan, 0., 1.], mask=[0, 0, 1])
  260. data_fixed = fix_invalid(data)
  261. assert_equal(data_fixed._data, [data.fill_value, 0., 1.])
  262. assert_equal(data_fixed._mask, [1., 0., 1.])
  263. def test_maskedelement(self):
  264. # Test of masked element
  265. x = arange(6)
  266. x[1] = masked
  267. assert_(str(masked) == '--')
  268. assert_(x[1] is masked)
  269. assert_equal(filled(x[1], 0), 0)
  270. def test_set_element_as_object(self):
  271. # Tests setting elements with object
  272. a = empty(1, dtype=object)
  273. x = (1, 2, 3, 4, 5)
  274. a[0] = x
  275. assert_equal(a[0], x)
  276. assert_(a[0] is x)
  277. import datetime
  278. dt = datetime.datetime.now()
  279. a[0] = dt
  280. assert_(a[0] is dt)
  281. def test_indexing(self):
  282. # Tests conversions and indexing
  283. x1 = np.array([1, 2, 4, 3])
  284. x2 = array(x1, mask=[1, 0, 0, 0])
  285. x3 = array(x1, mask=[0, 1, 0, 1])
  286. x4 = array(x1)
  287. # test conversion to strings
  288. str(x2) # raises?
  289. repr(x2) # raises?
  290. assert_equal(np.sort(x1), sort(x2, endwith=False))
  291. # tests of indexing
  292. assert_(type(x2[1]) is type(x1[1]))
  293. assert_(x1[1] == x2[1])
  294. assert_(x2[0] is masked)
  295. assert_equal(x1[2], x2[2])
  296. assert_equal(x1[2:5], x2[2:5])
  297. assert_equal(x1[:], x2[:])
  298. assert_equal(x1[1:], x3[1:])
  299. x1[2] = 9
  300. x2[2] = 9
  301. assert_equal(x1, x2)
  302. x1[1:3] = 99
  303. x2[1:3] = 99
  304. assert_equal(x1, x2)
  305. x2[1] = masked
  306. assert_equal(x1, x2)
  307. x2[1:3] = masked
  308. assert_equal(x1, x2)
  309. x2[:] = x1
  310. x2[1] = masked
  311. assert_(allequal(getmask(x2), array([0, 1, 0, 0])))
  312. x3[:] = masked_array([1, 2, 3, 4], [0, 1, 1, 0])
  313. assert_(allequal(getmask(x3), array([0, 1, 1, 0])))
  314. x4[:] = masked_array([1, 2, 3, 4], [0, 1, 1, 0])
  315. assert_(allequal(getmask(x4), array([0, 1, 1, 0])))
  316. assert_(allequal(x4, array([1, 2, 3, 4])))
  317. x1 = np.arange(5) * 1.0
  318. x2 = masked_values(x1, 3.0)
  319. assert_equal(x1, x2)
  320. assert_(allequal(array([0, 0, 0, 1, 0], MaskType), x2.mask))
  321. assert_equal(3.0, x2.fill_value)
  322. x1 = array([1, 'hello', 2, 3], object)
  323. x2 = np.array([1, 'hello', 2, 3], object)
  324. s1 = x1[1]
  325. s2 = x2[1]
  326. assert_equal(type(s2), str)
  327. assert_equal(type(s1), str)
  328. assert_equal(s1, s2)
  329. assert_(x1[1:1].shape == (0,))
  330. @suppress_copy_mask_on_assignment
  331. def test_copy(self):
  332. # Tests of some subtle points of copying and sizing.
  333. n = [0, 0, 1, 0, 0]
  334. m = make_mask(n)
  335. m2 = make_mask(m)
  336. assert_(m is m2)
  337. m3 = make_mask(m, copy=True)
  338. assert_(m is not m3)
  339. x1 = np.arange(5)
  340. y1 = array(x1, mask=m)
  341. assert_equal(y1._data.__array_interface__, x1.__array_interface__)
  342. assert_(allequal(x1, y1.data))
  343. assert_equal(y1._mask.__array_interface__, m.__array_interface__)
  344. y1a = array(y1)
  345. # Default for masked array is not to copy; see gh-10318.
  346. assert_(y1a._data.__array_interface__ ==
  347. y1._data.__array_interface__)
  348. assert_(y1a._mask.__array_interface__ ==
  349. y1._mask.__array_interface__)
  350. y2 = array(x1, mask=m3)
  351. assert_(y2._data.__array_interface__ == x1.__array_interface__)
  352. assert_(y2._mask.__array_interface__ == m3.__array_interface__)
  353. assert_(y2[2] is masked)
  354. y2[2] = 9
  355. assert_(y2[2] is not masked)
  356. assert_(y2._mask.__array_interface__ == m3.__array_interface__)
  357. assert_(allequal(y2.mask, 0))
  358. y2a = array(x1, mask=m, copy=1)
  359. assert_(y2a._data.__array_interface__ != x1.__array_interface__)
  360. #assert_( y2a._mask is not m)
  361. assert_(y2a._mask.__array_interface__ != m.__array_interface__)
  362. assert_(y2a[2] is masked)
  363. y2a[2] = 9
  364. assert_(y2a[2] is not masked)
  365. #assert_( y2a._mask is not m)
  366. assert_(y2a._mask.__array_interface__ != m.__array_interface__)
  367. assert_(allequal(y2a.mask, 0))
  368. y3 = array(x1 * 1.0, mask=m)
  369. assert_(filled(y3).dtype is (x1 * 1.0).dtype)
  370. x4 = arange(4)
  371. x4[2] = masked
  372. y4 = resize(x4, (8,))
  373. assert_equal(concatenate([x4, x4]), y4)
  374. assert_equal(getmask(y4), [0, 0, 1, 0, 0, 0, 1, 0])
  375. y5 = repeat(x4, (2, 2, 2, 2), axis=0)
  376. assert_equal(y5, [0, 0, 1, 1, 2, 2, 3, 3])
  377. y6 = repeat(x4, 2, axis=0)
  378. assert_equal(y5, y6)
  379. y7 = x4.repeat((2, 2, 2, 2), axis=0)
  380. assert_equal(y5, y7)
  381. y8 = x4.repeat(2, 0)
  382. assert_equal(y5, y8)
  383. y9 = x4.copy()
  384. assert_equal(y9._data, x4._data)
  385. assert_equal(y9._mask, x4._mask)
  386. x = masked_array([1, 2, 3], mask=[0, 1, 0])
  387. # Copy is False by default
  388. y = masked_array(x)
  389. assert_equal(y._data.ctypes.data, x._data.ctypes.data)
  390. assert_equal(y._mask.ctypes.data, x._mask.ctypes.data)
  391. y = masked_array(x, copy=True)
  392. assert_not_equal(y._data.ctypes.data, x._data.ctypes.data)
  393. assert_not_equal(y._mask.ctypes.data, x._mask.ctypes.data)
  394. def test_copy_0d(self):
  395. # gh-9430
  396. x = np.ma.array(43, mask=True)
  397. xc = x.copy()
  398. assert_equal(xc.mask, True)
  399. def test_copy_on_python_builtins(self):
  400. # Tests copy works on python builtins (issue#8019)
  401. assert_(isMaskedArray(np.ma.copy([1,2,3])))
  402. assert_(isMaskedArray(np.ma.copy((1,2,3))))
  403. def test_copy_immutable(self):
  404. # Tests that the copy method is immutable, GitHub issue #5247
  405. a = np.ma.array([1, 2, 3])
  406. b = np.ma.array([4, 5, 6])
  407. a_copy_method = a.copy
  408. b.copy
  409. assert_equal(a_copy_method(), [1, 2, 3])
  410. def test_deepcopy(self):
  411. from copy import deepcopy
  412. a = array([0, 1, 2], mask=[False, True, False])
  413. copied = deepcopy(a)
  414. assert_equal(copied.mask, a.mask)
  415. assert_not_equal(id(a._mask), id(copied._mask))
  416. copied[1] = 1
  417. assert_equal(copied.mask, [0, 0, 0])
  418. assert_equal(a.mask, [0, 1, 0])
  419. copied = deepcopy(a)
  420. assert_equal(copied.mask, a.mask)
  421. copied.mask[1] = False
  422. assert_equal(copied.mask, [0, 0, 0])
  423. assert_equal(a.mask, [0, 1, 0])
  424. def test_format(self):
  425. a = array([0, 1, 2], mask=[False, True, False])
  426. assert_equal(format(a), "[0 -- 2]")
  427. assert_equal(format(masked), "--")
  428. assert_equal(format(masked, ""), "--")
  429. # Postponed from PR #15410, perhaps address in the future.
  430. # assert_equal(format(masked, " >5"), " --")
  431. # assert_equal(format(masked, " <5"), "-- ")
  432. # Expect a FutureWarning for using format_spec with MaskedElement
  433. with assert_warns(FutureWarning):
  434. with_format_string = format(masked, " >5")
  435. assert_equal(with_format_string, "--")
  436. def test_str_repr(self):
  437. a = array([0, 1, 2], mask=[False, True, False])
  438. assert_equal(str(a), '[0 -- 2]')
  439. assert_equal(
  440. repr(a),
  441. textwrap.dedent('''\
  442. masked_array(data=[0, --, 2],
  443. mask=[False, True, False],
  444. fill_value=999999)''')
  445. )
  446. # arrays with a continuation
  447. a = np.ma.arange(2000)
  448. a[1:50] = np.ma.masked
  449. assert_equal(
  450. repr(a),
  451. textwrap.dedent('''\
  452. masked_array(data=[0, --, --, ..., 1997, 1998, 1999],
  453. mask=[False, True, True, ..., False, False, False],
  454. fill_value=999999)''')
  455. )
  456. # line-wrapped 1d arrays are correctly aligned
  457. a = np.ma.arange(20)
  458. assert_equal(
  459. repr(a),
  460. textwrap.dedent('''\
  461. masked_array(data=[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
  462. 14, 15, 16, 17, 18, 19],
  463. mask=False,
  464. fill_value=999999)''')
  465. )
  466. # 2d arrays cause wrapping
  467. a = array([[1, 2, 3], [4, 5, 6]], dtype=np.int8)
  468. a[1,1] = np.ma.masked
  469. assert_equal(
  470. repr(a),
  471. textwrap.dedent('''\
  472. masked_array(
  473. data=[[1, 2, 3],
  474. [4, --, 6]],
  475. mask=[[False, False, False],
  476. [False, True, False]],
  477. fill_value=999999,
  478. dtype=int8)''')
  479. )
  480. # but not it they're a row vector
  481. assert_equal(
  482. repr(a[:1]),
  483. textwrap.dedent('''\
  484. masked_array(data=[[1, 2, 3]],
  485. mask=[[False, False, False]],
  486. fill_value=999999,
  487. dtype=int8)''')
  488. )
  489. # dtype=int is implied, so not shown
  490. assert_equal(
  491. repr(a.astype(int)),
  492. textwrap.dedent('''\
  493. masked_array(
  494. data=[[1, 2, 3],
  495. [4, --, 6]],
  496. mask=[[False, False, False],
  497. [False, True, False]],
  498. fill_value=999999)''')
  499. )
  500. def test_str_repr_legacy(self):
  501. oldopts = np.get_printoptions()
  502. np.set_printoptions(legacy='1.13')
  503. try:
  504. a = array([0, 1, 2], mask=[False, True, False])
  505. assert_equal(str(a), '[0 -- 2]')
  506. assert_equal(repr(a), 'masked_array(data = [0 -- 2],\n'
  507. ' mask = [False True False],\n'
  508. ' fill_value = 999999)\n')
  509. a = np.ma.arange(2000)
  510. a[1:50] = np.ma.masked
  511. assert_equal(
  512. repr(a),
  513. 'masked_array(data = [0 -- -- ..., 1997 1998 1999],\n'
  514. ' mask = [False True True ..., False False False],\n'
  515. ' fill_value = 999999)\n'
  516. )
  517. finally:
  518. np.set_printoptions(**oldopts)
  519. def test_0d_unicode(self):
  520. u = u'caf\xe9'
  521. utype = type(u)
  522. arr_nomask = np.ma.array(u)
  523. arr_masked = np.ma.array(u, mask=True)
  524. assert_equal(utype(arr_nomask), u)
  525. assert_equal(utype(arr_masked), u'--')
  526. def test_pickling(self):
  527. # Tests pickling
  528. for dtype in (int, float, str, object):
  529. a = arange(10).astype(dtype)
  530. a.fill_value = 999
  531. masks = ([0, 0, 0, 1, 0, 1, 0, 1, 0, 1], # partially masked
  532. True, # Fully masked
  533. False) # Fully unmasked
  534. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  535. for mask in masks:
  536. a.mask = mask
  537. a_pickled = pickle.loads(pickle.dumps(a, protocol=proto))
  538. assert_equal(a_pickled._mask, a._mask)
  539. assert_equal(a_pickled._data, a._data)
  540. if dtype in (object, int):
  541. assert_equal(a_pickled.fill_value, 999)
  542. else:
  543. assert_equal(a_pickled.fill_value, dtype(999))
  544. assert_array_equal(a_pickled.mask, mask)
  545. def test_pickling_subbaseclass(self):
  546. # Test pickling w/ a subclass of ndarray
  547. x = np.array([(1.0, 2), (3.0, 4)],
  548. dtype=[('x', float), ('y', int)]).view(np.recarray)
  549. a = masked_array(x, mask=[(True, False), (False, True)])
  550. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  551. a_pickled = pickle.loads(pickle.dumps(a, protocol=proto))
  552. assert_equal(a_pickled._mask, a._mask)
  553. assert_equal(a_pickled, a)
  554. assert_(isinstance(a_pickled._data, np.recarray))
  555. def test_pickling_maskedconstant(self):
  556. # Test pickling MaskedConstant
  557. mc = np.ma.masked
  558. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  559. mc_pickled = pickle.loads(pickle.dumps(mc, protocol=proto))
  560. assert_equal(mc_pickled._baseclass, mc._baseclass)
  561. assert_equal(mc_pickled._mask, mc._mask)
  562. assert_equal(mc_pickled._data, mc._data)
  563. def test_pickling_wstructured(self):
  564. # Tests pickling w/ structured array
  565. a = array([(1, 1.), (2, 2.)], mask=[(0, 0), (0, 1)],
  566. dtype=[('a', int), ('b', float)])
  567. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  568. a_pickled = pickle.loads(pickle.dumps(a, protocol=proto))
  569. assert_equal(a_pickled._mask, a._mask)
  570. assert_equal(a_pickled, a)
  571. def test_pickling_keepalignment(self):
  572. # Tests pickling w/ F_CONTIGUOUS arrays
  573. a = arange(10)
  574. a.shape = (-1, 2)
  575. b = a.T
  576. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  577. test = pickle.loads(pickle.dumps(b, protocol=proto))
  578. assert_equal(test, b)
  579. def test_single_element_subscript(self):
  580. # Tests single element subscripts of Maskedarrays.
  581. a = array([1, 3, 2])
  582. b = array([1, 3, 2], mask=[1, 0, 1])
  583. assert_equal(a[0].shape, ())
  584. assert_equal(b[0].shape, ())
  585. assert_equal(b[1].shape, ())
  586. def test_topython(self):
  587. # Tests some communication issues with Python.
  588. assert_equal(1, int(array(1)))
  589. assert_equal(1.0, float(array(1)))
  590. assert_equal(1, int(array([[[1]]])))
  591. assert_equal(1.0, float(array([[1]])))
  592. assert_raises(TypeError, float, array([1, 1]))
  593. with suppress_warnings() as sup:
  594. sup.filter(UserWarning, 'Warning: converting a masked element')
  595. assert_(np.isnan(float(array([1], mask=[1]))))
  596. a = array([1, 2, 3], mask=[1, 0, 0])
  597. assert_raises(TypeError, lambda: float(a))
  598. assert_equal(float(a[-1]), 3.)
  599. assert_(np.isnan(float(a[0])))
  600. assert_raises(TypeError, int, a)
  601. assert_equal(int(a[-1]), 3)
  602. assert_raises(MAError, lambda:int(a[0]))
  603. def test_oddfeatures_1(self):
  604. # Test of other odd features
  605. x = arange(20)
  606. x = x.reshape(4, 5)
  607. x.flat[5] = 12
  608. assert_(x[1, 0] == 12)
  609. z = x + 10j * x
  610. assert_equal(z.real, x)
  611. assert_equal(z.imag, 10 * x)
  612. assert_equal((z * conjugate(z)).real, 101 * x * x)
  613. z.imag[...] = 0.0
  614. x = arange(10)
  615. x[3] = masked
  616. assert_(str(x[3]) == str(masked))
  617. c = x >= 8
  618. assert_(count(where(c, masked, masked)) == 0)
  619. assert_(shape(where(c, masked, masked)) == c.shape)
  620. z = masked_where(c, x)
  621. assert_(z.dtype is x.dtype)
  622. assert_(z[3] is masked)
  623. assert_(z[4] is not masked)
  624. assert_(z[7] is not masked)
  625. assert_(z[8] is masked)
  626. assert_(z[9] is masked)
  627. assert_equal(x, z)
  628. def test_oddfeatures_2(self):
  629. # Tests some more features.
  630. x = array([1., 2., 3., 4., 5.])
  631. c = array([1, 1, 1, 0, 0])
  632. x[2] = masked
  633. z = where(c, x, -x)
  634. assert_equal(z, [1., 2., 0., -4., -5])
  635. c[0] = masked
  636. z = where(c, x, -x)
  637. assert_equal(z, [1., 2., 0., -4., -5])
  638. assert_(z[0] is masked)
  639. assert_(z[1] is not masked)
  640. assert_(z[2] is masked)
  641. @suppress_copy_mask_on_assignment
  642. def test_oddfeatures_3(self):
  643. # Tests some generic features
  644. atest = array([10], mask=True)
  645. btest = array([20])
  646. idx = atest.mask
  647. atest[idx] = btest[idx]
  648. assert_equal(atest, [20])
  649. def test_filled_with_object_dtype(self):
  650. a = np.ma.masked_all(1, dtype='O')
  651. assert_equal(a.filled('x')[0], 'x')
  652. def test_filled_with_flexible_dtype(self):
  653. # Test filled w/ flexible dtype
  654. flexi = array([(1, 1, 1)],
  655. dtype=[('i', int), ('s', '|S8'), ('f', float)])
  656. flexi[0] = masked
  657. assert_equal(flexi.filled(),
  658. np.array([(default_fill_value(0),
  659. default_fill_value('0'),
  660. default_fill_value(0.),)], dtype=flexi.dtype))
  661. flexi[0] = masked
  662. assert_equal(flexi.filled(1),
  663. np.array([(1, '1', 1.)], dtype=flexi.dtype))
  664. def test_filled_with_mvoid(self):
  665. # Test filled w/ mvoid
  666. ndtype = [('a', int), ('b', float)]
  667. a = mvoid((1, 2.), mask=[(0, 1)], dtype=ndtype)
  668. # Filled using default
  669. test = a.filled()
  670. assert_equal(tuple(test), (1, default_fill_value(1.)))
  671. # Explicit fill_value
  672. test = a.filled((-1, -1))
  673. assert_equal(tuple(test), (1, -1))
  674. # Using predefined filling values
  675. a.fill_value = (-999, -999)
  676. assert_equal(tuple(a.filled()), (1, -999))
  677. def test_filled_with_nested_dtype(self):
  678. # Test filled w/ nested dtype
  679. ndtype = [('A', int), ('B', [('BA', int), ('BB', int)])]
  680. a = array([(1, (1, 1)), (2, (2, 2))],
  681. mask=[(0, (1, 0)), (0, (0, 1))], dtype=ndtype)
  682. test = a.filled(0)
  683. control = np.array([(1, (0, 1)), (2, (2, 0))], dtype=ndtype)
  684. assert_equal(test, control)
  685. test = a['B'].filled(0)
  686. control = np.array([(0, 1), (2, 0)], dtype=a['B'].dtype)
  687. assert_equal(test, control)
  688. # test if mask gets set correctly (see #6760)
  689. Z = numpy.ma.zeros(2, numpy.dtype([("A", "(2,2)i1,(2,2)i1", (2,2))]))
  690. assert_equal(Z.data.dtype, numpy.dtype([('A', [('f0', 'i1', (2, 2)),
  691. ('f1', 'i1', (2, 2))], (2, 2))]))
  692. assert_equal(Z.mask.dtype, numpy.dtype([('A', [('f0', '?', (2, 2)),
  693. ('f1', '?', (2, 2))], (2, 2))]))
  694. def test_filled_with_f_order(self):
  695. # Test filled w/ F-contiguous array
  696. a = array(np.array([(0, 1, 2), (4, 5, 6)], order='F'),
  697. mask=np.array([(0, 0, 1), (1, 0, 0)], order='F'),
  698. order='F') # this is currently ignored
  699. assert_(a.flags['F_CONTIGUOUS'])
  700. assert_(a.filled(0).flags['F_CONTIGUOUS'])
  701. def test_optinfo_propagation(self):
  702. # Checks that _optinfo dictionary isn't back-propagated
  703. x = array([1, 2, 3, ], dtype=float)
  704. x._optinfo['info'] = '???'
  705. y = x.copy()
  706. assert_equal(y._optinfo['info'], '???')
  707. y._optinfo['info'] = '!!!'
  708. assert_equal(x._optinfo['info'], '???')
  709. def test_optinfo_forward_propagation(self):
  710. a = array([1,2,2,4])
  711. a._optinfo["key"] = "value"
  712. assert_equal(a._optinfo["key"], (a == 2)._optinfo["key"])
  713. assert_equal(a._optinfo["key"], (a != 2)._optinfo["key"])
  714. assert_equal(a._optinfo["key"], (a > 2)._optinfo["key"])
  715. assert_equal(a._optinfo["key"], (a >= 2)._optinfo["key"])
  716. assert_equal(a._optinfo["key"], (a <= 2)._optinfo["key"])
  717. assert_equal(a._optinfo["key"], (a + 2)._optinfo["key"])
  718. assert_equal(a._optinfo["key"], (a - 2)._optinfo["key"])
  719. assert_equal(a._optinfo["key"], (a * 2)._optinfo["key"])
  720. assert_equal(a._optinfo["key"], (a / 2)._optinfo["key"])
  721. assert_equal(a._optinfo["key"], a[:2]._optinfo["key"])
  722. assert_equal(a._optinfo["key"], a[[0,0,2]]._optinfo["key"])
  723. assert_equal(a._optinfo["key"], np.exp(a)._optinfo["key"])
  724. assert_equal(a._optinfo["key"], np.abs(a)._optinfo["key"])
  725. assert_equal(a._optinfo["key"], array(a, copy=True)._optinfo["key"])
  726. assert_equal(a._optinfo["key"], np.zeros_like(a)._optinfo["key"])
  727. def test_fancy_printoptions(self):
  728. # Test printing a masked array w/ fancy dtype.
  729. fancydtype = np.dtype([('x', int), ('y', [('t', int), ('s', float)])])
  730. test = array([(1, (2, 3.0)), (4, (5, 6.0))],
  731. mask=[(1, (0, 1)), (0, (1, 0))],
  732. dtype=fancydtype)
  733. control = "[(--, (2, --)) (4, (--, 6.0))]"
  734. assert_equal(str(test), control)
  735. # Test 0-d array with multi-dimensional dtype
  736. t_2d0 = masked_array(data = (0, [[0.0, 0.0, 0.0],
  737. [0.0, 0.0, 0.0]],
  738. 0.0),
  739. mask = (False, [[True, False, True],
  740. [False, False, True]],
  741. False),
  742. dtype = "int, (2,3)float, float")
  743. control = "(0, [[--, 0.0, --], [0.0, 0.0, --]], 0.0)"
  744. assert_equal(str(t_2d0), control)
  745. def test_flatten_structured_array(self):
  746. # Test flatten_structured_array on arrays
  747. # On ndarray
  748. ndtype = [('a', int), ('b', float)]
  749. a = np.array([(1, 1), (2, 2)], dtype=ndtype)
  750. test = flatten_structured_array(a)
  751. control = np.array([[1., 1.], [2., 2.]], dtype=float)
  752. assert_equal(test, control)
  753. assert_equal(test.dtype, control.dtype)
  754. # On masked_array
  755. a = array([(1, 1), (2, 2)], mask=[(0, 1), (1, 0)], dtype=ndtype)
  756. test = flatten_structured_array(a)
  757. control = array([[1., 1.], [2., 2.]],
  758. mask=[[0, 1], [1, 0]], dtype=float)
  759. assert_equal(test, control)
  760. assert_equal(test.dtype, control.dtype)
  761. assert_equal(test.mask, control.mask)
  762. # On masked array with nested structure
  763. ndtype = [('a', int), ('b', [('ba', int), ('bb', float)])]
  764. a = array([(1, (1, 1.1)), (2, (2, 2.2))],
  765. mask=[(0, (1, 0)), (1, (0, 1))], dtype=ndtype)
  766. test = flatten_structured_array(a)
  767. control = array([[1., 1., 1.1], [2., 2., 2.2]],
  768. mask=[[0, 1, 0], [1, 0, 1]], dtype=float)
  769. assert_equal(test, control)
  770. assert_equal(test.dtype, control.dtype)
  771. assert_equal(test.mask, control.mask)
  772. # Keeping the initial shape
  773. ndtype = [('a', int), ('b', float)]
  774. a = np.array([[(1, 1), ], [(2, 2), ]], dtype=ndtype)
  775. test = flatten_structured_array(a)
  776. control = np.array([[[1., 1.], ], [[2., 2.], ]], dtype=float)
  777. assert_equal(test, control)
  778. assert_equal(test.dtype, control.dtype)
  779. def test_void0d(self):
  780. # Test creating a mvoid object
  781. ndtype = [('a', int), ('b', int)]
  782. a = np.array([(1, 2,)], dtype=ndtype)[0]
  783. f = mvoid(a)
  784. assert_(isinstance(f, mvoid))
  785. a = masked_array([(1, 2)], mask=[(1, 0)], dtype=ndtype)[0]
  786. assert_(isinstance(a, mvoid))
  787. a = masked_array([(1, 2), (1, 2)], mask=[(1, 0), (0, 0)], dtype=ndtype)
  788. f = mvoid(a._data[0], a._mask[0])
  789. assert_(isinstance(f, mvoid))
  790. def test_mvoid_getitem(self):
  791. # Test mvoid.__getitem__
  792. ndtype = [('a', int), ('b', int)]
  793. a = masked_array([(1, 2,), (3, 4)], mask=[(0, 0), (1, 0)],
  794. dtype=ndtype)
  795. # w/o mask
  796. f = a[0]
  797. assert_(isinstance(f, mvoid))
  798. assert_equal((f[0], f['a']), (1, 1))
  799. assert_equal(f['b'], 2)
  800. # w/ mask
  801. f = a[1]
  802. assert_(isinstance(f, mvoid))
  803. assert_(f[0] is masked)
  804. assert_(f['a'] is masked)
  805. assert_equal(f[1], 4)
  806. # exotic dtype
  807. A = masked_array(data=[([0,1],)],
  808. mask=[([True, False],)],
  809. dtype=[("A", ">i2", (2,))])
  810. assert_equal(A[0]["A"], A["A"][0])
  811. assert_equal(A[0]["A"], masked_array(data=[0, 1],
  812. mask=[True, False], dtype=">i2"))
  813. def test_mvoid_iter(self):
  814. # Test iteration on __getitem__
  815. ndtype = [('a', int), ('b', int)]
  816. a = masked_array([(1, 2,), (3, 4)], mask=[(0, 0), (1, 0)],
  817. dtype=ndtype)
  818. # w/o mask
  819. assert_equal(list(a[0]), [1, 2])
  820. # w/ mask
  821. assert_equal(list(a[1]), [masked, 4])
  822. def test_mvoid_print(self):
  823. # Test printing a mvoid
  824. mx = array([(1, 1), (2, 2)], dtype=[('a', int), ('b', int)])
  825. assert_equal(str(mx[0]), "(1, 1)")
  826. mx['b'][0] = masked
  827. ini_display = masked_print_option._display
  828. masked_print_option.set_display("-X-")
  829. try:
  830. assert_equal(str(mx[0]), "(1, -X-)")
  831. assert_equal(repr(mx[0]), "(1, -X-)")
  832. finally:
  833. masked_print_option.set_display(ini_display)
  834. # also check if there are object datatypes (see gh-7493)
  835. mx = array([(1,), (2,)], dtype=[('a', 'O')])
  836. assert_equal(str(mx[0]), "(1,)")
  837. def test_mvoid_multidim_print(self):
  838. # regression test for gh-6019
  839. t_ma = masked_array(data = [([1, 2, 3],)],
  840. mask = [([False, True, False],)],
  841. fill_value = ([999999, 999999, 999999],),
  842. dtype = [('a', '<i4', (3,))])
  843. assert_(str(t_ma[0]) == "([1, --, 3],)")
  844. assert_(repr(t_ma[0]) == "([1, --, 3],)")
  845. # additional tests with structured arrays
  846. t_2d = masked_array(data = [([[1, 2], [3,4]],)],
  847. mask = [([[False, True], [True, False]],)],
  848. dtype = [('a', '<i4', (2,2))])
  849. assert_(str(t_2d[0]) == "([[1, --], [--, 4]],)")
  850. assert_(repr(t_2d[0]) == "([[1, --], [--, 4]],)")
  851. t_0d = masked_array(data = [(1,2)],
  852. mask = [(True,False)],
  853. dtype = [('a', '<i4'), ('b', '<i4')])
  854. assert_(str(t_0d[0]) == "(--, 2)")
  855. assert_(repr(t_0d[0]) == "(--, 2)")
  856. t_2d = masked_array(data = [([[1, 2], [3,4]], 1)],
  857. mask = [([[False, True], [True, False]], False)],
  858. dtype = [('a', '<i4', (2,2)), ('b', float)])
  859. assert_(str(t_2d[0]) == "([[1, --], [--, 4]], 1.0)")
  860. assert_(repr(t_2d[0]) == "([[1, --], [--, 4]], 1.0)")
  861. t_ne = masked_array(data=[(1, (1, 1))],
  862. mask=[(True, (True, False))],
  863. dtype = [('a', '<i4'), ('b', 'i4,i4')])
  864. assert_(str(t_ne[0]) == "(--, (--, 1))")
  865. assert_(repr(t_ne[0]) == "(--, (--, 1))")
  866. def test_object_with_array(self):
  867. mx1 = masked_array([1.], mask=[True])
  868. mx2 = masked_array([1., 2.])
  869. mx = masked_array([mx1, mx2], mask=[False, True], dtype=object)
  870. assert_(mx[0] is mx1)
  871. assert_(mx[1] is not mx2)
  872. assert_(np.all(mx[1].data == mx2.data))
  873. assert_(np.all(mx[1].mask))
  874. # check that we return a view.
  875. mx[1].data[0] = 0.
  876. assert_(mx2[0] == 0.)
  877. class TestMaskedArrayArithmetic:
  878. # Base test class for MaskedArrays.
  879. def setup(self):
  880. # Base data definition.
  881. x = np.array([1., 1., 1., -2., pi/2.0, 4., 5., -10., 10., 1., 2., 3.])
  882. y = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.])
  883. a10 = 10.
  884. m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
  885. m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1]
  886. xm = masked_array(x, mask=m1)
  887. ym = masked_array(y, mask=m2)
  888. z = np.array([-.5, 0., .5, .8])
  889. zm = masked_array(z, mask=[0, 1, 0, 0])
  890. xf = np.where(m1, 1e+20, x)
  891. xm.set_fill_value(1e+20)
  892. self.d = (x, y, a10, m1, m2, xm, ym, z, zm, xf)
  893. self.err_status = np.geterr()
  894. np.seterr(divide='ignore', invalid='ignore')
  895. def teardown(self):
  896. np.seterr(**self.err_status)
  897. def test_basic_arithmetic(self):
  898. # Test of basic arithmetic.
  899. (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
  900. a2d = array([[1, 2], [0, 4]])
  901. a2dm = masked_array(a2d, [[0, 0], [1, 0]])
  902. assert_equal(a2d * a2d, a2d * a2dm)
  903. assert_equal(a2d + a2d, a2d + a2dm)
  904. assert_equal(a2d - a2d, a2d - a2dm)
  905. for s in [(12,), (4, 3), (2, 6)]:
  906. x = x.reshape(s)
  907. y = y.reshape(s)
  908. xm = xm.reshape(s)
  909. ym = ym.reshape(s)
  910. xf = xf.reshape(s)
  911. assert_equal(-x, -xm)
  912. assert_equal(x + y, xm + ym)
  913. assert_equal(x - y, xm - ym)
  914. assert_equal(x * y, xm * ym)
  915. assert_equal(x / y, xm / ym)
  916. assert_equal(a10 + y, a10 + ym)
  917. assert_equal(a10 - y, a10 - ym)
  918. assert_equal(a10 * y, a10 * ym)
  919. assert_equal(a10 / y, a10 / ym)
  920. assert_equal(x + a10, xm + a10)
  921. assert_equal(x - a10, xm - a10)
  922. assert_equal(x * a10, xm * a10)
  923. assert_equal(x / a10, xm / a10)
  924. assert_equal(x ** 2, xm ** 2)
  925. assert_equal(abs(x) ** 2.5, abs(xm) ** 2.5)
  926. assert_equal(x ** y, xm ** ym)
  927. assert_equal(np.add(x, y), add(xm, ym))
  928. assert_equal(np.subtract(x, y), subtract(xm, ym))
  929. assert_equal(np.multiply(x, y), multiply(xm, ym))
  930. assert_equal(np.divide(x, y), divide(xm, ym))
  931. def test_divide_on_different_shapes(self):
  932. x = arange(6, dtype=float)
  933. x.shape = (2, 3)
  934. y = arange(3, dtype=float)
  935. z = x / y
  936. assert_equal(z, [[-1., 1., 1.], [-1., 4., 2.5]])
  937. assert_equal(z.mask, [[1, 0, 0], [1, 0, 0]])
  938. z = x / y[None,:]
  939. assert_equal(z, [[-1., 1., 1.], [-1., 4., 2.5]])
  940. assert_equal(z.mask, [[1, 0, 0], [1, 0, 0]])
  941. y = arange(2, dtype=float)
  942. z = x / y[:, None]
  943. assert_equal(z, [[-1., -1., -1.], [3., 4., 5.]])
  944. assert_equal(z.mask, [[1, 1, 1], [0, 0, 0]])
  945. def test_mixed_arithmetic(self):
  946. # Tests mixed arithmetics.
  947. na = np.array([1])
  948. ma = array([1])
  949. assert_(isinstance(na + ma, MaskedArray))
  950. assert_(isinstance(ma + na, MaskedArray))
  951. def test_limits_arithmetic(self):
  952. tiny = np.finfo(float).tiny
  953. a = array([tiny, 1. / tiny, 0.])
  954. assert_equal(getmaskarray(a / 2), [0, 0, 0])
  955. assert_equal(getmaskarray(2 / a), [1, 0, 1])
  956. def test_masked_singleton_arithmetic(self):
  957. # Tests some scalar arithmetics on MaskedArrays.
  958. # Masked singleton should remain masked no matter what
  959. xm = array(0, mask=1)
  960. assert_((1 / array(0)).mask)
  961. assert_((1 + xm).mask)
  962. assert_((-xm).mask)
  963. assert_(maximum(xm, xm).mask)
  964. assert_(minimum(xm, xm).mask)
  965. def test_masked_singleton_equality(self):
  966. # Tests (in)equality on masked singleton
  967. a = array([1, 2, 3], mask=[1, 1, 0])
  968. assert_((a[0] == 0) is masked)
  969. assert_((a[0] != 0) is masked)
  970. assert_equal((a[-1] == 0), False)
  971. assert_equal((a[-1] != 0), True)
  972. def test_arithmetic_with_masked_singleton(self):
  973. # Checks that there's no collapsing to masked
  974. x = masked_array([1, 2])
  975. y = x * masked
  976. assert_equal(y.shape, x.shape)
  977. assert_equal(y._mask, [True, True])
  978. y = x[0] * masked
  979. assert_(y is masked)
  980. y = x + masked
  981. assert_equal(y.shape, x.shape)
  982. assert_equal(y._mask, [True, True])
  983. def test_arithmetic_with_masked_singleton_on_1d_singleton(self):
  984. # Check that we're not losing the shape of a singleton
  985. x = masked_array([1, ])
  986. y = x + masked
  987. assert_equal(y.shape, x.shape)
  988. assert_equal(y.mask, [True, ])
  989. def test_scalar_arithmetic(self):
  990. x = array(0, mask=0)
  991. assert_equal(x.filled().ctypes.data, x.ctypes.data)
  992. # Make sure we don't lose the shape in some circumstances
  993. xm = array((0, 0)) / 0.
  994. assert_equal(xm.shape, (2,))
  995. assert_equal(xm.mask, [1, 1])
  996. def test_basic_ufuncs(self):
  997. # Test various functions such as sin, cos.
  998. (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
  999. assert_equal(np.cos(x), cos(xm))
  1000. assert_equal(np.cosh(x), cosh(xm))
  1001. assert_equal(np.sin(x), sin(xm))
  1002. assert_equal(np.sinh(x), sinh(xm))
  1003. assert_equal(np.tan(x), tan(xm))
  1004. assert_equal(np.tanh(x), tanh(xm))
  1005. assert_equal(np.sqrt(abs(x)), sqrt(xm))
  1006. assert_equal(np.log(abs(x)), log(xm))
  1007. assert_equal(np.log10(abs(x)), log10(xm))
  1008. assert_equal(np.exp(x), exp(xm))
  1009. assert_equal(np.arcsin(z), arcsin(zm))
  1010. assert_equal(np.arccos(z), arccos(zm))
  1011. assert_equal(np.arctan(z), arctan(zm))
  1012. assert_equal(np.arctan2(x, y), arctan2(xm, ym))
  1013. assert_equal(np.absolute(x), absolute(xm))
  1014. assert_equal(np.angle(x + 1j*y), angle(xm + 1j*ym))
  1015. assert_equal(np.angle(x + 1j*y, deg=True), angle(xm + 1j*ym, deg=True))
  1016. assert_equal(np.equal(x, y), equal(xm, ym))
  1017. assert_equal(np.not_equal(x, y), not_equal(xm, ym))
  1018. assert_equal(np.less(x, y), less(xm, ym))
  1019. assert_equal(np.greater(x, y), greater(xm, ym))
  1020. assert_equal(np.less_equal(x, y), less_equal(xm, ym))
  1021. assert_equal(np.greater_equal(x, y), greater_equal(xm, ym))
  1022. assert_equal(np.conjugate(x), conjugate(xm))
  1023. def test_count_func(self):
  1024. # Tests count
  1025. assert_equal(1, count(1))
  1026. assert_equal(0, array(1, mask=[1]))
  1027. ott = array([0., 1., 2., 3.], mask=[1, 0, 0, 0])
  1028. res = count(ott)
  1029. assert_(res.dtype.type is np.intp)
  1030. assert_equal(3, res)
  1031. ott = ott.reshape((2, 2))
  1032. res = count(ott)
  1033. assert_(res.dtype.type is np.intp)
  1034. assert_equal(3, res)
  1035. res = count(ott, 0)
  1036. assert_(isinstance(res, ndarray))
  1037. assert_equal([1, 2], res)
  1038. assert_(getmask(res) is nomask)
  1039. ott = array([0., 1., 2., 3.])
  1040. res = count(ott, 0)
  1041. assert_(isinstance(res, ndarray))
  1042. assert_(res.dtype.type is np.intp)
  1043. assert_raises(np.AxisError, ott.count, axis=1)
  1044. def test_count_on_python_builtins(self):
  1045. # Tests count works on python builtins (issue#8019)
  1046. assert_equal(3, count([1,2,3]))
  1047. assert_equal(2, count((1,2)))
  1048. def test_minmax_func(self):
  1049. # Tests minimum and maximum.
  1050. (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
  1051. # max doesn't work if shaped
  1052. xr = np.ravel(x)
  1053. xmr = ravel(xm)
  1054. # following are true because of careful selection of data
  1055. assert_equal(max(xr), maximum.reduce(xmr))
  1056. assert_equal(min(xr), minimum.reduce(xmr))
  1057. assert_equal(minimum([1, 2, 3], [4, 0, 9]), [1, 0, 3])
  1058. assert_equal(maximum([1, 2, 3], [4, 0, 9]), [4, 2, 9])
  1059. x = arange(5)
  1060. y = arange(5) - 2
  1061. x[3] = masked
  1062. y[0] = masked
  1063. assert_equal(minimum(x, y), where(less(x, y), x, y))
  1064. assert_equal(maximum(x, y), where(greater(x, y), x, y))
  1065. assert_(minimum.reduce(x) == 0)
  1066. assert_(maximum.reduce(x) == 4)
  1067. x = arange(4).reshape(2, 2)
  1068. x[-1, -1] = masked
  1069. assert_equal(maximum.reduce(x, axis=None), 2)
  1070. def test_minimummaximum_func(self):
  1071. a = np.ones((2, 2))
  1072. aminimum = minimum(a, a)
  1073. assert_(isinstance(aminimum, MaskedArray))
  1074. assert_equal(aminimum, np.minimum(a, a))
  1075. aminimum = minimum.outer(a, a)
  1076. assert_(isinstance(aminimum, MaskedArray))
  1077. assert_equal(aminimum, np.minimum.outer(a, a))
  1078. amaximum = maximum(a, a)
  1079. assert_(isinstance(amaximum, MaskedArray))
  1080. assert_equal(amaximum, np.maximum(a, a))
  1081. amaximum = maximum.outer(a, a)
  1082. assert_(isinstance(amaximum, MaskedArray))
  1083. assert_equal(amaximum, np.maximum.outer(a, a))
  1084. def test_minmax_reduce(self):
  1085. # Test np.min/maximum.reduce on array w/ full False mask
  1086. a = array([1, 2, 3], mask=[False, False, False])
  1087. b = np.maximum.reduce(a)
  1088. assert_equal(b, 3)
  1089. def test_minmax_funcs_with_output(self):
  1090. # Tests the min/max functions with explicit outputs
  1091. mask = np.random.rand(12).round()
  1092. xm = array(np.random.uniform(0, 10, 12), mask=mask)
  1093. xm.shape = (3, 4)
  1094. for funcname in ('min', 'max'):
  1095. # Initialize
  1096. npfunc = getattr(np, funcname)
  1097. mafunc = getattr(numpy.ma.core, funcname)
  1098. # Use the np version
  1099. nout = np.empty((4,), dtype=int)
  1100. try:
  1101. result = npfunc(xm, axis=0, out=nout)
  1102. except MaskError:
  1103. pass
  1104. nout = np.empty((4,), dtype=float)
  1105. result = npfunc(xm, axis=0, out=nout)
  1106. assert_(result is nout)
  1107. # Use the ma version
  1108. nout.fill(-999)
  1109. result = mafunc(xm, axis=0, out=nout)
  1110. assert_(result is nout)
  1111. def test_minmax_methods(self):
  1112. # Additional tests on max/min
  1113. (_, _, _, _, _, xm, _, _, _, _) = self.d
  1114. xm.shape = (xm.size,)
  1115. assert_equal(xm.max(), 10)
  1116. assert_(xm[0].max() is masked)
  1117. assert_(xm[0].max(0) is masked)
  1118. assert_(xm[0].max(-1) is masked)
  1119. assert_equal(xm.min(), -10.)
  1120. assert_(xm[0].min() is masked)
  1121. assert_(xm[0].min(0) is masked)
  1122. assert_(xm[0].min(-1) is masked)
  1123. assert_equal(xm.ptp(), 20.)
  1124. assert_(xm[0].ptp() is masked)
  1125. assert_(xm[0].ptp(0) is masked)
  1126. assert_(xm[0].ptp(-1) is masked)
  1127. x = array([1, 2, 3], mask=True)
  1128. assert_(x.min() is masked)
  1129. assert_(x.max() is masked)
  1130. assert_(x.ptp() is masked)
  1131. def test_minmax_dtypes(self):
  1132. # Additional tests on max/min for non-standard float and complex dtypes
  1133. x = np.array([1., 1., 1., -2., pi/2.0, 4., 5., -10., 10., 1., 2., 3.])
  1134. a10 = 10.
  1135. an10 = -10.0
  1136. m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
  1137. xm = masked_array(x, mask=m1)
  1138. xm.set_fill_value(1e+20)
  1139. float_dtypes = [np.half, np.single, np.double,
  1140. np.longdouble, np.cfloat, np.cdouble, np.clongdouble]
  1141. for float_dtype in float_dtypes:
  1142. assert_equal(masked_array(x, mask=m1, dtype=float_dtype).max(),
  1143. float_dtype(a10))
  1144. assert_equal(masked_array(x, mask=m1, dtype=float_dtype).min(),
  1145. float_dtype(an10))
  1146. assert_equal(xm.min(), an10)
  1147. assert_equal(xm.max(), a10)
  1148. # Non-complex type only test
  1149. for float_dtype in float_dtypes[:4]:
  1150. assert_equal(masked_array(x, mask=m1, dtype=float_dtype).max(),
  1151. float_dtype(a10))
  1152. assert_equal(masked_array(x, mask=m1, dtype=float_dtype).min(),
  1153. float_dtype(an10))
  1154. # Complex types only test
  1155. for float_dtype in float_dtypes[-3:]:
  1156. ym = masked_array([1e20+1j, 1e20-2j, 1e20-1j], mask=[0, 1, 0],
  1157. dtype=float_dtype)
  1158. assert_equal(ym.min(), float_dtype(1e20-1j))
  1159. assert_equal(ym.max(), float_dtype(1e20+1j))
  1160. zm = masked_array([np.inf+2j, np.inf+3j, -np.inf-1j], mask=[0, 1, 0],
  1161. dtype=float_dtype)
  1162. assert_equal(zm.min(), float_dtype(-np.inf-1j))
  1163. assert_equal(zm.max(), float_dtype(np.inf+2j))
  1164. cmax = np.inf - 1j * np.finfo(np.float64).max
  1165. assert masked_array([-cmax, 0], mask=[0, 1]).max() == -cmax
  1166. assert masked_array([cmax, 0], mask=[0, 1]).min() == cmax
  1167. def test_addsumprod(self):
  1168. # Tests add, sum, product.
  1169. (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
  1170. assert_equal(np.add.reduce(x), add.reduce(x))
  1171. assert_equal(np.add.accumulate(x), add.accumulate(x))
  1172. assert_equal(4, sum(array(4), axis=0))
  1173. assert_equal(4, sum(array(4), axis=0))
  1174. assert_equal(np.sum(x, axis=0), sum(x, axis=0))
  1175. assert_equal(np.sum(filled(xm, 0), axis=0), sum(xm, axis=0))
  1176. assert_equal(np.sum(x, 0), sum(x, 0))
  1177. assert_equal(np.product(x, axis=0), product(x, axis=0))
  1178. assert_equal(np.product(x, 0), product(x, 0))
  1179. assert_equal(np.product(filled(xm, 1), axis=0), product(xm, axis=0))
  1180. s = (3, 4)
  1181. x.shape = y.shape = xm.shape = ym.shape = s
  1182. if len(s) > 1:
  1183. assert_equal(np.concatenate((x, y), 1), concatenate((xm, ym), 1))
  1184. assert_equal(np.add.reduce(x, 1), add.reduce(x, 1))
  1185. assert_equal(np.sum(x, 1), sum(x, 1))
  1186. assert_equal(np.product(x, 1), product(x, 1))
  1187. def test_binops_d2D(self):
  1188. # Test binary operations on 2D data
  1189. a = array([[1.], [2.], [3.]], mask=[[False], [True], [True]])
  1190. b = array([[2., 3.], [4., 5.], [6., 7.]])
  1191. test = a * b
  1192. control = array([[2., 3.], [2., 2.], [3., 3.]],
  1193. mask=[[0, 0], [1, 1], [1, 1]])
  1194. assert_equal(test, control)
  1195. assert_equal(test.data, control.data)
  1196. assert_equal(test.mask, control.mask)
  1197. test = b * a
  1198. control = array([[2., 3.], [4., 5.], [6., 7.]],
  1199. mask=[[0, 0], [1, 1], [1, 1]])
  1200. assert_equal(test, control)
  1201. assert_equal(test.data, control.data)
  1202. assert_equal(test.mask, control.mask)
  1203. a = array([[1.], [2.], [3.]])
  1204. b = array([[2., 3.], [4., 5.], [6., 7.]],
  1205. mask=[[0, 0], [0, 0], [0, 1]])
  1206. test = a * b
  1207. control = array([[2, 3], [8, 10], [18, 3]],
  1208. mask=[[0, 0], [0, 0], [0, 1]])
  1209. assert_equal(test, control)
  1210. assert_equal(test.data, control.data)
  1211. assert_equal(test.mask, control.mask)
  1212. test = b * a
  1213. control = array([[2, 3], [8, 10], [18, 7]],
  1214. mask=[[0, 0], [0, 0], [0, 1]])
  1215. assert_equal(test, control)
  1216. assert_equal(test.data, control.data)
  1217. assert_equal(test.mask, control.mask)
  1218. def test_domained_binops_d2D(self):
  1219. # Test domained binary operations on 2D data
  1220. a = array([[1.], [2.], [3.]], mask=[[False], [True], [True]])
  1221. b = array([[2., 3.], [4., 5.], [6., 7.]])
  1222. test = a / b
  1223. control = array([[1. / 2., 1. / 3.], [2., 2.], [3., 3.]],
  1224. mask=[[0, 0], [1, 1], [1, 1]])
  1225. assert_equal(test, control)
  1226. assert_equal(test.data, control.data)
  1227. assert_equal(test.mask, control.mask)
  1228. test = b / a
  1229. control = array([[2. / 1., 3. / 1.], [4., 5.], [6., 7.]],
  1230. mask=[[0, 0], [1, 1], [1, 1]])
  1231. assert_equal(test, control)
  1232. assert_equal(test.data, control.data)
  1233. assert_equal(test.mask, control.mask)
  1234. a = array([[1.], [2.], [3.]])
  1235. b = array([[2., 3.], [4., 5.], [6., 7.]],
  1236. mask=[[0, 0], [0, 0], [0, 1]])
  1237. test = a / b
  1238. control = array([[1. / 2, 1. / 3], [2. / 4, 2. / 5], [3. / 6, 3]],
  1239. mask=[[0, 0], [0, 0], [0, 1]])
  1240. assert_equal(test, control)
  1241. assert_equal(test.data, control.data)
  1242. assert_equal(test.mask, control.mask)
  1243. test = b / a
  1244. control = array([[2 / 1., 3 / 1.], [4 / 2., 5 / 2.], [6 / 3., 7]],
  1245. mask=[[0, 0], [0, 0], [0, 1]])
  1246. assert_equal(test, control)
  1247. assert_equal(test.data, control.data)
  1248. assert_equal(test.mask, control.mask)
  1249. def test_noshrinking(self):
  1250. # Check that we don't shrink a mask when not wanted
  1251. # Binary operations
  1252. a = masked_array([1., 2., 3.], mask=[False, False, False],
  1253. shrink=False)
  1254. b = a + 1
  1255. assert_equal(b.mask, [0, 0, 0])
  1256. # In place binary operation
  1257. a += 1
  1258. assert_equal(a.mask, [0, 0, 0])
  1259. # Domained binary operation
  1260. b = a / 1.
  1261. assert_equal(b.mask, [0, 0, 0])
  1262. # In place binary operation
  1263. a /= 1.
  1264. assert_equal(a.mask, [0, 0, 0])
  1265. def test_ufunc_nomask(self):
  1266. # check the case ufuncs should set the mask to false
  1267. m = np.ma.array([1])
  1268. # check we don't get array([False], dtype=bool)
  1269. assert_equal(np.true_divide(m, 5).mask.shape, ())
  1270. def test_noshink_on_creation(self):
  1271. # Check that the mask is not shrunk on array creation when not wanted
  1272. a = np.ma.masked_values([1., 2.5, 3.1], 1.5, shrink=False)
  1273. assert_equal(a.mask, [0, 0, 0])
  1274. def test_mod(self):
  1275. # Tests mod
  1276. (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
  1277. assert_equal(mod(x, y), mod(xm, ym))
  1278. test = mod(ym, xm)
  1279. assert_equal(test, np.mod(ym, xm))
  1280. assert_equal(test.mask, mask_or(xm.mask, ym.mask))
  1281. test = mod(xm, ym)
  1282. assert_equal(test, np.mod(xm, ym))
  1283. assert_equal(test.mask, mask_or(mask_or(xm.mask, ym.mask), (ym == 0)))
  1284. def test_TakeTransposeInnerOuter(self):
  1285. # Test of take, transpose, inner, outer products
  1286. x = arange(24)
  1287. y = np.arange(24)
  1288. x[5:6] = masked
  1289. x = x.reshape(2, 3, 4)
  1290. y = y.reshape(2, 3, 4)
  1291. assert_equal(np.transpose(y, (2, 0, 1)), transpose(x, (2, 0, 1)))
  1292. assert_equal(np.take(y, (2, 0, 1), 1), take(x, (2, 0, 1), 1))
  1293. assert_equal(np.inner(filled(x, 0), filled(y, 0)),
  1294. inner(x, y))
  1295. assert_equal(np.outer(filled(x, 0), filled(y, 0)),
  1296. outer(x, y))
  1297. y = array(['abc', 1, 'def', 2, 3], object)
  1298. y[2] = masked
  1299. t = take(y, [0, 3, 4])
  1300. assert_(t[0] == 'abc')
  1301. assert_(t[1] == 2)
  1302. assert_(t[2] == 3)
  1303. def test_imag_real(self):
  1304. # Check complex
  1305. xx = array([1 + 10j, 20 + 2j], mask=[1, 0])
  1306. assert_equal(xx.imag, [10, 2])
  1307. assert_equal(xx.imag.filled(), [1e+20, 2])
  1308. assert_equal(xx.imag.dtype, xx._data.imag.dtype)
  1309. assert_equal(xx.real, [1, 20])
  1310. assert_equal(xx.real.filled(), [1e+20, 20])
  1311. assert_equal(xx.real.dtype, xx._data.real.dtype)
  1312. def test_methods_with_output(self):
  1313. xm = array(np.random.uniform(0, 10, 12)).reshape(3, 4)
  1314. xm[:, 0] = xm[0] = xm[-1, -1] = masked
  1315. funclist = ('sum', 'prod', 'var', 'std', 'max', 'min', 'ptp', 'mean',)
  1316. for funcname in funclist:
  1317. npfunc = getattr(np, funcname)
  1318. xmmeth = getattr(xm, funcname)
  1319. # A ndarray as explicit input
  1320. output = np.empty(4, dtype=float)
  1321. output.fill(-9999)
  1322. result = npfunc(xm, axis=0, out=output)
  1323. # ... the result should be the given output
  1324. assert_(result is output)
  1325. assert_equal(result, xmmeth(axis=0, out=output))
  1326. output = empty(4, dtype=int)
  1327. result = xmmeth(axis=0, out=output)
  1328. assert_(result is output)
  1329. assert_(output[0] is masked)
  1330. def test_eq_on_structured(self):
  1331. # Test the equality of structured arrays
  1332. ndtype = [('A', int), ('B', int)]
  1333. a = array([(1, 1), (2, 2)], mask=[(0, 1), (0, 0)], dtype=ndtype)
  1334. test = (a == a)
  1335. assert_equal(test.data, [True, True])
  1336. assert_equal(test.mask, [False, False])
  1337. assert_(test.fill_value == True)
  1338. test = (a == a[0])
  1339. assert_equal(test.data, [True, False])
  1340. assert_equal(test.mask, [False, False])
  1341. assert_(test.fill_value == True)
  1342. b = array([(1, 1), (2, 2)], mask=[(1, 0), (0, 0)], dtype=ndtype)
  1343. test = (a == b)
  1344. assert_equal(test.data, [False, True])
  1345. assert_equal(test.mask, [True, False])
  1346. assert_(test.fill_value == True)
  1347. test = (a[0] == b)
  1348. assert_equal(test.data, [False, False])
  1349. assert_equal(test.mask, [True, False])
  1350. assert_(test.fill_value == True)
  1351. b = array([(1, 1), (2, 2)], mask=[(0, 1), (1, 0)], dtype=ndtype)
  1352. test = (a == b)
  1353. assert_equal(test.data, [True, True])
  1354. assert_equal(test.mask, [False, False])
  1355. assert_(test.fill_value == True)
  1356. # complicated dtype, 2-dimensional array.
  1357. ndtype = [('A', int), ('B', [('BA', int), ('BB', int)])]
  1358. a = array([[(1, (1, 1)), (2, (2, 2))],
  1359. [(3, (3, 3)), (4, (4, 4))]],
  1360. mask=[[(0, (1, 0)), (0, (0, 1))],
  1361. [(1, (0, 0)), (1, (1, 1))]], dtype=ndtype)
  1362. test = (a[0, 0] == a)
  1363. assert_equal(test.data, [[True, False], [False, False]])
  1364. assert_equal(test.mask, [[False, False], [False, True]])
  1365. assert_(test.fill_value == True)
  1366. def test_ne_on_structured(self):
  1367. # Test the equality of structured arrays
  1368. ndtype = [('A', int), ('B', int)]
  1369. a = array([(1, 1), (2, 2)], mask=[(0, 1), (0, 0)], dtype=ndtype)
  1370. test = (a != a)
  1371. assert_equal(test.data, [False, False])
  1372. assert_equal(test.mask, [False, False])
  1373. assert_(test.fill_value == True)
  1374. test = (a != a[0])
  1375. assert_equal(test.data, [False, True])
  1376. assert_equal(test.mask, [False, False])
  1377. assert_(test.fill_value == True)
  1378. b = array([(1, 1), (2, 2)], mask=[(1, 0), (0, 0)], dtype=ndtype)
  1379. test = (a != b)
  1380. assert_equal(test.data, [True, False])
  1381. assert_equal(test.mask, [True, False])
  1382. assert_(test.fill_value == True)
  1383. test = (a[0] != b)
  1384. assert_equal(test.data, [True, True])
  1385. assert_equal(test.mask, [True, False])
  1386. assert_(test.fill_value == True)
  1387. b = array([(1, 1), (2, 2)], mask=[(0, 1), (1, 0)], dtype=ndtype)
  1388. test = (a != b)
  1389. assert_equal(test.data, [False, False])
  1390. assert_equal(test.mask, [False, False])
  1391. assert_(test.fill_value == True)
  1392. # complicated dtype, 2-dimensional array.
  1393. ndtype = [('A', int), ('B', [('BA', int), ('BB', int)])]
  1394. a = array([[(1, (1, 1)), (2, (2, 2))],
  1395. [(3, (3, 3)), (4, (4, 4))]],
  1396. mask=[[(0, (1, 0)), (0, (0, 1))],
  1397. [(1, (0, 0)), (1, (1, 1))]], dtype=ndtype)
  1398. test = (a[0, 0] != a)
  1399. assert_equal(test.data, [[False, True], [True, True]])
  1400. assert_equal(test.mask, [[False, False], [False, True]])
  1401. assert_(test.fill_value == True)
  1402. def test_eq_ne_structured_extra(self):
  1403. # ensure simple examples are symmetric and make sense.
  1404. # from https://github.com/numpy/numpy/pull/8590#discussion_r101126465
  1405. dt = np.dtype('i4,i4')
  1406. for m1 in (mvoid((1, 2), mask=(0, 0), dtype=dt),
  1407. mvoid((1, 2), mask=(0, 1), dtype=dt),
  1408. mvoid((1, 2), mask=(1, 0), dtype=dt),
  1409. mvoid((1, 2), mask=(1, 1), dtype=dt)):
  1410. ma1 = m1.view(MaskedArray)
  1411. r1 = ma1.view('2i4')
  1412. for m2 in (np.array((1, 1), dtype=dt),
  1413. mvoid((1, 1), dtype=dt),
  1414. mvoid((1, 0), mask=(0, 1), dtype=dt),
  1415. mvoid((3, 2), mask=(0, 1), dtype=dt)):
  1416. ma2 = m2.view(MaskedArray)
  1417. r2 = ma2.view('2i4')
  1418. eq_expected = (r1 == r2).all()
  1419. assert_equal(m1 == m2, eq_expected)
  1420. assert_equal(m2 == m1, eq_expected)
  1421. assert_equal(ma1 == m2, eq_expected)
  1422. assert_equal(m1 == ma2, eq_expected)
  1423. assert_equal(ma1 == ma2, eq_expected)
  1424. # Also check it is the same if we do it element by element.
  1425. el_by_el = [m1[name] == m2[name] for name in dt.names]
  1426. assert_equal(array(el_by_el, dtype=bool).all(), eq_expected)
  1427. ne_expected = (r1 != r2).any()
  1428. assert_equal(m1 != m2, ne_expected)
  1429. assert_equal(m2 != m1, ne_expected)
  1430. assert_equal(ma1 != m2, ne_expected)
  1431. assert_equal(m1 != ma2, ne_expected)
  1432. assert_equal(ma1 != ma2, ne_expected)
  1433. el_by_el = [m1[name] != m2[name] for name in dt.names]
  1434. assert_equal(array(el_by_el, dtype=bool).any(), ne_expected)
  1435. @pytest.mark.parametrize('dt', ['S', 'U'])
  1436. @pytest.mark.parametrize('fill', [None, 'A'])
  1437. def test_eq_for_strings(self, dt, fill):
  1438. # Test the equality of structured arrays
  1439. a = array(['a', 'b'], dtype=dt, mask=[0, 1], fill_value=fill)
  1440. test = (a == a)
  1441. assert_equal(test.data, [True, True])
  1442. assert_equal(test.mask, [False, True])
  1443. assert_(test.fill_value == True)
  1444. test = (a == a[0])
  1445. assert_equal(test.data, [True, False])
  1446. assert_equal(test.mask, [False, True])
  1447. assert_(test.fill_value == True)
  1448. b = array(['a', 'b'], dtype=dt, mask=[1, 0], fill_value=fill)
  1449. test = (a == b)
  1450. assert_equal(test.data, [False, False])
  1451. assert_equal(test.mask, [True, True])
  1452. assert_(test.fill_value == True)
  1453. test = (a[0] == b)
  1454. assert_equal(test.data, [False, False])
  1455. assert_equal(test.mask, [True, False])
  1456. assert_(test.fill_value == True)
  1457. test = (b == a[0])
  1458. assert_equal(test.data, [False, False])
  1459. assert_equal(test.mask, [True, False])
  1460. assert_(test.fill_value == True)
  1461. @pytest.mark.parametrize('dt', ['S', 'U'])
  1462. @pytest.mark.parametrize('fill', [None, 'A'])
  1463. def test_ne_for_strings(self, dt, fill):
  1464. # Test the equality of structured arrays
  1465. a = array(['a', 'b'], dtype=dt, mask=[0, 1], fill_value=fill)
  1466. test = (a != a)
  1467. assert_equal(test.data, [False, False])
  1468. assert_equal(test.mask, [False, True])
  1469. assert_(test.fill_value == True)
  1470. test = (a != a[0])
  1471. assert_equal(test.data, [False, True])
  1472. assert_equal(test.mask, [False, True])
  1473. assert_(test.fill_value == True)
  1474. b = array(['a', 'b'], dtype=dt, mask=[1, 0], fill_value=fill)
  1475. test = (a != b)
  1476. assert_equal(test.data, [True, True])
  1477. assert_equal(test.mask, [True, True])
  1478. assert_(test.fill_value == True)
  1479. test = (a[0] != b)
  1480. assert_equal(test.data, [True, True])
  1481. assert_equal(test.mask, [True, False])
  1482. assert_(test.fill_value == True)
  1483. test = (b != a[0])
  1484. assert_equal(test.data, [True, True])
  1485. assert_equal(test.mask, [True, False])
  1486. assert_(test.fill_value == True)
  1487. @pytest.mark.parametrize('dt1', num_dts, ids=num_ids)
  1488. @pytest.mark.parametrize('dt2', num_dts, ids=num_ids)
  1489. @pytest.mark.parametrize('fill', [None, 1])
  1490. def test_eq_for_numeric(self, dt1, dt2, fill):
  1491. # Test the equality of structured arrays
  1492. a = array([0, 1], dtype=dt1, mask=[0, 1], fill_value=fill)
  1493. test = (a == a)
  1494. assert_equal(test.data, [True, True])
  1495. assert_equal(test.mask, [False, True])
  1496. assert_(test.fill_value == True)
  1497. test = (a == a[0])
  1498. assert_equal(test.data, [True, False])
  1499. assert_equal(test.mask, [False, True])
  1500. assert_(test.fill_value == True)
  1501. b = array([0, 1], dtype=dt2, mask=[1, 0], fill_value=fill)
  1502. test = (a == b)
  1503. assert_equal(test.data, [False, False])
  1504. assert_equal(test.mask, [True, True])
  1505. assert_(test.fill_value == True)
  1506. test = (a[0] == b)
  1507. assert_equal(test.data, [False, False])
  1508. assert_equal(test.mask, [True, False])
  1509. assert_(test.fill_value == True)
  1510. test = (b == a[0])
  1511. assert_equal(test.data, [False, False])
  1512. assert_equal(test.mask, [True, False])
  1513. assert_(test.fill_value == True)
  1514. @pytest.mark.parametrize('dt1', num_dts, ids=num_ids)
  1515. @pytest.mark.parametrize('dt2', num_dts, ids=num_ids)
  1516. @pytest.mark.parametrize('fill', [None, 1])
  1517. def test_ne_for_numeric(self, dt1, dt2, fill):
  1518. # Test the equality of structured arrays
  1519. a = array([0, 1], dtype=dt1, mask=[0, 1], fill_value=fill)
  1520. test = (a != a)
  1521. assert_equal(test.data, [False, False])
  1522. assert_equal(test.mask, [False, True])
  1523. assert_(test.fill_value == True)
  1524. test = (a != a[0])
  1525. assert_equal(test.data, [False, True])
  1526. assert_equal(test.mask, [False, True])
  1527. assert_(test.fill_value == True)
  1528. b = array([0, 1], dtype=dt2, mask=[1, 0], fill_value=fill)
  1529. test = (a != b)
  1530. assert_equal(test.data, [True, True])
  1531. assert_equal(test.mask, [True, True])
  1532. assert_(test.fill_value == True)
  1533. test = (a[0] != b)
  1534. assert_equal(test.data, [True, True])
  1535. assert_equal(test.mask, [True, False])
  1536. assert_(test.fill_value == True)
  1537. test = (b != a[0])
  1538. assert_equal(test.data, [True, True])
  1539. assert_equal(test.mask, [True, False])
  1540. assert_(test.fill_value == True)
  1541. def test_eq_with_None(self):
  1542. # Really, comparisons with None should not be done, but check them
  1543. # anyway. Note that pep8 will flag these tests.
  1544. # Deprecation is in place for arrays, and when it happens this
  1545. # test will fail (and have to be changed accordingly).
  1546. # With partial mask
  1547. with suppress_warnings() as sup:
  1548. sup.filter(FutureWarning, "Comparison to `None`")
  1549. a = array([None, 1], mask=[0, 1])
  1550. assert_equal(a == None, array([True, False], mask=[0, 1]))
  1551. assert_equal(a.data == None, [True, False])
  1552. assert_equal(a != None, array([False, True], mask=[0, 1]))
  1553. # With nomask
  1554. a = array([None, 1], mask=False)
  1555. assert_equal(a == None, [True, False])
  1556. assert_equal(a != None, [False, True])
  1557. # With complete mask
  1558. a = array([None, 2], mask=True)
  1559. assert_equal(a == None, array([False, True], mask=True))
  1560. assert_equal(a != None, array([True, False], mask=True))
  1561. # Fully masked, even comparison to None should return "masked"
  1562. a = masked
  1563. assert_equal(a == None, masked)
  1564. def test_eq_with_scalar(self):
  1565. a = array(1)
  1566. assert_equal(a == 1, True)
  1567. assert_equal(a == 0, False)
  1568. assert_equal(a != 1, False)
  1569. assert_equal(a != 0, True)
  1570. b = array(1, mask=True)
  1571. assert_equal(b == 0, masked)
  1572. assert_equal(b == 1, masked)
  1573. assert_equal(b != 0, masked)
  1574. assert_equal(b != 1, masked)
  1575. def test_eq_different_dimensions(self):
  1576. m1 = array([1, 1], mask=[0, 1])
  1577. # test comparison with both masked and regular arrays.
  1578. for m2 in (array([[0, 1], [1, 2]]),
  1579. np.array([[0, 1], [1, 2]])):
  1580. test = (m1 == m2)
  1581. assert_equal(test.data, [[False, False],
  1582. [True, False]])
  1583. assert_equal(test.mask, [[False, True],
  1584. [False, True]])
  1585. def test_numpyarithmetics(self):
  1586. # Check that the mask is not back-propagated when using numpy functions
  1587. a = masked_array([-1, 0, 1, 2, 3], mask=[0, 0, 0, 0, 1])
  1588. control = masked_array([np.nan, np.nan, 0, np.log(2), -1],
  1589. mask=[1, 1, 0, 0, 1])
  1590. test = log(a)
  1591. assert_equal(test, control)
  1592. assert_equal(test.mask, control.mask)
  1593. assert_equal(a.mask, [0, 0, 0, 0, 1])
  1594. test = np.log(a)
  1595. assert_equal(test, control)
  1596. assert_equal(test.mask, control.mask)
  1597. assert_equal(a.mask, [0, 0, 0, 0, 1])
  1598. class TestMaskedArrayAttributes:
  1599. def test_keepmask(self):
  1600. # Tests the keep mask flag
  1601. x = masked_array([1, 2, 3], mask=[1, 0, 0])
  1602. mx = masked_array(x)
  1603. assert_equal(mx.mask, x.mask)
  1604. mx = masked_array(x, mask=[0, 1, 0], keep_mask=False)
  1605. assert_equal(mx.mask, [0, 1, 0])
  1606. mx = masked_array(x, mask=[0, 1, 0], keep_mask=True)
  1607. assert_equal(mx.mask, [1, 1, 0])
  1608. # We default to true
  1609. mx = masked_array(x, mask=[0, 1, 0])
  1610. assert_equal(mx.mask, [1, 1, 0])
  1611. def test_hardmask(self):
  1612. # Test hard_mask
  1613. d = arange(5)
  1614. n = [0, 0, 0, 1, 1]
  1615. m = make_mask(n)
  1616. xh = array(d, mask=m, hard_mask=True)
  1617. # We need to copy, to avoid updating d in xh !
  1618. xs = array(d, mask=m, hard_mask=False, copy=True)
  1619. xh[[1, 4]] = [10, 40]
  1620. xs[[1, 4]] = [10, 40]
  1621. assert_equal(xh._data, [0, 10, 2, 3, 4])
  1622. assert_equal(xs._data, [0, 10, 2, 3, 40])
  1623. assert_equal(xs.mask, [0, 0, 0, 1, 0])
  1624. assert_(xh._hardmask)
  1625. assert_(not xs._hardmask)
  1626. xh[1:4] = [10, 20, 30]
  1627. xs[1:4] = [10, 20, 30]
  1628. assert_equal(xh._data, [0, 10, 20, 3, 4])
  1629. assert_equal(xs._data, [0, 10, 20, 30, 40])
  1630. assert_equal(xs.mask, nomask)
  1631. xh[0] = masked
  1632. xs[0] = masked
  1633. assert_equal(xh.mask, [1, 0, 0, 1, 1])
  1634. assert_equal(xs.mask, [1, 0, 0, 0, 0])
  1635. xh[:] = 1
  1636. xs[:] = 1
  1637. assert_equal(xh._data, [0, 1, 1, 3, 4])
  1638. assert_equal(xs._data, [1, 1, 1, 1, 1])
  1639. assert_equal(xh.mask, [1, 0, 0, 1, 1])
  1640. assert_equal(xs.mask, nomask)
  1641. # Switch to soft mask
  1642. xh.soften_mask()
  1643. xh[:] = arange(5)
  1644. assert_equal(xh._data, [0, 1, 2, 3, 4])
  1645. assert_equal(xh.mask, nomask)
  1646. # Switch back to hard mask
  1647. xh.harden_mask()
  1648. xh[xh < 3] = masked
  1649. assert_equal(xh._data, [0, 1, 2, 3, 4])
  1650. assert_equal(xh._mask, [1, 1, 1, 0, 0])
  1651. xh[filled(xh > 1, False)] = 5
  1652. assert_equal(xh._data, [0, 1, 2, 5, 5])
  1653. assert_equal(xh._mask, [1, 1, 1, 0, 0])
  1654. xh = array([[1, 2], [3, 4]], mask=[[1, 0], [0, 0]], hard_mask=True)
  1655. xh[0] = 0
  1656. assert_equal(xh._data, [[1, 0], [3, 4]])
  1657. assert_equal(xh._mask, [[1, 0], [0, 0]])
  1658. xh[-1, -1] = 5
  1659. assert_equal(xh._data, [[1, 0], [3, 5]])
  1660. assert_equal(xh._mask, [[1, 0], [0, 0]])
  1661. xh[filled(xh < 5, False)] = 2
  1662. assert_equal(xh._data, [[1, 2], [2, 5]])
  1663. assert_equal(xh._mask, [[1, 0], [0, 0]])
  1664. def test_hardmask_again(self):
  1665. # Another test of hardmask
  1666. d = arange(5)
  1667. n = [0, 0, 0, 1, 1]
  1668. m = make_mask(n)
  1669. xh = array(d, mask=m, hard_mask=True)
  1670. xh[4:5] = 999
  1671. xh[0:1] = 999
  1672. assert_equal(xh._data, [999, 1, 2, 3, 4])
  1673. def test_hardmask_oncemore_yay(self):
  1674. # OK, yet another test of hardmask
  1675. # Make sure that harden_mask/soften_mask//unshare_mask returns self
  1676. a = array([1, 2, 3], mask=[1, 0, 0])
  1677. b = a.harden_mask()
  1678. assert_equal(a, b)
  1679. b[0] = 0
  1680. assert_equal(a, b)
  1681. assert_equal(b, array([1, 2, 3], mask=[1, 0, 0]))
  1682. a = b.soften_mask()
  1683. a[0] = 0
  1684. assert_equal(a, b)
  1685. assert_equal(b, array([0, 2, 3], mask=[0, 0, 0]))
  1686. def test_smallmask(self):
  1687. # Checks the behaviour of _smallmask
  1688. a = arange(10)
  1689. a[1] = masked
  1690. a[1] = 1
  1691. assert_equal(a._mask, nomask)
  1692. a = arange(10)
  1693. a._smallmask = False
  1694. a[1] = masked
  1695. a[1] = 1
  1696. assert_equal(a._mask, zeros(10))
  1697. def test_shrink_mask(self):
  1698. # Tests .shrink_mask()
  1699. a = array([1, 2, 3], mask=[0, 0, 0])
  1700. b = a.shrink_mask()
  1701. assert_equal(a, b)
  1702. assert_equal(a.mask, nomask)
  1703. # Mask cannot be shrunk on structured types, so is a no-op
  1704. a = np.ma.array([(1, 2.0)], [('a', int), ('b', float)])
  1705. b = a.copy()
  1706. a.shrink_mask()
  1707. assert_equal(a.mask, b.mask)
  1708. def test_flat(self):
  1709. # Test that flat can return all types of items [#4585, #4615]
  1710. # test 2-D record array
  1711. # ... on structured array w/ masked records
  1712. x = array([[(1, 1.1, 'one'), (2, 2.2, 'two'), (3, 3.3, 'thr')],
  1713. [(4, 4.4, 'fou'), (5, 5.5, 'fiv'), (6, 6.6, 'six')]],
  1714. dtype=[('a', int), ('b', float), ('c', '|S8')])
  1715. x['a'][0, 1] = masked
  1716. x['b'][1, 0] = masked
  1717. x['c'][0, 2] = masked
  1718. x[-1, -1] = masked
  1719. xflat = x.flat
  1720. assert_equal(xflat[0], x[0, 0])
  1721. assert_equal(xflat[1], x[0, 1])
  1722. assert_equal(xflat[2], x[0, 2])
  1723. assert_equal(xflat[:3], x[0])
  1724. assert_equal(xflat[3], x[1, 0])
  1725. assert_equal(xflat[4], x[1, 1])
  1726. assert_equal(xflat[5], x[1, 2])
  1727. assert_equal(xflat[3:], x[1])
  1728. assert_equal(xflat[-1], x[-1, -1])
  1729. i = 0
  1730. j = 0
  1731. for xf in xflat:
  1732. assert_equal(xf, x[j, i])
  1733. i += 1
  1734. if i >= x.shape[-1]:
  1735. i = 0
  1736. j += 1
  1737. def test_assign_dtype(self):
  1738. # check that the mask's dtype is updated when dtype is changed
  1739. a = np.zeros(4, dtype='f4,i4')
  1740. m = np.ma.array(a)
  1741. m.dtype = np.dtype('f4')
  1742. repr(m) # raises?
  1743. assert_equal(m.dtype, np.dtype('f4'))
  1744. # check that dtype changes that change shape of mask too much
  1745. # are not allowed
  1746. def assign():
  1747. m = np.ma.array(a)
  1748. m.dtype = np.dtype('f8')
  1749. assert_raises(ValueError, assign)
  1750. b = a.view(dtype='f4', type=np.ma.MaskedArray) # raises?
  1751. assert_equal(b.dtype, np.dtype('f4'))
  1752. # check that nomask is preserved
  1753. a = np.zeros(4, dtype='f4')
  1754. m = np.ma.array(a)
  1755. m.dtype = np.dtype('f4,i4')
  1756. assert_equal(m.dtype, np.dtype('f4,i4'))
  1757. assert_equal(m._mask, np.ma.nomask)
  1758. class TestFillingValues:
  1759. def test_check_on_scalar(self):
  1760. # Test _check_fill_value set to valid and invalid values
  1761. _check_fill_value = np.ma.core._check_fill_value
  1762. fval = _check_fill_value(0, int)
  1763. assert_equal(fval, 0)
  1764. fval = _check_fill_value(None, int)
  1765. assert_equal(fval, default_fill_value(0))
  1766. fval = _check_fill_value(0, "|S3")
  1767. assert_equal(fval, b"0")
  1768. fval = _check_fill_value(None, "|S3")
  1769. assert_equal(fval, default_fill_value(b"camelot!"))
  1770. assert_raises(TypeError, _check_fill_value, 1e+20, int)
  1771. assert_raises(TypeError, _check_fill_value, 'stuff', int)
  1772. def test_check_on_fields(self):
  1773. # Tests _check_fill_value with records
  1774. _check_fill_value = np.ma.core._check_fill_value
  1775. ndtype = [('a', int), ('b', float), ('c', "|S3")]
  1776. # A check on a list should return a single record
  1777. fval = _check_fill_value([-999, -12345678.9, "???"], ndtype)
  1778. assert_(isinstance(fval, ndarray))
  1779. assert_equal(fval.item(), [-999, -12345678.9, b"???"])
  1780. # A check on None should output the defaults
  1781. fval = _check_fill_value(None, ndtype)
  1782. assert_(isinstance(fval, ndarray))
  1783. assert_equal(fval.item(), [default_fill_value(0),
  1784. default_fill_value(0.),
  1785. asbytes(default_fill_value("0"))])
  1786. #.....Using a structured type as fill_value should work
  1787. fill_val = np.array((-999, -12345678.9, "???"), dtype=ndtype)
  1788. fval = _check_fill_value(fill_val, ndtype)
  1789. assert_(isinstance(fval, ndarray))
  1790. assert_equal(fval.item(), [-999, -12345678.9, b"???"])
  1791. #.....Using a flexible type w/ a different type shouldn't matter
  1792. # BEHAVIOR in 1.5 and earlier, and 1.13 and later: match structured
  1793. # types by position
  1794. fill_val = np.array((-999, -12345678.9, "???"),
  1795. dtype=[("A", int), ("B", float), ("C", "|S3")])
  1796. fval = _check_fill_value(fill_val, ndtype)
  1797. assert_(isinstance(fval, ndarray))
  1798. assert_equal(fval.item(), [-999, -12345678.9, b"???"])
  1799. #.....Using an object-array shouldn't matter either
  1800. fill_val = np.ndarray(shape=(1,), dtype=object)
  1801. fill_val[0] = (-999, -12345678.9, b"???")
  1802. fval = _check_fill_value(fill_val, object)
  1803. assert_(isinstance(fval, ndarray))
  1804. assert_equal(fval.item(), [-999, -12345678.9, b"???"])
  1805. # NOTE: This test was never run properly as "fill_value" rather than
  1806. # "fill_val" was assigned. Written properly, it fails.
  1807. #fill_val = np.array((-999, -12345678.9, "???"))
  1808. #fval = _check_fill_value(fill_val, ndtype)
  1809. #assert_(isinstance(fval, ndarray))
  1810. #assert_equal(fval.item(), [-999, -12345678.9, b"???"])
  1811. #.....One-field-only flexible type should work as well
  1812. ndtype = [("a", int)]
  1813. fval = _check_fill_value(-999999999, ndtype)
  1814. assert_(isinstance(fval, ndarray))
  1815. assert_equal(fval.item(), (-999999999,))
  1816. def test_fillvalue_conversion(self):
  1817. # Tests the behavior of fill_value during conversion
  1818. # We had a tailored comment to make sure special attributes are
  1819. # properly dealt with
  1820. a = array([b'3', b'4', b'5'])
  1821. a._optinfo.update({'comment':"updated!"})
  1822. b = array(a, dtype=int)
  1823. assert_equal(b._data, [3, 4, 5])
  1824. assert_equal(b.fill_value, default_fill_value(0))
  1825. b = array(a, dtype=float)
  1826. assert_equal(b._data, [3, 4, 5])
  1827. assert_equal(b.fill_value, default_fill_value(0.))
  1828. b = a.astype(int)
  1829. assert_equal(b._data, [3, 4, 5])
  1830. assert_equal(b.fill_value, default_fill_value(0))
  1831. assert_equal(b._optinfo['comment'], "updated!")
  1832. b = a.astype([('a', '|S3')])
  1833. assert_equal(b['a']._data, a._data)
  1834. assert_equal(b['a'].fill_value, a.fill_value)
  1835. def test_default_fill_value(self):
  1836. # check all calling conventions
  1837. f1 = default_fill_value(1.)
  1838. f2 = default_fill_value(np.array(1.))
  1839. f3 = default_fill_value(np.array(1.).dtype)
  1840. assert_equal(f1, f2)
  1841. assert_equal(f1, f3)
  1842. def test_default_fill_value_structured(self):
  1843. fields = array([(1, 1, 1)],
  1844. dtype=[('i', int), ('s', '|S8'), ('f', float)])
  1845. f1 = default_fill_value(fields)
  1846. f2 = default_fill_value(fields.dtype)
  1847. expected = np.array((default_fill_value(0),
  1848. default_fill_value('0'),
  1849. default_fill_value(0.)), dtype=fields.dtype)
  1850. assert_equal(f1, expected)
  1851. assert_equal(f2, expected)
  1852. def test_default_fill_value_void(self):
  1853. dt = np.dtype([('v', 'V7')])
  1854. f = default_fill_value(dt)
  1855. assert_equal(f['v'], np.array(default_fill_value(dt['v']), dt['v']))
  1856. def test_fillvalue(self):
  1857. # Yet more fun with the fill_value
  1858. data = masked_array([1, 2, 3], fill_value=-999)
  1859. series = data[[0, 2, 1]]
  1860. assert_equal(series._fill_value, data._fill_value)
  1861. mtype = [('f', float), ('s', '|S3')]
  1862. x = array([(1, 'a'), (2, 'b'), (pi, 'pi')], dtype=mtype)
  1863. x.fill_value = 999
  1864. assert_equal(x.fill_value.item(), [999., b'999'])
  1865. assert_equal(x['f'].fill_value, 999)
  1866. assert_equal(x['s'].fill_value, b'999')
  1867. x.fill_value = (9, '???')
  1868. assert_equal(x.fill_value.item(), (9, b'???'))
  1869. assert_equal(x['f'].fill_value, 9)
  1870. assert_equal(x['s'].fill_value, b'???')
  1871. x = array([1, 2, 3.1])
  1872. x.fill_value = 999
  1873. assert_equal(np.asarray(x.fill_value).dtype, float)
  1874. assert_equal(x.fill_value, 999.)
  1875. assert_equal(x._fill_value, np.array(999.))
  1876. def test_subarray_fillvalue(self):
  1877. # gh-10483 test multi-field index fill value
  1878. fields = array([(1, 1, 1)],
  1879. dtype=[('i', int), ('s', '|S8'), ('f', float)])
  1880. with suppress_warnings() as sup:
  1881. sup.filter(FutureWarning, "Numpy has detected")
  1882. subfields = fields[['i', 'f']]
  1883. assert_equal(tuple(subfields.fill_value), (999999, 1.e+20))
  1884. # test comparison does not raise:
  1885. subfields[1:] == subfields[:-1]
  1886. def test_fillvalue_exotic_dtype(self):
  1887. # Tests yet more exotic flexible dtypes
  1888. _check_fill_value = np.ma.core._check_fill_value
  1889. ndtype = [('i', int), ('s', '|S8'), ('f', float)]
  1890. control = np.array((default_fill_value(0),
  1891. default_fill_value('0'),
  1892. default_fill_value(0.),),
  1893. dtype=ndtype)
  1894. assert_equal(_check_fill_value(None, ndtype), control)
  1895. # The shape shouldn't matter
  1896. ndtype = [('f0', float, (2, 2))]
  1897. control = np.array((default_fill_value(0.),),
  1898. dtype=[('f0', float)]).astype(ndtype)
  1899. assert_equal(_check_fill_value(None, ndtype), control)
  1900. control = np.array((0,), dtype=[('f0', float)]).astype(ndtype)
  1901. assert_equal(_check_fill_value(0, ndtype), control)
  1902. ndtype = np.dtype("int, (2,3)float, float")
  1903. control = np.array((default_fill_value(0),
  1904. default_fill_value(0.),
  1905. default_fill_value(0.),),
  1906. dtype="int, float, float").astype(ndtype)
  1907. test = _check_fill_value(None, ndtype)
  1908. assert_equal(test, control)
  1909. control = np.array((0, 0, 0), dtype="int, float, float").astype(ndtype)
  1910. assert_equal(_check_fill_value(0, ndtype), control)
  1911. # but when indexing, fill value should become scalar not tuple
  1912. # See issue #6723
  1913. M = masked_array(control)
  1914. assert_equal(M["f1"].fill_value.ndim, 0)
  1915. def test_fillvalue_datetime_timedelta(self):
  1916. # Test default fillvalue for datetime64 and timedelta64 types.
  1917. # See issue #4476, this would return '?' which would cause errors
  1918. # elsewhere
  1919. for timecode in ("as", "fs", "ps", "ns", "us", "ms", "s", "m",
  1920. "h", "D", "W", "M", "Y"):
  1921. control = numpy.datetime64("NaT", timecode)
  1922. test = default_fill_value(numpy.dtype("<M8[" + timecode + "]"))
  1923. np.testing.assert_equal(test, control)
  1924. control = numpy.timedelta64("NaT", timecode)
  1925. test = default_fill_value(numpy.dtype("<m8[" + timecode + "]"))
  1926. np.testing.assert_equal(test, control)
  1927. def test_extremum_fill_value(self):
  1928. # Tests extremum fill values for flexible type.
  1929. a = array([(1, (2, 3)), (4, (5, 6))],
  1930. dtype=[('A', int), ('B', [('BA', int), ('BB', int)])])
  1931. test = a.fill_value
  1932. assert_equal(test.dtype, a.dtype)
  1933. assert_equal(test['A'], default_fill_value(a['A']))
  1934. assert_equal(test['B']['BA'], default_fill_value(a['B']['BA']))
  1935. assert_equal(test['B']['BB'], default_fill_value(a['B']['BB']))
  1936. test = minimum_fill_value(a)
  1937. assert_equal(test.dtype, a.dtype)
  1938. assert_equal(test[0], minimum_fill_value(a['A']))
  1939. assert_equal(test[1][0], minimum_fill_value(a['B']['BA']))
  1940. assert_equal(test[1][1], minimum_fill_value(a['B']['BB']))
  1941. assert_equal(test[1], minimum_fill_value(a['B']))
  1942. test = maximum_fill_value(a)
  1943. assert_equal(test.dtype, a.dtype)
  1944. assert_equal(test[0], maximum_fill_value(a['A']))
  1945. assert_equal(test[1][0], maximum_fill_value(a['B']['BA']))
  1946. assert_equal(test[1][1], maximum_fill_value(a['B']['BB']))
  1947. assert_equal(test[1], maximum_fill_value(a['B']))
  1948. def test_extremum_fill_value_subdtype(self):
  1949. a = array(([2, 3, 4],), dtype=[('value', np.int8, 3)])
  1950. test = minimum_fill_value(a)
  1951. assert_equal(test.dtype, a.dtype)
  1952. assert_equal(test[0], np.full(3, minimum_fill_value(a['value'])))
  1953. test = maximum_fill_value(a)
  1954. assert_equal(test.dtype, a.dtype)
  1955. assert_equal(test[0], np.full(3, maximum_fill_value(a['value'])))
  1956. def test_fillvalue_individual_fields(self):
  1957. # Test setting fill_value on individual fields
  1958. ndtype = [('a', int), ('b', int)]
  1959. # Explicit fill_value
  1960. a = array(list(zip([1, 2, 3], [4, 5, 6])),
  1961. fill_value=(-999, -999), dtype=ndtype)
  1962. aa = a['a']
  1963. aa.set_fill_value(10)
  1964. assert_equal(aa._fill_value, np.array(10))
  1965. assert_equal(tuple(a.fill_value), (10, -999))
  1966. a.fill_value['b'] = -10
  1967. assert_equal(tuple(a.fill_value), (10, -10))
  1968. # Implicit fill_value
  1969. t = array(list(zip([1, 2, 3], [4, 5, 6])), dtype=ndtype)
  1970. tt = t['a']
  1971. tt.set_fill_value(10)
  1972. assert_equal(tt._fill_value, np.array(10))
  1973. assert_equal(tuple(t.fill_value), (10, default_fill_value(0)))
  1974. def test_fillvalue_implicit_structured_array(self):
  1975. # Check that fill_value is always defined for structured arrays
  1976. ndtype = ('b', float)
  1977. adtype = ('a', float)
  1978. a = array([(1.,), (2.,)], mask=[(False,), (False,)],
  1979. fill_value=(np.nan,), dtype=np.dtype([adtype]))
  1980. b = empty(a.shape, dtype=[adtype, ndtype])
  1981. b['a'] = a['a']
  1982. b['a'].set_fill_value(a['a'].fill_value)
  1983. f = b._fill_value[()]
  1984. assert_(np.isnan(f[0]))
  1985. assert_equal(f[-1], default_fill_value(1.))
  1986. def test_fillvalue_as_arguments(self):
  1987. # Test adding a fill_value parameter to empty/ones/zeros
  1988. a = empty(3, fill_value=999.)
  1989. assert_equal(a.fill_value, 999.)
  1990. a = ones(3, fill_value=999., dtype=float)
  1991. assert_equal(a.fill_value, 999.)
  1992. a = zeros(3, fill_value=0., dtype=complex)
  1993. assert_equal(a.fill_value, 0.)
  1994. a = identity(3, fill_value=0., dtype=complex)
  1995. assert_equal(a.fill_value, 0.)
  1996. def test_shape_argument(self):
  1997. # Test that shape can be provides as an argument
  1998. # GH issue 6106
  1999. a = empty(shape=(3, ))
  2000. assert_equal(a.shape, (3, ))
  2001. a = ones(shape=(3, ), dtype=float)
  2002. assert_equal(a.shape, (3, ))
  2003. a = zeros(shape=(3, ), dtype=complex)
  2004. assert_equal(a.shape, (3, ))
  2005. def test_fillvalue_in_view(self):
  2006. # Test the behavior of fill_value in view
  2007. # Create initial masked array
  2008. x = array([1, 2, 3], fill_value=1, dtype=np.int64)
  2009. # Check that fill_value is preserved by default
  2010. y = x.view()
  2011. assert_(y.fill_value == 1)
  2012. # Check that fill_value is preserved if dtype is specified and the
  2013. # dtype is an ndarray sub-class and has a _fill_value attribute
  2014. y = x.view(MaskedArray)
  2015. assert_(y.fill_value == 1)
  2016. # Check that fill_value is preserved if type is specified and the
  2017. # dtype is an ndarray sub-class and has a _fill_value attribute (by
  2018. # default, the first argument is dtype, not type)
  2019. y = x.view(type=MaskedArray)
  2020. assert_(y.fill_value == 1)
  2021. # Check that code does not crash if passed an ndarray sub-class that
  2022. # does not have a _fill_value attribute
  2023. y = x.view(np.ndarray)
  2024. y = x.view(type=np.ndarray)
  2025. # Check that fill_value can be overridden with view
  2026. y = x.view(MaskedArray, fill_value=2)
  2027. assert_(y.fill_value == 2)
  2028. # Check that fill_value can be overridden with view (using type=)
  2029. y = x.view(type=MaskedArray, fill_value=2)
  2030. assert_(y.fill_value == 2)
  2031. # Check that fill_value gets reset if passed a dtype but not a
  2032. # fill_value. This is because even though in some cases one can safely
  2033. # cast the fill_value, e.g. if taking an int64 view of an int32 array,
  2034. # in other cases, this cannot be done (e.g. int32 view of an int64
  2035. # array with a large fill_value).
  2036. y = x.view(dtype=np.int32)
  2037. assert_(y.fill_value == 999999)
  2038. def test_fillvalue_bytes_or_str(self):
  2039. # Test whether fill values work as expected for structured dtypes
  2040. # containing bytes or str. See issue #7259.
  2041. a = empty(shape=(3, ), dtype="(2)3S,(2)3U")
  2042. assert_equal(a["f0"].fill_value, default_fill_value(b"spam"))
  2043. assert_equal(a["f1"].fill_value, default_fill_value("eggs"))
  2044. class TestUfuncs:
  2045. # Test class for the application of ufuncs on MaskedArrays.
  2046. def setup(self):
  2047. # Base data definition.
  2048. self.d = (array([1.0, 0, -1, pi / 2] * 2, mask=[0, 1] + [0] * 6),
  2049. array([1.0, 0, -1, pi / 2] * 2, mask=[1, 0] + [0] * 6),)
  2050. self.err_status = np.geterr()
  2051. np.seterr(divide='ignore', invalid='ignore')
  2052. def teardown(self):
  2053. np.seterr(**self.err_status)
  2054. def test_testUfuncRegression(self):
  2055. # Tests new ufuncs on MaskedArrays.
  2056. for f in ['sqrt', 'log', 'log10', 'exp', 'conjugate',
  2057. 'sin', 'cos', 'tan',
  2058. 'arcsin', 'arccos', 'arctan',
  2059. 'sinh', 'cosh', 'tanh',
  2060. 'arcsinh',
  2061. 'arccosh',
  2062. 'arctanh',
  2063. 'absolute', 'fabs', 'negative',
  2064. 'floor', 'ceil',
  2065. 'logical_not',
  2066. 'add', 'subtract', 'multiply',
  2067. 'divide', 'true_divide', 'floor_divide',
  2068. 'remainder', 'fmod', 'hypot', 'arctan2',
  2069. 'equal', 'not_equal', 'less_equal', 'greater_equal',
  2070. 'less', 'greater',
  2071. 'logical_and', 'logical_or', 'logical_xor',
  2072. ]:
  2073. try:
  2074. uf = getattr(umath, f)
  2075. except AttributeError:
  2076. uf = getattr(fromnumeric, f)
  2077. mf = getattr(numpy.ma.core, f)
  2078. args = self.d[:uf.nin]
  2079. ur = uf(*args)
  2080. mr = mf(*args)
  2081. assert_equal(ur.filled(0), mr.filled(0), f)
  2082. assert_mask_equal(ur.mask, mr.mask, err_msg=f)
  2083. def test_reduce(self):
  2084. # Tests reduce on MaskedArrays.
  2085. a = self.d[0]
  2086. assert_(not alltrue(a, axis=0))
  2087. assert_(sometrue(a, axis=0))
  2088. assert_equal(sum(a[:3], axis=0), 0)
  2089. assert_equal(product(a, axis=0), 0)
  2090. assert_equal(add.reduce(a), pi)
  2091. def test_minmax(self):
  2092. # Tests extrema on MaskedArrays.
  2093. a = arange(1, 13).reshape(3, 4)
  2094. amask = masked_where(a < 5, a)
  2095. assert_equal(amask.max(), a.max())
  2096. assert_equal(amask.min(), 5)
  2097. assert_equal(amask.max(0), a.max(0))
  2098. assert_equal(amask.min(0), [5, 6, 7, 8])
  2099. assert_(amask.max(1)[0].mask)
  2100. assert_(amask.min(1)[0].mask)
  2101. def test_ndarray_mask(self):
  2102. # Check that the mask of the result is a ndarray (not a MaskedArray...)
  2103. a = masked_array([-1, 0, 1, 2, 3], mask=[0, 0, 0, 0, 1])
  2104. test = np.sqrt(a)
  2105. control = masked_array([-1, 0, 1, np.sqrt(2), -1],
  2106. mask=[1, 0, 0, 0, 1])
  2107. assert_equal(test, control)
  2108. assert_equal(test.mask, control.mask)
  2109. assert_(not isinstance(test.mask, MaskedArray))
  2110. def test_treatment_of_NotImplemented(self):
  2111. # Check that NotImplemented is returned at appropriate places
  2112. a = masked_array([1., 2.], mask=[1, 0])
  2113. assert_raises(TypeError, operator.mul, a, "abc")
  2114. assert_raises(TypeError, operator.truediv, a, "abc")
  2115. class MyClass:
  2116. __array_priority__ = a.__array_priority__ + 1
  2117. def __mul__(self, other):
  2118. return "My mul"
  2119. def __rmul__(self, other):
  2120. return "My rmul"
  2121. me = MyClass()
  2122. assert_(me * a == "My mul")
  2123. assert_(a * me == "My rmul")
  2124. # and that __array_priority__ is respected
  2125. class MyClass2:
  2126. __array_priority__ = 100
  2127. def __mul__(self, other):
  2128. return "Me2mul"
  2129. def __rmul__(self, other):
  2130. return "Me2rmul"
  2131. def __rdiv__(self, other):
  2132. return "Me2rdiv"
  2133. __rtruediv__ = __rdiv__
  2134. me_too = MyClass2()
  2135. assert_(a.__mul__(me_too) is NotImplemented)
  2136. assert_(all(multiply.outer(a, me_too) == "Me2rmul"))
  2137. assert_(a.__truediv__(me_too) is NotImplemented)
  2138. assert_(me_too * a == "Me2mul")
  2139. assert_(a * me_too == "Me2rmul")
  2140. assert_(a / me_too == "Me2rdiv")
  2141. def test_no_masked_nan_warnings(self):
  2142. # check that a nan in masked position does not
  2143. # cause ufunc warnings
  2144. m = np.ma.array([0.5, np.nan], mask=[0,1])
  2145. with warnings.catch_warnings():
  2146. warnings.filterwarnings("error")
  2147. # test unary and binary ufuncs
  2148. exp(m)
  2149. add(m, 1)
  2150. m > 0
  2151. # test different unary domains
  2152. sqrt(m)
  2153. log(m)
  2154. tan(m)
  2155. arcsin(m)
  2156. arccos(m)
  2157. arccosh(m)
  2158. # test binary domains
  2159. divide(m, 2)
  2160. # also check that allclose uses ma ufuncs, to avoid warning
  2161. allclose(m, 0.5)
  2162. class TestMaskedArrayInPlaceArithmetics:
  2163. # Test MaskedArray Arithmetics
  2164. def setup(self):
  2165. x = arange(10)
  2166. y = arange(10)
  2167. xm = arange(10)
  2168. xm[2] = masked
  2169. self.intdata = (x, y, xm)
  2170. self.floatdata = (x.astype(float), y.astype(float), xm.astype(float))
  2171. self.othertypes = np.typecodes['AllInteger'] + np.typecodes['AllFloat']
  2172. self.othertypes = [np.dtype(_).type for _ in self.othertypes]
  2173. self.uint8data = (
  2174. x.astype(np.uint8),
  2175. y.astype(np.uint8),
  2176. xm.astype(np.uint8)
  2177. )
  2178. def test_inplace_addition_scalar(self):
  2179. # Test of inplace additions
  2180. (x, y, xm) = self.intdata
  2181. xm[2] = masked
  2182. x += 1
  2183. assert_equal(x, y + 1)
  2184. xm += 1
  2185. assert_equal(xm, y + 1)
  2186. (x, _, xm) = self.floatdata
  2187. id1 = x.data.ctypes.data
  2188. x += 1.
  2189. assert_(id1 == x.data.ctypes.data)
  2190. assert_equal(x, y + 1.)
  2191. def test_inplace_addition_array(self):
  2192. # Test of inplace additions
  2193. (x, y, xm) = self.intdata
  2194. m = xm.mask
  2195. a = arange(10, dtype=np.int16)
  2196. a[-1] = masked
  2197. x += a
  2198. xm += a
  2199. assert_equal(x, y + a)
  2200. assert_equal(xm, y + a)
  2201. assert_equal(xm.mask, mask_or(m, a.mask))
  2202. def test_inplace_subtraction_scalar(self):
  2203. # Test of inplace subtractions
  2204. (x, y, xm) = self.intdata
  2205. x -= 1
  2206. assert_equal(x, y - 1)
  2207. xm -= 1
  2208. assert_equal(xm, y - 1)
  2209. def test_inplace_subtraction_array(self):
  2210. # Test of inplace subtractions
  2211. (x, y, xm) = self.floatdata
  2212. m = xm.mask
  2213. a = arange(10, dtype=float)
  2214. a[-1] = masked
  2215. x -= a
  2216. xm -= a
  2217. assert_equal(x, y - a)
  2218. assert_equal(xm, y - a)
  2219. assert_equal(xm.mask, mask_or(m, a.mask))
  2220. def test_inplace_multiplication_scalar(self):
  2221. # Test of inplace multiplication
  2222. (x, y, xm) = self.floatdata
  2223. x *= 2.0
  2224. assert_equal(x, y * 2)
  2225. xm *= 2.0
  2226. assert_equal(xm, y * 2)
  2227. def test_inplace_multiplication_array(self):
  2228. # Test of inplace multiplication
  2229. (x, y, xm) = self.floatdata
  2230. m = xm.mask
  2231. a = arange(10, dtype=float)
  2232. a[-1] = masked
  2233. x *= a
  2234. xm *= a
  2235. assert_equal(x, y * a)
  2236. assert_equal(xm, y * a)
  2237. assert_equal(xm.mask, mask_or(m, a.mask))
  2238. def test_inplace_division_scalar_int(self):
  2239. # Test of inplace division
  2240. (x, y, xm) = self.intdata
  2241. x = arange(10) * 2
  2242. xm = arange(10) * 2
  2243. xm[2] = masked
  2244. x //= 2
  2245. assert_equal(x, y)
  2246. xm //= 2
  2247. assert_equal(xm, y)
  2248. def test_inplace_division_scalar_float(self):
  2249. # Test of inplace division
  2250. (x, y, xm) = self.floatdata
  2251. x /= 2.0
  2252. assert_equal(x, y / 2.0)
  2253. xm /= arange(10)
  2254. assert_equal(xm, ones((10,)))
  2255. def test_inplace_division_array_float(self):
  2256. # Test of inplace division
  2257. (x, y, xm) = self.floatdata
  2258. m = xm.mask
  2259. a = arange(10, dtype=float)
  2260. a[-1] = masked
  2261. x /= a
  2262. xm /= a
  2263. assert_equal(x, y / a)
  2264. assert_equal(xm, y / a)
  2265. assert_equal(xm.mask, mask_or(mask_or(m, a.mask), (a == 0)))
  2266. def test_inplace_division_misc(self):
  2267. x = [1., 1., 1., -2., pi / 2., 4., 5., -10., 10., 1., 2., 3.]
  2268. y = [5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.]
  2269. m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
  2270. m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1]
  2271. xm = masked_array(x, mask=m1)
  2272. ym = masked_array(y, mask=m2)
  2273. z = xm / ym
  2274. assert_equal(z._mask, [1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1])
  2275. assert_equal(z._data,
  2276. [1., 1., 1., -1., -pi / 2., 4., 5., 1., 1., 1., 2., 3.])
  2277. xm = xm.copy()
  2278. xm /= ym
  2279. assert_equal(xm._mask, [1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1])
  2280. assert_equal(z._data,
  2281. [1., 1., 1., -1., -pi / 2., 4., 5., 1., 1., 1., 2., 3.])
  2282. def test_datafriendly_add(self):
  2283. # Test keeping data w/ (inplace) addition
  2284. x = array([1, 2, 3], mask=[0, 0, 1])
  2285. # Test add w/ scalar
  2286. xx = x + 1
  2287. assert_equal(xx.data, [2, 3, 3])
  2288. assert_equal(xx.mask, [0, 0, 1])
  2289. # Test iadd w/ scalar
  2290. x += 1
  2291. assert_equal(x.data, [2, 3, 3])
  2292. assert_equal(x.mask, [0, 0, 1])
  2293. # Test add w/ array
  2294. x = array([1, 2, 3], mask=[0, 0, 1])
  2295. xx = x + array([1, 2, 3], mask=[1, 0, 0])
  2296. assert_equal(xx.data, [1, 4, 3])
  2297. assert_equal(xx.mask, [1, 0, 1])
  2298. # Test iadd w/ array
  2299. x = array([1, 2, 3], mask=[0, 0, 1])
  2300. x += array([1, 2, 3], mask=[1, 0, 0])
  2301. assert_equal(x.data, [1, 4, 3])
  2302. assert_equal(x.mask, [1, 0, 1])
  2303. def test_datafriendly_sub(self):
  2304. # Test keeping data w/ (inplace) subtraction
  2305. # Test sub w/ scalar
  2306. x = array([1, 2, 3], mask=[0, 0, 1])
  2307. xx = x - 1
  2308. assert_equal(xx.data, [0, 1, 3])
  2309. assert_equal(xx.mask, [0, 0, 1])
  2310. # Test isub w/ scalar
  2311. x = array([1, 2, 3], mask=[0, 0, 1])
  2312. x -= 1
  2313. assert_equal(x.data, [0, 1, 3])
  2314. assert_equal(x.mask, [0, 0, 1])
  2315. # Test sub w/ array
  2316. x = array([1, 2, 3], mask=[0, 0, 1])
  2317. xx = x - array([1, 2, 3], mask=[1, 0, 0])
  2318. assert_equal(xx.data, [1, 0, 3])
  2319. assert_equal(xx.mask, [1, 0, 1])
  2320. # Test isub w/ array
  2321. x = array([1, 2, 3], mask=[0, 0, 1])
  2322. x -= array([1, 2, 3], mask=[1, 0, 0])
  2323. assert_equal(x.data, [1, 0, 3])
  2324. assert_equal(x.mask, [1, 0, 1])
  2325. def test_datafriendly_mul(self):
  2326. # Test keeping data w/ (inplace) multiplication
  2327. # Test mul w/ scalar
  2328. x = array([1, 2, 3], mask=[0, 0, 1])
  2329. xx = x * 2
  2330. assert_equal(xx.data, [2, 4, 3])
  2331. assert_equal(xx.mask, [0, 0, 1])
  2332. # Test imul w/ scalar
  2333. x = array([1, 2, 3], mask=[0, 0, 1])
  2334. x *= 2
  2335. assert_equal(x.data, [2, 4, 3])
  2336. assert_equal(x.mask, [0, 0, 1])
  2337. # Test mul w/ array
  2338. x = array([1, 2, 3], mask=[0, 0, 1])
  2339. xx = x * array([10, 20, 30], mask=[1, 0, 0])
  2340. assert_equal(xx.data, [1, 40, 3])
  2341. assert_equal(xx.mask, [1, 0, 1])
  2342. # Test imul w/ array
  2343. x = array([1, 2, 3], mask=[0, 0, 1])
  2344. x *= array([10, 20, 30], mask=[1, 0, 0])
  2345. assert_equal(x.data, [1, 40, 3])
  2346. assert_equal(x.mask, [1, 0, 1])
  2347. def test_datafriendly_div(self):
  2348. # Test keeping data w/ (inplace) division
  2349. # Test div on scalar
  2350. x = array([1, 2, 3], mask=[0, 0, 1])
  2351. xx = x / 2.
  2352. assert_equal(xx.data, [1 / 2., 2 / 2., 3])
  2353. assert_equal(xx.mask, [0, 0, 1])
  2354. # Test idiv on scalar
  2355. x = array([1., 2., 3.], mask=[0, 0, 1])
  2356. x /= 2.
  2357. assert_equal(x.data, [1 / 2., 2 / 2., 3])
  2358. assert_equal(x.mask, [0, 0, 1])
  2359. # Test div on array
  2360. x = array([1., 2., 3.], mask=[0, 0, 1])
  2361. xx = x / array([10., 20., 30.], mask=[1, 0, 0])
  2362. assert_equal(xx.data, [1., 2. / 20., 3.])
  2363. assert_equal(xx.mask, [1, 0, 1])
  2364. # Test idiv on array
  2365. x = array([1., 2., 3.], mask=[0, 0, 1])
  2366. x /= array([10., 20., 30.], mask=[1, 0, 0])
  2367. assert_equal(x.data, [1., 2 / 20., 3.])
  2368. assert_equal(x.mask, [1, 0, 1])
  2369. def test_datafriendly_pow(self):
  2370. # Test keeping data w/ (inplace) power
  2371. # Test pow on scalar
  2372. x = array([1., 2., 3.], mask=[0, 0, 1])
  2373. xx = x ** 2.5
  2374. assert_equal(xx.data, [1., 2. ** 2.5, 3.])
  2375. assert_equal(xx.mask, [0, 0, 1])
  2376. # Test ipow on scalar
  2377. x **= 2.5
  2378. assert_equal(x.data, [1., 2. ** 2.5, 3])
  2379. assert_equal(x.mask, [0, 0, 1])
  2380. def test_datafriendly_add_arrays(self):
  2381. a = array([[1, 1], [3, 3]])
  2382. b = array([1, 1], mask=[0, 0])
  2383. a += b
  2384. assert_equal(a, [[2, 2], [4, 4]])
  2385. if a.mask is not nomask:
  2386. assert_equal(a.mask, [[0, 0], [0, 0]])
  2387. a = array([[1, 1], [3, 3]])
  2388. b = array([1, 1], mask=[0, 1])
  2389. a += b
  2390. assert_equal(a, [[2, 2], [4, 4]])
  2391. assert_equal(a.mask, [[0, 1], [0, 1]])
  2392. def test_datafriendly_sub_arrays(self):
  2393. a = array([[1, 1], [3, 3]])
  2394. b = array([1, 1], mask=[0, 0])
  2395. a -= b
  2396. assert_equal(a, [[0, 0], [2, 2]])
  2397. if a.mask is not nomask:
  2398. assert_equal(a.mask, [[0, 0], [0, 0]])
  2399. a = array([[1, 1], [3, 3]])
  2400. b = array([1, 1], mask=[0, 1])
  2401. a -= b
  2402. assert_equal(a, [[0, 0], [2, 2]])
  2403. assert_equal(a.mask, [[0, 1], [0, 1]])
  2404. def test_datafriendly_mul_arrays(self):
  2405. a = array([[1, 1], [3, 3]])
  2406. b = array([1, 1], mask=[0, 0])
  2407. a *= b
  2408. assert_equal(a, [[1, 1], [3, 3]])
  2409. if a.mask is not nomask:
  2410. assert_equal(a.mask, [[0, 0], [0, 0]])
  2411. a = array([[1, 1], [3, 3]])
  2412. b = array([1, 1], mask=[0, 1])
  2413. a *= b
  2414. assert_equal(a, [[1, 1], [3, 3]])
  2415. assert_equal(a.mask, [[0, 1], [0, 1]])
  2416. def test_inplace_addition_scalar_type(self):
  2417. # Test of inplace additions
  2418. for t in self.othertypes:
  2419. with warnings.catch_warnings(record=True) as w:
  2420. warnings.filterwarnings("always")
  2421. (x, y, xm) = (_.astype(t) for _ in self.uint8data)
  2422. xm[2] = masked
  2423. x += t(1)
  2424. assert_equal(x, y + t(1))
  2425. xm += t(1)
  2426. assert_equal(xm, y + t(1))
  2427. assert_equal(len(w), 0, f'Failed on type={t}.')
  2428. def test_inplace_addition_array_type(self):
  2429. # Test of inplace additions
  2430. for t in self.othertypes:
  2431. with warnings.catch_warnings(record=True) as w:
  2432. warnings.filterwarnings("always")
  2433. (x, y, xm) = (_.astype(t) for _ in self.uint8data)
  2434. m = xm.mask
  2435. a = arange(10, dtype=t)
  2436. a[-1] = masked
  2437. x += a
  2438. xm += a
  2439. assert_equal(x, y + a)
  2440. assert_equal(xm, y + a)
  2441. assert_equal(xm.mask, mask_or(m, a.mask))
  2442. assert_equal(len(w), 0, f'Failed on type={t}.')
  2443. def test_inplace_subtraction_scalar_type(self):
  2444. # Test of inplace subtractions
  2445. for t in self.othertypes:
  2446. with warnings.catch_warnings(record=True) as w:
  2447. warnings.filterwarnings("always")
  2448. (x, y, xm) = (_.astype(t) for _ in self.uint8data)
  2449. x -= t(1)
  2450. assert_equal(x, y - t(1))
  2451. xm -= t(1)
  2452. assert_equal(xm, y - t(1))
  2453. assert_equal(len(w), 0, f'Failed on type={t}.')
  2454. def test_inplace_subtraction_array_type(self):
  2455. # Test of inplace subtractions
  2456. for t in self.othertypes:
  2457. with warnings.catch_warnings(record=True) as w:
  2458. warnings.filterwarnings("always")
  2459. (x, y, xm) = (_.astype(t) for _ in self.uint8data)
  2460. m = xm.mask
  2461. a = arange(10, dtype=t)
  2462. a[-1] = masked
  2463. x -= a
  2464. xm -= a
  2465. assert_equal(x, y - a)
  2466. assert_equal(xm, y - a)
  2467. assert_equal(xm.mask, mask_or(m, a.mask))
  2468. assert_equal(len(w), 0, f'Failed on type={t}.')
  2469. def test_inplace_multiplication_scalar_type(self):
  2470. # Test of inplace multiplication
  2471. for t in self.othertypes:
  2472. with warnings.catch_warnings(record=True) as w:
  2473. warnings.filterwarnings("always")
  2474. (x, y, xm) = (_.astype(t) for _ in self.uint8data)
  2475. x *= t(2)
  2476. assert_equal(x, y * t(2))
  2477. xm *= t(2)
  2478. assert_equal(xm, y * t(2))
  2479. assert_equal(len(w), 0, f'Failed on type={t}.')
  2480. def test_inplace_multiplication_array_type(self):
  2481. # Test of inplace multiplication
  2482. for t in self.othertypes:
  2483. with warnings.catch_warnings(record=True) as w:
  2484. warnings.filterwarnings("always")
  2485. (x, y, xm) = (_.astype(t) for _ in self.uint8data)
  2486. m = xm.mask
  2487. a = arange(10, dtype=t)
  2488. a[-1] = masked
  2489. x *= a
  2490. xm *= a
  2491. assert_equal(x, y * a)
  2492. assert_equal(xm, y * a)
  2493. assert_equal(xm.mask, mask_or(m, a.mask))
  2494. assert_equal(len(w), 0, f'Failed on type={t}.')
  2495. def test_inplace_floor_division_scalar_type(self):
  2496. # Test of inplace division
  2497. for t in self.othertypes:
  2498. with warnings.catch_warnings(record=True) as w:
  2499. warnings.filterwarnings("always")
  2500. (x, y, xm) = (_.astype(t) for _ in self.uint8data)
  2501. x = arange(10, dtype=t) * t(2)
  2502. xm = arange(10, dtype=t) * t(2)
  2503. xm[2] = masked
  2504. x //= t(2)
  2505. xm //= t(2)
  2506. assert_equal(x, y)
  2507. assert_equal(xm, y)
  2508. assert_equal(len(w), 0, "Failed on type=%s." % t)
  2509. def test_inplace_floor_division_array_type(self):
  2510. # Test of inplace division
  2511. for t in self.othertypes:
  2512. with warnings.catch_warnings(record=True) as w:
  2513. warnings.filterwarnings("always")
  2514. (x, y, xm) = (_.astype(t) for _ in self.uint8data)
  2515. m = xm.mask
  2516. a = arange(10, dtype=t)
  2517. a[-1] = masked
  2518. x //= a
  2519. xm //= a
  2520. assert_equal(x, y // a)
  2521. assert_equal(xm, y // a)
  2522. assert_equal(
  2523. xm.mask,
  2524. mask_or(mask_or(m, a.mask), (a == t(0)))
  2525. )
  2526. assert_equal(len(w), 0, f'Failed on type={t}.')
  2527. def test_inplace_division_scalar_type(self):
  2528. # Test of inplace division
  2529. for t in self.othertypes:
  2530. with suppress_warnings() as sup:
  2531. sup.record(UserWarning)
  2532. (x, y, xm) = (_.astype(t) for _ in self.uint8data)
  2533. x = arange(10, dtype=t) * t(2)
  2534. xm = arange(10, dtype=t) * t(2)
  2535. xm[2] = masked
  2536. # May get a DeprecationWarning or a TypeError.
  2537. #
  2538. # This is a consequence of the fact that this is true divide
  2539. # and will require casting to float for calculation and
  2540. # casting back to the original type. This will only be raised
  2541. # with integers. Whether it is an error or warning is only
  2542. # dependent on how stringent the casting rules are.
  2543. #
  2544. # Will handle the same way.
  2545. try:
  2546. x /= t(2)
  2547. assert_equal(x, y)
  2548. except (DeprecationWarning, TypeError) as e:
  2549. warnings.warn(str(e), stacklevel=1)
  2550. try:
  2551. xm /= t(2)
  2552. assert_equal(xm, y)
  2553. except (DeprecationWarning, TypeError) as e:
  2554. warnings.warn(str(e), stacklevel=1)
  2555. if issubclass(t, np.integer):
  2556. assert_equal(len(sup.log), 2, f'Failed on type={t}.')
  2557. else:
  2558. assert_equal(len(sup.log), 0, f'Failed on type={t}.')
  2559. def test_inplace_division_array_type(self):
  2560. # Test of inplace division
  2561. for t in self.othertypes:
  2562. with suppress_warnings() as sup:
  2563. sup.record(UserWarning)
  2564. (x, y, xm) = (_.astype(t) for _ in self.uint8data)
  2565. m = xm.mask
  2566. a = arange(10, dtype=t)
  2567. a[-1] = masked
  2568. # May get a DeprecationWarning or a TypeError.
  2569. #
  2570. # This is a consequence of the fact that this is true divide
  2571. # and will require casting to float for calculation and
  2572. # casting back to the original type. This will only be raised
  2573. # with integers. Whether it is an error or warning is only
  2574. # dependent on how stringent the casting rules are.
  2575. #
  2576. # Will handle the same way.
  2577. try:
  2578. x /= a
  2579. assert_equal(x, y / a)
  2580. except (DeprecationWarning, TypeError) as e:
  2581. warnings.warn(str(e), stacklevel=1)
  2582. try:
  2583. xm /= a
  2584. assert_equal(xm, y / a)
  2585. assert_equal(
  2586. xm.mask,
  2587. mask_or(mask_or(m, a.mask), (a == t(0)))
  2588. )
  2589. except (DeprecationWarning, TypeError) as e:
  2590. warnings.warn(str(e), stacklevel=1)
  2591. if issubclass(t, np.integer):
  2592. assert_equal(len(sup.log), 2, f'Failed on type={t}.')
  2593. else:
  2594. assert_equal(len(sup.log), 0, f'Failed on type={t}.')
  2595. def test_inplace_pow_type(self):
  2596. # Test keeping data w/ (inplace) power
  2597. for t in self.othertypes:
  2598. with warnings.catch_warnings(record=True) as w:
  2599. warnings.filterwarnings("always")
  2600. # Test pow on scalar
  2601. x = array([1, 2, 3], mask=[0, 0, 1], dtype=t)
  2602. xx = x ** t(2)
  2603. xx_r = array([1, 2 ** 2, 3], mask=[0, 0, 1], dtype=t)
  2604. assert_equal(xx.data, xx_r.data)
  2605. assert_equal(xx.mask, xx_r.mask)
  2606. # Test ipow on scalar
  2607. x **= t(2)
  2608. assert_equal(x.data, xx_r.data)
  2609. assert_equal(x.mask, xx_r.mask)
  2610. assert_equal(len(w), 0, f'Failed on type={t}.')
  2611. class TestMaskedArrayMethods:
  2612. # Test class for miscellaneous MaskedArrays methods.
  2613. def setup(self):
  2614. # Base data definition.
  2615. x = np.array([8.375, 7.545, 8.828, 8.5, 1.757, 5.928,
  2616. 8.43, 7.78, 9.865, 5.878, 8.979, 4.732,
  2617. 3.012, 6.022, 5.095, 3.116, 5.238, 3.957,
  2618. 6.04, 9.63, 7.712, 3.382, 4.489, 6.479,
  2619. 7.189, 9.645, 5.395, 4.961, 9.894, 2.893,
  2620. 7.357, 9.828, 6.272, 3.758, 6.693, 0.993])
  2621. X = x.reshape(6, 6)
  2622. XX = x.reshape(3, 2, 2, 3)
  2623. m = np.array([0, 1, 0, 1, 0, 0,
  2624. 1, 0, 1, 1, 0, 1,
  2625. 0, 0, 0, 1, 0, 1,
  2626. 0, 0, 0, 1, 1, 1,
  2627. 1, 0, 0, 1, 0, 0,
  2628. 0, 0, 1, 0, 1, 0])
  2629. mx = array(data=x, mask=m)
  2630. mX = array(data=X, mask=m.reshape(X.shape))
  2631. mXX = array(data=XX, mask=m.reshape(XX.shape))
  2632. m2 = np.array([1, 1, 0, 1, 0, 0,
  2633. 1, 1, 1, 1, 0, 1,
  2634. 0, 0, 1, 1, 0, 1,
  2635. 0, 0, 0, 1, 1, 1,
  2636. 1, 0, 0, 1, 1, 0,
  2637. 0, 0, 1, 0, 1, 1])
  2638. m2x = array(data=x, mask=m2)
  2639. m2X = array(data=X, mask=m2.reshape(X.shape))
  2640. m2XX = array(data=XX, mask=m2.reshape(XX.shape))
  2641. self.d = (x, X, XX, m, mx, mX, mXX, m2x, m2X, m2XX)
  2642. def test_generic_methods(self):
  2643. # Tests some MaskedArray methods.
  2644. a = array([1, 3, 2])
  2645. assert_equal(a.any(), a._data.any())
  2646. assert_equal(a.all(), a._data.all())
  2647. assert_equal(a.argmax(), a._data.argmax())
  2648. assert_equal(a.argmin(), a._data.argmin())
  2649. assert_equal(a.choose(0, 1, 2, 3, 4), a._data.choose(0, 1, 2, 3, 4))
  2650. assert_equal(a.compress([1, 0, 1]), a._data.compress([1, 0, 1]))
  2651. assert_equal(a.conj(), a._data.conj())
  2652. assert_equal(a.conjugate(), a._data.conjugate())
  2653. m = array([[1, 2], [3, 4]])
  2654. assert_equal(m.diagonal(), m._data.diagonal())
  2655. assert_equal(a.sum(), a._data.sum())
  2656. assert_equal(a.take([1, 2]), a._data.take([1, 2]))
  2657. assert_equal(m.transpose(), m._data.transpose())
  2658. def test_allclose(self):
  2659. # Tests allclose on arrays
  2660. a = np.random.rand(10)
  2661. b = a + np.random.rand(10) * 1e-8
  2662. assert_(allclose(a, b))
  2663. # Test allclose w/ infs
  2664. a[0] = np.inf
  2665. assert_(not allclose(a, b))
  2666. b[0] = np.inf
  2667. assert_(allclose(a, b))
  2668. # Test allclose w/ masked
  2669. a = masked_array(a)
  2670. a[-1] = masked
  2671. assert_(allclose(a, b, masked_equal=True))
  2672. assert_(not allclose(a, b, masked_equal=False))
  2673. # Test comparison w/ scalar
  2674. a *= 1e-8
  2675. a[0] = 0
  2676. assert_(allclose(a, 0, masked_equal=True))
  2677. # Test that the function works for MIN_INT integer typed arrays
  2678. a = masked_array([np.iinfo(np.int_).min], dtype=np.int_)
  2679. assert_(allclose(a, a))
  2680. def test_allclose_timedelta(self):
  2681. # Allclose currently works for timedelta64 as long as `atol` is
  2682. # an integer or also a timedelta64
  2683. a = np.array([[1, 2, 3, 4]], dtype="m8[ns]")
  2684. assert allclose(a, a, atol=0)
  2685. assert allclose(a, a, atol=np.timedelta64(1, "ns"))
  2686. def test_allany(self):
  2687. # Checks the any/all methods/functions.
  2688. x = np.array([[0.13, 0.26, 0.90],
  2689. [0.28, 0.33, 0.63],
  2690. [0.31, 0.87, 0.70]])
  2691. m = np.array([[True, False, False],
  2692. [False, False, False],
  2693. [True, True, False]], dtype=np.bool_)
  2694. mx = masked_array(x, mask=m)
  2695. mxbig = (mx > 0.5)
  2696. mxsmall = (mx < 0.5)
  2697. assert_(not mxbig.all())
  2698. assert_(mxbig.any())
  2699. assert_equal(mxbig.all(0), [False, False, True])
  2700. assert_equal(mxbig.all(1), [False, False, True])
  2701. assert_equal(mxbig.any(0), [False, False, True])
  2702. assert_equal(mxbig.any(1), [True, True, True])
  2703. assert_(not mxsmall.all())
  2704. assert_(mxsmall.any())
  2705. assert_equal(mxsmall.all(0), [True, True, False])
  2706. assert_equal(mxsmall.all(1), [False, False, False])
  2707. assert_equal(mxsmall.any(0), [True, True, False])
  2708. assert_equal(mxsmall.any(1), [True, True, False])
  2709. def test_allany_oddities(self):
  2710. # Some fun with all and any
  2711. store = empty((), dtype=bool)
  2712. full = array([1, 2, 3], mask=True)
  2713. assert_(full.all() is masked)
  2714. full.all(out=store)
  2715. assert_(store)
  2716. assert_(store._mask, True)
  2717. assert_(store is not masked)
  2718. store = empty((), dtype=bool)
  2719. assert_(full.any() is masked)
  2720. full.any(out=store)
  2721. assert_(not store)
  2722. assert_(store._mask, True)
  2723. assert_(store is not masked)
  2724. def test_argmax_argmin(self):
  2725. # Tests argmin & argmax on MaskedArrays.
  2726. (x, X, XX, m, mx, mX, mXX, m2x, m2X, m2XX) = self.d
  2727. assert_equal(mx.argmin(), 35)
  2728. assert_equal(mX.argmin(), 35)
  2729. assert_equal(m2x.argmin(), 4)
  2730. assert_equal(m2X.argmin(), 4)
  2731. assert_equal(mx.argmax(), 28)
  2732. assert_equal(mX.argmax(), 28)
  2733. assert_equal(m2x.argmax(), 31)
  2734. assert_equal(m2X.argmax(), 31)
  2735. assert_equal(mX.argmin(0), [2, 2, 2, 5, 0, 5])
  2736. assert_equal(m2X.argmin(0), [2, 2, 4, 5, 0, 4])
  2737. assert_equal(mX.argmax(0), [0, 5, 0, 5, 4, 0])
  2738. assert_equal(m2X.argmax(0), [5, 5, 0, 5, 1, 0])
  2739. assert_equal(mX.argmin(1), [4, 1, 0, 0, 5, 5, ])
  2740. assert_equal(m2X.argmin(1), [4, 4, 0, 0, 5, 3])
  2741. assert_equal(mX.argmax(1), [2, 4, 1, 1, 4, 1])
  2742. assert_equal(m2X.argmax(1), [2, 4, 1, 1, 1, 1])
  2743. def test_clip(self):
  2744. # Tests clip on MaskedArrays.
  2745. x = np.array([8.375, 7.545, 8.828, 8.5, 1.757, 5.928,
  2746. 8.43, 7.78, 9.865, 5.878, 8.979, 4.732,
  2747. 3.012, 6.022, 5.095, 3.116, 5.238, 3.957,
  2748. 6.04, 9.63, 7.712, 3.382, 4.489, 6.479,
  2749. 7.189, 9.645, 5.395, 4.961, 9.894, 2.893,
  2750. 7.357, 9.828, 6.272, 3.758, 6.693, 0.993])
  2751. m = np.array([0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1,
  2752. 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1,
  2753. 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0])
  2754. mx = array(x, mask=m)
  2755. clipped = mx.clip(2, 8)
  2756. assert_equal(clipped.mask, mx.mask)
  2757. assert_equal(clipped._data, x.clip(2, 8))
  2758. assert_equal(clipped._data, mx._data.clip(2, 8))
  2759. def test_clip_out(self):
  2760. # gh-14140
  2761. a = np.arange(10)
  2762. m = np.ma.MaskedArray(a, mask=[0, 1] * 5)
  2763. m.clip(0, 5, out=m)
  2764. assert_equal(m.mask, [0, 1] * 5)
  2765. def test_compress(self):
  2766. # test compress
  2767. a = masked_array([1., 2., 3., 4., 5.], fill_value=9999)
  2768. condition = (a > 1.5) & (a < 3.5)
  2769. assert_equal(a.compress(condition), [2., 3.])
  2770. a[[2, 3]] = masked
  2771. b = a.compress(condition)
  2772. assert_equal(b._data, [2., 3.])
  2773. assert_equal(b._mask, [0, 1])
  2774. assert_equal(b.fill_value, 9999)
  2775. assert_equal(b, a[condition])
  2776. condition = (a < 4.)
  2777. b = a.compress(condition)
  2778. assert_equal(b._data, [1., 2., 3.])
  2779. assert_equal(b._mask, [0, 0, 1])
  2780. assert_equal(b.fill_value, 9999)
  2781. assert_equal(b, a[condition])
  2782. a = masked_array([[10, 20, 30], [40, 50, 60]],
  2783. mask=[[0, 0, 1], [1, 0, 0]])
  2784. b = a.compress(a.ravel() >= 22)
  2785. assert_equal(b._data, [30, 40, 50, 60])
  2786. assert_equal(b._mask, [1, 1, 0, 0])
  2787. x = np.array([3, 1, 2])
  2788. b = a.compress(x >= 2, axis=1)
  2789. assert_equal(b._data, [[10, 30], [40, 60]])
  2790. assert_equal(b._mask, [[0, 1], [1, 0]])
  2791. def test_compressed(self):
  2792. # Tests compressed
  2793. a = array([1, 2, 3, 4], mask=[0, 0, 0, 0])
  2794. b = a.compressed()
  2795. assert_equal(b, a)
  2796. a[0] = masked
  2797. b = a.compressed()
  2798. assert_equal(b, [2, 3, 4])
  2799. def test_empty(self):
  2800. # Tests empty/like
  2801. datatype = [('a', int), ('b', float), ('c', '|S8')]
  2802. a = masked_array([(1, 1.1, '1.1'), (2, 2.2, '2.2'), (3, 3.3, '3.3')],
  2803. dtype=datatype)
  2804. assert_equal(len(a.fill_value.item()), len(datatype))
  2805. b = empty_like(a)
  2806. assert_equal(b.shape, a.shape)
  2807. assert_equal(b.fill_value, a.fill_value)
  2808. b = empty(len(a), dtype=datatype)
  2809. assert_equal(b.shape, a.shape)
  2810. assert_equal(b.fill_value, a.fill_value)
  2811. # check empty_like mask handling
  2812. a = masked_array([1, 2, 3], mask=[False, True, False])
  2813. b = empty_like(a)
  2814. assert_(not np.may_share_memory(a.mask, b.mask))
  2815. b = a.view(masked_array)
  2816. assert_(np.may_share_memory(a.mask, b.mask))
  2817. @suppress_copy_mask_on_assignment
  2818. def test_put(self):
  2819. # Tests put.
  2820. d = arange(5)
  2821. n = [0, 0, 0, 1, 1]
  2822. m = make_mask(n)
  2823. x = array(d, mask=m)
  2824. assert_(x[3] is masked)
  2825. assert_(x[4] is masked)
  2826. x[[1, 4]] = [10, 40]
  2827. assert_(x[3] is masked)
  2828. assert_(x[4] is not masked)
  2829. assert_equal(x, [0, 10, 2, -1, 40])
  2830. x = masked_array(arange(10), mask=[1, 0, 0, 0, 0] * 2)
  2831. i = [0, 2, 4, 6]
  2832. x.put(i, [6, 4, 2, 0])
  2833. assert_equal(x, asarray([6, 1, 4, 3, 2, 5, 0, 7, 8, 9, ]))
  2834. assert_equal(x.mask, [0, 0, 0, 0, 0, 1, 0, 0, 0, 0])
  2835. x.put(i, masked_array([0, 2, 4, 6], [1, 0, 1, 0]))
  2836. assert_array_equal(x, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ])
  2837. assert_equal(x.mask, [1, 0, 0, 0, 1, 1, 0, 0, 0, 0])
  2838. x = masked_array(arange(10), mask=[1, 0, 0, 0, 0] * 2)
  2839. put(x, i, [6, 4, 2, 0])
  2840. assert_equal(x, asarray([6, 1, 4, 3, 2, 5, 0, 7, 8, 9, ]))
  2841. assert_equal(x.mask, [0, 0, 0, 0, 0, 1, 0, 0, 0, 0])
  2842. put(x, i, masked_array([0, 2, 4, 6], [1, 0, 1, 0]))
  2843. assert_array_equal(x, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ])
  2844. assert_equal(x.mask, [1, 0, 0, 0, 1, 1, 0, 0, 0, 0])
  2845. def test_put_nomask(self):
  2846. # GitHub issue 6425
  2847. x = zeros(10)
  2848. z = array([3., -1.], mask=[False, True])
  2849. x.put([1, 2], z)
  2850. assert_(x[0] is not masked)
  2851. assert_equal(x[0], 0)
  2852. assert_(x[1] is not masked)
  2853. assert_equal(x[1], 3)
  2854. assert_(x[2] is masked)
  2855. assert_(x[3] is not masked)
  2856. assert_equal(x[3], 0)
  2857. def test_put_hardmask(self):
  2858. # Tests put on hardmask
  2859. d = arange(5)
  2860. n = [0, 0, 0, 1, 1]
  2861. m = make_mask(n)
  2862. xh = array(d + 1, mask=m, hard_mask=True, copy=True)
  2863. xh.put([4, 2, 0, 1, 3], [1, 2, 3, 4, 5])
  2864. assert_equal(xh._data, [3, 4, 2, 4, 5])
  2865. def test_putmask(self):
  2866. x = arange(6) + 1
  2867. mx = array(x, mask=[0, 0, 0, 1, 1, 1])
  2868. mask = [0, 0, 1, 0, 0, 1]
  2869. # w/o mask, w/o masked values
  2870. xx = x.copy()
  2871. putmask(xx, mask, 99)
  2872. assert_equal(xx, [1, 2, 99, 4, 5, 99])
  2873. # w/ mask, w/o masked values
  2874. mxx = mx.copy()
  2875. putmask(mxx, mask, 99)
  2876. assert_equal(mxx._data, [1, 2, 99, 4, 5, 99])
  2877. assert_equal(mxx._mask, [0, 0, 0, 1, 1, 0])
  2878. # w/o mask, w/ masked values
  2879. values = array([10, 20, 30, 40, 50, 60], mask=[1, 1, 1, 0, 0, 0])
  2880. xx = x.copy()
  2881. putmask(xx, mask, values)
  2882. assert_equal(xx._data, [1, 2, 30, 4, 5, 60])
  2883. assert_equal(xx._mask, [0, 0, 1, 0, 0, 0])
  2884. # w/ mask, w/ masked values
  2885. mxx = mx.copy()
  2886. putmask(mxx, mask, values)
  2887. assert_equal(mxx._data, [1, 2, 30, 4, 5, 60])
  2888. assert_equal(mxx._mask, [0, 0, 1, 1, 1, 0])
  2889. # w/ mask, w/ masked values + hardmask
  2890. mxx = mx.copy()
  2891. mxx.harden_mask()
  2892. putmask(mxx, mask, values)
  2893. assert_equal(mxx, [1, 2, 30, 4, 5, 60])
  2894. def test_ravel(self):
  2895. # Tests ravel
  2896. a = array([[1, 2, 3, 4, 5]], mask=[[0, 1, 0, 0, 0]])
  2897. aravel = a.ravel()
  2898. assert_equal(aravel._mask.shape, aravel.shape)
  2899. a = array([0, 0], mask=[1, 1])
  2900. aravel = a.ravel()
  2901. assert_equal(aravel._mask.shape, a.shape)
  2902. # Checks that small_mask is preserved
  2903. a = array([1, 2, 3, 4], mask=[0, 0, 0, 0], shrink=False)
  2904. assert_equal(a.ravel()._mask, [0, 0, 0, 0])
  2905. # Test that the fill_value is preserved
  2906. a.fill_value = -99
  2907. a.shape = (2, 2)
  2908. ar = a.ravel()
  2909. assert_equal(ar._mask, [0, 0, 0, 0])
  2910. assert_equal(ar._data, [1, 2, 3, 4])
  2911. assert_equal(ar.fill_value, -99)
  2912. # Test index ordering
  2913. assert_equal(a.ravel(order='C'), [1, 2, 3, 4])
  2914. assert_equal(a.ravel(order='F'), [1, 3, 2, 4])
  2915. def test_reshape(self):
  2916. # Tests reshape
  2917. x = arange(4)
  2918. x[0] = masked
  2919. y = x.reshape(2, 2)
  2920. assert_equal(y.shape, (2, 2,))
  2921. assert_equal(y._mask.shape, (2, 2,))
  2922. assert_equal(x.shape, (4,))
  2923. assert_equal(x._mask.shape, (4,))
  2924. def test_sort(self):
  2925. # Test sort
  2926. x = array([1, 4, 2, 3], mask=[0, 1, 0, 0], dtype=np.uint8)
  2927. sortedx = sort(x)
  2928. assert_equal(sortedx._data, [1, 2, 3, 4])
  2929. assert_equal(sortedx._mask, [0, 0, 0, 1])
  2930. sortedx = sort(x, endwith=False)
  2931. assert_equal(sortedx._data, [4, 1, 2, 3])
  2932. assert_equal(sortedx._mask, [1, 0, 0, 0])
  2933. x.sort()
  2934. assert_equal(x._data, [1, 2, 3, 4])
  2935. assert_equal(x._mask, [0, 0, 0, 1])
  2936. x = array([1, 4, 2, 3], mask=[0, 1, 0, 0], dtype=np.uint8)
  2937. x.sort(endwith=False)
  2938. assert_equal(x._data, [4, 1, 2, 3])
  2939. assert_equal(x._mask, [1, 0, 0, 0])
  2940. x = [1, 4, 2, 3]
  2941. sortedx = sort(x)
  2942. assert_(not isinstance(sorted, MaskedArray))
  2943. x = array([0, 1, -1, -2, 2], mask=nomask, dtype=np.int8)
  2944. sortedx = sort(x, endwith=False)
  2945. assert_equal(sortedx._data, [-2, -1, 0, 1, 2])
  2946. x = array([0, 1, -1, -2, 2], mask=[0, 1, 0, 0, 1], dtype=np.int8)
  2947. sortedx = sort(x, endwith=False)
  2948. assert_equal(sortedx._data, [1, 2, -2, -1, 0])
  2949. assert_equal(sortedx._mask, [1, 1, 0, 0, 0])
  2950. def test_stable_sort(self):
  2951. x = array([1, 2, 3, 1, 2, 3], dtype=np.uint8)
  2952. expected = array([0, 3, 1, 4, 2, 5])
  2953. computed = argsort(x, kind='stable')
  2954. assert_equal(computed, expected)
  2955. def test_argsort_matches_sort(self):
  2956. x = array([1, 4, 2, 3], mask=[0, 1, 0, 0], dtype=np.uint8)
  2957. for kwargs in [dict(),
  2958. dict(endwith=True),
  2959. dict(endwith=False),
  2960. dict(fill_value=2),
  2961. dict(fill_value=2, endwith=True),
  2962. dict(fill_value=2, endwith=False)]:
  2963. sortedx = sort(x, **kwargs)
  2964. argsortedx = x[argsort(x, **kwargs)]
  2965. assert_equal(sortedx._data, argsortedx._data)
  2966. assert_equal(sortedx._mask, argsortedx._mask)
  2967. def test_sort_2d(self):
  2968. # Check sort of 2D array.
  2969. # 2D array w/o mask
  2970. a = masked_array([[8, 4, 1], [2, 0, 9]])
  2971. a.sort(0)
  2972. assert_equal(a, [[2, 0, 1], [8, 4, 9]])
  2973. a = masked_array([[8, 4, 1], [2, 0, 9]])
  2974. a.sort(1)
  2975. assert_equal(a, [[1, 4, 8], [0, 2, 9]])
  2976. # 2D array w/mask
  2977. a = masked_array([[8, 4, 1], [2, 0, 9]], mask=[[1, 0, 0], [0, 0, 1]])
  2978. a.sort(0)
  2979. assert_equal(a, [[2, 0, 1], [8, 4, 9]])
  2980. assert_equal(a._mask, [[0, 0, 0], [1, 0, 1]])
  2981. a = masked_array([[8, 4, 1], [2, 0, 9]], mask=[[1, 0, 0], [0, 0, 1]])
  2982. a.sort(1)
  2983. assert_equal(a, [[1, 4, 8], [0, 2, 9]])
  2984. assert_equal(a._mask, [[0, 0, 1], [0, 0, 1]])
  2985. # 3D
  2986. a = masked_array([[[7, 8, 9], [4, 5, 6], [1, 2, 3]],
  2987. [[1, 2, 3], [7, 8, 9], [4, 5, 6]],
  2988. [[7, 8, 9], [1, 2, 3], [4, 5, 6]],
  2989. [[4, 5, 6], [1, 2, 3], [7, 8, 9]]])
  2990. a[a % 4 == 0] = masked
  2991. am = a.copy()
  2992. an = a.filled(99)
  2993. am.sort(0)
  2994. an.sort(0)
  2995. assert_equal(am, an)
  2996. am = a.copy()
  2997. an = a.filled(99)
  2998. am.sort(1)
  2999. an.sort(1)
  3000. assert_equal(am, an)
  3001. am = a.copy()
  3002. an = a.filled(99)
  3003. am.sort(2)
  3004. an.sort(2)
  3005. assert_equal(am, an)
  3006. def test_sort_flexible(self):
  3007. # Test sort on structured dtype.
  3008. a = array(
  3009. data=[(3, 3), (3, 2), (2, 2), (2, 1), (1, 0), (1, 1), (1, 2)],
  3010. mask=[(0, 0), (0, 1), (0, 0), (0, 0), (1, 0), (0, 0), (0, 0)],
  3011. dtype=[('A', int), ('B', int)])
  3012. mask_last = array(
  3013. data=[(1, 1), (1, 2), (2, 1), (2, 2), (3, 3), (3, 2), (1, 0)],
  3014. mask=[(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 1), (1, 0)],
  3015. dtype=[('A', int), ('B', int)])
  3016. mask_first = array(
  3017. data=[(1, 0), (1, 1), (1, 2), (2, 1), (2, 2), (3, 2), (3, 3)],
  3018. mask=[(1, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 1), (0, 0)],
  3019. dtype=[('A', int), ('B', int)])
  3020. test = sort(a)
  3021. assert_equal(test, mask_last)
  3022. assert_equal(test.mask, mask_last.mask)
  3023. test = sort(a, endwith=False)
  3024. assert_equal(test, mask_first)
  3025. assert_equal(test.mask, mask_first.mask)
  3026. # Test sort on dtype with subarray (gh-8069)
  3027. # Just check that the sort does not error, structured array subarrays
  3028. # are treated as byte strings and that leads to differing behavior
  3029. # depending on endianess and `endwith`.
  3030. dt = np.dtype([('v', int, 2)])
  3031. a = a.view(dt)
  3032. test = sort(a)
  3033. test = sort(a, endwith=False)
  3034. def test_argsort(self):
  3035. # Test argsort
  3036. a = array([1, 5, 2, 4, 3], mask=[1, 0, 0, 1, 0])
  3037. assert_equal(np.argsort(a), argsort(a))
  3038. def test_squeeze(self):
  3039. # Check squeeze
  3040. data = masked_array([[1, 2, 3]])
  3041. assert_equal(data.squeeze(), [1, 2, 3])
  3042. data = masked_array([[1, 2, 3]], mask=[[1, 1, 1]])
  3043. assert_equal(data.squeeze(), [1, 2, 3])
  3044. assert_equal(data.squeeze()._mask, [1, 1, 1])
  3045. # normal ndarrays return a view
  3046. arr = np.array([[1]])
  3047. arr_sq = arr.squeeze()
  3048. assert_equal(arr_sq, 1)
  3049. arr_sq[...] = 2
  3050. assert_equal(arr[0,0], 2)
  3051. # so maskedarrays should too
  3052. m_arr = masked_array([[1]], mask=True)
  3053. m_arr_sq = m_arr.squeeze()
  3054. assert_(m_arr_sq is not np.ma.masked)
  3055. assert_equal(m_arr_sq.mask, True)
  3056. m_arr_sq[...] = 2
  3057. assert_equal(m_arr[0,0], 2)
  3058. def test_swapaxes(self):
  3059. # Tests swapaxes on MaskedArrays.
  3060. x = np.array([8.375, 7.545, 8.828, 8.5, 1.757, 5.928,
  3061. 8.43, 7.78, 9.865, 5.878, 8.979, 4.732,
  3062. 3.012, 6.022, 5.095, 3.116, 5.238, 3.957,
  3063. 6.04, 9.63, 7.712, 3.382, 4.489, 6.479,
  3064. 7.189, 9.645, 5.395, 4.961, 9.894, 2.893,
  3065. 7.357, 9.828, 6.272, 3.758, 6.693, 0.993])
  3066. m = np.array([0, 1, 0, 1, 0, 0,
  3067. 1, 0, 1, 1, 0, 1,
  3068. 0, 0, 0, 1, 0, 1,
  3069. 0, 0, 0, 1, 1, 1,
  3070. 1, 0, 0, 1, 0, 0,
  3071. 0, 0, 1, 0, 1, 0])
  3072. mX = array(x, mask=m).reshape(6, 6)
  3073. mXX = mX.reshape(3, 2, 2, 3)
  3074. mXswapped = mX.swapaxes(0, 1)
  3075. assert_equal(mXswapped[-1], mX[:, -1])
  3076. mXXswapped = mXX.swapaxes(0, 2)
  3077. assert_equal(mXXswapped.shape, (2, 2, 3, 3))
  3078. def test_take(self):
  3079. # Tests take
  3080. x = masked_array([10, 20, 30, 40], [0, 1, 0, 1])
  3081. assert_equal(x.take([0, 0, 3]), masked_array([10, 10, 40], [0, 0, 1]))
  3082. assert_equal(x.take([0, 0, 3]), x[[0, 0, 3]])
  3083. assert_equal(x.take([[0, 1], [0, 1]]),
  3084. masked_array([[10, 20], [10, 20]], [[0, 1], [0, 1]]))
  3085. # assert_equal crashes when passed np.ma.mask
  3086. assert_(x[1] is np.ma.masked)
  3087. assert_(x.take(1) is np.ma.masked)
  3088. x = array([[10, 20, 30], [40, 50, 60]], mask=[[0, 0, 1], [1, 0, 0, ]])
  3089. assert_equal(x.take([0, 2], axis=1),
  3090. array([[10, 30], [40, 60]], mask=[[0, 1], [1, 0]]))
  3091. assert_equal(take(x, [0, 2], axis=1),
  3092. array([[10, 30], [40, 60]], mask=[[0, 1], [1, 0]]))
  3093. def test_take_masked_indices(self):
  3094. # Test take w/ masked indices
  3095. a = np.array((40, 18, 37, 9, 22))
  3096. indices = np.arange(3)[None,:] + np.arange(5)[:, None]
  3097. mindices = array(indices, mask=(indices >= len(a)))
  3098. # No mask
  3099. test = take(a, mindices, mode='clip')
  3100. ctrl = array([[40, 18, 37],
  3101. [18, 37, 9],
  3102. [37, 9, 22],
  3103. [9, 22, 22],
  3104. [22, 22, 22]])
  3105. assert_equal(test, ctrl)
  3106. # Masked indices
  3107. test = take(a, mindices)
  3108. ctrl = array([[40, 18, 37],
  3109. [18, 37, 9],
  3110. [37, 9, 22],
  3111. [9, 22, 40],
  3112. [22, 40, 40]])
  3113. ctrl[3, 2] = ctrl[4, 1] = ctrl[4, 2] = masked
  3114. assert_equal(test, ctrl)
  3115. assert_equal(test.mask, ctrl.mask)
  3116. # Masked input + masked indices
  3117. a = array((40, 18, 37, 9, 22), mask=(0, 1, 0, 0, 0))
  3118. test = take(a, mindices)
  3119. ctrl[0, 1] = ctrl[1, 0] = masked
  3120. assert_equal(test, ctrl)
  3121. assert_equal(test.mask, ctrl.mask)
  3122. def test_tolist(self):
  3123. # Tests to list
  3124. # ... on 1D
  3125. x = array(np.arange(12))
  3126. x[[1, -2]] = masked
  3127. xlist = x.tolist()
  3128. assert_(xlist[1] is None)
  3129. assert_(xlist[-2] is None)
  3130. # ... on 2D
  3131. x.shape = (3, 4)
  3132. xlist = x.tolist()
  3133. ctrl = [[0, None, 2, 3], [4, 5, 6, 7], [8, 9, None, 11]]
  3134. assert_equal(xlist[0], [0, None, 2, 3])
  3135. assert_equal(xlist[1], [4, 5, 6, 7])
  3136. assert_equal(xlist[2], [8, 9, None, 11])
  3137. assert_equal(xlist, ctrl)
  3138. # ... on structured array w/ masked records
  3139. x = array(list(zip([1, 2, 3],
  3140. [1.1, 2.2, 3.3],
  3141. ['one', 'two', 'thr'])),
  3142. dtype=[('a', int), ('b', float), ('c', '|S8')])
  3143. x[-1] = masked
  3144. assert_equal(x.tolist(),
  3145. [(1, 1.1, b'one'),
  3146. (2, 2.2, b'two'),
  3147. (None, None, None)])
  3148. # ... on structured array w/ masked fields
  3149. a = array([(1, 2,), (3, 4)], mask=[(0, 1), (0, 0)],
  3150. dtype=[('a', int), ('b', int)])
  3151. test = a.tolist()
  3152. assert_equal(test, [[1, None], [3, 4]])
  3153. # ... on mvoid
  3154. a = a[0]
  3155. test = a.tolist()
  3156. assert_equal(test, [1, None])
  3157. def test_tolist_specialcase(self):
  3158. # Test mvoid.tolist: make sure we return a standard Python object
  3159. a = array([(0, 1), (2, 3)], dtype=[('a', int), ('b', int)])
  3160. # w/o mask: each entry is a np.void whose elements are standard Python
  3161. for entry in a:
  3162. for item in entry.tolist():
  3163. assert_(not isinstance(item, np.generic))
  3164. # w/ mask: each entry is a ma.void whose elements should be
  3165. # standard Python
  3166. a.mask[0] = (0, 1)
  3167. for entry in a:
  3168. for item in entry.tolist():
  3169. assert_(not isinstance(item, np.generic))
  3170. def test_toflex(self):
  3171. # Test the conversion to records
  3172. data = arange(10)
  3173. record = data.toflex()
  3174. assert_equal(record['_data'], data._data)
  3175. assert_equal(record['_mask'], data._mask)
  3176. data[[0, 1, 2, -1]] = masked
  3177. record = data.toflex()
  3178. assert_equal(record['_data'], data._data)
  3179. assert_equal(record['_mask'], data._mask)
  3180. ndtype = [('i', int), ('s', '|S3'), ('f', float)]
  3181. data = array([(i, s, f) for (i, s, f) in zip(np.arange(10),
  3182. 'ABCDEFGHIJKLM',
  3183. np.random.rand(10))],
  3184. dtype=ndtype)
  3185. data[[0, 1, 2, -1]] = masked
  3186. record = data.toflex()
  3187. assert_equal(record['_data'], data._data)
  3188. assert_equal(record['_mask'], data._mask)
  3189. ndtype = np.dtype("int, (2,3)float, float")
  3190. data = array([(i, f, ff) for (i, f, ff) in zip(np.arange(10),
  3191. np.random.rand(10),
  3192. np.random.rand(10))],
  3193. dtype=ndtype)
  3194. data[[0, 1, 2, -1]] = masked
  3195. record = data.toflex()
  3196. assert_equal_records(record['_data'], data._data)
  3197. assert_equal_records(record['_mask'], data._mask)
  3198. def test_fromflex(self):
  3199. # Test the reconstruction of a masked_array from a record
  3200. a = array([1, 2, 3])
  3201. test = fromflex(a.toflex())
  3202. assert_equal(test, a)
  3203. assert_equal(test.mask, a.mask)
  3204. a = array([1, 2, 3], mask=[0, 0, 1])
  3205. test = fromflex(a.toflex())
  3206. assert_equal(test, a)
  3207. assert_equal(test.mask, a.mask)
  3208. a = array([(1, 1.), (2, 2.), (3, 3.)], mask=[(1, 0), (0, 0), (0, 1)],
  3209. dtype=[('A', int), ('B', float)])
  3210. test = fromflex(a.toflex())
  3211. assert_equal(test, a)
  3212. assert_equal(test.data, a.data)
  3213. def test_arraymethod(self):
  3214. # Test a _arraymethod w/ n argument
  3215. marray = masked_array([[1, 2, 3, 4, 5]], mask=[0, 0, 1, 0, 0])
  3216. control = masked_array([[1], [2], [3], [4], [5]],
  3217. mask=[0, 0, 1, 0, 0])
  3218. assert_equal(marray.T, control)
  3219. assert_equal(marray.transpose(), control)
  3220. assert_equal(MaskedArray.cumsum(marray.T, 0), control.cumsum(0))
  3221. def test_arraymethod_0d(self):
  3222. # gh-9430
  3223. x = np.ma.array(42, mask=True)
  3224. assert_equal(x.T.mask, x.mask)
  3225. assert_equal(x.T.data, x.data)
  3226. def test_transpose_view(self):
  3227. x = np.ma.array([[1, 2, 3], [4, 5, 6]])
  3228. x[0,1] = np.ma.masked
  3229. xt = x.T
  3230. xt[1,0] = 10
  3231. xt[0,1] = np.ma.masked
  3232. assert_equal(x.data, xt.T.data)
  3233. assert_equal(x.mask, xt.T.mask)
  3234. def test_diagonal_view(self):
  3235. x = np.ma.zeros((3,3))
  3236. x[0,0] = 10
  3237. x[1,1] = np.ma.masked
  3238. x[2,2] = 20
  3239. xd = x.diagonal()
  3240. x[1,1] = 15
  3241. assert_equal(xd.mask, x.diagonal().mask)
  3242. assert_equal(xd.data, x.diagonal().data)
  3243. class TestMaskedArrayMathMethods:
  3244. def setup(self):
  3245. # Base data definition.
  3246. x = np.array([8.375, 7.545, 8.828, 8.5, 1.757, 5.928,
  3247. 8.43, 7.78, 9.865, 5.878, 8.979, 4.732,
  3248. 3.012, 6.022, 5.095, 3.116, 5.238, 3.957,
  3249. 6.04, 9.63, 7.712, 3.382, 4.489, 6.479,
  3250. 7.189, 9.645, 5.395, 4.961, 9.894, 2.893,
  3251. 7.357, 9.828, 6.272, 3.758, 6.693, 0.993])
  3252. X = x.reshape(6, 6)
  3253. XX = x.reshape(3, 2, 2, 3)
  3254. m = np.array([0, 1, 0, 1, 0, 0,
  3255. 1, 0, 1, 1, 0, 1,
  3256. 0, 0, 0, 1, 0, 1,
  3257. 0, 0, 0, 1, 1, 1,
  3258. 1, 0, 0, 1, 0, 0,
  3259. 0, 0, 1, 0, 1, 0])
  3260. mx = array(data=x, mask=m)
  3261. mX = array(data=X, mask=m.reshape(X.shape))
  3262. mXX = array(data=XX, mask=m.reshape(XX.shape))
  3263. m2 = np.array([1, 1, 0, 1, 0, 0,
  3264. 1, 1, 1, 1, 0, 1,
  3265. 0, 0, 1, 1, 0, 1,
  3266. 0, 0, 0, 1, 1, 1,
  3267. 1, 0, 0, 1, 1, 0,
  3268. 0, 0, 1, 0, 1, 1])
  3269. m2x = array(data=x, mask=m2)
  3270. m2X = array(data=X, mask=m2.reshape(X.shape))
  3271. m2XX = array(data=XX, mask=m2.reshape(XX.shape))
  3272. self.d = (x, X, XX, m, mx, mX, mXX, m2x, m2X, m2XX)
  3273. def test_cumsumprod(self):
  3274. # Tests cumsum & cumprod on MaskedArrays.
  3275. (x, X, XX, m, mx, mX, mXX, m2x, m2X, m2XX) = self.d
  3276. mXcp = mX.cumsum(0)
  3277. assert_equal(mXcp._data, mX.filled(0).cumsum(0))
  3278. mXcp = mX.cumsum(1)
  3279. assert_equal(mXcp._data, mX.filled(0).cumsum(1))
  3280. mXcp = mX.cumprod(0)
  3281. assert_equal(mXcp._data, mX.filled(1).cumprod(0))
  3282. mXcp = mX.cumprod(1)
  3283. assert_equal(mXcp._data, mX.filled(1).cumprod(1))
  3284. def test_cumsumprod_with_output(self):
  3285. # Tests cumsum/cumprod w/ output
  3286. xm = array(np.random.uniform(0, 10, 12)).reshape(3, 4)
  3287. xm[:, 0] = xm[0] = xm[-1, -1] = masked
  3288. for funcname in ('cumsum', 'cumprod'):
  3289. npfunc = getattr(np, funcname)
  3290. xmmeth = getattr(xm, funcname)
  3291. # A ndarray as explicit input
  3292. output = np.empty((3, 4), dtype=float)
  3293. output.fill(-9999)
  3294. result = npfunc(xm, axis=0, out=output)
  3295. # ... the result should be the given output
  3296. assert_(result is output)
  3297. assert_equal(result, xmmeth(axis=0, out=output))
  3298. output = empty((3, 4), dtype=int)
  3299. result = xmmeth(axis=0, out=output)
  3300. assert_(result is output)
  3301. def test_ptp(self):
  3302. # Tests ptp on MaskedArrays.
  3303. (x, X, XX, m, mx, mX, mXX, m2x, m2X, m2XX) = self.d
  3304. (n, m) = X.shape
  3305. assert_equal(mx.ptp(), mx.compressed().ptp())
  3306. rows = np.zeros(n, float)
  3307. cols = np.zeros(m, float)
  3308. for k in range(m):
  3309. cols[k] = mX[:, k].compressed().ptp()
  3310. for k in range(n):
  3311. rows[k] = mX[k].compressed().ptp()
  3312. assert_equal(mX.ptp(0), cols)
  3313. assert_equal(mX.ptp(1), rows)
  3314. def test_add_object(self):
  3315. x = masked_array(['a', 'b'], mask=[1, 0], dtype=object)
  3316. y = x + 'x'
  3317. assert_equal(y[1], 'bx')
  3318. assert_(y.mask[0])
  3319. def test_sum_object(self):
  3320. # Test sum on object dtype
  3321. a = masked_array([1, 2, 3], mask=[1, 0, 0], dtype=object)
  3322. assert_equal(a.sum(), 5)
  3323. a = masked_array([[1, 2, 3], [4, 5, 6]], dtype=object)
  3324. assert_equal(a.sum(axis=0), [5, 7, 9])
  3325. def test_prod_object(self):
  3326. # Test prod on object dtype
  3327. a = masked_array([1, 2, 3], mask=[1, 0, 0], dtype=object)
  3328. assert_equal(a.prod(), 2 * 3)
  3329. a = masked_array([[1, 2, 3], [4, 5, 6]], dtype=object)
  3330. assert_equal(a.prod(axis=0), [4, 10, 18])
  3331. def test_meananom_object(self):
  3332. # Test mean/anom on object dtype
  3333. a = masked_array([1, 2, 3], dtype=object)
  3334. assert_equal(a.mean(), 2)
  3335. assert_equal(a.anom(), [-1, 0, 1])
  3336. def test_trace(self):
  3337. # Tests trace on MaskedArrays.
  3338. (x, X, XX, m, mx, mX, mXX, m2x, m2X, m2XX) = self.d
  3339. mXdiag = mX.diagonal()
  3340. assert_equal(mX.trace(), mX.diagonal().compressed().sum())
  3341. assert_almost_equal(mX.trace(),
  3342. X.trace() - sum(mXdiag.mask * X.diagonal(),
  3343. axis=0))
  3344. assert_equal(np.trace(mX), mX.trace())
  3345. # gh-5560
  3346. arr = np.arange(2*4*4).reshape(2,4,4)
  3347. m_arr = np.ma.masked_array(arr, False)
  3348. assert_equal(arr.trace(axis1=1, axis2=2), m_arr.trace(axis1=1, axis2=2))
  3349. def test_dot(self):
  3350. # Tests dot on MaskedArrays.
  3351. (x, X, XX, m, mx, mX, mXX, m2x, m2X, m2XX) = self.d
  3352. fx = mx.filled(0)
  3353. r = mx.dot(mx)
  3354. assert_almost_equal(r.filled(0), fx.dot(fx))
  3355. assert_(r.mask is nomask)
  3356. fX = mX.filled(0)
  3357. r = mX.dot(mX)
  3358. assert_almost_equal(r.filled(0), fX.dot(fX))
  3359. assert_(r.mask[1,3])
  3360. r1 = empty_like(r)
  3361. mX.dot(mX, out=r1)
  3362. assert_almost_equal(r, r1)
  3363. mYY = mXX.swapaxes(-1, -2)
  3364. fXX, fYY = mXX.filled(0), mYY.filled(0)
  3365. r = mXX.dot(mYY)
  3366. assert_almost_equal(r.filled(0), fXX.dot(fYY))
  3367. r1 = empty_like(r)
  3368. mXX.dot(mYY, out=r1)
  3369. assert_almost_equal(r, r1)
  3370. def test_dot_shape_mismatch(self):
  3371. # regression test
  3372. x = masked_array([[1,2],[3,4]], mask=[[0,1],[0,0]])
  3373. y = masked_array([[1,2],[3,4]], mask=[[0,1],[0,0]])
  3374. z = masked_array([[0,1],[3,3]])
  3375. x.dot(y, out=z)
  3376. assert_almost_equal(z.filled(0), [[1, 0], [15, 16]])
  3377. assert_almost_equal(z.mask, [[0, 1], [0, 0]])
  3378. def test_varmean_nomask(self):
  3379. # gh-5769
  3380. foo = array([1,2,3,4], dtype='f8')
  3381. bar = array([1,2,3,4], dtype='f8')
  3382. assert_equal(type(foo.mean()), np.float64)
  3383. assert_equal(type(foo.var()), np.float64)
  3384. assert((foo.mean() == bar.mean()) is np.bool_(True))
  3385. # check array type is preserved and out works
  3386. foo = array(np.arange(16).reshape((4,4)), dtype='f8')
  3387. bar = empty(4, dtype='f4')
  3388. assert_equal(type(foo.mean(axis=1)), MaskedArray)
  3389. assert_equal(type(foo.var(axis=1)), MaskedArray)
  3390. assert_(foo.mean(axis=1, out=bar) is bar)
  3391. assert_(foo.var(axis=1, out=bar) is bar)
  3392. def test_varstd(self):
  3393. # Tests var & std on MaskedArrays.
  3394. (x, X, XX, m, mx, mX, mXX, m2x, m2X, m2XX) = self.d
  3395. assert_almost_equal(mX.var(axis=None), mX.compressed().var())
  3396. assert_almost_equal(mX.std(axis=None), mX.compressed().std())
  3397. assert_almost_equal(mX.std(axis=None, ddof=1),
  3398. mX.compressed().std(ddof=1))
  3399. assert_almost_equal(mX.var(axis=None, ddof=1),
  3400. mX.compressed().var(ddof=1))
  3401. assert_equal(mXX.var(axis=3).shape, XX.var(axis=3).shape)
  3402. assert_equal(mX.var().shape, X.var().shape)
  3403. (mXvar0, mXvar1) = (mX.var(axis=0), mX.var(axis=1))
  3404. assert_almost_equal(mX.var(axis=None, ddof=2),
  3405. mX.compressed().var(ddof=2))
  3406. assert_almost_equal(mX.std(axis=None, ddof=2),
  3407. mX.compressed().std(ddof=2))
  3408. for k in range(6):
  3409. assert_almost_equal(mXvar1[k], mX[k].compressed().var())
  3410. assert_almost_equal(mXvar0[k], mX[:, k].compressed().var())
  3411. assert_almost_equal(np.sqrt(mXvar0[k]),
  3412. mX[:, k].compressed().std())
  3413. @suppress_copy_mask_on_assignment
  3414. def test_varstd_specialcases(self):
  3415. # Test a special case for var
  3416. nout = np.array(-1, dtype=float)
  3417. mout = array(-1, dtype=float)
  3418. x = array(arange(10), mask=True)
  3419. for methodname in ('var', 'std'):
  3420. method = getattr(x, methodname)
  3421. assert_(method() is masked)
  3422. assert_(method(0) is masked)
  3423. assert_(method(-1) is masked)
  3424. # Using a masked array as explicit output
  3425. method(out=mout)
  3426. assert_(mout is not masked)
  3427. assert_equal(mout.mask, True)
  3428. # Using a ndarray as explicit output
  3429. method(out=nout)
  3430. assert_(np.isnan(nout))
  3431. x = array(arange(10), mask=True)
  3432. x[-1] = 9
  3433. for methodname in ('var', 'std'):
  3434. method = getattr(x, methodname)
  3435. assert_(method(ddof=1) is masked)
  3436. assert_(method(0, ddof=1) is masked)
  3437. assert_(method(-1, ddof=1) is masked)
  3438. # Using a masked array as explicit output
  3439. method(out=mout, ddof=1)
  3440. assert_(mout is not masked)
  3441. assert_equal(mout.mask, True)
  3442. # Using a ndarray as explicit output
  3443. method(out=nout, ddof=1)
  3444. assert_(np.isnan(nout))
  3445. def test_varstd_ddof(self):
  3446. a = array([[1, 1, 0], [1, 1, 0]], mask=[[0, 0, 1], [0, 0, 1]])
  3447. test = a.std(axis=0, ddof=0)
  3448. assert_equal(test.filled(0), [0, 0, 0])
  3449. assert_equal(test.mask, [0, 0, 1])
  3450. test = a.std(axis=0, ddof=1)
  3451. assert_equal(test.filled(0), [0, 0, 0])
  3452. assert_equal(test.mask, [0, 0, 1])
  3453. test = a.std(axis=0, ddof=2)
  3454. assert_equal(test.filled(0), [0, 0, 0])
  3455. assert_equal(test.mask, [1, 1, 1])
  3456. def test_diag(self):
  3457. # Test diag
  3458. x = arange(9).reshape((3, 3))
  3459. x[1, 1] = masked
  3460. out = np.diag(x)
  3461. assert_equal(out, [0, 4, 8])
  3462. out = diag(x)
  3463. assert_equal(out, [0, 4, 8])
  3464. assert_equal(out.mask, [0, 1, 0])
  3465. out = diag(out)
  3466. control = array([[0, 0, 0], [0, 4, 0], [0, 0, 8]],
  3467. mask=[[0, 0, 0], [0, 1, 0], [0, 0, 0]])
  3468. assert_equal(out, control)
  3469. def test_axis_methods_nomask(self):
  3470. # Test the combination nomask & methods w/ axis
  3471. a = array([[1, 2, 3], [4, 5, 6]])
  3472. assert_equal(a.sum(0), [5, 7, 9])
  3473. assert_equal(a.sum(-1), [6, 15])
  3474. assert_equal(a.sum(1), [6, 15])
  3475. assert_equal(a.prod(0), [4, 10, 18])
  3476. assert_equal(a.prod(-1), [6, 120])
  3477. assert_equal(a.prod(1), [6, 120])
  3478. assert_equal(a.min(0), [1, 2, 3])
  3479. assert_equal(a.min(-1), [1, 4])
  3480. assert_equal(a.min(1), [1, 4])
  3481. assert_equal(a.max(0), [4, 5, 6])
  3482. assert_equal(a.max(-1), [3, 6])
  3483. assert_equal(a.max(1), [3, 6])
  3484. class TestMaskedArrayMathMethodsComplex:
  3485. # Test class for miscellaneous MaskedArrays methods.
  3486. def setup(self):
  3487. # Base data definition.
  3488. x = np.array([8.375j, 7.545j, 8.828j, 8.5j, 1.757j, 5.928,
  3489. 8.43, 7.78, 9.865, 5.878, 8.979, 4.732,
  3490. 3.012, 6.022, 5.095, 3.116, 5.238, 3.957,
  3491. 6.04, 9.63, 7.712, 3.382, 4.489, 6.479j,
  3492. 7.189j, 9.645, 5.395, 4.961, 9.894, 2.893,
  3493. 7.357, 9.828, 6.272, 3.758, 6.693, 0.993j])
  3494. X = x.reshape(6, 6)
  3495. XX = x.reshape(3, 2, 2, 3)
  3496. m = np.array([0, 1, 0, 1, 0, 0,
  3497. 1, 0, 1, 1, 0, 1,
  3498. 0, 0, 0, 1, 0, 1,
  3499. 0, 0, 0, 1, 1, 1,
  3500. 1, 0, 0, 1, 0, 0,
  3501. 0, 0, 1, 0, 1, 0])
  3502. mx = array(data=x, mask=m)
  3503. mX = array(data=X, mask=m.reshape(X.shape))
  3504. mXX = array(data=XX, mask=m.reshape(XX.shape))
  3505. m2 = np.array([1, 1, 0, 1, 0, 0,
  3506. 1, 1, 1, 1, 0, 1,
  3507. 0, 0, 1, 1, 0, 1,
  3508. 0, 0, 0, 1, 1, 1,
  3509. 1, 0, 0, 1, 1, 0,
  3510. 0, 0, 1, 0, 1, 1])
  3511. m2x = array(data=x, mask=m2)
  3512. m2X = array(data=X, mask=m2.reshape(X.shape))
  3513. m2XX = array(data=XX, mask=m2.reshape(XX.shape))
  3514. self.d = (x, X, XX, m, mx, mX, mXX, m2x, m2X, m2XX)
  3515. def test_varstd(self):
  3516. # Tests var & std on MaskedArrays.
  3517. (x, X, XX, m, mx, mX, mXX, m2x, m2X, m2XX) = self.d
  3518. assert_almost_equal(mX.var(axis=None), mX.compressed().var())
  3519. assert_almost_equal(mX.std(axis=None), mX.compressed().std())
  3520. assert_equal(mXX.var(axis=3).shape, XX.var(axis=3).shape)
  3521. assert_equal(mX.var().shape, X.var().shape)
  3522. (mXvar0, mXvar1) = (mX.var(axis=0), mX.var(axis=1))
  3523. assert_almost_equal(mX.var(axis=None, ddof=2),
  3524. mX.compressed().var(ddof=2))
  3525. assert_almost_equal(mX.std(axis=None, ddof=2),
  3526. mX.compressed().std(ddof=2))
  3527. for k in range(6):
  3528. assert_almost_equal(mXvar1[k], mX[k].compressed().var())
  3529. assert_almost_equal(mXvar0[k], mX[:, k].compressed().var())
  3530. assert_almost_equal(np.sqrt(mXvar0[k]),
  3531. mX[:, k].compressed().std())
  3532. class TestMaskedArrayFunctions:
  3533. # Test class for miscellaneous functions.
  3534. def setup(self):
  3535. x = np.array([1., 1., 1., -2., pi/2.0, 4., 5., -10., 10., 1., 2., 3.])
  3536. y = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.])
  3537. m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
  3538. m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1]
  3539. xm = masked_array(x, mask=m1)
  3540. ym = masked_array(y, mask=m2)
  3541. xm.set_fill_value(1e+20)
  3542. self.info = (xm, ym)
  3543. def test_masked_where_bool(self):
  3544. x = [1, 2]
  3545. y = masked_where(False, x)
  3546. assert_equal(y, [1, 2])
  3547. assert_equal(y[1], 2)
  3548. def test_masked_equal_wlist(self):
  3549. x = [1, 2, 3]
  3550. mx = masked_equal(x, 3)
  3551. assert_equal(mx, x)
  3552. assert_equal(mx._mask, [0, 0, 1])
  3553. mx = masked_not_equal(x, 3)
  3554. assert_equal(mx, x)
  3555. assert_equal(mx._mask, [1, 1, 0])
  3556. def test_masked_equal_fill_value(self):
  3557. x = [1, 2, 3]
  3558. mx = masked_equal(x, 3)
  3559. assert_equal(mx._mask, [0, 0, 1])
  3560. assert_equal(mx.fill_value, 3)
  3561. def test_masked_where_condition(self):
  3562. # Tests masking functions.
  3563. x = array([1., 2., 3., 4., 5.])
  3564. x[2] = masked
  3565. assert_equal(masked_where(greater(x, 2), x), masked_greater(x, 2))
  3566. assert_equal(masked_where(greater_equal(x, 2), x),
  3567. masked_greater_equal(x, 2))
  3568. assert_equal(masked_where(less(x, 2), x), masked_less(x, 2))
  3569. assert_equal(masked_where(less_equal(x, 2), x),
  3570. masked_less_equal(x, 2))
  3571. assert_equal(masked_where(not_equal(x, 2), x), masked_not_equal(x, 2))
  3572. assert_equal(masked_where(equal(x, 2), x), masked_equal(x, 2))
  3573. assert_equal(masked_where(not_equal(x, 2), x), masked_not_equal(x, 2))
  3574. assert_equal(masked_where([1, 1, 0, 0, 0], [1, 2, 3, 4, 5]),
  3575. [99, 99, 3, 4, 5])
  3576. def test_masked_where_oddities(self):
  3577. # Tests some generic features.
  3578. atest = ones((10, 10, 10), dtype=float)
  3579. btest = zeros(atest.shape, MaskType)
  3580. ctest = masked_where(btest, atest)
  3581. assert_equal(atest, ctest)
  3582. def test_masked_where_shape_constraint(self):
  3583. a = arange(10)
  3584. with assert_raises(IndexError):
  3585. masked_equal(1, a)
  3586. test = masked_equal(a, 1)
  3587. assert_equal(test.mask, [0, 1, 0, 0, 0, 0, 0, 0, 0, 0])
  3588. def test_masked_where_structured(self):
  3589. # test that masked_where on a structured array sets a structured
  3590. # mask (see issue #2972)
  3591. a = np.zeros(10, dtype=[("A", "<f2"), ("B", "<f4")])
  3592. am = np.ma.masked_where(a["A"] < 5, a)
  3593. assert_equal(am.mask.dtype.names, am.dtype.names)
  3594. assert_equal(am["A"],
  3595. np.ma.masked_array(np.zeros(10), np.ones(10)))
  3596. def test_masked_where_mismatch(self):
  3597. # gh-4520
  3598. x = np.arange(10)
  3599. y = np.arange(5)
  3600. assert_raises(IndexError, np.ma.masked_where, y > 6, x)
  3601. def test_masked_otherfunctions(self):
  3602. assert_equal(masked_inside(list(range(5)), 1, 3),
  3603. [0, 199, 199, 199, 4])
  3604. assert_equal(masked_outside(list(range(5)), 1, 3), [199, 1, 2, 3, 199])
  3605. assert_equal(masked_inside(array(list(range(5)),
  3606. mask=[1, 0, 0, 0, 0]), 1, 3).mask,
  3607. [1, 1, 1, 1, 0])
  3608. assert_equal(masked_outside(array(list(range(5)),
  3609. mask=[0, 1, 0, 0, 0]), 1, 3).mask,
  3610. [1, 1, 0, 0, 1])
  3611. assert_equal(masked_equal(array(list(range(5)),
  3612. mask=[1, 0, 0, 0, 0]), 2).mask,
  3613. [1, 0, 1, 0, 0])
  3614. assert_equal(masked_not_equal(array([2, 2, 1, 2, 1],
  3615. mask=[1, 0, 0, 0, 0]), 2).mask,
  3616. [1, 0, 1, 0, 1])
  3617. def test_round(self):
  3618. a = array([1.23456, 2.34567, 3.45678, 4.56789, 5.67890],
  3619. mask=[0, 1, 0, 0, 0])
  3620. assert_equal(a.round(), [1., 2., 3., 5., 6.])
  3621. assert_equal(a.round(1), [1.2, 2.3, 3.5, 4.6, 5.7])
  3622. assert_equal(a.round(3), [1.235, 2.346, 3.457, 4.568, 5.679])
  3623. b = empty_like(a)
  3624. a.round(out=b)
  3625. assert_equal(b, [1., 2., 3., 5., 6.])
  3626. x = array([1., 2., 3., 4., 5.])
  3627. c = array([1, 1, 1, 0, 0])
  3628. x[2] = masked
  3629. z = where(c, x, -x)
  3630. assert_equal(z, [1., 2., 0., -4., -5])
  3631. c[0] = masked
  3632. z = where(c, x, -x)
  3633. assert_equal(z, [1., 2., 0., -4., -5])
  3634. assert_(z[0] is masked)
  3635. assert_(z[1] is not masked)
  3636. assert_(z[2] is masked)
  3637. def test_round_with_output(self):
  3638. # Testing round with an explicit output
  3639. xm = array(np.random.uniform(0, 10, 12)).reshape(3, 4)
  3640. xm[:, 0] = xm[0] = xm[-1, -1] = masked
  3641. # A ndarray as explicit input
  3642. output = np.empty((3, 4), dtype=float)
  3643. output.fill(-9999)
  3644. result = np.round(xm, decimals=2, out=output)
  3645. # ... the result should be the given output
  3646. assert_(result is output)
  3647. assert_equal(result, xm.round(decimals=2, out=output))
  3648. output = empty((3, 4), dtype=float)
  3649. result = xm.round(decimals=2, out=output)
  3650. assert_(result is output)
  3651. def test_round_with_scalar(self):
  3652. # Testing round with scalar/zero dimension input
  3653. # GH issue 2244
  3654. a = array(1.1, mask=[False])
  3655. assert_equal(a.round(), 1)
  3656. a = array(1.1, mask=[True])
  3657. assert_(a.round() is masked)
  3658. a = array(1.1, mask=[False])
  3659. output = np.empty(1, dtype=float)
  3660. output.fill(-9999)
  3661. a.round(out=output)
  3662. assert_equal(output, 1)
  3663. a = array(1.1, mask=[False])
  3664. output = array(-9999., mask=[True])
  3665. a.round(out=output)
  3666. assert_equal(output[()], 1)
  3667. a = array(1.1, mask=[True])
  3668. output = array(-9999., mask=[False])
  3669. a.round(out=output)
  3670. assert_(output[()] is masked)
  3671. def test_identity(self):
  3672. a = identity(5)
  3673. assert_(isinstance(a, MaskedArray))
  3674. assert_equal(a, np.identity(5))
  3675. def test_power(self):
  3676. x = -1.1
  3677. assert_almost_equal(power(x, 2.), 1.21)
  3678. assert_(power(x, masked) is masked)
  3679. x = array([-1.1, -1.1, 1.1, 1.1, 0.])
  3680. b = array([0.5, 2., 0.5, 2., -1.], mask=[0, 0, 0, 0, 1])
  3681. y = power(x, b)
  3682. assert_almost_equal(y, [0, 1.21, 1.04880884817, 1.21, 0.])
  3683. assert_equal(y._mask, [1, 0, 0, 0, 1])
  3684. b.mask = nomask
  3685. y = power(x, b)
  3686. assert_equal(y._mask, [1, 0, 0, 0, 1])
  3687. z = x ** b
  3688. assert_equal(z._mask, y._mask)
  3689. assert_almost_equal(z, y)
  3690. assert_almost_equal(z._data, y._data)
  3691. x **= b
  3692. assert_equal(x._mask, y._mask)
  3693. assert_almost_equal(x, y)
  3694. assert_almost_equal(x._data, y._data)
  3695. def test_power_with_broadcasting(self):
  3696. # Test power w/ broadcasting
  3697. a2 = np.array([[1., 2., 3.], [4., 5., 6.]])
  3698. a2m = array(a2, mask=[[1, 0, 0], [0, 0, 1]])
  3699. b1 = np.array([2, 4, 3])
  3700. b2 = np.array([b1, b1])
  3701. b2m = array(b2, mask=[[0, 1, 0], [0, 1, 0]])
  3702. ctrl = array([[1 ** 2, 2 ** 4, 3 ** 3], [4 ** 2, 5 ** 4, 6 ** 3]],
  3703. mask=[[1, 1, 0], [0, 1, 1]])
  3704. # No broadcasting, base & exp w/ mask
  3705. test = a2m ** b2m
  3706. assert_equal(test, ctrl)
  3707. assert_equal(test.mask, ctrl.mask)
  3708. # No broadcasting, base w/ mask, exp w/o mask
  3709. test = a2m ** b2
  3710. assert_equal(test, ctrl)
  3711. assert_equal(test.mask, a2m.mask)
  3712. # No broadcasting, base w/o mask, exp w/ mask
  3713. test = a2 ** b2m
  3714. assert_equal(test, ctrl)
  3715. assert_equal(test.mask, b2m.mask)
  3716. ctrl = array([[2 ** 2, 4 ** 4, 3 ** 3], [2 ** 2, 4 ** 4, 3 ** 3]],
  3717. mask=[[0, 1, 0], [0, 1, 0]])
  3718. test = b1 ** b2m
  3719. assert_equal(test, ctrl)
  3720. assert_equal(test.mask, ctrl.mask)
  3721. test = b2m ** b1
  3722. assert_equal(test, ctrl)
  3723. assert_equal(test.mask, ctrl.mask)
  3724. def test_where(self):
  3725. # Test the where function
  3726. x = np.array([1., 1., 1., -2., pi/2.0, 4., 5., -10., 10., 1., 2., 3.])
  3727. y = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.])
  3728. m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
  3729. m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1]
  3730. xm = masked_array(x, mask=m1)
  3731. ym = masked_array(y, mask=m2)
  3732. xm.set_fill_value(1e+20)
  3733. d = where(xm > 2, xm, -9)
  3734. assert_equal(d, [-9., -9., -9., -9., -9., 4.,
  3735. -9., -9., 10., -9., -9., 3.])
  3736. assert_equal(d._mask, xm._mask)
  3737. d = where(xm > 2, -9, ym)
  3738. assert_equal(d, [5., 0., 3., 2., -1., -9.,
  3739. -9., -10., -9., 1., 0., -9.])
  3740. assert_equal(d._mask, [1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0])
  3741. d = where(xm > 2, xm, masked)
  3742. assert_equal(d, [-9., -9., -9., -9., -9., 4.,
  3743. -9., -9., 10., -9., -9., 3.])
  3744. tmp = xm._mask.copy()
  3745. tmp[(xm <= 2).filled(True)] = True
  3746. assert_equal(d._mask, tmp)
  3747. ixm = xm.astype(int)
  3748. d = where(ixm > 2, ixm, masked)
  3749. assert_equal(d, [-9, -9, -9, -9, -9, 4, -9, -9, 10, -9, -9, 3])
  3750. assert_equal(d.dtype, ixm.dtype)
  3751. def test_where_object(self):
  3752. a = np.array(None)
  3753. b = masked_array(None)
  3754. r = b.copy()
  3755. assert_equal(np.ma.where(True, a, a), r)
  3756. assert_equal(np.ma.where(True, b, b), r)
  3757. def test_where_with_masked_choice(self):
  3758. x = arange(10)
  3759. x[3] = masked
  3760. c = x >= 8
  3761. # Set False to masked
  3762. z = where(c, x, masked)
  3763. assert_(z.dtype is x.dtype)
  3764. assert_(z[3] is masked)
  3765. assert_(z[4] is masked)
  3766. assert_(z[7] is masked)
  3767. assert_(z[8] is not masked)
  3768. assert_(z[9] is not masked)
  3769. assert_equal(x, z)
  3770. # Set True to masked
  3771. z = where(c, masked, x)
  3772. assert_(z.dtype is x.dtype)
  3773. assert_(z[3] is masked)
  3774. assert_(z[4] is not masked)
  3775. assert_(z[7] is not masked)
  3776. assert_(z[8] is masked)
  3777. assert_(z[9] is masked)
  3778. def test_where_with_masked_condition(self):
  3779. x = array([1., 2., 3., 4., 5.])
  3780. c = array([1, 1, 1, 0, 0])
  3781. x[2] = masked
  3782. z = where(c, x, -x)
  3783. assert_equal(z, [1., 2., 0., -4., -5])
  3784. c[0] = masked
  3785. z = where(c, x, -x)
  3786. assert_equal(z, [1., 2., 0., -4., -5])
  3787. assert_(z[0] is masked)
  3788. assert_(z[1] is not masked)
  3789. assert_(z[2] is masked)
  3790. x = arange(1, 6)
  3791. x[-1] = masked
  3792. y = arange(1, 6) * 10
  3793. y[2] = masked
  3794. c = array([1, 1, 1, 0, 0], mask=[1, 0, 0, 0, 0])
  3795. cm = c.filled(1)
  3796. z = where(c, x, y)
  3797. zm = where(cm, x, y)
  3798. assert_equal(z, zm)
  3799. assert_(getmask(zm) is nomask)
  3800. assert_equal(zm, [1, 2, 3, 40, 50])
  3801. z = where(c, masked, 1)
  3802. assert_equal(z, [99, 99, 99, 1, 1])
  3803. z = where(c, 1, masked)
  3804. assert_equal(z, [99, 1, 1, 99, 99])
  3805. def test_where_type(self):
  3806. # Test the type conservation with where
  3807. x = np.arange(4, dtype=np.int32)
  3808. y = np.arange(4, dtype=np.float32) * 2.2
  3809. test = where(x > 1.5, y, x).dtype
  3810. control = np.find_common_type([np.int32, np.float32], [])
  3811. assert_equal(test, control)
  3812. def test_where_broadcast(self):
  3813. # Issue 8599
  3814. x = np.arange(9).reshape(3, 3)
  3815. y = np.zeros(3)
  3816. core = np.where([1, 0, 1], x, y)
  3817. ma = where([1, 0, 1], x, y)
  3818. assert_equal(core, ma)
  3819. assert_equal(core.dtype, ma.dtype)
  3820. def test_where_structured(self):
  3821. # Issue 8600
  3822. dt = np.dtype([('a', int), ('b', int)])
  3823. x = np.array([(1, 2), (3, 4), (5, 6)], dtype=dt)
  3824. y = np.array((10, 20), dtype=dt)
  3825. core = np.where([0, 1, 1], x, y)
  3826. ma = np.where([0, 1, 1], x, y)
  3827. assert_equal(core, ma)
  3828. assert_equal(core.dtype, ma.dtype)
  3829. def test_where_structured_masked(self):
  3830. dt = np.dtype([('a', int), ('b', int)])
  3831. x = np.array([(1, 2), (3, 4), (5, 6)], dtype=dt)
  3832. ma = where([0, 1, 1], x, masked)
  3833. expected = masked_where([1, 0, 0], x)
  3834. assert_equal(ma.dtype, expected.dtype)
  3835. assert_equal(ma, expected)
  3836. assert_equal(ma.mask, expected.mask)
  3837. def test_choose(self):
  3838. # Test choose
  3839. choices = [[0, 1, 2, 3], [10, 11, 12, 13],
  3840. [20, 21, 22, 23], [30, 31, 32, 33]]
  3841. chosen = choose([2, 3, 1, 0], choices)
  3842. assert_equal(chosen, array([20, 31, 12, 3]))
  3843. chosen = choose([2, 4, 1, 0], choices, mode='clip')
  3844. assert_equal(chosen, array([20, 31, 12, 3]))
  3845. chosen = choose([2, 4, 1, 0], choices, mode='wrap')
  3846. assert_equal(chosen, array([20, 1, 12, 3]))
  3847. # Check with some masked indices
  3848. indices_ = array([2, 4, 1, 0], mask=[1, 0, 0, 1])
  3849. chosen = choose(indices_, choices, mode='wrap')
  3850. assert_equal(chosen, array([99, 1, 12, 99]))
  3851. assert_equal(chosen.mask, [1, 0, 0, 1])
  3852. # Check with some masked choices
  3853. choices = array(choices, mask=[[0, 0, 0, 1], [1, 1, 0, 1],
  3854. [1, 0, 0, 0], [0, 0, 0, 0]])
  3855. indices_ = [2, 3, 1, 0]
  3856. chosen = choose(indices_, choices, mode='wrap')
  3857. assert_equal(chosen, array([20, 31, 12, 3]))
  3858. assert_equal(chosen.mask, [1, 0, 0, 1])
  3859. def test_choose_with_out(self):
  3860. # Test choose with an explicit out keyword
  3861. choices = [[0, 1, 2, 3], [10, 11, 12, 13],
  3862. [20, 21, 22, 23], [30, 31, 32, 33]]
  3863. store = empty(4, dtype=int)
  3864. chosen = choose([2, 3, 1, 0], choices, out=store)
  3865. assert_equal(store, array([20, 31, 12, 3]))
  3866. assert_(store is chosen)
  3867. # Check with some masked indices + out
  3868. store = empty(4, dtype=int)
  3869. indices_ = array([2, 3, 1, 0], mask=[1, 0, 0, 1])
  3870. chosen = choose(indices_, choices, mode='wrap', out=store)
  3871. assert_equal(store, array([99, 31, 12, 99]))
  3872. assert_equal(store.mask, [1, 0, 0, 1])
  3873. # Check with some masked choices + out ina ndarray !
  3874. choices = array(choices, mask=[[0, 0, 0, 1], [1, 1, 0, 1],
  3875. [1, 0, 0, 0], [0, 0, 0, 0]])
  3876. indices_ = [2, 3, 1, 0]
  3877. store = empty(4, dtype=int).view(ndarray)
  3878. chosen = choose(indices_, choices, mode='wrap', out=store)
  3879. assert_equal(store, array([999999, 31, 12, 999999]))
  3880. def test_reshape(self):
  3881. a = arange(10)
  3882. a[0] = masked
  3883. # Try the default
  3884. b = a.reshape((5, 2))
  3885. assert_equal(b.shape, (5, 2))
  3886. assert_(b.flags['C'])
  3887. # Try w/ arguments as list instead of tuple
  3888. b = a.reshape(5, 2)
  3889. assert_equal(b.shape, (5, 2))
  3890. assert_(b.flags['C'])
  3891. # Try w/ order
  3892. b = a.reshape((5, 2), order='F')
  3893. assert_equal(b.shape, (5, 2))
  3894. assert_(b.flags['F'])
  3895. # Try w/ order
  3896. b = a.reshape(5, 2, order='F')
  3897. assert_equal(b.shape, (5, 2))
  3898. assert_(b.flags['F'])
  3899. c = np.reshape(a, (2, 5))
  3900. assert_(isinstance(c, MaskedArray))
  3901. assert_equal(c.shape, (2, 5))
  3902. assert_(c[0, 0] is masked)
  3903. assert_(c.flags['C'])
  3904. def test_make_mask_descr(self):
  3905. # Flexible
  3906. ntype = [('a', float), ('b', float)]
  3907. test = make_mask_descr(ntype)
  3908. assert_equal(test, [('a', bool), ('b', bool)])
  3909. assert_(test is make_mask_descr(test))
  3910. # Standard w/ shape
  3911. ntype = (float, 2)
  3912. test = make_mask_descr(ntype)
  3913. assert_equal(test, (bool, 2))
  3914. assert_(test is make_mask_descr(test))
  3915. # Standard standard
  3916. ntype = float
  3917. test = make_mask_descr(ntype)
  3918. assert_equal(test, np.dtype(bool))
  3919. assert_(test is make_mask_descr(test))
  3920. # Nested
  3921. ntype = [('a', float), ('b', [('ba', float), ('bb', float)])]
  3922. test = make_mask_descr(ntype)
  3923. control = np.dtype([('a', 'b1'), ('b', [('ba', 'b1'), ('bb', 'b1')])])
  3924. assert_equal(test, control)
  3925. assert_(test is make_mask_descr(test))
  3926. # Named+ shape
  3927. ntype = [('a', (float, 2))]
  3928. test = make_mask_descr(ntype)
  3929. assert_equal(test, np.dtype([('a', (bool, 2))]))
  3930. assert_(test is make_mask_descr(test))
  3931. # 2 names
  3932. ntype = [(('A', 'a'), float)]
  3933. test = make_mask_descr(ntype)
  3934. assert_equal(test, np.dtype([(('A', 'a'), bool)]))
  3935. assert_(test is make_mask_descr(test))
  3936. # nested boolean types should preserve identity
  3937. base_type = np.dtype([('a', int, 3)])
  3938. base_mtype = make_mask_descr(base_type)
  3939. sub_type = np.dtype([('a', int), ('b', base_mtype)])
  3940. test = make_mask_descr(sub_type)
  3941. assert_equal(test, np.dtype([('a', bool), ('b', [('a', bool, 3)])]))
  3942. assert_(test.fields['b'][0] is base_mtype)
  3943. def test_make_mask(self):
  3944. # Test make_mask
  3945. # w/ a list as an input
  3946. mask = [0, 1]
  3947. test = make_mask(mask)
  3948. assert_equal(test.dtype, MaskType)
  3949. assert_equal(test, [0, 1])
  3950. # w/ a ndarray as an input
  3951. mask = np.array([0, 1], dtype=bool)
  3952. test = make_mask(mask)
  3953. assert_equal(test.dtype, MaskType)
  3954. assert_equal(test, [0, 1])
  3955. # w/ a flexible-type ndarray as an input - use default
  3956. mdtype = [('a', bool), ('b', bool)]
  3957. mask = np.array([(0, 0), (0, 1)], dtype=mdtype)
  3958. test = make_mask(mask)
  3959. assert_equal(test.dtype, MaskType)
  3960. assert_equal(test, [1, 1])
  3961. # w/ a flexible-type ndarray as an input - use input dtype
  3962. mdtype = [('a', bool), ('b', bool)]
  3963. mask = np.array([(0, 0), (0, 1)], dtype=mdtype)
  3964. test = make_mask(mask, dtype=mask.dtype)
  3965. assert_equal(test.dtype, mdtype)
  3966. assert_equal(test, mask)
  3967. # w/ a flexible-type ndarray as an input - use input dtype
  3968. mdtype = [('a', float), ('b', float)]
  3969. bdtype = [('a', bool), ('b', bool)]
  3970. mask = np.array([(0, 0), (0, 1)], dtype=mdtype)
  3971. test = make_mask(mask, dtype=mask.dtype)
  3972. assert_equal(test.dtype, bdtype)
  3973. assert_equal(test, np.array([(0, 0), (0, 1)], dtype=bdtype))
  3974. # Ensure this also works for void
  3975. mask = np.array((False, True), dtype='?,?')[()]
  3976. assert_(isinstance(mask, np.void))
  3977. test = make_mask(mask, dtype=mask.dtype)
  3978. assert_equal(test, mask)
  3979. assert_(test is not mask)
  3980. mask = np.array((0, 1), dtype='i4,i4')[()]
  3981. test2 = make_mask(mask, dtype=mask.dtype)
  3982. assert_equal(test2, test)
  3983. # test that nomask is returned when m is nomask.
  3984. bools = [True, False]
  3985. dtypes = [MaskType, float]
  3986. msgformat = 'copy=%s, shrink=%s, dtype=%s'
  3987. for cpy, shr, dt in itertools.product(bools, bools, dtypes):
  3988. res = make_mask(nomask, copy=cpy, shrink=shr, dtype=dt)
  3989. assert_(res is nomask, msgformat % (cpy, shr, dt))
  3990. def test_mask_or(self):
  3991. # Initialize
  3992. mtype = [('a', bool), ('b', bool)]
  3993. mask = np.array([(0, 0), (0, 1), (1, 0), (0, 0)], dtype=mtype)
  3994. # Test using nomask as input
  3995. test = mask_or(mask, nomask)
  3996. assert_equal(test, mask)
  3997. test = mask_or(nomask, mask)
  3998. assert_equal(test, mask)
  3999. # Using False as input
  4000. test = mask_or(mask, False)
  4001. assert_equal(test, mask)
  4002. # Using another array w / the same dtype
  4003. other = np.array([(0, 1), (0, 1), (0, 1), (0, 1)], dtype=mtype)
  4004. test = mask_or(mask, other)
  4005. control = np.array([(0, 1), (0, 1), (1, 1), (0, 1)], dtype=mtype)
  4006. assert_equal(test, control)
  4007. # Using another array w / a different dtype
  4008. othertype = [('A', bool), ('B', bool)]
  4009. other = np.array([(0, 1), (0, 1), (0, 1), (0, 1)], dtype=othertype)
  4010. try:
  4011. test = mask_or(mask, other)
  4012. except ValueError:
  4013. pass
  4014. # Using nested arrays
  4015. dtype = [('a', bool), ('b', [('ba', bool), ('bb', bool)])]
  4016. amask = np.array([(0, (1, 0)), (0, (1, 0))], dtype=dtype)
  4017. bmask = np.array([(1, (0, 1)), (0, (0, 0))], dtype=dtype)
  4018. cntrl = np.array([(1, (1, 1)), (0, (1, 0))], dtype=dtype)
  4019. assert_equal(mask_or(amask, bmask), cntrl)
  4020. def test_flatten_mask(self):
  4021. # Tests flatten mask
  4022. # Standard dtype
  4023. mask = np.array([0, 0, 1], dtype=bool)
  4024. assert_equal(flatten_mask(mask), mask)
  4025. # Flexible dtype
  4026. mask = np.array([(0, 0), (0, 1)], dtype=[('a', bool), ('b', bool)])
  4027. test = flatten_mask(mask)
  4028. control = np.array([0, 0, 0, 1], dtype=bool)
  4029. assert_equal(test, control)
  4030. mdtype = [('a', bool), ('b', [('ba', bool), ('bb', bool)])]
  4031. data = [(0, (0, 0)), (0, (0, 1))]
  4032. mask = np.array(data, dtype=mdtype)
  4033. test = flatten_mask(mask)
  4034. control = np.array([0, 0, 0, 0, 0, 1], dtype=bool)
  4035. assert_equal(test, control)
  4036. def test_on_ndarray(self):
  4037. # Test functions on ndarrays
  4038. a = np.array([1, 2, 3, 4])
  4039. m = array(a, mask=False)
  4040. test = anom(a)
  4041. assert_equal(test, m.anom())
  4042. test = reshape(a, (2, 2))
  4043. assert_equal(test, m.reshape(2, 2))
  4044. def test_compress(self):
  4045. # Test compress function on ndarray and masked array
  4046. # Address Github #2495.
  4047. arr = np.arange(8)
  4048. arr.shape = 4, 2
  4049. cond = np.array([True, False, True, True])
  4050. control = arr[[0, 2, 3]]
  4051. test = np.ma.compress(cond, arr, axis=0)
  4052. assert_equal(test, control)
  4053. marr = np.ma.array(arr)
  4054. test = np.ma.compress(cond, marr, axis=0)
  4055. assert_equal(test, control)
  4056. def test_compressed(self):
  4057. # Test ma.compressed function.
  4058. # Address gh-4026
  4059. a = np.ma.array([1, 2])
  4060. test = np.ma.compressed(a)
  4061. assert_(type(test) is np.ndarray)
  4062. # Test case when input data is ndarray subclass
  4063. class A(np.ndarray):
  4064. pass
  4065. a = np.ma.array(A(shape=0))
  4066. test = np.ma.compressed(a)
  4067. assert_(type(test) is A)
  4068. # Test that compress flattens
  4069. test = np.ma.compressed([[1],[2]])
  4070. assert_equal(test.ndim, 1)
  4071. test = np.ma.compressed([[[[[1]]]]])
  4072. assert_equal(test.ndim, 1)
  4073. # Test case when input is MaskedArray subclass
  4074. class M(MaskedArray):
  4075. pass
  4076. test = np.ma.compressed(M([[[]], [[]]]))
  4077. assert_equal(test.ndim, 1)
  4078. # with .compressed() overridden
  4079. class M(MaskedArray):
  4080. def compressed(self):
  4081. return 42
  4082. test = np.ma.compressed(M([[[]], [[]]]))
  4083. assert_equal(test, 42)
  4084. def test_convolve(self):
  4085. a = masked_equal(np.arange(5), 2)
  4086. b = np.array([1, 1])
  4087. test = np.ma.convolve(a, b)
  4088. assert_equal(test, masked_equal([0, 1, -1, -1, 7, 4], -1))
  4089. test = np.ma.convolve(a, b, propagate_mask=False)
  4090. assert_equal(test, masked_equal([0, 1, 1, 3, 7, 4], -1))
  4091. test = np.ma.convolve([1, 1], [1, 1, 1])
  4092. assert_equal(test, masked_equal([1, 2, 2, 1], -1))
  4093. a = [1, 1]
  4094. b = masked_equal([1, -1, -1, 1], -1)
  4095. test = np.ma.convolve(a, b, propagate_mask=False)
  4096. assert_equal(test, masked_equal([1, 1, -1, 1, 1], -1))
  4097. test = np.ma.convolve(a, b, propagate_mask=True)
  4098. assert_equal(test, masked_equal([-1, -1, -1, -1, -1], -1))
  4099. class TestMaskedFields:
  4100. def setup(self):
  4101. ilist = [1, 2, 3, 4, 5]
  4102. flist = [1.1, 2.2, 3.3, 4.4, 5.5]
  4103. slist = ['one', 'two', 'three', 'four', 'five']
  4104. ddtype = [('a', int), ('b', float), ('c', '|S8')]
  4105. mdtype = [('a', bool), ('b', bool), ('c', bool)]
  4106. mask = [0, 1, 0, 0, 1]
  4107. base = array(list(zip(ilist, flist, slist)), mask=mask, dtype=ddtype)
  4108. self.data = dict(base=base, mask=mask, ddtype=ddtype, mdtype=mdtype)
  4109. def test_set_records_masks(self):
  4110. base = self.data['base']
  4111. mdtype = self.data['mdtype']
  4112. # Set w/ nomask or masked
  4113. base.mask = nomask
  4114. assert_equal_records(base._mask, np.zeros(base.shape, dtype=mdtype))
  4115. base.mask = masked
  4116. assert_equal_records(base._mask, np.ones(base.shape, dtype=mdtype))
  4117. # Set w/ simple boolean
  4118. base.mask = False
  4119. assert_equal_records(base._mask, np.zeros(base.shape, dtype=mdtype))
  4120. base.mask = True
  4121. assert_equal_records(base._mask, np.ones(base.shape, dtype=mdtype))
  4122. # Set w/ list
  4123. base.mask = [0, 0, 0, 1, 1]
  4124. assert_equal_records(base._mask,
  4125. np.array([(x, x, x) for x in [0, 0, 0, 1, 1]],
  4126. dtype=mdtype))
  4127. def test_set_record_element(self):
  4128. # Check setting an element of a record)
  4129. base = self.data['base']
  4130. (base_a, base_b, base_c) = (base['a'], base['b'], base['c'])
  4131. base[0] = (pi, pi, 'pi')
  4132. assert_equal(base_a.dtype, int)
  4133. assert_equal(base_a._data, [3, 2, 3, 4, 5])
  4134. assert_equal(base_b.dtype, float)
  4135. assert_equal(base_b._data, [pi, 2.2, 3.3, 4.4, 5.5])
  4136. assert_equal(base_c.dtype, '|S8')
  4137. assert_equal(base_c._data,
  4138. [b'pi', b'two', b'three', b'four', b'five'])
  4139. def test_set_record_slice(self):
  4140. base = self.data['base']
  4141. (base_a, base_b, base_c) = (base['a'], base['b'], base['c'])
  4142. base[:3] = (pi, pi, 'pi')
  4143. assert_equal(base_a.dtype, int)
  4144. assert_equal(base_a._data, [3, 3, 3, 4, 5])
  4145. assert_equal(base_b.dtype, float)
  4146. assert_equal(base_b._data, [pi, pi, pi, 4.4, 5.5])
  4147. assert_equal(base_c.dtype, '|S8')
  4148. assert_equal(base_c._data,
  4149. [b'pi', b'pi', b'pi', b'four', b'five'])
  4150. def test_mask_element(self):
  4151. "Check record access"
  4152. base = self.data['base']
  4153. base[0] = masked
  4154. for n in ('a', 'b', 'c'):
  4155. assert_equal(base[n].mask, [1, 1, 0, 0, 1])
  4156. assert_equal(base[n]._data, base._data[n])
  4157. def test_getmaskarray(self):
  4158. # Test getmaskarray on flexible dtype
  4159. ndtype = [('a', int), ('b', float)]
  4160. test = empty(3, dtype=ndtype)
  4161. assert_equal(getmaskarray(test),
  4162. np.array([(0, 0), (0, 0), (0, 0)],
  4163. dtype=[('a', '|b1'), ('b', '|b1')]))
  4164. test[:] = masked
  4165. assert_equal(getmaskarray(test),
  4166. np.array([(1, 1), (1, 1), (1, 1)],
  4167. dtype=[('a', '|b1'), ('b', '|b1')]))
  4168. def test_view(self):
  4169. # Test view w/ flexible dtype
  4170. iterator = list(zip(np.arange(10), np.random.rand(10)))
  4171. data = np.array(iterator)
  4172. a = array(iterator, dtype=[('a', float), ('b', float)])
  4173. a.mask[0] = (1, 0)
  4174. controlmask = np.array([1] + 19 * [0], dtype=bool)
  4175. # Transform globally to simple dtype
  4176. test = a.view(float)
  4177. assert_equal(test, data.ravel())
  4178. assert_equal(test.mask, controlmask)
  4179. # Transform globally to dty
  4180. test = a.view((float, 2))
  4181. assert_equal(test, data)
  4182. assert_equal(test.mask, controlmask.reshape(-1, 2))
  4183. def test_getitem(self):
  4184. ndtype = [('a', float), ('b', float)]
  4185. a = array(list(zip(np.random.rand(10), np.arange(10))), dtype=ndtype)
  4186. a.mask = np.array(list(zip([0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
  4187. [1, 0, 0, 0, 0, 0, 0, 0, 1, 0])),
  4188. dtype=[('a', bool), ('b', bool)])
  4189. def _test_index(i):
  4190. assert_equal(type(a[i]), mvoid)
  4191. assert_equal_records(a[i]._data, a._data[i])
  4192. assert_equal_records(a[i]._mask, a._mask[i])
  4193. assert_equal(type(a[i, ...]), MaskedArray)
  4194. assert_equal_records(a[i,...]._data, a._data[i,...])
  4195. assert_equal_records(a[i,...]._mask, a._mask[i,...])
  4196. _test_index(1) # No mask
  4197. _test_index(0) # One element masked
  4198. _test_index(-2) # All element masked
  4199. def test_setitem(self):
  4200. # Issue 4866: check that one can set individual items in [record][col]
  4201. # and [col][record] order
  4202. ndtype = np.dtype([('a', float), ('b', int)])
  4203. ma = np.ma.MaskedArray([(1.0, 1), (2.0, 2)], dtype=ndtype)
  4204. ma['a'][1] = 3.0
  4205. assert_equal(ma['a'], np.array([1.0, 3.0]))
  4206. ma[1]['a'] = 4.0
  4207. assert_equal(ma['a'], np.array([1.0, 4.0]))
  4208. # Issue 2403
  4209. mdtype = np.dtype([('a', bool), ('b', bool)])
  4210. # soft mask
  4211. control = np.array([(False, True), (True, True)], dtype=mdtype)
  4212. a = np.ma.masked_all((2,), dtype=ndtype)
  4213. a['a'][0] = 2
  4214. assert_equal(a.mask, control)
  4215. a = np.ma.masked_all((2,), dtype=ndtype)
  4216. a[0]['a'] = 2
  4217. assert_equal(a.mask, control)
  4218. # hard mask
  4219. control = np.array([(True, True), (True, True)], dtype=mdtype)
  4220. a = np.ma.masked_all((2,), dtype=ndtype)
  4221. a.harden_mask()
  4222. a['a'][0] = 2
  4223. assert_equal(a.mask, control)
  4224. a = np.ma.masked_all((2,), dtype=ndtype)
  4225. a.harden_mask()
  4226. a[0]['a'] = 2
  4227. assert_equal(a.mask, control)
  4228. def test_setitem_scalar(self):
  4229. # 8510
  4230. mask_0d = np.ma.masked_array(1, mask=True)
  4231. arr = np.ma.arange(3)
  4232. arr[0] = mask_0d
  4233. assert_array_equal(arr.mask, [True, False, False])
  4234. def test_element_len(self):
  4235. # check that len() works for mvoid (Github issue #576)
  4236. for rec in self.data['base']:
  4237. assert_equal(len(rec), len(self.data['ddtype']))
  4238. class TestMaskedObjectArray:
  4239. def test_getitem(self):
  4240. arr = np.ma.array([None, None])
  4241. for dt in [float, object]:
  4242. a0 = np.eye(2).astype(dt)
  4243. a1 = np.eye(3).astype(dt)
  4244. arr[0] = a0
  4245. arr[1] = a1
  4246. assert_(arr[0] is a0)
  4247. assert_(arr[1] is a1)
  4248. assert_(isinstance(arr[0,...], MaskedArray))
  4249. assert_(isinstance(arr[1,...], MaskedArray))
  4250. assert_(arr[0,...][()] is a0)
  4251. assert_(arr[1,...][()] is a1)
  4252. arr[0] = np.ma.masked
  4253. assert_(arr[1] is a1)
  4254. assert_(isinstance(arr[0,...], MaskedArray))
  4255. assert_(isinstance(arr[1,...], MaskedArray))
  4256. assert_equal(arr[0,...].mask, True)
  4257. assert_(arr[1,...][()] is a1)
  4258. # gh-5962 - object arrays of arrays do something special
  4259. assert_equal(arr[0].data, a0)
  4260. assert_equal(arr[0].mask, True)
  4261. assert_equal(arr[0,...][()].data, a0)
  4262. assert_equal(arr[0,...][()].mask, True)
  4263. def test_nested_ma(self):
  4264. arr = np.ma.array([None, None])
  4265. # set the first object to be an unmasked masked constant. A little fiddly
  4266. arr[0,...] = np.array([np.ma.masked], object)[0,...]
  4267. # check the above line did what we were aiming for
  4268. assert_(arr.data[0] is np.ma.masked)
  4269. # test that getitem returned the value by identity
  4270. assert_(arr[0] is np.ma.masked)
  4271. # now mask the masked value!
  4272. arr[0] = np.ma.masked
  4273. assert_(arr[0] is np.ma.masked)
  4274. class TestMaskedView:
  4275. def setup(self):
  4276. iterator = list(zip(np.arange(10), np.random.rand(10)))
  4277. data = np.array(iterator)
  4278. a = array(iterator, dtype=[('a', float), ('b', float)])
  4279. a.mask[0] = (1, 0)
  4280. controlmask = np.array([1] + 19 * [0], dtype=bool)
  4281. self.data = (data, a, controlmask)
  4282. def test_view_to_nothing(self):
  4283. (data, a, controlmask) = self.data
  4284. test = a.view()
  4285. assert_(isinstance(test, MaskedArray))
  4286. assert_equal(test._data, a._data)
  4287. assert_equal(test._mask, a._mask)
  4288. def test_view_to_type(self):
  4289. (data, a, controlmask) = self.data
  4290. test = a.view(np.ndarray)
  4291. assert_(not isinstance(test, MaskedArray))
  4292. assert_equal(test, a._data)
  4293. assert_equal_records(test, data.view(a.dtype).squeeze())
  4294. def test_view_to_simple_dtype(self):
  4295. (data, a, controlmask) = self.data
  4296. # View globally
  4297. test = a.view(float)
  4298. assert_(isinstance(test, MaskedArray))
  4299. assert_equal(test, data.ravel())
  4300. assert_equal(test.mask, controlmask)
  4301. def test_view_to_flexible_dtype(self):
  4302. (data, a, controlmask) = self.data
  4303. test = a.view([('A', float), ('B', float)])
  4304. assert_equal(test.mask.dtype.names, ('A', 'B'))
  4305. assert_equal(test['A'], a['a'])
  4306. assert_equal(test['B'], a['b'])
  4307. test = a[0].view([('A', float), ('B', float)])
  4308. assert_(isinstance(test, MaskedArray))
  4309. assert_equal(test.mask.dtype.names, ('A', 'B'))
  4310. assert_equal(test['A'], a['a'][0])
  4311. assert_equal(test['B'], a['b'][0])
  4312. test = a[-1].view([('A', float), ('B', float)])
  4313. assert_(isinstance(test, MaskedArray))
  4314. assert_equal(test.dtype.names, ('A', 'B'))
  4315. assert_equal(test['A'], a['a'][-1])
  4316. assert_equal(test['B'], a['b'][-1])
  4317. def test_view_to_subdtype(self):
  4318. (data, a, controlmask) = self.data
  4319. # View globally
  4320. test = a.view((float, 2))
  4321. assert_(isinstance(test, MaskedArray))
  4322. assert_equal(test, data)
  4323. assert_equal(test.mask, controlmask.reshape(-1, 2))
  4324. # View on 1 masked element
  4325. test = a[0].view((float, 2))
  4326. assert_(isinstance(test, MaskedArray))
  4327. assert_equal(test, data[0])
  4328. assert_equal(test.mask, (1, 0))
  4329. # View on 1 unmasked element
  4330. test = a[-1].view((float, 2))
  4331. assert_(isinstance(test, MaskedArray))
  4332. assert_equal(test, data[-1])
  4333. def test_view_to_dtype_and_type(self):
  4334. (data, a, controlmask) = self.data
  4335. test = a.view((float, 2), np.recarray)
  4336. assert_equal(test, data)
  4337. assert_(isinstance(test, np.recarray))
  4338. assert_(not isinstance(test, MaskedArray))
  4339. class TestOptionalArgs:
  4340. def test_ndarrayfuncs(self):
  4341. # test axis arg behaves the same as ndarray (including multiple axes)
  4342. d = np.arange(24.0).reshape((2,3,4))
  4343. m = np.zeros(24, dtype=bool).reshape((2,3,4))
  4344. # mask out last element of last dimension
  4345. m[:,:,-1] = True
  4346. a = np.ma.array(d, mask=m)
  4347. def testaxis(f, a, d):
  4348. numpy_f = numpy.__getattribute__(f)
  4349. ma_f = np.ma.__getattribute__(f)
  4350. # test axis arg
  4351. assert_equal(ma_f(a, axis=1)[...,:-1], numpy_f(d[...,:-1], axis=1))
  4352. assert_equal(ma_f(a, axis=(0,1))[...,:-1],
  4353. numpy_f(d[...,:-1], axis=(0,1)))
  4354. def testkeepdims(f, a, d):
  4355. numpy_f = numpy.__getattribute__(f)
  4356. ma_f = np.ma.__getattribute__(f)
  4357. # test keepdims arg
  4358. assert_equal(ma_f(a, keepdims=True).shape,
  4359. numpy_f(d, keepdims=True).shape)
  4360. assert_equal(ma_f(a, keepdims=False).shape,
  4361. numpy_f(d, keepdims=False).shape)
  4362. # test both at once
  4363. assert_equal(ma_f(a, axis=1, keepdims=True)[...,:-1],
  4364. numpy_f(d[...,:-1], axis=1, keepdims=True))
  4365. assert_equal(ma_f(a, axis=(0,1), keepdims=True)[...,:-1],
  4366. numpy_f(d[...,:-1], axis=(0,1), keepdims=True))
  4367. for f in ['sum', 'prod', 'mean', 'var', 'std']:
  4368. testaxis(f, a, d)
  4369. testkeepdims(f, a, d)
  4370. for f in ['min', 'max']:
  4371. testaxis(f, a, d)
  4372. d = (np.arange(24).reshape((2,3,4))%2 == 0)
  4373. a = np.ma.array(d, mask=m)
  4374. for f in ['all', 'any']:
  4375. testaxis(f, a, d)
  4376. testkeepdims(f, a, d)
  4377. def test_count(self):
  4378. # test np.ma.count specially
  4379. d = np.arange(24.0).reshape((2,3,4))
  4380. m = np.zeros(24, dtype=bool).reshape((2,3,4))
  4381. m[:,0,:] = True
  4382. a = np.ma.array(d, mask=m)
  4383. assert_equal(count(a), 16)
  4384. assert_equal(count(a, axis=1), 2*ones((2,4)))
  4385. assert_equal(count(a, axis=(0,1)), 4*ones((4,)))
  4386. assert_equal(count(a, keepdims=True), 16*ones((1,1,1)))
  4387. assert_equal(count(a, axis=1, keepdims=True), 2*ones((2,1,4)))
  4388. assert_equal(count(a, axis=(0,1), keepdims=True), 4*ones((1,1,4)))
  4389. assert_equal(count(a, axis=-2), 2*ones((2,4)))
  4390. assert_raises(ValueError, count, a, axis=(1,1))
  4391. assert_raises(np.AxisError, count, a, axis=3)
  4392. # check the 'nomask' path
  4393. a = np.ma.array(d, mask=nomask)
  4394. assert_equal(count(a), 24)
  4395. assert_equal(count(a, axis=1), 3*ones((2,4)))
  4396. assert_equal(count(a, axis=(0,1)), 6*ones((4,)))
  4397. assert_equal(count(a, keepdims=True), 24*ones((1,1,1)))
  4398. assert_equal(np.ndim(count(a, keepdims=True)), 3)
  4399. assert_equal(count(a, axis=1, keepdims=True), 3*ones((2,1,4)))
  4400. assert_equal(count(a, axis=(0,1), keepdims=True), 6*ones((1,1,4)))
  4401. assert_equal(count(a, axis=-2), 3*ones((2,4)))
  4402. assert_raises(ValueError, count, a, axis=(1,1))
  4403. assert_raises(np.AxisError, count, a, axis=3)
  4404. # check the 'masked' singleton
  4405. assert_equal(count(np.ma.masked), 0)
  4406. # check 0-d arrays do not allow axis > 0
  4407. assert_raises(np.AxisError, count, np.ma.array(1), axis=1)
  4408. class TestMaskedConstant:
  4409. def _do_add_test(self, add):
  4410. # sanity check
  4411. assert_(add(np.ma.masked, 1) is np.ma.masked)
  4412. # now try with a vector
  4413. vector = np.array([1, 2, 3])
  4414. result = add(np.ma.masked, vector)
  4415. # lots of things could go wrong here
  4416. assert_(result is not np.ma.masked)
  4417. assert_(not isinstance(result, np.ma.core.MaskedConstant))
  4418. assert_equal(result.shape, vector.shape)
  4419. assert_equal(np.ma.getmask(result), np.ones(vector.shape, dtype=bool))
  4420. def test_ufunc(self):
  4421. self._do_add_test(np.add)
  4422. def test_operator(self):
  4423. self._do_add_test(lambda a, b: a + b)
  4424. def test_ctor(self):
  4425. m = np.ma.array(np.ma.masked)
  4426. # most importantly, we do not want to create a new MaskedConstant
  4427. # instance
  4428. assert_(not isinstance(m, np.ma.core.MaskedConstant))
  4429. assert_(m is not np.ma.masked)
  4430. def test_repr(self):
  4431. # copies should not exist, but if they do, it should be obvious that
  4432. # something is wrong
  4433. assert_equal(repr(np.ma.masked), 'masked')
  4434. # create a new instance in a weird way
  4435. masked2 = np.ma.MaskedArray.__new__(np.ma.core.MaskedConstant)
  4436. assert_not_equal(repr(masked2), 'masked')
  4437. def test_pickle(self):
  4438. from io import BytesIO
  4439. for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
  4440. with BytesIO() as f:
  4441. pickle.dump(np.ma.masked, f, protocol=proto)
  4442. f.seek(0)
  4443. res = pickle.load(f)
  4444. assert_(res is np.ma.masked)
  4445. def test_copy(self):
  4446. # gh-9328
  4447. # copy is a no-op, like it is with np.True_
  4448. assert_equal(
  4449. np.ma.masked.copy() is np.ma.masked,
  4450. np.True_.copy() is np.True_)
  4451. def test__copy(self):
  4452. import copy
  4453. assert_(
  4454. copy.copy(np.ma.masked) is np.ma.masked)
  4455. def test_deepcopy(self):
  4456. import copy
  4457. assert_(
  4458. copy.deepcopy(np.ma.masked) is np.ma.masked)
  4459. def test_immutable(self):
  4460. orig = np.ma.masked
  4461. assert_raises(np.ma.core.MaskError, operator.setitem, orig, (), 1)
  4462. assert_raises(ValueError,operator.setitem, orig.data, (), 1)
  4463. assert_raises(ValueError, operator.setitem, orig.mask, (), False)
  4464. view = np.ma.masked.view(np.ma.MaskedArray)
  4465. assert_raises(ValueError, operator.setitem, view, (), 1)
  4466. assert_raises(ValueError, operator.setitem, view.data, (), 1)
  4467. assert_raises(ValueError, operator.setitem, view.mask, (), False)
  4468. def test_coercion_int(self):
  4469. a_i = np.zeros((), int)
  4470. assert_raises(MaskError, operator.setitem, a_i, (), np.ma.masked)
  4471. assert_raises(MaskError, int, np.ma.masked)
  4472. def test_coercion_float(self):
  4473. a_f = np.zeros((), float)
  4474. assert_warns(UserWarning, operator.setitem, a_f, (), np.ma.masked)
  4475. assert_(np.isnan(a_f[()]))
  4476. @pytest.mark.xfail(reason="See gh-9750")
  4477. def test_coercion_unicode(self):
  4478. a_u = np.zeros((), 'U10')
  4479. a_u[()] = np.ma.masked
  4480. assert_equal(a_u[()], u'--')
  4481. @pytest.mark.xfail(reason="See gh-9750")
  4482. def test_coercion_bytes(self):
  4483. a_b = np.zeros((), 'S10')
  4484. a_b[()] = np.ma.masked
  4485. assert_equal(a_b[()], b'--')
  4486. def test_subclass(self):
  4487. # https://github.com/astropy/astropy/issues/6645
  4488. class Sub(type(np.ma.masked)): pass
  4489. a = Sub()
  4490. assert_(a is Sub())
  4491. assert_(a is not np.ma.masked)
  4492. assert_not_equal(repr(a), 'masked')
  4493. def test_attributes_readonly(self):
  4494. assert_raises(AttributeError, setattr, np.ma.masked, 'shape', (1,))
  4495. assert_raises(AttributeError, setattr, np.ma.masked, 'dtype', np.int64)
  4496. class TestMaskedWhereAliases:
  4497. # TODO: Test masked_object, masked_equal, ...
  4498. def test_masked_values(self):
  4499. res = masked_values(np.array([-32768.0]), np.int16(-32768))
  4500. assert_equal(res.mask, [True])
  4501. res = masked_values(np.inf, np.inf)
  4502. assert_equal(res.mask, True)
  4503. res = np.ma.masked_values(np.inf, -np.inf)
  4504. assert_equal(res.mask, False)
  4505. res = np.ma.masked_values([1, 2, 3, 4], 5, shrink=True)
  4506. assert_(res.mask is np.ma.nomask)
  4507. res = np.ma.masked_values([1, 2, 3, 4], 5, shrink=False)
  4508. assert_equal(res.mask, [False] * 4)
  4509. def test_masked_array():
  4510. a = np.ma.array([0, 1, 2, 3], mask=[0, 0, 1, 0])
  4511. assert_equal(np.argwhere(a), [[1], [3]])
  4512. def test_append_masked_array():
  4513. a = np.ma.masked_equal([1,2,3], value=2)
  4514. b = np.ma.masked_equal([4,3,2], value=2)
  4515. result = np.ma.append(a, b)
  4516. expected_data = [1, 2, 3, 4, 3, 2]
  4517. expected_mask = [False, True, False, False, False, True]
  4518. assert_array_equal(result.data, expected_data)
  4519. assert_array_equal(result.mask, expected_mask)
  4520. a = np.ma.masked_all((2,2))
  4521. b = np.ma.ones((3,1))
  4522. result = np.ma.append(a, b)
  4523. expected_data = [1] * 3
  4524. expected_mask = [True] * 4 + [False] * 3
  4525. assert_array_equal(result.data[-3], expected_data)
  4526. assert_array_equal(result.mask, expected_mask)
  4527. result = np.ma.append(a, b, axis=None)
  4528. assert_array_equal(result.data[-3], expected_data)
  4529. assert_array_equal(result.mask, expected_mask)
  4530. def test_append_masked_array_along_axis():
  4531. a = np.ma.masked_equal([1,2,3], value=2)
  4532. b = np.ma.masked_values([[4, 5, 6], [7, 8, 9]], 7)
  4533. # When `axis` is specified, `values` must have the correct shape.
  4534. assert_raises(ValueError, np.ma.append, a, b, axis=0)
  4535. result = np.ma.append(a[np.newaxis,:], b, axis=0)
  4536. expected = np.ma.arange(1, 10)
  4537. expected[[1, 6]] = np.ma.masked
  4538. expected = expected.reshape((3,3))
  4539. assert_array_equal(result.data, expected.data)
  4540. assert_array_equal(result.mask, expected.mask)
  4541. def test_default_fill_value_complex():
  4542. # regression test for Python 3, where 'unicode' was not defined
  4543. assert_(default_fill_value(1 + 1j) == 1.e20 + 0.0j)
  4544. def test_ufunc_with_output():
  4545. # check that giving an output argument always returns that output.
  4546. # Regression test for gh-8416.
  4547. x = array([1., 2., 3.], mask=[0, 0, 1])
  4548. y = np.add(x, 1., out=x)
  4549. assert_(y is x)
  4550. def test_ufunc_with_out_varied():
  4551. """ Test that masked arrays are immune to gh-10459 """
  4552. # the mask of the output should not affect the result, however it is passed
  4553. a = array([ 1, 2, 3], mask=[1, 0, 0])
  4554. b = array([10, 20, 30], mask=[1, 0, 0])
  4555. out = array([ 0, 0, 0], mask=[0, 0, 1])
  4556. expected = array([11, 22, 33], mask=[1, 0, 0])
  4557. out_pos = out.copy()
  4558. res_pos = np.add(a, b, out_pos)
  4559. out_kw = out.copy()
  4560. res_kw = np.add(a, b, out=out_kw)
  4561. out_tup = out.copy()
  4562. res_tup = np.add(a, b, out=(out_tup,))
  4563. assert_equal(res_kw.mask, expected.mask)
  4564. assert_equal(res_kw.data, expected.data)
  4565. assert_equal(res_tup.mask, expected.mask)
  4566. assert_equal(res_tup.data, expected.data)
  4567. assert_equal(res_pos.mask, expected.mask)
  4568. assert_equal(res_pos.data, expected.data)
  4569. def test_astype_mask_ordering():
  4570. descr = [('v', int, 3), ('x', [('y', float)])]
  4571. x = array([
  4572. [([1, 2, 3], (1.0,)), ([1, 2, 3], (2.0,))],
  4573. [([1, 2, 3], (3.0,)), ([1, 2, 3], (4.0,))]], dtype=descr)
  4574. x[0]['v'][0] = np.ma.masked
  4575. x_a = x.astype(descr)
  4576. assert x_a.dtype.names == np.dtype(descr).names
  4577. assert x_a.mask.dtype.names == np.dtype(descr).names
  4578. assert_equal(x, x_a)
  4579. assert_(x is x.astype(x.dtype, copy=False))
  4580. assert_equal(type(x.astype(x.dtype, subok=False)), np.ndarray)
  4581. x_f = x.astype(x.dtype, order='F')
  4582. assert_(x_f.flags.f_contiguous)
  4583. assert_(x_f.mask.flags.f_contiguous)
  4584. # Also test the same indirectly, via np.array
  4585. x_a2 = np.array(x, dtype=descr, subok=True)
  4586. assert x_a2.dtype.names == np.dtype(descr).names
  4587. assert x_a2.mask.dtype.names == np.dtype(descr).names
  4588. assert_equal(x, x_a2)
  4589. assert_(x is np.array(x, dtype=descr, copy=False, subok=True))
  4590. x_f2 = np.array(x, dtype=x.dtype, order='F', subok=True)
  4591. assert_(x_f2.flags.f_contiguous)
  4592. assert_(x_f2.mask.flags.f_contiguous)
  4593. @pytest.mark.parametrize('dt1', num_dts, ids=num_ids)
  4594. @pytest.mark.parametrize('dt2', num_dts, ids=num_ids)
  4595. @pytest.mark.filterwarnings('ignore::numpy.ComplexWarning')
  4596. def test_astype_basic(dt1, dt2):
  4597. # See gh-12070
  4598. src = np.ma.array(ones(3, dt1), fill_value=1)
  4599. dst = src.astype(dt2)
  4600. assert_(src.fill_value == 1)
  4601. assert_(src.dtype == dt1)
  4602. assert_(src.fill_value.dtype == dt1)
  4603. assert_(dst.fill_value == 1)
  4604. assert_(dst.dtype == dt2)
  4605. assert_(dst.fill_value.dtype == dt2)
  4606. assert_equal(src, dst)
  4607. def test_fieldless_void():
  4608. dt = np.dtype([]) # a void dtype with no fields
  4609. x = np.empty(4, dt)
  4610. # these arrays contain no values, so there's little to test - but this
  4611. # shouldn't crash
  4612. mx = np.ma.array(x)
  4613. assert_equal(mx.dtype, x.dtype)
  4614. assert_equal(mx.shape, x.shape)
  4615. mx = np.ma.array(x, mask=x)
  4616. assert_equal(mx.dtype, x.dtype)
  4617. assert_equal(mx.shape, x.shape)
  4618. def test_mask_shape_assignment_does_not_break_masked():
  4619. a = np.ma.masked
  4620. b = np.ma.array(1, mask=a.mask)
  4621. b.shape = (1,)
  4622. assert_equal(a.mask.shape, ())
  4623. @pytest.mark.skipif(sys.flags.optimize > 1,
  4624. reason="no docstrings present to inspect when PYTHONOPTIMIZE/Py_OptimizeFlag > 1")
  4625. def test_doc_note():
  4626. def method(self):
  4627. """This docstring
  4628. Has multiple lines
  4629. And notes
  4630. Notes
  4631. -----
  4632. original note
  4633. """
  4634. pass
  4635. expected_doc = """This docstring
  4636. Has multiple lines
  4637. And notes
  4638. Notes
  4639. -----
  4640. note
  4641. original note"""
  4642. assert_equal(np.ma.core.doc_note(method.__doc__, "note"), expected_doc)