core.py 282 KB

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