core.py 272 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565
  1. """
  2. numpy.ma : a package to handle missing or invalid values.
  3. This package was initially written for numarray by Paul F. Dubois
  4. at Lawrence Livermore National Laboratory.
  5. In 2006, the package was completely rewritten by Pierre Gerard-Marchant
  6. (University of Georgia) to make the MaskedArray class a subclass of ndarray,
  7. and to improve support of structured arrays.
  8. Copyright 1999, 2000, 2001 Regents of the University of California.
  9. Released for unlimited redistribution.
  10. * Adapted for numpy_core 2005 by Travis Oliphant and (mainly) Paul Dubois.
  11. * Subclassing of the base `ndarray` 2006 by Pierre Gerard-Marchant
  12. (pgmdevlist_AT_gmail_DOT_com)
  13. * Improvements suggested by Reggie Dugard (reggie_AT_merfinllc_DOT_com)
  14. .. moduleauthor:: Pierre Gerard-Marchant
  15. """
  16. # pylint: disable-msg=E1002
  17. import builtins
  18. import inspect
  19. import operator
  20. import warnings
  21. import textwrap
  22. import re
  23. from functools import reduce
  24. import numpy as np
  25. import numpy.core.umath as umath
  26. import numpy.core.numerictypes as ntypes
  27. from numpy.core import multiarray as mu
  28. from numpy import ndarray, amax, amin, iscomplexobj, bool_, _NoValue
  29. from numpy import array as narray
  30. from numpy.lib.function_base import angle
  31. from numpy.compat import (
  32. getargspec, formatargspec, long, unicode, bytes
  33. )
  34. from numpy import expand_dims
  35. from numpy.core.numeric import normalize_axis_tuple
  36. __all__ = [
  37. 'MAError', 'MaskError', 'MaskType', 'MaskedArray', 'abs', 'absolute',
  38. 'add', 'all', 'allclose', 'allequal', 'alltrue', 'amax', 'amin',
  39. 'angle', 'anom', 'anomalies', 'any', 'append', 'arange', 'arccos',
  40. 'arccosh', 'arcsin', 'arcsinh', 'arctan', 'arctan2', 'arctanh',
  41. 'argmax', 'argmin', 'argsort', 'around', 'array', 'asanyarray',
  42. 'asarray', 'bitwise_and', 'bitwise_or', 'bitwise_xor', 'bool_', 'ceil',
  43. 'choose', 'clip', 'common_fill_value', 'compress', 'compressed',
  44. 'concatenate', 'conjugate', 'convolve', 'copy', 'correlate', 'cos', 'cosh',
  45. 'count', 'cumprod', 'cumsum', 'default_fill_value', 'diag', 'diagonal',
  46. 'diff', 'divide', 'empty', 'empty_like', 'equal', 'exp',
  47. 'expand_dims', 'fabs', 'filled', 'fix_invalid', 'flatten_mask',
  48. 'flatten_structured_array', 'floor', 'floor_divide', 'fmod',
  49. 'frombuffer', 'fromflex', 'fromfunction', 'getdata', 'getmask',
  50. 'getmaskarray', 'greater', 'greater_equal', 'harden_mask', 'hypot',
  51. 'identity', 'ids', 'indices', 'inner', 'innerproduct', 'isMA',
  52. 'isMaskedArray', 'is_mask', 'is_masked', 'isarray', 'left_shift',
  53. 'less', 'less_equal', 'log', 'log10', 'log2',
  54. 'logical_and', 'logical_not', 'logical_or', 'logical_xor', 'make_mask',
  55. 'make_mask_descr', 'make_mask_none', 'mask_or', 'masked',
  56. 'masked_array', 'masked_equal', 'masked_greater',
  57. 'masked_greater_equal', 'masked_inside', 'masked_invalid',
  58. 'masked_less', 'masked_less_equal', 'masked_not_equal',
  59. 'masked_object', 'masked_outside', 'masked_print_option',
  60. 'masked_singleton', 'masked_values', 'masked_where', 'max', 'maximum',
  61. 'maximum_fill_value', 'mean', 'min', 'minimum', 'minimum_fill_value',
  62. 'mod', 'multiply', 'mvoid', 'ndim', 'negative', 'nomask', 'nonzero',
  63. 'not_equal', 'ones', 'ones_like', 'outer', 'outerproduct', 'power', 'prod',
  64. 'product', 'ptp', 'put', 'putmask', 'ravel', 'remainder',
  65. 'repeat', 'reshape', 'resize', 'right_shift', 'round', 'round_',
  66. 'set_fill_value', 'shape', 'sin', 'sinh', 'size', 'soften_mask',
  67. 'sometrue', 'sort', 'sqrt', 'squeeze', 'std', 'subtract', 'sum',
  68. 'swapaxes', 'take', 'tan', 'tanh', 'trace', 'transpose', 'true_divide',
  69. 'var', 'where', 'zeros', 'zeros_like',
  70. ]
  71. MaskType = np.bool_
  72. nomask = MaskType(0)
  73. class MaskedArrayFutureWarning(FutureWarning):
  74. pass
  75. def _deprecate_argsort_axis(arr):
  76. """
  77. Adjust the axis passed to argsort, warning if necessary
  78. Parameters
  79. ----------
  80. arr
  81. The array which argsort was called on
  82. np.ma.argsort has a long-term bug where the default of the axis argument
  83. is wrong (gh-8701), which now must be kept for backwards compatibility.
  84. Thankfully, this only makes a difference when arrays are 2- or more-
  85. dimensional, so we only need a warning then.
  86. """
  87. if arr.ndim <= 1:
  88. # no warning needed - but switch to -1 anyway, to avoid surprising
  89. # subclasses, which are more likely to implement scalar axes.
  90. return -1
  91. else:
  92. # 2017-04-11, Numpy 1.13.0, gh-8701: warn on axis default
  93. warnings.warn(
  94. "In the future the default for argsort will be axis=-1, not the "
  95. "current None, to match its documentation and np.argsort. "
  96. "Explicitly pass -1 or None to silence this warning.",
  97. MaskedArrayFutureWarning, stacklevel=3)
  98. return None
  99. def doc_note(initialdoc, note):
  100. """
  101. Adds a Notes section to an existing docstring.
  102. """
  103. if initialdoc is None:
  104. return
  105. if note is None:
  106. return initialdoc
  107. notesplit = re.split(r'\n\s*?Notes\n\s*?-----', inspect.cleandoc(initialdoc))
  108. notedoc = "\n\nNotes\n-----\n%s\n" % inspect.cleandoc(note)
  109. return ''.join(notesplit[:1] + [notedoc] + notesplit[1:])
  110. def get_object_signature(obj):
  111. """
  112. Get the signature from obj
  113. """
  114. try:
  115. sig = formatargspec(*getargspec(obj))
  116. except TypeError:
  117. sig = ''
  118. return sig
  119. ###############################################################################
  120. # Exceptions #
  121. ###############################################################################
  122. class MAError(Exception):
  123. """
  124. Class for masked array related errors.
  125. """
  126. pass
  127. class MaskError(MAError):
  128. """
  129. Class for mask related errors.
  130. """
  131. pass
  132. ###############################################################################
  133. # Filling options #
  134. ###############################################################################
  135. # b: boolean - c: complex - f: floats - i: integer - O: object - S: string
  136. default_filler = {'b': True,
  137. 'c': 1.e20 + 0.0j,
  138. 'f': 1.e20,
  139. 'i': 999999,
  140. 'O': '?',
  141. 'S': b'N/A',
  142. 'u': 999999,
  143. 'V': b'???',
  144. 'U': 'N/A'
  145. }
  146. # Add datetime64 and timedelta64 types
  147. for v in ["Y", "M", "W", "D", "h", "m", "s", "ms", "us", "ns", "ps",
  148. "fs", "as"]:
  149. default_filler["M8[" + v + "]"] = np.datetime64("NaT", v)
  150. default_filler["m8[" + v + "]"] = np.timedelta64("NaT", v)
  151. float_types_list = [np.half, np.single, np.double, np.longdouble,
  152. np.csingle, np.cdouble, np.clongdouble]
  153. max_filler = ntypes._minvals
  154. max_filler.update([(k, -np.inf) for k in float_types_list[:4]])
  155. max_filler.update([(k, complex(-np.inf, -np.inf)) for k in float_types_list[-3:]])
  156. min_filler = ntypes._maxvals
  157. min_filler.update([(k, +np.inf) for k in float_types_list[:4]])
  158. min_filler.update([(k, complex(+np.inf, +np.inf)) for k in float_types_list[-3:]])
  159. del float_types_list
  160. def _recursive_fill_value(dtype, f):
  161. """
  162. Recursively produce a fill value for `dtype`, calling f on scalar dtypes
  163. """
  164. if dtype.names is not None:
  165. # We wrap into `array` here, which ensures we use NumPy cast rules
  166. # for integer casts, this allows the use of 99999 as a fill value
  167. # for int8.
  168. # TODO: This is probably a mess, but should best preserve behavior?
  169. vals = tuple(
  170. np.array(_recursive_fill_value(dtype[name], f))
  171. for name in dtype.names)
  172. return np.array(vals, dtype=dtype)[()] # decay to void scalar from 0d
  173. elif dtype.subdtype:
  174. subtype, shape = dtype.subdtype
  175. subval = _recursive_fill_value(subtype, f)
  176. return np.full(shape, subval)
  177. else:
  178. return f(dtype)
  179. def _get_dtype_of(obj):
  180. """ Convert the argument for *_fill_value into a dtype """
  181. if isinstance(obj, np.dtype):
  182. return obj
  183. elif hasattr(obj, 'dtype'):
  184. return obj.dtype
  185. else:
  186. return np.asanyarray(obj).dtype
  187. def default_fill_value(obj):
  188. """
  189. Return the default fill value for the argument object.
  190. The default filling value depends on the datatype of the input
  191. array or the type of the input scalar:
  192. ======== ========
  193. datatype default
  194. ======== ========
  195. bool True
  196. int 999999
  197. float 1.e20
  198. complex 1.e20+0j
  199. object '?'
  200. string 'N/A'
  201. ======== ========
  202. For structured types, a structured scalar is returned, with each field the
  203. default fill value for its type.
  204. For subarray types, the fill value is an array of the same size containing
  205. the default scalar fill value.
  206. Parameters
  207. ----------
  208. obj : ndarray, dtype or scalar
  209. The array data-type or scalar for which the default fill value
  210. is returned.
  211. Returns
  212. -------
  213. fill_value : scalar
  214. The default fill value.
  215. Examples
  216. --------
  217. >>> np.ma.default_fill_value(1)
  218. 999999
  219. >>> np.ma.default_fill_value(np.array([1.1, 2., np.pi]))
  220. 1e+20
  221. >>> np.ma.default_fill_value(np.dtype(complex))
  222. (1e+20+0j)
  223. """
  224. def _scalar_fill_value(dtype):
  225. if dtype.kind in 'Mm':
  226. return default_filler.get(dtype.str[1:], '?')
  227. else:
  228. return default_filler.get(dtype.kind, '?')
  229. dtype = _get_dtype_of(obj)
  230. return _recursive_fill_value(dtype, _scalar_fill_value)
  231. def _extremum_fill_value(obj, extremum, extremum_name):
  232. def _scalar_fill_value(dtype):
  233. try:
  234. return extremum[dtype]
  235. except KeyError as e:
  236. raise TypeError(
  237. f"Unsuitable type {dtype} for calculating {extremum_name}."
  238. ) from None
  239. dtype = _get_dtype_of(obj)
  240. return _recursive_fill_value(dtype, _scalar_fill_value)
  241. def minimum_fill_value(obj):
  242. """
  243. Return the maximum value that can be represented by the dtype of an object.
  244. This function is useful for calculating a fill value suitable for
  245. taking the minimum of an array with a given dtype.
  246. Parameters
  247. ----------
  248. obj : ndarray, dtype or scalar
  249. An object that can be queried for it's numeric type.
  250. Returns
  251. -------
  252. val : scalar
  253. The maximum representable value.
  254. Raises
  255. ------
  256. TypeError
  257. If `obj` isn't a suitable numeric type.
  258. See Also
  259. --------
  260. maximum_fill_value : The inverse function.
  261. set_fill_value : Set the filling value of a masked array.
  262. MaskedArray.fill_value : Return current fill value.
  263. Examples
  264. --------
  265. >>> import numpy.ma as ma
  266. >>> a = np.int8()
  267. >>> ma.minimum_fill_value(a)
  268. 127
  269. >>> a = np.int32()
  270. >>> ma.minimum_fill_value(a)
  271. 2147483647
  272. An array of numeric data can also be passed.
  273. >>> a = np.array([1, 2, 3], dtype=np.int8)
  274. >>> ma.minimum_fill_value(a)
  275. 127
  276. >>> a = np.array([1, 2, 3], dtype=np.float32)
  277. >>> ma.minimum_fill_value(a)
  278. inf
  279. """
  280. return _extremum_fill_value(obj, min_filler, "minimum")
  281. def maximum_fill_value(obj):
  282. """
  283. Return the minimum value that can be represented by the dtype of an object.
  284. This function is useful for calculating a fill value suitable for
  285. taking the maximum of an array with a given dtype.
  286. Parameters
  287. ----------
  288. obj : ndarray, dtype or scalar
  289. An object that can be queried for it's numeric type.
  290. Returns
  291. -------
  292. val : scalar
  293. The minimum representable value.
  294. Raises
  295. ------
  296. TypeError
  297. If `obj` isn't a suitable numeric type.
  298. See Also
  299. --------
  300. minimum_fill_value : The inverse function.
  301. set_fill_value : Set the filling value of a masked array.
  302. MaskedArray.fill_value : Return current fill value.
  303. Examples
  304. --------
  305. >>> import numpy.ma as ma
  306. >>> a = np.int8()
  307. >>> ma.maximum_fill_value(a)
  308. -128
  309. >>> a = np.int32()
  310. >>> ma.maximum_fill_value(a)
  311. -2147483648
  312. An array of numeric data can also be passed.
  313. >>> a = np.array([1, 2, 3], dtype=np.int8)
  314. >>> ma.maximum_fill_value(a)
  315. -128
  316. >>> a = np.array([1, 2, 3], dtype=np.float32)
  317. >>> ma.maximum_fill_value(a)
  318. -inf
  319. """
  320. return _extremum_fill_value(obj, max_filler, "maximum")
  321. def _recursive_set_fill_value(fillvalue, dt):
  322. """
  323. Create a fill value for a structured dtype.
  324. Parameters
  325. ----------
  326. fillvalue : scalar or array_like
  327. Scalar or array representing the fill value. If it is of shorter
  328. length than the number of fields in dt, it will be resized.
  329. dt : dtype
  330. The structured dtype for which to create the fill value.
  331. Returns
  332. -------
  333. val : tuple
  334. A tuple of values corresponding to the structured fill value.
  335. """
  336. fillvalue = np.resize(fillvalue, len(dt.names))
  337. output_value = []
  338. for (fval, name) in zip(fillvalue, dt.names):
  339. cdtype = dt[name]
  340. if cdtype.subdtype:
  341. cdtype = cdtype.subdtype[0]
  342. if cdtype.names is not None:
  343. output_value.append(tuple(_recursive_set_fill_value(fval, cdtype)))
  344. else:
  345. output_value.append(np.array(fval, dtype=cdtype).item())
  346. return tuple(output_value)
  347. def _check_fill_value(fill_value, ndtype):
  348. """
  349. Private function validating the given `fill_value` for the given dtype.
  350. If fill_value is None, it is set to the default corresponding to the dtype.
  351. If fill_value is not None, its value is forced to the given dtype.
  352. The result is always a 0d array.
  353. """
  354. ndtype = np.dtype(ndtype)
  355. if fill_value is None:
  356. fill_value = default_fill_value(ndtype)
  357. elif ndtype.names is not None:
  358. if isinstance(fill_value, (ndarray, np.void)):
  359. try:
  360. fill_value = np.array(fill_value, copy=False, dtype=ndtype)
  361. except ValueError as e:
  362. err_msg = "Unable to transform %s to dtype %s"
  363. raise ValueError(err_msg % (fill_value, ndtype)) from e
  364. else:
  365. fill_value = np.asarray(fill_value, dtype=object)
  366. fill_value = np.array(_recursive_set_fill_value(fill_value, ndtype),
  367. dtype=ndtype)
  368. else:
  369. if isinstance(fill_value, str) and (ndtype.char not in 'OSVU'):
  370. # Note this check doesn't work if fill_value is not a scalar
  371. err_msg = "Cannot set fill value of string with array of dtype %s"
  372. raise TypeError(err_msg % ndtype)
  373. else:
  374. # In case we want to convert 1e20 to int.
  375. # Also in case of converting string arrays.
  376. try:
  377. fill_value = np.array(fill_value, copy=False, dtype=ndtype)
  378. except (OverflowError, ValueError) as e:
  379. # Raise TypeError instead of OverflowError or ValueError.
  380. # OverflowError is seldom used, and the real problem here is
  381. # that the passed fill_value is not compatible with the ndtype.
  382. err_msg = "Cannot convert fill_value %s to dtype %s"
  383. raise TypeError(err_msg % (fill_value, ndtype)) from e
  384. return np.array(fill_value)
  385. def set_fill_value(a, fill_value):
  386. """
  387. Set the filling value of a, if a is a masked array.
  388. This function changes the fill value of the masked array `a` in place.
  389. If `a` is not a masked array, the function returns silently, without
  390. doing anything.
  391. Parameters
  392. ----------
  393. a : array_like
  394. Input array.
  395. fill_value : dtype
  396. Filling value. A consistency test is performed to make sure
  397. the value is compatible with the dtype of `a`.
  398. Returns
  399. -------
  400. None
  401. Nothing returned by this function.
  402. See Also
  403. --------
  404. maximum_fill_value : Return the default fill value for a dtype.
  405. MaskedArray.fill_value : Return current fill value.
  406. MaskedArray.set_fill_value : Equivalent method.
  407. Examples
  408. --------
  409. >>> import numpy.ma as ma
  410. >>> a = np.arange(5)
  411. >>> a
  412. array([0, 1, 2, 3, 4])
  413. >>> a = ma.masked_where(a < 3, a)
  414. >>> a
  415. masked_array(data=[--, --, --, 3, 4],
  416. mask=[ True, True, True, False, False],
  417. fill_value=999999)
  418. >>> ma.set_fill_value(a, -999)
  419. >>> a
  420. masked_array(data=[--, --, --, 3, 4],
  421. mask=[ True, True, True, False, False],
  422. fill_value=-999)
  423. Nothing happens if `a` is not a masked array.
  424. >>> a = list(range(5))
  425. >>> a
  426. [0, 1, 2, 3, 4]
  427. >>> ma.set_fill_value(a, 100)
  428. >>> a
  429. [0, 1, 2, 3, 4]
  430. >>> a = np.arange(5)
  431. >>> a
  432. array([0, 1, 2, 3, 4])
  433. >>> ma.set_fill_value(a, 100)
  434. >>> a
  435. array([0, 1, 2, 3, 4])
  436. """
  437. if isinstance(a, MaskedArray):
  438. a.set_fill_value(fill_value)
  439. return
  440. def get_fill_value(a):
  441. """
  442. Return the filling value of a, if any. Otherwise, returns the
  443. default filling value for that type.
  444. """
  445. if isinstance(a, MaskedArray):
  446. result = a.fill_value
  447. else:
  448. result = default_fill_value(a)
  449. return result
  450. def common_fill_value(a, b):
  451. """
  452. Return the common filling value of two masked arrays, if any.
  453. If ``a.fill_value == b.fill_value``, return the fill value,
  454. otherwise return None.
  455. Parameters
  456. ----------
  457. a, b : MaskedArray
  458. The masked arrays for which to compare fill values.
  459. Returns
  460. -------
  461. fill_value : scalar or None
  462. The common fill value, or None.
  463. Examples
  464. --------
  465. >>> x = np.ma.array([0, 1.], fill_value=3)
  466. >>> y = np.ma.array([0, 1.], fill_value=3)
  467. >>> np.ma.common_fill_value(x, y)
  468. 3.0
  469. """
  470. t1 = get_fill_value(a)
  471. t2 = get_fill_value(b)
  472. if t1 == t2:
  473. return t1
  474. return None
  475. def filled(a, fill_value=None):
  476. """
  477. Return input as an array with masked data replaced by a fill value.
  478. If `a` is not a `MaskedArray`, `a` itself is returned.
  479. If `a` is a `MaskedArray` and `fill_value` is None, `fill_value` is set to
  480. ``a.fill_value``.
  481. Parameters
  482. ----------
  483. a : MaskedArray or array_like
  484. An input object.
  485. fill_value : array_like, optional.
  486. Can be scalar or non-scalar. If non-scalar, the
  487. resulting filled array should be broadcastable
  488. over input array. Default is None.
  489. Returns
  490. -------
  491. a : ndarray
  492. The filled array.
  493. See Also
  494. --------
  495. compressed
  496. Examples
  497. --------
  498. >>> x = np.ma.array(np.arange(9).reshape(3, 3), mask=[[1, 0, 0],
  499. ... [1, 0, 0],
  500. ... [0, 0, 0]])
  501. >>> x.filled()
  502. array([[999999, 1, 2],
  503. [999999, 4, 5],
  504. [ 6, 7, 8]])
  505. >>> x.filled(fill_value=333)
  506. array([[333, 1, 2],
  507. [333, 4, 5],
  508. [ 6, 7, 8]])
  509. >>> x.filled(fill_value=np.arange(3))
  510. array([[0, 1, 2],
  511. [0, 4, 5],
  512. [6, 7, 8]])
  513. """
  514. if hasattr(a, 'filled'):
  515. return a.filled(fill_value)
  516. elif isinstance(a, ndarray):
  517. # Should we check for contiguity ? and a.flags['CONTIGUOUS']:
  518. return a
  519. elif isinstance(a, dict):
  520. return np.array(a, 'O')
  521. else:
  522. return np.array(a)
  523. def get_masked_subclass(*arrays):
  524. """
  525. Return the youngest subclass of MaskedArray from a list of (masked) arrays.
  526. In case of siblings, the first listed takes over.
  527. """
  528. if len(arrays) == 1:
  529. arr = arrays[0]
  530. if isinstance(arr, MaskedArray):
  531. rcls = type(arr)
  532. else:
  533. rcls = MaskedArray
  534. else:
  535. arrcls = [type(a) for a in arrays]
  536. rcls = arrcls[0]
  537. if not issubclass(rcls, MaskedArray):
  538. rcls = MaskedArray
  539. for cls in arrcls[1:]:
  540. if issubclass(cls, rcls):
  541. rcls = cls
  542. # Don't return MaskedConstant as result: revert to MaskedArray
  543. if rcls.__name__ == 'MaskedConstant':
  544. return MaskedArray
  545. return rcls
  546. def getdata(a, subok=True):
  547. """
  548. Return the data of a masked array as an ndarray.
  549. Return the data of `a` (if any) as an ndarray if `a` is a ``MaskedArray``,
  550. else return `a` as a ndarray or subclass (depending on `subok`) if not.
  551. Parameters
  552. ----------
  553. a : array_like
  554. Input ``MaskedArray``, alternatively a ndarray or a subclass thereof.
  555. subok : bool
  556. Whether to force the output to be a `pure` ndarray (False) or to
  557. return a subclass of ndarray if appropriate (True, default).
  558. See Also
  559. --------
  560. getmask : Return the mask of a masked array, or nomask.
  561. getmaskarray : Return the mask of a masked array, or full array of False.
  562. Examples
  563. --------
  564. >>> import numpy.ma as ma
  565. >>> a = ma.masked_equal([[1,2],[3,4]], 2)
  566. >>> a
  567. masked_array(
  568. data=[[1, --],
  569. [3, 4]],
  570. mask=[[False, True],
  571. [False, False]],
  572. fill_value=2)
  573. >>> ma.getdata(a)
  574. array([[1, 2],
  575. [3, 4]])
  576. Equivalently use the ``MaskedArray`` `data` attribute.
  577. >>> a.data
  578. array([[1, 2],
  579. [3, 4]])
  580. """
  581. try:
  582. data = a._data
  583. except AttributeError:
  584. data = np.array(a, copy=False, subok=subok)
  585. if not subok:
  586. return data.view(ndarray)
  587. return data
  588. get_data = getdata
  589. def fix_invalid(a, mask=nomask, copy=True, fill_value=None):
  590. """
  591. Return input with invalid data masked and replaced by a fill value.
  592. Invalid data means values of `nan`, `inf`, etc.
  593. Parameters
  594. ----------
  595. a : array_like
  596. Input array, a (subclass of) ndarray.
  597. mask : sequence, optional
  598. Mask. Must be convertible to an array of booleans with the same
  599. shape as `data`. True indicates a masked (i.e. invalid) data.
  600. copy : bool, optional
  601. Whether to use a copy of `a` (True) or to fix `a` in place (False).
  602. Default is True.
  603. fill_value : scalar, optional
  604. Value used for fixing invalid data. Default is None, in which case
  605. the ``a.fill_value`` is used.
  606. Returns
  607. -------
  608. b : MaskedArray
  609. The input array with invalid entries fixed.
  610. Notes
  611. -----
  612. A copy is performed by default.
  613. Examples
  614. --------
  615. >>> x = np.ma.array([1., -1, np.nan, np.inf], mask=[1] + [0]*3)
  616. >>> x
  617. masked_array(data=[--, -1.0, nan, inf],
  618. mask=[ True, False, False, False],
  619. fill_value=1e+20)
  620. >>> np.ma.fix_invalid(x)
  621. masked_array(data=[--, -1.0, --, --],
  622. mask=[ True, False, True, True],
  623. fill_value=1e+20)
  624. >>> fixed = np.ma.fix_invalid(x)
  625. >>> fixed.data
  626. array([ 1.e+00, -1.e+00, 1.e+20, 1.e+20])
  627. >>> x.data
  628. array([ 1., -1., nan, inf])
  629. """
  630. a = masked_array(a, copy=copy, mask=mask, subok=True)
  631. invalid = np.logical_not(np.isfinite(a._data))
  632. if not invalid.any():
  633. return a
  634. a._mask |= invalid
  635. if fill_value is None:
  636. fill_value = a.fill_value
  637. a._data[invalid] = fill_value
  638. return a
  639. def is_string_or_list_of_strings(val):
  640. return (isinstance(val, str) or
  641. (isinstance(val, list) and val and
  642. builtins.all(isinstance(s, str) for s in val)))
  643. ###############################################################################
  644. # Ufuncs #
  645. ###############################################################################
  646. ufunc_domain = {}
  647. ufunc_fills = {}
  648. class _DomainCheckInterval:
  649. """
  650. Define a valid interval, so that :
  651. ``domain_check_interval(a,b)(x) == True`` where
  652. ``x < a`` or ``x > b``.
  653. """
  654. def __init__(self, a, b):
  655. "domain_check_interval(a,b)(x) = true where x < a or y > b"
  656. if a > b:
  657. (a, b) = (b, a)
  658. self.a = a
  659. self.b = b
  660. def __call__(self, x):
  661. "Execute the call behavior."
  662. # nans at masked positions cause RuntimeWarnings, even though
  663. # they are masked. To avoid this we suppress warnings.
  664. with np.errstate(invalid='ignore'):
  665. return umath.logical_or(umath.greater(x, self.b),
  666. umath.less(x, self.a))
  667. class _DomainTan:
  668. """
  669. Define a valid interval for the `tan` function, so that:
  670. ``domain_tan(eps) = True`` where ``abs(cos(x)) < eps``
  671. """
  672. def __init__(self, eps):
  673. "domain_tan(eps) = true where abs(cos(x)) < eps)"
  674. self.eps = eps
  675. def __call__(self, x):
  676. "Executes the call behavior."
  677. with np.errstate(invalid='ignore'):
  678. return umath.less(umath.absolute(umath.cos(x)), self.eps)
  679. class _DomainSafeDivide:
  680. """
  681. Define a domain for safe division.
  682. """
  683. def __init__(self, tolerance=None):
  684. self.tolerance = tolerance
  685. def __call__(self, a, b):
  686. # Delay the selection of the tolerance to here in order to reduce numpy
  687. # import times. The calculation of these parameters is a substantial
  688. # component of numpy's import time.
  689. if self.tolerance is None:
  690. self.tolerance = np.finfo(float).tiny
  691. # don't call ma ufuncs from __array_wrap__ which would fail for scalars
  692. a, b = np.asarray(a), np.asarray(b)
  693. with np.errstate(invalid='ignore'):
  694. return umath.absolute(a) * self.tolerance >= umath.absolute(b)
  695. class _DomainGreater:
  696. """
  697. DomainGreater(v)(x) is True where x <= v.
  698. """
  699. def __init__(self, critical_value):
  700. "DomainGreater(v)(x) = true where x <= v"
  701. self.critical_value = critical_value
  702. def __call__(self, x):
  703. "Executes the call behavior."
  704. with np.errstate(invalid='ignore'):
  705. return umath.less_equal(x, self.critical_value)
  706. class _DomainGreaterEqual:
  707. """
  708. DomainGreaterEqual(v)(x) is True where x < v.
  709. """
  710. def __init__(self, critical_value):
  711. "DomainGreaterEqual(v)(x) = true where x < v"
  712. self.critical_value = critical_value
  713. def __call__(self, x):
  714. "Executes the call behavior."
  715. with np.errstate(invalid='ignore'):
  716. return umath.less(x, self.critical_value)
  717. class _MaskedUFunc:
  718. def __init__(self, ufunc):
  719. self.f = ufunc
  720. self.__doc__ = ufunc.__doc__
  721. self.__name__ = ufunc.__name__
  722. def __str__(self):
  723. return f"Masked version of {self.f}"
  724. class _MaskedUnaryOperation(_MaskedUFunc):
  725. """
  726. Defines masked version of unary operations, where invalid values are
  727. pre-masked.
  728. Parameters
  729. ----------
  730. mufunc : callable
  731. The function for which to define a masked version. Made available
  732. as ``_MaskedUnaryOperation.f``.
  733. fill : scalar, optional
  734. Filling value, default is 0.
  735. domain : class instance
  736. Domain for the function. Should be one of the ``_Domain*``
  737. classes. Default is None.
  738. """
  739. def __init__(self, mufunc, fill=0, domain=None):
  740. super().__init__(mufunc)
  741. self.fill = fill
  742. self.domain = domain
  743. ufunc_domain[mufunc] = domain
  744. ufunc_fills[mufunc] = fill
  745. def __call__(self, a, *args, **kwargs):
  746. """
  747. Execute the call behavior.
  748. """
  749. d = getdata(a)
  750. # Deal with domain
  751. if self.domain is not None:
  752. # Case 1.1. : Domained function
  753. # nans at masked positions cause RuntimeWarnings, even though
  754. # they are masked. To avoid this we suppress warnings.
  755. with np.errstate(divide='ignore', invalid='ignore'):
  756. result = self.f(d, *args, **kwargs)
  757. # Make a mask
  758. m = ~umath.isfinite(result)
  759. m |= self.domain(d)
  760. m |= getmask(a)
  761. else:
  762. # Case 1.2. : Function without a domain
  763. # Get the result and the mask
  764. with np.errstate(divide='ignore', invalid='ignore'):
  765. result = self.f(d, *args, **kwargs)
  766. m = getmask(a)
  767. if not result.ndim:
  768. # Case 2.1. : The result is scalarscalar
  769. if m:
  770. return masked
  771. return result
  772. if m is not nomask:
  773. # Case 2.2. The result is an array
  774. # We need to fill the invalid data back w/ the input Now,
  775. # that's plain silly: in C, we would just skip the element and
  776. # keep the original, but we do have to do it that way in Python
  777. # In case result has a lower dtype than the inputs (as in
  778. # equal)
  779. try:
  780. np.copyto(result, d, where=m)
  781. except TypeError:
  782. pass
  783. # Transform to
  784. masked_result = result.view(get_masked_subclass(a))
  785. masked_result._mask = m
  786. masked_result._update_from(a)
  787. return masked_result
  788. class _MaskedBinaryOperation(_MaskedUFunc):
  789. """
  790. Define masked version of binary operations, where invalid
  791. values are pre-masked.
  792. Parameters
  793. ----------
  794. mbfunc : function
  795. The function for which to define a masked version. Made available
  796. as ``_MaskedBinaryOperation.f``.
  797. domain : class instance
  798. Default domain for the function. Should be one of the ``_Domain*``
  799. classes. Default is None.
  800. fillx : scalar, optional
  801. Filling value for the first argument, default is 0.
  802. filly : scalar, optional
  803. Filling value for the second argument, default is 0.
  804. """
  805. def __init__(self, mbfunc, fillx=0, filly=0):
  806. """
  807. abfunc(fillx, filly) must be defined.
  808. abfunc(x, filly) = x for all x to enable reduce.
  809. """
  810. super().__init__(mbfunc)
  811. self.fillx = fillx
  812. self.filly = filly
  813. ufunc_domain[mbfunc] = None
  814. ufunc_fills[mbfunc] = (fillx, filly)
  815. def __call__(self, a, b, *args, **kwargs):
  816. """
  817. Execute the call behavior.
  818. """
  819. # Get the data, as ndarray
  820. (da, db) = (getdata(a), getdata(b))
  821. # Get the result
  822. with np.errstate():
  823. np.seterr(divide='ignore', invalid='ignore')
  824. result = self.f(da, db, *args, **kwargs)
  825. # Get the mask for the result
  826. (ma, mb) = (getmask(a), getmask(b))
  827. if ma is nomask:
  828. if mb is nomask:
  829. m = nomask
  830. else:
  831. m = umath.logical_or(getmaskarray(a), mb)
  832. elif mb is nomask:
  833. m = umath.logical_or(ma, getmaskarray(b))
  834. else:
  835. m = umath.logical_or(ma, mb)
  836. # Case 1. : scalar
  837. if not result.ndim:
  838. if m:
  839. return masked
  840. return result
  841. # Case 2. : array
  842. # Revert result to da where masked
  843. if m is not nomask and m.any():
  844. # any errors, just abort; impossible to guarantee masked values
  845. try:
  846. np.copyto(result, da, casting='unsafe', where=m)
  847. except Exception:
  848. pass
  849. # Transforms to a (subclass of) MaskedArray
  850. masked_result = result.view(get_masked_subclass(a, b))
  851. masked_result._mask = m
  852. if isinstance(a, MaskedArray):
  853. masked_result._update_from(a)
  854. elif isinstance(b, MaskedArray):
  855. masked_result._update_from(b)
  856. return masked_result
  857. def reduce(self, target, axis=0, dtype=None):
  858. """
  859. Reduce `target` along the given `axis`.
  860. """
  861. tclass = get_masked_subclass(target)
  862. m = getmask(target)
  863. t = filled(target, self.filly)
  864. if t.shape == ():
  865. t = t.reshape(1)
  866. if m is not nomask:
  867. m = make_mask(m, copy=True)
  868. m.shape = (1,)
  869. if m is nomask:
  870. tr = self.f.reduce(t, axis)
  871. mr = nomask
  872. else:
  873. tr = self.f.reduce(t, axis, dtype=dtype)
  874. mr = umath.logical_and.reduce(m, axis)
  875. if not tr.shape:
  876. if mr:
  877. return masked
  878. else:
  879. return tr
  880. masked_tr = tr.view(tclass)
  881. masked_tr._mask = mr
  882. return masked_tr
  883. def outer(self, a, b):
  884. """
  885. Return the function applied to the outer product of a and b.
  886. """
  887. (da, db) = (getdata(a), getdata(b))
  888. d = self.f.outer(da, db)
  889. ma = getmask(a)
  890. mb = getmask(b)
  891. if ma is nomask and mb is nomask:
  892. m = nomask
  893. else:
  894. ma = getmaskarray(a)
  895. mb = getmaskarray(b)
  896. m = umath.logical_or.outer(ma, mb)
  897. if (not m.ndim) and m:
  898. return masked
  899. if m is not nomask:
  900. np.copyto(d, da, where=m)
  901. if not d.shape:
  902. return d
  903. masked_d = d.view(get_masked_subclass(a, b))
  904. masked_d._mask = m
  905. return masked_d
  906. def accumulate(self, target, axis=0):
  907. """Accumulate `target` along `axis` after filling with y fill
  908. value.
  909. """
  910. tclass = get_masked_subclass(target)
  911. t = filled(target, self.filly)
  912. result = self.f.accumulate(t, axis)
  913. masked_result = result.view(tclass)
  914. return masked_result
  915. class _DomainedBinaryOperation(_MaskedUFunc):
  916. """
  917. Define binary operations that have a domain, like divide.
  918. They have no reduce, outer or accumulate.
  919. Parameters
  920. ----------
  921. mbfunc : function
  922. The function for which to define a masked version. Made available
  923. as ``_DomainedBinaryOperation.f``.
  924. domain : class instance
  925. Default domain for the function. Should be one of the ``_Domain*``
  926. classes.
  927. fillx : scalar, optional
  928. Filling value for the first argument, default is 0.
  929. filly : scalar, optional
  930. Filling value for the second argument, default is 0.
  931. """
  932. def __init__(self, dbfunc, domain, fillx=0, filly=0):
  933. """abfunc(fillx, filly) must be defined.
  934. abfunc(x, filly) = x for all x to enable reduce.
  935. """
  936. super().__init__(dbfunc)
  937. self.domain = domain
  938. self.fillx = fillx
  939. self.filly = filly
  940. ufunc_domain[dbfunc] = domain
  941. ufunc_fills[dbfunc] = (fillx, filly)
  942. def __call__(self, a, b, *args, **kwargs):
  943. "Execute the call behavior."
  944. # Get the data
  945. (da, db) = (getdata(a), getdata(b))
  946. # Get the result
  947. with np.errstate(divide='ignore', invalid='ignore'):
  948. result = self.f(da, db, *args, **kwargs)
  949. # Get the mask as a combination of the source masks and invalid
  950. m = ~umath.isfinite(result)
  951. m |= getmask(a)
  952. m |= getmask(b)
  953. # Apply the domain
  954. domain = ufunc_domain.get(self.f, None)
  955. if domain is not None:
  956. m |= domain(da, db)
  957. # Take care of the scalar case first
  958. if not m.ndim:
  959. if m:
  960. return masked
  961. else:
  962. return result
  963. # When the mask is True, put back da if possible
  964. # any errors, just abort; impossible to guarantee masked values
  965. try:
  966. np.copyto(result, 0, casting='unsafe', where=m)
  967. # avoid using "*" since this may be overlaid
  968. masked_da = umath.multiply(m, da)
  969. # only add back if it can be cast safely
  970. if np.can_cast(masked_da.dtype, result.dtype, casting='safe'):
  971. result += masked_da
  972. except Exception:
  973. pass
  974. # Transforms to a (subclass of) MaskedArray
  975. masked_result = result.view(get_masked_subclass(a, b))
  976. masked_result._mask = m
  977. if isinstance(a, MaskedArray):
  978. masked_result._update_from(a)
  979. elif isinstance(b, MaskedArray):
  980. masked_result._update_from(b)
  981. return masked_result
  982. # Unary ufuncs
  983. exp = _MaskedUnaryOperation(umath.exp)
  984. conjugate = _MaskedUnaryOperation(umath.conjugate)
  985. sin = _MaskedUnaryOperation(umath.sin)
  986. cos = _MaskedUnaryOperation(umath.cos)
  987. arctan = _MaskedUnaryOperation(umath.arctan)
  988. arcsinh = _MaskedUnaryOperation(umath.arcsinh)
  989. sinh = _MaskedUnaryOperation(umath.sinh)
  990. cosh = _MaskedUnaryOperation(umath.cosh)
  991. tanh = _MaskedUnaryOperation(umath.tanh)
  992. abs = absolute = _MaskedUnaryOperation(umath.absolute)
  993. angle = _MaskedUnaryOperation(angle) # from numpy.lib.function_base
  994. fabs = _MaskedUnaryOperation(umath.fabs)
  995. negative = _MaskedUnaryOperation(umath.negative)
  996. floor = _MaskedUnaryOperation(umath.floor)
  997. ceil = _MaskedUnaryOperation(umath.ceil)
  998. around = _MaskedUnaryOperation(np.round_)
  999. logical_not = _MaskedUnaryOperation(umath.logical_not)
  1000. # Domained unary ufuncs
  1001. sqrt = _MaskedUnaryOperation(umath.sqrt, 0.0,
  1002. _DomainGreaterEqual(0.0))
  1003. log = _MaskedUnaryOperation(umath.log, 1.0,
  1004. _DomainGreater(0.0))
  1005. log2 = _MaskedUnaryOperation(umath.log2, 1.0,
  1006. _DomainGreater(0.0))
  1007. log10 = _MaskedUnaryOperation(umath.log10, 1.0,
  1008. _DomainGreater(0.0))
  1009. tan = _MaskedUnaryOperation(umath.tan, 0.0,
  1010. _DomainTan(1e-35))
  1011. arcsin = _MaskedUnaryOperation(umath.arcsin, 0.0,
  1012. _DomainCheckInterval(-1.0, 1.0))
  1013. arccos = _MaskedUnaryOperation(umath.arccos, 0.0,
  1014. _DomainCheckInterval(-1.0, 1.0))
  1015. arccosh = _MaskedUnaryOperation(umath.arccosh, 1.0,
  1016. _DomainGreaterEqual(1.0))
  1017. arctanh = _MaskedUnaryOperation(umath.arctanh, 0.0,
  1018. _DomainCheckInterval(-1.0 + 1e-15, 1.0 - 1e-15))
  1019. # Binary ufuncs
  1020. add = _MaskedBinaryOperation(umath.add)
  1021. subtract = _MaskedBinaryOperation(umath.subtract)
  1022. multiply = _MaskedBinaryOperation(umath.multiply, 1, 1)
  1023. arctan2 = _MaskedBinaryOperation(umath.arctan2, 0.0, 1.0)
  1024. equal = _MaskedBinaryOperation(umath.equal)
  1025. equal.reduce = None
  1026. not_equal = _MaskedBinaryOperation(umath.not_equal)
  1027. not_equal.reduce = None
  1028. less_equal = _MaskedBinaryOperation(umath.less_equal)
  1029. less_equal.reduce = None
  1030. greater_equal = _MaskedBinaryOperation(umath.greater_equal)
  1031. greater_equal.reduce = None
  1032. less = _MaskedBinaryOperation(umath.less)
  1033. less.reduce = None
  1034. greater = _MaskedBinaryOperation(umath.greater)
  1035. greater.reduce = None
  1036. logical_and = _MaskedBinaryOperation(umath.logical_and)
  1037. alltrue = _MaskedBinaryOperation(umath.logical_and, 1, 1).reduce
  1038. logical_or = _MaskedBinaryOperation(umath.logical_or)
  1039. sometrue = logical_or.reduce
  1040. logical_xor = _MaskedBinaryOperation(umath.logical_xor)
  1041. bitwise_and = _MaskedBinaryOperation(umath.bitwise_and)
  1042. bitwise_or = _MaskedBinaryOperation(umath.bitwise_or)
  1043. bitwise_xor = _MaskedBinaryOperation(umath.bitwise_xor)
  1044. hypot = _MaskedBinaryOperation(umath.hypot)
  1045. # Domained binary ufuncs
  1046. divide = _DomainedBinaryOperation(umath.divide, _DomainSafeDivide(), 0, 1)
  1047. true_divide = _DomainedBinaryOperation(umath.true_divide,
  1048. _DomainSafeDivide(), 0, 1)
  1049. floor_divide = _DomainedBinaryOperation(umath.floor_divide,
  1050. _DomainSafeDivide(), 0, 1)
  1051. remainder = _DomainedBinaryOperation(umath.remainder,
  1052. _DomainSafeDivide(), 0, 1)
  1053. fmod = _DomainedBinaryOperation(umath.fmod, _DomainSafeDivide(), 0, 1)
  1054. mod = _DomainedBinaryOperation(umath.mod, _DomainSafeDivide(), 0, 1)
  1055. ###############################################################################
  1056. # Mask creation functions #
  1057. ###############################################################################
  1058. def _replace_dtype_fields_recursive(dtype, primitive_dtype):
  1059. "Private function allowing recursion in _replace_dtype_fields."
  1060. _recurse = _replace_dtype_fields_recursive
  1061. # Do we have some name fields ?
  1062. if dtype.names is not None:
  1063. descr = []
  1064. for name in dtype.names:
  1065. field = dtype.fields[name]
  1066. if len(field) == 3:
  1067. # Prepend the title to the name
  1068. name = (field[-1], name)
  1069. descr.append((name, _recurse(field[0], primitive_dtype)))
  1070. new_dtype = np.dtype(descr)
  1071. # Is this some kind of composite a la (float,2)
  1072. elif dtype.subdtype:
  1073. descr = list(dtype.subdtype)
  1074. descr[0] = _recurse(dtype.subdtype[0], primitive_dtype)
  1075. new_dtype = np.dtype(tuple(descr))
  1076. # this is a primitive type, so do a direct replacement
  1077. else:
  1078. new_dtype = primitive_dtype
  1079. # preserve identity of dtypes
  1080. if new_dtype == dtype:
  1081. new_dtype = dtype
  1082. return new_dtype
  1083. def _replace_dtype_fields(dtype, primitive_dtype):
  1084. """
  1085. Construct a dtype description list from a given dtype.
  1086. Returns a new dtype object, with all fields and subtypes in the given type
  1087. recursively replaced with `primitive_dtype`.
  1088. Arguments are coerced to dtypes first.
  1089. """
  1090. dtype = np.dtype(dtype)
  1091. primitive_dtype = np.dtype(primitive_dtype)
  1092. return _replace_dtype_fields_recursive(dtype, primitive_dtype)
  1093. def make_mask_descr(ndtype):
  1094. """
  1095. Construct a dtype description list from a given dtype.
  1096. Returns a new dtype object, with the type of all fields in `ndtype` to a
  1097. boolean type. Field names are not altered.
  1098. Parameters
  1099. ----------
  1100. ndtype : dtype
  1101. The dtype to convert.
  1102. Returns
  1103. -------
  1104. result : dtype
  1105. A dtype that looks like `ndtype`, the type of all fields is boolean.
  1106. Examples
  1107. --------
  1108. >>> import numpy.ma as ma
  1109. >>> dtype = np.dtype({'names':['foo', 'bar'],
  1110. ... 'formats':[np.float32, np.int64]})
  1111. >>> dtype
  1112. dtype([('foo', '<f4'), ('bar', '<i8')])
  1113. >>> ma.make_mask_descr(dtype)
  1114. dtype([('foo', '|b1'), ('bar', '|b1')])
  1115. >>> ma.make_mask_descr(np.float32)
  1116. dtype('bool')
  1117. """
  1118. return _replace_dtype_fields(ndtype, MaskType)
  1119. def getmask(a):
  1120. """
  1121. Return the mask of a masked array, or nomask.
  1122. Return the mask of `a` as an ndarray if `a` is a `MaskedArray` and the
  1123. mask is not `nomask`, else return `nomask`. To guarantee a full array
  1124. of booleans of the same shape as a, use `getmaskarray`.
  1125. Parameters
  1126. ----------
  1127. a : array_like
  1128. Input `MaskedArray` for which the mask is required.
  1129. See Also
  1130. --------
  1131. getdata : Return the data of a masked array as an ndarray.
  1132. getmaskarray : Return the mask of a masked array, or full array of False.
  1133. Examples
  1134. --------
  1135. >>> import numpy.ma as ma
  1136. >>> a = ma.masked_equal([[1,2],[3,4]], 2)
  1137. >>> a
  1138. masked_array(
  1139. data=[[1, --],
  1140. [3, 4]],
  1141. mask=[[False, True],
  1142. [False, False]],
  1143. fill_value=2)
  1144. >>> ma.getmask(a)
  1145. array([[False, True],
  1146. [False, False]])
  1147. Equivalently use the `MaskedArray` `mask` attribute.
  1148. >>> a.mask
  1149. array([[False, True],
  1150. [False, False]])
  1151. Result when mask == `nomask`
  1152. >>> b = ma.masked_array([[1,2],[3,4]])
  1153. >>> b
  1154. masked_array(
  1155. data=[[1, 2],
  1156. [3, 4]],
  1157. mask=False,
  1158. fill_value=999999)
  1159. >>> ma.nomask
  1160. False
  1161. >>> ma.getmask(b) == ma.nomask
  1162. True
  1163. >>> b.mask == ma.nomask
  1164. True
  1165. """
  1166. return getattr(a, '_mask', nomask)
  1167. get_mask = getmask
  1168. def getmaskarray(arr):
  1169. """
  1170. Return the mask of a masked array, or full boolean array of False.
  1171. Return the mask of `arr` as an ndarray if `arr` is a `MaskedArray` and
  1172. the mask is not `nomask`, else return a full boolean array of False of
  1173. the same shape as `arr`.
  1174. Parameters
  1175. ----------
  1176. arr : array_like
  1177. Input `MaskedArray` for which the mask is required.
  1178. See Also
  1179. --------
  1180. getmask : Return the mask of a masked array, or nomask.
  1181. getdata : Return the data of a masked array as an ndarray.
  1182. Examples
  1183. --------
  1184. >>> import numpy.ma as ma
  1185. >>> a = ma.masked_equal([[1,2],[3,4]], 2)
  1186. >>> a
  1187. masked_array(
  1188. data=[[1, --],
  1189. [3, 4]],
  1190. mask=[[False, True],
  1191. [False, False]],
  1192. fill_value=2)
  1193. >>> ma.getmaskarray(a)
  1194. array([[False, True],
  1195. [False, False]])
  1196. Result when mask == ``nomask``
  1197. >>> b = ma.masked_array([[1,2],[3,4]])
  1198. >>> b
  1199. masked_array(
  1200. data=[[1, 2],
  1201. [3, 4]],
  1202. mask=False,
  1203. fill_value=999999)
  1204. >>> ma.getmaskarray(b)
  1205. array([[False, False],
  1206. [False, False]])
  1207. """
  1208. mask = getmask(arr)
  1209. if mask is nomask:
  1210. mask = make_mask_none(np.shape(arr), getattr(arr, 'dtype', None))
  1211. return mask
  1212. def is_mask(m):
  1213. """
  1214. Return True if m is a valid, standard mask.
  1215. This function does not check the contents of the input, only that the
  1216. type is MaskType. In particular, this function returns False if the
  1217. mask has a flexible dtype.
  1218. Parameters
  1219. ----------
  1220. m : array_like
  1221. Array to test.
  1222. Returns
  1223. -------
  1224. result : bool
  1225. True if `m.dtype.type` is MaskType, False otherwise.
  1226. See Also
  1227. --------
  1228. ma.isMaskedArray : Test whether input is an instance of MaskedArray.
  1229. Examples
  1230. --------
  1231. >>> import numpy.ma as ma
  1232. >>> m = ma.masked_equal([0, 1, 0, 2, 3], 0)
  1233. >>> m
  1234. masked_array(data=[--, 1, --, 2, 3],
  1235. mask=[ True, False, True, False, False],
  1236. fill_value=0)
  1237. >>> ma.is_mask(m)
  1238. False
  1239. >>> ma.is_mask(m.mask)
  1240. True
  1241. Input must be an ndarray (or have similar attributes)
  1242. for it to be considered a valid mask.
  1243. >>> m = [False, True, False]
  1244. >>> ma.is_mask(m)
  1245. False
  1246. >>> m = np.array([False, True, False])
  1247. >>> m
  1248. array([False, True, False])
  1249. >>> ma.is_mask(m)
  1250. True
  1251. Arrays with complex dtypes don't return True.
  1252. >>> dtype = np.dtype({'names':['monty', 'pithon'],
  1253. ... 'formats':[bool, bool]})
  1254. >>> dtype
  1255. dtype([('monty', '|b1'), ('pithon', '|b1')])
  1256. >>> m = np.array([(True, False), (False, True), (True, False)],
  1257. ... dtype=dtype)
  1258. >>> m
  1259. array([( True, False), (False, True), ( True, False)],
  1260. dtype=[('monty', '?'), ('pithon', '?')])
  1261. >>> ma.is_mask(m)
  1262. False
  1263. """
  1264. try:
  1265. return m.dtype.type is MaskType
  1266. except AttributeError:
  1267. return False
  1268. def _shrink_mask(m):
  1269. """
  1270. Shrink a mask to nomask if possible
  1271. """
  1272. if m.dtype.names is None and not m.any():
  1273. return nomask
  1274. else:
  1275. return m
  1276. def make_mask(m, copy=False, shrink=True, dtype=MaskType):
  1277. """
  1278. Create a boolean mask from an array.
  1279. Return `m` as a boolean mask, creating a copy if necessary or requested.
  1280. The function can accept any sequence that is convertible to integers,
  1281. or ``nomask``. Does not require that contents must be 0s and 1s, values
  1282. of 0 are interpreted as False, everything else as True.
  1283. Parameters
  1284. ----------
  1285. m : array_like
  1286. Potential mask.
  1287. copy : bool, optional
  1288. Whether to return a copy of `m` (True) or `m` itself (False).
  1289. shrink : bool, optional
  1290. Whether to shrink `m` to ``nomask`` if all its values are False.
  1291. dtype : dtype, optional
  1292. Data-type of the output mask. By default, the output mask has a
  1293. dtype of MaskType (bool). If the dtype is flexible, each field has
  1294. a boolean dtype. This is ignored when `m` is ``nomask``, in which
  1295. case ``nomask`` is always returned.
  1296. Returns
  1297. -------
  1298. result : ndarray
  1299. A boolean mask derived from `m`.
  1300. Examples
  1301. --------
  1302. >>> import numpy.ma as ma
  1303. >>> m = [True, False, True, True]
  1304. >>> ma.make_mask(m)
  1305. array([ True, False, True, True])
  1306. >>> m = [1, 0, 1, 1]
  1307. >>> ma.make_mask(m)
  1308. array([ True, False, True, True])
  1309. >>> m = [1, 0, 2, -3]
  1310. >>> ma.make_mask(m)
  1311. array([ True, False, True, True])
  1312. Effect of the `shrink` parameter.
  1313. >>> m = np.zeros(4)
  1314. >>> m
  1315. array([0., 0., 0., 0.])
  1316. >>> ma.make_mask(m)
  1317. False
  1318. >>> ma.make_mask(m, shrink=False)
  1319. array([False, False, False, False])
  1320. Using a flexible `dtype`.
  1321. >>> m = [1, 0, 1, 1]
  1322. >>> n = [0, 1, 0, 0]
  1323. >>> arr = []
  1324. >>> for man, mouse in zip(m, n):
  1325. ... arr.append((man, mouse))
  1326. >>> arr
  1327. [(1, 0), (0, 1), (1, 0), (1, 0)]
  1328. >>> dtype = np.dtype({'names':['man', 'mouse'],
  1329. ... 'formats':[np.int64, np.int64]})
  1330. >>> arr = np.array(arr, dtype=dtype)
  1331. >>> arr
  1332. array([(1, 0), (0, 1), (1, 0), (1, 0)],
  1333. dtype=[('man', '<i8'), ('mouse', '<i8')])
  1334. >>> ma.make_mask(arr, dtype=dtype)
  1335. array([(True, False), (False, True), (True, False), (True, False)],
  1336. dtype=[('man', '|b1'), ('mouse', '|b1')])
  1337. """
  1338. if m is nomask:
  1339. return nomask
  1340. # Make sure the input dtype is valid.
  1341. dtype = make_mask_descr(dtype)
  1342. # legacy boolean special case: "existence of fields implies true"
  1343. if isinstance(m, ndarray) and m.dtype.fields and dtype == np.bool_:
  1344. return np.ones(m.shape, dtype=dtype)
  1345. # Fill the mask in case there are missing data; turn it into an ndarray.
  1346. result = np.array(filled(m, True), copy=copy, dtype=dtype, subok=True)
  1347. # Bas les masques !
  1348. if shrink:
  1349. result = _shrink_mask(result)
  1350. return result
  1351. def make_mask_none(newshape, dtype=None):
  1352. """
  1353. Return a boolean mask of the given shape, filled with False.
  1354. This function returns a boolean ndarray with all entries False, that can
  1355. be used in common mask manipulations. If a complex dtype is specified, the
  1356. type of each field is converted to a boolean type.
  1357. Parameters
  1358. ----------
  1359. newshape : tuple
  1360. A tuple indicating the shape of the mask.
  1361. dtype : {None, dtype}, optional
  1362. If None, use a MaskType instance. Otherwise, use a new datatype with
  1363. the same fields as `dtype`, converted to boolean types.
  1364. Returns
  1365. -------
  1366. result : ndarray
  1367. An ndarray of appropriate shape and dtype, filled with False.
  1368. See Also
  1369. --------
  1370. make_mask : Create a boolean mask from an array.
  1371. make_mask_descr : Construct a dtype description list from a given dtype.
  1372. Examples
  1373. --------
  1374. >>> import numpy.ma as ma
  1375. >>> ma.make_mask_none((3,))
  1376. array([False, False, False])
  1377. Defining a more complex dtype.
  1378. >>> dtype = np.dtype({'names':['foo', 'bar'],
  1379. ... 'formats':[np.float32, np.int64]})
  1380. >>> dtype
  1381. dtype([('foo', '<f4'), ('bar', '<i8')])
  1382. >>> ma.make_mask_none((3,), dtype=dtype)
  1383. array([(False, False), (False, False), (False, False)],
  1384. dtype=[('foo', '|b1'), ('bar', '|b1')])
  1385. """
  1386. if dtype is None:
  1387. result = np.zeros(newshape, dtype=MaskType)
  1388. else:
  1389. result = np.zeros(newshape, dtype=make_mask_descr(dtype))
  1390. return result
  1391. def _recursive_mask_or(m1, m2, newmask):
  1392. names = m1.dtype.names
  1393. for name in names:
  1394. current1 = m1[name]
  1395. if current1.dtype.names is not None:
  1396. _recursive_mask_or(current1, m2[name], newmask[name])
  1397. else:
  1398. umath.logical_or(current1, m2[name], newmask[name])
  1399. def mask_or(m1, m2, copy=False, shrink=True):
  1400. """
  1401. Combine two masks with the ``logical_or`` operator.
  1402. The result may be a view on `m1` or `m2` if the other is `nomask`
  1403. (i.e. False).
  1404. Parameters
  1405. ----------
  1406. m1, m2 : array_like
  1407. Input masks.
  1408. copy : bool, optional
  1409. If copy is False and one of the inputs is `nomask`, return a view
  1410. of the other input mask. Defaults to False.
  1411. shrink : bool, optional
  1412. Whether to shrink the output to `nomask` if all its values are
  1413. False. Defaults to True.
  1414. Returns
  1415. -------
  1416. mask : output mask
  1417. The result masks values that are masked in either `m1` or `m2`.
  1418. Raises
  1419. ------
  1420. ValueError
  1421. If `m1` and `m2` have different flexible dtypes.
  1422. Examples
  1423. --------
  1424. >>> m1 = np.ma.make_mask([0, 1, 1, 0])
  1425. >>> m2 = np.ma.make_mask([1, 0, 0, 0])
  1426. >>> np.ma.mask_or(m1, m2)
  1427. array([ True, True, True, False])
  1428. """
  1429. if (m1 is nomask) or (m1 is False):
  1430. dtype = getattr(m2, 'dtype', MaskType)
  1431. return make_mask(m2, copy=copy, shrink=shrink, dtype=dtype)
  1432. if (m2 is nomask) or (m2 is False):
  1433. dtype = getattr(m1, 'dtype', MaskType)
  1434. return make_mask(m1, copy=copy, shrink=shrink, dtype=dtype)
  1435. if m1 is m2 and is_mask(m1):
  1436. return m1
  1437. (dtype1, dtype2) = (getattr(m1, 'dtype', None), getattr(m2, 'dtype', None))
  1438. if dtype1 != dtype2:
  1439. raise ValueError("Incompatible dtypes '%s'<>'%s'" % (dtype1, dtype2))
  1440. if dtype1.names is not None:
  1441. # Allocate an output mask array with the properly broadcast shape.
  1442. newmask = np.empty(np.broadcast(m1, m2).shape, dtype1)
  1443. _recursive_mask_or(m1, m2, newmask)
  1444. return newmask
  1445. return make_mask(umath.logical_or(m1, m2), copy=copy, shrink=shrink)
  1446. def flatten_mask(mask):
  1447. """
  1448. Returns a completely flattened version of the mask, where nested fields
  1449. are collapsed.
  1450. Parameters
  1451. ----------
  1452. mask : array_like
  1453. Input array, which will be interpreted as booleans.
  1454. Returns
  1455. -------
  1456. flattened_mask : ndarray of bools
  1457. The flattened input.
  1458. Examples
  1459. --------
  1460. >>> mask = np.array([0, 0, 1])
  1461. >>> np.ma.flatten_mask(mask)
  1462. array([False, False, True])
  1463. >>> mask = np.array([(0, 0), (0, 1)], dtype=[('a', bool), ('b', bool)])
  1464. >>> np.ma.flatten_mask(mask)
  1465. array([False, False, False, True])
  1466. >>> mdtype = [('a', bool), ('b', [('ba', bool), ('bb', bool)])]
  1467. >>> mask = np.array([(0, (0, 0)), (0, (0, 1))], dtype=mdtype)
  1468. >>> np.ma.flatten_mask(mask)
  1469. array([False, False, False, False, False, True])
  1470. """
  1471. def _flatmask(mask):
  1472. "Flatten the mask and returns a (maybe nested) sequence of booleans."
  1473. mnames = mask.dtype.names
  1474. if mnames is not None:
  1475. return [flatten_mask(mask[name]) for name in mnames]
  1476. else:
  1477. return mask
  1478. def _flatsequence(sequence):
  1479. "Generates a flattened version of the sequence."
  1480. try:
  1481. for element in sequence:
  1482. if hasattr(element, '__iter__'):
  1483. yield from _flatsequence(element)
  1484. else:
  1485. yield element
  1486. except TypeError:
  1487. yield sequence
  1488. mask = np.asarray(mask)
  1489. flattened = _flatsequence(_flatmask(mask))
  1490. return np.array([_ for _ in flattened], dtype=bool)
  1491. def _check_mask_axis(mask, axis, keepdims=np._NoValue):
  1492. "Check whether there are masked values along the given axis"
  1493. kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims}
  1494. if mask is not nomask:
  1495. return mask.all(axis=axis, **kwargs)
  1496. return nomask
  1497. ###############################################################################
  1498. # Masking functions #
  1499. ###############################################################################
  1500. def masked_where(condition, a, copy=True):
  1501. """
  1502. Mask an array where a condition is met.
  1503. Return `a` as an array masked where `condition` is True.
  1504. Any masked values of `a` or `condition` are also masked in the output.
  1505. Parameters
  1506. ----------
  1507. condition : array_like
  1508. Masking condition. When `condition` tests floating point values for
  1509. equality, consider using ``masked_values`` instead.
  1510. a : array_like
  1511. Array to mask.
  1512. copy : bool
  1513. If True (default) make a copy of `a` in the result. If False modify
  1514. `a` in place and return a view.
  1515. Returns
  1516. -------
  1517. result : MaskedArray
  1518. The result of masking `a` where `condition` is True.
  1519. See Also
  1520. --------
  1521. masked_values : Mask using floating point equality.
  1522. masked_equal : Mask where equal to a given value.
  1523. masked_not_equal : Mask where `not` equal to a given value.
  1524. masked_less_equal : Mask where less than or equal to a given value.
  1525. masked_greater_equal : Mask where greater than or equal to a given value.
  1526. masked_less : Mask where less than a given value.
  1527. masked_greater : Mask where greater than a given value.
  1528. masked_inside : Mask inside a given interval.
  1529. masked_outside : Mask outside a given interval.
  1530. masked_invalid : Mask invalid values (NaNs or infs).
  1531. Examples
  1532. --------
  1533. >>> import numpy.ma as ma
  1534. >>> a = np.arange(4)
  1535. >>> a
  1536. array([0, 1, 2, 3])
  1537. >>> ma.masked_where(a <= 2, a)
  1538. masked_array(data=[--, --, --, 3],
  1539. mask=[ True, True, True, False],
  1540. fill_value=999999)
  1541. Mask array `b` conditional on `a`.
  1542. >>> b = ['a', 'b', 'c', 'd']
  1543. >>> ma.masked_where(a == 2, b)
  1544. masked_array(data=['a', 'b', --, 'd'],
  1545. mask=[False, False, True, False],
  1546. fill_value='N/A',
  1547. dtype='<U1')
  1548. Effect of the `copy` argument.
  1549. >>> c = ma.masked_where(a <= 2, a)
  1550. >>> c
  1551. masked_array(data=[--, --, --, 3],
  1552. mask=[ True, True, True, False],
  1553. fill_value=999999)
  1554. >>> c[0] = 99
  1555. >>> c
  1556. masked_array(data=[99, --, --, 3],
  1557. mask=[False, True, True, False],
  1558. fill_value=999999)
  1559. >>> a
  1560. array([0, 1, 2, 3])
  1561. >>> c = ma.masked_where(a <= 2, a, copy=False)
  1562. >>> c[0] = 99
  1563. >>> c
  1564. masked_array(data=[99, --, --, 3],
  1565. mask=[False, True, True, False],
  1566. fill_value=999999)
  1567. >>> a
  1568. array([99, 1, 2, 3])
  1569. When `condition` or `a` contain masked values.
  1570. >>> a = np.arange(4)
  1571. >>> a = ma.masked_where(a == 2, a)
  1572. >>> a
  1573. masked_array(data=[0, 1, --, 3],
  1574. mask=[False, False, True, False],
  1575. fill_value=999999)
  1576. >>> b = np.arange(4)
  1577. >>> b = ma.masked_where(b == 0, b)
  1578. >>> b
  1579. masked_array(data=[--, 1, 2, 3],
  1580. mask=[ True, False, False, False],
  1581. fill_value=999999)
  1582. >>> ma.masked_where(a == 3, b)
  1583. masked_array(data=[--, 1, --, --],
  1584. mask=[ True, False, True, True],
  1585. fill_value=999999)
  1586. """
  1587. # Make sure that condition is a valid standard-type mask.
  1588. cond = make_mask(condition, shrink=False)
  1589. a = np.array(a, copy=copy, subok=True)
  1590. (cshape, ashape) = (cond.shape, a.shape)
  1591. if cshape and cshape != ashape:
  1592. raise IndexError("Inconsistent shape between the condition and the input"
  1593. " (got %s and %s)" % (cshape, ashape))
  1594. if hasattr(a, '_mask'):
  1595. cond = mask_or(cond, a._mask)
  1596. cls = type(a)
  1597. else:
  1598. cls = MaskedArray
  1599. result = a.view(cls)
  1600. # Assign to *.mask so that structured masks are handled correctly.
  1601. result.mask = _shrink_mask(cond)
  1602. # There is no view of a boolean so when 'a' is a MaskedArray with nomask
  1603. # the update to the result's mask has no effect.
  1604. if not copy and hasattr(a, '_mask') and getmask(a) is nomask:
  1605. a._mask = result._mask.view()
  1606. return result
  1607. def masked_greater(x, value, copy=True):
  1608. """
  1609. Mask an array where greater than a given value.
  1610. This function is a shortcut to ``masked_where``, with
  1611. `condition` = (x > value).
  1612. See Also
  1613. --------
  1614. masked_where : Mask where a condition is met.
  1615. Examples
  1616. --------
  1617. >>> import numpy.ma as ma
  1618. >>> a = np.arange(4)
  1619. >>> a
  1620. array([0, 1, 2, 3])
  1621. >>> ma.masked_greater(a, 2)
  1622. masked_array(data=[0, 1, 2, --],
  1623. mask=[False, False, False, True],
  1624. fill_value=999999)
  1625. """
  1626. return masked_where(greater(x, value), x, copy=copy)
  1627. def masked_greater_equal(x, value, copy=True):
  1628. """
  1629. Mask an array where greater than or equal to a given value.
  1630. This function is a shortcut to ``masked_where``, with
  1631. `condition` = (x >= value).
  1632. See Also
  1633. --------
  1634. masked_where : Mask where a condition is met.
  1635. Examples
  1636. --------
  1637. >>> import numpy.ma as ma
  1638. >>> a = np.arange(4)
  1639. >>> a
  1640. array([0, 1, 2, 3])
  1641. >>> ma.masked_greater_equal(a, 2)
  1642. masked_array(data=[0, 1, --, --],
  1643. mask=[False, False, True, True],
  1644. fill_value=999999)
  1645. """
  1646. return masked_where(greater_equal(x, value), x, copy=copy)
  1647. def masked_less(x, value, copy=True):
  1648. """
  1649. Mask an array where less than a given value.
  1650. This function is a shortcut to ``masked_where``, with
  1651. `condition` = (x < value).
  1652. See Also
  1653. --------
  1654. masked_where : Mask where a condition is met.
  1655. Examples
  1656. --------
  1657. >>> import numpy.ma as ma
  1658. >>> a = np.arange(4)
  1659. >>> a
  1660. array([0, 1, 2, 3])
  1661. >>> ma.masked_less(a, 2)
  1662. masked_array(data=[--, --, 2, 3],
  1663. mask=[ True, True, False, False],
  1664. fill_value=999999)
  1665. """
  1666. return masked_where(less(x, value), x, copy=copy)
  1667. def masked_less_equal(x, value, copy=True):
  1668. """
  1669. Mask an array where less than or equal to a given value.
  1670. This function is a shortcut to ``masked_where``, with
  1671. `condition` = (x <= value).
  1672. See Also
  1673. --------
  1674. masked_where : Mask where a condition is met.
  1675. Examples
  1676. --------
  1677. >>> import numpy.ma as ma
  1678. >>> a = np.arange(4)
  1679. >>> a
  1680. array([0, 1, 2, 3])
  1681. >>> ma.masked_less_equal(a, 2)
  1682. masked_array(data=[--, --, --, 3],
  1683. mask=[ True, True, True, False],
  1684. fill_value=999999)
  1685. """
  1686. return masked_where(less_equal(x, value), x, copy=copy)
  1687. def masked_not_equal(x, value, copy=True):
  1688. """
  1689. Mask an array where `not` equal to a given value.
  1690. This function is a shortcut to ``masked_where``, with
  1691. `condition` = (x != value).
  1692. See Also
  1693. --------
  1694. masked_where : Mask where a condition is met.
  1695. Examples
  1696. --------
  1697. >>> import numpy.ma as ma
  1698. >>> a = np.arange(4)
  1699. >>> a
  1700. array([0, 1, 2, 3])
  1701. >>> ma.masked_not_equal(a, 2)
  1702. masked_array(data=[--, --, 2, --],
  1703. mask=[ True, True, False, True],
  1704. fill_value=999999)
  1705. """
  1706. return masked_where(not_equal(x, value), x, copy=copy)
  1707. def masked_equal(x, value, copy=True):
  1708. """
  1709. Mask an array where equal to a given value.
  1710. Return a MaskedArray, masked where the data in array `x` are
  1711. equal to `value`. The fill_value of the returned MaskedArray
  1712. is set to `value`.
  1713. For floating point arrays, consider using ``masked_values(x, value)``.
  1714. See Also
  1715. --------
  1716. masked_where : Mask where a condition is met.
  1717. masked_values : Mask using floating point equality.
  1718. Examples
  1719. --------
  1720. >>> import numpy.ma as ma
  1721. >>> a = np.arange(4)
  1722. >>> a
  1723. array([0, 1, 2, 3])
  1724. >>> ma.masked_equal(a, 2)
  1725. masked_array(data=[0, 1, --, 3],
  1726. mask=[False, False, True, False],
  1727. fill_value=2)
  1728. """
  1729. output = masked_where(equal(x, value), x, copy=copy)
  1730. output.fill_value = value
  1731. return output
  1732. def masked_inside(x, v1, v2, copy=True):
  1733. """
  1734. Mask an array inside a given interval.
  1735. Shortcut to ``masked_where``, where `condition` is True for `x` inside
  1736. the interval [v1,v2] (v1 <= x <= v2). The boundaries `v1` and `v2`
  1737. can be given in either order.
  1738. See Also
  1739. --------
  1740. masked_where : Mask where a condition is met.
  1741. Notes
  1742. -----
  1743. The array `x` is prefilled with its filling value.
  1744. Examples
  1745. --------
  1746. >>> import numpy.ma as ma
  1747. >>> x = [0.31, 1.2, 0.01, 0.2, -0.4, -1.1]
  1748. >>> ma.masked_inside(x, -0.3, 0.3)
  1749. masked_array(data=[0.31, 1.2, --, --, -0.4, -1.1],
  1750. mask=[False, False, True, True, False, False],
  1751. fill_value=1e+20)
  1752. The order of `v1` and `v2` doesn't matter.
  1753. >>> ma.masked_inside(x, 0.3, -0.3)
  1754. masked_array(data=[0.31, 1.2, --, --, -0.4, -1.1],
  1755. mask=[False, False, True, True, False, False],
  1756. fill_value=1e+20)
  1757. """
  1758. if v2 < v1:
  1759. (v1, v2) = (v2, v1)
  1760. xf = filled(x)
  1761. condition = (xf >= v1) & (xf <= v2)
  1762. return masked_where(condition, x, copy=copy)
  1763. def masked_outside(x, v1, v2, copy=True):
  1764. """
  1765. Mask an array outside a given interval.
  1766. Shortcut to ``masked_where``, where `condition` is True for `x` outside
  1767. the interval [v1,v2] (x < v1)|(x > v2).
  1768. The boundaries `v1` and `v2` can be given in either order.
  1769. See Also
  1770. --------
  1771. masked_where : Mask where a condition is met.
  1772. Notes
  1773. -----
  1774. The array `x` is prefilled with its filling value.
  1775. Examples
  1776. --------
  1777. >>> import numpy.ma as ma
  1778. >>> x = [0.31, 1.2, 0.01, 0.2, -0.4, -1.1]
  1779. >>> ma.masked_outside(x, -0.3, 0.3)
  1780. masked_array(data=[--, --, 0.01, 0.2, --, --],
  1781. mask=[ True, True, False, False, True, True],
  1782. fill_value=1e+20)
  1783. The order of `v1` and `v2` doesn't matter.
  1784. >>> ma.masked_outside(x, 0.3, -0.3)
  1785. masked_array(data=[--, --, 0.01, 0.2, --, --],
  1786. mask=[ True, True, False, False, True, True],
  1787. fill_value=1e+20)
  1788. """
  1789. if v2 < v1:
  1790. (v1, v2) = (v2, v1)
  1791. xf = filled(x)
  1792. condition = (xf < v1) | (xf > v2)
  1793. return masked_where(condition, x, copy=copy)
  1794. def masked_object(x, value, copy=True, shrink=True):
  1795. """
  1796. Mask the array `x` where the data are exactly equal to value.
  1797. This function is similar to `masked_values`, but only suitable
  1798. for object arrays: for floating point, use `masked_values` instead.
  1799. Parameters
  1800. ----------
  1801. x : array_like
  1802. Array to mask
  1803. value : object
  1804. Comparison value
  1805. copy : {True, False}, optional
  1806. Whether to return a copy of `x`.
  1807. shrink : {True, False}, optional
  1808. Whether to collapse a mask full of False to nomask
  1809. Returns
  1810. -------
  1811. result : MaskedArray
  1812. The result of masking `x` where equal to `value`.
  1813. See Also
  1814. --------
  1815. masked_where : Mask where a condition is met.
  1816. masked_equal : Mask where equal to a given value (integers).
  1817. masked_values : Mask using floating point equality.
  1818. Examples
  1819. --------
  1820. >>> import numpy.ma as ma
  1821. >>> food = np.array(['green_eggs', 'ham'], dtype=object)
  1822. >>> # don't eat spoiled food
  1823. >>> eat = ma.masked_object(food, 'green_eggs')
  1824. >>> eat
  1825. masked_array(data=[--, 'ham'],
  1826. mask=[ True, False],
  1827. fill_value='green_eggs',
  1828. dtype=object)
  1829. >>> # plain ol` ham is boring
  1830. >>> fresh_food = np.array(['cheese', 'ham', 'pineapple'], dtype=object)
  1831. >>> eat = ma.masked_object(fresh_food, 'green_eggs')
  1832. >>> eat
  1833. masked_array(data=['cheese', 'ham', 'pineapple'],
  1834. mask=False,
  1835. fill_value='green_eggs',
  1836. dtype=object)
  1837. Note that `mask` is set to ``nomask`` if possible.
  1838. >>> eat
  1839. masked_array(data=['cheese', 'ham', 'pineapple'],
  1840. mask=False,
  1841. fill_value='green_eggs',
  1842. dtype=object)
  1843. """
  1844. if isMaskedArray(x):
  1845. condition = umath.equal(x._data, value)
  1846. mask = x._mask
  1847. else:
  1848. condition = umath.equal(np.asarray(x), value)
  1849. mask = nomask
  1850. mask = mask_or(mask, make_mask(condition, shrink=shrink))
  1851. return masked_array(x, mask=mask, copy=copy, fill_value=value)
  1852. def masked_values(x, value, rtol=1e-5, atol=1e-8, copy=True, shrink=True):
  1853. """
  1854. Mask using floating point equality.
  1855. Return a MaskedArray, masked where the data in array `x` are approximately
  1856. equal to `value`, determined using `isclose`. The default tolerances for
  1857. `masked_values` are the same as those for `isclose`.
  1858. For integer types, exact equality is used, in the same way as
  1859. `masked_equal`.
  1860. The fill_value is set to `value` and the mask is set to ``nomask`` if
  1861. possible.
  1862. Parameters
  1863. ----------
  1864. x : array_like
  1865. Array to mask.
  1866. value : float
  1867. Masking value.
  1868. rtol, atol : float, optional
  1869. Tolerance parameters passed on to `isclose`
  1870. copy : bool, optional
  1871. Whether to return a copy of `x`.
  1872. shrink : bool, optional
  1873. Whether to collapse a mask full of False to ``nomask``.
  1874. Returns
  1875. -------
  1876. result : MaskedArray
  1877. The result of masking `x` where approximately equal to `value`.
  1878. See Also
  1879. --------
  1880. masked_where : Mask where a condition is met.
  1881. masked_equal : Mask where equal to a given value (integers).
  1882. Examples
  1883. --------
  1884. >>> import numpy.ma as ma
  1885. >>> x = np.array([1, 1.1, 2, 1.1, 3])
  1886. >>> ma.masked_values(x, 1.1)
  1887. masked_array(data=[1.0, --, 2.0, --, 3.0],
  1888. mask=[False, True, False, True, False],
  1889. fill_value=1.1)
  1890. Note that `mask` is set to ``nomask`` if possible.
  1891. >>> ma.masked_values(x, 2.1)
  1892. masked_array(data=[1. , 1.1, 2. , 1.1, 3. ],
  1893. mask=False,
  1894. fill_value=2.1)
  1895. Unlike `masked_equal`, `masked_values` can perform approximate equalities.
  1896. >>> ma.masked_values(x, 2.1, atol=1e-1)
  1897. masked_array(data=[1.0, 1.1, --, 1.1, 3.0],
  1898. mask=[False, False, True, False, False],
  1899. fill_value=2.1)
  1900. """
  1901. xnew = filled(x, value)
  1902. if np.issubdtype(xnew.dtype, np.floating):
  1903. mask = np.isclose(xnew, value, atol=atol, rtol=rtol)
  1904. else:
  1905. mask = umath.equal(xnew, value)
  1906. ret = masked_array(xnew, mask=mask, copy=copy, fill_value=value)
  1907. if shrink:
  1908. ret.shrink_mask()
  1909. return ret
  1910. def masked_invalid(a, copy=True):
  1911. """
  1912. Mask an array where invalid values occur (NaNs or infs).
  1913. This function is a shortcut to ``masked_where``, with
  1914. `condition` = ~(np.isfinite(a)). Any pre-existing mask is conserved.
  1915. Only applies to arrays with a dtype where NaNs or infs make sense
  1916. (i.e. floating point types), but accepts any array_like object.
  1917. See Also
  1918. --------
  1919. masked_where : Mask where a condition is met.
  1920. Examples
  1921. --------
  1922. >>> import numpy.ma as ma
  1923. >>> a = np.arange(5, dtype=float)
  1924. >>> a[2] = np.NaN
  1925. >>> a[3] = np.PINF
  1926. >>> a
  1927. array([ 0., 1., nan, inf, 4.])
  1928. >>> ma.masked_invalid(a)
  1929. masked_array(data=[0.0, 1.0, --, --, 4.0],
  1930. mask=[False, False, True, True, False],
  1931. fill_value=1e+20)
  1932. """
  1933. a = np.array(a, copy=False, subok=True)
  1934. res = masked_where(~(np.isfinite(a)), a, copy=copy)
  1935. # masked_invalid previously never returned nomask as a mask and doing so
  1936. # threw off matplotlib (gh-22842). So use shrink=False:
  1937. if res._mask is nomask:
  1938. res._mask = make_mask_none(res.shape, res.dtype)
  1939. return res
  1940. ###############################################################################
  1941. # Printing options #
  1942. ###############################################################################
  1943. class _MaskedPrintOption:
  1944. """
  1945. Handle the string used to represent missing data in a masked array.
  1946. """
  1947. def __init__(self, display):
  1948. """
  1949. Create the masked_print_option object.
  1950. """
  1951. self._display = display
  1952. self._enabled = True
  1953. def display(self):
  1954. """
  1955. Display the string to print for masked values.
  1956. """
  1957. return self._display
  1958. def set_display(self, s):
  1959. """
  1960. Set the string to print for masked values.
  1961. """
  1962. self._display = s
  1963. def enabled(self):
  1964. """
  1965. Is the use of the display value enabled?
  1966. """
  1967. return self._enabled
  1968. def enable(self, shrink=1):
  1969. """
  1970. Set the enabling shrink to `shrink`.
  1971. """
  1972. self._enabled = shrink
  1973. def __str__(self):
  1974. return str(self._display)
  1975. __repr__ = __str__
  1976. # if you single index into a masked location you get this object.
  1977. masked_print_option = _MaskedPrintOption('--')
  1978. def _recursive_printoption(result, mask, printopt):
  1979. """
  1980. Puts printoptions in result where mask is True.
  1981. Private function allowing for recursion
  1982. """
  1983. names = result.dtype.names
  1984. if names is not None:
  1985. for name in names:
  1986. curdata = result[name]
  1987. curmask = mask[name]
  1988. _recursive_printoption(curdata, curmask, printopt)
  1989. else:
  1990. np.copyto(result, printopt, where=mask)
  1991. return
  1992. # For better or worse, these end in a newline
  1993. _legacy_print_templates = dict(
  1994. long_std=textwrap.dedent("""\
  1995. masked_%(name)s(data =
  1996. %(data)s,
  1997. %(nlen)s mask =
  1998. %(mask)s,
  1999. %(nlen)s fill_value = %(fill)s)
  2000. """),
  2001. long_flx=textwrap.dedent("""\
  2002. masked_%(name)s(data =
  2003. %(data)s,
  2004. %(nlen)s mask =
  2005. %(mask)s,
  2006. %(nlen)s fill_value = %(fill)s,
  2007. %(nlen)s dtype = %(dtype)s)
  2008. """),
  2009. short_std=textwrap.dedent("""\
  2010. masked_%(name)s(data = %(data)s,
  2011. %(nlen)s mask = %(mask)s,
  2012. %(nlen)s fill_value = %(fill)s)
  2013. """),
  2014. short_flx=textwrap.dedent("""\
  2015. masked_%(name)s(data = %(data)s,
  2016. %(nlen)s mask = %(mask)s,
  2017. %(nlen)s fill_value = %(fill)s,
  2018. %(nlen)s dtype = %(dtype)s)
  2019. """)
  2020. )
  2021. ###############################################################################
  2022. # MaskedArray class #
  2023. ###############################################################################
  2024. def _recursive_filled(a, mask, fill_value):
  2025. """
  2026. Recursively fill `a` with `fill_value`.
  2027. """
  2028. names = a.dtype.names
  2029. for name in names:
  2030. current = a[name]
  2031. if current.dtype.names is not None:
  2032. _recursive_filled(current, mask[name], fill_value[name])
  2033. else:
  2034. np.copyto(current, fill_value[name], where=mask[name])
  2035. def flatten_structured_array(a):
  2036. """
  2037. Flatten a structured array.
  2038. The data type of the output is chosen such that it can represent all of the
  2039. (nested) fields.
  2040. Parameters
  2041. ----------
  2042. a : structured array
  2043. Returns
  2044. -------
  2045. output : masked array or ndarray
  2046. A flattened masked array if the input is a masked array, otherwise a
  2047. standard ndarray.
  2048. Examples
  2049. --------
  2050. >>> ndtype = [('a', int), ('b', float)]
  2051. >>> a = np.array([(1, 1), (2, 2)], dtype=ndtype)
  2052. >>> np.ma.flatten_structured_array(a)
  2053. array([[1., 1.],
  2054. [2., 2.]])
  2055. """
  2056. def flatten_sequence(iterable):
  2057. """
  2058. Flattens a compound of nested iterables.
  2059. """
  2060. for elm in iter(iterable):
  2061. if hasattr(elm, '__iter__'):
  2062. yield from flatten_sequence(elm)
  2063. else:
  2064. yield elm
  2065. a = np.asanyarray(a)
  2066. inishape = a.shape
  2067. a = a.ravel()
  2068. if isinstance(a, MaskedArray):
  2069. out = np.array([tuple(flatten_sequence(d.item())) for d in a._data])
  2070. out = out.view(MaskedArray)
  2071. out._mask = np.array([tuple(flatten_sequence(d.item()))
  2072. for d in getmaskarray(a)])
  2073. else:
  2074. out = np.array([tuple(flatten_sequence(d.item())) for d in a])
  2075. if len(inishape) > 1:
  2076. newshape = list(out.shape)
  2077. newshape[0] = inishape
  2078. out.shape = tuple(flatten_sequence(newshape))
  2079. return out
  2080. def _arraymethod(funcname, onmask=True):
  2081. """
  2082. Return a class method wrapper around a basic array method.
  2083. Creates a class method which returns a masked array, where the new
  2084. ``_data`` array is the output of the corresponding basic method called
  2085. on the original ``_data``.
  2086. If `onmask` is True, the new mask is the output of the method called
  2087. on the initial mask. Otherwise, the new mask is just a reference
  2088. to the initial mask.
  2089. Parameters
  2090. ----------
  2091. funcname : str
  2092. Name of the function to apply on data.
  2093. onmask : bool
  2094. Whether the mask must be processed also (True) or left
  2095. alone (False). Default is True. Make available as `_onmask`
  2096. attribute.
  2097. Returns
  2098. -------
  2099. method : instancemethod
  2100. Class method wrapper of the specified basic array method.
  2101. """
  2102. def wrapped_method(self, *args, **params):
  2103. result = getattr(self._data, funcname)(*args, **params)
  2104. result = result.view(type(self))
  2105. result._update_from(self)
  2106. mask = self._mask
  2107. if not onmask:
  2108. result.__setmask__(mask)
  2109. elif mask is not nomask:
  2110. # __setmask__ makes a copy, which we don't want
  2111. result._mask = getattr(mask, funcname)(*args, **params)
  2112. return result
  2113. methdoc = getattr(ndarray, funcname, None) or getattr(np, funcname, None)
  2114. if methdoc is not None:
  2115. wrapped_method.__doc__ = methdoc.__doc__
  2116. wrapped_method.__name__ = funcname
  2117. return wrapped_method
  2118. class MaskedIterator:
  2119. """
  2120. Flat iterator object to iterate over masked arrays.
  2121. A `MaskedIterator` iterator is returned by ``x.flat`` for any masked array
  2122. `x`. It allows iterating over the array as if it were a 1-D array,
  2123. either in a for-loop or by calling its `next` method.
  2124. Iteration is done in C-contiguous style, with the last index varying the
  2125. fastest. The iterator can also be indexed using basic slicing or
  2126. advanced indexing.
  2127. See Also
  2128. --------
  2129. MaskedArray.flat : Return a flat iterator over an array.
  2130. MaskedArray.flatten : Returns a flattened copy of an array.
  2131. Notes
  2132. -----
  2133. `MaskedIterator` is not exported by the `ma` module. Instead of
  2134. instantiating a `MaskedIterator` directly, use `MaskedArray.flat`.
  2135. Examples
  2136. --------
  2137. >>> x = np.ma.array(arange(6).reshape(2, 3))
  2138. >>> fl = x.flat
  2139. >>> type(fl)
  2140. <class 'numpy.ma.core.MaskedIterator'>
  2141. >>> for item in fl:
  2142. ... print(item)
  2143. ...
  2144. 0
  2145. 1
  2146. 2
  2147. 3
  2148. 4
  2149. 5
  2150. Extracting more than a single element b indexing the `MaskedIterator`
  2151. returns a masked array:
  2152. >>> fl[2:4]
  2153. masked_array(data = [2 3],
  2154. mask = False,
  2155. fill_value = 999999)
  2156. """
  2157. def __init__(self, ma):
  2158. self.ma = ma
  2159. self.dataiter = ma._data.flat
  2160. if ma._mask is nomask:
  2161. self.maskiter = None
  2162. else:
  2163. self.maskiter = ma._mask.flat
  2164. def __iter__(self):
  2165. return self
  2166. def __getitem__(self, indx):
  2167. result = self.dataiter.__getitem__(indx).view(type(self.ma))
  2168. if self.maskiter is not None:
  2169. _mask = self.maskiter.__getitem__(indx)
  2170. if isinstance(_mask, ndarray):
  2171. # set shape to match that of data; this is needed for matrices
  2172. _mask.shape = result.shape
  2173. result._mask = _mask
  2174. elif isinstance(_mask, np.void):
  2175. return mvoid(result, mask=_mask, hardmask=self.ma._hardmask)
  2176. elif _mask: # Just a scalar, masked
  2177. return masked
  2178. return result
  2179. # This won't work if ravel makes a copy
  2180. def __setitem__(self, index, value):
  2181. self.dataiter[index] = getdata(value)
  2182. if self.maskiter is not None:
  2183. self.maskiter[index] = getmaskarray(value)
  2184. def __next__(self):
  2185. """
  2186. Return the next value, or raise StopIteration.
  2187. Examples
  2188. --------
  2189. >>> x = np.ma.array([3, 2], mask=[0, 1])
  2190. >>> fl = x.flat
  2191. >>> next(fl)
  2192. 3
  2193. >>> next(fl)
  2194. masked
  2195. >>> next(fl)
  2196. Traceback (most recent call last):
  2197. ...
  2198. StopIteration
  2199. """
  2200. d = next(self.dataiter)
  2201. if self.maskiter is not None:
  2202. m = next(self.maskiter)
  2203. if isinstance(m, np.void):
  2204. return mvoid(d, mask=m, hardmask=self.ma._hardmask)
  2205. elif m: # Just a scalar, masked
  2206. return masked
  2207. return d
  2208. class MaskedArray(ndarray):
  2209. """
  2210. An array class with possibly masked values.
  2211. Masked values of True exclude the corresponding element from any
  2212. computation.
  2213. Construction::
  2214. x = MaskedArray(data, mask=nomask, dtype=None, copy=False, subok=True,
  2215. ndmin=0, fill_value=None, keep_mask=True, hard_mask=None,
  2216. shrink=True, order=None)
  2217. Parameters
  2218. ----------
  2219. data : array_like
  2220. Input data.
  2221. mask : sequence, optional
  2222. Mask. Must be convertible to an array of booleans with the same
  2223. shape as `data`. True indicates a masked (i.e. invalid) data.
  2224. dtype : dtype, optional
  2225. Data type of the output.
  2226. If `dtype` is None, the type of the data argument (``data.dtype``)
  2227. is used. If `dtype` is not None and different from ``data.dtype``,
  2228. a copy is performed.
  2229. copy : bool, optional
  2230. Whether to copy the input data (True), or to use a reference instead.
  2231. Default is False.
  2232. subok : bool, optional
  2233. Whether to return a subclass of `MaskedArray` if possible (True) or a
  2234. plain `MaskedArray`. Default is True.
  2235. ndmin : int, optional
  2236. Minimum number of dimensions. Default is 0.
  2237. fill_value : scalar, optional
  2238. Value used to fill in the masked values when necessary.
  2239. If None, a default based on the data-type is used.
  2240. keep_mask : bool, optional
  2241. Whether to combine `mask` with the mask of the input data, if any
  2242. (True), or to use only `mask` for the output (False). Default is True.
  2243. hard_mask : bool, optional
  2244. Whether to use a hard mask or not. With a hard mask, masked values
  2245. cannot be unmasked. Default is False.
  2246. shrink : bool, optional
  2247. Whether to force compression of an empty mask. Default is True.
  2248. order : {'C', 'F', 'A'}, optional
  2249. Specify the order of the array. If order is 'C', then the array
  2250. will be in C-contiguous order (last-index varies the fastest).
  2251. If order is 'F', then the returned array will be in
  2252. Fortran-contiguous order (first-index varies the fastest).
  2253. If order is 'A' (default), then the returned array may be
  2254. in any order (either C-, Fortran-contiguous, or even discontiguous),
  2255. unless a copy is required, in which case it will be C-contiguous.
  2256. Examples
  2257. --------
  2258. The ``mask`` can be initialized with an array of boolean values
  2259. with the same shape as ``data``.
  2260. >>> data = np.arange(6).reshape((2, 3))
  2261. >>> np.ma.MaskedArray(data, mask=[[False, True, False],
  2262. ... [False, False, True]])
  2263. masked_array(
  2264. data=[[0, --, 2],
  2265. [3, 4, --]],
  2266. mask=[[False, True, False],
  2267. [False, False, True]],
  2268. fill_value=999999)
  2269. Alternatively, the ``mask`` can be initialized to homogeneous boolean
  2270. array with the same shape as ``data`` by passing in a scalar
  2271. boolean value:
  2272. >>> np.ma.MaskedArray(data, mask=False)
  2273. masked_array(
  2274. data=[[0, 1, 2],
  2275. [3, 4, 5]],
  2276. mask=[[False, False, False],
  2277. [False, False, False]],
  2278. fill_value=999999)
  2279. >>> np.ma.MaskedArray(data, mask=True)
  2280. masked_array(
  2281. data=[[--, --, --],
  2282. [--, --, --]],
  2283. mask=[[ True, True, True],
  2284. [ True, True, True]],
  2285. fill_value=999999,
  2286. dtype=int64)
  2287. .. note::
  2288. The recommended practice for initializing ``mask`` with a scalar
  2289. boolean value is to use ``True``/``False`` rather than
  2290. ``np.True_``/``np.False_``. The reason is :attr:`nomask`
  2291. is represented internally as ``np.False_``.
  2292. >>> np.False_ is np.ma.nomask
  2293. True
  2294. """
  2295. __array_priority__ = 15
  2296. _defaultmask = nomask
  2297. _defaulthardmask = False
  2298. _baseclass = ndarray
  2299. # Maximum number of elements per axis used when printing an array. The
  2300. # 1d case is handled separately because we need more values in this case.
  2301. _print_width = 100
  2302. _print_width_1d = 1500
  2303. def __new__(cls, data=None, mask=nomask, dtype=None, copy=False,
  2304. subok=True, ndmin=0, fill_value=None, keep_mask=True,
  2305. hard_mask=None, shrink=True, order=None):
  2306. """
  2307. Create a new masked array from scratch.
  2308. Notes
  2309. -----
  2310. A masked array can also be created by taking a .view(MaskedArray).
  2311. """
  2312. # Process data.
  2313. _data = np.array(data, dtype=dtype, copy=copy,
  2314. order=order, subok=True, ndmin=ndmin)
  2315. _baseclass = getattr(data, '_baseclass', type(_data))
  2316. # Check that we're not erasing the mask.
  2317. if isinstance(data, MaskedArray) and (data.shape != _data.shape):
  2318. copy = True
  2319. # Here, we copy the _view_, so that we can attach new properties to it
  2320. # we must never do .view(MaskedConstant), as that would create a new
  2321. # instance of np.ma.masked, which make identity comparison fail
  2322. if isinstance(data, cls) and subok and not isinstance(data, MaskedConstant):
  2323. _data = ndarray.view(_data, type(data))
  2324. else:
  2325. _data = ndarray.view(_data, cls)
  2326. # Handle the case where data is not a subclass of ndarray, but
  2327. # still has the _mask attribute like MaskedArrays
  2328. if hasattr(data, '_mask') and not isinstance(data, ndarray):
  2329. _data._mask = data._mask
  2330. # FIXME: should we set `_data._sharedmask = True`?
  2331. # Process mask.
  2332. # Type of the mask
  2333. mdtype = make_mask_descr(_data.dtype)
  2334. if mask is nomask:
  2335. # Case 1. : no mask in input.
  2336. # Erase the current mask ?
  2337. if not keep_mask:
  2338. # With a reduced version
  2339. if shrink:
  2340. _data._mask = nomask
  2341. # With full version
  2342. else:
  2343. _data._mask = np.zeros(_data.shape, dtype=mdtype)
  2344. # Check whether we missed something
  2345. elif isinstance(data, (tuple, list)):
  2346. try:
  2347. # If data is a sequence of masked array
  2348. mask = np.array(
  2349. [getmaskarray(np.asanyarray(m, dtype=_data.dtype))
  2350. for m in data], dtype=mdtype)
  2351. except (ValueError, TypeError):
  2352. # If data is nested
  2353. mask = nomask
  2354. # Force shrinking of the mask if needed (and possible)
  2355. if (mdtype == MaskType) and mask.any():
  2356. _data._mask = mask
  2357. _data._sharedmask = False
  2358. else:
  2359. _data._sharedmask = not copy
  2360. if copy:
  2361. _data._mask = _data._mask.copy()
  2362. # Reset the shape of the original mask
  2363. if getmask(data) is not nomask:
  2364. # gh-21022 encounters an issue here
  2365. # because data._mask.shape is not writeable, but
  2366. # the op was also pointless in that case, because
  2367. # the shapes were the same, so we can at least
  2368. # avoid that path
  2369. if data._mask.shape != data.shape:
  2370. data._mask.shape = data.shape
  2371. else:
  2372. # Case 2. : With a mask in input.
  2373. # If mask is boolean, create an array of True or False
  2374. # if users pass `mask=None` be forgiving here and cast it False
  2375. # for speed; although the default is `mask=nomask` and can differ.
  2376. if mask is None:
  2377. mask = False
  2378. if mask is True and mdtype == MaskType:
  2379. mask = np.ones(_data.shape, dtype=mdtype)
  2380. elif mask is False and mdtype == MaskType:
  2381. mask = np.zeros(_data.shape, dtype=mdtype)
  2382. else:
  2383. # Read the mask with the current mdtype
  2384. try:
  2385. mask = np.array(mask, copy=copy, dtype=mdtype)
  2386. # Or assume it's a sequence of bool/int
  2387. except TypeError:
  2388. mask = np.array([tuple([m] * len(mdtype)) for m in mask],
  2389. dtype=mdtype)
  2390. # Make sure the mask and the data have the same shape
  2391. if mask.shape != _data.shape:
  2392. (nd, nm) = (_data.size, mask.size)
  2393. if nm == 1:
  2394. mask = np.resize(mask, _data.shape)
  2395. elif nm == nd:
  2396. mask = np.reshape(mask, _data.shape)
  2397. else:
  2398. msg = "Mask and data not compatible: data size is %i, " + \
  2399. "mask size is %i."
  2400. raise MaskError(msg % (nd, nm))
  2401. copy = True
  2402. # Set the mask to the new value
  2403. if _data._mask is nomask:
  2404. _data._mask = mask
  2405. _data._sharedmask = not copy
  2406. else:
  2407. if not keep_mask:
  2408. _data._mask = mask
  2409. _data._sharedmask = not copy
  2410. else:
  2411. if _data.dtype.names is not None:
  2412. def _recursive_or(a, b):
  2413. "do a|=b on each field of a, recursively"
  2414. for name in a.dtype.names:
  2415. (af, bf) = (a[name], b[name])
  2416. if af.dtype.names is not None:
  2417. _recursive_or(af, bf)
  2418. else:
  2419. af |= bf
  2420. _recursive_or(_data._mask, mask)
  2421. else:
  2422. _data._mask = np.logical_or(mask, _data._mask)
  2423. _data._sharedmask = False
  2424. # Update fill_value.
  2425. if fill_value is None:
  2426. fill_value = getattr(data, '_fill_value', None)
  2427. # But don't run the check unless we have something to check.
  2428. if fill_value is not None:
  2429. _data._fill_value = _check_fill_value(fill_value, _data.dtype)
  2430. # Process extra options ..
  2431. if hard_mask is None:
  2432. _data._hardmask = getattr(data, '_hardmask', False)
  2433. else:
  2434. _data._hardmask = hard_mask
  2435. _data._baseclass = _baseclass
  2436. return _data
  2437. def _update_from(self, obj):
  2438. """
  2439. Copies some attributes of obj to self.
  2440. """
  2441. if isinstance(obj, ndarray):
  2442. _baseclass = type(obj)
  2443. else:
  2444. _baseclass = ndarray
  2445. # We need to copy the _basedict to avoid backward propagation
  2446. _optinfo = {}
  2447. _optinfo.update(getattr(obj, '_optinfo', {}))
  2448. _optinfo.update(getattr(obj, '_basedict', {}))
  2449. if not isinstance(obj, MaskedArray):
  2450. _optinfo.update(getattr(obj, '__dict__', {}))
  2451. _dict = dict(_fill_value=getattr(obj, '_fill_value', None),
  2452. _hardmask=getattr(obj, '_hardmask', False),
  2453. _sharedmask=getattr(obj, '_sharedmask', False),
  2454. _isfield=getattr(obj, '_isfield', False),
  2455. _baseclass=getattr(obj, '_baseclass', _baseclass),
  2456. _optinfo=_optinfo,
  2457. _basedict=_optinfo)
  2458. self.__dict__.update(_dict)
  2459. self.__dict__.update(_optinfo)
  2460. return
  2461. def __array_finalize__(self, obj):
  2462. """
  2463. Finalizes the masked array.
  2464. """
  2465. # Get main attributes.
  2466. self._update_from(obj)
  2467. # We have to decide how to initialize self.mask, based on
  2468. # obj.mask. This is very difficult. There might be some
  2469. # correspondence between the elements in the array we are being
  2470. # created from (= obj) and us. Or there might not. This method can
  2471. # be called in all kinds of places for all kinds of reasons -- could
  2472. # be empty_like, could be slicing, could be a ufunc, could be a view.
  2473. # The numpy subclassing interface simply doesn't give us any way
  2474. # to know, which means that at best this method will be based on
  2475. # guesswork and heuristics. To make things worse, there isn't even any
  2476. # clear consensus about what the desired behavior is. For instance,
  2477. # most users think that np.empty_like(marr) -- which goes via this
  2478. # method -- should return a masked array with an empty mask (see
  2479. # gh-3404 and linked discussions), but others disagree, and they have
  2480. # existing code which depends on empty_like returning an array that
  2481. # matches the input mask.
  2482. #
  2483. # Historically our algorithm was: if the template object mask had the
  2484. # same *number of elements* as us, then we used *it's mask object
  2485. # itself* as our mask, so that writes to us would also write to the
  2486. # original array. This is horribly broken in multiple ways.
  2487. #
  2488. # Now what we do instead is, if the template object mask has the same
  2489. # number of elements as us, and we do not have the same base pointer
  2490. # as the template object (b/c views like arr[...] should keep the same
  2491. # mask), then we make a copy of the template object mask and use
  2492. # that. This is also horribly broken but somewhat less so. Maybe.
  2493. if isinstance(obj, ndarray):
  2494. # XX: This looks like a bug -- shouldn't it check self.dtype
  2495. # instead?
  2496. if obj.dtype.names is not None:
  2497. _mask = getmaskarray(obj)
  2498. else:
  2499. _mask = getmask(obj)
  2500. # If self and obj point to exactly the same data, then probably
  2501. # self is a simple view of obj (e.g., self = obj[...]), so they
  2502. # should share the same mask. (This isn't 100% reliable, e.g. self
  2503. # could be the first row of obj, or have strange strides, but as a
  2504. # heuristic it's not bad.) In all other cases, we make a copy of
  2505. # the mask, so that future modifications to 'self' do not end up
  2506. # side-effecting 'obj' as well.
  2507. if (_mask is not nomask and obj.__array_interface__["data"][0]
  2508. != self.__array_interface__["data"][0]):
  2509. # We should make a copy. But we could get here via astype,
  2510. # in which case the mask might need a new dtype as well
  2511. # (e.g., changing to or from a structured dtype), and the
  2512. # order could have changed. So, change the mask type if
  2513. # needed and use astype instead of copy.
  2514. if self.dtype == obj.dtype:
  2515. _mask_dtype = _mask.dtype
  2516. else:
  2517. _mask_dtype = make_mask_descr(self.dtype)
  2518. if self.flags.c_contiguous:
  2519. order = "C"
  2520. elif self.flags.f_contiguous:
  2521. order = "F"
  2522. else:
  2523. order = "K"
  2524. _mask = _mask.astype(_mask_dtype, order)
  2525. else:
  2526. # Take a view so shape changes, etc., do not propagate back.
  2527. _mask = _mask.view()
  2528. else:
  2529. _mask = nomask
  2530. self._mask = _mask
  2531. # Finalize the mask
  2532. if self._mask is not nomask:
  2533. try:
  2534. self._mask.shape = self.shape
  2535. except ValueError:
  2536. self._mask = nomask
  2537. except (TypeError, AttributeError):
  2538. # When _mask.shape is not writable (because it's a void)
  2539. pass
  2540. # Finalize the fill_value
  2541. if self._fill_value is not None:
  2542. self._fill_value = _check_fill_value(self._fill_value, self.dtype)
  2543. elif self.dtype.names is not None:
  2544. # Finalize the default fill_value for structured arrays
  2545. self._fill_value = _check_fill_value(None, self.dtype)
  2546. def __array_wrap__(self, obj, context=None):
  2547. """
  2548. Special hook for ufuncs.
  2549. Wraps the numpy array and sets the mask according to context.
  2550. """
  2551. if obj is self: # for in-place operations
  2552. result = obj
  2553. else:
  2554. result = obj.view(type(self))
  2555. result._update_from(self)
  2556. if context is not None:
  2557. result._mask = result._mask.copy()
  2558. func, args, out_i = context
  2559. # args sometimes contains outputs (gh-10459), which we don't want
  2560. input_args = args[:func.nin]
  2561. m = reduce(mask_or, [getmaskarray(arg) for arg in input_args])
  2562. # Get the domain mask
  2563. domain = ufunc_domain.get(func, None)
  2564. if domain is not None:
  2565. # Take the domain, and make sure it's a ndarray
  2566. with np.errstate(divide='ignore', invalid='ignore'):
  2567. d = filled(domain(*input_args), True)
  2568. if d.any():
  2569. # Fill the result where the domain is wrong
  2570. try:
  2571. # Binary domain: take the last value
  2572. fill_value = ufunc_fills[func][-1]
  2573. except TypeError:
  2574. # Unary domain: just use this one
  2575. fill_value = ufunc_fills[func]
  2576. except KeyError:
  2577. # Domain not recognized, use fill_value instead
  2578. fill_value = self.fill_value
  2579. np.copyto(result, fill_value, where=d)
  2580. # Update the mask
  2581. if m is nomask:
  2582. m = d
  2583. else:
  2584. # Don't modify inplace, we risk back-propagation
  2585. m = (m | d)
  2586. # Make sure the mask has the proper size
  2587. if result is not self and result.shape == () and m:
  2588. return masked
  2589. else:
  2590. result._mask = m
  2591. result._sharedmask = False
  2592. return result
  2593. def view(self, dtype=None, type=None, fill_value=None):
  2594. """
  2595. Return a view of the MaskedArray data.
  2596. Parameters
  2597. ----------
  2598. dtype : data-type or ndarray sub-class, optional
  2599. Data-type descriptor of the returned view, e.g., float32 or int16.
  2600. The default, None, results in the view having the same data-type
  2601. as `a`. As with ``ndarray.view``, dtype can also be specified as
  2602. an ndarray sub-class, which then specifies the type of the
  2603. returned object (this is equivalent to setting the ``type``
  2604. parameter).
  2605. type : Python type, optional
  2606. Type of the returned view, either ndarray or a subclass. The
  2607. default None results in type preservation.
  2608. fill_value : scalar, optional
  2609. The value to use for invalid entries (None by default).
  2610. If None, then this argument is inferred from the passed `dtype`, or
  2611. in its absence the original array, as discussed in the notes below.
  2612. See Also
  2613. --------
  2614. numpy.ndarray.view : Equivalent method on ndarray object.
  2615. Notes
  2616. -----
  2617. ``a.view()`` is used two different ways:
  2618. ``a.view(some_dtype)`` or ``a.view(dtype=some_dtype)`` constructs a view
  2619. of the array's memory with a different data-type. This can cause a
  2620. reinterpretation of the bytes of memory.
  2621. ``a.view(ndarray_subclass)`` or ``a.view(type=ndarray_subclass)`` just
  2622. returns an instance of `ndarray_subclass` that looks at the same array
  2623. (same shape, dtype, etc.) This does not cause a reinterpretation of the
  2624. memory.
  2625. If `fill_value` is not specified, but `dtype` is specified (and is not
  2626. an ndarray sub-class), the `fill_value` of the MaskedArray will be
  2627. reset. If neither `fill_value` nor `dtype` are specified (or if
  2628. `dtype` is an ndarray sub-class), then the fill value is preserved.
  2629. Finally, if `fill_value` is specified, but `dtype` is not, the fill
  2630. value is set to the specified value.
  2631. For ``a.view(some_dtype)``, if ``some_dtype`` has a different number of
  2632. bytes per entry than the previous dtype (for example, converting a
  2633. regular array to a structured array), then the behavior of the view
  2634. cannot be predicted just from the superficial appearance of ``a`` (shown
  2635. by ``print(a)``). It also depends on exactly how ``a`` is stored in
  2636. memory. Therefore if ``a`` is C-ordered versus fortran-ordered, versus
  2637. defined as a slice or transpose, etc., the view may give different
  2638. results.
  2639. """
  2640. if dtype is None:
  2641. if type is None:
  2642. output = ndarray.view(self)
  2643. else:
  2644. output = ndarray.view(self, type)
  2645. elif type is None:
  2646. try:
  2647. if issubclass(dtype, ndarray):
  2648. output = ndarray.view(self, dtype)
  2649. dtype = None
  2650. else:
  2651. output = ndarray.view(self, dtype)
  2652. except TypeError:
  2653. output = ndarray.view(self, dtype)
  2654. else:
  2655. output = ndarray.view(self, dtype, type)
  2656. # also make the mask be a view (so attr changes to the view's
  2657. # mask do no affect original object's mask)
  2658. # (especially important to avoid affecting np.masked singleton)
  2659. if getmask(output) is not nomask:
  2660. output._mask = output._mask.view()
  2661. # Make sure to reset the _fill_value if needed
  2662. if getattr(output, '_fill_value', None) is not None:
  2663. if fill_value is None:
  2664. if dtype is None:
  2665. pass # leave _fill_value as is
  2666. else:
  2667. output._fill_value = None
  2668. else:
  2669. output.fill_value = fill_value
  2670. return output
  2671. def __getitem__(self, indx):
  2672. """
  2673. x.__getitem__(y) <==> x[y]
  2674. Return the item described by i, as a masked array.
  2675. """
  2676. # We could directly use ndarray.__getitem__ on self.
  2677. # But then we would have to modify __array_finalize__ to prevent the
  2678. # mask of being reshaped if it hasn't been set up properly yet
  2679. # So it's easier to stick to the current version
  2680. dout = self.data[indx]
  2681. _mask = self._mask
  2682. def _is_scalar(m):
  2683. return not isinstance(m, np.ndarray)
  2684. def _scalar_heuristic(arr, elem):
  2685. """
  2686. Return whether `elem` is a scalar result of indexing `arr`, or None
  2687. if undecidable without promoting nomask to a full mask
  2688. """
  2689. # obviously a scalar
  2690. if not isinstance(elem, np.ndarray):
  2691. return True
  2692. # object array scalar indexing can return anything
  2693. elif arr.dtype.type is np.object_:
  2694. if arr.dtype is not elem.dtype:
  2695. # elem is an array, but dtypes do not match, so must be
  2696. # an element
  2697. return True
  2698. # well-behaved subclass that only returns 0d arrays when
  2699. # expected - this is not a scalar
  2700. elif type(arr).__getitem__ == ndarray.__getitem__:
  2701. return False
  2702. return None
  2703. if _mask is not nomask:
  2704. # _mask cannot be a subclass, so it tells us whether we should
  2705. # expect a scalar. It also cannot be of dtype object.
  2706. mout = _mask[indx]
  2707. scalar_expected = _is_scalar(mout)
  2708. else:
  2709. # attempt to apply the heuristic to avoid constructing a full mask
  2710. mout = nomask
  2711. scalar_expected = _scalar_heuristic(self.data, dout)
  2712. if scalar_expected is None:
  2713. # heuristics have failed
  2714. # construct a full array, so we can be certain. This is costly.
  2715. # we could also fall back on ndarray.__getitem__(self.data, indx)
  2716. scalar_expected = _is_scalar(getmaskarray(self)[indx])
  2717. # Did we extract a single item?
  2718. if scalar_expected:
  2719. # A record
  2720. if isinstance(dout, np.void):
  2721. # We should always re-cast to mvoid, otherwise users can
  2722. # change masks on rows that already have masked values, but not
  2723. # on rows that have no masked values, which is inconsistent.
  2724. return mvoid(dout, mask=mout, hardmask=self._hardmask)
  2725. # special case introduced in gh-5962
  2726. elif (self.dtype.type is np.object_ and
  2727. isinstance(dout, np.ndarray) and
  2728. dout is not masked):
  2729. # If masked, turn into a MaskedArray, with everything masked.
  2730. if mout:
  2731. return MaskedArray(dout, mask=True)
  2732. else:
  2733. return dout
  2734. # Just a scalar
  2735. else:
  2736. if mout:
  2737. return masked
  2738. else:
  2739. return dout
  2740. else:
  2741. # Force dout to MA
  2742. dout = dout.view(type(self))
  2743. # Inherit attributes from self
  2744. dout._update_from(self)
  2745. # Check the fill_value
  2746. if is_string_or_list_of_strings(indx):
  2747. if self._fill_value is not None:
  2748. dout._fill_value = self._fill_value[indx]
  2749. # Something like gh-15895 has happened if this check fails.
  2750. # _fill_value should always be an ndarray.
  2751. if not isinstance(dout._fill_value, np.ndarray):
  2752. raise RuntimeError('Internal NumPy error.')
  2753. # If we're indexing a multidimensional field in a
  2754. # structured array (such as dtype("(2,)i2,(2,)i1")),
  2755. # dimensionality goes up (M[field].ndim == M.ndim +
  2756. # M.dtype[field].ndim). That's fine for
  2757. # M[field] but problematic for M[field].fill_value
  2758. # which should have shape () to avoid breaking several
  2759. # methods. There is no great way out, so set to
  2760. # first element. See issue #6723.
  2761. if dout._fill_value.ndim > 0:
  2762. if not (dout._fill_value ==
  2763. dout._fill_value.flat[0]).all():
  2764. warnings.warn(
  2765. "Upon accessing multidimensional field "
  2766. f"{indx!s}, need to keep dimensionality "
  2767. "of fill_value at 0. Discarding "
  2768. "heterogeneous fill_value and setting "
  2769. f"all to {dout._fill_value[0]!s}.",
  2770. stacklevel=2)
  2771. # Need to use `.flat[0:1].squeeze(...)` instead of just
  2772. # `.flat[0]` to ensure the result is a 0d array and not
  2773. # a scalar.
  2774. dout._fill_value = dout._fill_value.flat[0:1].squeeze(axis=0)
  2775. dout._isfield = True
  2776. # Update the mask if needed
  2777. if mout is not nomask:
  2778. # set shape to match that of data; this is needed for matrices
  2779. dout._mask = reshape(mout, dout.shape)
  2780. dout._sharedmask = True
  2781. # Note: Don't try to check for m.any(), that'll take too long
  2782. return dout
  2783. # setitem may put NaNs into integer arrays or occasionally overflow a
  2784. # float. But this may happen in masked values, so avoid otherwise
  2785. # correct warnings (as is typical also in masked calculations).
  2786. @np.errstate(over='ignore', invalid='ignore')
  2787. def __setitem__(self, indx, value):
  2788. """
  2789. x.__setitem__(i, y) <==> x[i]=y
  2790. Set item described by index. If value is masked, masks those
  2791. locations.
  2792. """
  2793. if self is masked:
  2794. raise MaskError('Cannot alter the masked element.')
  2795. _data = self._data
  2796. _mask = self._mask
  2797. if isinstance(indx, str):
  2798. _data[indx] = value
  2799. if _mask is nomask:
  2800. self._mask = _mask = make_mask_none(self.shape, self.dtype)
  2801. _mask[indx] = getmask(value)
  2802. return
  2803. _dtype = _data.dtype
  2804. if value is masked:
  2805. # The mask wasn't set: create a full version.
  2806. if _mask is nomask:
  2807. _mask = self._mask = make_mask_none(self.shape, _dtype)
  2808. # Now, set the mask to its value.
  2809. if _dtype.names is not None:
  2810. _mask[indx] = tuple([True] * len(_dtype.names))
  2811. else:
  2812. _mask[indx] = True
  2813. return
  2814. # Get the _data part of the new value
  2815. dval = getattr(value, '_data', value)
  2816. # Get the _mask part of the new value
  2817. mval = getmask(value)
  2818. if _dtype.names is not None and mval is nomask:
  2819. mval = tuple([False] * len(_dtype.names))
  2820. if _mask is nomask:
  2821. # Set the data, then the mask
  2822. _data[indx] = dval
  2823. if mval is not nomask:
  2824. _mask = self._mask = make_mask_none(self.shape, _dtype)
  2825. _mask[indx] = mval
  2826. elif not self._hardmask:
  2827. # Set the data, then the mask
  2828. if (isinstance(indx, masked_array) and
  2829. not isinstance(value, masked_array)):
  2830. _data[indx.data] = dval
  2831. else:
  2832. _data[indx] = dval
  2833. _mask[indx] = mval
  2834. elif hasattr(indx, 'dtype') and (indx.dtype == MaskType):
  2835. indx = indx * umath.logical_not(_mask)
  2836. _data[indx] = dval
  2837. else:
  2838. if _dtype.names is not None:
  2839. err_msg = "Flexible 'hard' masks are not yet supported."
  2840. raise NotImplementedError(err_msg)
  2841. mindx = mask_or(_mask[indx], mval, copy=True)
  2842. dindx = self._data[indx]
  2843. if dindx.size > 1:
  2844. np.copyto(dindx, dval, where=~mindx)
  2845. elif mindx is nomask:
  2846. dindx = dval
  2847. _data[indx] = dindx
  2848. _mask[indx] = mindx
  2849. return
  2850. # Define so that we can overwrite the setter.
  2851. @property
  2852. def dtype(self):
  2853. return super().dtype
  2854. @dtype.setter
  2855. def dtype(self, dtype):
  2856. super(MaskedArray, type(self)).dtype.__set__(self, dtype)
  2857. if self._mask is not nomask:
  2858. self._mask = self._mask.view(make_mask_descr(dtype), ndarray)
  2859. # Try to reset the shape of the mask (if we don't have a void).
  2860. # This raises a ValueError if the dtype change won't work.
  2861. try:
  2862. self._mask.shape = self.shape
  2863. except (AttributeError, TypeError):
  2864. pass
  2865. @property
  2866. def shape(self):
  2867. return super().shape
  2868. @shape.setter
  2869. def shape(self, shape):
  2870. super(MaskedArray, type(self)).shape.__set__(self, shape)
  2871. # Cannot use self._mask, since it may not (yet) exist when a
  2872. # masked matrix sets the shape.
  2873. if getmask(self) is not nomask:
  2874. self._mask.shape = self.shape
  2875. def __setmask__(self, mask, copy=False):
  2876. """
  2877. Set the mask.
  2878. """
  2879. idtype = self.dtype
  2880. current_mask = self._mask
  2881. if mask is masked:
  2882. mask = True
  2883. if current_mask is nomask:
  2884. # Make sure the mask is set
  2885. # Just don't do anything if there's nothing to do.
  2886. if mask is nomask:
  2887. return
  2888. current_mask = self._mask = make_mask_none(self.shape, idtype)
  2889. if idtype.names is None:
  2890. # No named fields.
  2891. # Hardmask: don't unmask the data
  2892. if self._hardmask:
  2893. current_mask |= mask
  2894. # Softmask: set everything to False
  2895. # If it's obviously a compatible scalar, use a quick update
  2896. # method.
  2897. elif isinstance(mask, (int, float, np.bool_, np.number)):
  2898. current_mask[...] = mask
  2899. # Otherwise fall back to the slower, general purpose way.
  2900. else:
  2901. current_mask.flat = mask
  2902. else:
  2903. # Named fields w/
  2904. mdtype = current_mask.dtype
  2905. mask = np.array(mask, copy=False)
  2906. # Mask is a singleton
  2907. if not mask.ndim:
  2908. # It's a boolean : make a record
  2909. if mask.dtype.kind == 'b':
  2910. mask = np.array(tuple([mask.item()] * len(mdtype)),
  2911. dtype=mdtype)
  2912. # It's a record: make sure the dtype is correct
  2913. else:
  2914. mask = mask.astype(mdtype)
  2915. # Mask is a sequence
  2916. else:
  2917. # Make sure the new mask is a ndarray with the proper dtype
  2918. try:
  2919. mask = np.array(mask, copy=copy, dtype=mdtype)
  2920. # Or assume it's a sequence of bool/int
  2921. except TypeError:
  2922. mask = np.array([tuple([m] * len(mdtype)) for m in mask],
  2923. dtype=mdtype)
  2924. # Hardmask: don't unmask the data
  2925. if self._hardmask:
  2926. for n in idtype.names:
  2927. current_mask[n] |= mask[n]
  2928. # Softmask: set everything to False
  2929. # If it's obviously a compatible scalar, use a quick update
  2930. # method.
  2931. elif isinstance(mask, (int, float, np.bool_, np.number)):
  2932. current_mask[...] = mask
  2933. # Otherwise fall back to the slower, general purpose way.
  2934. else:
  2935. current_mask.flat = mask
  2936. # Reshape if needed
  2937. if current_mask.shape:
  2938. current_mask.shape = self.shape
  2939. return
  2940. _set_mask = __setmask__
  2941. @property
  2942. def mask(self):
  2943. """ Current mask. """
  2944. # We could try to force a reshape, but that wouldn't work in some
  2945. # cases.
  2946. # Return a view so that the dtype and shape cannot be changed in place
  2947. # This still preserves nomask by identity
  2948. return self._mask.view()
  2949. @mask.setter
  2950. def mask(self, value):
  2951. self.__setmask__(value)
  2952. @property
  2953. def recordmask(self):
  2954. """
  2955. Get or set the mask of the array if it has no named fields. For
  2956. structured arrays, returns a ndarray of booleans where entries are
  2957. ``True`` if **all** the fields are masked, ``False`` otherwise:
  2958. >>> x = np.ma.array([(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)],
  2959. ... mask=[(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)],
  2960. ... dtype=[('a', int), ('b', int)])
  2961. >>> x.recordmask
  2962. array([False, False, True, False, False])
  2963. """
  2964. _mask = self._mask.view(ndarray)
  2965. if _mask.dtype.names is None:
  2966. return _mask
  2967. return np.all(flatten_structured_array(_mask), axis=-1)
  2968. @recordmask.setter
  2969. def recordmask(self, mask):
  2970. raise NotImplementedError("Coming soon: setting the mask per records!")
  2971. def harden_mask(self):
  2972. """
  2973. Force the mask to hard, preventing unmasking by assignment.
  2974. Whether the mask of a masked array is hard or soft is determined by
  2975. its `~ma.MaskedArray.hardmask` property. `harden_mask` sets
  2976. `~ma.MaskedArray.hardmask` to ``True`` (and returns the modified
  2977. self).
  2978. See Also
  2979. --------
  2980. ma.MaskedArray.hardmask
  2981. ma.MaskedArray.soften_mask
  2982. """
  2983. self._hardmask = True
  2984. return self
  2985. def soften_mask(self):
  2986. """
  2987. Force the mask to soft (default), allowing unmasking by assignment.
  2988. Whether the mask of a masked array is hard or soft is determined by
  2989. its `~ma.MaskedArray.hardmask` property. `soften_mask` sets
  2990. `~ma.MaskedArray.hardmask` to ``False`` (and returns the modified
  2991. self).
  2992. See Also
  2993. --------
  2994. ma.MaskedArray.hardmask
  2995. ma.MaskedArray.harden_mask
  2996. """
  2997. self._hardmask = False
  2998. return self
  2999. @property
  3000. def hardmask(self):
  3001. """
  3002. Specifies whether values can be unmasked through assignments.
  3003. By default, assigning definite values to masked array entries will
  3004. unmask them. When `hardmask` is ``True``, the mask will not change
  3005. through assignments.
  3006. See Also
  3007. --------
  3008. ma.MaskedArray.harden_mask
  3009. ma.MaskedArray.soften_mask
  3010. Examples
  3011. --------
  3012. >>> x = np.arange(10)
  3013. >>> m = np.ma.masked_array(x, x>5)
  3014. >>> assert not m.hardmask
  3015. Since `m` has a soft mask, assigning an element value unmasks that
  3016. element:
  3017. >>> m[8] = 42
  3018. >>> m
  3019. masked_array(data=[0, 1, 2, 3, 4, 5, --, --, 42, --],
  3020. mask=[False, False, False, False, False, False,
  3021. True, True, False, True],
  3022. fill_value=999999)
  3023. After hardening, the mask is not affected by assignments:
  3024. >>> hardened = np.ma.harden_mask(m)
  3025. >>> assert m.hardmask and hardened is m
  3026. >>> m[:] = 23
  3027. >>> m
  3028. masked_array(data=[23, 23, 23, 23, 23, 23, --, --, 23, --],
  3029. mask=[False, False, False, False, False, False,
  3030. True, True, False, True],
  3031. fill_value=999999)
  3032. """
  3033. return self._hardmask
  3034. def unshare_mask(self):
  3035. """
  3036. Copy the mask and set the `sharedmask` flag to ``False``.
  3037. Whether the mask is shared between masked arrays can be seen from
  3038. the `sharedmask` property. `unshare_mask` ensures the mask is not
  3039. shared. A copy of the mask is only made if it was shared.
  3040. See Also
  3041. --------
  3042. sharedmask
  3043. """
  3044. if self._sharedmask:
  3045. self._mask = self._mask.copy()
  3046. self._sharedmask = False
  3047. return self
  3048. @property
  3049. def sharedmask(self):
  3050. """ Share status of the mask (read-only). """
  3051. return self._sharedmask
  3052. def shrink_mask(self):
  3053. """
  3054. Reduce a mask to nomask when possible.
  3055. Parameters
  3056. ----------
  3057. None
  3058. Returns
  3059. -------
  3060. None
  3061. Examples
  3062. --------
  3063. >>> x = np.ma.array([[1,2 ], [3, 4]], mask=[0]*4)
  3064. >>> x.mask
  3065. array([[False, False],
  3066. [False, False]])
  3067. >>> x.shrink_mask()
  3068. masked_array(
  3069. data=[[1, 2],
  3070. [3, 4]],
  3071. mask=False,
  3072. fill_value=999999)
  3073. >>> x.mask
  3074. False
  3075. """
  3076. self._mask = _shrink_mask(self._mask)
  3077. return self
  3078. @property
  3079. def baseclass(self):
  3080. """ Class of the underlying data (read-only). """
  3081. return self._baseclass
  3082. def _get_data(self):
  3083. """
  3084. Returns the underlying data, as a view of the masked array.
  3085. If the underlying data is a subclass of :class:`numpy.ndarray`, it is
  3086. returned as such.
  3087. >>> x = np.ma.array(np.matrix([[1, 2], [3, 4]]), mask=[[0, 1], [1, 0]])
  3088. >>> x.data
  3089. matrix([[1, 2],
  3090. [3, 4]])
  3091. The type of the data can be accessed through the :attr:`baseclass`
  3092. attribute.
  3093. """
  3094. return ndarray.view(self, self._baseclass)
  3095. _data = property(fget=_get_data)
  3096. data = property(fget=_get_data)
  3097. @property
  3098. def flat(self):
  3099. """ Return a flat iterator, or set a flattened version of self to value. """
  3100. return MaskedIterator(self)
  3101. @flat.setter
  3102. def flat(self, value):
  3103. y = self.ravel()
  3104. y[:] = value
  3105. @property
  3106. def fill_value(self):
  3107. """
  3108. The filling value of the masked array is a scalar. When setting, None
  3109. will set to a default based on the data type.
  3110. Examples
  3111. --------
  3112. >>> for dt in [np.int32, np.int64, np.float64, np.complex128]:
  3113. ... np.ma.array([0, 1], dtype=dt).get_fill_value()
  3114. ...
  3115. 999999
  3116. 999999
  3117. 1e+20
  3118. (1e+20+0j)
  3119. >>> x = np.ma.array([0, 1.], fill_value=-np.inf)
  3120. >>> x.fill_value
  3121. -inf
  3122. >>> x.fill_value = np.pi
  3123. >>> x.fill_value
  3124. 3.1415926535897931 # may vary
  3125. Reset to default:
  3126. >>> x.fill_value = None
  3127. >>> x.fill_value
  3128. 1e+20
  3129. """
  3130. if self._fill_value is None:
  3131. self._fill_value = _check_fill_value(None, self.dtype)
  3132. # Temporary workaround to account for the fact that str and bytes
  3133. # scalars cannot be indexed with (), whereas all other numpy
  3134. # scalars can. See issues #7259 and #7267.
  3135. # The if-block can be removed after #7267 has been fixed.
  3136. if isinstance(self._fill_value, ndarray):
  3137. return self._fill_value[()]
  3138. return self._fill_value
  3139. @fill_value.setter
  3140. def fill_value(self, value=None):
  3141. target = _check_fill_value(value, self.dtype)
  3142. if not target.ndim == 0:
  3143. # 2019-11-12, 1.18.0
  3144. warnings.warn(
  3145. "Non-scalar arrays for the fill value are deprecated. Use "
  3146. "arrays with scalar values instead. The filled function "
  3147. "still supports any array as `fill_value`.",
  3148. DeprecationWarning, stacklevel=2)
  3149. _fill_value = self._fill_value
  3150. if _fill_value is None:
  3151. # Create the attribute if it was undefined
  3152. self._fill_value = target
  3153. else:
  3154. # Don't overwrite the attribute, just fill it (for propagation)
  3155. _fill_value[()] = target
  3156. # kept for compatibility
  3157. get_fill_value = fill_value.fget
  3158. set_fill_value = fill_value.fset
  3159. def filled(self, fill_value=None):
  3160. """
  3161. Return a copy of self, with masked values filled with a given value.
  3162. **However**, if there are no masked values to fill, self will be
  3163. returned instead as an ndarray.
  3164. Parameters
  3165. ----------
  3166. fill_value : array_like, optional
  3167. The value to use for invalid entries. Can be scalar or non-scalar.
  3168. If non-scalar, the resulting ndarray must be broadcastable over
  3169. input array. Default is None, in which case, the `fill_value`
  3170. attribute of the array is used instead.
  3171. Returns
  3172. -------
  3173. filled_array : ndarray
  3174. A copy of ``self`` with invalid entries replaced by *fill_value*
  3175. (be it the function argument or the attribute of ``self``), or
  3176. ``self`` itself as an ndarray if there are no invalid entries to
  3177. be replaced.
  3178. Notes
  3179. -----
  3180. The result is **not** a MaskedArray!
  3181. Examples
  3182. --------
  3183. >>> x = np.ma.array([1,2,3,4,5], mask=[0,0,1,0,1], fill_value=-999)
  3184. >>> x.filled()
  3185. array([ 1, 2, -999, 4, -999])
  3186. >>> x.filled(fill_value=1000)
  3187. array([ 1, 2, 1000, 4, 1000])
  3188. >>> type(x.filled())
  3189. <class 'numpy.ndarray'>
  3190. Subclassing is preserved. This means that if, e.g., the data part of
  3191. the masked array is a recarray, `filled` returns a recarray:
  3192. >>> x = np.array([(-1, 2), (-3, 4)], dtype='i8,i8').view(np.recarray)
  3193. >>> m = np.ma.array(x, mask=[(True, False), (False, True)])
  3194. >>> m.filled()
  3195. rec.array([(999999, 2), ( -3, 999999)],
  3196. dtype=[('f0', '<i8'), ('f1', '<i8')])
  3197. """
  3198. m = self._mask
  3199. if m is nomask:
  3200. return self._data
  3201. if fill_value is None:
  3202. fill_value = self.fill_value
  3203. else:
  3204. fill_value = _check_fill_value(fill_value, self.dtype)
  3205. if self is masked_singleton:
  3206. return np.asanyarray(fill_value)
  3207. if m.dtype.names is not None:
  3208. result = self._data.copy('K')
  3209. _recursive_filled(result, self._mask, fill_value)
  3210. elif not m.any():
  3211. return self._data
  3212. else:
  3213. result = self._data.copy('K')
  3214. try:
  3215. np.copyto(result, fill_value, where=m)
  3216. except (TypeError, AttributeError):
  3217. fill_value = narray(fill_value, dtype=object)
  3218. d = result.astype(object)
  3219. result = np.choose(m, (d, fill_value))
  3220. except IndexError:
  3221. # ok, if scalar
  3222. if self._data.shape:
  3223. raise
  3224. elif m:
  3225. result = np.array(fill_value, dtype=self.dtype)
  3226. else:
  3227. result = self._data
  3228. return result
  3229. def compressed(self):
  3230. """
  3231. Return all the non-masked data as a 1-D array.
  3232. Returns
  3233. -------
  3234. data : ndarray
  3235. A new `ndarray` holding the non-masked data is returned.
  3236. Notes
  3237. -----
  3238. The result is **not** a MaskedArray!
  3239. Examples
  3240. --------
  3241. >>> x = np.ma.array(np.arange(5), mask=[0]*2 + [1]*3)
  3242. >>> x.compressed()
  3243. array([0, 1])
  3244. >>> type(x.compressed())
  3245. <class 'numpy.ndarray'>
  3246. """
  3247. data = ndarray.ravel(self._data)
  3248. if self._mask is not nomask:
  3249. data = data.compress(np.logical_not(ndarray.ravel(self._mask)))
  3250. return data
  3251. def compress(self, condition, axis=None, out=None):
  3252. """
  3253. Return `a` where condition is ``True``.
  3254. If condition is a `~ma.MaskedArray`, missing values are considered
  3255. as ``False``.
  3256. Parameters
  3257. ----------
  3258. condition : var
  3259. Boolean 1-d array selecting which entries to return. If len(condition)
  3260. is less than the size of a along the axis, then output is truncated
  3261. to length of condition array.
  3262. axis : {None, int}, optional
  3263. Axis along which the operation must be performed.
  3264. out : {None, ndarray}, optional
  3265. Alternative output array in which to place the result. It must have
  3266. the same shape as the expected output but the type will be cast if
  3267. necessary.
  3268. Returns
  3269. -------
  3270. result : MaskedArray
  3271. A :class:`~ma.MaskedArray` object.
  3272. Notes
  3273. -----
  3274. Please note the difference with :meth:`compressed` !
  3275. The output of :meth:`compress` has a mask, the output of
  3276. :meth:`compressed` does not.
  3277. Examples
  3278. --------
  3279. >>> x = np.ma.array([[1,2,3],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4)
  3280. >>> x
  3281. masked_array(
  3282. data=[[1, --, 3],
  3283. [--, 5, --],
  3284. [7, --, 9]],
  3285. mask=[[False, True, False],
  3286. [ True, False, True],
  3287. [False, True, False]],
  3288. fill_value=999999)
  3289. >>> x.compress([1, 0, 1])
  3290. masked_array(data=[1, 3],
  3291. mask=[False, False],
  3292. fill_value=999999)
  3293. >>> x.compress([1, 0, 1], axis=1)
  3294. masked_array(
  3295. data=[[1, 3],
  3296. [--, --],
  3297. [7, 9]],
  3298. mask=[[False, False],
  3299. [ True, True],
  3300. [False, False]],
  3301. fill_value=999999)
  3302. """
  3303. # Get the basic components
  3304. (_data, _mask) = (self._data, self._mask)
  3305. # Force the condition to a regular ndarray and forget the missing
  3306. # values.
  3307. condition = np.asarray(condition)
  3308. _new = _data.compress(condition, axis=axis, out=out).view(type(self))
  3309. _new._update_from(self)
  3310. if _mask is not nomask:
  3311. _new._mask = _mask.compress(condition, axis=axis)
  3312. return _new
  3313. def _insert_masked_print(self):
  3314. """
  3315. Replace masked values with masked_print_option, casting all innermost
  3316. dtypes to object.
  3317. """
  3318. if masked_print_option.enabled():
  3319. mask = self._mask
  3320. if mask is nomask:
  3321. res = self._data
  3322. else:
  3323. # convert to object array to make filled work
  3324. data = self._data
  3325. # For big arrays, to avoid a costly conversion to the
  3326. # object dtype, extract the corners before the conversion.
  3327. print_width = (self._print_width if self.ndim > 1
  3328. else self._print_width_1d)
  3329. for axis in range(self.ndim):
  3330. if data.shape[axis] > print_width:
  3331. ind = print_width // 2
  3332. arr = np.split(data, (ind, -ind), axis=axis)
  3333. data = np.concatenate((arr[0], arr[2]), axis=axis)
  3334. arr = np.split(mask, (ind, -ind), axis=axis)
  3335. mask = np.concatenate((arr[0], arr[2]), axis=axis)
  3336. rdtype = _replace_dtype_fields(self.dtype, "O")
  3337. res = data.astype(rdtype)
  3338. _recursive_printoption(res, mask, masked_print_option)
  3339. else:
  3340. res = self.filled(self.fill_value)
  3341. return res
  3342. def __str__(self):
  3343. return str(self._insert_masked_print())
  3344. def __repr__(self):
  3345. """
  3346. Literal string representation.
  3347. """
  3348. if self._baseclass is np.ndarray:
  3349. name = 'array'
  3350. else:
  3351. name = self._baseclass.__name__
  3352. # 2016-11-19: Demoted to legacy format
  3353. if np.core.arrayprint._get_legacy_print_mode() <= 113:
  3354. is_long = self.ndim > 1
  3355. parameters = dict(
  3356. name=name,
  3357. nlen=" " * len(name),
  3358. data=str(self),
  3359. mask=str(self._mask),
  3360. fill=str(self.fill_value),
  3361. dtype=str(self.dtype)
  3362. )
  3363. is_structured = bool(self.dtype.names)
  3364. key = '{}_{}'.format(
  3365. 'long' if is_long else 'short',
  3366. 'flx' if is_structured else 'std'
  3367. )
  3368. return _legacy_print_templates[key] % parameters
  3369. prefix = f"masked_{name}("
  3370. dtype_needed = (
  3371. not np.core.arrayprint.dtype_is_implied(self.dtype) or
  3372. np.all(self.mask) or
  3373. self.size == 0
  3374. )
  3375. # determine which keyword args need to be shown
  3376. keys = ['data', 'mask', 'fill_value']
  3377. if dtype_needed:
  3378. keys.append('dtype')
  3379. # array has only one row (non-column)
  3380. is_one_row = builtins.all(dim == 1 for dim in self.shape[:-1])
  3381. # choose what to indent each keyword with
  3382. min_indent = 2
  3383. if is_one_row:
  3384. # first key on the same line as the type, remaining keys
  3385. # aligned by equals
  3386. indents = {}
  3387. indents[keys[0]] = prefix
  3388. for k in keys[1:]:
  3389. n = builtins.max(min_indent, len(prefix + keys[0]) - len(k))
  3390. indents[k] = ' ' * n
  3391. prefix = '' # absorbed into the first indent
  3392. else:
  3393. # each key on its own line, indented by two spaces
  3394. indents = {k: ' ' * min_indent for k in keys}
  3395. prefix = prefix + '\n' # first key on the next line
  3396. # format the field values
  3397. reprs = {}
  3398. reprs['data'] = np.array2string(
  3399. self._insert_masked_print(),
  3400. separator=", ",
  3401. prefix=indents['data'] + 'data=',
  3402. suffix=',')
  3403. reprs['mask'] = np.array2string(
  3404. self._mask,
  3405. separator=", ",
  3406. prefix=indents['mask'] + 'mask=',
  3407. suffix=',')
  3408. reprs['fill_value'] = repr(self.fill_value)
  3409. if dtype_needed:
  3410. reprs['dtype'] = np.core.arrayprint.dtype_short_repr(self.dtype)
  3411. # join keys with values and indentations
  3412. result = ',\n'.join(
  3413. '{}{}={}'.format(indents[k], k, reprs[k])
  3414. for k in keys
  3415. )
  3416. return prefix + result + ')'
  3417. def _delegate_binop(self, other):
  3418. # This emulates the logic in
  3419. # private/binop_override.h:forward_binop_should_defer
  3420. if isinstance(other, type(self)):
  3421. return False
  3422. array_ufunc = getattr(other, "__array_ufunc__", False)
  3423. if array_ufunc is False:
  3424. other_priority = getattr(other, "__array_priority__", -1000000)
  3425. return self.__array_priority__ < other_priority
  3426. else:
  3427. # If array_ufunc is not None, it will be called inside the ufunc;
  3428. # None explicitly tells us to not call the ufunc, i.e., defer.
  3429. return array_ufunc is None
  3430. def _comparison(self, other, compare):
  3431. """Compare self with other using operator.eq or operator.ne.
  3432. When either of the elements is masked, the result is masked as well,
  3433. but the underlying boolean data are still set, with self and other
  3434. considered equal if both are masked, and unequal otherwise.
  3435. For structured arrays, all fields are combined, with masked values
  3436. ignored. The result is masked if all fields were masked, with self
  3437. and other considered equal only if both were fully masked.
  3438. """
  3439. omask = getmask(other)
  3440. smask = self.mask
  3441. mask = mask_or(smask, omask, copy=True)
  3442. odata = getdata(other)
  3443. if mask.dtype.names is not None:
  3444. # only == and != are reasonably defined for structured dtypes,
  3445. # so give up early for all other comparisons:
  3446. if compare not in (operator.eq, operator.ne):
  3447. return NotImplemented
  3448. # For possibly masked structured arrays we need to be careful,
  3449. # since the standard structured array comparison will use all
  3450. # fields, masked or not. To avoid masked fields influencing the
  3451. # outcome, we set all masked fields in self to other, so they'll
  3452. # count as equal. To prepare, we ensure we have the right shape.
  3453. broadcast_shape = np.broadcast(self, odata).shape
  3454. sbroadcast = np.broadcast_to(self, broadcast_shape, subok=True)
  3455. sbroadcast._mask = mask
  3456. sdata = sbroadcast.filled(odata)
  3457. # Now take care of the mask; the merged mask should have an item
  3458. # masked if all fields were masked (in one and/or other).
  3459. mask = (mask == np.ones((), mask.dtype))
  3460. # Ensure we can compare masks below if other was not masked.
  3461. if omask is np.False_:
  3462. omask = np.zeros((), smask.dtype)
  3463. else:
  3464. # For regular arrays, just use the data as they come.
  3465. sdata = self.data
  3466. check = compare(sdata, odata)
  3467. if isinstance(check, (np.bool_, bool)):
  3468. return masked if mask else check
  3469. if mask is not nomask:
  3470. if compare in (operator.eq, operator.ne):
  3471. # Adjust elements that were masked, which should be treated
  3472. # as equal if masked in both, unequal if masked in one.
  3473. # Note that this works automatically for structured arrays too.
  3474. # Ignore this for operations other than `==` and `!=`
  3475. check = np.where(mask, compare(smask, omask), check)
  3476. if mask.shape != check.shape:
  3477. # Guarantee consistency of the shape, making a copy since the
  3478. # the mask may need to get written to later.
  3479. mask = np.broadcast_to(mask, check.shape).copy()
  3480. check = check.view(type(self))
  3481. check._update_from(self)
  3482. check._mask = mask
  3483. # Cast fill value to bool_ if needed. If it cannot be cast, the
  3484. # default boolean fill value is used.
  3485. if check._fill_value is not None:
  3486. try:
  3487. fill = _check_fill_value(check._fill_value, np.bool_)
  3488. except (TypeError, ValueError):
  3489. fill = _check_fill_value(None, np.bool_)
  3490. check._fill_value = fill
  3491. return check
  3492. def __eq__(self, other):
  3493. """Check whether other equals self elementwise.
  3494. When either of the elements is masked, the result is masked as well,
  3495. but the underlying boolean data are still set, with self and other
  3496. considered equal if both are masked, and unequal otherwise.
  3497. For structured arrays, all fields are combined, with masked values
  3498. ignored. The result is masked if all fields were masked, with self
  3499. and other considered equal only if both were fully masked.
  3500. """
  3501. return self._comparison(other, operator.eq)
  3502. def __ne__(self, other):
  3503. """Check whether other does not equal self elementwise.
  3504. When either of the elements is masked, the result is masked as well,
  3505. but the underlying boolean data are still set, with self and other
  3506. considered equal if both are masked, and unequal otherwise.
  3507. For structured arrays, all fields are combined, with masked values
  3508. ignored. The result is masked if all fields were masked, with self
  3509. and other considered equal only if both were fully masked.
  3510. """
  3511. return self._comparison(other, operator.ne)
  3512. # All other comparisons:
  3513. def __le__(self, other):
  3514. return self._comparison(other, operator.le)
  3515. def __lt__(self, other):
  3516. return self._comparison(other, operator.lt)
  3517. def __ge__(self, other):
  3518. return self._comparison(other, operator.ge)
  3519. def __gt__(self, other):
  3520. return self._comparison(other, operator.gt)
  3521. def __add__(self, other):
  3522. """
  3523. Add self to other, and return a new masked array.
  3524. """
  3525. if self._delegate_binop(other):
  3526. return NotImplemented
  3527. return add(self, other)
  3528. def __radd__(self, other):
  3529. """
  3530. Add other to self, and return a new masked array.
  3531. """
  3532. # In analogy with __rsub__ and __rdiv__, use original order:
  3533. # we get here from `other + self`.
  3534. return add(other, self)
  3535. def __sub__(self, other):
  3536. """
  3537. Subtract other from self, and return a new masked array.
  3538. """
  3539. if self._delegate_binop(other):
  3540. return NotImplemented
  3541. return subtract(self, other)
  3542. def __rsub__(self, other):
  3543. """
  3544. Subtract self from other, and return a new masked array.
  3545. """
  3546. return subtract(other, self)
  3547. def __mul__(self, other):
  3548. "Multiply self by other, and return a new masked array."
  3549. if self._delegate_binop(other):
  3550. return NotImplemented
  3551. return multiply(self, other)
  3552. def __rmul__(self, other):
  3553. """
  3554. Multiply other by self, and return a new masked array.
  3555. """
  3556. # In analogy with __rsub__ and __rdiv__, use original order:
  3557. # we get here from `other * self`.
  3558. return multiply(other, self)
  3559. def __div__(self, other):
  3560. """
  3561. Divide other into self, and return a new masked array.
  3562. """
  3563. if self._delegate_binop(other):
  3564. return NotImplemented
  3565. return divide(self, other)
  3566. def __truediv__(self, other):
  3567. """
  3568. Divide other into self, and return a new masked array.
  3569. """
  3570. if self._delegate_binop(other):
  3571. return NotImplemented
  3572. return true_divide(self, other)
  3573. def __rtruediv__(self, other):
  3574. """
  3575. Divide self into other, and return a new masked array.
  3576. """
  3577. return true_divide(other, self)
  3578. def __floordiv__(self, other):
  3579. """
  3580. Divide other into self, and return a new masked array.
  3581. """
  3582. if self._delegate_binop(other):
  3583. return NotImplemented
  3584. return floor_divide(self, other)
  3585. def __rfloordiv__(self, other):
  3586. """
  3587. Divide self into other, and return a new masked array.
  3588. """
  3589. return floor_divide(other, self)
  3590. def __pow__(self, other):
  3591. """
  3592. Raise self to the power other, masking the potential NaNs/Infs
  3593. """
  3594. if self._delegate_binop(other):
  3595. return NotImplemented
  3596. return power(self, other)
  3597. def __rpow__(self, other):
  3598. """
  3599. Raise other to the power self, masking the potential NaNs/Infs
  3600. """
  3601. return power(other, self)
  3602. def __iadd__(self, other):
  3603. """
  3604. Add other to self in-place.
  3605. """
  3606. m = getmask(other)
  3607. if self._mask is nomask:
  3608. if m is not nomask and m.any():
  3609. self._mask = make_mask_none(self.shape, self.dtype)
  3610. self._mask += m
  3611. else:
  3612. if m is not nomask:
  3613. self._mask += m
  3614. other_data = getdata(other)
  3615. other_data = np.where(self._mask, other_data.dtype.type(0), other_data)
  3616. self._data.__iadd__(other_data)
  3617. return self
  3618. def __isub__(self, other):
  3619. """
  3620. Subtract other from self in-place.
  3621. """
  3622. m = getmask(other)
  3623. if self._mask is nomask:
  3624. if m is not nomask and m.any():
  3625. self._mask = make_mask_none(self.shape, self.dtype)
  3626. self._mask += m
  3627. elif m is not nomask:
  3628. self._mask += m
  3629. other_data = getdata(other)
  3630. other_data = np.where(self._mask, other_data.dtype.type(0), other_data)
  3631. self._data.__isub__(other_data)
  3632. return self
  3633. def __imul__(self, other):
  3634. """
  3635. Multiply self by other in-place.
  3636. """
  3637. m = getmask(other)
  3638. if self._mask is nomask:
  3639. if m is not nomask and m.any():
  3640. self._mask = make_mask_none(self.shape, self.dtype)
  3641. self._mask += m
  3642. elif m is not nomask:
  3643. self._mask += m
  3644. other_data = getdata(other)
  3645. other_data = np.where(self._mask, other_data.dtype.type(1), other_data)
  3646. self._data.__imul__(other_data)
  3647. return self
  3648. def __idiv__(self, other):
  3649. """
  3650. Divide self by other in-place.
  3651. """
  3652. other_data = getdata(other)
  3653. dom_mask = _DomainSafeDivide().__call__(self._data, other_data)
  3654. other_mask = getmask(other)
  3655. new_mask = mask_or(other_mask, dom_mask)
  3656. # The following 4 lines control the domain filling
  3657. if dom_mask.any():
  3658. (_, fval) = ufunc_fills[np.divide]
  3659. other_data = np.where(
  3660. dom_mask, other_data.dtype.type(fval), other_data)
  3661. self._mask |= new_mask
  3662. other_data = np.where(self._mask, other_data.dtype.type(1), other_data)
  3663. self._data.__idiv__(other_data)
  3664. return self
  3665. def __ifloordiv__(self, other):
  3666. """
  3667. Floor divide self by other in-place.
  3668. """
  3669. other_data = getdata(other)
  3670. dom_mask = _DomainSafeDivide().__call__(self._data, other_data)
  3671. other_mask = getmask(other)
  3672. new_mask = mask_or(other_mask, dom_mask)
  3673. # The following 3 lines control the domain filling
  3674. if dom_mask.any():
  3675. (_, fval) = ufunc_fills[np.floor_divide]
  3676. other_data = np.where(
  3677. dom_mask, other_data.dtype.type(fval), other_data)
  3678. self._mask |= new_mask
  3679. other_data = np.where(self._mask, other_data.dtype.type(1), other_data)
  3680. self._data.__ifloordiv__(other_data)
  3681. return self
  3682. def __itruediv__(self, other):
  3683. """
  3684. True divide self by other in-place.
  3685. """
  3686. other_data = getdata(other)
  3687. dom_mask = _DomainSafeDivide().__call__(self._data, other_data)
  3688. other_mask = getmask(other)
  3689. new_mask = mask_or(other_mask, dom_mask)
  3690. # The following 3 lines control the domain filling
  3691. if dom_mask.any():
  3692. (_, fval) = ufunc_fills[np.true_divide]
  3693. other_data = np.where(
  3694. dom_mask, other_data.dtype.type(fval), other_data)
  3695. self._mask |= new_mask
  3696. other_data = np.where(self._mask, other_data.dtype.type(1), other_data)
  3697. self._data.__itruediv__(other_data)
  3698. return self
  3699. def __ipow__(self, other):
  3700. """
  3701. Raise self to the power other, in place.
  3702. """
  3703. other_data = getdata(other)
  3704. other_data = np.where(self._mask, other_data.dtype.type(1), other_data)
  3705. other_mask = getmask(other)
  3706. with np.errstate(divide='ignore', invalid='ignore'):
  3707. self._data.__ipow__(other_data)
  3708. invalid = np.logical_not(np.isfinite(self._data))
  3709. if invalid.any():
  3710. if self._mask is not nomask:
  3711. self._mask |= invalid
  3712. else:
  3713. self._mask = invalid
  3714. np.copyto(self._data, self.fill_value, where=invalid)
  3715. new_mask = mask_or(other_mask, invalid)
  3716. self._mask = mask_or(self._mask, new_mask)
  3717. return self
  3718. def __float__(self):
  3719. """
  3720. Convert to float.
  3721. """
  3722. if self.size > 1:
  3723. raise TypeError("Only length-1 arrays can be converted "
  3724. "to Python scalars")
  3725. elif self._mask:
  3726. warnings.warn("Warning: converting a masked element to nan.", stacklevel=2)
  3727. return np.nan
  3728. return float(self.item())
  3729. def __int__(self):
  3730. """
  3731. Convert to int.
  3732. """
  3733. if self.size > 1:
  3734. raise TypeError("Only length-1 arrays can be converted "
  3735. "to Python scalars")
  3736. elif self._mask:
  3737. raise MaskError('Cannot convert masked element to a Python int.')
  3738. return int(self.item())
  3739. @property
  3740. def imag(self):
  3741. """
  3742. The imaginary part of the masked array.
  3743. This property is a view on the imaginary part of this `MaskedArray`.
  3744. See Also
  3745. --------
  3746. real
  3747. Examples
  3748. --------
  3749. >>> x = np.ma.array([1+1.j, -2j, 3.45+1.6j], mask=[False, True, False])
  3750. >>> x.imag
  3751. masked_array(data=[1.0, --, 1.6],
  3752. mask=[False, True, False],
  3753. fill_value=1e+20)
  3754. """
  3755. result = self._data.imag.view(type(self))
  3756. result.__setmask__(self._mask)
  3757. return result
  3758. # kept for compatibility
  3759. get_imag = imag.fget
  3760. @property
  3761. def real(self):
  3762. """
  3763. The real part of the masked array.
  3764. This property is a view on the real part of this `MaskedArray`.
  3765. See Also
  3766. --------
  3767. imag
  3768. Examples
  3769. --------
  3770. >>> x = np.ma.array([1+1.j, -2j, 3.45+1.6j], mask=[False, True, False])
  3771. >>> x.real
  3772. masked_array(data=[1.0, --, 3.45],
  3773. mask=[False, True, False],
  3774. fill_value=1e+20)
  3775. """
  3776. result = self._data.real.view(type(self))
  3777. result.__setmask__(self._mask)
  3778. return result
  3779. # kept for compatibility
  3780. get_real = real.fget
  3781. def count(self, axis=None, keepdims=np._NoValue):
  3782. """
  3783. Count the non-masked elements of the array along the given axis.
  3784. Parameters
  3785. ----------
  3786. axis : None or int or tuple of ints, optional
  3787. Axis or axes along which the count is performed.
  3788. The default, None, performs the count over all
  3789. the dimensions of the input array. `axis` may be negative, in
  3790. which case it counts from the last to the first axis.
  3791. .. versionadded:: 1.10.0
  3792. If this is a tuple of ints, the count is performed on multiple
  3793. axes, instead of a single axis or all the axes as before.
  3794. keepdims : bool, optional
  3795. If this is set to True, the axes which are reduced are left
  3796. in the result as dimensions with size one. With this option,
  3797. the result will broadcast correctly against the array.
  3798. Returns
  3799. -------
  3800. result : ndarray or scalar
  3801. An array with the same shape as the input array, with the specified
  3802. axis removed. If the array is a 0-d array, or if `axis` is None, a
  3803. scalar is returned.
  3804. See Also
  3805. --------
  3806. ma.count_masked : Count masked elements in array or along a given axis.
  3807. Examples
  3808. --------
  3809. >>> import numpy.ma as ma
  3810. >>> a = ma.arange(6).reshape((2, 3))
  3811. >>> a[1, :] = ma.masked
  3812. >>> a
  3813. masked_array(
  3814. data=[[0, 1, 2],
  3815. [--, --, --]],
  3816. mask=[[False, False, False],
  3817. [ True, True, True]],
  3818. fill_value=999999)
  3819. >>> a.count()
  3820. 3
  3821. When the `axis` keyword is specified an array of appropriate size is
  3822. returned.
  3823. >>> a.count(axis=0)
  3824. array([1, 1, 1])
  3825. >>> a.count(axis=1)
  3826. array([3, 0])
  3827. """
  3828. kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims}
  3829. m = self._mask
  3830. # special case for matrices (we assume no other subclasses modify
  3831. # their dimensions)
  3832. if isinstance(self.data, np.matrix):
  3833. if m is nomask:
  3834. m = np.zeros(self.shape, dtype=np.bool_)
  3835. m = m.view(type(self.data))
  3836. if m is nomask:
  3837. # compare to _count_reduce_items in _methods.py
  3838. if self.shape == ():
  3839. if axis not in (None, 0):
  3840. raise np.AxisError(axis=axis, ndim=self.ndim)
  3841. return 1
  3842. elif axis is None:
  3843. if kwargs.get('keepdims', False):
  3844. return np.array(self.size, dtype=np.intp, ndmin=self.ndim)
  3845. return self.size
  3846. axes = normalize_axis_tuple(axis, self.ndim)
  3847. items = 1
  3848. for ax in axes:
  3849. items *= self.shape[ax]
  3850. if kwargs.get('keepdims', False):
  3851. out_dims = list(self.shape)
  3852. for a in axes:
  3853. out_dims[a] = 1
  3854. else:
  3855. out_dims = [d for n, d in enumerate(self.shape)
  3856. if n not in axes]
  3857. # make sure to return a 0-d array if axis is supplied
  3858. return np.full(out_dims, items, dtype=np.intp)
  3859. # take care of the masked singleton
  3860. if self is masked:
  3861. return 0
  3862. return (~m).sum(axis=axis, dtype=np.intp, **kwargs)
  3863. def ravel(self, order='C'):
  3864. """
  3865. Returns a 1D version of self, as a view.
  3866. Parameters
  3867. ----------
  3868. order : {'C', 'F', 'A', 'K'}, optional
  3869. The elements of `a` are read using this index order. 'C' means to
  3870. index the elements in C-like order, with the last axis index
  3871. changing fastest, back to the first axis index changing slowest.
  3872. 'F' means to index the elements in Fortran-like index order, with
  3873. the first index changing fastest, and the last index changing
  3874. slowest. Note that the 'C' and 'F' options take no account of the
  3875. memory layout of the underlying array, and only refer to the order
  3876. of axis indexing. 'A' means to read the elements in Fortran-like
  3877. index order if `m` is Fortran *contiguous* in memory, C-like order
  3878. otherwise. 'K' means to read the elements in the order they occur
  3879. in memory, except for reversing the data when strides are negative.
  3880. By default, 'C' index order is used.
  3881. (Masked arrays currently use 'A' on the data when 'K' is passed.)
  3882. Returns
  3883. -------
  3884. MaskedArray
  3885. Output view is of shape ``(self.size,)`` (or
  3886. ``(np.ma.product(self.shape),)``).
  3887. Examples
  3888. --------
  3889. >>> x = np.ma.array([[1,2,3],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4)
  3890. >>> x
  3891. masked_array(
  3892. data=[[1, --, 3],
  3893. [--, 5, --],
  3894. [7, --, 9]],
  3895. mask=[[False, True, False],
  3896. [ True, False, True],
  3897. [False, True, False]],
  3898. fill_value=999999)
  3899. >>> x.ravel()
  3900. masked_array(data=[1, --, 3, --, 5, --, 7, --, 9],
  3901. mask=[False, True, False, True, False, True, False, True,
  3902. False],
  3903. fill_value=999999)
  3904. """
  3905. # The order of _data and _mask could be different (it shouldn't be
  3906. # normally). Passing order `K` or `A` would be incorrect.
  3907. # So we ignore the mask memory order.
  3908. # TODO: We don't actually support K, so use A instead. We could
  3909. # try to guess this correct by sorting strides or deprecate.
  3910. if order in "kKaA":
  3911. order = "F" if self._data.flags.fnc else "C"
  3912. r = ndarray.ravel(self._data, order=order).view(type(self))
  3913. r._update_from(self)
  3914. if self._mask is not nomask:
  3915. r._mask = ndarray.ravel(self._mask, order=order).reshape(r.shape)
  3916. else:
  3917. r._mask = nomask
  3918. return r
  3919. def reshape(self, *s, **kwargs):
  3920. """
  3921. Give a new shape to the array without changing its data.
  3922. Returns a masked array containing the same data, but with a new shape.
  3923. The result is a view on the original array; if this is not possible, a
  3924. ValueError is raised.
  3925. Parameters
  3926. ----------
  3927. shape : int or tuple of ints
  3928. The new shape should be compatible with the original shape. If an
  3929. integer is supplied, then the result will be a 1-D array of that
  3930. length.
  3931. order : {'C', 'F'}, optional
  3932. Determines whether the array data should be viewed as in C
  3933. (row-major) or FORTRAN (column-major) order.
  3934. Returns
  3935. -------
  3936. reshaped_array : array
  3937. A new view on the array.
  3938. See Also
  3939. --------
  3940. reshape : Equivalent function in the masked array module.
  3941. numpy.ndarray.reshape : Equivalent method on ndarray object.
  3942. numpy.reshape : Equivalent function in the NumPy module.
  3943. Notes
  3944. -----
  3945. The reshaping operation cannot guarantee that a copy will not be made,
  3946. to modify the shape in place, use ``a.shape = s``
  3947. Examples
  3948. --------
  3949. >>> x = np.ma.array([[1,2],[3,4]], mask=[1,0,0,1])
  3950. >>> x
  3951. masked_array(
  3952. data=[[--, 2],
  3953. [3, --]],
  3954. mask=[[ True, False],
  3955. [False, True]],
  3956. fill_value=999999)
  3957. >>> x = x.reshape((4,1))
  3958. >>> x
  3959. masked_array(
  3960. data=[[--],
  3961. [2],
  3962. [3],
  3963. [--]],
  3964. mask=[[ True],
  3965. [False],
  3966. [False],
  3967. [ True]],
  3968. fill_value=999999)
  3969. """
  3970. kwargs.update(order=kwargs.get('order', 'C'))
  3971. result = self._data.reshape(*s, **kwargs).view(type(self))
  3972. result._update_from(self)
  3973. mask = self._mask
  3974. if mask is not nomask:
  3975. result._mask = mask.reshape(*s, **kwargs)
  3976. return result
  3977. def resize(self, newshape, refcheck=True, order=False):
  3978. """
  3979. .. warning::
  3980. This method does nothing, except raise a ValueError exception. A
  3981. masked array does not own its data and therefore cannot safely be
  3982. resized in place. Use the `numpy.ma.resize` function instead.
  3983. This method is difficult to implement safely and may be deprecated in
  3984. future releases of NumPy.
  3985. """
  3986. # Note : the 'order' keyword looks broken, let's just drop it
  3987. errmsg = "A masked array does not own its data "\
  3988. "and therefore cannot be resized.\n" \
  3989. "Use the numpy.ma.resize function instead."
  3990. raise ValueError(errmsg)
  3991. def put(self, indices, values, mode='raise'):
  3992. """
  3993. Set storage-indexed locations to corresponding values.
  3994. Sets self._data.flat[n] = values[n] for each n in indices.
  3995. If `values` is shorter than `indices` then it will repeat.
  3996. If `values` has some masked values, the initial mask is updated
  3997. in consequence, else the corresponding values are unmasked.
  3998. Parameters
  3999. ----------
  4000. indices : 1-D array_like
  4001. Target indices, interpreted as integers.
  4002. values : array_like
  4003. Values to place in self._data copy at target indices.
  4004. mode : {'raise', 'wrap', 'clip'}, optional
  4005. Specifies how out-of-bounds indices will behave.
  4006. 'raise' : raise an error.
  4007. 'wrap' : wrap around.
  4008. 'clip' : clip to the range.
  4009. Notes
  4010. -----
  4011. `values` can be a scalar or length 1 array.
  4012. Examples
  4013. --------
  4014. >>> x = np.ma.array([[1,2,3],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4)
  4015. >>> x
  4016. masked_array(
  4017. data=[[1, --, 3],
  4018. [--, 5, --],
  4019. [7, --, 9]],
  4020. mask=[[False, True, False],
  4021. [ True, False, True],
  4022. [False, True, False]],
  4023. fill_value=999999)
  4024. >>> x.put([0,4,8],[10,20,30])
  4025. >>> x
  4026. masked_array(
  4027. data=[[10, --, 3],
  4028. [--, 20, --],
  4029. [7, --, 30]],
  4030. mask=[[False, True, False],
  4031. [ True, False, True],
  4032. [False, True, False]],
  4033. fill_value=999999)
  4034. >>> x.put(4,999)
  4035. >>> x
  4036. masked_array(
  4037. data=[[10, --, 3],
  4038. [--, 999, --],
  4039. [7, --, 30]],
  4040. mask=[[False, True, False],
  4041. [ True, False, True],
  4042. [False, True, False]],
  4043. fill_value=999999)
  4044. """
  4045. # Hard mask: Get rid of the values/indices that fall on masked data
  4046. if self._hardmask and self._mask is not nomask:
  4047. mask = self._mask[indices]
  4048. indices = narray(indices, copy=False)
  4049. values = narray(values, copy=False, subok=True)
  4050. values.resize(indices.shape)
  4051. indices = indices[~mask]
  4052. values = values[~mask]
  4053. self._data.put(indices, values, mode=mode)
  4054. # short circuit if neither self nor values are masked
  4055. if self._mask is nomask and getmask(values) is nomask:
  4056. return
  4057. m = getmaskarray(self)
  4058. if getmask(values) is nomask:
  4059. m.put(indices, False, mode=mode)
  4060. else:
  4061. m.put(indices, values._mask, mode=mode)
  4062. m = make_mask(m, copy=False, shrink=True)
  4063. self._mask = m
  4064. return
  4065. def ids(self):
  4066. """
  4067. Return the addresses of the data and mask areas.
  4068. Parameters
  4069. ----------
  4070. None
  4071. Examples
  4072. --------
  4073. >>> x = np.ma.array([1, 2, 3], mask=[0, 1, 1])
  4074. >>> x.ids()
  4075. (166670640, 166659832) # may vary
  4076. If the array has no mask, the address of `nomask` is returned. This address
  4077. is typically not close to the data in memory:
  4078. >>> x = np.ma.array([1, 2, 3])
  4079. >>> x.ids()
  4080. (166691080, 3083169284) # may vary
  4081. """
  4082. if self._mask is nomask:
  4083. return (self.ctypes.data, id(nomask))
  4084. return (self.ctypes.data, self._mask.ctypes.data)
  4085. def iscontiguous(self):
  4086. """
  4087. Return a boolean indicating whether the data is contiguous.
  4088. Parameters
  4089. ----------
  4090. None
  4091. Examples
  4092. --------
  4093. >>> x = np.ma.array([1, 2, 3])
  4094. >>> x.iscontiguous()
  4095. True
  4096. `iscontiguous` returns one of the flags of the masked array:
  4097. >>> x.flags
  4098. C_CONTIGUOUS : True
  4099. F_CONTIGUOUS : True
  4100. OWNDATA : False
  4101. WRITEABLE : True
  4102. ALIGNED : True
  4103. WRITEBACKIFCOPY : False
  4104. """
  4105. return self.flags['CONTIGUOUS']
  4106. def all(self, axis=None, out=None, keepdims=np._NoValue):
  4107. """
  4108. Returns True if all elements evaluate to True.
  4109. The output array is masked where all the values along the given axis
  4110. are masked: if the output would have been a scalar and that all the
  4111. values are masked, then the output is `masked`.
  4112. Refer to `numpy.all` for full documentation.
  4113. See Also
  4114. --------
  4115. numpy.ndarray.all : corresponding function for ndarrays
  4116. numpy.all : equivalent function
  4117. Examples
  4118. --------
  4119. >>> np.ma.array([1,2,3]).all()
  4120. True
  4121. >>> a = np.ma.array([1,2,3], mask=True)
  4122. >>> (a.all() is np.ma.masked)
  4123. True
  4124. """
  4125. kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims}
  4126. mask = _check_mask_axis(self._mask, axis, **kwargs)
  4127. if out is None:
  4128. d = self.filled(True).all(axis=axis, **kwargs).view(type(self))
  4129. if d.ndim:
  4130. d.__setmask__(mask)
  4131. elif mask:
  4132. return masked
  4133. return d
  4134. self.filled(True).all(axis=axis, out=out, **kwargs)
  4135. if isinstance(out, MaskedArray):
  4136. if out.ndim or mask:
  4137. out.__setmask__(mask)
  4138. return out
  4139. def any(self, axis=None, out=None, keepdims=np._NoValue):
  4140. """
  4141. Returns True if any of the elements of `a` evaluate to True.
  4142. Masked values are considered as False during computation.
  4143. Refer to `numpy.any` for full documentation.
  4144. See Also
  4145. --------
  4146. numpy.ndarray.any : corresponding function for ndarrays
  4147. numpy.any : equivalent function
  4148. """
  4149. kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims}
  4150. mask = _check_mask_axis(self._mask, axis, **kwargs)
  4151. if out is None:
  4152. d = self.filled(False).any(axis=axis, **kwargs).view(type(self))
  4153. if d.ndim:
  4154. d.__setmask__(mask)
  4155. elif mask:
  4156. d = masked
  4157. return d
  4158. self.filled(False).any(axis=axis, out=out, **kwargs)
  4159. if isinstance(out, MaskedArray):
  4160. if out.ndim or mask:
  4161. out.__setmask__(mask)
  4162. return out
  4163. def nonzero(self):
  4164. """
  4165. Return the indices of unmasked elements that are not zero.
  4166. Returns a tuple of arrays, one for each dimension, containing the
  4167. indices of the non-zero elements in that dimension. The corresponding
  4168. non-zero values can be obtained with::
  4169. a[a.nonzero()]
  4170. To group the indices by element, rather than dimension, use
  4171. instead::
  4172. np.transpose(a.nonzero())
  4173. The result of this is always a 2d array, with a row for each non-zero
  4174. element.
  4175. Parameters
  4176. ----------
  4177. None
  4178. Returns
  4179. -------
  4180. tuple_of_arrays : tuple
  4181. Indices of elements that are non-zero.
  4182. See Also
  4183. --------
  4184. numpy.nonzero :
  4185. Function operating on ndarrays.
  4186. flatnonzero :
  4187. Return indices that are non-zero in the flattened version of the input
  4188. array.
  4189. numpy.ndarray.nonzero :
  4190. Equivalent ndarray method.
  4191. count_nonzero :
  4192. Counts the number of non-zero elements in the input array.
  4193. Examples
  4194. --------
  4195. >>> import numpy.ma as ma
  4196. >>> x = ma.array(np.eye(3))
  4197. >>> x
  4198. masked_array(
  4199. data=[[1., 0., 0.],
  4200. [0., 1., 0.],
  4201. [0., 0., 1.]],
  4202. mask=False,
  4203. fill_value=1e+20)
  4204. >>> x.nonzero()
  4205. (array([0, 1, 2]), array([0, 1, 2]))
  4206. Masked elements are ignored.
  4207. >>> x[1, 1] = ma.masked
  4208. >>> x
  4209. masked_array(
  4210. data=[[1.0, 0.0, 0.0],
  4211. [0.0, --, 0.0],
  4212. [0.0, 0.0, 1.0]],
  4213. mask=[[False, False, False],
  4214. [False, True, False],
  4215. [False, False, False]],
  4216. fill_value=1e+20)
  4217. >>> x.nonzero()
  4218. (array([0, 2]), array([0, 2]))
  4219. Indices can also be grouped by element.
  4220. >>> np.transpose(x.nonzero())
  4221. array([[0, 0],
  4222. [2, 2]])
  4223. A common use for ``nonzero`` is to find the indices of an array, where
  4224. a condition is True. Given an array `a`, the condition `a` > 3 is a
  4225. boolean array and since False is interpreted as 0, ma.nonzero(a > 3)
  4226. yields the indices of the `a` where the condition is true.
  4227. >>> a = ma.array([[1,2,3],[4,5,6],[7,8,9]])
  4228. >>> a > 3
  4229. masked_array(
  4230. data=[[False, False, False],
  4231. [ True, True, True],
  4232. [ True, True, True]],
  4233. mask=False,
  4234. fill_value=True)
  4235. >>> ma.nonzero(a > 3)
  4236. (array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))
  4237. The ``nonzero`` method of the condition array can also be called.
  4238. >>> (a > 3).nonzero()
  4239. (array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))
  4240. """
  4241. return narray(self.filled(0), copy=False).nonzero()
  4242. def trace(self, offset=0, axis1=0, axis2=1, dtype=None, out=None):
  4243. """
  4244. (this docstring should be overwritten)
  4245. """
  4246. #!!!: implement out + test!
  4247. m = self._mask
  4248. if m is nomask:
  4249. result = super().trace(offset=offset, axis1=axis1, axis2=axis2,
  4250. out=out)
  4251. return result.astype(dtype)
  4252. else:
  4253. D = self.diagonal(offset=offset, axis1=axis1, axis2=axis2)
  4254. return D.astype(dtype).filled(0).sum(axis=-1, out=out)
  4255. trace.__doc__ = ndarray.trace.__doc__
  4256. def dot(self, b, out=None, strict=False):
  4257. """
  4258. a.dot(b, out=None)
  4259. Masked dot product of two arrays. Note that `out` and `strict` are
  4260. located in different positions than in `ma.dot`. In order to
  4261. maintain compatibility with the functional version, it is
  4262. recommended that the optional arguments be treated as keyword only.
  4263. At some point that may be mandatory.
  4264. .. versionadded:: 1.10.0
  4265. Parameters
  4266. ----------
  4267. b : masked_array_like
  4268. Inputs array.
  4269. out : masked_array, optional
  4270. Output argument. This must have the exact kind that would be
  4271. returned if it was not used. In particular, it must have the
  4272. right type, must be C-contiguous, and its dtype must be the
  4273. dtype that would be returned for `ma.dot(a,b)`. This is a
  4274. performance feature. Therefore, if these conditions are not
  4275. met, an exception is raised, instead of attempting to be
  4276. flexible.
  4277. strict : bool, optional
  4278. Whether masked data are propagated (True) or set to 0 (False)
  4279. for the computation. Default is False. Propagating the mask
  4280. means that if a masked value appears in a row or column, the
  4281. whole row or column is considered masked.
  4282. .. versionadded:: 1.10.2
  4283. See Also
  4284. --------
  4285. numpy.ma.dot : equivalent function
  4286. """
  4287. return dot(self, b, out=out, strict=strict)
  4288. def sum(self, axis=None, dtype=None, out=None, keepdims=np._NoValue):
  4289. """
  4290. Return the sum of the array elements over the given axis.
  4291. Masked elements are set to 0 internally.
  4292. Refer to `numpy.sum` for full documentation.
  4293. See Also
  4294. --------
  4295. numpy.ndarray.sum : corresponding function for ndarrays
  4296. numpy.sum : equivalent function
  4297. Examples
  4298. --------
  4299. >>> x = np.ma.array([[1,2,3],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4)
  4300. >>> x
  4301. masked_array(
  4302. data=[[1, --, 3],
  4303. [--, 5, --],
  4304. [7, --, 9]],
  4305. mask=[[False, True, False],
  4306. [ True, False, True],
  4307. [False, True, False]],
  4308. fill_value=999999)
  4309. >>> x.sum()
  4310. 25
  4311. >>> x.sum(axis=1)
  4312. masked_array(data=[4, 5, 16],
  4313. mask=[False, False, False],
  4314. fill_value=999999)
  4315. >>> x.sum(axis=0)
  4316. masked_array(data=[8, 5, 12],
  4317. mask=[False, False, False],
  4318. fill_value=999999)
  4319. >>> print(type(x.sum(axis=0, dtype=np.int64)[0]))
  4320. <class 'numpy.int64'>
  4321. """
  4322. kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims}
  4323. _mask = self._mask
  4324. newmask = _check_mask_axis(_mask, axis, **kwargs)
  4325. # No explicit output
  4326. if out is None:
  4327. result = self.filled(0).sum(axis, dtype=dtype, **kwargs)
  4328. rndim = getattr(result, 'ndim', 0)
  4329. if rndim:
  4330. result = result.view(type(self))
  4331. result.__setmask__(newmask)
  4332. elif newmask:
  4333. result = masked
  4334. return result
  4335. # Explicit output
  4336. result = self.filled(0).sum(axis, dtype=dtype, out=out, **kwargs)
  4337. if isinstance(out, MaskedArray):
  4338. outmask = getmask(out)
  4339. if outmask is nomask:
  4340. outmask = out._mask = make_mask_none(out.shape)
  4341. outmask.flat = newmask
  4342. return out
  4343. def cumsum(self, axis=None, dtype=None, out=None):
  4344. """
  4345. Return the cumulative sum of the array elements over the given axis.
  4346. Masked values are set to 0 internally during the computation.
  4347. However, their position is saved, and the result will be masked at
  4348. the same locations.
  4349. Refer to `numpy.cumsum` for full documentation.
  4350. Notes
  4351. -----
  4352. The mask is lost if `out` is not a valid :class:`ma.MaskedArray` !
  4353. Arithmetic is modular when using integer types, and no error is
  4354. raised on overflow.
  4355. See Also
  4356. --------
  4357. numpy.ndarray.cumsum : corresponding function for ndarrays
  4358. numpy.cumsum : equivalent function
  4359. Examples
  4360. --------
  4361. >>> marr = np.ma.array(np.arange(10), mask=[0,0,0,1,1,1,0,0,0,0])
  4362. >>> marr.cumsum()
  4363. masked_array(data=[0, 1, 3, --, --, --, 9, 16, 24, 33],
  4364. mask=[False, False, False, True, True, True, False, False,
  4365. False, False],
  4366. fill_value=999999)
  4367. """
  4368. result = self.filled(0).cumsum(axis=axis, dtype=dtype, out=out)
  4369. if out is not None:
  4370. if isinstance(out, MaskedArray):
  4371. out.__setmask__(self.mask)
  4372. return out
  4373. result = result.view(type(self))
  4374. result.__setmask__(self._mask)
  4375. return result
  4376. def prod(self, axis=None, dtype=None, out=None, keepdims=np._NoValue):
  4377. """
  4378. Return the product of the array elements over the given axis.
  4379. Masked elements are set to 1 internally for computation.
  4380. Refer to `numpy.prod` for full documentation.
  4381. Notes
  4382. -----
  4383. Arithmetic is modular when using integer types, and no error is raised
  4384. on overflow.
  4385. See Also
  4386. --------
  4387. numpy.ndarray.prod : corresponding function for ndarrays
  4388. numpy.prod : equivalent function
  4389. """
  4390. kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims}
  4391. _mask = self._mask
  4392. newmask = _check_mask_axis(_mask, axis, **kwargs)
  4393. # No explicit output
  4394. if out is None:
  4395. result = self.filled(1).prod(axis, dtype=dtype, **kwargs)
  4396. rndim = getattr(result, 'ndim', 0)
  4397. if rndim:
  4398. result = result.view(type(self))
  4399. result.__setmask__(newmask)
  4400. elif newmask:
  4401. result = masked
  4402. return result
  4403. # Explicit output
  4404. result = self.filled(1).prod(axis, dtype=dtype, out=out, **kwargs)
  4405. if isinstance(out, MaskedArray):
  4406. outmask = getmask(out)
  4407. if outmask is nomask:
  4408. outmask = out._mask = make_mask_none(out.shape)
  4409. outmask.flat = newmask
  4410. return out
  4411. product = prod
  4412. def cumprod(self, axis=None, dtype=None, out=None):
  4413. """
  4414. Return the cumulative product of the array elements over the given axis.
  4415. Masked values are set to 1 internally during the computation.
  4416. However, their position is saved, and the result will be masked at
  4417. the same locations.
  4418. Refer to `numpy.cumprod` for full documentation.
  4419. Notes
  4420. -----
  4421. The mask is lost if `out` is not a valid MaskedArray !
  4422. Arithmetic is modular when using integer types, and no error is
  4423. raised on overflow.
  4424. See Also
  4425. --------
  4426. numpy.ndarray.cumprod : corresponding function for ndarrays
  4427. numpy.cumprod : equivalent function
  4428. """
  4429. result = self.filled(1).cumprod(axis=axis, dtype=dtype, out=out)
  4430. if out is not None:
  4431. if isinstance(out, MaskedArray):
  4432. out.__setmask__(self._mask)
  4433. return out
  4434. result = result.view(type(self))
  4435. result.__setmask__(self._mask)
  4436. return result
  4437. def mean(self, axis=None, dtype=None, out=None, keepdims=np._NoValue):
  4438. """
  4439. Returns the average of the array elements along given axis.
  4440. Masked entries are ignored, and result elements which are not
  4441. finite will be masked.
  4442. Refer to `numpy.mean` for full documentation.
  4443. See Also
  4444. --------
  4445. numpy.ndarray.mean : corresponding function for ndarrays
  4446. numpy.mean : Equivalent function
  4447. numpy.ma.average : Weighted average.
  4448. Examples
  4449. --------
  4450. >>> a = np.ma.array([1,2,3], mask=[False, False, True])
  4451. >>> a
  4452. masked_array(data=[1, 2, --],
  4453. mask=[False, False, True],
  4454. fill_value=999999)
  4455. >>> a.mean()
  4456. 1.5
  4457. """
  4458. kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims}
  4459. if self._mask is nomask:
  4460. result = super().mean(axis=axis, dtype=dtype, **kwargs)[()]
  4461. else:
  4462. is_float16_result = False
  4463. if dtype is None:
  4464. if issubclass(self.dtype.type, (ntypes.integer, ntypes.bool_)):
  4465. dtype = mu.dtype('f8')
  4466. elif issubclass(self.dtype.type, ntypes.float16):
  4467. dtype = mu.dtype('f4')
  4468. is_float16_result = True
  4469. dsum = self.sum(axis=axis, dtype=dtype, **kwargs)
  4470. cnt = self.count(axis=axis, **kwargs)
  4471. if cnt.shape == () and (cnt == 0):
  4472. result = masked
  4473. elif is_float16_result:
  4474. result = self.dtype.type(dsum * 1. / cnt)
  4475. else:
  4476. result = dsum * 1. / cnt
  4477. if out is not None:
  4478. out.flat = result
  4479. if isinstance(out, MaskedArray):
  4480. outmask = getmask(out)
  4481. if outmask is nomask:
  4482. outmask = out._mask = make_mask_none(out.shape)
  4483. outmask.flat = getmask(result)
  4484. return out
  4485. return result
  4486. def anom(self, axis=None, dtype=None):
  4487. """
  4488. Compute the anomalies (deviations from the arithmetic mean)
  4489. along the given axis.
  4490. Returns an array of anomalies, with the same shape as the input and
  4491. where the arithmetic mean is computed along the given axis.
  4492. Parameters
  4493. ----------
  4494. axis : int, optional
  4495. Axis over which the anomalies are taken.
  4496. The default is to use the mean of the flattened array as reference.
  4497. dtype : dtype, optional
  4498. Type to use in computing the variance. For arrays of integer type
  4499. the default is float32; for arrays of float types it is the same as
  4500. the array type.
  4501. See Also
  4502. --------
  4503. mean : Compute the mean of the array.
  4504. Examples
  4505. --------
  4506. >>> a = np.ma.array([1,2,3])
  4507. >>> a.anom()
  4508. masked_array(data=[-1., 0., 1.],
  4509. mask=False,
  4510. fill_value=1e+20)
  4511. """
  4512. m = self.mean(axis, dtype)
  4513. if not axis:
  4514. return self - m
  4515. else:
  4516. return self - expand_dims(m, axis)
  4517. def var(self, axis=None, dtype=None, out=None, ddof=0,
  4518. keepdims=np._NoValue):
  4519. """
  4520. Returns the variance of the array elements along given axis.
  4521. Masked entries are ignored, and result elements which are not
  4522. finite will be masked.
  4523. Refer to `numpy.var` for full documentation.
  4524. See Also
  4525. --------
  4526. numpy.ndarray.var : corresponding function for ndarrays
  4527. numpy.var : Equivalent function
  4528. """
  4529. kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims}
  4530. # Easy case: nomask, business as usual
  4531. if self._mask is nomask:
  4532. ret = super().var(axis=axis, dtype=dtype, out=out, ddof=ddof,
  4533. **kwargs)[()]
  4534. if out is not None:
  4535. if isinstance(out, MaskedArray):
  4536. out.__setmask__(nomask)
  4537. return out
  4538. return ret
  4539. # Some data are masked, yay!
  4540. cnt = self.count(axis=axis, **kwargs) - ddof
  4541. danom = self - self.mean(axis, dtype, keepdims=True)
  4542. if iscomplexobj(self):
  4543. danom = umath.absolute(danom) ** 2
  4544. else:
  4545. danom *= danom
  4546. dvar = divide(danom.sum(axis, **kwargs), cnt).view(type(self))
  4547. # Apply the mask if it's not a scalar
  4548. if dvar.ndim:
  4549. dvar._mask = mask_or(self._mask.all(axis, **kwargs), (cnt <= 0))
  4550. dvar._update_from(self)
  4551. elif getmask(dvar):
  4552. # Make sure that masked is returned when the scalar is masked.
  4553. dvar = masked
  4554. if out is not None:
  4555. if isinstance(out, MaskedArray):
  4556. out.flat = 0
  4557. out.__setmask__(True)
  4558. elif out.dtype.kind in 'biu':
  4559. errmsg = "Masked data information would be lost in one or "\
  4560. "more location."
  4561. raise MaskError(errmsg)
  4562. else:
  4563. out.flat = np.nan
  4564. return out
  4565. # In case with have an explicit output
  4566. if out is not None:
  4567. # Set the data
  4568. out.flat = dvar
  4569. # Set the mask if needed
  4570. if isinstance(out, MaskedArray):
  4571. out.__setmask__(dvar.mask)
  4572. return out
  4573. return dvar
  4574. var.__doc__ = np.var.__doc__
  4575. def std(self, axis=None, dtype=None, out=None, ddof=0,
  4576. keepdims=np._NoValue):
  4577. """
  4578. Returns the standard deviation of the array elements along given axis.
  4579. Masked entries are ignored.
  4580. Refer to `numpy.std` for full documentation.
  4581. See Also
  4582. --------
  4583. numpy.ndarray.std : corresponding function for ndarrays
  4584. numpy.std : Equivalent function
  4585. """
  4586. kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims}
  4587. dvar = self.var(axis, dtype, out, ddof, **kwargs)
  4588. if dvar is not masked:
  4589. if out is not None:
  4590. np.power(out, 0.5, out=out, casting='unsafe')
  4591. return out
  4592. dvar = sqrt(dvar)
  4593. return dvar
  4594. def round(self, decimals=0, out=None):
  4595. """
  4596. Return each element rounded to the given number of decimals.
  4597. Refer to `numpy.around` for full documentation.
  4598. See Also
  4599. --------
  4600. numpy.ndarray.round : corresponding function for ndarrays
  4601. numpy.around : equivalent function
  4602. """
  4603. result = self._data.round(decimals=decimals, out=out).view(type(self))
  4604. if result.ndim > 0:
  4605. result._mask = self._mask
  4606. result._update_from(self)
  4607. elif self._mask:
  4608. # Return masked when the scalar is masked
  4609. result = masked
  4610. # No explicit output: we're done
  4611. if out is None:
  4612. return result
  4613. if isinstance(out, MaskedArray):
  4614. out.__setmask__(self._mask)
  4615. return out
  4616. def argsort(self, axis=np._NoValue, kind=None, order=None,
  4617. endwith=True, fill_value=None):
  4618. """
  4619. Return an ndarray of indices that sort the array along the
  4620. specified axis. Masked values are filled beforehand to
  4621. `fill_value`.
  4622. Parameters
  4623. ----------
  4624. axis : int, optional
  4625. Axis along which to sort. If None, the default, the flattened array
  4626. is used.
  4627. .. versionchanged:: 1.13.0
  4628. Previously, the default was documented to be -1, but that was
  4629. in error. At some future date, the default will change to -1, as
  4630. originally intended.
  4631. Until then, the axis should be given explicitly when
  4632. ``arr.ndim > 1``, to avoid a FutureWarning.
  4633. kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, optional
  4634. The sorting algorithm used.
  4635. order : list, optional
  4636. When `a` is an array with fields defined, this argument specifies
  4637. which fields to compare first, second, etc. Not all fields need be
  4638. specified.
  4639. endwith : {True, False}, optional
  4640. Whether missing values (if any) should be treated as the largest values
  4641. (True) or the smallest values (False)
  4642. When the array contains unmasked values at the same extremes of the
  4643. datatype, the ordering of these values and the masked values is
  4644. undefined.
  4645. fill_value : scalar or None, optional
  4646. Value used internally for the masked values.
  4647. If ``fill_value`` is not None, it supersedes ``endwith``.
  4648. Returns
  4649. -------
  4650. index_array : ndarray, int
  4651. Array of indices that sort `a` along the specified axis.
  4652. In other words, ``a[index_array]`` yields a sorted `a`.
  4653. See Also
  4654. --------
  4655. ma.MaskedArray.sort : Describes sorting algorithms used.
  4656. lexsort : Indirect stable sort with multiple keys.
  4657. numpy.ndarray.sort : Inplace sort.
  4658. Notes
  4659. -----
  4660. See `sort` for notes on the different sorting algorithms.
  4661. Examples
  4662. --------
  4663. >>> a = np.ma.array([3,2,1], mask=[False, False, True])
  4664. >>> a
  4665. masked_array(data=[3, 2, --],
  4666. mask=[False, False, True],
  4667. fill_value=999999)
  4668. >>> a.argsort()
  4669. array([1, 0, 2])
  4670. """
  4671. # 2017-04-11, Numpy 1.13.0, gh-8701: warn on axis default
  4672. if axis is np._NoValue:
  4673. axis = _deprecate_argsort_axis(self)
  4674. if fill_value is None:
  4675. if endwith:
  4676. # nan > inf
  4677. if np.issubdtype(self.dtype, np.floating):
  4678. fill_value = np.nan
  4679. else:
  4680. fill_value = minimum_fill_value(self)
  4681. else:
  4682. fill_value = maximum_fill_value(self)
  4683. filled = self.filled(fill_value)
  4684. return filled.argsort(axis=axis, kind=kind, order=order)
  4685. def argmin(self, axis=None, fill_value=None, out=None, *,
  4686. keepdims=np._NoValue):
  4687. """
  4688. Return array of indices to the minimum values along the given axis.
  4689. Parameters
  4690. ----------
  4691. axis : {None, integer}
  4692. If None, the index is into the flattened array, otherwise along
  4693. the specified axis
  4694. fill_value : scalar or None, optional
  4695. Value used to fill in the masked values. If None, the output of
  4696. minimum_fill_value(self._data) is used instead.
  4697. out : {None, array}, optional
  4698. Array into which the result can be placed. Its type is preserved
  4699. and it must be of the right shape to hold the output.
  4700. Returns
  4701. -------
  4702. ndarray or scalar
  4703. If multi-dimension input, returns a new ndarray of indices to the
  4704. minimum values along the given axis. Otherwise, returns a scalar
  4705. of index to the minimum values along the given axis.
  4706. Examples
  4707. --------
  4708. >>> x = np.ma.array(np.arange(4), mask=[1,1,0,0])
  4709. >>> x.shape = (2,2)
  4710. >>> x
  4711. masked_array(
  4712. data=[[--, --],
  4713. [2, 3]],
  4714. mask=[[ True, True],
  4715. [False, False]],
  4716. fill_value=999999)
  4717. >>> x.argmin(axis=0, fill_value=-1)
  4718. array([0, 0])
  4719. >>> x.argmin(axis=0, fill_value=9)
  4720. array([1, 1])
  4721. """
  4722. if fill_value is None:
  4723. fill_value = minimum_fill_value(self)
  4724. d = self.filled(fill_value).view(ndarray)
  4725. keepdims = False if keepdims is np._NoValue else bool(keepdims)
  4726. return d.argmin(axis, out=out, keepdims=keepdims)
  4727. def argmax(self, axis=None, fill_value=None, out=None, *,
  4728. keepdims=np._NoValue):
  4729. """
  4730. Returns array of indices of the maximum values along the given axis.
  4731. Masked values are treated as if they had the value fill_value.
  4732. Parameters
  4733. ----------
  4734. axis : {None, integer}
  4735. If None, the index is into the flattened array, otherwise along
  4736. the specified axis
  4737. fill_value : scalar or None, optional
  4738. Value used to fill in the masked values. If None, the output of
  4739. maximum_fill_value(self._data) is used instead.
  4740. out : {None, array}, optional
  4741. Array into which the result can be placed. Its type is preserved
  4742. and it must be of the right shape to hold the output.
  4743. Returns
  4744. -------
  4745. index_array : {integer_array}
  4746. Examples
  4747. --------
  4748. >>> a = np.arange(6).reshape(2,3)
  4749. >>> a.argmax()
  4750. 5
  4751. >>> a.argmax(0)
  4752. array([1, 1, 1])
  4753. >>> a.argmax(1)
  4754. array([2, 2])
  4755. """
  4756. if fill_value is None:
  4757. fill_value = maximum_fill_value(self._data)
  4758. d = self.filled(fill_value).view(ndarray)
  4759. keepdims = False if keepdims is np._NoValue else bool(keepdims)
  4760. return d.argmax(axis, out=out, keepdims=keepdims)
  4761. def sort(self, axis=-1, kind=None, order=None,
  4762. endwith=True, fill_value=None):
  4763. """
  4764. Sort the array, in-place
  4765. Parameters
  4766. ----------
  4767. a : array_like
  4768. Array to be sorted.
  4769. axis : int, optional
  4770. Axis along which to sort. If None, the array is flattened before
  4771. sorting. The default is -1, which sorts along the last axis.
  4772. kind : {'quicksort', 'mergesort', 'heapsort', 'stable'}, optional
  4773. The sorting algorithm used.
  4774. order : list, optional
  4775. When `a` is a structured array, this argument specifies which fields
  4776. to compare first, second, and so on. This list does not need to
  4777. include all of the fields.
  4778. endwith : {True, False}, optional
  4779. Whether missing values (if any) should be treated as the largest values
  4780. (True) or the smallest values (False)
  4781. When the array contains unmasked values sorting at the same extremes of the
  4782. datatype, the ordering of these values and the masked values is
  4783. undefined.
  4784. fill_value : scalar or None, optional
  4785. Value used internally for the masked values.
  4786. If ``fill_value`` is not None, it supersedes ``endwith``.
  4787. Returns
  4788. -------
  4789. sorted_array : ndarray
  4790. Array of the same type and shape as `a`.
  4791. See Also
  4792. --------
  4793. numpy.ndarray.sort : Method to sort an array in-place.
  4794. argsort : Indirect sort.
  4795. lexsort : Indirect stable sort on multiple keys.
  4796. searchsorted : Find elements in a sorted array.
  4797. Notes
  4798. -----
  4799. See ``sort`` for notes on the different sorting algorithms.
  4800. Examples
  4801. --------
  4802. >>> a = np.ma.array([1, 2, 5, 4, 3],mask=[0, 1, 0, 1, 0])
  4803. >>> # Default
  4804. >>> a.sort()
  4805. >>> a
  4806. masked_array(data=[1, 3, 5, --, --],
  4807. mask=[False, False, False, True, True],
  4808. fill_value=999999)
  4809. >>> a = np.ma.array([1, 2, 5, 4, 3],mask=[0, 1, 0, 1, 0])
  4810. >>> # Put missing values in the front
  4811. >>> a.sort(endwith=False)
  4812. >>> a
  4813. masked_array(data=[--, --, 1, 3, 5],
  4814. mask=[ True, True, False, False, False],
  4815. fill_value=999999)
  4816. >>> a = np.ma.array([1, 2, 5, 4, 3],mask=[0, 1, 0, 1, 0])
  4817. >>> # fill_value takes over endwith
  4818. >>> a.sort(endwith=False, fill_value=3)
  4819. >>> a
  4820. masked_array(data=[1, --, --, 3, 5],
  4821. mask=[False, True, True, False, False],
  4822. fill_value=999999)
  4823. """
  4824. if self._mask is nomask:
  4825. ndarray.sort(self, axis=axis, kind=kind, order=order)
  4826. return
  4827. if self is masked:
  4828. return
  4829. sidx = self.argsort(axis=axis, kind=kind, order=order,
  4830. fill_value=fill_value, endwith=endwith)
  4831. self[...] = np.take_along_axis(self, sidx, axis=axis)
  4832. def min(self, axis=None, out=None, fill_value=None, keepdims=np._NoValue):
  4833. """
  4834. Return the minimum along a given axis.
  4835. Parameters
  4836. ----------
  4837. axis : None or int or tuple of ints, optional
  4838. Axis along which to operate. By default, ``axis`` is None and the
  4839. flattened input is used.
  4840. .. versionadded:: 1.7.0
  4841. If this is a tuple of ints, the minimum is selected over multiple
  4842. axes, instead of a single axis or all the axes as before.
  4843. out : array_like, optional
  4844. Alternative output array in which to place the result. Must be of
  4845. the same shape and buffer length as the expected output.
  4846. fill_value : scalar or None, optional
  4847. Value used to fill in the masked values.
  4848. If None, use the output of `minimum_fill_value`.
  4849. keepdims : bool, optional
  4850. If this is set to True, the axes which are reduced are left
  4851. in the result as dimensions with size one. With this option,
  4852. the result will broadcast correctly against the array.
  4853. Returns
  4854. -------
  4855. amin : array_like
  4856. New array holding the result.
  4857. If ``out`` was specified, ``out`` is returned.
  4858. See Also
  4859. --------
  4860. ma.minimum_fill_value
  4861. Returns the minimum filling value for a given datatype.
  4862. Examples
  4863. --------
  4864. >>> import numpy.ma as ma
  4865. >>> x = [[1., -2., 3.], [0.2, -0.7, 0.1]]
  4866. >>> mask = [[1, 1, 0], [0, 0, 1]]
  4867. >>> masked_x = ma.masked_array(x, mask)
  4868. >>> masked_x
  4869. masked_array(
  4870. data=[[--, --, 3.0],
  4871. [0.2, -0.7, --]],
  4872. mask=[[ True, True, False],
  4873. [False, False, True]],
  4874. fill_value=1e+20)
  4875. >>> ma.min(masked_x)
  4876. -0.7
  4877. >>> ma.min(masked_x, axis=-1)
  4878. masked_array(data=[3.0, -0.7],
  4879. mask=[False, False],
  4880. fill_value=1e+20)
  4881. >>> ma.min(masked_x, axis=0, keepdims=True)
  4882. masked_array(data=[[0.2, -0.7, 3.0]],
  4883. mask=[[False, False, False]],
  4884. fill_value=1e+20)
  4885. >>> mask = [[1, 1, 1,], [1, 1, 1]]
  4886. >>> masked_x = ma.masked_array(x, mask)
  4887. >>> ma.min(masked_x, axis=0)
  4888. masked_array(data=[--, --, --],
  4889. mask=[ True, True, True],
  4890. fill_value=1e+20,
  4891. dtype=float64)
  4892. """
  4893. kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims}
  4894. _mask = self._mask
  4895. newmask = _check_mask_axis(_mask, axis, **kwargs)
  4896. if fill_value is None:
  4897. fill_value = minimum_fill_value(self)
  4898. # No explicit output
  4899. if out is None:
  4900. result = self.filled(fill_value).min(
  4901. axis=axis, out=out, **kwargs).view(type(self))
  4902. if result.ndim:
  4903. # Set the mask
  4904. result.__setmask__(newmask)
  4905. # Get rid of Infs
  4906. if newmask.ndim:
  4907. np.copyto(result, result.fill_value, where=newmask)
  4908. elif newmask:
  4909. result = masked
  4910. return result
  4911. # Explicit output
  4912. result = self.filled(fill_value).min(axis=axis, out=out, **kwargs)
  4913. if isinstance(out, MaskedArray):
  4914. outmask = getmask(out)
  4915. if outmask is nomask:
  4916. outmask = out._mask = make_mask_none(out.shape)
  4917. outmask.flat = newmask
  4918. else:
  4919. if out.dtype.kind in 'biu':
  4920. errmsg = "Masked data information would be lost in one or more"\
  4921. " location."
  4922. raise MaskError(errmsg)
  4923. np.copyto(out, np.nan, where=newmask)
  4924. return out
  4925. def max(self, axis=None, out=None, fill_value=None, keepdims=np._NoValue):
  4926. """
  4927. Return the maximum along a given axis.
  4928. Parameters
  4929. ----------
  4930. axis : None or int or tuple of ints, optional
  4931. Axis along which to operate. By default, ``axis`` is None and the
  4932. flattened input is used.
  4933. .. versionadded:: 1.7.0
  4934. If this is a tuple of ints, the maximum is selected over multiple
  4935. axes, instead of a single axis or all the axes as before.
  4936. out : array_like, optional
  4937. Alternative output array in which to place the result. Must
  4938. be of the same shape and buffer length as the expected output.
  4939. fill_value : scalar or None, optional
  4940. Value used to fill in the masked values.
  4941. If None, use the output of maximum_fill_value().
  4942. keepdims : bool, optional
  4943. If this is set to True, the axes which are reduced are left
  4944. in the result as dimensions with size one. With this option,
  4945. the result will broadcast correctly against the array.
  4946. Returns
  4947. -------
  4948. amax : array_like
  4949. New array holding the result.
  4950. If ``out`` was specified, ``out`` is returned.
  4951. See Also
  4952. --------
  4953. ma.maximum_fill_value
  4954. Returns the maximum filling value for a given datatype.
  4955. Examples
  4956. --------
  4957. >>> import numpy.ma as ma
  4958. >>> x = [[-1., 2.5], [4., -2.], [3., 0.]]
  4959. >>> mask = [[0, 0], [1, 0], [1, 0]]
  4960. >>> masked_x = ma.masked_array(x, mask)
  4961. >>> masked_x
  4962. masked_array(
  4963. data=[[-1.0, 2.5],
  4964. [--, -2.0],
  4965. [--, 0.0]],
  4966. mask=[[False, False],
  4967. [ True, False],
  4968. [ True, False]],
  4969. fill_value=1e+20)
  4970. >>> ma.max(masked_x)
  4971. 2.5
  4972. >>> ma.max(masked_x, axis=0)
  4973. masked_array(data=[-1.0, 2.5],
  4974. mask=[False, False],
  4975. fill_value=1e+20)
  4976. >>> ma.max(masked_x, axis=1, keepdims=True)
  4977. masked_array(
  4978. data=[[2.5],
  4979. [-2.0],
  4980. [0.0]],
  4981. mask=[[False],
  4982. [False],
  4983. [False]],
  4984. fill_value=1e+20)
  4985. >>> mask = [[1, 1], [1, 1], [1, 1]]
  4986. >>> masked_x = ma.masked_array(x, mask)
  4987. >>> ma.max(masked_x, axis=1)
  4988. masked_array(data=[--, --, --],
  4989. mask=[ True, True, True],
  4990. fill_value=1e+20,
  4991. dtype=float64)
  4992. """
  4993. kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims}
  4994. _mask = self._mask
  4995. newmask = _check_mask_axis(_mask, axis, **kwargs)
  4996. if fill_value is None:
  4997. fill_value = maximum_fill_value(self)
  4998. # No explicit output
  4999. if out is None:
  5000. result = self.filled(fill_value).max(
  5001. axis=axis, out=out, **kwargs).view(type(self))
  5002. if result.ndim:
  5003. # Set the mask
  5004. result.__setmask__(newmask)
  5005. # Get rid of Infs
  5006. if newmask.ndim:
  5007. np.copyto(result, result.fill_value, where=newmask)
  5008. elif newmask:
  5009. result = masked
  5010. return result
  5011. # Explicit output
  5012. result = self.filled(fill_value).max(axis=axis, out=out, **kwargs)
  5013. if isinstance(out, MaskedArray):
  5014. outmask = getmask(out)
  5015. if outmask is nomask:
  5016. outmask = out._mask = make_mask_none(out.shape)
  5017. outmask.flat = newmask
  5018. else:
  5019. if out.dtype.kind in 'biu':
  5020. errmsg = "Masked data information would be lost in one or more"\
  5021. " location."
  5022. raise MaskError(errmsg)
  5023. np.copyto(out, np.nan, where=newmask)
  5024. return out
  5025. def ptp(self, axis=None, out=None, fill_value=None, keepdims=False):
  5026. """
  5027. Return (maximum - minimum) along the given dimension
  5028. (i.e. peak-to-peak value).
  5029. .. warning::
  5030. `ptp` preserves the data type of the array. This means the
  5031. return value for an input of signed integers with n bits
  5032. (e.g. `np.int8`, `np.int16`, etc) is also a signed integer
  5033. with n bits. In that case, peak-to-peak values greater than
  5034. ``2**(n-1)-1`` will be returned as negative values. An example
  5035. with a work-around is shown below.
  5036. Parameters
  5037. ----------
  5038. axis : {None, int}, optional
  5039. Axis along which to find the peaks. If None (default) the
  5040. flattened array is used.
  5041. out : {None, array_like}, optional
  5042. Alternative output array in which to place the result. It must
  5043. have the same shape and buffer length as the expected output
  5044. but the type will be cast if necessary.
  5045. fill_value : scalar or None, optional
  5046. Value used to fill in the masked values.
  5047. keepdims : bool, optional
  5048. If this is set to True, the axes which are reduced are left
  5049. in the result as dimensions with size one. With this option,
  5050. the result will broadcast correctly against the array.
  5051. Returns
  5052. -------
  5053. ptp : ndarray.
  5054. A new array holding the result, unless ``out`` was
  5055. specified, in which case a reference to ``out`` is returned.
  5056. Examples
  5057. --------
  5058. >>> x = np.ma.MaskedArray([[4, 9, 2, 10],
  5059. ... [6, 9, 7, 12]])
  5060. >>> x.ptp(axis=1)
  5061. masked_array(data=[8, 6],
  5062. mask=False,
  5063. fill_value=999999)
  5064. >>> x.ptp(axis=0)
  5065. masked_array(data=[2, 0, 5, 2],
  5066. mask=False,
  5067. fill_value=999999)
  5068. >>> x.ptp()
  5069. 10
  5070. This example shows that a negative value can be returned when
  5071. the input is an array of signed integers.
  5072. >>> y = np.ma.MaskedArray([[1, 127],
  5073. ... [0, 127],
  5074. ... [-1, 127],
  5075. ... [-2, 127]], dtype=np.int8)
  5076. >>> y.ptp(axis=1)
  5077. masked_array(data=[ 126, 127, -128, -127],
  5078. mask=False,
  5079. fill_value=999999,
  5080. dtype=int8)
  5081. A work-around is to use the `view()` method to view the result as
  5082. unsigned integers with the same bit width:
  5083. >>> y.ptp(axis=1).view(np.uint8)
  5084. masked_array(data=[126, 127, 128, 129],
  5085. mask=False,
  5086. fill_value=999999,
  5087. dtype=uint8)
  5088. """
  5089. if out is None:
  5090. result = self.max(axis=axis, fill_value=fill_value,
  5091. keepdims=keepdims)
  5092. result -= self.min(axis=axis, fill_value=fill_value,
  5093. keepdims=keepdims)
  5094. return result
  5095. out.flat = self.max(axis=axis, out=out, fill_value=fill_value,
  5096. keepdims=keepdims)
  5097. min_value = self.min(axis=axis, fill_value=fill_value,
  5098. keepdims=keepdims)
  5099. np.subtract(out, min_value, out=out, casting='unsafe')
  5100. return out
  5101. def partition(self, *args, **kwargs):
  5102. warnings.warn("Warning: 'partition' will ignore the 'mask' "
  5103. f"of the {self.__class__.__name__}.",
  5104. stacklevel=2)
  5105. return super().partition(*args, **kwargs)
  5106. def argpartition(self, *args, **kwargs):
  5107. warnings.warn("Warning: 'argpartition' will ignore the 'mask' "
  5108. f"of the {self.__class__.__name__}.",
  5109. stacklevel=2)
  5110. return super().argpartition(*args, **kwargs)
  5111. def take(self, indices, axis=None, out=None, mode='raise'):
  5112. """
  5113. """
  5114. (_data, _mask) = (self._data, self._mask)
  5115. cls = type(self)
  5116. # Make sure the indices are not masked
  5117. maskindices = getmask(indices)
  5118. if maskindices is not nomask:
  5119. indices = indices.filled(0)
  5120. # Get the data, promoting scalars to 0d arrays with [...] so that
  5121. # .view works correctly
  5122. if out is None:
  5123. out = _data.take(indices, axis=axis, mode=mode)[...].view(cls)
  5124. else:
  5125. np.take(_data, indices, axis=axis, mode=mode, out=out)
  5126. # Get the mask
  5127. if isinstance(out, MaskedArray):
  5128. if _mask is nomask:
  5129. outmask = maskindices
  5130. else:
  5131. outmask = _mask.take(indices, axis=axis, mode=mode)
  5132. outmask |= maskindices
  5133. out.__setmask__(outmask)
  5134. # demote 0d arrays back to scalars, for consistency with ndarray.take
  5135. return out[()]
  5136. # Array methods
  5137. copy = _arraymethod('copy')
  5138. diagonal = _arraymethod('diagonal')
  5139. flatten = _arraymethod('flatten')
  5140. repeat = _arraymethod('repeat')
  5141. squeeze = _arraymethod('squeeze')
  5142. swapaxes = _arraymethod('swapaxes')
  5143. T = property(fget=lambda self: self.transpose())
  5144. transpose = _arraymethod('transpose')
  5145. def tolist(self, fill_value=None):
  5146. """
  5147. Return the data portion of the masked array as a hierarchical Python list.
  5148. Data items are converted to the nearest compatible Python type.
  5149. Masked values are converted to `fill_value`. If `fill_value` is None,
  5150. the corresponding entries in the output list will be ``None``.
  5151. Parameters
  5152. ----------
  5153. fill_value : scalar, optional
  5154. The value to use for invalid entries. Default is None.
  5155. Returns
  5156. -------
  5157. result : list
  5158. The Python list representation of the masked array.
  5159. Examples
  5160. --------
  5161. >>> x = np.ma.array([[1,2,3], [4,5,6], [7,8,9]], mask=[0] + [1,0]*4)
  5162. >>> x.tolist()
  5163. [[1, None, 3], [None, 5, None], [7, None, 9]]
  5164. >>> x.tolist(-999)
  5165. [[1, -999, 3], [-999, 5, -999], [7, -999, 9]]
  5166. """
  5167. _mask = self._mask
  5168. # No mask ? Just return .data.tolist ?
  5169. if _mask is nomask:
  5170. return self._data.tolist()
  5171. # Explicit fill_value: fill the array and get the list
  5172. if fill_value is not None:
  5173. return self.filled(fill_value).tolist()
  5174. # Structured array.
  5175. names = self.dtype.names
  5176. if names:
  5177. result = self._data.astype([(_, object) for _ in names])
  5178. for n in names:
  5179. result[n][_mask[n]] = None
  5180. return result.tolist()
  5181. # Standard arrays.
  5182. if _mask is nomask:
  5183. return [None]
  5184. # Set temps to save time when dealing w/ marrays.
  5185. inishape = self.shape
  5186. result = np.array(self._data.ravel(), dtype=object)
  5187. result[_mask.ravel()] = None
  5188. result.shape = inishape
  5189. return result.tolist()
  5190. def tostring(self, fill_value=None, order='C'):
  5191. r"""
  5192. A compatibility alias for `tobytes`, with exactly the same behavior.
  5193. Despite its name, it returns `bytes` not `str`\ s.
  5194. .. deprecated:: 1.19.0
  5195. """
  5196. # 2020-03-30, Numpy 1.19.0
  5197. warnings.warn(
  5198. "tostring() is deprecated. Use tobytes() instead.",
  5199. DeprecationWarning, stacklevel=2)
  5200. return self.tobytes(fill_value, order=order)
  5201. def tobytes(self, fill_value=None, order='C'):
  5202. """
  5203. Return the array data as a string containing the raw bytes in the array.
  5204. The array is filled with a fill value before the string conversion.
  5205. .. versionadded:: 1.9.0
  5206. Parameters
  5207. ----------
  5208. fill_value : scalar, optional
  5209. Value used to fill in the masked values. Default is None, in which
  5210. case `MaskedArray.fill_value` is used.
  5211. order : {'C','F','A'}, optional
  5212. Order of the data item in the copy. Default is 'C'.
  5213. - 'C' -- C order (row major).
  5214. - 'F' -- Fortran order (column major).
  5215. - 'A' -- Any, current order of array.
  5216. - None -- Same as 'A'.
  5217. See Also
  5218. --------
  5219. numpy.ndarray.tobytes
  5220. tolist, tofile
  5221. Notes
  5222. -----
  5223. As for `ndarray.tobytes`, information about the shape, dtype, etc.,
  5224. but also about `fill_value`, will be lost.
  5225. Examples
  5226. --------
  5227. >>> x = np.ma.array(np.array([[1, 2], [3, 4]]), mask=[[0, 1], [1, 0]])
  5228. >>> x.tobytes()
  5229. b'\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x00?B\\x0f\\x00\\x00\\x00\\x00\\x00?B\\x0f\\x00\\x00\\x00\\x00\\x00\\x04\\x00\\x00\\x00\\x00\\x00\\x00\\x00'
  5230. """
  5231. return self.filled(fill_value).tobytes(order=order)
  5232. def tofile(self, fid, sep="", format="%s"):
  5233. """
  5234. Save a masked array to a file in binary format.
  5235. .. warning::
  5236. This function is not implemented yet.
  5237. Raises
  5238. ------
  5239. NotImplementedError
  5240. When `tofile` is called.
  5241. """
  5242. raise NotImplementedError("MaskedArray.tofile() not implemented yet.")
  5243. def toflex(self):
  5244. """
  5245. Transforms a masked array into a flexible-type array.
  5246. The flexible type array that is returned will have two fields:
  5247. * the ``_data`` field stores the ``_data`` part of the array.
  5248. * the ``_mask`` field stores the ``_mask`` part of the array.
  5249. Parameters
  5250. ----------
  5251. None
  5252. Returns
  5253. -------
  5254. record : ndarray
  5255. A new flexible-type `ndarray` with two fields: the first element
  5256. containing a value, the second element containing the corresponding
  5257. mask boolean. The returned record shape matches self.shape.
  5258. Notes
  5259. -----
  5260. A side-effect of transforming a masked array into a flexible `ndarray` is
  5261. that meta information (``fill_value``, ...) will be lost.
  5262. Examples
  5263. --------
  5264. >>> x = np.ma.array([[1,2,3],[4,5,6],[7,8,9]], mask=[0] + [1,0]*4)
  5265. >>> x
  5266. masked_array(
  5267. data=[[1, --, 3],
  5268. [--, 5, --],
  5269. [7, --, 9]],
  5270. mask=[[False, True, False],
  5271. [ True, False, True],
  5272. [False, True, False]],
  5273. fill_value=999999)
  5274. >>> x.toflex()
  5275. array([[(1, False), (2, True), (3, False)],
  5276. [(4, True), (5, False), (6, True)],
  5277. [(7, False), (8, True), (9, False)]],
  5278. dtype=[('_data', '<i8'), ('_mask', '?')])
  5279. """
  5280. # Get the basic dtype.
  5281. ddtype = self.dtype
  5282. # Make sure we have a mask
  5283. _mask = self._mask
  5284. if _mask is None:
  5285. _mask = make_mask_none(self.shape, ddtype)
  5286. # And get its dtype
  5287. mdtype = self._mask.dtype
  5288. record = np.ndarray(shape=self.shape,
  5289. dtype=[('_data', ddtype), ('_mask', mdtype)])
  5290. record['_data'] = self._data
  5291. record['_mask'] = self._mask
  5292. return record
  5293. torecords = toflex
  5294. # Pickling
  5295. def __getstate__(self):
  5296. """Return the internal state of the masked array, for pickling
  5297. purposes.
  5298. """
  5299. cf = 'CF'[self.flags.fnc]
  5300. data_state = super().__reduce__()[2]
  5301. return data_state + (getmaskarray(self).tobytes(cf), self._fill_value)
  5302. def __setstate__(self, state):
  5303. """Restore the internal state of the masked array, for
  5304. pickling purposes. ``state`` is typically the output of the
  5305. ``__getstate__`` output, and is a 5-tuple:
  5306. - class name
  5307. - a tuple giving the shape of the data
  5308. - a typecode for the data
  5309. - a binary string for the data
  5310. - a binary string for the mask.
  5311. """
  5312. (_, shp, typ, isf, raw, msk, flv) = state
  5313. super().__setstate__((shp, typ, isf, raw))
  5314. self._mask.__setstate__((shp, make_mask_descr(typ), isf, msk))
  5315. self.fill_value = flv
  5316. def __reduce__(self):
  5317. """Return a 3-tuple for pickling a MaskedArray.
  5318. """
  5319. return (_mareconstruct,
  5320. (self.__class__, self._baseclass, (0,), 'b',),
  5321. self.__getstate__())
  5322. def __deepcopy__(self, memo=None):
  5323. from copy import deepcopy
  5324. copied = MaskedArray.__new__(type(self), self, copy=True)
  5325. if memo is None:
  5326. memo = {}
  5327. memo[id(self)] = copied
  5328. for (k, v) in self.__dict__.items():
  5329. copied.__dict__[k] = deepcopy(v, memo)
  5330. # as clearly documented for np.copy(), you need to use
  5331. # deepcopy() directly for arrays of object type that may
  5332. # contain compound types--you cannot depend on normal
  5333. # copy semantics to do the right thing here
  5334. if self.dtype.hasobject:
  5335. copied._data[...] = deepcopy(copied._data)
  5336. return copied
  5337. def _mareconstruct(subtype, baseclass, baseshape, basetype,):
  5338. """Internal function that builds a new MaskedArray from the
  5339. information stored in a pickle.
  5340. """
  5341. _data = ndarray.__new__(baseclass, baseshape, basetype)
  5342. _mask = ndarray.__new__(ndarray, baseshape, make_mask_descr(basetype))
  5343. return subtype.__new__(subtype, _data, mask=_mask, dtype=basetype,)
  5344. class mvoid(MaskedArray):
  5345. """
  5346. Fake a 'void' object to use for masked array with structured dtypes.
  5347. """
  5348. def __new__(self, data, mask=nomask, dtype=None, fill_value=None,
  5349. hardmask=False, copy=False, subok=True):
  5350. _data = np.array(data, copy=copy, subok=subok, dtype=dtype)
  5351. _data = _data.view(self)
  5352. _data._hardmask = hardmask
  5353. if mask is not nomask:
  5354. if isinstance(mask, np.void):
  5355. _data._mask = mask
  5356. else:
  5357. try:
  5358. # Mask is already a 0D array
  5359. _data._mask = np.void(mask)
  5360. except TypeError:
  5361. # Transform the mask to a void
  5362. mdtype = make_mask_descr(dtype)
  5363. _data._mask = np.array(mask, dtype=mdtype)[()]
  5364. if fill_value is not None:
  5365. _data.fill_value = fill_value
  5366. return _data
  5367. @property
  5368. def _data(self):
  5369. # Make sure that the _data part is a np.void
  5370. return super()._data[()]
  5371. def __getitem__(self, indx):
  5372. """
  5373. Get the index.
  5374. """
  5375. m = self._mask
  5376. if isinstance(m[indx], ndarray):
  5377. # Can happen when indx is a multi-dimensional field:
  5378. # A = ma.masked_array(data=[([0,1],)], mask=[([True,
  5379. # False],)], dtype=[("A", ">i2", (2,))])
  5380. # x = A[0]; y = x["A"]; then y.mask["A"].size==2
  5381. # and we can not say masked/unmasked.
  5382. # The result is no longer mvoid!
  5383. # See also issue #6724.
  5384. return masked_array(
  5385. data=self._data[indx], mask=m[indx],
  5386. fill_value=self._fill_value[indx],
  5387. hard_mask=self._hardmask)
  5388. if m is not nomask and m[indx]:
  5389. return masked
  5390. return self._data[indx]
  5391. def __setitem__(self, indx, value):
  5392. self._data[indx] = value
  5393. if self._hardmask:
  5394. self._mask[indx] |= getattr(value, "_mask", False)
  5395. else:
  5396. self._mask[indx] = getattr(value, "_mask", False)
  5397. def __str__(self):
  5398. m = self._mask
  5399. if m is nomask:
  5400. return str(self._data)
  5401. rdtype = _replace_dtype_fields(self._data.dtype, "O")
  5402. data_arr = super()._data
  5403. res = data_arr.astype(rdtype)
  5404. _recursive_printoption(res, self._mask, masked_print_option)
  5405. return str(res)
  5406. __repr__ = __str__
  5407. def __iter__(self):
  5408. "Defines an iterator for mvoid"
  5409. (_data, _mask) = (self._data, self._mask)
  5410. if _mask is nomask:
  5411. yield from _data
  5412. else:
  5413. for (d, m) in zip(_data, _mask):
  5414. if m:
  5415. yield masked
  5416. else:
  5417. yield d
  5418. def __len__(self):
  5419. return self._data.__len__()
  5420. def filled(self, fill_value=None):
  5421. """
  5422. Return a copy with masked fields filled with a given value.
  5423. Parameters
  5424. ----------
  5425. fill_value : array_like, optional
  5426. The value to use for invalid entries. Can be scalar or
  5427. non-scalar. If latter is the case, the filled array should
  5428. be broadcastable over input array. Default is None, in
  5429. which case the `fill_value` attribute is used instead.
  5430. Returns
  5431. -------
  5432. filled_void
  5433. A `np.void` object
  5434. See Also
  5435. --------
  5436. MaskedArray.filled
  5437. """
  5438. return asarray(self).filled(fill_value)[()]
  5439. def tolist(self):
  5440. """
  5441. Transforms the mvoid object into a tuple.
  5442. Masked fields are replaced by None.
  5443. Returns
  5444. -------
  5445. returned_tuple
  5446. Tuple of fields
  5447. """
  5448. _mask = self._mask
  5449. if _mask is nomask:
  5450. return self._data.tolist()
  5451. result = []
  5452. for (d, m) in zip(self._data, self._mask):
  5453. if m:
  5454. result.append(None)
  5455. else:
  5456. # .item() makes sure we return a standard Python object
  5457. result.append(d.item())
  5458. return tuple(result)
  5459. ##############################################################################
  5460. # Shortcuts #
  5461. ##############################################################################
  5462. def isMaskedArray(x):
  5463. """
  5464. Test whether input is an instance of MaskedArray.
  5465. This function returns True if `x` is an instance of MaskedArray
  5466. and returns False otherwise. Any object is accepted as input.
  5467. Parameters
  5468. ----------
  5469. x : object
  5470. Object to test.
  5471. Returns
  5472. -------
  5473. result : bool
  5474. True if `x` is a MaskedArray.
  5475. See Also
  5476. --------
  5477. isMA : Alias to isMaskedArray.
  5478. isarray : Alias to isMaskedArray.
  5479. Examples
  5480. --------
  5481. >>> import numpy.ma as ma
  5482. >>> a = np.eye(3, 3)
  5483. >>> a
  5484. array([[ 1., 0., 0.],
  5485. [ 0., 1., 0.],
  5486. [ 0., 0., 1.]])
  5487. >>> m = ma.masked_values(a, 0)
  5488. >>> m
  5489. masked_array(
  5490. data=[[1.0, --, --],
  5491. [--, 1.0, --],
  5492. [--, --, 1.0]],
  5493. mask=[[False, True, True],
  5494. [ True, False, True],
  5495. [ True, True, False]],
  5496. fill_value=0.0)
  5497. >>> ma.isMaskedArray(a)
  5498. False
  5499. >>> ma.isMaskedArray(m)
  5500. True
  5501. >>> ma.isMaskedArray([0, 1, 2])
  5502. False
  5503. """
  5504. return isinstance(x, MaskedArray)
  5505. isarray = isMaskedArray
  5506. isMA = isMaskedArray # backward compatibility
  5507. class MaskedConstant(MaskedArray):
  5508. # the lone np.ma.masked instance
  5509. __singleton = None
  5510. @classmethod
  5511. def __has_singleton(cls):
  5512. # second case ensures `cls.__singleton` is not just a view on the
  5513. # superclass singleton
  5514. return cls.__singleton is not None and type(cls.__singleton) is cls
  5515. def __new__(cls):
  5516. if not cls.__has_singleton():
  5517. # We define the masked singleton as a float for higher precedence.
  5518. # Note that it can be tricky sometimes w/ type comparison
  5519. data = np.array(0.)
  5520. mask = np.array(True)
  5521. # prevent any modifications
  5522. data.flags.writeable = False
  5523. mask.flags.writeable = False
  5524. # don't fall back on MaskedArray.__new__(MaskedConstant), since
  5525. # that might confuse it - this way, the construction is entirely
  5526. # within our control
  5527. cls.__singleton = MaskedArray(data, mask=mask).view(cls)
  5528. return cls.__singleton
  5529. def __array_finalize__(self, obj):
  5530. if not self.__has_singleton():
  5531. # this handles the `.view` in __new__, which we want to copy across
  5532. # properties normally
  5533. return super().__array_finalize__(obj)
  5534. elif self is self.__singleton:
  5535. # not clear how this can happen, play it safe
  5536. pass
  5537. else:
  5538. # everywhere else, we want to downcast to MaskedArray, to prevent a
  5539. # duplicate maskedconstant.
  5540. self.__class__ = MaskedArray
  5541. MaskedArray.__array_finalize__(self, obj)
  5542. def __array_prepare__(self, obj, context=None):
  5543. return self.view(MaskedArray).__array_prepare__(obj, context)
  5544. def __array_wrap__(self, obj, context=None):
  5545. return self.view(MaskedArray).__array_wrap__(obj, context)
  5546. def __str__(self):
  5547. return str(masked_print_option._display)
  5548. def __repr__(self):
  5549. if self is MaskedConstant.__singleton:
  5550. return 'masked'
  5551. else:
  5552. # it's a subclass, or something is wrong, make it obvious
  5553. return object.__repr__(self)
  5554. def __format__(self, format_spec):
  5555. # Replace ndarray.__format__ with the default, which supports no format characters.
  5556. # Supporting format characters is unwise here, because we do not know what type
  5557. # the user was expecting - better to not guess.
  5558. try:
  5559. return object.__format__(self, format_spec)
  5560. except TypeError:
  5561. # 2020-03-23, NumPy 1.19.0
  5562. warnings.warn(
  5563. "Format strings passed to MaskedConstant are ignored, but in future may "
  5564. "error or produce different behavior",
  5565. FutureWarning, stacklevel=2
  5566. )
  5567. return object.__format__(self, "")
  5568. def __reduce__(self):
  5569. """Override of MaskedArray's __reduce__.
  5570. """
  5571. return (self.__class__, ())
  5572. # inplace operations have no effect. We have to override them to avoid
  5573. # trying to modify the readonly data and mask arrays
  5574. def __iop__(self, other):
  5575. return self
  5576. __iadd__ = \
  5577. __isub__ = \
  5578. __imul__ = \
  5579. __ifloordiv__ = \
  5580. __itruediv__ = \
  5581. __ipow__ = \
  5582. __iop__
  5583. del __iop__ # don't leave this around
  5584. def copy(self, *args, **kwargs):
  5585. """ Copy is a no-op on the maskedconstant, as it is a scalar """
  5586. # maskedconstant is a scalar, so copy doesn't need to copy. There's
  5587. # precedent for this with `np.bool_` scalars.
  5588. return self
  5589. def __copy__(self):
  5590. return self
  5591. def __deepcopy__(self, memo):
  5592. return self
  5593. def __setattr__(self, attr, value):
  5594. if not self.__has_singleton():
  5595. # allow the singleton to be initialized
  5596. return super().__setattr__(attr, value)
  5597. elif self is self.__singleton:
  5598. raise AttributeError(
  5599. f"attributes of {self!r} are not writeable")
  5600. else:
  5601. # duplicate instance - we can end up here from __array_finalize__,
  5602. # where we set the __class__ attribute
  5603. return super().__setattr__(attr, value)
  5604. masked = masked_singleton = MaskedConstant()
  5605. masked_array = MaskedArray
  5606. def array(data, dtype=None, copy=False, order=None,
  5607. mask=nomask, fill_value=None, keep_mask=True,
  5608. hard_mask=False, shrink=True, subok=True, ndmin=0):
  5609. """
  5610. Shortcut to MaskedArray.
  5611. The options are in a different order for convenience and backwards
  5612. compatibility.
  5613. """
  5614. return MaskedArray(data, mask=mask, dtype=dtype, copy=copy,
  5615. subok=subok, keep_mask=keep_mask,
  5616. hard_mask=hard_mask, fill_value=fill_value,
  5617. ndmin=ndmin, shrink=shrink, order=order)
  5618. array.__doc__ = masked_array.__doc__
  5619. def is_masked(x):
  5620. """
  5621. Determine whether input has masked values.
  5622. Accepts any object as input, but always returns False unless the
  5623. input is a MaskedArray containing masked values.
  5624. Parameters
  5625. ----------
  5626. x : array_like
  5627. Array to check for masked values.
  5628. Returns
  5629. -------
  5630. result : bool
  5631. True if `x` is a MaskedArray with masked values, False otherwise.
  5632. Examples
  5633. --------
  5634. >>> import numpy.ma as ma
  5635. >>> x = ma.masked_equal([0, 1, 0, 2, 3], 0)
  5636. >>> x
  5637. masked_array(data=[--, 1, --, 2, 3],
  5638. mask=[ True, False, True, False, False],
  5639. fill_value=0)
  5640. >>> ma.is_masked(x)
  5641. True
  5642. >>> x = ma.masked_equal([0, 1, 0, 2, 3], 42)
  5643. >>> x
  5644. masked_array(data=[0, 1, 0, 2, 3],
  5645. mask=False,
  5646. fill_value=42)
  5647. >>> ma.is_masked(x)
  5648. False
  5649. Always returns False if `x` isn't a MaskedArray.
  5650. >>> x = [False, True, False]
  5651. >>> ma.is_masked(x)
  5652. False
  5653. >>> x = 'a string'
  5654. >>> ma.is_masked(x)
  5655. False
  5656. """
  5657. m = getmask(x)
  5658. if m is nomask:
  5659. return False
  5660. elif m.any():
  5661. return True
  5662. return False
  5663. ##############################################################################
  5664. # Extrema functions #
  5665. ##############################################################################
  5666. class _extrema_operation(_MaskedUFunc):
  5667. """
  5668. Generic class for maximum/minimum functions.
  5669. .. note::
  5670. This is the base class for `_maximum_operation` and
  5671. `_minimum_operation`.
  5672. """
  5673. def __init__(self, ufunc, compare, fill_value):
  5674. super().__init__(ufunc)
  5675. self.compare = compare
  5676. self.fill_value_func = fill_value
  5677. def __call__(self, a, b):
  5678. "Executes the call behavior."
  5679. return where(self.compare(a, b), a, b)
  5680. def reduce(self, target, axis=np._NoValue):
  5681. "Reduce target along the given axis."
  5682. target = narray(target, copy=False, subok=True)
  5683. m = getmask(target)
  5684. if axis is np._NoValue and target.ndim > 1:
  5685. # 2017-05-06, Numpy 1.13.0: warn on axis default
  5686. warnings.warn(
  5687. f"In the future the default for ma.{self.__name__}.reduce will be axis=0, "
  5688. f"not the current None, to match np.{self.__name__}.reduce. "
  5689. "Explicitly pass 0 or None to silence this warning.",
  5690. MaskedArrayFutureWarning, stacklevel=2)
  5691. axis = None
  5692. if axis is not np._NoValue:
  5693. kwargs = dict(axis=axis)
  5694. else:
  5695. kwargs = dict()
  5696. if m is nomask:
  5697. t = self.f.reduce(target, **kwargs)
  5698. else:
  5699. target = target.filled(
  5700. self.fill_value_func(target)).view(type(target))
  5701. t = self.f.reduce(target, **kwargs)
  5702. m = umath.logical_and.reduce(m, **kwargs)
  5703. if hasattr(t, '_mask'):
  5704. t._mask = m
  5705. elif m:
  5706. t = masked
  5707. return t
  5708. def outer(self, a, b):
  5709. "Return the function applied to the outer product of a and b."
  5710. ma = getmask(a)
  5711. mb = getmask(b)
  5712. if ma is nomask and mb is nomask:
  5713. m = nomask
  5714. else:
  5715. ma = getmaskarray(a)
  5716. mb = getmaskarray(b)
  5717. m = logical_or.outer(ma, mb)
  5718. result = self.f.outer(filled(a), filled(b))
  5719. if not isinstance(result, MaskedArray):
  5720. result = result.view(MaskedArray)
  5721. result._mask = m
  5722. return result
  5723. def min(obj, axis=None, out=None, fill_value=None, keepdims=np._NoValue):
  5724. kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims}
  5725. try:
  5726. return obj.min(axis=axis, fill_value=fill_value, out=out, **kwargs)
  5727. except (AttributeError, TypeError):
  5728. # If obj doesn't have a min method, or if the method doesn't accept a
  5729. # fill_value argument
  5730. return asanyarray(obj).min(axis=axis, fill_value=fill_value,
  5731. out=out, **kwargs)
  5732. min.__doc__ = MaskedArray.min.__doc__
  5733. def max(obj, axis=None, out=None, fill_value=None, keepdims=np._NoValue):
  5734. kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims}
  5735. try:
  5736. return obj.max(axis=axis, fill_value=fill_value, out=out, **kwargs)
  5737. except (AttributeError, TypeError):
  5738. # If obj doesn't have a max method, or if the method doesn't accept a
  5739. # fill_value argument
  5740. return asanyarray(obj).max(axis=axis, fill_value=fill_value,
  5741. out=out, **kwargs)
  5742. max.__doc__ = MaskedArray.max.__doc__
  5743. def ptp(obj, axis=None, out=None, fill_value=None, keepdims=np._NoValue):
  5744. kwargs = {} if keepdims is np._NoValue else {'keepdims': keepdims}
  5745. try:
  5746. return obj.ptp(axis, out=out, fill_value=fill_value, **kwargs)
  5747. except (AttributeError, TypeError):
  5748. # If obj doesn't have a ptp method or if the method doesn't accept
  5749. # a fill_value argument
  5750. return asanyarray(obj).ptp(axis=axis, fill_value=fill_value,
  5751. out=out, **kwargs)
  5752. ptp.__doc__ = MaskedArray.ptp.__doc__
  5753. ##############################################################################
  5754. # Definition of functions from the corresponding methods #
  5755. ##############################################################################
  5756. class _frommethod:
  5757. """
  5758. Define functions from existing MaskedArray methods.
  5759. Parameters
  5760. ----------
  5761. methodname : str
  5762. Name of the method to transform.
  5763. """
  5764. def __init__(self, methodname, reversed=False):
  5765. self.__name__ = methodname
  5766. self.__doc__ = self.getdoc()
  5767. self.reversed = reversed
  5768. def getdoc(self):
  5769. "Return the doc of the function (from the doc of the method)."
  5770. meth = getattr(MaskedArray, self.__name__, None) or\
  5771. getattr(np, self.__name__, None)
  5772. signature = self.__name__ + get_object_signature(meth)
  5773. if meth is not None:
  5774. doc = """ %s\n%s""" % (
  5775. signature, getattr(meth, '__doc__', None))
  5776. return doc
  5777. def __call__(self, a, *args, **params):
  5778. if self.reversed:
  5779. args = list(args)
  5780. a, args[0] = args[0], a
  5781. marr = asanyarray(a)
  5782. method_name = self.__name__
  5783. method = getattr(type(marr), method_name, None)
  5784. if method is None:
  5785. # use the corresponding np function
  5786. method = getattr(np, method_name)
  5787. return method(marr, *args, **params)
  5788. all = _frommethod('all')
  5789. anomalies = anom = _frommethod('anom')
  5790. any = _frommethod('any')
  5791. compress = _frommethod('compress', reversed=True)
  5792. cumprod = _frommethod('cumprod')
  5793. cumsum = _frommethod('cumsum')
  5794. copy = _frommethod('copy')
  5795. diagonal = _frommethod('diagonal')
  5796. harden_mask = _frommethod('harden_mask')
  5797. ids = _frommethod('ids')
  5798. maximum = _extrema_operation(umath.maximum, greater, maximum_fill_value)
  5799. mean = _frommethod('mean')
  5800. minimum = _extrema_operation(umath.minimum, less, minimum_fill_value)
  5801. nonzero = _frommethod('nonzero')
  5802. prod = _frommethod('prod')
  5803. product = _frommethod('prod')
  5804. ravel = _frommethod('ravel')
  5805. repeat = _frommethod('repeat')
  5806. shrink_mask = _frommethod('shrink_mask')
  5807. soften_mask = _frommethod('soften_mask')
  5808. std = _frommethod('std')
  5809. sum = _frommethod('sum')
  5810. swapaxes = _frommethod('swapaxes')
  5811. #take = _frommethod('take')
  5812. trace = _frommethod('trace')
  5813. var = _frommethod('var')
  5814. count = _frommethod('count')
  5815. def take(a, indices, axis=None, out=None, mode='raise'):
  5816. """
  5817. """
  5818. a = masked_array(a)
  5819. return a.take(indices, axis=axis, out=out, mode=mode)
  5820. def power(a, b, third=None):
  5821. """
  5822. Returns element-wise base array raised to power from second array.
  5823. This is the masked array version of `numpy.power`. For details see
  5824. `numpy.power`.
  5825. See Also
  5826. --------
  5827. numpy.power
  5828. Notes
  5829. -----
  5830. The *out* argument to `numpy.power` is not supported, `third` has to be
  5831. None.
  5832. Examples
  5833. --------
  5834. >>> import numpy.ma as ma
  5835. >>> x = [11.2, -3.973, 0.801, -1.41]
  5836. >>> mask = [0, 0, 0, 1]
  5837. >>> masked_x = ma.masked_array(x, mask)
  5838. >>> masked_x
  5839. masked_array(data=[11.2, -3.973, 0.801, --],
  5840. mask=[False, False, False, True],
  5841. fill_value=1e+20)
  5842. >>> ma.power(masked_x, 2)
  5843. masked_array(data=[125.43999999999998, 15.784728999999999,
  5844. 0.6416010000000001, --],
  5845. mask=[False, False, False, True],
  5846. fill_value=1e+20)
  5847. >>> y = [-0.5, 2, 0, 17]
  5848. >>> masked_y = ma.masked_array(y, mask)
  5849. >>> masked_y
  5850. masked_array(data=[-0.5, 2.0, 0.0, --],
  5851. mask=[False, False, False, True],
  5852. fill_value=1e+20)
  5853. >>> ma.power(masked_x, masked_y)
  5854. masked_array(data=[0.29880715233359845, 15.784728999999999, 1.0, --],
  5855. mask=[False, False, False, True],
  5856. fill_value=1e+20)
  5857. """
  5858. if third is not None:
  5859. raise MaskError("3-argument power not supported.")
  5860. # Get the masks
  5861. ma = getmask(a)
  5862. mb = getmask(b)
  5863. m = mask_or(ma, mb)
  5864. # Get the rawdata
  5865. fa = getdata(a)
  5866. fb = getdata(b)
  5867. # Get the type of the result (so that we preserve subclasses)
  5868. if isinstance(a, MaskedArray):
  5869. basetype = type(a)
  5870. else:
  5871. basetype = MaskedArray
  5872. # Get the result and view it as a (subclass of) MaskedArray
  5873. with np.errstate(divide='ignore', invalid='ignore'):
  5874. result = np.where(m, fa, umath.power(fa, fb)).view(basetype)
  5875. result._update_from(a)
  5876. # Find where we're in trouble w/ NaNs and Infs
  5877. invalid = np.logical_not(np.isfinite(result.view(ndarray)))
  5878. # Add the initial mask
  5879. if m is not nomask:
  5880. if not result.ndim:
  5881. return masked
  5882. result._mask = np.logical_or(m, invalid)
  5883. # Fix the invalid parts
  5884. if invalid.any():
  5885. if not result.ndim:
  5886. return masked
  5887. elif result._mask is nomask:
  5888. result._mask = invalid
  5889. result._data[invalid] = result.fill_value
  5890. return result
  5891. argmin = _frommethod('argmin')
  5892. argmax = _frommethod('argmax')
  5893. def argsort(a, axis=np._NoValue, kind=None, order=None, endwith=True, fill_value=None):
  5894. "Function version of the eponymous method."
  5895. a = np.asanyarray(a)
  5896. # 2017-04-11, Numpy 1.13.0, gh-8701: warn on axis default
  5897. if axis is np._NoValue:
  5898. axis = _deprecate_argsort_axis(a)
  5899. if isinstance(a, MaskedArray):
  5900. return a.argsort(axis=axis, kind=kind, order=order,
  5901. endwith=endwith, fill_value=fill_value)
  5902. else:
  5903. return a.argsort(axis=axis, kind=kind, order=order)
  5904. argsort.__doc__ = MaskedArray.argsort.__doc__
  5905. def sort(a, axis=-1, kind=None, order=None, endwith=True, fill_value=None):
  5906. """
  5907. Return a sorted copy of the masked array.
  5908. Equivalent to creating a copy of the array
  5909. and applying the MaskedArray ``sort()`` method.
  5910. Refer to ``MaskedArray.sort`` for the full documentation
  5911. See Also
  5912. --------
  5913. MaskedArray.sort : equivalent method
  5914. Examples
  5915. --------
  5916. >>> import numpy.ma as ma
  5917. >>> x = [11.2, -3.973, 0.801, -1.41]
  5918. >>> mask = [0, 0, 0, 1]
  5919. >>> masked_x = ma.masked_array(x, mask)
  5920. >>> masked_x
  5921. masked_array(data=[11.2, -3.973, 0.801, --],
  5922. mask=[False, False, False, True],
  5923. fill_value=1e+20)
  5924. >>> ma.sort(masked_x)
  5925. masked_array(data=[-3.973, 0.801, 11.2, --],
  5926. mask=[False, False, False, True],
  5927. fill_value=1e+20)
  5928. """
  5929. a = np.array(a, copy=True, subok=True)
  5930. if axis is None:
  5931. a = a.flatten()
  5932. axis = 0
  5933. if isinstance(a, MaskedArray):
  5934. a.sort(axis=axis, kind=kind, order=order,
  5935. endwith=endwith, fill_value=fill_value)
  5936. else:
  5937. a.sort(axis=axis, kind=kind, order=order)
  5938. return a
  5939. def compressed(x):
  5940. """
  5941. Return all the non-masked data as a 1-D array.
  5942. This function is equivalent to calling the "compressed" method of a
  5943. `ma.MaskedArray`, see `ma.MaskedArray.compressed` for details.
  5944. See Also
  5945. --------
  5946. ma.MaskedArray.compressed : Equivalent method.
  5947. Examples
  5948. --------
  5949. Create an array with negative values masked:
  5950. >>> import numpy as np
  5951. >>> x = np.array([[1, -1, 0], [2, -1, 3], [7, 4, -1]])
  5952. >>> masked_x = np.ma.masked_array(x, mask=x < 0)
  5953. >>> masked_x
  5954. masked_array(
  5955. data=[[1, --, 0],
  5956. [2, --, 3],
  5957. [7, 4, --]],
  5958. mask=[[False, True, False],
  5959. [False, True, False],
  5960. [False, False, True]],
  5961. fill_value=999999)
  5962. Compress the masked array into a 1-D array of non-masked values:
  5963. >>> np.ma.compressed(masked_x)
  5964. array([1, 0, 2, 3, 7, 4])
  5965. """
  5966. return asanyarray(x).compressed()
  5967. def concatenate(arrays, axis=0):
  5968. """
  5969. Concatenate a sequence of arrays along the given axis.
  5970. Parameters
  5971. ----------
  5972. arrays : sequence of array_like
  5973. The arrays must have the same shape, except in the dimension
  5974. corresponding to `axis` (the first, by default).
  5975. axis : int, optional
  5976. The axis along which the arrays will be joined. Default is 0.
  5977. Returns
  5978. -------
  5979. result : MaskedArray
  5980. The concatenated array with any masked entries preserved.
  5981. See Also
  5982. --------
  5983. numpy.concatenate : Equivalent function in the top-level NumPy module.
  5984. Examples
  5985. --------
  5986. >>> import numpy.ma as ma
  5987. >>> a = ma.arange(3)
  5988. >>> a[1] = ma.masked
  5989. >>> b = ma.arange(2, 5)
  5990. >>> a
  5991. masked_array(data=[0, --, 2],
  5992. mask=[False, True, False],
  5993. fill_value=999999)
  5994. >>> b
  5995. masked_array(data=[2, 3, 4],
  5996. mask=False,
  5997. fill_value=999999)
  5998. >>> ma.concatenate([a, b])
  5999. masked_array(data=[0, --, 2, 2, 3, 4],
  6000. mask=[False, True, False, False, False, False],
  6001. fill_value=999999)
  6002. """
  6003. d = np.concatenate([getdata(a) for a in arrays], axis)
  6004. rcls = get_masked_subclass(*arrays)
  6005. data = d.view(rcls)
  6006. # Check whether one of the arrays has a non-empty mask.
  6007. for x in arrays:
  6008. if getmask(x) is not nomask:
  6009. break
  6010. else:
  6011. return data
  6012. # OK, so we have to concatenate the masks
  6013. dm = np.concatenate([getmaskarray(a) for a in arrays], axis)
  6014. dm = dm.reshape(d.shape)
  6015. # If we decide to keep a '_shrinkmask' option, we want to check that
  6016. # all of them are True, and then check for dm.any()
  6017. data._mask = _shrink_mask(dm)
  6018. return data
  6019. def diag(v, k=0):
  6020. """
  6021. Extract a diagonal or construct a diagonal array.
  6022. This function is the equivalent of `numpy.diag` that takes masked
  6023. values into account, see `numpy.diag` for details.
  6024. See Also
  6025. --------
  6026. numpy.diag : Equivalent function for ndarrays.
  6027. Examples
  6028. --------
  6029. Create an array with negative values masked:
  6030. >>> import numpy as np
  6031. >>> x = np.array([[11.2, -3.973, 18], [0.801, -1.41, 12], [7, 33, -12]])
  6032. >>> masked_x = np.ma.masked_array(x, mask=x < 0)
  6033. >>> masked_x
  6034. masked_array(
  6035. data=[[11.2, --, 18.0],
  6036. [0.801, --, 12.0],
  6037. [7.0, 33.0, --]],
  6038. mask=[[False, True, False],
  6039. [False, True, False],
  6040. [False, False, True]],
  6041. fill_value=1e+20)
  6042. Isolate the main diagonal from the masked array:
  6043. >>> np.ma.diag(masked_x)
  6044. masked_array(data=[11.2, --, --],
  6045. mask=[False, True, True],
  6046. fill_value=1e+20)
  6047. Isolate the first diagonal below the main diagonal:
  6048. >>> np.ma.diag(masked_x, -1)
  6049. masked_array(data=[0.801, 33.0],
  6050. mask=[False, False],
  6051. fill_value=1e+20)
  6052. """
  6053. output = np.diag(v, k).view(MaskedArray)
  6054. if getmask(v) is not nomask:
  6055. output._mask = np.diag(v._mask, k)
  6056. return output
  6057. def left_shift(a, n):
  6058. """
  6059. Shift the bits of an integer to the left.
  6060. This is the masked array version of `numpy.left_shift`, for details
  6061. see that function.
  6062. See Also
  6063. --------
  6064. numpy.left_shift
  6065. """
  6066. m = getmask(a)
  6067. if m is nomask:
  6068. d = umath.left_shift(filled(a), n)
  6069. return masked_array(d)
  6070. else:
  6071. d = umath.left_shift(filled(a, 0), n)
  6072. return masked_array(d, mask=m)
  6073. def right_shift(a, n):
  6074. """
  6075. Shift the bits of an integer to the right.
  6076. This is the masked array version of `numpy.right_shift`, for details
  6077. see that function.
  6078. See Also
  6079. --------
  6080. numpy.right_shift
  6081. Examples
  6082. --------
  6083. >>> import numpy.ma as ma
  6084. >>> x = [11, 3, 8, 1]
  6085. >>> mask = [0, 0, 0, 1]
  6086. >>> masked_x = ma.masked_array(x, mask)
  6087. >>> masked_x
  6088. masked_array(data=[11, 3, 8, --],
  6089. mask=[False, False, False, True],
  6090. fill_value=999999)
  6091. >>> ma.right_shift(masked_x,1)
  6092. masked_array(data=[5, 1, 4, --],
  6093. mask=[False, False, False, True],
  6094. fill_value=999999)
  6095. """
  6096. m = getmask(a)
  6097. if m is nomask:
  6098. d = umath.right_shift(filled(a), n)
  6099. return masked_array(d)
  6100. else:
  6101. d = umath.right_shift(filled(a, 0), n)
  6102. return masked_array(d, mask=m)
  6103. def put(a, indices, values, mode='raise'):
  6104. """
  6105. Set storage-indexed locations to corresponding values.
  6106. This function is equivalent to `MaskedArray.put`, see that method
  6107. for details.
  6108. See Also
  6109. --------
  6110. MaskedArray.put
  6111. """
  6112. # We can't use 'frommethod', the order of arguments is different
  6113. try:
  6114. return a.put(indices, values, mode=mode)
  6115. except AttributeError:
  6116. return narray(a, copy=False).put(indices, values, mode=mode)
  6117. def putmask(a, mask, values): # , mode='raise'):
  6118. """
  6119. Changes elements of an array based on conditional and input values.
  6120. This is the masked array version of `numpy.putmask`, for details see
  6121. `numpy.putmask`.
  6122. See Also
  6123. --------
  6124. numpy.putmask
  6125. Notes
  6126. -----
  6127. Using a masked array as `values` will **not** transform a `ndarray` into
  6128. a `MaskedArray`.
  6129. """
  6130. # We can't use 'frommethod', the order of arguments is different
  6131. if not isinstance(a, MaskedArray):
  6132. a = a.view(MaskedArray)
  6133. (valdata, valmask) = (getdata(values), getmask(values))
  6134. if getmask(a) is nomask:
  6135. if valmask is not nomask:
  6136. a._sharedmask = True
  6137. a._mask = make_mask_none(a.shape, a.dtype)
  6138. np.copyto(a._mask, valmask, where=mask)
  6139. elif a._hardmask:
  6140. if valmask is not nomask:
  6141. m = a._mask.copy()
  6142. np.copyto(m, valmask, where=mask)
  6143. a.mask |= m
  6144. else:
  6145. if valmask is nomask:
  6146. valmask = getmaskarray(values)
  6147. np.copyto(a._mask, valmask, where=mask)
  6148. np.copyto(a._data, valdata, where=mask)
  6149. return
  6150. def transpose(a, axes=None):
  6151. """
  6152. Permute the dimensions of an array.
  6153. This function is exactly equivalent to `numpy.transpose`.
  6154. See Also
  6155. --------
  6156. numpy.transpose : Equivalent function in top-level NumPy module.
  6157. Examples
  6158. --------
  6159. >>> import numpy.ma as ma
  6160. >>> x = ma.arange(4).reshape((2,2))
  6161. >>> x[1, 1] = ma.masked
  6162. >>> x
  6163. masked_array(
  6164. data=[[0, 1],
  6165. [2, --]],
  6166. mask=[[False, False],
  6167. [False, True]],
  6168. fill_value=999999)
  6169. >>> ma.transpose(x)
  6170. masked_array(
  6171. data=[[0, 2],
  6172. [1, --]],
  6173. mask=[[False, False],
  6174. [False, True]],
  6175. fill_value=999999)
  6176. """
  6177. # We can't use 'frommethod', as 'transpose' doesn't take keywords
  6178. try:
  6179. return a.transpose(axes)
  6180. except AttributeError:
  6181. return narray(a, copy=False).transpose(axes).view(MaskedArray)
  6182. def reshape(a, new_shape, order='C'):
  6183. """
  6184. Returns an array containing the same data with a new shape.
  6185. Refer to `MaskedArray.reshape` for full documentation.
  6186. See Also
  6187. --------
  6188. MaskedArray.reshape : equivalent function
  6189. """
  6190. # We can't use 'frommethod', it whine about some parameters. Dmmit.
  6191. try:
  6192. return a.reshape(new_shape, order=order)
  6193. except AttributeError:
  6194. _tmp = narray(a, copy=False).reshape(new_shape, order=order)
  6195. return _tmp.view(MaskedArray)
  6196. def resize(x, new_shape):
  6197. """
  6198. Return a new masked array with the specified size and shape.
  6199. This is the masked equivalent of the `numpy.resize` function. The new
  6200. array is filled with repeated copies of `x` (in the order that the
  6201. data are stored in memory). If `x` is masked, the new array will be
  6202. masked, and the new mask will be a repetition of the old one.
  6203. See Also
  6204. --------
  6205. numpy.resize : Equivalent function in the top level NumPy module.
  6206. Examples
  6207. --------
  6208. >>> import numpy.ma as ma
  6209. >>> a = ma.array([[1, 2] ,[3, 4]])
  6210. >>> a[0, 1] = ma.masked
  6211. >>> a
  6212. masked_array(
  6213. data=[[1, --],
  6214. [3, 4]],
  6215. mask=[[False, True],
  6216. [False, False]],
  6217. fill_value=999999)
  6218. >>> np.resize(a, (3, 3))
  6219. masked_array(
  6220. data=[[1, 2, 3],
  6221. [4, 1, 2],
  6222. [3, 4, 1]],
  6223. mask=False,
  6224. fill_value=999999)
  6225. >>> ma.resize(a, (3, 3))
  6226. masked_array(
  6227. data=[[1, --, 3],
  6228. [4, 1, --],
  6229. [3, 4, 1]],
  6230. mask=[[False, True, False],
  6231. [False, False, True],
  6232. [False, False, False]],
  6233. fill_value=999999)
  6234. A MaskedArray is always returned, regardless of the input type.
  6235. >>> a = np.array([[1, 2] ,[3, 4]])
  6236. >>> ma.resize(a, (3, 3))
  6237. masked_array(
  6238. data=[[1, 2, 3],
  6239. [4, 1, 2],
  6240. [3, 4, 1]],
  6241. mask=False,
  6242. fill_value=999999)
  6243. """
  6244. # We can't use _frommethods here, as N.resize is notoriously whiny.
  6245. m = getmask(x)
  6246. if m is not nomask:
  6247. m = np.resize(m, new_shape)
  6248. result = np.resize(x, new_shape).view(get_masked_subclass(x))
  6249. if result.ndim:
  6250. result._mask = m
  6251. return result
  6252. def ndim(obj):
  6253. """
  6254. maskedarray version of the numpy function.
  6255. """
  6256. return np.ndim(getdata(obj))
  6257. ndim.__doc__ = np.ndim.__doc__
  6258. def shape(obj):
  6259. "maskedarray version of the numpy function."
  6260. return np.shape(getdata(obj))
  6261. shape.__doc__ = np.shape.__doc__
  6262. def size(obj, axis=None):
  6263. "maskedarray version of the numpy function."
  6264. return np.size(getdata(obj), axis)
  6265. size.__doc__ = np.size.__doc__
  6266. def diff(a, /, n=1, axis=-1, prepend=np._NoValue, append=np._NoValue):
  6267. """
  6268. Calculate the n-th discrete difference along the given axis.
  6269. The first difference is given by ``out[i] = a[i+1] - a[i]`` along
  6270. the given axis, higher differences are calculated by using `diff`
  6271. recursively.
  6272. Preserves the input mask.
  6273. Parameters
  6274. ----------
  6275. a : array_like
  6276. Input array
  6277. n : int, optional
  6278. The number of times values are differenced. If zero, the input
  6279. is returned as-is.
  6280. axis : int, optional
  6281. The axis along which the difference is taken, default is the
  6282. last axis.
  6283. prepend, append : array_like, optional
  6284. Values to prepend or append to `a` along axis prior to
  6285. performing the difference. Scalar values are expanded to
  6286. arrays with length 1 in the direction of axis and the shape
  6287. of the input array in along all other axes. Otherwise the
  6288. dimension and shape must match `a` except along axis.
  6289. Returns
  6290. -------
  6291. diff : MaskedArray
  6292. The n-th differences. The shape of the output is the same as `a`
  6293. except along `axis` where the dimension is smaller by `n`. The
  6294. type of the output is the same as the type of the difference
  6295. between any two elements of `a`. This is the same as the type of
  6296. `a` in most cases. A notable exception is `datetime64`, which
  6297. results in a `timedelta64` output array.
  6298. See Also
  6299. --------
  6300. numpy.diff : Equivalent function in the top-level NumPy module.
  6301. Notes
  6302. -----
  6303. Type is preserved for boolean arrays, so the result will contain
  6304. `False` when consecutive elements are the same and `True` when they
  6305. differ.
  6306. For unsigned integer arrays, the results will also be unsigned. This
  6307. should not be surprising, as the result is consistent with
  6308. calculating the difference directly:
  6309. >>> u8_arr = np.array([1, 0], dtype=np.uint8)
  6310. >>> np.ma.diff(u8_arr)
  6311. masked_array(data=[255],
  6312. mask=False,
  6313. fill_value=999999,
  6314. dtype=uint8)
  6315. >>> u8_arr[1,...] - u8_arr[0,...]
  6316. 255
  6317. If this is not desirable, then the array should be cast to a larger
  6318. integer type first:
  6319. >>> i16_arr = u8_arr.astype(np.int16)
  6320. >>> np.ma.diff(i16_arr)
  6321. masked_array(data=[-1],
  6322. mask=False,
  6323. fill_value=999999,
  6324. dtype=int16)
  6325. Examples
  6326. --------
  6327. >>> a = np.array([1, 2, 3, 4, 7, 0, 2, 3])
  6328. >>> x = np.ma.masked_where(a < 2, a)
  6329. >>> np.ma.diff(x)
  6330. masked_array(data=[--, 1, 1, 3, --, --, 1],
  6331. mask=[ True, False, False, False, True, True, False],
  6332. fill_value=999999)
  6333. >>> np.ma.diff(x, n=2)
  6334. masked_array(data=[--, 0, 2, --, --, --],
  6335. mask=[ True, False, False, True, True, True],
  6336. fill_value=999999)
  6337. >>> a = np.array([[1, 3, 1, 5, 10], [0, 1, 5, 6, 8]])
  6338. >>> x = np.ma.masked_equal(a, value=1)
  6339. >>> np.ma.diff(x)
  6340. masked_array(
  6341. data=[[--, --, --, 5],
  6342. [--, --, 1, 2]],
  6343. mask=[[ True, True, True, False],
  6344. [ True, True, False, False]],
  6345. fill_value=1)
  6346. >>> np.ma.diff(x, axis=0)
  6347. masked_array(data=[[--, --, --, 1, -2]],
  6348. mask=[[ True, True, True, False, False]],
  6349. fill_value=1)
  6350. """
  6351. if n == 0:
  6352. return a
  6353. if n < 0:
  6354. raise ValueError("order must be non-negative but got " + repr(n))
  6355. a = np.ma.asanyarray(a)
  6356. if a.ndim == 0:
  6357. raise ValueError(
  6358. "diff requires input that is at least one dimensional"
  6359. )
  6360. combined = []
  6361. if prepend is not np._NoValue:
  6362. prepend = np.ma.asanyarray(prepend)
  6363. if prepend.ndim == 0:
  6364. shape = list(a.shape)
  6365. shape[axis] = 1
  6366. prepend = np.broadcast_to(prepend, tuple(shape))
  6367. combined.append(prepend)
  6368. combined.append(a)
  6369. if append is not np._NoValue:
  6370. append = np.ma.asanyarray(append)
  6371. if append.ndim == 0:
  6372. shape = list(a.shape)
  6373. shape[axis] = 1
  6374. append = np.broadcast_to(append, tuple(shape))
  6375. combined.append(append)
  6376. if len(combined) > 1:
  6377. a = np.ma.concatenate(combined, axis)
  6378. # GH 22465 np.diff without prepend/append preserves the mask
  6379. return np.diff(a, n, axis)
  6380. ##############################################################################
  6381. # Extra functions #
  6382. ##############################################################################
  6383. def where(condition, x=_NoValue, y=_NoValue):
  6384. """
  6385. Return a masked array with elements from `x` or `y`, depending on condition.
  6386. .. note::
  6387. When only `condition` is provided, this function is identical to
  6388. `nonzero`. The rest of this documentation covers only the case where
  6389. all three arguments are provided.
  6390. Parameters
  6391. ----------
  6392. condition : array_like, bool
  6393. Where True, yield `x`, otherwise yield `y`.
  6394. x, y : array_like, optional
  6395. Values from which to choose. `x`, `y` and `condition` need to be
  6396. broadcastable to some shape.
  6397. Returns
  6398. -------
  6399. out : MaskedArray
  6400. An masked array with `masked` elements where the condition is masked,
  6401. elements from `x` where `condition` is True, and elements from `y`
  6402. elsewhere.
  6403. See Also
  6404. --------
  6405. numpy.where : Equivalent function in the top-level NumPy module.
  6406. nonzero : The function that is called when x and y are omitted
  6407. Examples
  6408. --------
  6409. >>> x = np.ma.array(np.arange(9.).reshape(3, 3), mask=[[0, 1, 0],
  6410. ... [1, 0, 1],
  6411. ... [0, 1, 0]])
  6412. >>> x
  6413. masked_array(
  6414. data=[[0.0, --, 2.0],
  6415. [--, 4.0, --],
  6416. [6.0, --, 8.0]],
  6417. mask=[[False, True, False],
  6418. [ True, False, True],
  6419. [False, True, False]],
  6420. fill_value=1e+20)
  6421. >>> np.ma.where(x > 5, x, -3.1416)
  6422. masked_array(
  6423. data=[[-3.1416, --, -3.1416],
  6424. [--, -3.1416, --],
  6425. [6.0, --, 8.0]],
  6426. mask=[[False, True, False],
  6427. [ True, False, True],
  6428. [False, True, False]],
  6429. fill_value=1e+20)
  6430. """
  6431. # handle the single-argument case
  6432. missing = (x is _NoValue, y is _NoValue).count(True)
  6433. if missing == 1:
  6434. raise ValueError("Must provide both 'x' and 'y' or neither.")
  6435. if missing == 2:
  6436. return nonzero(condition)
  6437. # we only care if the condition is true - false or masked pick y
  6438. cf = filled(condition, False)
  6439. xd = getdata(x)
  6440. yd = getdata(y)
  6441. # we need the full arrays here for correct final dimensions
  6442. cm = getmaskarray(condition)
  6443. xm = getmaskarray(x)
  6444. ym = getmaskarray(y)
  6445. # deal with the fact that masked.dtype == float64, but we don't actually
  6446. # want to treat it as that.
  6447. if x is masked and y is not masked:
  6448. xd = np.zeros((), dtype=yd.dtype)
  6449. xm = np.ones((), dtype=ym.dtype)
  6450. elif y is masked and x is not masked:
  6451. yd = np.zeros((), dtype=xd.dtype)
  6452. ym = np.ones((), dtype=xm.dtype)
  6453. data = np.where(cf, xd, yd)
  6454. mask = np.where(cf, xm, ym)
  6455. mask = np.where(cm, np.ones((), dtype=mask.dtype), mask)
  6456. # collapse the mask, for backwards compatibility
  6457. mask = _shrink_mask(mask)
  6458. return masked_array(data, mask=mask)
  6459. def choose(indices, choices, out=None, mode='raise'):
  6460. """
  6461. Use an index array to construct a new array from a list of choices.
  6462. Given an array of integers and a list of n choice arrays, this method
  6463. will create a new array that merges each of the choice arrays. Where a
  6464. value in `index` is i, the new array will have the value that choices[i]
  6465. contains in the same place.
  6466. Parameters
  6467. ----------
  6468. indices : ndarray of ints
  6469. This array must contain integers in ``[0, n-1]``, where n is the
  6470. number of choices.
  6471. choices : sequence of arrays
  6472. Choice arrays. The index array and all of the choices should be
  6473. broadcastable to the same shape.
  6474. out : array, optional
  6475. If provided, the result will be inserted into this array. It should
  6476. be of the appropriate shape and `dtype`.
  6477. mode : {'raise', 'wrap', 'clip'}, optional
  6478. Specifies how out-of-bounds indices will behave.
  6479. * 'raise' : raise an error
  6480. * 'wrap' : wrap around
  6481. * 'clip' : clip to the range
  6482. Returns
  6483. -------
  6484. merged_array : array
  6485. See Also
  6486. --------
  6487. choose : equivalent function
  6488. Examples
  6489. --------
  6490. >>> choice = np.array([[1,1,1], [2,2,2], [3,3,3]])
  6491. >>> a = np.array([2, 1, 0])
  6492. >>> np.ma.choose(a, choice)
  6493. masked_array(data=[3, 2, 1],
  6494. mask=False,
  6495. fill_value=999999)
  6496. """
  6497. def fmask(x):
  6498. "Returns the filled array, or True if masked."
  6499. if x is masked:
  6500. return True
  6501. return filled(x)
  6502. def nmask(x):
  6503. "Returns the mask, True if ``masked``, False if ``nomask``."
  6504. if x is masked:
  6505. return True
  6506. return getmask(x)
  6507. # Get the indices.
  6508. c = filled(indices, 0)
  6509. # Get the masks.
  6510. masks = [nmask(x) for x in choices]
  6511. data = [fmask(x) for x in choices]
  6512. # Construct the mask
  6513. outputmask = np.choose(c, masks, mode=mode)
  6514. outputmask = make_mask(mask_or(outputmask, getmask(indices)),
  6515. copy=False, shrink=True)
  6516. # Get the choices.
  6517. d = np.choose(c, data, mode=mode, out=out).view(MaskedArray)
  6518. if out is not None:
  6519. if isinstance(out, MaskedArray):
  6520. out.__setmask__(outputmask)
  6521. return out
  6522. d.__setmask__(outputmask)
  6523. return d
  6524. def round_(a, decimals=0, out=None):
  6525. """
  6526. Return a copy of a, rounded to 'decimals' places.
  6527. When 'decimals' is negative, it specifies the number of positions
  6528. to the left of the decimal point. The real and imaginary parts of
  6529. complex numbers are rounded separately. Nothing is done if the
  6530. array is not of float type and 'decimals' is greater than or equal
  6531. to 0.
  6532. Parameters
  6533. ----------
  6534. decimals : int
  6535. Number of decimals to round to. May be negative.
  6536. out : array_like
  6537. Existing array to use for output.
  6538. If not given, returns a default copy of a.
  6539. Notes
  6540. -----
  6541. If out is given and does not have a mask attribute, the mask of a
  6542. is lost!
  6543. Examples
  6544. --------
  6545. >>> import numpy.ma as ma
  6546. >>> x = [11.2, -3.973, 0.801, -1.41]
  6547. >>> mask = [0, 0, 0, 1]
  6548. >>> masked_x = ma.masked_array(x, mask)
  6549. >>> masked_x
  6550. masked_array(data=[11.2, -3.973, 0.801, --],
  6551. mask=[False, False, False, True],
  6552. fill_value=1e+20)
  6553. >>> ma.round_(masked_x)
  6554. masked_array(data=[11.0, -4.0, 1.0, --],
  6555. mask=[False, False, False, True],
  6556. fill_value=1e+20)
  6557. >>> ma.round(masked_x, decimals=1)
  6558. masked_array(data=[11.2, -4.0, 0.8, --],
  6559. mask=[False, False, False, True],
  6560. fill_value=1e+20)
  6561. >>> ma.round_(masked_x, decimals=-1)
  6562. masked_array(data=[10.0, -0.0, 0.0, --],
  6563. mask=[False, False, False, True],
  6564. fill_value=1e+20)
  6565. """
  6566. if out is None:
  6567. return np.round_(a, decimals, out)
  6568. else:
  6569. np.round_(getdata(a), decimals, out)
  6570. if hasattr(out, '_mask'):
  6571. out._mask = getmask(a)
  6572. return out
  6573. round = round_
  6574. def _mask_propagate(a, axis):
  6575. """
  6576. Mask whole 1-d vectors of an array that contain masked values.
  6577. """
  6578. a = array(a, subok=False)
  6579. m = getmask(a)
  6580. if m is nomask or not m.any() or axis is None:
  6581. return a
  6582. a._mask = a._mask.copy()
  6583. axes = normalize_axis_tuple(axis, a.ndim)
  6584. for ax in axes:
  6585. a._mask |= m.any(axis=ax, keepdims=True)
  6586. return a
  6587. # Include masked dot here to avoid import problems in getting it from
  6588. # extras.py. Note that it is not included in __all__, but rather exported
  6589. # from extras in order to avoid backward compatibility problems.
  6590. def dot(a, b, strict=False, out=None):
  6591. """
  6592. Return the dot product of two arrays.
  6593. This function is the equivalent of `numpy.dot` that takes masked values
  6594. into account. Note that `strict` and `out` are in different position
  6595. than in the method version. In order to maintain compatibility with the
  6596. corresponding method, it is recommended that the optional arguments be
  6597. treated as keyword only. At some point that may be mandatory.
  6598. Parameters
  6599. ----------
  6600. a, b : masked_array_like
  6601. Inputs arrays.
  6602. strict : bool, optional
  6603. Whether masked data are propagated (True) or set to 0 (False) for
  6604. the computation. Default is False. Propagating the mask means that
  6605. if a masked value appears in a row or column, the whole row or
  6606. column is considered masked.
  6607. out : masked_array, optional
  6608. Output argument. This must have the exact kind that would be returned
  6609. if it was not used. In particular, it must have the right type, must be
  6610. C-contiguous, and its dtype must be the dtype that would be returned
  6611. for `dot(a,b)`. This is a performance feature. Therefore, if these
  6612. conditions are not met, an exception is raised, instead of attempting
  6613. to be flexible.
  6614. .. versionadded:: 1.10.2
  6615. See Also
  6616. --------
  6617. numpy.dot : Equivalent function for ndarrays.
  6618. Examples
  6619. --------
  6620. >>> a = np.ma.array([[1, 2, 3], [4, 5, 6]], mask=[[1, 0, 0], [0, 0, 0]])
  6621. >>> b = np.ma.array([[1, 2], [3, 4], [5, 6]], mask=[[1, 0], [0, 0], [0, 0]])
  6622. >>> np.ma.dot(a, b)
  6623. masked_array(
  6624. data=[[21, 26],
  6625. [45, 64]],
  6626. mask=[[False, False],
  6627. [False, False]],
  6628. fill_value=999999)
  6629. >>> np.ma.dot(a, b, strict=True)
  6630. masked_array(
  6631. data=[[--, --],
  6632. [--, 64]],
  6633. mask=[[ True, True],
  6634. [ True, False]],
  6635. fill_value=999999)
  6636. """
  6637. if strict is True:
  6638. if np.ndim(a) == 0 or np.ndim(b) == 0:
  6639. pass
  6640. elif b.ndim == 1:
  6641. a = _mask_propagate(a, a.ndim - 1)
  6642. b = _mask_propagate(b, b.ndim - 1)
  6643. else:
  6644. a = _mask_propagate(a, a.ndim - 1)
  6645. b = _mask_propagate(b, b.ndim - 2)
  6646. am = ~getmaskarray(a)
  6647. bm = ~getmaskarray(b)
  6648. if out is None:
  6649. d = np.dot(filled(a, 0), filled(b, 0))
  6650. m = ~np.dot(am, bm)
  6651. if np.ndim(d) == 0:
  6652. d = np.asarray(d)
  6653. r = d.view(get_masked_subclass(a, b))
  6654. r.__setmask__(m)
  6655. return r
  6656. else:
  6657. d = np.dot(filled(a, 0), filled(b, 0), out._data)
  6658. if out.mask.shape != d.shape:
  6659. out._mask = np.empty(d.shape, MaskType)
  6660. np.dot(am, bm, out._mask)
  6661. np.logical_not(out._mask, out._mask)
  6662. return out
  6663. def inner(a, b):
  6664. """
  6665. Returns the inner product of a and b for arrays of floating point types.
  6666. Like the generic NumPy equivalent the product sum is over the last dimension
  6667. of a and b. The first argument is not conjugated.
  6668. """
  6669. fa = filled(a, 0)
  6670. fb = filled(b, 0)
  6671. if fa.ndim == 0:
  6672. fa.shape = (1,)
  6673. if fb.ndim == 0:
  6674. fb.shape = (1,)
  6675. return np.inner(fa, fb).view(MaskedArray)
  6676. inner.__doc__ = doc_note(np.inner.__doc__,
  6677. "Masked values are replaced by 0.")
  6678. innerproduct = inner
  6679. def outer(a, b):
  6680. "maskedarray version of the numpy function."
  6681. fa = filled(a, 0).ravel()
  6682. fb = filled(b, 0).ravel()
  6683. d = np.outer(fa, fb)
  6684. ma = getmask(a)
  6685. mb = getmask(b)
  6686. if ma is nomask and mb is nomask:
  6687. return masked_array(d)
  6688. ma = getmaskarray(a)
  6689. mb = getmaskarray(b)
  6690. m = make_mask(1 - np.outer(1 - ma, 1 - mb), copy=False)
  6691. return masked_array(d, mask=m)
  6692. outer.__doc__ = doc_note(np.outer.__doc__,
  6693. "Masked values are replaced by 0.")
  6694. outerproduct = outer
  6695. def _convolve_or_correlate(f, a, v, mode, propagate_mask):
  6696. """
  6697. Helper function for ma.correlate and ma.convolve
  6698. """
  6699. if propagate_mask:
  6700. # results which are contributed to by either item in any pair being invalid
  6701. mask = (
  6702. f(getmaskarray(a), np.ones(np.shape(v), dtype=bool), mode=mode)
  6703. | f(np.ones(np.shape(a), dtype=bool), getmaskarray(v), mode=mode)
  6704. )
  6705. data = f(getdata(a), getdata(v), mode=mode)
  6706. else:
  6707. # results which are not contributed to by any pair of valid elements
  6708. mask = ~f(~getmaskarray(a), ~getmaskarray(v))
  6709. data = f(filled(a, 0), filled(v, 0), mode=mode)
  6710. return masked_array(data, mask=mask)
  6711. def correlate(a, v, mode='valid', propagate_mask=True):
  6712. """
  6713. Cross-correlation of two 1-dimensional sequences.
  6714. Parameters
  6715. ----------
  6716. a, v : array_like
  6717. Input sequences.
  6718. mode : {'valid', 'same', 'full'}, optional
  6719. Refer to the `np.convolve` docstring. Note that the default
  6720. is 'valid', unlike `convolve`, which uses 'full'.
  6721. propagate_mask : bool
  6722. If True, then a result element is masked if any masked element contributes towards it.
  6723. If False, then a result element is only masked if no non-masked element
  6724. contribute towards it
  6725. Returns
  6726. -------
  6727. out : MaskedArray
  6728. Discrete cross-correlation of `a` and `v`.
  6729. See Also
  6730. --------
  6731. numpy.correlate : Equivalent function in the top-level NumPy module.
  6732. """
  6733. return _convolve_or_correlate(np.correlate, a, v, mode, propagate_mask)
  6734. def convolve(a, v, mode='full', propagate_mask=True):
  6735. """
  6736. Returns the discrete, linear convolution of two one-dimensional sequences.
  6737. Parameters
  6738. ----------
  6739. a, v : array_like
  6740. Input sequences.
  6741. mode : {'valid', 'same', 'full'}, optional
  6742. Refer to the `np.convolve` docstring.
  6743. propagate_mask : bool
  6744. If True, then if any masked element is included in the sum for a result
  6745. element, then the result is masked.
  6746. If False, then the result element is only masked if no non-masked cells
  6747. contribute towards it
  6748. Returns
  6749. -------
  6750. out : MaskedArray
  6751. Discrete, linear convolution of `a` and `v`.
  6752. See Also
  6753. --------
  6754. numpy.convolve : Equivalent function in the top-level NumPy module.
  6755. """
  6756. return _convolve_or_correlate(np.convolve, a, v, mode, propagate_mask)
  6757. def allequal(a, b, fill_value=True):
  6758. """
  6759. Return True if all entries of a and b are equal, using
  6760. fill_value as a truth value where either or both are masked.
  6761. Parameters
  6762. ----------
  6763. a, b : array_like
  6764. Input arrays to compare.
  6765. fill_value : bool, optional
  6766. Whether masked values in a or b are considered equal (True) or not
  6767. (False).
  6768. Returns
  6769. -------
  6770. y : bool
  6771. Returns True if the two arrays are equal within the given
  6772. tolerance, False otherwise. If either array contains NaN,
  6773. then False is returned.
  6774. See Also
  6775. --------
  6776. all, any
  6777. numpy.ma.allclose
  6778. Examples
  6779. --------
  6780. >>> a = np.ma.array([1e10, 1e-7, 42.0], mask=[0, 0, 1])
  6781. >>> a
  6782. masked_array(data=[10000000000.0, 1e-07, --],
  6783. mask=[False, False, True],
  6784. fill_value=1e+20)
  6785. >>> b = np.array([1e10, 1e-7, -42.0])
  6786. >>> b
  6787. array([ 1.00000000e+10, 1.00000000e-07, -4.20000000e+01])
  6788. >>> np.ma.allequal(a, b, fill_value=False)
  6789. False
  6790. >>> np.ma.allequal(a, b)
  6791. True
  6792. """
  6793. m = mask_or(getmask(a), getmask(b))
  6794. if m is nomask:
  6795. x = getdata(a)
  6796. y = getdata(b)
  6797. d = umath.equal(x, y)
  6798. return d.all()
  6799. elif fill_value:
  6800. x = getdata(a)
  6801. y = getdata(b)
  6802. d = umath.equal(x, y)
  6803. dm = array(d, mask=m, copy=False)
  6804. return dm.filled(True).all(None)
  6805. else:
  6806. return False
  6807. def allclose(a, b, masked_equal=True, rtol=1e-5, atol=1e-8):
  6808. """
  6809. Returns True if two arrays are element-wise equal within a tolerance.
  6810. This function is equivalent to `allclose` except that masked values
  6811. are treated as equal (default) or unequal, depending on the `masked_equal`
  6812. argument.
  6813. Parameters
  6814. ----------
  6815. a, b : array_like
  6816. Input arrays to compare.
  6817. masked_equal : bool, optional
  6818. Whether masked values in `a` and `b` are considered equal (True) or not
  6819. (False). They are considered equal by default.
  6820. rtol : float, optional
  6821. Relative tolerance. The relative difference is equal to ``rtol * b``.
  6822. Default is 1e-5.
  6823. atol : float, optional
  6824. Absolute tolerance. The absolute difference is equal to `atol`.
  6825. Default is 1e-8.
  6826. Returns
  6827. -------
  6828. y : bool
  6829. Returns True if the two arrays are equal within the given
  6830. tolerance, False otherwise. If either array contains NaN, then
  6831. False is returned.
  6832. See Also
  6833. --------
  6834. all, any
  6835. numpy.allclose : the non-masked `allclose`.
  6836. Notes
  6837. -----
  6838. If the following equation is element-wise True, then `allclose` returns
  6839. True::
  6840. absolute(`a` - `b`) <= (`atol` + `rtol` * absolute(`b`))
  6841. Return True if all elements of `a` and `b` are equal subject to
  6842. given tolerances.
  6843. Examples
  6844. --------
  6845. >>> a = np.ma.array([1e10, 1e-7, 42.0], mask=[0, 0, 1])
  6846. >>> a
  6847. masked_array(data=[10000000000.0, 1e-07, --],
  6848. mask=[False, False, True],
  6849. fill_value=1e+20)
  6850. >>> b = np.ma.array([1e10, 1e-8, -42.0], mask=[0, 0, 1])
  6851. >>> np.ma.allclose(a, b)
  6852. False
  6853. >>> a = np.ma.array([1e10, 1e-8, 42.0], mask=[0, 0, 1])
  6854. >>> b = np.ma.array([1.00001e10, 1e-9, -42.0], mask=[0, 0, 1])
  6855. >>> np.ma.allclose(a, b)
  6856. True
  6857. >>> np.ma.allclose(a, b, masked_equal=False)
  6858. False
  6859. Masked values are not compared directly.
  6860. >>> a = np.ma.array([1e10, 1e-8, 42.0], mask=[0, 0, 1])
  6861. >>> b = np.ma.array([1.00001e10, 1e-9, 42.0], mask=[0, 0, 1])
  6862. >>> np.ma.allclose(a, b)
  6863. True
  6864. >>> np.ma.allclose(a, b, masked_equal=False)
  6865. False
  6866. """
  6867. x = masked_array(a, copy=False)
  6868. y = masked_array(b, copy=False)
  6869. # make sure y is an inexact type to avoid abs(MIN_INT); will cause
  6870. # casting of x later.
  6871. # NOTE: We explicitly allow timedelta, which used to work. This could
  6872. # possibly be deprecated. See also gh-18286.
  6873. # timedelta works if `atol` is an integer or also a timedelta.
  6874. # Although, the default tolerances are unlikely to be useful
  6875. if y.dtype.kind != "m":
  6876. dtype = np.result_type(y, 1.)
  6877. if y.dtype != dtype:
  6878. y = masked_array(y, dtype=dtype, copy=False)
  6879. m = mask_or(getmask(x), getmask(y))
  6880. xinf = np.isinf(masked_array(x, copy=False, mask=m)).filled(False)
  6881. # If we have some infs, they should fall at the same place.
  6882. if not np.all(xinf == filled(np.isinf(y), False)):
  6883. return False
  6884. # No infs at all
  6885. if not np.any(xinf):
  6886. d = filled(less_equal(absolute(x - y), atol + rtol * absolute(y)),
  6887. masked_equal)
  6888. return np.all(d)
  6889. if not np.all(filled(x[xinf] == y[xinf], masked_equal)):
  6890. return False
  6891. x = x[~xinf]
  6892. y = y[~xinf]
  6893. d = filled(less_equal(absolute(x - y), atol + rtol * absolute(y)),
  6894. masked_equal)
  6895. return np.all(d)
  6896. def asarray(a, dtype=None, order=None):
  6897. """
  6898. Convert the input to a masked array of the given data-type.
  6899. No copy is performed if the input is already an `ndarray`. If `a` is
  6900. a subclass of `MaskedArray`, a base class `MaskedArray` is returned.
  6901. Parameters
  6902. ----------
  6903. a : array_like
  6904. Input data, in any form that can be converted to a masked array. This
  6905. includes lists, lists of tuples, tuples, tuples of tuples, tuples
  6906. of lists, ndarrays and masked arrays.
  6907. dtype : dtype, optional
  6908. By default, the data-type is inferred from the input data.
  6909. order : {'C', 'F'}, optional
  6910. Whether to use row-major ('C') or column-major ('FORTRAN') memory
  6911. representation. Default is 'C'.
  6912. Returns
  6913. -------
  6914. out : MaskedArray
  6915. Masked array interpretation of `a`.
  6916. See Also
  6917. --------
  6918. asanyarray : Similar to `asarray`, but conserves subclasses.
  6919. Examples
  6920. --------
  6921. >>> x = np.arange(10.).reshape(2, 5)
  6922. >>> x
  6923. array([[0., 1., 2., 3., 4.],
  6924. [5., 6., 7., 8., 9.]])
  6925. >>> np.ma.asarray(x)
  6926. masked_array(
  6927. data=[[0., 1., 2., 3., 4.],
  6928. [5., 6., 7., 8., 9.]],
  6929. mask=False,
  6930. fill_value=1e+20)
  6931. >>> type(np.ma.asarray(x))
  6932. <class 'numpy.ma.core.MaskedArray'>
  6933. """
  6934. order = order or 'C'
  6935. return masked_array(a, dtype=dtype, copy=False, keep_mask=True,
  6936. subok=False, order=order)
  6937. def asanyarray(a, dtype=None):
  6938. """
  6939. Convert the input to a masked array, conserving subclasses.
  6940. If `a` is a subclass of `MaskedArray`, its class is conserved.
  6941. No copy is performed if the input is already an `ndarray`.
  6942. Parameters
  6943. ----------
  6944. a : array_like
  6945. Input data, in any form that can be converted to an array.
  6946. dtype : dtype, optional
  6947. By default, the data-type is inferred from the input data.
  6948. order : {'C', 'F'}, optional
  6949. Whether to use row-major ('C') or column-major ('FORTRAN') memory
  6950. representation. Default is 'C'.
  6951. Returns
  6952. -------
  6953. out : MaskedArray
  6954. MaskedArray interpretation of `a`.
  6955. See Also
  6956. --------
  6957. asarray : Similar to `asanyarray`, but does not conserve subclass.
  6958. Examples
  6959. --------
  6960. >>> x = np.arange(10.).reshape(2, 5)
  6961. >>> x
  6962. array([[0., 1., 2., 3., 4.],
  6963. [5., 6., 7., 8., 9.]])
  6964. >>> np.ma.asanyarray(x)
  6965. masked_array(
  6966. data=[[0., 1., 2., 3., 4.],
  6967. [5., 6., 7., 8., 9.]],
  6968. mask=False,
  6969. fill_value=1e+20)
  6970. >>> type(np.ma.asanyarray(x))
  6971. <class 'numpy.ma.core.MaskedArray'>
  6972. """
  6973. # workaround for #8666, to preserve identity. Ideally the bottom line
  6974. # would handle this for us.
  6975. if isinstance(a, MaskedArray) and (dtype is None or dtype == a.dtype):
  6976. return a
  6977. return masked_array(a, dtype=dtype, copy=False, keep_mask=True, subok=True)
  6978. ##############################################################################
  6979. # Pickling #
  6980. ##############################################################################
  6981. def fromfile(file, dtype=float, count=-1, sep=''):
  6982. raise NotImplementedError(
  6983. "fromfile() not yet implemented for a MaskedArray.")
  6984. def fromflex(fxarray):
  6985. """
  6986. Build a masked array from a suitable flexible-type array.
  6987. The input array has to have a data-type with ``_data`` and ``_mask``
  6988. fields. This type of array is output by `MaskedArray.toflex`.
  6989. Parameters
  6990. ----------
  6991. fxarray : ndarray
  6992. The structured input array, containing ``_data`` and ``_mask``
  6993. fields. If present, other fields are discarded.
  6994. Returns
  6995. -------
  6996. result : MaskedArray
  6997. The constructed masked array.
  6998. See Also
  6999. --------
  7000. MaskedArray.toflex : Build a flexible-type array from a masked array.
  7001. Examples
  7002. --------
  7003. >>> x = np.ma.array(np.arange(9).reshape(3, 3), mask=[0] + [1, 0] * 4)
  7004. >>> rec = x.toflex()
  7005. >>> rec
  7006. array([[(0, False), (1, True), (2, False)],
  7007. [(3, True), (4, False), (5, True)],
  7008. [(6, False), (7, True), (8, False)]],
  7009. dtype=[('_data', '<i8'), ('_mask', '?')])
  7010. >>> x2 = np.ma.fromflex(rec)
  7011. >>> x2
  7012. masked_array(
  7013. data=[[0, --, 2],
  7014. [--, 4, --],
  7015. [6, --, 8]],
  7016. mask=[[False, True, False],
  7017. [ True, False, True],
  7018. [False, True, False]],
  7019. fill_value=999999)
  7020. Extra fields can be present in the structured array but are discarded:
  7021. >>> dt = [('_data', '<i4'), ('_mask', '|b1'), ('field3', '<f4')]
  7022. >>> rec2 = np.zeros((2, 2), dtype=dt)
  7023. >>> rec2
  7024. array([[(0, False, 0.), (0, False, 0.)],
  7025. [(0, False, 0.), (0, False, 0.)]],
  7026. dtype=[('_data', '<i4'), ('_mask', '?'), ('field3', '<f4')])
  7027. >>> y = np.ma.fromflex(rec2)
  7028. >>> y
  7029. masked_array(
  7030. data=[[0, 0],
  7031. [0, 0]],
  7032. mask=[[False, False],
  7033. [False, False]],
  7034. fill_value=999999,
  7035. dtype=int32)
  7036. """
  7037. return masked_array(fxarray['_data'], mask=fxarray['_mask'])
  7038. class _convert2ma:
  7039. """
  7040. Convert functions from numpy to numpy.ma.
  7041. Parameters
  7042. ----------
  7043. _methodname : string
  7044. Name of the method to transform.
  7045. """
  7046. __doc__ = None
  7047. def __init__(self, funcname, np_ret, np_ma_ret, params=None):
  7048. self._func = getattr(np, funcname)
  7049. self.__doc__ = self.getdoc(np_ret, np_ma_ret)
  7050. self._extras = params or {}
  7051. def getdoc(self, np_ret, np_ma_ret):
  7052. "Return the doc of the function (from the doc of the method)."
  7053. doc = getattr(self._func, '__doc__', None)
  7054. sig = get_object_signature(self._func)
  7055. if doc:
  7056. doc = self._replace_return_type(doc, np_ret, np_ma_ret)
  7057. # Add the signature of the function at the beginning of the doc
  7058. if sig:
  7059. sig = "%s%s\n" % (self._func.__name__, sig)
  7060. doc = sig + doc
  7061. return doc
  7062. def _replace_return_type(self, doc, np_ret, np_ma_ret):
  7063. """
  7064. Replace documentation of ``np`` function's return type.
  7065. Replaces it with the proper type for the ``np.ma`` function.
  7066. Parameters
  7067. ----------
  7068. doc : str
  7069. The documentation of the ``np`` method.
  7070. np_ret : str
  7071. The return type string of the ``np`` method that we want to
  7072. replace. (e.g. "out : ndarray")
  7073. np_ma_ret : str
  7074. The return type string of the ``np.ma`` method.
  7075. (e.g. "out : MaskedArray")
  7076. """
  7077. if np_ret not in doc:
  7078. raise RuntimeError(
  7079. f"Failed to replace `{np_ret}` with `{np_ma_ret}`. "
  7080. f"The documentation string for return type, {np_ret}, is not "
  7081. f"found in the docstring for `np.{self._func.__name__}`. "
  7082. f"Fix the docstring for `np.{self._func.__name__}` or "
  7083. "update the expected string for return type."
  7084. )
  7085. return doc.replace(np_ret, np_ma_ret)
  7086. def __call__(self, *args, **params):
  7087. # Find the common parameters to the call and the definition
  7088. _extras = self._extras
  7089. common_params = set(params).intersection(_extras)
  7090. # Drop the common parameters from the call
  7091. for p in common_params:
  7092. _extras[p] = params.pop(p)
  7093. # Get the result
  7094. result = self._func.__call__(*args, **params).view(MaskedArray)
  7095. if "fill_value" in common_params:
  7096. result.fill_value = _extras.get("fill_value", None)
  7097. if "hardmask" in common_params:
  7098. result._hardmask = bool(_extras.get("hard_mask", False))
  7099. return result
  7100. arange = _convert2ma(
  7101. 'arange',
  7102. params=dict(fill_value=None, hardmask=False),
  7103. np_ret='arange : ndarray',
  7104. np_ma_ret='arange : MaskedArray',
  7105. )
  7106. clip = _convert2ma(
  7107. 'clip',
  7108. params=dict(fill_value=None, hardmask=False),
  7109. np_ret='clipped_array : ndarray',
  7110. np_ma_ret='clipped_array : MaskedArray',
  7111. )
  7112. empty = _convert2ma(
  7113. 'empty',
  7114. params=dict(fill_value=None, hardmask=False),
  7115. np_ret='out : ndarray',
  7116. np_ma_ret='out : MaskedArray',
  7117. )
  7118. empty_like = _convert2ma(
  7119. 'empty_like',
  7120. np_ret='out : ndarray',
  7121. np_ma_ret='out : MaskedArray',
  7122. )
  7123. frombuffer = _convert2ma(
  7124. 'frombuffer',
  7125. np_ret='out : ndarray',
  7126. np_ma_ret='out: MaskedArray',
  7127. )
  7128. fromfunction = _convert2ma(
  7129. 'fromfunction',
  7130. np_ret='fromfunction : any',
  7131. np_ma_ret='fromfunction: MaskedArray',
  7132. )
  7133. identity = _convert2ma(
  7134. 'identity',
  7135. params=dict(fill_value=None, hardmask=False),
  7136. np_ret='out : ndarray',
  7137. np_ma_ret='out : MaskedArray',
  7138. )
  7139. indices = _convert2ma(
  7140. 'indices',
  7141. params=dict(fill_value=None, hardmask=False),
  7142. np_ret='grid : one ndarray or tuple of ndarrays',
  7143. np_ma_ret='grid : one MaskedArray or tuple of MaskedArrays',
  7144. )
  7145. ones = _convert2ma(
  7146. 'ones',
  7147. params=dict(fill_value=None, hardmask=False),
  7148. np_ret='out : ndarray',
  7149. np_ma_ret='out : MaskedArray',
  7150. )
  7151. ones_like = _convert2ma(
  7152. 'ones_like',
  7153. np_ret='out : ndarray',
  7154. np_ma_ret='out : MaskedArray',
  7155. )
  7156. squeeze = _convert2ma(
  7157. 'squeeze',
  7158. params=dict(fill_value=None, hardmask=False),
  7159. np_ret='squeezed : ndarray',
  7160. np_ma_ret='squeezed : MaskedArray',
  7161. )
  7162. zeros = _convert2ma(
  7163. 'zeros',
  7164. params=dict(fill_value=None, hardmask=False),
  7165. np_ret='out : ndarray',
  7166. np_ma_ret='out : MaskedArray',
  7167. )
  7168. zeros_like = _convert2ma(
  7169. 'zeros_like',
  7170. np_ret='out : ndarray',
  7171. np_ma_ret='out : MaskedArray',
  7172. )
  7173. def append(a, b, axis=None):
  7174. """Append values to the end of an array.
  7175. .. versionadded:: 1.9.0
  7176. Parameters
  7177. ----------
  7178. a : array_like
  7179. Values are appended to a copy of this array.
  7180. b : array_like
  7181. These values are appended to a copy of `a`. It must be of the
  7182. correct shape (the same shape as `a`, excluding `axis`). If `axis`
  7183. is not specified, `b` can be any shape and will be flattened
  7184. before use.
  7185. axis : int, optional
  7186. The axis along which `v` are appended. If `axis` is not given,
  7187. both `a` and `b` are flattened before use.
  7188. Returns
  7189. -------
  7190. append : MaskedArray
  7191. A copy of `a` with `b` appended to `axis`. Note that `append`
  7192. does not occur in-place: a new array is allocated and filled. If
  7193. `axis` is None, the result is a flattened array.
  7194. See Also
  7195. --------
  7196. numpy.append : Equivalent function in the top-level NumPy module.
  7197. Examples
  7198. --------
  7199. >>> import numpy.ma as ma
  7200. >>> a = ma.masked_values([1, 2, 3], 2)
  7201. >>> b = ma.masked_values([[4, 5, 6], [7, 8, 9]], 7)
  7202. >>> ma.append(a, b)
  7203. masked_array(data=[1, --, 3, 4, 5, 6, --, 8, 9],
  7204. mask=[False, True, False, False, False, False, True, False,
  7205. False],
  7206. fill_value=999999)
  7207. """
  7208. return concatenate([a, b], axis)