test_morphology.py 135 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105
  1. import numpy as np
  2. from scipy._lib._array_api import (
  3. is_cupy, is_numpy,
  4. xp_assert_close, xp_assert_equal, assert_array_almost_equal,
  5. make_xp_test_case,
  6. make_xp_pytest_param,
  7. )
  8. import pytest
  9. from pytest import raises as assert_raises
  10. from scipy import ndimage
  11. from . import types
  12. skip_xp_backends = pytest.mark.skip_xp_backends
  13. xfail_xp_backends = pytest.mark.xfail_xp_backends
  14. class TestNdimageMorphology:
  15. @make_xp_test_case(ndimage.distance_transform_bf)
  16. @pytest.mark.parametrize('dtype', types)
  17. def test_distance_transform_bf01(self, dtype, xp):
  18. dtype = getattr(xp, dtype)
  19. # brute force (bf) distance transform
  20. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  21. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  22. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  23. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  24. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  25. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  26. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  27. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  28. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  29. out, ft = ndimage.distance_transform_bf(data, 'euclidean',
  30. return_indices=True)
  31. expected = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
  32. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  33. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  34. [0, 0, 1, 2, 4, 2, 1, 0, 0],
  35. [0, 0, 1, 4, 8, 4, 1, 0, 0],
  36. [0, 0, 1, 2, 4, 2, 1, 0, 0],
  37. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  38. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  39. [0, 0, 0, 0, 0, 0, 0, 0, 0]]
  40. expected = xp.asarray(expected)
  41. assert_array_almost_equal(out * out, expected)
  42. expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0],
  43. [1, 1, 1, 1, 1, 1, 1, 1, 1],
  44. [2, 2, 2, 2, 1, 2, 2, 2, 2],
  45. [3, 3, 3, 2, 1, 2, 3, 3, 3],
  46. [4, 4, 4, 4, 6, 4, 4, 4, 4],
  47. [5, 5, 6, 6, 7, 6, 6, 5, 5],
  48. [6, 6, 6, 7, 7, 7, 6, 6, 6],
  49. [7, 7, 7, 7, 7, 7, 7, 7, 7],
  50. [8, 8, 8, 8, 8, 8, 8, 8, 8]],
  51. [[0, 1, 2, 3, 4, 5, 6, 7, 8],
  52. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  53. [0, 1, 2, 2, 4, 6, 6, 7, 8],
  54. [0, 1, 1, 2, 4, 6, 7, 7, 8],
  55. [0, 1, 1, 1, 6, 7, 7, 7, 8],
  56. [0, 1, 2, 2, 4, 6, 6, 7, 8],
  57. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  58. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  59. [0, 1, 2, 3, 4, 5, 6, 7, 8]]]
  60. expected = xp.asarray(expected)
  61. assert_array_almost_equal(ft, expected)
  62. @make_xp_test_case(ndimage.distance_transform_bf)
  63. @pytest.mark.parametrize('dtype', types)
  64. def test_distance_transform_bf02(self, dtype, xp):
  65. dtype = getattr(xp, dtype)
  66. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  67. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  68. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  69. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  70. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  71. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  72. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  73. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  74. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  75. out, ft = ndimage.distance_transform_bf(data, 'cityblock',
  76. return_indices=True)
  77. expected = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
  78. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  79. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  80. [0, 0, 1, 2, 2, 2, 1, 0, 0],
  81. [0, 0, 1, 2, 3, 2, 1, 0, 0],
  82. [0, 0, 1, 2, 2, 2, 1, 0, 0],
  83. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  84. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  85. [0, 0, 0, 0, 0, 0, 0, 0, 0]]
  86. expected = xp.asarray(expected)
  87. assert_array_almost_equal(out, expected)
  88. expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0],
  89. [1, 1, 1, 1, 1, 1, 1, 1, 1],
  90. [2, 2, 2, 2, 1, 2, 2, 2, 2],
  91. [3, 3, 3, 3, 1, 3, 3, 3, 3],
  92. [4, 4, 4, 4, 7, 4, 4, 4, 4],
  93. [5, 5, 6, 7, 7, 7, 6, 5, 5],
  94. [6, 6, 6, 7, 7, 7, 6, 6, 6],
  95. [7, 7, 7, 7, 7, 7, 7, 7, 7],
  96. [8, 8, 8, 8, 8, 8, 8, 8, 8]],
  97. [[0, 1, 2, 3, 4, 5, 6, 7, 8],
  98. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  99. [0, 1, 2, 2, 4, 6, 6, 7, 8],
  100. [0, 1, 1, 1, 4, 7, 7, 7, 8],
  101. [0, 1, 1, 1, 4, 7, 7, 7, 8],
  102. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  103. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  104. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  105. [0, 1, 2, 3, 4, 5, 6, 7, 8]]]
  106. expected = xp.asarray(expected)
  107. assert_array_almost_equal(ft, expected)
  108. @make_xp_test_case(ndimage.distance_transform_bf)
  109. @pytest.mark.parametrize('dtype', types)
  110. def test_distance_transform_bf03(self, dtype, xp):
  111. dtype = getattr(xp, dtype)
  112. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  113. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  114. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  115. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  116. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  117. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  118. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  119. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  120. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  121. out, ft = ndimage.distance_transform_bf(data, 'chessboard',
  122. return_indices=True)
  123. expected = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
  124. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  125. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  126. [0, 0, 1, 1, 2, 1, 1, 0, 0],
  127. [0, 0, 1, 2, 2, 2, 1, 0, 0],
  128. [0, 0, 1, 1, 2, 1, 1, 0, 0],
  129. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  130. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  131. [0, 0, 0, 0, 0, 0, 0, 0, 0]]
  132. expected = xp.asarray(expected)
  133. assert_array_almost_equal(out, expected)
  134. expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0],
  135. [1, 1, 1, 1, 1, 1, 1, 1, 1],
  136. [2, 2, 2, 2, 1, 2, 2, 2, 2],
  137. [3, 3, 4, 2, 2, 2, 4, 3, 3],
  138. [4, 4, 5, 6, 6, 6, 5, 4, 4],
  139. [5, 5, 6, 6, 7, 6, 6, 5, 5],
  140. [6, 6, 6, 7, 7, 7, 6, 6, 6],
  141. [7, 7, 7, 7, 7, 7, 7, 7, 7],
  142. [8, 8, 8, 8, 8, 8, 8, 8, 8]],
  143. [[0, 1, 2, 3, 4, 5, 6, 7, 8],
  144. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  145. [0, 1, 2, 2, 5, 6, 6, 7, 8],
  146. [0, 1, 1, 2, 6, 6, 7, 7, 8],
  147. [0, 1, 1, 2, 6, 7, 7, 7, 8],
  148. [0, 1, 2, 2, 6, 6, 7, 7, 8],
  149. [0, 1, 2, 4, 5, 6, 6, 7, 8],
  150. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  151. [0, 1, 2, 3, 4, 5, 6, 7, 8]]]
  152. expected = xp.asarray(expected)
  153. assert_array_almost_equal(ft, expected)
  154. @skip_xp_backends(
  155. np_only=True, reason='inplace distances= arrays are numpy-specific'
  156. )
  157. @make_xp_test_case(ndimage.distance_transform_bf)
  158. @pytest.mark.parametrize('dtype', types)
  159. def test_distance_transform_bf04(self, dtype, xp):
  160. dtype = getattr(xp, dtype)
  161. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  162. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  163. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  164. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  165. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  166. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  167. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  168. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  169. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  170. tdt, tft = ndimage.distance_transform_bf(data, return_indices=1)
  171. dts = []
  172. fts = []
  173. dt = xp.zeros(data.shape, dtype=xp.float64)
  174. ndimage.distance_transform_bf(data, distances=dt)
  175. dts.append(dt)
  176. ft = ndimage.distance_transform_bf(
  177. data, return_distances=False, return_indices=1)
  178. fts.append(ft)
  179. ft = np.indices(data.shape, dtype=xp.int32)
  180. ndimage.distance_transform_bf(
  181. data, return_distances=False, return_indices=True, indices=ft)
  182. fts.append(ft)
  183. dt, ft = ndimage.distance_transform_bf(
  184. data, return_indices=1)
  185. dts.append(dt)
  186. fts.append(ft)
  187. dt = xp.zeros(data.shape, dtype=xp.float64)
  188. ft = ndimage.distance_transform_bf(
  189. data, distances=dt, return_indices=True)
  190. dts.append(dt)
  191. fts.append(ft)
  192. ft = np.indices(data.shape, dtype=xp.int32)
  193. dt = ndimage.distance_transform_bf(
  194. data, return_indices=True, indices=ft)
  195. dts.append(dt)
  196. fts.append(ft)
  197. dt = xp.zeros(data.shape, dtype=xp.float64)
  198. ft = np.indices(data.shape, dtype=xp.int32)
  199. ndimage.distance_transform_bf(
  200. data, distances=dt, return_indices=True, indices=ft)
  201. dts.append(dt)
  202. fts.append(ft)
  203. for dt in dts:
  204. assert_array_almost_equal(tdt, dt)
  205. for ft in fts:
  206. assert_array_almost_equal(tft, ft)
  207. @xfail_xp_backends('cupy', reason='CuPy does not have distance_transform_bf')
  208. @make_xp_test_case(ndimage.distance_transform_bf)
  209. @pytest.mark.parametrize('dtype', types)
  210. def test_distance_transform_bf05(self, dtype, xp):
  211. dtype = getattr(xp, dtype)
  212. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  213. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  214. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  215. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  216. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  217. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  218. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  219. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  220. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  221. out, ft = ndimage.distance_transform_bf(
  222. data, 'euclidean', return_indices=True, sampling=[2, 2])
  223. expected = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
  224. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  225. [0, 0, 0, 4, 4, 4, 0, 0, 0],
  226. [0, 0, 4, 8, 16, 8, 4, 0, 0],
  227. [0, 0, 4, 16, 32, 16, 4, 0, 0],
  228. [0, 0, 4, 8, 16, 8, 4, 0, 0],
  229. [0, 0, 0, 4, 4, 4, 0, 0, 0],
  230. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  231. [0, 0, 0, 0, 0, 0, 0, 0, 0]]
  232. expected = xp.asarray(expected)
  233. assert_array_almost_equal(out * out, expected)
  234. expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0],
  235. [1, 1, 1, 1, 1, 1, 1, 1, 1],
  236. [2, 2, 2, 2, 1, 2, 2, 2, 2],
  237. [3, 3, 3, 2, 1, 2, 3, 3, 3],
  238. [4, 4, 4, 4, 6, 4, 4, 4, 4],
  239. [5, 5, 6, 6, 7, 6, 6, 5, 5],
  240. [6, 6, 6, 7, 7, 7, 6, 6, 6],
  241. [7, 7, 7, 7, 7, 7, 7, 7, 7],
  242. [8, 8, 8, 8, 8, 8, 8, 8, 8]],
  243. [[0, 1, 2, 3, 4, 5, 6, 7, 8],
  244. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  245. [0, 1, 2, 2, 4, 6, 6, 7, 8],
  246. [0, 1, 1, 2, 4, 6, 7, 7, 8],
  247. [0, 1, 1, 1, 6, 7, 7, 7, 8],
  248. [0, 1, 2, 2, 4, 6, 6, 7, 8],
  249. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  250. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  251. [0, 1, 2, 3, 4, 5, 6, 7, 8]]]
  252. expected = xp.asarray(expected)
  253. assert_array_almost_equal(ft, expected)
  254. @make_xp_test_case(ndimage.distance_transform_bf)
  255. @pytest.mark.parametrize('dtype', types)
  256. def test_distance_transform_bf06(self, dtype, xp):
  257. dtype = getattr(xp, dtype)
  258. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  259. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  260. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  261. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  262. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  263. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  264. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  265. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  266. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  267. out, ft = ndimage.distance_transform_bf(
  268. data, 'euclidean', return_indices=True, sampling=[2, 1])
  269. expected = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
  270. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  271. [0, 0, 0, 1, 4, 1, 0, 0, 0],
  272. [0, 0, 1, 4, 8, 4, 1, 0, 0],
  273. [0, 0, 1, 4, 9, 4, 1, 0, 0],
  274. [0, 0, 1, 4, 8, 4, 1, 0, 0],
  275. [0, 0, 0, 1, 4, 1, 0, 0, 0],
  276. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  277. [0, 0, 0, 0, 0, 0, 0, 0, 0]]
  278. expected = xp.asarray(expected)
  279. assert_array_almost_equal(out * out, expected)
  280. expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0],
  281. [1, 1, 1, 1, 1, 1, 1, 1, 1],
  282. [2, 2, 2, 2, 2, 2, 2, 2, 2],
  283. [3, 3, 3, 3, 2, 3, 3, 3, 3],
  284. [4, 4, 4, 4, 4, 4, 4, 4, 4],
  285. [5, 5, 5, 5, 6, 5, 5, 5, 5],
  286. [6, 6, 6, 6, 7, 6, 6, 6, 6],
  287. [7, 7, 7, 7, 7, 7, 7, 7, 7],
  288. [8, 8, 8, 8, 8, 8, 8, 8, 8]],
  289. [[0, 1, 2, 3, 4, 5, 6, 7, 8],
  290. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  291. [0, 1, 2, 2, 6, 6, 6, 7, 8],
  292. [0, 1, 1, 1, 6, 7, 7, 7, 8],
  293. [0, 1, 1, 1, 7, 7, 7, 7, 8],
  294. [0, 1, 1, 1, 6, 7, 7, 7, 8],
  295. [0, 1, 2, 2, 4, 6, 6, 7, 8],
  296. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  297. [0, 1, 2, 3, 4, 5, 6, 7, 8]]]
  298. expected = xp.asarray(expected)
  299. assert_array_almost_equal(ft, expected)
  300. @make_xp_test_case(ndimage.distance_transform_bf)
  301. def test_distance_transform_bf07(self, xp):
  302. # test input validation per discussion on PR #13302
  303. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  304. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  305. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  306. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  307. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  308. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  309. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  310. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  311. [0, 0, 0, 0, 0, 0, 0, 0, 0]])
  312. with assert_raises(RuntimeError):
  313. ndimage.distance_transform_bf(
  314. data, return_distances=False, return_indices=False
  315. )
  316. @make_xp_test_case(ndimage.distance_transform_bf,
  317. ndimage.distance_transform_cdt)
  318. @pytest.mark.parametrize('dtype', types)
  319. def test_distance_transform_cdt01(self, dtype, xp):
  320. dtype = getattr(xp, dtype)
  321. # chamfer type distance (cdt) transform
  322. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  323. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  324. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  325. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  326. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  327. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  328. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  329. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  330. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  331. out, ft = ndimage.distance_transform_cdt(
  332. data, 'cityblock', return_indices=True)
  333. bf = ndimage.distance_transform_bf(data, 'cityblock')
  334. assert_array_almost_equal(bf, out)
  335. expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0],
  336. [1, 1, 1, 1, 1, 1, 1, 1, 1],
  337. [2, 2, 2, 1, 1, 1, 2, 2, 2],
  338. [3, 3, 2, 1, 1, 1, 2, 3, 3],
  339. [4, 4, 4, 4, 1, 4, 4, 4, 4],
  340. [5, 5, 5, 5, 7, 7, 6, 5, 5],
  341. [6, 6, 6, 6, 7, 7, 6, 6, 6],
  342. [7, 7, 7, 7, 7, 7, 7, 7, 7],
  343. [8, 8, 8, 8, 8, 8, 8, 8, 8]],
  344. [[0, 1, 2, 3, 4, 5, 6, 7, 8],
  345. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  346. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  347. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  348. [0, 1, 1, 1, 4, 7, 7, 7, 8],
  349. [0, 1, 1, 1, 4, 5, 6, 7, 8],
  350. [0, 1, 2, 2, 4, 5, 6, 7, 8],
  351. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  352. [0, 1, 2, 3, 4, 5, 6, 7, 8]]]
  353. expected = xp.asarray(expected)
  354. assert_array_almost_equal(ft, expected)
  355. @make_xp_test_case(ndimage.distance_transform_bf,
  356. ndimage.distance_transform_cdt)
  357. @pytest.mark.parametrize('dtype', types)
  358. def test_distance_transform_cdt02(self, dtype, xp):
  359. dtype = getattr(xp, dtype)
  360. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  361. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  362. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  363. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  364. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  365. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  366. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  367. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  368. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  369. out, ft = ndimage.distance_transform_cdt(data, 'chessboard',
  370. return_indices=True)
  371. bf = ndimage.distance_transform_bf(data, 'chessboard')
  372. assert_array_almost_equal(bf, out)
  373. expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0],
  374. [1, 1, 1, 1, 1, 1, 1, 1, 1],
  375. [2, 2, 2, 1, 1, 1, 2, 2, 2],
  376. [3, 3, 2, 2, 1, 2, 2, 3, 3],
  377. [4, 4, 3, 2, 2, 2, 3, 4, 4],
  378. [5, 5, 4, 6, 7, 6, 4, 5, 5],
  379. [6, 6, 6, 6, 7, 7, 6, 6, 6],
  380. [7, 7, 7, 7, 7, 7, 7, 7, 7],
  381. [8, 8, 8, 8, 8, 8, 8, 8, 8]],
  382. [[0, 1, 2, 3, 4, 5, 6, 7, 8],
  383. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  384. [0, 1, 2, 2, 3, 4, 6, 7, 8],
  385. [0, 1, 1, 2, 2, 6, 6, 7, 8],
  386. [0, 1, 1, 1, 2, 6, 7, 7, 8],
  387. [0, 1, 1, 2, 6, 6, 7, 7, 8],
  388. [0, 1, 2, 2, 5, 6, 6, 7, 8],
  389. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  390. [0, 1, 2, 3, 4, 5, 6, 7, 8]]]
  391. expected = xp.asarray(expected)
  392. assert_array_almost_equal(ft, expected)
  393. @skip_xp_backends(
  394. np_only=True, reason='inplace indices= arrays are numpy-specific'
  395. )
  396. @make_xp_test_case(ndimage.distance_transform_cdt)
  397. @pytest.mark.parametrize('dtype', types)
  398. def test_distance_transform_cdt03(self, dtype, xp):
  399. dtype = getattr(xp, dtype)
  400. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  401. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  402. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  403. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  404. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  405. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  406. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  407. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  408. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  409. tdt, tft = ndimage.distance_transform_cdt(data, return_indices=True)
  410. dts = []
  411. fts = []
  412. dt = xp.zeros(data.shape, dtype=xp.int32)
  413. ndimage.distance_transform_cdt(data, distances=dt)
  414. dts.append(dt)
  415. ft = ndimage.distance_transform_cdt(
  416. data, return_distances=False, return_indices=True)
  417. fts.append(ft)
  418. ft = xp.asarray(np.indices(data.shape, dtype=np.int32))
  419. ndimage.distance_transform_cdt(
  420. data, return_distances=False, return_indices=True, indices=ft)
  421. fts.append(ft)
  422. dt, ft = ndimage.distance_transform_cdt(
  423. data, return_indices=True)
  424. dts.append(dt)
  425. fts.append(ft)
  426. dt = xp.zeros(data.shape, dtype=xp.int32)
  427. ft = ndimage.distance_transform_cdt(
  428. data, distances=dt, return_indices=True)
  429. dts.append(dt)
  430. fts.append(ft)
  431. ft = xp.asarray(np.indices(data.shape, dtype=np.int32))
  432. dt = ndimage.distance_transform_cdt(
  433. data, return_indices=True, indices=ft)
  434. dts.append(dt)
  435. fts.append(ft)
  436. dt = xp.zeros(data.shape, dtype=xp.int32)
  437. ft = xp.asarray(np.indices(data.shape, dtype=np.int32))
  438. ndimage.distance_transform_cdt(data, distances=dt,
  439. return_indices=True, indices=ft)
  440. dts.append(dt)
  441. fts.append(ft)
  442. for dt in dts:
  443. assert_array_almost_equal(tdt, dt)
  444. for ft in fts:
  445. assert_array_almost_equal(tft, ft)
  446. @skip_xp_backends(
  447. np_only=True, reason='XXX: does not raise unless indices is a numpy array'
  448. )
  449. @make_xp_test_case(ndimage.distance_transform_cdt)
  450. def test_distance_transform_cdt04(self, xp):
  451. # test input validation per discussion on PR #13302
  452. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  453. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  454. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  455. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  456. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  457. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  458. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  459. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  460. [0, 0, 0, 0, 0, 0, 0, 0, 0]])
  461. indices_out = xp.zeros((data.ndim,) + data.shape, dtype=xp.int32)
  462. with assert_raises(RuntimeError):
  463. ndimage.distance_transform_bf(
  464. data,
  465. return_distances=True,
  466. return_indices=False,
  467. indices=indices_out
  468. )
  469. @xfail_xp_backends("torch", reason="int overflow")
  470. @make_xp_test_case(ndimage.distance_transform_cdt)
  471. @pytest.mark.parametrize('dtype', types)
  472. def test_distance_transform_cdt05(self, dtype, xp):
  473. dtype = getattr(xp, dtype)
  474. # test custom metric type per discussion on issue #17381
  475. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  476. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  477. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  478. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  479. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  480. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  481. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  482. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  483. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  484. metric_arg = xp.ones((3, 3))
  485. actual = ndimage.distance_transform_cdt(data, metric=metric_arg)
  486. assert xp.sum(actual) == -21
  487. @skip_xp_backends("cupy", reason="CuPy does not have distance_transform_bf")
  488. @make_xp_test_case(ndimage.distance_transform_edt,
  489. ndimage.distance_transform_bf)
  490. @pytest.mark.parametrize('dtype', types)
  491. def test_distance_transform_edt01(self, dtype, xp):
  492. dtype = getattr(xp, dtype)
  493. # euclidean distance transform (edt)
  494. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  495. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  496. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  497. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  498. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  499. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  500. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  501. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  502. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  503. out, ft = ndimage.distance_transform_edt(data, return_indices=True)
  504. bf = ndimage.distance_transform_bf(data, 'euclidean')
  505. assert_array_almost_equal(bf, out)
  506. # np-specific check
  507. np_ft = np.asarray(ft)
  508. dt = np_ft - np.indices(np_ft.shape[1:], dtype=np_ft.dtype)
  509. dt = dt.astype(np.float64)
  510. np.multiply(dt, dt, dt)
  511. dt = np.add.reduce(dt, axis=0)
  512. np.sqrt(dt, dt)
  513. dt = xp.asarray(dt)
  514. assert_array_almost_equal(bf, dt)
  515. @skip_xp_backends(
  516. np_only=True, reason='inplace distances= are numpy-specific'
  517. )
  518. @make_xp_test_case(ndimage.distance_transform_edt,
  519. ndimage.distance_transform_bf)
  520. @pytest.mark.parametrize('dtype', types)
  521. def test_distance_transform_edt02(self, dtype, xp):
  522. dtype = getattr(xp, dtype)
  523. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  524. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  525. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  526. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  527. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  528. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  529. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  530. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  531. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  532. tdt, tft = ndimage.distance_transform_edt(data, return_indices=True)
  533. dts = []
  534. fts = []
  535. dt = xp.zeros(data.shape, dtype=xp.float64)
  536. ndimage.distance_transform_edt(data, distances=dt)
  537. dts.append(dt)
  538. ft = ndimage.distance_transform_edt(
  539. data, return_distances=0, return_indices=True)
  540. fts.append(ft)
  541. ft = np.indices(data.shape, dtype=xp.int32)
  542. ft = xp.asarray(ft)
  543. ndimage.distance_transform_edt(
  544. data, return_distances=False, return_indices=True, indices=ft)
  545. fts.append(ft)
  546. dt, ft = ndimage.distance_transform_edt(
  547. data, return_indices=True)
  548. dts.append(dt)
  549. fts.append(ft)
  550. dt = xp.zeros(data.shape, dtype=xp.float64)
  551. ft = ndimage.distance_transform_edt(
  552. data, distances=dt, return_indices=True)
  553. dts.append(dt)
  554. fts.append(ft)
  555. ft = np.indices(data.shape, dtype=xp.int32)
  556. ft = xp.asarray(ft)
  557. dt = ndimage.distance_transform_edt(
  558. data, return_indices=True, indices=ft)
  559. dts.append(dt)
  560. fts.append(ft)
  561. dt = xp.zeros(data.shape, dtype=xp.float64)
  562. ft = np.indices(data.shape, dtype=xp.int32)
  563. ft = xp.asarray(ft)
  564. ndimage.distance_transform_edt(
  565. data, distances=dt, return_indices=True, indices=ft)
  566. dts.append(dt)
  567. fts.append(ft)
  568. for dt in dts:
  569. assert_array_almost_equal(tdt, dt)
  570. for ft in fts:
  571. assert_array_almost_equal(tft, ft)
  572. @make_xp_test_case(ndimage.distance_transform_edt,
  573. ndimage.distance_transform_bf)
  574. @pytest.mark.parametrize('dtype', types)
  575. def test_distance_transform_edt03(self, dtype, xp):
  576. dtype = getattr(xp, dtype)
  577. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  578. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  579. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  580. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  581. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  582. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  583. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  584. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  585. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  586. ref = ndimage.distance_transform_bf(data, 'euclidean', sampling=[2, 2])
  587. out = ndimage.distance_transform_edt(data, sampling=[2, 2])
  588. assert_array_almost_equal(out, ref)
  589. @make_xp_test_case(ndimage.distance_transform_edt,
  590. ndimage.distance_transform_bf)
  591. @pytest.mark.parametrize('dtype', types)
  592. def test_distance_transform_edt4(self, dtype, xp):
  593. dtype = getattr(xp, dtype)
  594. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  595. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  596. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  597. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  598. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  599. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  600. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  601. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  602. [0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  603. ref = ndimage.distance_transform_bf(data, 'euclidean', sampling=[2, 1])
  604. out = ndimage.distance_transform_edt(data, sampling=[2, 1])
  605. assert_array_almost_equal(out, ref)
  606. @xfail_xp_backends(
  607. "cupy", reason="Only 2D and 3D distance transforms are supported in CuPy"
  608. )
  609. @make_xp_test_case(ndimage.distance_transform_edt)
  610. def test_distance_transform_edt5(self, xp):
  611. # Ticket #954 regression test
  612. out = ndimage.distance_transform_edt(xp.asarray(False))
  613. assert_array_almost_equal(out, xp.asarray([0.]))
  614. @xfail_xp_backends(
  615. np_only=True, reason='XXX: does not raise unless indices is a numpy array'
  616. )
  617. @make_xp_test_case(ndimage.distance_transform_edt)
  618. def test_distance_transform_edt6(self, xp):
  619. # test input validation per discussion on PR #13302
  620. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  621. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  622. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  623. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  624. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  625. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  626. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  627. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  628. [0, 0, 0, 0, 0, 0, 0, 0, 0]])
  629. distances_out = xp.zeros(data.shape, dtype=xp.float64)
  630. with assert_raises(RuntimeError):
  631. ndimage.distance_transform_bf(
  632. data,
  633. return_indices=True,
  634. return_distances=False,
  635. distances=distances_out
  636. )
  637. @make_xp_test_case(ndimage.generate_binary_structure)
  638. def test_generate_structure01(self, xp):
  639. struct = ndimage.generate_binary_structure(0, 1)
  640. assert struct == 1
  641. @make_xp_test_case(ndimage.generate_binary_structure)
  642. def test_generate_structure02(self, xp):
  643. struct = ndimage.generate_binary_structure(1, 1)
  644. assert_array_almost_equal(struct, [1, 1, 1])
  645. @make_xp_test_case(ndimage.generate_binary_structure)
  646. def test_generate_structure03(self, xp):
  647. struct = ndimage.generate_binary_structure(2, 1)
  648. assert_array_almost_equal(struct, [[0, 1, 0],
  649. [1, 1, 1],
  650. [0, 1, 0]])
  651. @make_xp_test_case(ndimage.generate_binary_structure)
  652. def test_generate_structure04(self, xp):
  653. struct = ndimage.generate_binary_structure(2, 2)
  654. assert_array_almost_equal(struct, [[1, 1, 1],
  655. [1, 1, 1],
  656. [1, 1, 1]])
  657. @make_xp_test_case(ndimage.iterate_structure)
  658. def test_iterate_structure01(self, xp):
  659. struct = [[0, 1, 0],
  660. [1, 1, 1],
  661. [0, 1, 0]]
  662. struct = xp.asarray(struct)
  663. out = ndimage.iterate_structure(struct, 2)
  664. expected = np.asarray([[0, 0, 1, 0, 0],
  665. [0, 1, 1, 1, 0],
  666. [1, 1, 1, 1, 1],
  667. [0, 1, 1, 1, 0],
  668. [0, 0, 1, 0, 0]], dtype=bool)
  669. expected = xp.asarray(expected)
  670. assert_array_almost_equal(out, expected)
  671. @make_xp_test_case(ndimage.iterate_structure)
  672. def test_iterate_structure02(self, xp):
  673. struct = [[0, 1],
  674. [1, 1],
  675. [0, 1]]
  676. struct = xp.asarray(struct)
  677. out = ndimage.iterate_structure(struct, 2)
  678. expected = np.asarray([[0, 0, 1],
  679. [0, 1, 1],
  680. [1, 1, 1],
  681. [0, 1, 1],
  682. [0, 0, 1]], dtype=bool)
  683. expected = xp.asarray(expected)
  684. assert_array_almost_equal(out, expected)
  685. @make_xp_test_case(ndimage.iterate_structure)
  686. def test_iterate_structure03(self, xp):
  687. struct = [[0, 1, 0],
  688. [1, 1, 1],
  689. [0, 1, 0]]
  690. struct = xp.asarray(struct)
  691. out = ndimage.iterate_structure(struct, 2, 1)
  692. expected = [[0, 0, 1, 0, 0],
  693. [0, 1, 1, 1, 0],
  694. [1, 1, 1, 1, 1],
  695. [0, 1, 1, 1, 0],
  696. [0, 0, 1, 0, 0]]
  697. expected = np.asarray(expected, dtype=bool)
  698. expected = xp.asarray(expected)
  699. assert_array_almost_equal(out[0], expected)
  700. assert out[1] == [2, 2]
  701. @make_xp_test_case(ndimage.binary_erosion)
  702. @pytest.mark.parametrize('dtype', types)
  703. def test_binary_erosion01(self, dtype, xp):
  704. dtype = getattr(xp, dtype)
  705. data = xp.ones([], dtype=dtype)
  706. out = ndimage.binary_erosion(data)
  707. assert out == xp.asarray(1, dtype=out.dtype)
  708. @make_xp_test_case(ndimage.binary_erosion)
  709. @pytest.mark.parametrize('dtype', types)
  710. def test_binary_erosion02(self, dtype, xp):
  711. dtype = getattr(xp, dtype)
  712. data = xp.ones([], dtype=dtype)
  713. out = ndimage.binary_erosion(data, border_value=1)
  714. assert out == xp.asarray(1, dtype=out.dtype)
  715. @make_xp_test_case(ndimage.binary_erosion)
  716. @pytest.mark.parametrize('dtype', types)
  717. def test_binary_erosion03(self, dtype, xp):
  718. dtype = getattr(xp, dtype)
  719. data = xp.ones([1], dtype=dtype)
  720. out = ndimage.binary_erosion(data)
  721. assert_array_almost_equal(out, xp.asarray([0]))
  722. @make_xp_test_case(ndimage.binary_erosion)
  723. @pytest.mark.parametrize('dtype', types)
  724. def test_binary_erosion04(self, dtype, xp):
  725. dtype = getattr(xp, dtype)
  726. data = xp.ones([1], dtype=dtype)
  727. out = ndimage.binary_erosion(data, border_value=1)
  728. assert_array_almost_equal(out, xp.asarray([1]))
  729. @make_xp_test_case(ndimage.binary_erosion)
  730. @pytest.mark.parametrize('dtype', types)
  731. def test_binary_erosion05(self, dtype, xp):
  732. dtype = getattr(xp, dtype)
  733. data = xp.ones([3], dtype=dtype)
  734. out = ndimage.binary_erosion(data)
  735. assert_array_almost_equal(out, xp.asarray([0, 1, 0]))
  736. @xfail_xp_backends("cupy", reason="https://github.com/cupy/cupy/issues/8912")
  737. @make_xp_test_case(ndimage.binary_erosion)
  738. @pytest.mark.parametrize('dtype', types)
  739. def test_binary_erosion05_broadcasted(self, dtype, xp):
  740. dtype = getattr(xp, dtype)
  741. data = xp.ones((1, ), dtype=dtype)
  742. data = xp.broadcast_to(data, (3, ))
  743. out = ndimage.binary_erosion(data)
  744. assert_array_almost_equal(out, xp.asarray([0, 1, 0]))
  745. @make_xp_test_case(ndimage.binary_erosion)
  746. @pytest.mark.parametrize('dtype', types)
  747. def test_binary_erosion06(self, dtype, xp):
  748. dtype = getattr(xp, dtype)
  749. data = xp.ones([3], dtype=dtype)
  750. out = ndimage.binary_erosion(data, border_value=1)
  751. assert_array_almost_equal(out, xp.asarray([1, 1, 1]))
  752. @make_xp_test_case(ndimage.binary_erosion)
  753. @pytest.mark.parametrize('dtype', types)
  754. def test_binary_erosion07(self, dtype, xp):
  755. dtype = getattr(xp, dtype)
  756. data = xp.ones([5], dtype=dtype)
  757. out = ndimage.binary_erosion(data)
  758. assert_array_almost_equal(out, xp.asarray([0, 1, 1, 1, 0]))
  759. @make_xp_test_case(ndimage.binary_erosion)
  760. @pytest.mark.parametrize('dtype', types)
  761. def test_binary_erosion08(self, dtype, xp):
  762. dtype = getattr(xp, dtype)
  763. data = xp.ones([5], dtype=dtype)
  764. out = ndimage.binary_erosion(data, border_value=1)
  765. assert_array_almost_equal(out, xp.asarray([1, 1, 1, 1, 1]))
  766. @make_xp_test_case(ndimage.binary_erosion)
  767. @pytest.mark.parametrize('dtype', types)
  768. def test_binary_erosion09(self, dtype, xp):
  769. data = np.ones([5], dtype=dtype)
  770. data[2] = 0
  771. data = xp.asarray(data)
  772. out = ndimage.binary_erosion(data)
  773. assert_array_almost_equal(out, xp.asarray([0, 0, 0, 0, 0]))
  774. @make_xp_test_case(ndimage.binary_erosion)
  775. @pytest.mark.parametrize('dtype', types)
  776. def test_binary_erosion10(self, dtype, xp):
  777. data = np.ones([5], dtype=dtype)
  778. data[2] = 0
  779. data = xp.asarray(data)
  780. out = ndimage.binary_erosion(data, border_value=1)
  781. assert_array_almost_equal(out, xp.asarray([1, 0, 0, 0, 1]))
  782. @make_xp_test_case(ndimage.binary_erosion)
  783. @pytest.mark.parametrize('dtype', types)
  784. def test_binary_erosion11(self, dtype, xp):
  785. data = np.ones([5], dtype=dtype)
  786. data[2] = 0
  787. data = xp.asarray(data)
  788. struct = xp.asarray([1, 0, 1])
  789. out = ndimage.binary_erosion(data, struct, border_value=1)
  790. assert_array_almost_equal(out, xp.asarray([1, 0, 1, 0, 1]))
  791. @make_xp_test_case(ndimage.binary_erosion)
  792. @pytest.mark.parametrize('dtype', types)
  793. def test_binary_erosion12(self, dtype, xp):
  794. data = np.ones([5], dtype=dtype)
  795. data[2] = 0
  796. data = xp.asarray(data)
  797. struct = xp.asarray([1, 0, 1])
  798. out = ndimage.binary_erosion(data, struct, border_value=1, origin=-1)
  799. assert_array_almost_equal(out, xp.asarray([0, 1, 0, 1, 1]))
  800. @make_xp_test_case(ndimage.binary_erosion)
  801. @pytest.mark.parametrize('dtype', types)
  802. def test_binary_erosion13(self, dtype, xp):
  803. data = np.ones([5], dtype=dtype)
  804. data[2] = 0
  805. data = xp.asarray(data)
  806. struct = xp.asarray([1, 0, 1])
  807. out = ndimage.binary_erosion(data, struct, border_value=1, origin=1)
  808. assert_array_almost_equal(out, xp.asarray([1, 1, 0, 1, 0]))
  809. @make_xp_test_case(ndimage.binary_erosion)
  810. @pytest.mark.parametrize('dtype', types)
  811. def test_binary_erosion14(self, dtype, xp):
  812. data = np.ones([5], dtype=dtype)
  813. data[2] = 0
  814. data = xp.asarray(data)
  815. struct = xp.asarray([1, 1])
  816. out = ndimage.binary_erosion(data, struct, border_value=1)
  817. assert_array_almost_equal(out, xp.asarray([1, 1, 0, 0, 1]))
  818. @make_xp_test_case(ndimage.binary_erosion)
  819. @pytest.mark.parametrize('dtype', types)
  820. def test_binary_erosion15(self, dtype, xp):
  821. data = np.ones([5], dtype=dtype)
  822. data[2] = 0
  823. data = xp.asarray(data)
  824. struct = xp.asarray([1, 1])
  825. out = ndimage.binary_erosion(data, struct, border_value=1, origin=-1)
  826. assert_array_almost_equal(out, xp.asarray([1, 0, 0, 1, 1]))
  827. @make_xp_test_case(ndimage.binary_erosion)
  828. @pytest.mark.parametrize('dtype', types)
  829. def test_binary_erosion16(self, dtype, xp):
  830. dtype = getattr(xp, dtype)
  831. data = xp.ones([1, 1], dtype=dtype)
  832. out = ndimage.binary_erosion(data, border_value=1)
  833. assert_array_almost_equal(out, xp.asarray([[1]]))
  834. @make_xp_test_case(ndimage.binary_erosion)
  835. @pytest.mark.parametrize('dtype', types)
  836. def test_binary_erosion17(self, dtype, xp):
  837. dtype = getattr(xp, dtype)
  838. data = xp.ones([1, 1], dtype=dtype)
  839. out = ndimage.binary_erosion(data)
  840. assert_array_almost_equal(out, xp.asarray([[0]]))
  841. @make_xp_test_case(ndimage.binary_erosion)
  842. @pytest.mark.parametrize('dtype', types)
  843. def test_binary_erosion18(self, dtype, xp):
  844. dtype = getattr(xp, dtype)
  845. data = xp.ones([1, 3], dtype=dtype)
  846. out = ndimage.binary_erosion(data)
  847. assert_array_almost_equal(out, xp.asarray([[0, 0, 0]]))
  848. @make_xp_test_case(ndimage.binary_erosion)
  849. @pytest.mark.parametrize('dtype', types)
  850. def test_binary_erosion19(self, dtype, xp):
  851. dtype = getattr(xp, dtype)
  852. data = xp.ones([1, 3], dtype=dtype)
  853. out = ndimage.binary_erosion(data, border_value=1)
  854. assert_array_almost_equal(out, xp.asarray([[1, 1, 1]]))
  855. @make_xp_test_case(ndimage.binary_erosion)
  856. @pytest.mark.parametrize('dtype', types)
  857. def test_binary_erosion20(self, dtype, xp):
  858. dtype = getattr(xp, dtype)
  859. data = xp.ones([3, 3], dtype=dtype)
  860. out = ndimage.binary_erosion(data)
  861. assert_array_almost_equal(out, xp.asarray([[0, 0, 0],
  862. [0, 1, 0],
  863. [0, 0, 0]]))
  864. @make_xp_test_case(ndimage.binary_erosion)
  865. @pytest.mark.parametrize('dtype', types)
  866. def test_binary_erosion21(self, dtype, xp):
  867. dtype = getattr(xp, dtype)
  868. data = xp.ones([3, 3], dtype=dtype)
  869. out = ndimage.binary_erosion(data, border_value=1)
  870. assert_array_almost_equal(out, xp.asarray([[1, 1, 1],
  871. [1, 1, 1],
  872. [1, 1, 1]]))
  873. @make_xp_test_case(ndimage.binary_erosion)
  874. @pytest.mark.parametrize('dtype', types)
  875. def test_binary_erosion22(self, dtype, xp):
  876. dtype = getattr(xp, dtype)
  877. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  878. [0, 0, 0, 0, 0, 0, 0, 0],
  879. [0, 0, 0, 0, 0, 0, 0, 0],
  880. [0, 0, 0, 0, 0, 1, 0, 0],
  881. [0, 0, 0, 1, 1, 0, 0, 0],
  882. [0, 0, 1, 0, 0, 1, 0, 0],
  883. [0, 0, 0, 0, 0, 0, 0, 0],
  884. [0, 0, 0, 0, 0, 0, 0, 0]]
  885. expected = xp.asarray(expected)
  886. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0],
  887. [0, 1, 0, 0, 0, 0, 0, 0],
  888. [0, 0, 0, 0, 0, 1, 1, 1],
  889. [0, 0, 1, 1, 1, 1, 1, 1],
  890. [0, 0, 1, 1, 1, 1, 0, 0],
  891. [0, 1, 1, 1, 1, 1, 1, 0],
  892. [0, 1, 1, 0, 0, 1, 1, 0],
  893. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  894. out = ndimage.binary_erosion(data, border_value=1)
  895. assert_array_almost_equal(out, expected)
  896. @make_xp_test_case(ndimage.binary_erosion)
  897. @pytest.mark.parametrize('dtype', types)
  898. def test_binary_erosion23(self, dtype, xp):
  899. dtype = getattr(xp, dtype)
  900. struct = ndimage.generate_binary_structure(2, 2)
  901. struct = xp.asarray(struct)
  902. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  903. [0, 0, 0, 0, 0, 0, 0, 0],
  904. [0, 0, 0, 0, 0, 0, 0, 0],
  905. [0, 0, 0, 0, 0, 0, 0, 0],
  906. [0, 0, 0, 1, 1, 0, 0, 0],
  907. [0, 0, 0, 0, 0, 0, 0, 0],
  908. [0, 0, 0, 0, 0, 0, 0, 0],
  909. [0, 0, 0, 0, 0, 0, 0, 0]]
  910. expected = xp.asarray(expected)
  911. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0],
  912. [0, 1, 0, 0, 0, 0, 0, 0],
  913. [0, 0, 0, 0, 0, 1, 1, 1],
  914. [0, 0, 1, 1, 1, 1, 1, 1],
  915. [0, 0, 1, 1, 1, 1, 0, 0],
  916. [0, 1, 1, 1, 1, 1, 1, 0],
  917. [0, 1, 1, 0, 0, 1, 1, 0],
  918. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  919. out = ndimage.binary_erosion(data, struct, border_value=1)
  920. assert_array_almost_equal(out, expected)
  921. @make_xp_test_case(ndimage.binary_erosion)
  922. @pytest.mark.parametrize('dtype', types)
  923. def test_binary_erosion24(self, dtype, xp):
  924. dtype = getattr(xp, dtype)
  925. struct = xp.asarray([[0, 1],
  926. [1, 1]])
  927. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  928. [0, 0, 0, 0, 0, 0, 0, 0],
  929. [0, 0, 0, 0, 0, 0, 0, 0],
  930. [0, 0, 0, 0, 0, 1, 1, 1],
  931. [0, 0, 0, 1, 1, 1, 0, 0],
  932. [0, 0, 1, 1, 1, 1, 0, 0],
  933. [0, 0, 1, 0, 0, 0, 1, 0],
  934. [0, 0, 0, 0, 0, 0, 0, 0]]
  935. expected = xp.asarray(expected)
  936. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0],
  937. [0, 1, 0, 0, 0, 0, 0, 0],
  938. [0, 0, 0, 0, 0, 1, 1, 1],
  939. [0, 0, 1, 1, 1, 1, 1, 1],
  940. [0, 0, 1, 1, 1, 1, 0, 0],
  941. [0, 1, 1, 1, 1, 1, 1, 0],
  942. [0, 1, 1, 0, 0, 1, 1, 0],
  943. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  944. out = ndimage.binary_erosion(data, struct, border_value=1)
  945. assert_array_almost_equal(out, expected)
  946. @make_xp_test_case(ndimage.binary_erosion)
  947. @pytest.mark.parametrize('dtype', types)
  948. def test_binary_erosion25(self, dtype, xp):
  949. dtype = getattr(xp, dtype)
  950. struct = [[0, 1, 0],
  951. [1, 0, 1],
  952. [0, 1, 0]]
  953. struct = xp.asarray(struct)
  954. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  955. [0, 0, 0, 0, 0, 0, 0, 0],
  956. [0, 0, 0, 0, 0, 0, 0, 0],
  957. [0, 0, 0, 0, 0, 1, 0, 0],
  958. [0, 0, 0, 1, 0, 0, 0, 0],
  959. [0, 0, 1, 0, 0, 1, 0, 0],
  960. [0, 0, 0, 0, 0, 0, 0, 0],
  961. [0, 0, 0, 0, 0, 0, 0, 0]]
  962. expected = xp.asarray(expected)
  963. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0],
  964. [0, 1, 0, 0, 0, 0, 0, 0],
  965. [0, 0, 0, 0, 0, 1, 1, 1],
  966. [0, 0, 1, 1, 1, 0, 1, 1],
  967. [0, 0, 1, 0, 1, 1, 0, 0],
  968. [0, 1, 0, 1, 1, 1, 1, 0],
  969. [0, 1, 1, 0, 0, 1, 1, 0],
  970. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  971. out = ndimage.binary_erosion(data, struct, border_value=1)
  972. assert_array_almost_equal(out, expected)
  973. @make_xp_test_case(ndimage.binary_erosion)
  974. @pytest.mark.parametrize('dtype', types)
  975. def test_binary_erosion26(self, dtype, xp):
  976. dtype = getattr(xp, dtype)
  977. struct = [[0, 1, 0],
  978. [1, 0, 1],
  979. [0, 1, 0]]
  980. struct = xp.asarray(struct)
  981. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  982. [0, 0, 0, 0, 0, 0, 0, 1],
  983. [0, 0, 0, 0, 1, 0, 0, 1],
  984. [0, 0, 1, 0, 0, 0, 0, 0],
  985. [0, 1, 0, 0, 1, 0, 0, 0],
  986. [0, 0, 0, 0, 0, 0, 0, 0],
  987. [0, 0, 0, 0, 0, 0, 0, 0],
  988. [0, 0, 0, 0, 0, 0, 0, 1]]
  989. expected = xp.asarray(expected)
  990. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0],
  991. [0, 1, 0, 0, 0, 0, 0, 0],
  992. [0, 0, 0, 0, 0, 1, 1, 1],
  993. [0, 0, 1, 1, 1, 0, 1, 1],
  994. [0, 0, 1, 0, 1, 1, 0, 0],
  995. [0, 1, 0, 1, 1, 1, 1, 0],
  996. [0, 1, 1, 0, 0, 1, 1, 0],
  997. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  998. out = ndimage.binary_erosion(data, struct, border_value=1,
  999. origin=(-1, -1))
  1000. assert_array_almost_equal(out, expected)
  1001. @xfail_xp_backends(
  1002. "cupy", reason="CuPy: NotImplementedError: only brute_force iteration"
  1003. )
  1004. @make_xp_test_case(ndimage.binary_erosion)
  1005. def test_binary_erosion27(self, xp):
  1006. struct = [[0, 1, 0],
  1007. [1, 1, 1],
  1008. [0, 1, 0]]
  1009. struct = xp.asarray(struct)
  1010. expected = [[0, 0, 0, 0, 0, 0, 0],
  1011. [0, 0, 0, 0, 0, 0, 0],
  1012. [0, 0, 0, 0, 0, 0, 0],
  1013. [0, 0, 0, 1, 0, 0, 0],
  1014. [0, 0, 0, 0, 0, 0, 0],
  1015. [0, 0, 0, 0, 0, 0, 0],
  1016. [0, 0, 0, 0, 0, 0, 0]]
  1017. expected = xp.asarray(expected)
  1018. data = np.asarray([[0, 0, 0, 0, 0, 0, 0],
  1019. [0, 0, 0, 1, 0, 0, 0],
  1020. [0, 0, 1, 1, 1, 0, 0],
  1021. [0, 1, 1, 1, 1, 1, 0],
  1022. [0, 0, 1, 1, 1, 0, 0],
  1023. [0, 0, 0, 1, 0, 0, 0],
  1024. [0, 0, 0, 0, 0, 0, 0]], dtype=bool)
  1025. data = xp.asarray(data)
  1026. out = ndimage.binary_erosion(data, struct, border_value=1,
  1027. iterations=2)
  1028. assert_array_almost_equal(out, expected)
  1029. @skip_xp_backends(np_only=True, exceptions=["cupy"],
  1030. reason='inplace out= arguments are numpy-specific')
  1031. @xfail_xp_backends("cupy",
  1032. reason="NotImplementedError: only brute_force iteration")
  1033. @make_xp_test_case(ndimage.binary_erosion)
  1034. def test_binary_erosion28(self, xp):
  1035. struct = [[0, 1, 0],
  1036. [1, 1, 1],
  1037. [0, 1, 0]]
  1038. struct = xp.asarray(struct)
  1039. expected = [[0, 0, 0, 0, 0, 0, 0],
  1040. [0, 0, 0, 0, 0, 0, 0],
  1041. [0, 0, 0, 0, 0, 0, 0],
  1042. [0, 0, 0, 1, 0, 0, 0],
  1043. [0, 0, 0, 0, 0, 0, 0],
  1044. [0, 0, 0, 0, 0, 0, 0],
  1045. [0, 0, 0, 0, 0, 0, 0]]
  1046. expected = np.asarray(expected, dtype=bool)
  1047. expected = xp.asarray(expected)
  1048. data = np.asarray([[0, 0, 0, 0, 0, 0, 0],
  1049. [0, 0, 0, 1, 0, 0, 0],
  1050. [0, 0, 1, 1, 1, 0, 0],
  1051. [0, 1, 1, 1, 1, 1, 0],
  1052. [0, 0, 1, 1, 1, 0, 0],
  1053. [0, 0, 0, 1, 0, 0, 0],
  1054. [0, 0, 0, 0, 0, 0, 0]], dtype=bool)
  1055. data = xp.asarray(data)
  1056. out = np.zeros(data.shape, dtype=bool)
  1057. out = xp.asarray(out)
  1058. ndimage.binary_erosion(data, struct, border_value=1,
  1059. iterations=2, output=out)
  1060. assert_array_almost_equal(out, expected)
  1061. @xfail_xp_backends(
  1062. "cupy", reason="CuPy: NotImplementedError: only brute_force iteration"
  1063. )
  1064. @make_xp_test_case(ndimage.binary_erosion)
  1065. def test_binary_erosion29(self, xp):
  1066. struct = [[0, 1, 0],
  1067. [1, 1, 1],
  1068. [0, 1, 0]]
  1069. struct = xp.asarray(struct)
  1070. expected = [[0, 0, 0, 0, 0, 0, 0],
  1071. [0, 0, 0, 0, 0, 0, 0],
  1072. [0, 0, 0, 0, 0, 0, 0],
  1073. [0, 0, 0, 1, 0, 0, 0],
  1074. [0, 0, 0, 0, 0, 0, 0],
  1075. [0, 0, 0, 0, 0, 0, 0],
  1076. [0, 0, 0, 0, 0, 0, 0]]
  1077. expected = xp.asarray(expected)
  1078. data = np.asarray([[0, 0, 0, 1, 0, 0, 0],
  1079. [0, 0, 1, 1, 1, 0, 0],
  1080. [0, 1, 1, 1, 1, 1, 0],
  1081. [1, 1, 1, 1, 1, 1, 1],
  1082. [0, 1, 1, 1, 1, 1, 0],
  1083. [0, 0, 1, 1, 1, 0, 0],
  1084. [0, 0, 0, 1, 0, 0, 0]], dtype=bool)
  1085. data = xp.asarray(data)
  1086. out = ndimage.binary_erosion(data, struct,
  1087. border_value=1, iterations=3)
  1088. assert_array_almost_equal(out, expected)
  1089. @skip_xp_backends(np_only=True, exceptions=["cupy"],
  1090. reason='inplace out= arguments are numpy-specific')
  1091. @xfail_xp_backends("cupy",
  1092. reason="NotImplementedError: only brute_force iteration")
  1093. @make_xp_test_case(ndimage.binary_erosion)
  1094. def test_binary_erosion30(self, xp):
  1095. struct = [[0, 1, 0],
  1096. [1, 1, 1],
  1097. [0, 1, 0]]
  1098. struct = xp.asarray(struct)
  1099. expected = [[0, 0, 0, 0, 0, 0, 0],
  1100. [0, 0, 0, 0, 0, 0, 0],
  1101. [0, 0, 0, 0, 0, 0, 0],
  1102. [0, 0, 0, 1, 0, 0, 0],
  1103. [0, 0, 0, 0, 0, 0, 0],
  1104. [0, 0, 0, 0, 0, 0, 0],
  1105. [0, 0, 0, 0, 0, 0, 0]]
  1106. expected = np.asarray(expected, dtype=bool)
  1107. expected = xp.asarray(expected)
  1108. data = np.asarray([[0, 0, 0, 1, 0, 0, 0],
  1109. [0, 0, 1, 1, 1, 0, 0],
  1110. [0, 1, 1, 1, 1, 1, 0],
  1111. [1, 1, 1, 1, 1, 1, 1],
  1112. [0, 1, 1, 1, 1, 1, 0],
  1113. [0, 0, 1, 1, 1, 0, 0],
  1114. [0, 0, 0, 1, 0, 0, 0]], dtype=bool)
  1115. data = xp.asarray(data)
  1116. out = np.zeros(data.shape, dtype=bool)
  1117. out = xp.asarray(out)
  1118. ndimage.binary_erosion(data, struct, border_value=1,
  1119. iterations=3, output=out)
  1120. assert_array_almost_equal(out, expected)
  1121. # test with output memory overlap
  1122. ndimage.binary_erosion(data, struct, border_value=1,
  1123. iterations=3, output=data)
  1124. assert_array_almost_equal(data, expected)
  1125. @skip_xp_backends(np_only=True, exceptions=["cupy"],
  1126. reason='inplace out= arguments are numpy-specific')
  1127. @make_xp_test_case(ndimage.binary_erosion)
  1128. def test_binary_erosion31(self, xp):
  1129. struct = [[0, 1, 0],
  1130. [1, 1, 1],
  1131. [0, 1, 0]]
  1132. struct = xp.asarray(struct)
  1133. expected = [[0, 0, 1, 0, 0, 0, 0],
  1134. [0, 1, 1, 1, 0, 0, 0],
  1135. [1, 1, 1, 1, 1, 0, 1],
  1136. [0, 1, 1, 1, 0, 0, 0],
  1137. [0, 0, 1, 0, 0, 0, 0],
  1138. [0, 0, 0, 0, 0, 0, 0],
  1139. [0, 0, 1, 0, 0, 0, 1]]
  1140. expected = np.asarray(expected, dtype=bool)
  1141. expected = xp.asarray(expected)
  1142. data = np.asarray([[0, 0, 0, 1, 0, 0, 0],
  1143. [0, 0, 1, 1, 1, 0, 0],
  1144. [0, 1, 1, 1, 1, 1, 0],
  1145. [1, 1, 1, 1, 1, 1, 1],
  1146. [0, 1, 1, 1, 1, 1, 0],
  1147. [0, 0, 1, 1, 1, 0, 0],
  1148. [0, 0, 0, 1, 0, 0, 0]], dtype=bool)
  1149. data = xp.asarray(data)
  1150. out = np.zeros(data.shape, dtype=bool)
  1151. out = xp.asarray(out)
  1152. ndimage.binary_erosion(data, struct, border_value=1,
  1153. iterations=1, output=out, origin=(-1, -1))
  1154. assert_array_almost_equal(out, expected)
  1155. @xfail_xp_backends("cupy",
  1156. reason="NotImplementedError: only brute_force iteration")
  1157. @make_xp_test_case(ndimage.binary_erosion)
  1158. def test_binary_erosion32(self, xp):
  1159. struct = [[0, 1, 0],
  1160. [1, 1, 1],
  1161. [0, 1, 0]]
  1162. struct = xp.asarray(struct)
  1163. expected = [[0, 0, 0, 0, 0, 0, 0],
  1164. [0, 0, 0, 0, 0, 0, 0],
  1165. [0, 0, 0, 0, 0, 0, 0],
  1166. [0, 0, 0, 1, 0, 0, 0],
  1167. [0, 0, 0, 0, 0, 0, 0],
  1168. [0, 0, 0, 0, 0, 0, 0],
  1169. [0, 0, 0, 0, 0, 0, 0]]
  1170. expected = xp.asarray(expected)
  1171. data = np.asarray([[0, 0, 0, 0, 0, 0, 0],
  1172. [0, 0, 0, 1, 0, 0, 0],
  1173. [0, 0, 1, 1, 1, 0, 0],
  1174. [0, 1, 1, 1, 1, 1, 0],
  1175. [0, 0, 1, 1, 1, 0, 0],
  1176. [0, 0, 0, 1, 0, 0, 0],
  1177. [0, 0, 0, 0, 0, 0, 0]], dtype=bool)
  1178. data = xp.asarray(data)
  1179. out = ndimage.binary_erosion(data, struct,
  1180. border_value=1, iterations=2)
  1181. assert_array_almost_equal(out, expected)
  1182. @xfail_xp_backends("cupy",
  1183. reason="NotImplementedError: only brute_force iteration")
  1184. @make_xp_test_case(ndimage.binary_erosion)
  1185. def test_binary_erosion33(self, xp):
  1186. struct = [[0, 1, 0],
  1187. [1, 1, 1],
  1188. [0, 1, 0]]
  1189. struct = xp.asarray(struct)
  1190. expected = [[0, 0, 0, 0, 0, 1, 1],
  1191. [0, 0, 0, 0, 0, 0, 1],
  1192. [0, 0, 0, 0, 0, 0, 0],
  1193. [0, 0, 0, 0, 0, 0, 0],
  1194. [0, 0, 0, 0, 0, 0, 0],
  1195. [0, 0, 0, 0, 0, 0, 0],
  1196. [0, 0, 0, 0, 0, 0, 0]]
  1197. expected = xp.asarray(expected)
  1198. mask = [[1, 1, 1, 1, 1, 0, 0],
  1199. [1, 1, 1, 1, 1, 1, 0],
  1200. [1, 1, 1, 1, 1, 1, 1],
  1201. [1, 1, 1, 1, 1, 1, 1],
  1202. [1, 1, 1, 1, 1, 1, 1],
  1203. [1, 1, 1, 1, 1, 1, 1],
  1204. [1, 1, 1, 1, 1, 1, 1]]
  1205. mask = xp.asarray(mask)
  1206. data = np.asarray([[0, 0, 0, 0, 0, 1, 1],
  1207. [0, 0, 0, 1, 0, 0, 1],
  1208. [0, 0, 1, 1, 1, 0, 0],
  1209. [0, 0, 1, 1, 1, 0, 0],
  1210. [0, 0, 1, 1, 1, 0, 0],
  1211. [0, 0, 0, 1, 0, 0, 0],
  1212. [0, 0, 0, 0, 0, 0, 0]], dtype=bool)
  1213. data = xp.asarray(data)
  1214. out = ndimage.binary_erosion(data, struct,
  1215. border_value=1, mask=mask, iterations=-1)
  1216. assert_array_almost_equal(out, expected)
  1217. @make_xp_test_case(ndimage.binary_erosion)
  1218. def test_binary_erosion34(self, xp):
  1219. struct = [[0, 1, 0],
  1220. [1, 1, 1],
  1221. [0, 1, 0]]
  1222. struct = xp.asarray(struct)
  1223. expected = [[0, 0, 0, 0, 0, 0, 0],
  1224. [0, 0, 0, 1, 0, 0, 0],
  1225. [0, 0, 0, 1, 0, 0, 0],
  1226. [0, 1, 1, 1, 1, 1, 0],
  1227. [0, 0, 0, 1, 0, 0, 0],
  1228. [0, 0, 0, 1, 0, 0, 0],
  1229. [0, 0, 0, 0, 0, 0, 0]]
  1230. expected = xp.asarray(expected)
  1231. mask = [[0, 0, 0, 0, 0, 0, 0],
  1232. [0, 0, 0, 0, 0, 0, 0],
  1233. [0, 0, 1, 1, 1, 0, 0],
  1234. [0, 0, 1, 0, 1, 0, 0],
  1235. [0, 0, 1, 1, 1, 0, 0],
  1236. [0, 0, 0, 0, 0, 0, 0],
  1237. [0, 0, 0, 0, 0, 0, 0]]
  1238. mask = xp.asarray(mask)
  1239. data = np.asarray([[0, 0, 0, 0, 0, 0, 0],
  1240. [0, 0, 0, 1, 0, 0, 0],
  1241. [0, 0, 1, 1, 1, 0, 0],
  1242. [0, 1, 1, 1, 1, 1, 0],
  1243. [0, 0, 1, 1, 1, 0, 0],
  1244. [0, 0, 0, 1, 0, 0, 0],
  1245. [0, 0, 0, 0, 0, 0, 0]], dtype=bool)
  1246. data = xp.asarray(data)
  1247. out = ndimage.binary_erosion(data, struct,
  1248. border_value=1, mask=mask)
  1249. assert_array_almost_equal(out, expected)
  1250. @skip_xp_backends(np_only=True, exceptions=["cupy"],
  1251. reason='inplace out= arguments are numpy-specific')
  1252. @make_xp_test_case(ndimage.binary_erosion)
  1253. def test_binary_erosion35(self, xp):
  1254. struct = [[0, 1, 0],
  1255. [1, 1, 1],
  1256. [0, 1, 0]]
  1257. struct = xp.asarray(struct)
  1258. mask = [[0, 0, 0, 0, 0, 0, 0],
  1259. [0, 0, 0, 0, 0, 0, 0],
  1260. [0, 0, 1, 1, 1, 0, 0],
  1261. [0, 0, 1, 0, 1, 0, 0],
  1262. [0, 0, 1, 1, 1, 0, 0],
  1263. [0, 0, 0, 0, 0, 0, 0],
  1264. [0, 0, 0, 0, 0, 0, 0]]
  1265. mask = np.asarray(mask, dtype=bool)
  1266. mask = xp.asarray(mask)
  1267. data = np.asarray([[0, 0, 0, 1, 0, 0, 0],
  1268. [0, 0, 1, 1, 1, 0, 0],
  1269. [0, 1, 1, 1, 1, 1, 0],
  1270. [1, 1, 1, 1, 1, 1, 1],
  1271. [0, 1, 1, 1, 1, 1, 0],
  1272. [0, 0, 1, 1, 1, 0, 0],
  1273. [0, 0, 0, 1, 0, 0, 0]], dtype=bool)
  1274. data = xp.asarray(data)
  1275. tmp = [[0, 0, 1, 0, 0, 0, 0],
  1276. [0, 1, 1, 1, 0, 0, 0],
  1277. [1, 1, 1, 1, 1, 0, 1],
  1278. [0, 1, 1, 1, 0, 0, 0],
  1279. [0, 0, 1, 0, 0, 0, 0],
  1280. [0, 0, 0, 0, 0, 0, 0],
  1281. [0, 0, 1, 0, 0, 0, 1]]
  1282. tmp = np.asarray(tmp, dtype=bool)
  1283. tmp = xp.asarray(tmp)
  1284. expected = xp.logical_and(tmp, mask)
  1285. tmp = xp.logical_and(data, xp.logical_not(mask))
  1286. expected = xp.logical_or(expected, tmp)
  1287. out = np.zeros(data.shape, dtype=bool)
  1288. out = xp.asarray(out)
  1289. ndimage.binary_erosion(data, struct, border_value=1,
  1290. iterations=1, output=out,
  1291. origin=(-1, -1), mask=mask)
  1292. assert_array_almost_equal(out, expected)
  1293. @xfail_xp_backends("cupy",
  1294. reason="NotImplementedError: only brute_force iteration")
  1295. @make_xp_test_case(ndimage.binary_erosion)
  1296. def test_binary_erosion36(self, xp):
  1297. struct = [[0, 1, 0],
  1298. [1, 0, 1],
  1299. [0, 1, 0]]
  1300. struct = xp.asarray(struct)
  1301. mask = [[0, 0, 0, 0, 0, 0, 0, 0],
  1302. [0, 0, 0, 0, 0, 0, 0, 0],
  1303. [0, 0, 1, 1, 1, 0, 0, 0],
  1304. [0, 0, 1, 0, 1, 0, 0, 0],
  1305. [0, 0, 1, 1, 1, 0, 0, 0],
  1306. [0, 0, 1, 1, 1, 0, 0, 0],
  1307. [0, 0, 1, 1, 1, 0, 0, 0],
  1308. [0, 0, 0, 0, 0, 0, 0, 0]]
  1309. mask = np.asarray(mask, dtype=bool)
  1310. mask = xp.asarray(mask)
  1311. tmp = [[0, 0, 0, 0, 0, 0, 0, 0],
  1312. [0, 0, 0, 0, 0, 0, 0, 1],
  1313. [0, 0, 0, 0, 1, 0, 0, 1],
  1314. [0, 0, 1, 0, 0, 0, 0, 0],
  1315. [0, 1, 0, 0, 1, 0, 0, 0],
  1316. [0, 0, 0, 0, 0, 0, 0, 0],
  1317. [0, 0, 0, 0, 0, 0, 0, 0],
  1318. [0, 0, 0, 0, 0, 0, 0, 1]]
  1319. tmp = np.asarray(tmp, dtype=bool)
  1320. tmp = xp.asarray(tmp)
  1321. data = np.asarray([[0, 0, 0, 0, 0, 0, 0, 0],
  1322. [0, 1, 0, 0, 0, 0, 0, 0],
  1323. [0, 0, 0, 0, 0, 1, 1, 1],
  1324. [0, 0, 1, 1, 1, 0, 1, 1],
  1325. [0, 0, 1, 0, 1, 1, 0, 0],
  1326. [0, 1, 0, 1, 1, 1, 1, 0],
  1327. [0, 1, 1, 0, 0, 1, 1, 0],
  1328. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=bool)
  1329. data = xp.asarray(data)
  1330. expected = xp.logical_and(tmp, mask)
  1331. tmp = xp.logical_and(data, xp.logical_not(mask))
  1332. expected = xp.logical_or(expected, tmp)
  1333. out = ndimage.binary_erosion(data, struct, mask=mask,
  1334. border_value=1, origin=(-1, -1))
  1335. assert_array_almost_equal(out, expected)
  1336. @skip_xp_backends(np_only=True, exceptions=["cupy"],
  1337. reason='inplace out= arguments are numpy-specific')
  1338. @xfail_xp_backends("cupy",
  1339. reason="NotImplementedError: only brute_force iteration")
  1340. @make_xp_test_case(ndimage.binary_erosion)
  1341. def test_binary_erosion37(self, xp):
  1342. a = np.asarray([[1, 0, 1],
  1343. [0, 1, 0],
  1344. [1, 0, 1]], dtype=bool)
  1345. a = xp.asarray(a)
  1346. b = xp.zeros_like(a)
  1347. out = ndimage.binary_erosion(a, structure=a, output=b, iterations=0,
  1348. border_value=True, brute_force=True)
  1349. assert out is b
  1350. xp_assert_equal(
  1351. ndimage.binary_erosion(a, structure=a, iterations=0,
  1352. border_value=True),
  1353. b)
  1354. @make_xp_test_case(ndimage.binary_erosion)
  1355. def test_binary_erosion38(self, xp):
  1356. data = np.asarray([[1, 0, 1],
  1357. [0, 1, 0],
  1358. [1, 0, 1]], dtype=bool)
  1359. data = xp.asarray(data)
  1360. iterations = 2.0
  1361. with assert_raises(TypeError):
  1362. _ = ndimage.binary_erosion(data, iterations=iterations)
  1363. @skip_xp_backends(np_only=True, exceptions=["cupy"],
  1364. reason='inplace out= arguments are numpy-specific')
  1365. @xfail_xp_backends("cupy",
  1366. reason="NotImplementedError: only brute_force iteration")
  1367. @make_xp_test_case(ndimage.binary_erosion)
  1368. def test_binary_erosion39(self, xp):
  1369. iterations = np.int32(3)
  1370. struct = [[0, 1, 0],
  1371. [1, 1, 1],
  1372. [0, 1, 0]]
  1373. struct = xp.asarray(struct)
  1374. expected = [[0, 0, 0, 0, 0, 0, 0],
  1375. [0, 0, 0, 0, 0, 0, 0],
  1376. [0, 0, 0, 0, 0, 0, 0],
  1377. [0, 0, 0, 1, 0, 0, 0],
  1378. [0, 0, 0, 0, 0, 0, 0],
  1379. [0, 0, 0, 0, 0, 0, 0],
  1380. [0, 0, 0, 0, 0, 0, 0]]
  1381. expected = xp.asarray(expected, dtype=bool)
  1382. expected = xp.asarray(expected)
  1383. data = np.asarray([[0, 0, 0, 1, 0, 0, 0],
  1384. [0, 0, 1, 1, 1, 0, 0],
  1385. [0, 1, 1, 1, 1, 1, 0],
  1386. [1, 1, 1, 1, 1, 1, 1],
  1387. [0, 1, 1, 1, 1, 1, 0],
  1388. [0, 0, 1, 1, 1, 0, 0],
  1389. [0, 0, 0, 1, 0, 0, 0]], dtype=bool)
  1390. data = xp.asarray(data)
  1391. out = np.zeros(data.shape, dtype=bool)
  1392. out = xp.asarray(out)
  1393. ndimage.binary_erosion(data, struct, border_value=1,
  1394. iterations=iterations, output=out)
  1395. assert_array_almost_equal(out, expected)
  1396. @skip_xp_backends(np_only=True, exceptions=["cupy"],
  1397. reason='inplace out= arguments are numpy-specific')
  1398. @xfail_xp_backends("cupy",
  1399. reason="NotImplementedError: only brute_force iteration")
  1400. @make_xp_test_case(ndimage.binary_erosion)
  1401. def test_binary_erosion40(self, xp):
  1402. iterations = np.int64(3)
  1403. struct = [[0, 1, 0],
  1404. [1, 1, 1],
  1405. [0, 1, 0]]
  1406. struct = xp.asarray(struct)
  1407. expected = [[0, 0, 0, 0, 0, 0, 0],
  1408. [0, 0, 0, 0, 0, 0, 0],
  1409. [0, 0, 0, 0, 0, 0, 0],
  1410. [0, 0, 0, 1, 0, 0, 0],
  1411. [0, 0, 0, 0, 0, 0, 0],
  1412. [0, 0, 0, 0, 0, 0, 0],
  1413. [0, 0, 0, 0, 0, 0, 0]]
  1414. expected = np.asarray(expected, dtype=bool)
  1415. expected = xp.asarray(expected)
  1416. data = np.asarray([[0, 0, 0, 1, 0, 0, 0],
  1417. [0, 0, 1, 1, 1, 0, 0],
  1418. [0, 1, 1, 1, 1, 1, 0],
  1419. [1, 1, 1, 1, 1, 1, 1],
  1420. [0, 1, 1, 1, 1, 1, 0],
  1421. [0, 0, 1, 1, 1, 0, 0],
  1422. [0, 0, 0, 1, 0, 0, 0]], dtype=bool)
  1423. data = xp.asarray(data)
  1424. out = np.zeros(data.shape, dtype=bool)
  1425. out = xp.asarray(out)
  1426. ndimage.binary_erosion(data, struct, border_value=1,
  1427. iterations=iterations, output=out)
  1428. assert_array_almost_equal(out, expected)
  1429. @pytest.mark.parametrize('dtype', types)
  1430. @make_xp_test_case(ndimage.binary_dilation)
  1431. def test_binary_dilation01(self, dtype, xp):
  1432. dtype = getattr(xp, dtype)
  1433. data = xp.ones([], dtype=dtype)
  1434. out = ndimage.binary_dilation(data)
  1435. assert out == xp.asarray(1, dtype=out.dtype)
  1436. @pytest.mark.parametrize('dtype', types)
  1437. @make_xp_test_case(ndimage.binary_dilation)
  1438. def test_binary_dilation02(self, dtype, xp):
  1439. dtype = getattr(xp, dtype)
  1440. data = xp.zeros([], dtype=dtype)
  1441. out = ndimage.binary_dilation(data)
  1442. assert out == xp.asarray(False)
  1443. @pytest.mark.parametrize('dtype', types)
  1444. @make_xp_test_case(ndimage.binary_dilation)
  1445. def test_binary_dilation03(self, dtype, xp):
  1446. dtype = getattr(xp, dtype)
  1447. data = xp.ones([1], dtype=dtype)
  1448. out = ndimage.binary_dilation(data)
  1449. assert_array_almost_equal(out, xp.asarray([1], dtype=out.dtype))
  1450. @pytest.mark.parametrize('dtype', types)
  1451. @make_xp_test_case(ndimage.binary_dilation)
  1452. def test_binary_dilation04(self, dtype, xp):
  1453. dtype = getattr(xp, dtype)
  1454. data = xp.zeros([1], dtype=dtype)
  1455. out = ndimage.binary_dilation(data)
  1456. assert_array_almost_equal(out, xp.asarray([0]))
  1457. @pytest.mark.parametrize('dtype', types)
  1458. @make_xp_test_case(ndimage.binary_dilation)
  1459. def test_binary_dilation05(self, dtype, xp):
  1460. dtype = getattr(xp, dtype)
  1461. data = xp.ones([3], dtype=dtype)
  1462. out = ndimage.binary_dilation(data)
  1463. assert_array_almost_equal(out, xp.asarray([1, 1, 1]))
  1464. @pytest.mark.parametrize('dtype', types)
  1465. @make_xp_test_case(ndimage.binary_dilation)
  1466. def test_binary_dilation05_broadcasted(self, dtype, xp):
  1467. dtype = getattr(xp, dtype)
  1468. data = xp.ones((1, ), dtype=dtype)
  1469. data = xp.broadcast_to(data, (3,))
  1470. out = ndimage.binary_dilation(data)
  1471. assert_array_almost_equal(out, xp.asarray([1, 1, 1]))
  1472. @pytest.mark.parametrize('dtype', types)
  1473. @make_xp_test_case(ndimage.binary_dilation)
  1474. def test_binary_dilation06(self, dtype, xp):
  1475. dtype = getattr(xp, dtype)
  1476. data = xp.zeros([3], dtype=dtype)
  1477. out = ndimage.binary_dilation(data)
  1478. assert_array_almost_equal(out, xp.asarray([0, 0, 0]))
  1479. @pytest.mark.parametrize('dtype', types)
  1480. @make_xp_test_case(ndimage.binary_dilation)
  1481. def test_binary_dilation07(self, dtype, xp):
  1482. data = np.zeros([3], dtype=dtype)
  1483. data[1] = 1
  1484. data = xp.asarray(data)
  1485. out = ndimage.binary_dilation(data)
  1486. assert_array_almost_equal(out, xp.asarray([1, 1, 1]))
  1487. @pytest.mark.parametrize('dtype', types)
  1488. @make_xp_test_case(ndimage.binary_dilation)
  1489. def test_binary_dilation08(self, dtype, xp):
  1490. data = np.zeros([5], dtype=dtype)
  1491. data[1] = 1
  1492. data[3] = 1
  1493. data = xp.asarray(data)
  1494. out = ndimage.binary_dilation(data)
  1495. assert_array_almost_equal(out, xp.asarray([1, 1, 1, 1, 1]))
  1496. @pytest.mark.parametrize('dtype', types)
  1497. @make_xp_test_case(ndimage.binary_dilation)
  1498. def test_binary_dilation09(self, dtype, xp):
  1499. data = np.zeros([5], dtype=dtype)
  1500. data[1] = 1
  1501. data = xp.asarray(data)
  1502. out = ndimage.binary_dilation(data)
  1503. assert_array_almost_equal(out, xp.asarray([1, 1, 1, 0, 0]))
  1504. @pytest.mark.parametrize('dtype', types)
  1505. @make_xp_test_case(ndimage.binary_dilation)
  1506. def test_binary_dilation10(self, dtype, xp):
  1507. data = np.zeros([5], dtype=dtype)
  1508. data[1] = 1
  1509. data = xp.asarray(data)
  1510. out = ndimage.binary_dilation(data, origin=-1)
  1511. assert_array_almost_equal(out, xp.asarray([0, 1, 1, 1, 0]))
  1512. @pytest.mark.parametrize('dtype', types)
  1513. @make_xp_test_case(ndimage.binary_dilation)
  1514. def test_binary_dilation11(self, dtype, xp):
  1515. data = np.zeros([5], dtype=dtype)
  1516. data[1] = 1
  1517. data = xp.asarray(data)
  1518. out = ndimage.binary_dilation(data, origin=1)
  1519. assert_array_almost_equal(out, xp.asarray([1, 1, 0, 0, 0]))
  1520. @pytest.mark.parametrize('dtype', types)
  1521. @make_xp_test_case(ndimage.binary_dilation)
  1522. def test_binary_dilation12(self, dtype, xp):
  1523. data = np.zeros([5], dtype=dtype)
  1524. data[1] = 1
  1525. data = xp.asarray(data)
  1526. struct = xp.asarray([1, 0, 1])
  1527. out = ndimage.binary_dilation(data, struct)
  1528. assert_array_almost_equal(out, xp.asarray([1, 0, 1, 0, 0]))
  1529. @pytest.mark.parametrize('dtype', types)
  1530. @make_xp_test_case(ndimage.binary_dilation)
  1531. def test_binary_dilation13(self, dtype, xp):
  1532. data = np.zeros([5], dtype=dtype)
  1533. data[1] = 1
  1534. data = xp.asarray(data)
  1535. struct = xp.asarray([1, 0, 1])
  1536. out = ndimage.binary_dilation(data, struct, border_value=1)
  1537. assert_array_almost_equal(out, xp.asarray([1, 0, 1, 0, 1]))
  1538. @pytest.mark.parametrize('dtype', types)
  1539. @make_xp_test_case(ndimage.binary_dilation)
  1540. def test_binary_dilation14(self, dtype, xp):
  1541. data = np.zeros([5], dtype=dtype)
  1542. data[1] = 1
  1543. data = xp.asarray(data)
  1544. struct = xp.asarray([1, 0, 1])
  1545. out = ndimage.binary_dilation(data, struct, origin=-1)
  1546. assert_array_almost_equal(out, xp.asarray([0, 1, 0, 1, 0]))
  1547. @pytest.mark.parametrize('dtype', types)
  1548. @make_xp_test_case(ndimage.binary_dilation)
  1549. def test_binary_dilation15(self, dtype, xp):
  1550. data = np.zeros([5], dtype=dtype)
  1551. data[1] = 1
  1552. data = xp.asarray(data)
  1553. struct = xp.asarray([1, 0, 1])
  1554. out = ndimage.binary_dilation(data, struct,
  1555. origin=-1, border_value=1)
  1556. assert_array_almost_equal(out, xp.asarray([1, 1, 0, 1, 0]))
  1557. @pytest.mark.parametrize('dtype', types)
  1558. @make_xp_test_case(ndimage.binary_dilation)
  1559. def test_binary_dilation16(self, dtype, xp):
  1560. dtype = getattr(xp, dtype)
  1561. data = xp.ones([1, 1], dtype=dtype)
  1562. out = ndimage.binary_dilation(data)
  1563. assert_array_almost_equal(out, xp.asarray([[1]]))
  1564. @pytest.mark.parametrize('dtype', types)
  1565. @make_xp_test_case(ndimage.binary_dilation)
  1566. def test_binary_dilation17(self, dtype, xp):
  1567. dtype = getattr(xp, dtype)
  1568. data = xp.zeros([1, 1], dtype=dtype)
  1569. out = ndimage.binary_dilation(data)
  1570. assert_array_almost_equal(out, xp.asarray([[0]]))
  1571. @pytest.mark.parametrize('dtype', types)
  1572. @make_xp_test_case(ndimage.binary_dilation)
  1573. def test_binary_dilation18(self, dtype, xp):
  1574. dtype = getattr(xp, dtype)
  1575. data = xp.ones([1, 3], dtype=dtype)
  1576. out = ndimage.binary_dilation(data)
  1577. assert_array_almost_equal(out, xp.asarray([[1, 1, 1]]))
  1578. @pytest.mark.parametrize('dtype', types)
  1579. @make_xp_test_case(ndimage.binary_dilation)
  1580. def test_binary_dilation19(self, dtype, xp):
  1581. dtype = getattr(xp, dtype)
  1582. data = xp.ones([3, 3], dtype=dtype)
  1583. out = ndimage.binary_dilation(data)
  1584. assert_array_almost_equal(out, xp.asarray([[1, 1, 1],
  1585. [1, 1, 1],
  1586. [1, 1, 1]]))
  1587. @pytest.mark.parametrize('dtype', types)
  1588. @make_xp_test_case(ndimage.binary_dilation)
  1589. def test_binary_dilation20(self, dtype, xp):
  1590. data = np.zeros([3, 3], dtype=dtype)
  1591. data[1, 1] = 1
  1592. data = xp.asarray(data)
  1593. out = ndimage.binary_dilation(data)
  1594. assert_array_almost_equal(out, xp.asarray([[0, 1, 0],
  1595. [1, 1, 1],
  1596. [0, 1, 0]]))
  1597. @pytest.mark.parametrize('dtype', types)
  1598. @make_xp_test_case(ndimage.binary_dilation)
  1599. def test_binary_dilation21(self, dtype, xp):
  1600. struct = ndimage.generate_binary_structure(2, 2)
  1601. struct = xp.asarray(struct)
  1602. data = np.zeros([3, 3], dtype=dtype)
  1603. data[1, 1] = 1
  1604. data = xp.asarray(data)
  1605. out = ndimage.binary_dilation(data, struct)
  1606. assert_array_almost_equal(out, xp.asarray([[1, 1, 1],
  1607. [1, 1, 1],
  1608. [1, 1, 1]]))
  1609. @pytest.mark.parametrize('dtype', types)
  1610. @make_xp_test_case(ndimage.binary_dilation)
  1611. def test_binary_dilation22(self, dtype, xp):
  1612. dtype = getattr(xp, dtype)
  1613. expected = [[0, 1, 0, 0, 0, 0, 0, 0],
  1614. [1, 1, 1, 0, 0, 0, 0, 0],
  1615. [0, 1, 0, 0, 0, 1, 0, 0],
  1616. [0, 0, 0, 1, 1, 1, 1, 0],
  1617. [0, 0, 1, 1, 1, 1, 0, 0],
  1618. [0, 1, 1, 1, 1, 1, 1, 0],
  1619. [0, 0, 1, 0, 0, 1, 0, 0],
  1620. [0, 0, 0, 0, 0, 0, 0, 0]]
  1621. expected = xp.asarray(expected)
  1622. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0],
  1623. [0, 1, 0, 0, 0, 0, 0, 0],
  1624. [0, 0, 0, 0, 0, 0, 0, 0],
  1625. [0, 0, 0, 0, 0, 1, 0, 0],
  1626. [0, 0, 0, 1, 1, 0, 0, 0],
  1627. [0, 0, 1, 0, 0, 1, 0, 0],
  1628. [0, 0, 0, 0, 0, 0, 0, 0],
  1629. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  1630. out = ndimage.binary_dilation(data)
  1631. assert_array_almost_equal(out, expected)
  1632. @pytest.mark.parametrize('dtype', types)
  1633. @make_xp_test_case(ndimage.binary_dilation)
  1634. def test_binary_dilation23(self, dtype, xp):
  1635. dtype = getattr(xp, dtype)
  1636. expected = [[1, 1, 1, 1, 1, 1, 1, 1],
  1637. [1, 1, 1, 0, 0, 0, 0, 1],
  1638. [1, 1, 0, 0, 0, 1, 0, 1],
  1639. [1, 0, 0, 1, 1, 1, 1, 1],
  1640. [1, 0, 1, 1, 1, 1, 0, 1],
  1641. [1, 1, 1, 1, 1, 1, 1, 1],
  1642. [1, 0, 1, 0, 0, 1, 0, 1],
  1643. [1, 1, 1, 1, 1, 1, 1, 1]]
  1644. expected = xp.asarray(expected)
  1645. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0],
  1646. [0, 1, 0, 0, 0, 0, 0, 0],
  1647. [0, 0, 0, 0, 0, 0, 0, 0],
  1648. [0, 0, 0, 0, 0, 1, 0, 0],
  1649. [0, 0, 0, 1, 1, 0, 0, 0],
  1650. [0, 0, 1, 0, 0, 1, 0, 0],
  1651. [0, 0, 0, 0, 0, 0, 0, 0],
  1652. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  1653. out = ndimage.binary_dilation(data, border_value=1)
  1654. assert_array_almost_equal(out, expected)
  1655. @pytest.mark.parametrize('dtype', types)
  1656. @make_xp_test_case(ndimage.binary_dilation)
  1657. def test_binary_dilation24(self, dtype, xp):
  1658. dtype = getattr(xp, dtype)
  1659. expected = [[1, 1, 0, 0, 0, 0, 0, 0],
  1660. [1, 0, 0, 0, 1, 0, 0, 0],
  1661. [0, 0, 1, 1, 1, 1, 0, 0],
  1662. [0, 1, 1, 1, 1, 0, 0, 0],
  1663. [1, 1, 1, 1, 1, 1, 0, 0],
  1664. [0, 1, 0, 0, 1, 0, 0, 0],
  1665. [0, 0, 0, 0, 0, 0, 0, 0],
  1666. [0, 0, 0, 0, 0, 0, 0, 0]]
  1667. expected = xp.asarray(expected)
  1668. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0],
  1669. [0, 1, 0, 0, 0, 0, 0, 0],
  1670. [0, 0, 0, 0, 0, 0, 0, 0],
  1671. [0, 0, 0, 0, 0, 1, 0, 0],
  1672. [0, 0, 0, 1, 1, 0, 0, 0],
  1673. [0, 0, 1, 0, 0, 1, 0, 0],
  1674. [0, 0, 0, 0, 0, 0, 0, 0],
  1675. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  1676. out = ndimage.binary_dilation(data, origin=(1, 1))
  1677. assert_array_almost_equal(out, expected)
  1678. @pytest.mark.parametrize('dtype', types)
  1679. @make_xp_test_case(ndimage.binary_dilation)
  1680. def test_binary_dilation25(self, dtype, xp):
  1681. dtype = getattr(xp, dtype)
  1682. expected = [[1, 1, 0, 0, 0, 0, 1, 1],
  1683. [1, 0, 0, 0, 1, 0, 1, 1],
  1684. [0, 0, 1, 1, 1, 1, 1, 1],
  1685. [0, 1, 1, 1, 1, 0, 1, 1],
  1686. [1, 1, 1, 1, 1, 1, 1, 1],
  1687. [0, 1, 0, 0, 1, 0, 1, 1],
  1688. [1, 1, 1, 1, 1, 1, 1, 1],
  1689. [1, 1, 1, 1, 1, 1, 1, 1]]
  1690. expected = xp.asarray(expected)
  1691. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0],
  1692. [0, 1, 0, 0, 0, 0, 0, 0],
  1693. [0, 0, 0, 0, 0, 0, 0, 0],
  1694. [0, 0, 0, 0, 0, 1, 0, 0],
  1695. [0, 0, 0, 1, 1, 0, 0, 0],
  1696. [0, 0, 1, 0, 0, 1, 0, 0],
  1697. [0, 0, 0, 0, 0, 0, 0, 0],
  1698. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  1699. out = ndimage.binary_dilation(data, origin=(1, 1), border_value=1)
  1700. assert_array_almost_equal(out, expected)
  1701. @pytest.mark.parametrize('dtype', types)
  1702. @make_xp_test_case(ndimage.binary_dilation)
  1703. def test_binary_dilation26(self, dtype, xp):
  1704. dtype = getattr(xp, dtype)
  1705. struct = ndimage.generate_binary_structure(2, 2)
  1706. expected = [[1, 1, 1, 0, 0, 0, 0, 0],
  1707. [1, 1, 1, 0, 0, 0, 0, 0],
  1708. [1, 1, 1, 0, 1, 1, 1, 0],
  1709. [0, 0, 1, 1, 1, 1, 1, 0],
  1710. [0, 1, 1, 1, 1, 1, 1, 0],
  1711. [0, 1, 1, 1, 1, 1, 1, 0],
  1712. [0, 1, 1, 1, 1, 1, 1, 0],
  1713. [0, 0, 0, 0, 0, 0, 0, 0]]
  1714. struct = xp.asarray(struct)
  1715. expected = xp.asarray(expected)
  1716. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0],
  1717. [0, 1, 0, 0, 0, 0, 0, 0],
  1718. [0, 0, 0, 0, 0, 0, 0, 0],
  1719. [0, 0, 0, 0, 0, 1, 0, 0],
  1720. [0, 0, 0, 1, 1, 0, 0, 0],
  1721. [0, 0, 1, 0, 0, 1, 0, 0],
  1722. [0, 0, 0, 0, 0, 0, 0, 0],
  1723. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  1724. out = ndimage.binary_dilation(data, struct)
  1725. assert_array_almost_equal(out, expected)
  1726. @pytest.mark.parametrize('dtype', types)
  1727. @make_xp_test_case(ndimage.binary_dilation)
  1728. def test_binary_dilation27(self, dtype, xp):
  1729. dtype = getattr(xp, dtype)
  1730. struct = [[0, 1],
  1731. [1, 1]]
  1732. expected = [[0, 1, 0, 0, 0, 0, 0, 0],
  1733. [1, 1, 0, 0, 0, 0, 0, 0],
  1734. [0, 0, 0, 0, 0, 1, 0, 0],
  1735. [0, 0, 0, 1, 1, 1, 0, 0],
  1736. [0, 0, 1, 1, 1, 1, 0, 0],
  1737. [0, 1, 1, 0, 1, 1, 0, 0],
  1738. [0, 0, 0, 0, 0, 0, 0, 0],
  1739. [0, 0, 0, 0, 0, 0, 0, 0]]
  1740. struct = xp.asarray(struct)
  1741. expected = xp.asarray(expected)
  1742. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0],
  1743. [0, 1, 0, 0, 0, 0, 0, 0],
  1744. [0, 0, 0, 0, 0, 0, 0, 0],
  1745. [0, 0, 0, 0, 0, 1, 0, 0],
  1746. [0, 0, 0, 1, 1, 0, 0, 0],
  1747. [0, 0, 1, 0, 0, 1, 0, 0],
  1748. [0, 0, 0, 0, 0, 0, 0, 0],
  1749. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  1750. out = ndimage.binary_dilation(data, struct)
  1751. assert_array_almost_equal(out, expected)
  1752. @pytest.mark.parametrize('dtype', types)
  1753. @make_xp_test_case(ndimage.binary_dilation)
  1754. def test_binary_dilation28(self, dtype, xp):
  1755. dtype = getattr(xp, dtype)
  1756. expected = [[1, 1, 1, 1],
  1757. [1, 0, 0, 1],
  1758. [1, 0, 0, 1],
  1759. [1, 1, 1, 1]]
  1760. expected = xp.asarray(expected)
  1761. data = xp.asarray([[0, 0, 0, 0],
  1762. [0, 0, 0, 0],
  1763. [0, 0, 0, 0],
  1764. [0, 0, 0, 0]], dtype=dtype)
  1765. out = ndimage.binary_dilation(data, border_value=1)
  1766. assert_array_almost_equal(out, expected)
  1767. @xfail_xp_backends("cupy",
  1768. reason="NotImplementedError: only brute_force iteration")
  1769. @make_xp_test_case(ndimage.binary_dilation)
  1770. def test_binary_dilation29(self, xp):
  1771. struct = [[0, 1],
  1772. [1, 1]]
  1773. expected = [[0, 0, 0, 0, 0],
  1774. [0, 0, 0, 1, 0],
  1775. [0, 0, 1, 1, 0],
  1776. [0, 1, 1, 1, 0],
  1777. [0, 0, 0, 0, 0]]
  1778. struct = xp.asarray(struct)
  1779. expected = xp.asarray(expected)
  1780. data = np.asarray([[0, 0, 0, 0, 0],
  1781. [0, 0, 0, 0, 0],
  1782. [0, 0, 0, 0, 0],
  1783. [0, 0, 0, 1, 0],
  1784. [0, 0, 0, 0, 0]], dtype=bool)
  1785. data = xp.asarray(data)
  1786. out = ndimage.binary_dilation(data, struct, iterations=2)
  1787. assert_array_almost_equal(out, expected)
  1788. @skip_xp_backends(np_only=True, exceptions=["cupy"],
  1789. reason='output= arrays are numpy-specific')
  1790. @xfail_xp_backends("cupy",
  1791. reason="NotImplementedError: only brute_force iteration")
  1792. @make_xp_test_case(ndimage.binary_dilation)
  1793. def test_binary_dilation30(self, xp):
  1794. struct = [[0, 1],
  1795. [1, 1]]
  1796. expected = [[0, 0, 0, 0, 0],
  1797. [0, 0, 0, 1, 0],
  1798. [0, 0, 1, 1, 0],
  1799. [0, 1, 1, 1, 0],
  1800. [0, 0, 0, 0, 0]]
  1801. struct = xp.asarray(struct)
  1802. expected = xp.asarray(expected)
  1803. data = xp.asarray([[0, 0, 0, 0, 0],
  1804. [0, 0, 0, 0, 0],
  1805. [0, 0, 0, 0, 0],
  1806. [0, 0, 0, 1, 0],
  1807. [0, 0, 0, 0, 0]], dtype=bool)
  1808. data = xp.asarray(data)
  1809. out = np.zeros(data.shape, dtype=bool)
  1810. out = xp.asarray(out)
  1811. ndimage.binary_dilation(data, struct, iterations=2, output=out)
  1812. assert_array_almost_equal(out, expected)
  1813. @xfail_xp_backends("cupy",
  1814. reason="NotImplementedError: only brute_force iteration")
  1815. @make_xp_test_case(ndimage.binary_dilation)
  1816. def test_binary_dilation31(self, xp):
  1817. struct = [[0, 1],
  1818. [1, 1]]
  1819. expected = [[0, 0, 0, 1, 0],
  1820. [0, 0, 1, 1, 0],
  1821. [0, 1, 1, 1, 0],
  1822. [1, 1, 1, 1, 0],
  1823. [0, 0, 0, 0, 0]]
  1824. struct = xp.asarray(struct)
  1825. expected = xp.asarray(expected)
  1826. data = np.asarray([[0, 0, 0, 0, 0],
  1827. [0, 0, 0, 0, 0],
  1828. [0, 0, 0, 0, 0],
  1829. [0, 0, 0, 1, 0],
  1830. [0, 0, 0, 0, 0]], dtype=bool)
  1831. data = xp.asarray(data)
  1832. out = ndimage.binary_dilation(data, struct, iterations=3)
  1833. assert_array_almost_equal(out, expected)
  1834. @skip_xp_backends(np_only=True, exceptions=["cupy"],
  1835. reason='output= arrays are numpy-specific')
  1836. @xfail_xp_backends("cupy",
  1837. reason="NotImplementedError: only brute_force iteration")
  1838. @make_xp_test_case(ndimage.binary_dilation)
  1839. def test_binary_dilation32(self, xp):
  1840. struct = [[0, 1],
  1841. [1, 1]]
  1842. expected = [[0, 0, 0, 1, 0],
  1843. [0, 0, 1, 1, 0],
  1844. [0, 1, 1, 1, 0],
  1845. [1, 1, 1, 1, 0],
  1846. [0, 0, 0, 0, 0]]
  1847. struct = xp.asarray(struct)
  1848. expected = xp.asarray(expected)
  1849. data = np.asarray([[0, 0, 0, 0, 0],
  1850. [0, 0, 0, 0, 0],
  1851. [0, 0, 0, 0, 0],
  1852. [0, 0, 0, 1, 0],
  1853. [0, 0, 0, 0, 0]], dtype=bool)
  1854. data = xp.asarray(data)
  1855. out = np.zeros(data.shape, dtype=bool)
  1856. out = xp.asarray(out)
  1857. ndimage.binary_dilation(data, struct, iterations=3, output=out)
  1858. assert_array_almost_equal(out, expected)
  1859. @xfail_xp_backends("cupy",
  1860. reason="NotImplementedError: only brute_force iteration")
  1861. @make_xp_test_case(ndimage.binary_dilation)
  1862. def test_binary_dilation33(self, xp):
  1863. struct = [[0, 1, 0],
  1864. [1, 1, 1],
  1865. [0, 1, 0]]
  1866. struct = xp.asarray(struct)
  1867. expected = np.asarray([[0, 1, 0, 0, 0, 0, 0, 0],
  1868. [0, 0, 0, 0, 0, 0, 0, 0],
  1869. [0, 0, 0, 0, 0, 0, 0, 0],
  1870. [0, 0, 0, 0, 1, 1, 0, 0],
  1871. [0, 0, 1, 1, 1, 0, 0, 0],
  1872. [0, 1, 1, 0, 1, 1, 0, 0],
  1873. [0, 0, 0, 0, 0, 0, 0, 0],
  1874. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=bool)
  1875. expected = xp.asarray(expected)
  1876. mask = np.asarray([[0, 1, 0, 0, 0, 0, 0, 0],
  1877. [0, 0, 0, 0, 0, 0, 0, 0],
  1878. [0, 0, 0, 0, 0, 0, 1, 0],
  1879. [0, 0, 0, 0, 1, 1, 0, 0],
  1880. [0, 0, 1, 1, 1, 0, 0, 0],
  1881. [0, 1, 1, 0, 1, 1, 0, 0],
  1882. [0, 0, 0, 0, 0, 0, 0, 0],
  1883. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=bool)
  1884. mask = xp.asarray(mask)
  1885. data = np.asarray([[0, 1, 0, 0, 0, 0, 0, 0],
  1886. [0, 0, 0, 0, 0, 0, 0, 0],
  1887. [0, 0, 0, 0, 0, 0, 0, 0],
  1888. [0, 0, 0, 0, 0, 0, 0, 0],
  1889. [0, 0, 0, 0, 0, 0, 0, 0],
  1890. [0, 1, 0, 0, 0, 0, 0, 0],
  1891. [0, 0, 0, 0, 0, 0, 0, 0],
  1892. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=bool)
  1893. data = xp.asarray(data)
  1894. out = ndimage.binary_dilation(data, struct, iterations=-1,
  1895. mask=mask, border_value=0)
  1896. assert_array_almost_equal(out, expected)
  1897. @skip_xp_backends(np_only=True, exceptions=["cupy"],
  1898. reason='inplace output= arrays are numpy-specific')
  1899. @xfail_xp_backends("cupy",
  1900. reason="NotImplementedError: only brute_force iteration")
  1901. @make_xp_test_case(ndimage.binary_dilation)
  1902. def test_binary_dilation34(self, xp):
  1903. struct = [[0, 1, 0],
  1904. [1, 1, 1],
  1905. [0, 1, 0]]
  1906. struct = xp.asarray(struct)
  1907. expected = [[0, 1, 0, 0, 0, 0, 0, 0],
  1908. [0, 1, 1, 0, 0, 0, 0, 0],
  1909. [0, 0, 1, 0, 0, 0, 0, 0],
  1910. [0, 0, 0, 0, 0, 0, 0, 0],
  1911. [0, 0, 0, 0, 0, 0, 0, 0],
  1912. [0, 0, 0, 0, 0, 0, 0, 0],
  1913. [0, 0, 0, 0, 0, 0, 0, 0],
  1914. [0, 0, 0, 0, 0, 0, 0, 0]]
  1915. mask = np.asarray([[0, 1, 0, 0, 0, 0, 0, 0],
  1916. [0, 1, 1, 0, 0, 0, 0, 0],
  1917. [0, 0, 1, 0, 0, 0, 0, 0],
  1918. [0, 0, 0, 0, 0, 1, 0, 0],
  1919. [0, 0, 0, 1, 1, 0, 0, 0],
  1920. [0, 0, 1, 0, 0, 1, 0, 0],
  1921. [0, 0, 0, 0, 0, 0, 0, 0],
  1922. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=bool)
  1923. mask = xp.asarray(mask)
  1924. data = np.zeros(mask.shape, dtype=bool)
  1925. data = xp.asarray(data)
  1926. out = ndimage.binary_dilation(data, struct, iterations=-1,
  1927. mask=mask, border_value=1)
  1928. assert_array_almost_equal(out, expected)
  1929. @pytest.mark.parametrize('dtype', types)
  1930. @make_xp_test_case(ndimage.binary_dilation)
  1931. def test_binary_dilation35(self, dtype, xp):
  1932. dtype = getattr(xp, dtype)
  1933. tmp = [[1, 1, 0, 0, 0, 0, 1, 1],
  1934. [1, 0, 0, 0, 1, 0, 1, 1],
  1935. [0, 0, 1, 1, 1, 1, 1, 1],
  1936. [0, 1, 1, 1, 1, 0, 1, 1],
  1937. [1, 1, 1, 1, 1, 1, 1, 1],
  1938. [0, 1, 0, 0, 1, 0, 1, 1],
  1939. [1, 1, 1, 1, 1, 1, 1, 1],
  1940. [1, 1, 1, 1, 1, 1, 1, 1]]
  1941. data = np.asarray([[0, 0, 0, 0, 0, 0, 0, 0],
  1942. [0, 1, 0, 0, 0, 0, 0, 0],
  1943. [0, 0, 0, 0, 0, 0, 0, 0],
  1944. [0, 0, 0, 0, 0, 1, 0, 0],
  1945. [0, 0, 0, 1, 1, 0, 0, 0],
  1946. [0, 0, 1, 0, 0, 1, 0, 0],
  1947. [0, 0, 0, 0, 0, 0, 0, 0],
  1948. [0, 0, 0, 0, 0, 0, 0, 0]])
  1949. mask = [[0, 0, 0, 0, 0, 0, 0, 0],
  1950. [0, 0, 0, 0, 0, 0, 0, 0],
  1951. [0, 0, 0, 0, 0, 0, 0, 0],
  1952. [0, 0, 1, 1, 1, 1, 0, 0],
  1953. [0, 0, 1, 1, 1, 1, 0, 0],
  1954. [0, 0, 1, 1, 1, 1, 0, 0],
  1955. [0, 0, 0, 0, 0, 0, 0, 0],
  1956. [0, 0, 0, 0, 0, 0, 0, 0]]
  1957. mask = np.asarray(mask, dtype=bool)
  1958. expected = np.logical_and(tmp, mask)
  1959. tmp = np.logical_and(data, np.logical_not(mask))
  1960. expected = np.logical_or(expected, tmp)
  1961. mask = xp.asarray(mask)
  1962. expected = xp.asarray(expected)
  1963. data = xp.asarray([[0, 0, 0, 0, 0, 0, 0, 0],
  1964. [0, 1, 0, 0, 0, 0, 0, 0],
  1965. [0, 0, 0, 0, 0, 0, 0, 0],
  1966. [0, 0, 0, 0, 0, 1, 0, 0],
  1967. [0, 0, 0, 1, 1, 0, 0, 0],
  1968. [0, 0, 1, 0, 0, 1, 0, 0],
  1969. [0, 0, 0, 0, 0, 0, 0, 0],
  1970. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  1971. out = ndimage.binary_dilation(data, mask=mask,
  1972. origin=(1, 1), border_value=1)
  1973. assert_array_almost_equal(out, expected)
  1974. @make_xp_test_case(ndimage.binary_dilation)
  1975. def test_binary_dilation36(self, xp):
  1976. # gh-21009
  1977. data = np.zeros([], dtype=bool)
  1978. data = xp.asarray(data)
  1979. out = ndimage.binary_dilation(data, iterations=-1)
  1980. assert out == xp.asarray(False)
  1981. @make_xp_test_case(ndimage.binary_propagation)
  1982. def test_binary_propagation01(self, xp):
  1983. struct = [[0, 1, 0],
  1984. [1, 1, 1],
  1985. [0, 1, 0]]
  1986. struct = xp.asarray(struct)
  1987. expected = np.asarray([[0, 1, 0, 0, 0, 0, 0, 0],
  1988. [0, 0, 0, 0, 0, 0, 0, 0],
  1989. [0, 0, 0, 0, 0, 0, 0, 0],
  1990. [0, 0, 0, 0, 1, 1, 0, 0],
  1991. [0, 0, 1, 1, 1, 0, 0, 0],
  1992. [0, 1, 1, 0, 1, 1, 0, 0],
  1993. [0, 0, 0, 0, 0, 0, 0, 0],
  1994. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=bool)
  1995. expected = xp.asarray(expected)
  1996. mask = np.asarray([[0, 1, 0, 0, 0, 0, 0, 0],
  1997. [0, 0, 0, 0, 0, 0, 0, 0],
  1998. [0, 0, 0, 0, 0, 0, 1, 0],
  1999. [0, 0, 0, 0, 1, 1, 0, 0],
  2000. [0, 0, 1, 1, 1, 0, 0, 0],
  2001. [0, 1, 1, 0, 1, 1, 0, 0],
  2002. [0, 0, 0, 0, 0, 0, 0, 0],
  2003. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=bool)
  2004. mask = xp.asarray(mask)
  2005. data = np.asarray([[0, 1, 0, 0, 0, 0, 0, 0],
  2006. [0, 0, 0, 0, 0, 0, 0, 0],
  2007. [0, 0, 0, 0, 0, 0, 0, 0],
  2008. [0, 0, 0, 0, 0, 0, 0, 0],
  2009. [0, 0, 0, 0, 0, 0, 0, 0],
  2010. [0, 1, 0, 0, 0, 0, 0, 0],
  2011. [0, 0, 0, 0, 0, 0, 0, 0],
  2012. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=bool)
  2013. data = xp.asarray(data)
  2014. out = ndimage.binary_propagation(data, struct,
  2015. mask=mask, border_value=0)
  2016. assert_array_almost_equal(out, expected)
  2017. @make_xp_test_case(ndimage.binary_propagation)
  2018. def test_binary_propagation02(self, xp):
  2019. struct = [[0, 1, 0],
  2020. [1, 1, 1],
  2021. [0, 1, 0]]
  2022. expected = [[0, 1, 0, 0, 0, 0, 0, 0],
  2023. [0, 1, 1, 0, 0, 0, 0, 0],
  2024. [0, 0, 1, 0, 0, 0, 0, 0],
  2025. [0, 0, 0, 0, 0, 0, 0, 0],
  2026. [0, 0, 0, 0, 0, 0, 0, 0],
  2027. [0, 0, 0, 0, 0, 0, 0, 0],
  2028. [0, 0, 0, 0, 0, 0, 0, 0],
  2029. [0, 0, 0, 0, 0, 0, 0, 0]]
  2030. expected = xp.asarray(expected)
  2031. struct = xp.asarray(struct)
  2032. mask = np.asarray([[0, 1, 0, 0, 0, 0, 0, 0],
  2033. [0, 1, 1, 0, 0, 0, 0, 0],
  2034. [0, 0, 1, 0, 0, 0, 0, 0],
  2035. [0, 0, 0, 0, 0, 1, 0, 0],
  2036. [0, 0, 0, 1, 1, 0, 0, 0],
  2037. [0, 0, 1, 0, 0, 1, 0, 0],
  2038. [0, 0, 0, 0, 0, 0, 0, 0],
  2039. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=bool)
  2040. mask = xp.asarray(mask)
  2041. data = np.zeros(mask.shape, dtype=bool)
  2042. data = xp.asarray(data)
  2043. out = ndimage.binary_propagation(data, struct,
  2044. mask=mask, border_value=1)
  2045. assert_array_almost_equal(out, expected)
  2046. @make_xp_test_case(ndimage.binary_propagation)
  2047. def test_binary_propagation03(self, xp):
  2048. # gh-21009
  2049. data = xp.asarray(np.zeros([], dtype=bool))
  2050. expected = xp.asarray(np.zeros([], dtype=bool))
  2051. out = ndimage.binary_propagation(data)
  2052. assert out == expected
  2053. @make_xp_test_case(ndimage.binary_opening)
  2054. @pytest.mark.parametrize('dtype', types)
  2055. def test_binary_opening01(self, dtype, xp):
  2056. dtype = getattr(xp, dtype)
  2057. expected = [[0, 1, 0, 0, 0, 0, 0, 0],
  2058. [1, 1, 1, 0, 0, 0, 0, 0],
  2059. [0, 1, 0, 0, 0, 1, 0, 0],
  2060. [0, 0, 0, 0, 1, 1, 1, 0],
  2061. [0, 0, 1, 0, 0, 1, 0, 0],
  2062. [0, 1, 1, 1, 1, 1, 1, 0],
  2063. [0, 0, 1, 0, 0, 1, 0, 0],
  2064. [0, 0, 0, 0, 0, 0, 0, 0]]
  2065. expected = xp.asarray(expected)
  2066. data = xp.asarray([[0, 1, 0, 0, 0, 0, 0, 0],
  2067. [1, 1, 1, 0, 0, 0, 0, 0],
  2068. [0, 1, 0, 0, 0, 1, 0, 0],
  2069. [0, 0, 0, 1, 1, 1, 1, 0],
  2070. [0, 0, 1, 1, 0, 1, 0, 0],
  2071. [0, 1, 1, 1, 1, 1, 1, 0],
  2072. [0, 0, 1, 0, 0, 1, 0, 0],
  2073. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  2074. out = ndimage.binary_opening(data)
  2075. assert_array_almost_equal(out, expected)
  2076. @make_xp_test_case(ndimage.binary_opening)
  2077. @pytest.mark.parametrize('dtype', types)
  2078. def test_binary_opening02(self, dtype, xp):
  2079. dtype = getattr(xp, dtype)
  2080. struct = ndimage.generate_binary_structure(2, 2)
  2081. expected = [[1, 1, 1, 0, 0, 0, 0, 0],
  2082. [1, 1, 1, 0, 0, 0, 0, 0],
  2083. [1, 1, 1, 0, 0, 0, 0, 0],
  2084. [0, 0, 0, 0, 0, 0, 0, 0],
  2085. [0, 1, 1, 1, 0, 0, 0, 0],
  2086. [0, 1, 1, 1, 0, 0, 0, 0],
  2087. [0, 1, 1, 1, 0, 0, 0, 0],
  2088. [0, 0, 0, 0, 0, 0, 0, 0]]
  2089. expected = xp.asarray(expected)
  2090. struct = xp.asarray(struct)
  2091. data = xp.asarray([[1, 1, 1, 0, 0, 0, 0, 0],
  2092. [1, 1, 1, 0, 0, 0, 0, 0],
  2093. [1, 1, 1, 1, 1, 1, 1, 0],
  2094. [0, 0, 1, 1, 1, 1, 1, 0],
  2095. [0, 1, 1, 1, 0, 1, 1, 0],
  2096. [0, 1, 1, 1, 1, 1, 1, 0],
  2097. [0, 1, 1, 1, 1, 1, 1, 0],
  2098. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  2099. out = ndimage.binary_opening(data, struct)
  2100. assert_array_almost_equal(out, expected)
  2101. @pytest.mark.parametrize('dtype', types)
  2102. @make_xp_test_case(ndimage.binary_closing)
  2103. def test_binary_closing01(self, dtype, xp):
  2104. dtype = getattr(xp, dtype)
  2105. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  2106. [0, 1, 1, 0, 0, 0, 0, 0],
  2107. [0, 1, 1, 1, 0, 1, 0, 0],
  2108. [0, 0, 1, 1, 1, 1, 1, 0],
  2109. [0, 0, 1, 1, 1, 1, 0, 0],
  2110. [0, 1, 1, 1, 1, 1, 1, 0],
  2111. [0, 0, 1, 0, 0, 1, 0, 0],
  2112. [0, 0, 0, 0, 0, 0, 0, 0]]
  2113. expected = xp.asarray(expected)
  2114. data = xp.asarray([[0, 1, 0, 0, 0, 0, 0, 0],
  2115. [1, 1, 1, 0, 0, 0, 0, 0],
  2116. [0, 1, 0, 0, 0, 1, 0, 0],
  2117. [0, 0, 0, 1, 1, 1, 1, 0],
  2118. [0, 0, 1, 1, 0, 1, 0, 0],
  2119. [0, 1, 1, 1, 1, 1, 1, 0],
  2120. [0, 0, 1, 0, 0, 1, 0, 0],
  2121. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  2122. out = ndimage.binary_closing(data)
  2123. assert_array_almost_equal(out, expected)
  2124. @pytest.mark.parametrize('dtype', types)
  2125. @make_xp_test_case(ndimage.binary_closing)
  2126. def test_binary_closing02(self, dtype, xp):
  2127. dtype = getattr(xp, dtype)
  2128. struct = ndimage.generate_binary_structure(2, 2)
  2129. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  2130. [0, 1, 1, 0, 0, 0, 0, 0],
  2131. [0, 1, 1, 1, 1, 1, 1, 0],
  2132. [0, 1, 1, 1, 1, 1, 1, 0],
  2133. [0, 1, 1, 1, 1, 1, 1, 0],
  2134. [0, 1, 1, 1, 1, 1, 1, 0],
  2135. [0, 1, 1, 1, 1, 1, 1, 0],
  2136. [0, 0, 0, 0, 0, 0, 0, 0]]
  2137. expected = xp.asarray(expected)
  2138. struct = xp.asarray(struct)
  2139. data = xp.asarray([[1, 1, 1, 0, 0, 0, 0, 0],
  2140. [1, 1, 1, 0, 0, 0, 0, 0],
  2141. [1, 1, 1, 1, 1, 1, 1, 0],
  2142. [0, 0, 1, 1, 1, 1, 1, 0],
  2143. [0, 1, 1, 1, 0, 1, 1, 0],
  2144. [0, 1, 1, 1, 1, 1, 1, 0],
  2145. [0, 1, 1, 1, 1, 1, 1, 0],
  2146. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  2147. out = ndimage.binary_closing(data, struct)
  2148. assert_array_almost_equal(out, expected)
  2149. @make_xp_test_case(ndimage.binary_fill_holes)
  2150. def test_binary_fill_holes01(self, xp):
  2151. expected = np.asarray([[0, 0, 0, 0, 0, 0, 0, 0],
  2152. [0, 0, 1, 1, 1, 1, 0, 0],
  2153. [0, 0, 1, 1, 1, 1, 0, 0],
  2154. [0, 0, 1, 1, 1, 1, 0, 0],
  2155. [0, 0, 1, 1, 1, 1, 0, 0],
  2156. [0, 0, 1, 1, 1, 1, 0, 0],
  2157. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=bool)
  2158. expected = xp.asarray(expected)
  2159. data = np.asarray([[0, 0, 0, 0, 0, 0, 0, 0],
  2160. [0, 0, 1, 1, 1, 1, 0, 0],
  2161. [0, 0, 1, 0, 0, 1, 0, 0],
  2162. [0, 0, 1, 0, 0, 1, 0, 0],
  2163. [0, 0, 1, 0, 0, 1, 0, 0],
  2164. [0, 0, 1, 1, 1, 1, 0, 0],
  2165. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=bool)
  2166. data = xp.asarray(data)
  2167. out = ndimage.binary_fill_holes(data)
  2168. assert_array_almost_equal(out, expected)
  2169. @make_xp_test_case(ndimage.binary_fill_holes)
  2170. def test_binary_fill_holes02(self, xp):
  2171. expected = np.asarray([[0, 0, 0, 0, 0, 0, 0, 0],
  2172. [0, 0, 0, 1, 1, 0, 0, 0],
  2173. [0, 0, 1, 1, 1, 1, 0, 0],
  2174. [0, 0, 1, 1, 1, 1, 0, 0],
  2175. [0, 0, 1, 1, 1, 1, 0, 0],
  2176. [0, 0, 0, 1, 1, 0, 0, 0],
  2177. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=bool)
  2178. expected = xp.asarray(expected)
  2179. data = np.asarray([[0, 0, 0, 0, 0, 0, 0, 0],
  2180. [0, 0, 0, 1, 1, 0, 0, 0],
  2181. [0, 0, 1, 0, 0, 1, 0, 0],
  2182. [0, 0, 1, 0, 0, 1, 0, 0],
  2183. [0, 0, 1, 0, 0, 1, 0, 0],
  2184. [0, 0, 0, 1, 1, 0, 0, 0],
  2185. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=bool)
  2186. data = xp.asarray(data)
  2187. out = ndimage.binary_fill_holes(data)
  2188. assert_array_almost_equal(out, expected)
  2189. @make_xp_test_case(ndimage.binary_fill_holes)
  2190. def test_binary_fill_holes03(self, xp):
  2191. expected = np.asarray([[0, 0, 0, 0, 0, 0, 0, 0],
  2192. [0, 0, 1, 0, 0, 0, 0, 0],
  2193. [0, 1, 1, 1, 0, 1, 1, 1],
  2194. [0, 1, 1, 1, 0, 1, 1, 1],
  2195. [0, 1, 1, 1, 0, 1, 1, 1],
  2196. [0, 0, 1, 0, 0, 1, 1, 1],
  2197. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=bool)
  2198. expected = xp.asarray(expected)
  2199. data = np.asarray([[0, 0, 0, 0, 0, 0, 0, 0],
  2200. [0, 0, 1, 0, 0, 0, 0, 0],
  2201. [0, 1, 0, 1, 0, 1, 1, 1],
  2202. [0, 1, 0, 1, 0, 1, 0, 1],
  2203. [0, 1, 0, 1, 0, 1, 0, 1],
  2204. [0, 0, 1, 0, 0, 1, 1, 1],
  2205. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=bool)
  2206. data = xp.asarray(data)
  2207. out = ndimage.binary_fill_holes(data)
  2208. assert_array_almost_equal(out, expected)
  2209. @skip_xp_backends(cpu_only=True)
  2210. @skip_xp_backends(
  2211. "cupy", reason="these filters do not yet have axes support in CuPy")
  2212. @skip_xp_backends(
  2213. "jax.numpy", reason="these filters are not implemented in JAX.numpy")
  2214. @pytest.mark.parametrize('border_value',[0, 1])
  2215. @pytest.mark.parametrize('origin', [(0, 0), (-1, 0)])
  2216. @pytest.mark.parametrize('expand_axis', [0, 1, 2])
  2217. @pytest.mark.parametrize(
  2218. 'func', [
  2219. make_xp_pytest_param(ndimage.binary_erosion),
  2220. make_xp_pytest_param(ndimage.binary_dilation),
  2221. make_xp_pytest_param(ndimage.binary_opening),
  2222. make_xp_pytest_param(ndimage.binary_closing),
  2223. make_xp_pytest_param(ndimage.binary_hit_or_miss),
  2224. make_xp_pytest_param(ndimage.binary_propagation),
  2225. make_xp_pytest_param(ndimage.binary_fill_holes),
  2226. ]
  2227. )
  2228. def test_binary_axes(self, xp, func, expand_axis, origin, border_value):
  2229. func_name = func.__name__
  2230. struct = np.asarray([[0, 1, 0],
  2231. [1, 1, 1],
  2232. [0, 1, 0]], bool)
  2233. struct = xp.asarray(struct)
  2234. data = np.asarray([[0, 0, 0, 1, 0, 0, 0],
  2235. [0, 0, 0, 1, 0, 0, 0],
  2236. [0, 0, 1, 1, 0, 1, 0],
  2237. [0, 1, 0, 1, 1, 0, 1],
  2238. [0, 1, 1, 1, 1, 1, 0],
  2239. [0, 0, 1, 1, 0, 0, 0],
  2240. [0, 0, 0, 1, 0, 0, 0]], bool)
  2241. data = xp.asarray(data)
  2242. if func_name == "binary_hit_or_miss":
  2243. kwargs = dict(origin1=origin, origin2=origin)
  2244. else:
  2245. kwargs = dict(origin=origin)
  2246. border_supported = func_name not in ["binary_hit_or_miss",
  2247. "binary_fill_holes"]
  2248. if border_supported:
  2249. kwargs['border_value'] = border_value
  2250. elif border_value != 0:
  2251. pytest.skip('border_value !=0 unsupported by this function')
  2252. expected = func(data, struct, **kwargs)
  2253. # replicate data and expected result along a new axis
  2254. n_reps = 5
  2255. expected = xp.stack([expected] * n_reps, axis=expand_axis)
  2256. data = xp.stack([data] * n_reps, axis=expand_axis)
  2257. # filter all axes except expand_axis
  2258. axes = [0, 1, 2]
  2259. axes.remove(expand_axis)
  2260. if is_numpy(xp) or is_cupy(xp):
  2261. out = xp.asarray(np.zeros(data.shape, bool))
  2262. func(data, struct, output=out, axes=axes, **kwargs)
  2263. else:
  2264. # inplace output= is unsupported by JAX
  2265. out = func(data, struct, axes=axes, **kwargs)
  2266. xp_assert_close(out, expected)
  2267. @make_xp_test_case(ndimage.grey_erosion)
  2268. def test_grey_erosion01(self, xp):
  2269. array = xp.asarray([[3, 2, 5, 1, 4],
  2270. [7, 6, 9, 3, 5],
  2271. [5, 8, 3, 7, 1]])
  2272. footprint = xp.asarray([[1, 0, 1], [1, 1, 0]])
  2273. output = ndimage.grey_erosion(array, footprint=footprint)
  2274. assert_array_almost_equal(output,
  2275. xp.asarray([[2, 2, 1, 1, 1],
  2276. [2, 3, 1, 3, 1],
  2277. [5, 5, 3, 3, 1]]))
  2278. @skip_xp_backends("jax.numpy", reason="output=array requires buffer view")
  2279. @skip_xp_backends("dask.array", reason="output=array requires buffer view")
  2280. @xfail_xp_backends("cupy", reason="https://github.com/cupy/cupy/issues/8398")
  2281. @make_xp_test_case(ndimage.grey_erosion)
  2282. def test_grey_erosion01_overlap(self, xp):
  2283. array = xp.asarray([[3, 2, 5, 1, 4],
  2284. [7, 6, 9, 3, 5],
  2285. [5, 8, 3, 7, 1]])
  2286. footprint = xp.asarray([[1, 0, 1], [1, 1, 0]])
  2287. ndimage.grey_erosion(array, footprint=footprint, output=array)
  2288. assert_array_almost_equal(array,
  2289. xp.asarray([[2, 2, 1, 1, 1],
  2290. [2, 3, 1, 3, 1],
  2291. [5, 5, 3, 3, 1]])
  2292. )
  2293. @make_xp_test_case(ndimage.grey_erosion)
  2294. def test_grey_erosion02(self, xp):
  2295. array = xp.asarray([[3, 2, 5, 1, 4],
  2296. [7, 6, 9, 3, 5],
  2297. [5, 8, 3, 7, 1]])
  2298. footprint = xp.asarray([[1, 0, 1], [1, 1, 0]])
  2299. structure = xp.asarray([[0, 0, 0], [0, 0, 0]])
  2300. output = ndimage.grey_erosion(array, footprint=footprint,
  2301. structure=structure)
  2302. assert_array_almost_equal(output,
  2303. xp.asarray([[2, 2, 1, 1, 1],
  2304. [2, 3, 1, 3, 1],
  2305. [5, 5, 3, 3, 1]])
  2306. )
  2307. @make_xp_test_case(ndimage.grey_erosion)
  2308. def test_grey_erosion03(self, xp):
  2309. array = xp.asarray([[3, 2, 5, 1, 4],
  2310. [7, 6, 9, 3, 5],
  2311. [5, 8, 3, 7, 1]])
  2312. footprint = xp.asarray([[1, 0, 1], [1, 1, 0]])
  2313. structure = xp.asarray([[1, 1, 1], [1, 1, 1]])
  2314. output = ndimage.grey_erosion(array, footprint=footprint,
  2315. structure=structure)
  2316. assert_array_almost_equal(output,
  2317. xp.asarray([[1, 1, 0, 0, 0],
  2318. [1, 2, 0, 2, 0],
  2319. [4, 4, 2, 2, 0]])
  2320. )
  2321. @make_xp_test_case(ndimage.grey_dilation)
  2322. def test_grey_dilation01(self, xp):
  2323. array = xp.asarray([[3, 2, 5, 1, 4],
  2324. [7, 6, 9, 3, 5],
  2325. [5, 8, 3, 7, 1]])
  2326. footprint = xp.asarray([[0, 1, 1], [1, 0, 1]])
  2327. output = ndimage.grey_dilation(array, footprint=footprint)
  2328. assert_array_almost_equal(output,
  2329. xp.asarray([[7, 7, 9, 9, 5],
  2330. [7, 9, 8, 9, 7],
  2331. [8, 8, 8, 7, 7]]),
  2332. )
  2333. @make_xp_test_case(ndimage.grey_dilation)
  2334. def test_grey_dilation02(self, xp):
  2335. array = xp.asarray([[3, 2, 5, 1, 4],
  2336. [7, 6, 9, 3, 5],
  2337. [5, 8, 3, 7, 1]])
  2338. footprint = xp.asarray([[0, 1, 1], [1, 0, 1]])
  2339. structure = xp.asarray([[0, 0, 0], [0, 0, 0]])
  2340. output = ndimage.grey_dilation(array, footprint=footprint,
  2341. structure=structure)
  2342. assert_array_almost_equal(output,
  2343. xp.asarray([[7, 7, 9, 9, 5],
  2344. [7, 9, 8, 9, 7],
  2345. [8, 8, 8, 7, 7]]),
  2346. )
  2347. @make_xp_test_case(ndimage.grey_dilation)
  2348. def test_grey_dilation03(self, xp):
  2349. array = xp.asarray([[3, 2, 5, 1, 4],
  2350. [7, 6, 9, 3, 5],
  2351. [5, 8, 3, 7, 1]])
  2352. footprint = xp.asarray([[0, 1, 1], [1, 0, 1]])
  2353. structure = xp.asarray([[1, 1, 1], [1, 1, 1]])
  2354. output = ndimage.grey_dilation(array, footprint=footprint,
  2355. structure=structure)
  2356. assert_array_almost_equal(output,
  2357. xp.asarray([[8, 8, 10, 10, 6],
  2358. [8, 10, 9, 10, 8],
  2359. [9, 9, 9, 8, 8]]),
  2360. )
  2361. @make_xp_test_case(ndimage.grey_opening)
  2362. def test_grey_opening01(self, xp):
  2363. array = xp.asarray([[3, 2, 5, 1, 4],
  2364. [7, 6, 9, 3, 5],
  2365. [5, 8, 3, 7, 1]])
  2366. footprint = xp.asarray([[1, 0, 1], [1, 1, 0]])
  2367. tmp = ndimage.grey_erosion(array, footprint=footprint)
  2368. expected = ndimage.grey_dilation(tmp, footprint=footprint)
  2369. output = ndimage.grey_opening(array, footprint=footprint)
  2370. assert_array_almost_equal(output, expected)
  2371. @make_xp_test_case(ndimage.grey_opening)
  2372. def test_grey_opening02(self, xp):
  2373. array = xp.asarray([[3, 2, 5, 1, 4],
  2374. [7, 6, 9, 3, 5],
  2375. [5, 8, 3, 7, 1]])
  2376. footprint = xp.asarray([[1, 0, 1], [1, 1, 0]])
  2377. structure = xp.asarray([[0, 0, 0], [0, 0, 0]])
  2378. tmp = ndimage.grey_erosion(array, footprint=footprint,
  2379. structure=structure)
  2380. expected = ndimage.grey_dilation(tmp, footprint=footprint,
  2381. structure=structure)
  2382. output = ndimage.grey_opening(array, footprint=footprint,
  2383. structure=structure)
  2384. assert_array_almost_equal(output, expected)
  2385. @make_xp_test_case(ndimage.grey_closing)
  2386. def test_grey_closing01(self, xp):
  2387. array = xp.asarray([[3, 2, 5, 1, 4],
  2388. [7, 6, 9, 3, 5],
  2389. [5, 8, 3, 7, 1]])
  2390. footprint = xp.asarray([[1, 0, 1], [1, 1, 0]])
  2391. tmp = ndimage.grey_dilation(array, footprint=footprint)
  2392. expected = ndimage.grey_erosion(tmp, footprint=footprint)
  2393. output = ndimage.grey_closing(array, footprint=footprint)
  2394. assert_array_almost_equal(output, expected)
  2395. @make_xp_test_case(ndimage.grey_closing)
  2396. def test_grey_closing02(self, xp):
  2397. array = xp.asarray([[3, 2, 5, 1, 4],
  2398. [7, 6, 9, 3, 5],
  2399. [5, 8, 3, 7, 1]])
  2400. footprint = xp.asarray([[1, 0, 1], [1, 1, 0]])
  2401. structure = xp.asarray([[0, 0, 0], [0, 0, 0]])
  2402. tmp = ndimage.grey_dilation(array, footprint=footprint,
  2403. structure=structure)
  2404. expected = ndimage.grey_erosion(tmp, footprint=footprint,
  2405. structure=structure)
  2406. output = ndimage.grey_closing(array, footprint=footprint,
  2407. structure=structure)
  2408. assert_array_almost_equal(output, expected)
  2409. @skip_xp_backends(np_only=True, reason='output= arrays are numpy-specific')
  2410. @make_xp_test_case(ndimage.grey_dilation, ndimage.grey_erosion,
  2411. ndimage.morphological_gradient)
  2412. def test_morphological_gradient01(self, xp):
  2413. array = xp.asarray([[3, 2, 5, 1, 4],
  2414. [7, 6, 9, 3, 5],
  2415. [5, 8, 3, 7, 1]])
  2416. footprint = xp.asarray([[1, 0, 1], [1, 1, 0]])
  2417. structure = xp.asarray([[0, 0, 0], [0, 0, 0]])
  2418. tmp1 = ndimage.grey_dilation(array, footprint=footprint,
  2419. structure=structure)
  2420. tmp2 = ndimage.grey_erosion(array, footprint=footprint,
  2421. structure=structure)
  2422. expected = tmp1 - tmp2
  2423. output = xp.zeros(array.shape, dtype=array.dtype)
  2424. ndimage.morphological_gradient(array, footprint=footprint,
  2425. structure=structure, output=output)
  2426. assert_array_almost_equal(output, expected)
  2427. @make_xp_test_case(ndimage.grey_dilation, ndimage.grey_erosion,
  2428. ndimage.morphological_gradient)
  2429. def test_morphological_gradient02(self, xp):
  2430. array = xp.asarray([[3, 2, 5, 1, 4],
  2431. [7, 6, 9, 3, 5],
  2432. [5, 8, 3, 7, 1]])
  2433. footprint = xp.asarray([[1, 0, 1], [1, 1, 0]])
  2434. structure = xp.asarray([[0, 0, 0], [0, 0, 0]])
  2435. tmp1 = ndimage.grey_dilation(array, footprint=footprint,
  2436. structure=structure)
  2437. tmp2 = ndimage.grey_erosion(array, footprint=footprint,
  2438. structure=structure)
  2439. expected = tmp1 - tmp2
  2440. output = ndimage.morphological_gradient(array, footprint=footprint,
  2441. structure=structure)
  2442. assert_array_almost_equal(output, expected)
  2443. @skip_xp_backends(np_only=True, reason='output= arrays are numpy-specific')
  2444. @make_xp_test_case(ndimage.grey_dilation, ndimage.grey_erosion,
  2445. ndimage.morphological_laplace)
  2446. def test_morphological_laplace01(self, xp):
  2447. array = xp.asarray([[3, 2, 5, 1, 4],
  2448. [7, 6, 9, 3, 5],
  2449. [5, 8, 3, 7, 1]])
  2450. footprint = xp.asarray([[1, 0, 1], [1, 1, 0]])
  2451. structure = xp.asarray([[0, 0, 0], [0, 0, 0]])
  2452. tmp1 = ndimage.grey_dilation(array, footprint=footprint,
  2453. structure=structure)
  2454. tmp2 = ndimage.grey_erosion(array, footprint=footprint,
  2455. structure=structure)
  2456. expected = tmp1 + tmp2 - 2 * array
  2457. output = xp.zeros(array.shape, dtype=array.dtype)
  2458. ndimage.morphological_laplace(array, footprint=footprint,
  2459. structure=structure, output=output)
  2460. assert_array_almost_equal(output, expected)
  2461. @make_xp_test_case(ndimage.grey_dilation, ndimage.grey_erosion,
  2462. ndimage.morphological_laplace)
  2463. def test_morphological_laplace02(self, xp):
  2464. array = xp.asarray([[3, 2, 5, 1, 4],
  2465. [7, 6, 9, 3, 5],
  2466. [5, 8, 3, 7, 1]])
  2467. footprint = xp.asarray([[1, 0, 1], [1, 1, 0]])
  2468. structure = xp.asarray([[0, 0, 0], [0, 0, 0]])
  2469. tmp1 = ndimage.grey_dilation(array, footprint=footprint,
  2470. structure=structure)
  2471. tmp2 = ndimage.grey_erosion(array, footprint=footprint,
  2472. structure=structure)
  2473. expected = tmp1 + tmp2 - 2 * array
  2474. output = ndimage.morphological_laplace(array, footprint=footprint,
  2475. structure=structure)
  2476. assert_array_almost_equal(output, expected)
  2477. @skip_xp_backends("jax.numpy", reason="output=array requires buffer view")
  2478. @skip_xp_backends("dask.array", reason="output=array requires buffer view")
  2479. @make_xp_test_case(ndimage.grey_opening, ndimage.white_tophat)
  2480. def test_white_tophat01(self, xp):
  2481. array = xp.asarray([[3, 2, 5, 1, 4],
  2482. [7, 6, 9, 3, 5],
  2483. [5, 8, 3, 7, 1]])
  2484. footprint = xp.asarray([[1, 0, 1], [1, 1, 0]])
  2485. structure = xp.asarray([[0, 0, 0], [0, 0, 0]])
  2486. tmp = ndimage.grey_opening(array, footprint=footprint,
  2487. structure=structure)
  2488. expected = array - tmp
  2489. output = xp.zeros(array.shape, dtype=array.dtype)
  2490. ndimage.white_tophat(array, footprint=footprint,
  2491. structure=structure, output=output)
  2492. assert_array_almost_equal(output, expected)
  2493. @make_xp_test_case(ndimage.grey_opening, ndimage.white_tophat)
  2494. def test_white_tophat02(self, xp):
  2495. array = xp.asarray([[3, 2, 5, 1, 4],
  2496. [7, 6, 9, 3, 5],
  2497. [5, 8, 3, 7, 1]])
  2498. footprint = xp.asarray([[1, 0, 1], [1, 1, 0]])
  2499. structure = xp.asarray([[0, 0, 0], [0, 0, 0]])
  2500. tmp = ndimage.grey_opening(array, footprint=footprint,
  2501. structure=structure)
  2502. expected = array - tmp
  2503. output = ndimage.white_tophat(array, footprint=footprint,
  2504. structure=structure)
  2505. assert_array_almost_equal(output, expected)
  2506. @xfail_xp_backends('cupy', reason="cupy#8399")
  2507. @make_xp_test_case(ndimage.white_tophat)
  2508. def test_white_tophat03(self, xp):
  2509. array = np.asarray([[1, 0, 0, 0, 0, 0, 0],
  2510. [0, 1, 1, 1, 1, 1, 0],
  2511. [0, 1, 1, 1, 1, 1, 0],
  2512. [0, 1, 1, 1, 1, 1, 0],
  2513. [0, 1, 1, 1, 0, 1, 0],
  2514. [0, 1, 1, 1, 1, 1, 0],
  2515. [0, 0, 0, 0, 0, 0, 1]], dtype=bool)
  2516. array = xp.asarray(array)
  2517. structure = np.ones((3, 3), dtype=bool)
  2518. structure = xp.asarray(structure)
  2519. expected = np.asarray([[0, 1, 1, 0, 0, 0, 0],
  2520. [1, 0, 0, 1, 1, 1, 0],
  2521. [1, 0, 0, 1, 1, 1, 0],
  2522. [0, 1, 1, 0, 0, 0, 1],
  2523. [0, 1, 1, 0, 1, 0, 1],
  2524. [0, 1, 1, 0, 0, 0, 1],
  2525. [0, 0, 0, 1, 1, 1, 1]], dtype=bool)
  2526. expected = xp.asarray(expected)
  2527. output = ndimage.white_tophat(array, structure=structure)
  2528. xp_assert_equal(output, expected)
  2529. @skip_xp_backends("jax.numpy", reason="output=array requires buffer view")
  2530. @skip_xp_backends("dask.array", reason="output=array requires buffer view")
  2531. @make_xp_test_case(ndimage.white_tophat)
  2532. def test_white_tophat04(self, xp):
  2533. array = np.eye(5, dtype=bool)
  2534. structure = np.ones((3, 3), dtype=bool)
  2535. array = xp.asarray(array)
  2536. structure = xp.asarray(structure)
  2537. # Check that type mismatch is properly handled
  2538. output = xp.empty_like(array, dtype=xp.float64)
  2539. ndimage.white_tophat(array, structure=structure, output=output)
  2540. @skip_xp_backends("jax.numpy", reason="output=array requires buffer view")
  2541. @skip_xp_backends("dask.array", reason="output=array requires buffer view")
  2542. @make_xp_test_case(ndimage.grey_closing, ndimage.black_tophat)
  2543. def test_black_tophat01(self, xp):
  2544. array = xp.asarray([[3, 2, 5, 1, 4],
  2545. [7, 6, 9, 3, 5],
  2546. [5, 8, 3, 7, 1]])
  2547. footprint = xp.asarray([[1, 0, 1], [1, 1, 0]])
  2548. structure = xp.asarray([[0, 0, 0], [0, 0, 0]])
  2549. tmp = ndimage.grey_closing(array, footprint=footprint,
  2550. structure=structure)
  2551. expected = tmp - array
  2552. output = xp.zeros(array.shape, dtype=array.dtype)
  2553. ndimage.black_tophat(array, footprint=footprint,
  2554. structure=structure, output=output)
  2555. assert_array_almost_equal(output, expected)
  2556. @make_xp_test_case(ndimage.grey_closing, ndimage.black_tophat)
  2557. def test_black_tophat02(self, xp):
  2558. array = xp.asarray([[3, 2, 5, 1, 4],
  2559. [7, 6, 9, 3, 5],
  2560. [5, 8, 3, 7, 1]])
  2561. footprint = xp.asarray([[1, 0, 1], [1, 1, 0]])
  2562. structure = xp.asarray([[0, 0, 0], [0, 0, 0]])
  2563. tmp = ndimage.grey_closing(array, footprint=footprint,
  2564. structure=structure)
  2565. expected = tmp - array
  2566. output = ndimage.black_tophat(array, footprint=footprint,
  2567. structure=structure)
  2568. assert_array_almost_equal(output, expected)
  2569. @xfail_xp_backends('cupy', reason="cupy/cupy#8399")
  2570. @make_xp_test_case(ndimage.black_tophat)
  2571. def test_black_tophat03(self, xp):
  2572. array = np.asarray([[1, 0, 0, 0, 0, 0, 0],
  2573. [0, 1, 1, 1, 1, 1, 0],
  2574. [0, 1, 1, 1, 1, 1, 0],
  2575. [0, 1, 1, 1, 1, 1, 0],
  2576. [0, 1, 1, 1, 0, 1, 0],
  2577. [0, 1, 1, 1, 1, 1, 0],
  2578. [0, 0, 0, 0, 0, 0, 1]], dtype=bool)
  2579. array = xp.asarray(array)
  2580. structure = np.ones((3, 3), dtype=bool)
  2581. structure = xp.asarray(structure)
  2582. expected = np.asarray([[0, 1, 1, 1, 1, 1, 1],
  2583. [1, 0, 0, 0, 0, 0, 1],
  2584. [1, 0, 0, 0, 0, 0, 1],
  2585. [1, 0, 0, 0, 0, 0, 1],
  2586. [1, 0, 0, 0, 1, 0, 1],
  2587. [1, 0, 0, 0, 0, 0, 1],
  2588. [1, 1, 1, 1, 1, 1, 0]], dtype=bool)
  2589. expected = xp.asarray(expected)
  2590. output = ndimage.black_tophat(array, structure=structure)
  2591. xp_assert_equal(output, expected)
  2592. @skip_xp_backends("jax.numpy", reason="output=array requires buffer view")
  2593. @skip_xp_backends("dask.array", reason="output=array requires buffer view")
  2594. @make_xp_test_case(ndimage.black_tophat)
  2595. def test_black_tophat04(self, xp):
  2596. array = xp.asarray(np.eye(5, dtype=bool))
  2597. structure = xp.asarray(np.ones((3, 3), dtype=bool))
  2598. # Check that type mismatch is properly handled
  2599. output = xp.empty_like(array, dtype=xp.float64)
  2600. ndimage.black_tophat(array, structure=structure, output=output)
  2601. @skip_xp_backends(cpu_only=True)
  2602. @skip_xp_backends(
  2603. "cupy", reason="these filters do not yet have axes support in CuPy")
  2604. @skip_xp_backends(
  2605. "jax.numpy", reason="these filters are not implemented in JAX.numpy")
  2606. @pytest.mark.parametrize('origin', [(0, 0), (-1, 0)])
  2607. @pytest.mark.parametrize('expand_axis', [0, 1, 2])
  2608. @pytest.mark.parametrize('mode', ['reflect', 'constant', 'nearest',
  2609. 'mirror', 'wrap'])
  2610. @pytest.mark.parametrize('footprint_mode', ['size', 'footprint',
  2611. 'structure'])
  2612. @pytest.mark.parametrize(
  2613. 'func',
  2614. [
  2615. make_xp_pytest_param(ndimage.grey_erosion),
  2616. make_xp_pytest_param(ndimage.grey_dilation),
  2617. make_xp_pytest_param(ndimage.grey_opening),
  2618. make_xp_pytest_param(ndimage.grey_closing),
  2619. make_xp_pytest_param(ndimage.morphological_laplace),
  2620. make_xp_pytest_param(ndimage.morphological_gradient),
  2621. make_xp_pytest_param(ndimage.white_tophat),
  2622. make_xp_pytest_param(ndimage.black_tophat),
  2623. ]
  2624. )
  2625. def test_grey_axes(self, xp, func, expand_axis, origin, footprint_mode,
  2626. mode):
  2627. data = xp.asarray([[0, 0, 0, 1, 0, 0, 0],
  2628. [0, 0, 0, 4, 0, 0, 0],
  2629. [0, 0, 2, 1, 0, 2, 0],
  2630. [0, 3, 0, 6, 5, 0, 1],
  2631. [0, 4, 5, 3, 3, 4, 0],
  2632. [0, 0, 9, 3, 0, 0, 0],
  2633. [0, 0, 0, 2, 0, 0, 0]])
  2634. kwargs = dict(origin=origin, mode=mode)
  2635. if footprint_mode == 'size':
  2636. kwargs['size'] = (2, 3)
  2637. else:
  2638. kwargs['footprint'] = xp.asarray([[1, 0, 1], [1, 1, 0]])
  2639. if footprint_mode == 'structure':
  2640. kwargs['structure'] = xp.ones_like(kwargs['footprint'])
  2641. expected = func(data, **kwargs)
  2642. # replicate data and expected result along a new axis
  2643. n_reps = 5
  2644. expected = xp.stack([expected] * n_reps, axis=expand_axis)
  2645. data = xp.stack([data] * n_reps, axis=expand_axis)
  2646. # filter all axes except expand_axis
  2647. axes = [0, 1, 2]
  2648. axes.remove(expand_axis)
  2649. if is_numpy(xp) or is_cupy(xp):
  2650. out = xp.zeros(expected.shape, dtype=expected.dtype)
  2651. func(data, output=out, axes=axes, **kwargs)
  2652. else:
  2653. # inplace output= is unsupported by JAX
  2654. out = func(data, axes=axes, **kwargs)
  2655. xp_assert_close(out, expected)
  2656. @skip_xp_backends(np_only=True, exceptions=["cupy"],
  2657. reason="inplace output= is numpy-specific")
  2658. @make_xp_test_case(ndimage.binary_hit_or_miss)
  2659. @pytest.mark.parametrize('dtype', types)
  2660. def test_hit_or_miss01(self, dtype, xp):
  2661. dtype = getattr(xp, dtype)
  2662. struct = [[0, 1, 0],
  2663. [1, 1, 1],
  2664. [0, 1, 0]]
  2665. struct = xp.asarray(struct)
  2666. expected = [[0, 0, 0, 0, 0],
  2667. [0, 1, 0, 0, 0],
  2668. [0, 0, 0, 0, 0],
  2669. [0, 0, 0, 0, 0],
  2670. [0, 0, 0, 0, 0],
  2671. [0, 0, 0, 0, 0],
  2672. [0, 0, 0, 0, 0],
  2673. [0, 0, 0, 0, 0]]
  2674. expected = xp.asarray(expected)
  2675. data = xp.asarray([[0, 1, 0, 0, 0],
  2676. [1, 1, 1, 0, 0],
  2677. [0, 1, 0, 1, 1],
  2678. [0, 0, 1, 1, 1],
  2679. [0, 1, 1, 1, 0],
  2680. [0, 1, 1, 1, 1],
  2681. [0, 1, 1, 1, 1],
  2682. [0, 0, 0, 0, 0]], dtype=dtype)
  2683. out = xp.asarray(np.zeros(data.shape, dtype=bool))
  2684. ndimage.binary_hit_or_miss(data, struct, output=out)
  2685. assert_array_almost_equal(out, expected)
  2686. @make_xp_test_case(ndimage.binary_hit_or_miss)
  2687. @pytest.mark.parametrize('dtype', types)
  2688. def test_hit_or_miss02(self, dtype, xp):
  2689. dtype = getattr(xp, dtype)
  2690. struct = [[0, 1, 0],
  2691. [1, 1, 1],
  2692. [0, 1, 0]]
  2693. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  2694. [0, 1, 0, 0, 0, 0, 0, 0],
  2695. [0, 0, 0, 0, 0, 0, 0, 0],
  2696. [0, 0, 0, 0, 0, 0, 0, 0]]
  2697. struct = xp.asarray(struct)
  2698. expected = xp.asarray(expected)
  2699. data = xp.asarray([[0, 1, 0, 0, 1, 1, 1, 0],
  2700. [1, 1, 1, 0, 0, 1, 0, 0],
  2701. [0, 1, 0, 1, 1, 1, 1, 0],
  2702. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  2703. out = ndimage.binary_hit_or_miss(data, struct)
  2704. assert_array_almost_equal(out, expected)
  2705. @make_xp_test_case(ndimage.binary_hit_or_miss)
  2706. @pytest.mark.parametrize('dtype', types)
  2707. def test_hit_or_miss03(self, dtype, xp):
  2708. dtype = getattr(xp, dtype)
  2709. struct1 = [[0, 0, 0],
  2710. [1, 1, 1],
  2711. [0, 0, 0]]
  2712. struct2 = [[1, 1, 1],
  2713. [0, 0, 0],
  2714. [1, 1, 1]]
  2715. expected = [[0, 0, 0, 0, 0, 1, 0, 0],
  2716. [0, 0, 0, 0, 0, 0, 0, 0],
  2717. [0, 0, 0, 0, 0, 0, 0, 0],
  2718. [0, 0, 0, 0, 0, 0, 0, 0],
  2719. [0, 0, 0, 0, 0, 0, 0, 0],
  2720. [0, 0, 0, 0, 0, 0, 0, 0],
  2721. [0, 0, 1, 0, 0, 0, 0, 0],
  2722. [0, 0, 0, 0, 0, 0, 0, 0]]
  2723. struct1 = xp.asarray(struct1)
  2724. struct2 = xp.asarray(struct2)
  2725. expected = xp.asarray(expected)
  2726. data = xp.asarray([[0, 1, 0, 0, 1, 1, 1, 0],
  2727. [1, 1, 1, 0, 0, 0, 0, 0],
  2728. [0, 1, 0, 1, 1, 1, 1, 0],
  2729. [0, 0, 1, 1, 1, 1, 1, 0],
  2730. [0, 1, 1, 1, 0, 1, 1, 0],
  2731. [0, 0, 0, 0, 1, 1, 1, 0],
  2732. [0, 1, 1, 1, 1, 1, 1, 0],
  2733. [0, 0, 0, 0, 0, 0, 0, 0]], dtype=dtype)
  2734. out = ndimage.binary_hit_or_miss(data, struct1, struct2)
  2735. assert_array_almost_equal(out, expected)
  2736. @make_xp_test_case(ndimage.binary_dilation, ndimage.grey_dilation)
  2737. class TestDilateFix:
  2738. # pytest's setup_method seems to clash with the autouse `xp` fixture
  2739. # so call _setup manually from all methods
  2740. def _setup(self, xp):
  2741. # dilation related setup
  2742. self.array = xp.asarray([[0, 0, 0, 0, 0],
  2743. [0, 0, 0, 0, 0],
  2744. [0, 0, 0, 1, 0],
  2745. [0, 0, 1, 1, 0],
  2746. [0, 0, 0, 0, 0]], dtype=xp.uint8)
  2747. self.sq3x3 = xp.ones((3, 3))
  2748. dilated3x3 = ndimage.binary_dilation(self.array, structure=self.sq3x3)
  2749. if is_numpy(xp):
  2750. self.dilated3x3 = dilated3x3.view(xp.uint8)
  2751. else:
  2752. self.dilated3x3 = xp.astype(dilated3x3, xp.uint8)
  2753. def test_dilation_square_structure(self, xp):
  2754. self._setup(xp)
  2755. result = ndimage.grey_dilation(self.array, structure=self.sq3x3)
  2756. # +1 accounts for difference between grey and binary dilation
  2757. assert_array_almost_equal(result, self.dilated3x3 + 1)
  2758. def test_dilation_scalar_size(self, xp):
  2759. self._setup(xp)
  2760. result = ndimage.grey_dilation(self.array, size=3)
  2761. assert_array_almost_equal(result, self.dilated3x3)
  2762. @make_xp_test_case(ndimage.binary_opening, ndimage.binary_closing)
  2763. class TestBinaryOpeningClosing:
  2764. def _setup(self, xp):
  2765. a = np.zeros((5, 5), dtype=bool)
  2766. a[1:4, 1:4] = True
  2767. a[4, 4] = True
  2768. self.array = xp.asarray(a)
  2769. self.sq3x3 = xp.ones((3, 3))
  2770. self.opened_old = ndimage.binary_opening(self.array, self.sq3x3,
  2771. 1, None, 0)
  2772. self.closed_old = ndimage.binary_closing(self.array, self.sq3x3,
  2773. 1, None, 0)
  2774. def test_opening_new_arguments(self, xp):
  2775. self._setup(xp)
  2776. opened_new = ndimage.binary_opening(self.array, self.sq3x3, 1, None,
  2777. 0, None, 0, False)
  2778. xp_assert_equal(opened_new, self.opened_old)
  2779. def test_closing_new_arguments(self, xp):
  2780. self._setup(xp)
  2781. closed_new = ndimage.binary_closing(self.array, self.sq3x3, 1, None,
  2782. 0, None, 0, False)
  2783. xp_assert_equal(closed_new, self.closed_old)
  2784. @make_xp_test_case(ndimage.binary_erosion)
  2785. def test_binary_erosion_noninteger_iterations(xp):
  2786. # regression test for gh-9905, gh-9909: ValueError for
  2787. # non integer iterations
  2788. data = xp.ones([1])
  2789. assert_raises(TypeError, ndimage.binary_erosion, data, iterations=0.5)
  2790. assert_raises(TypeError, ndimage.binary_erosion, data, iterations=1.5)
  2791. @make_xp_test_case(ndimage.binary_dilation)
  2792. def test_binary_dilation_noninteger_iterations(xp):
  2793. # regression test for gh-9905, gh-9909: ValueError for
  2794. # non integer iterations
  2795. data = xp.ones([1])
  2796. assert_raises(TypeError, ndimage.binary_dilation, data, iterations=0.5)
  2797. assert_raises(TypeError, ndimage.binary_dilation, data, iterations=1.5)
  2798. @make_xp_test_case(ndimage.binary_opening)
  2799. def test_binary_opening_noninteger_iterations(xp):
  2800. # regression test for gh-9905, gh-9909: ValueError for
  2801. # non integer iterations
  2802. data = xp.ones([1])
  2803. assert_raises(TypeError, ndimage.binary_opening, data, iterations=0.5)
  2804. assert_raises(TypeError, ndimage.binary_opening, data, iterations=1.5)
  2805. @make_xp_test_case(ndimage.binary_closing)
  2806. def test_binary_closing_noninteger_iterations(xp):
  2807. # regression test for gh-9905, gh-9909: ValueError for
  2808. # non integer iterations
  2809. data = xp.ones([1])
  2810. assert_raises(TypeError, ndimage.binary_closing, data, iterations=0.5)
  2811. assert_raises(TypeError, ndimage.binary_closing, data, iterations=1.5)
  2812. @xfail_xp_backends(
  2813. "cupy", reason="CuPy: NotImplementedError: only brute_force iteration"
  2814. )
  2815. @make_xp_test_case(ndimage.binary_erosion)
  2816. def test_binary_closing_noninteger_brute_force_passes_when_true(xp):
  2817. # regression test for gh-9905, gh-9909: ValueError for non integer iterations
  2818. data = xp.ones([1])
  2819. xp_assert_equal(ndimage.binary_erosion(data, iterations=2, brute_force=1.5),
  2820. ndimage.binary_erosion(data, iterations=2, brute_force=bool(1.5))
  2821. )
  2822. xp_assert_equal(ndimage.binary_erosion(data, iterations=2, brute_force=0.0),
  2823. ndimage.binary_erosion(data, iterations=2, brute_force=bool(0.0))
  2824. )
  2825. @skip_xp_backends(np_only=True, exceptions=["cupy"],
  2826. reason="inplace output= is numpy-specific")
  2827. @xfail_xp_backends("cupy", reason="NotImplementedError: only brute_force iteration")
  2828. @pytest.mark.parametrize(
  2829. 'func',
  2830. [
  2831. make_xp_pytest_param(ndimage.binary_erosion),
  2832. make_xp_pytest_param(ndimage.binary_dilation),
  2833. make_xp_pytest_param(ndimage.binary_opening),
  2834. make_xp_pytest_param(ndimage.binary_closing),
  2835. ],
  2836. )
  2837. @pytest.mark.parametrize('iterations', [1, 5])
  2838. @pytest.mark.parametrize('brute_force', [False, True])
  2839. def test_binary_input_as_output(func, iterations, brute_force, xp):
  2840. rstate = np.random.RandomState(123)
  2841. data = rstate.randint(low=0, high=2, size=100).astype(bool)
  2842. data = xp.asarray(data)
  2843. # input data is not modified
  2844. data_orig = data.copy()
  2845. expected = func(data, brute_force=brute_force, iterations=iterations)
  2846. xp_assert_equal(data, data_orig)
  2847. # data should now contain the expected result
  2848. func(data, brute_force=brute_force, iterations=iterations, output=data)
  2849. xp_assert_equal(data, expected)
  2850. @skip_xp_backends(np_only=True, exceptions=["cupy"],
  2851. reason="inplace output= is numpy-specific")
  2852. @make_xp_test_case(ndimage.binary_hit_or_miss)
  2853. def test_binary_hit_or_miss_input_as_output(xp):
  2854. rstate = np.random.RandomState(123)
  2855. data = rstate.randint(low=0, high=2, size=100).astype(bool)
  2856. data = xp.asarray(data)
  2857. # input data is not modified
  2858. data_orig = data.copy()
  2859. expected = ndimage.binary_hit_or_miss(data)
  2860. xp_assert_equal(data, data_orig)
  2861. # data should now contain the expected result
  2862. ndimage.binary_hit_or_miss(data, output=data)
  2863. xp_assert_equal(data, expected)
  2864. @make_xp_test_case(ndimage.distance_transform_cdt)
  2865. def test_distance_transform_cdt_invalid_metric(xp):
  2866. msg = 'invalid metric provided'
  2867. with pytest.raises(ValueError, match=msg):
  2868. ndimage.distance_transform_cdt(xp.ones((5, 5)),
  2869. metric="garbage")