test_ndimage.py 199 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624
  1. # Copyright (C) 2003-2005 Peter J. Verveer
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions
  5. # are met:
  6. #
  7. # 1. Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. #
  10. # 2. Redistributions in binary form must reproduce the above
  11. # copyright notice, this list of conditions and the following
  12. # disclaimer in the documentation and/or other materials provided
  13. # with the distribution.
  14. #
  15. # 3. The name of the author may not be used to endorse or promote
  16. # products derived from this software without specific prior
  17. # written permission.
  18. #
  19. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  20. # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  23. # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  27. # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. from __future__ import division, print_function, absolute_import
  31. import math
  32. import sys
  33. import numpy
  34. from numpy import fft
  35. from numpy.testing import (assert_, assert_equal, assert_array_equal,
  36. assert_array_almost_equal, assert_almost_equal)
  37. import pytest
  38. from pytest import raises as assert_raises
  39. from scipy._lib._numpy_compat import suppress_warnings
  40. import scipy.ndimage as ndimage
  41. eps = 1e-12
  42. def sumsq(a, b):
  43. return math.sqrt(((a - b)**2).sum())
  44. class TestNdimage:
  45. def setup_method(self):
  46. # list of numarray data types
  47. self.integer_types = [
  48. numpy.int8, numpy.uint8, numpy.int16, numpy.uint16,
  49. numpy.int32, numpy.uint32, numpy.int64, numpy.uint64]
  50. self.float_types = [numpy.float32, numpy.float64]
  51. self.types = self.integer_types + self.float_types
  52. # list of boundary modes:
  53. self.modes = ['nearest', 'wrap', 'reflect', 'mirror', 'constant']
  54. def test_correlate01(self):
  55. array = numpy.array([1, 2])
  56. weights = numpy.array([2])
  57. expected = [2, 4]
  58. output = ndimage.correlate(array, weights)
  59. assert_array_almost_equal(output, expected)
  60. output = ndimage.convolve(array, weights)
  61. assert_array_almost_equal(output, expected)
  62. output = ndimage.correlate1d(array, weights)
  63. assert_array_almost_equal(output, expected)
  64. output = ndimage.convolve1d(array, weights)
  65. assert_array_almost_equal(output, expected)
  66. def test_correlate02(self):
  67. array = numpy.array([1, 2, 3])
  68. kernel = numpy.array([1])
  69. output = ndimage.correlate(array, kernel)
  70. assert_array_almost_equal(array, output)
  71. output = ndimage.convolve(array, kernel)
  72. assert_array_almost_equal(array, output)
  73. output = ndimage.correlate1d(array, kernel)
  74. assert_array_almost_equal(array, output)
  75. output = ndimage.convolve1d(array, kernel)
  76. assert_array_almost_equal(array, output)
  77. def test_correlate03(self):
  78. array = numpy.array([1])
  79. weights = numpy.array([1, 1])
  80. expected = [2]
  81. output = ndimage.correlate(array, weights)
  82. assert_array_almost_equal(output, expected)
  83. output = ndimage.convolve(array, weights)
  84. assert_array_almost_equal(output, expected)
  85. output = ndimage.correlate1d(array, weights)
  86. assert_array_almost_equal(output, expected)
  87. output = ndimage.convolve1d(array, weights)
  88. assert_array_almost_equal(output, expected)
  89. def test_correlate04(self):
  90. array = numpy.array([1, 2])
  91. tcor = [2, 3]
  92. tcov = [3, 4]
  93. weights = numpy.array([1, 1])
  94. output = ndimage.correlate(array, weights)
  95. assert_array_almost_equal(output, tcor)
  96. output = ndimage.convolve(array, weights)
  97. assert_array_almost_equal(output, tcov)
  98. output = ndimage.correlate1d(array, weights)
  99. assert_array_almost_equal(output, tcor)
  100. output = ndimage.convolve1d(array, weights)
  101. assert_array_almost_equal(output, tcov)
  102. def test_correlate05(self):
  103. array = numpy.array([1, 2, 3])
  104. tcor = [2, 3, 5]
  105. tcov = [3, 5, 6]
  106. kernel = numpy.array([1, 1])
  107. output = ndimage.correlate(array, kernel)
  108. assert_array_almost_equal(tcor, output)
  109. output = ndimage.convolve(array, kernel)
  110. assert_array_almost_equal(tcov, output)
  111. output = ndimage.correlate1d(array, kernel)
  112. assert_array_almost_equal(tcor, output)
  113. output = ndimage.convolve1d(array, kernel)
  114. assert_array_almost_equal(tcov, output)
  115. def test_correlate06(self):
  116. array = numpy.array([1, 2, 3])
  117. tcor = [9, 14, 17]
  118. tcov = [7, 10, 15]
  119. weights = numpy.array([1, 2, 3])
  120. output = ndimage.correlate(array, weights)
  121. assert_array_almost_equal(output, tcor)
  122. output = ndimage.convolve(array, weights)
  123. assert_array_almost_equal(output, tcov)
  124. output = ndimage.correlate1d(array, weights)
  125. assert_array_almost_equal(output, tcor)
  126. output = ndimage.convolve1d(array, weights)
  127. assert_array_almost_equal(output, tcov)
  128. def test_correlate07(self):
  129. array = numpy.array([1, 2, 3])
  130. expected = [5, 8, 11]
  131. weights = numpy.array([1, 2, 1])
  132. output = ndimage.correlate(array, weights)
  133. assert_array_almost_equal(output, expected)
  134. output = ndimage.convolve(array, weights)
  135. assert_array_almost_equal(output, expected)
  136. output = ndimage.correlate1d(array, weights)
  137. assert_array_almost_equal(output, expected)
  138. output = ndimage.convolve1d(array, weights)
  139. assert_array_almost_equal(output, expected)
  140. def test_correlate08(self):
  141. array = numpy.array([1, 2, 3])
  142. tcor = [1, 2, 5]
  143. tcov = [3, 6, 7]
  144. weights = numpy.array([1, 2, -1])
  145. output = ndimage.correlate(array, weights)
  146. assert_array_almost_equal(output, tcor)
  147. output = ndimage.convolve(array, weights)
  148. assert_array_almost_equal(output, tcov)
  149. output = ndimage.correlate1d(array, weights)
  150. assert_array_almost_equal(output, tcor)
  151. output = ndimage.convolve1d(array, weights)
  152. assert_array_almost_equal(output, tcov)
  153. def test_correlate09(self):
  154. array = []
  155. kernel = numpy.array([1, 1])
  156. output = ndimage.correlate(array, kernel)
  157. assert_array_almost_equal(array, output)
  158. output = ndimage.convolve(array, kernel)
  159. assert_array_almost_equal(array, output)
  160. output = ndimage.correlate1d(array, kernel)
  161. assert_array_almost_equal(array, output)
  162. output = ndimage.convolve1d(array, kernel)
  163. assert_array_almost_equal(array, output)
  164. def test_correlate10(self):
  165. array = [[]]
  166. kernel = numpy.array([[1, 1]])
  167. output = ndimage.correlate(array, kernel)
  168. assert_array_almost_equal(array, output)
  169. output = ndimage.convolve(array, kernel)
  170. assert_array_almost_equal(array, output)
  171. def test_correlate11(self):
  172. array = numpy.array([[1, 2, 3],
  173. [4, 5, 6]])
  174. kernel = numpy.array([[1, 1],
  175. [1, 1]])
  176. output = ndimage.correlate(array, kernel)
  177. assert_array_almost_equal([[4, 6, 10], [10, 12, 16]], output)
  178. output = ndimage.convolve(array, kernel)
  179. assert_array_almost_equal([[12, 16, 18], [18, 22, 24]], output)
  180. def test_correlate12(self):
  181. array = numpy.array([[1, 2, 3],
  182. [4, 5, 6]])
  183. kernel = numpy.array([[1, 0],
  184. [0, 1]])
  185. output = ndimage.correlate(array, kernel)
  186. assert_array_almost_equal([[2, 3, 5], [5, 6, 8]], output)
  187. output = ndimage.convolve(array, kernel)
  188. assert_array_almost_equal([[6, 8, 9], [9, 11, 12]], output)
  189. def test_correlate13(self):
  190. kernel = numpy.array([[1, 0],
  191. [0, 1]])
  192. for type1 in self.types:
  193. array = numpy.array([[1, 2, 3],
  194. [4, 5, 6]], type1)
  195. for type2 in self.types:
  196. output = ndimage.correlate(array, kernel, output=type2)
  197. assert_array_almost_equal([[2, 3, 5], [5, 6, 8]], output)
  198. assert_equal(output.dtype.type, type2)
  199. output = ndimage.convolve(array, kernel,
  200. output=type2)
  201. assert_array_almost_equal([[6, 8, 9], [9, 11, 12]], output)
  202. assert_equal(output.dtype.type, type2)
  203. def test_correlate14(self):
  204. kernel = numpy.array([[1, 0],
  205. [0, 1]])
  206. for type1 in self.types:
  207. array = numpy.array([[1, 2, 3],
  208. [4, 5, 6]], type1)
  209. for type2 in self.types:
  210. output = numpy.zeros(array.shape, type2)
  211. ndimage.correlate(array, kernel,
  212. output=output)
  213. assert_array_almost_equal([[2, 3, 5], [5, 6, 8]], output)
  214. assert_equal(output.dtype.type, type2)
  215. ndimage.convolve(array, kernel, output=output)
  216. assert_array_almost_equal([[6, 8, 9], [9, 11, 12]], output)
  217. assert_equal(output.dtype.type, type2)
  218. def test_correlate15(self):
  219. kernel = numpy.array([[1, 0],
  220. [0, 1]])
  221. for type1 in self.types:
  222. array = numpy.array([[1, 2, 3],
  223. [4, 5, 6]], type1)
  224. output = ndimage.correlate(array, kernel,
  225. output=numpy.float32)
  226. assert_array_almost_equal([[2, 3, 5], [5, 6, 8]], output)
  227. assert_equal(output.dtype.type, numpy.float32)
  228. output = ndimage.convolve(array, kernel,
  229. output=numpy.float32)
  230. assert_array_almost_equal([[6, 8, 9], [9, 11, 12]], output)
  231. assert_equal(output.dtype.type, numpy.float32)
  232. def test_correlate16(self):
  233. kernel = numpy.array([[0.5, 0],
  234. [0, 0.5]])
  235. for type1 in self.types:
  236. array = numpy.array([[1, 2, 3], [4, 5, 6]], type1)
  237. output = ndimage.correlate(array, kernel, output=numpy.float32)
  238. assert_array_almost_equal([[1, 1.5, 2.5], [2.5, 3, 4]], output)
  239. assert_equal(output.dtype.type, numpy.float32)
  240. output = ndimage.convolve(array, kernel, output=numpy.float32)
  241. assert_array_almost_equal([[3, 4, 4.5], [4.5, 5.5, 6]], output)
  242. assert_equal(output.dtype.type, numpy.float32)
  243. def test_correlate17(self):
  244. array = numpy.array([1, 2, 3])
  245. tcor = [3, 5, 6]
  246. tcov = [2, 3, 5]
  247. kernel = numpy.array([1, 1])
  248. output = ndimage.correlate(array, kernel, origin=-1)
  249. assert_array_almost_equal(tcor, output)
  250. output = ndimage.convolve(array, kernel, origin=-1)
  251. assert_array_almost_equal(tcov, output)
  252. output = ndimage.correlate1d(array, kernel, origin=-1)
  253. assert_array_almost_equal(tcor, output)
  254. output = ndimage.convolve1d(array, kernel, origin=-1)
  255. assert_array_almost_equal(tcov, output)
  256. def test_correlate18(self):
  257. kernel = numpy.array([[1, 0],
  258. [0, 1]])
  259. for type1 in self.types:
  260. array = numpy.array([[1, 2, 3],
  261. [4, 5, 6]], type1)
  262. output = ndimage.correlate(array, kernel,
  263. output=numpy.float32,
  264. mode='nearest', origin=-1)
  265. assert_array_almost_equal([[6, 8, 9], [9, 11, 12]], output)
  266. assert_equal(output.dtype.type, numpy.float32)
  267. output = ndimage.convolve(array, kernel,
  268. output=numpy.float32,
  269. mode='nearest', origin=-1)
  270. assert_array_almost_equal([[2, 3, 5], [5, 6, 8]], output)
  271. assert_equal(output.dtype.type, numpy.float32)
  272. def test_correlate19(self):
  273. kernel = numpy.array([[1, 0],
  274. [0, 1]])
  275. for type1 in self.types:
  276. array = numpy.array([[1, 2, 3],
  277. [4, 5, 6]], type1)
  278. output = ndimage.correlate(array, kernel,
  279. output=numpy.float32,
  280. mode='nearest', origin=[-1, 0])
  281. assert_array_almost_equal([[5, 6, 8], [8, 9, 11]], output)
  282. assert_equal(output.dtype.type, numpy.float32)
  283. output = ndimage.convolve(array, kernel,
  284. output=numpy.float32,
  285. mode='nearest', origin=[-1, 0])
  286. assert_array_almost_equal([[3, 5, 6], [6, 8, 9]], output)
  287. assert_equal(output.dtype.type, numpy.float32)
  288. def test_correlate20(self):
  289. weights = numpy.array([1, 2, 1])
  290. expected = [[5, 10, 15], [7, 14, 21]]
  291. for type1 in self.types:
  292. array = numpy.array([[1, 2, 3],
  293. [2, 4, 6]], type1)
  294. for type2 in self.types:
  295. output = numpy.zeros((2, 3), type2)
  296. ndimage.correlate1d(array, weights, axis=0,
  297. output=output)
  298. assert_array_almost_equal(output, expected)
  299. ndimage.convolve1d(array, weights, axis=0,
  300. output=output)
  301. assert_array_almost_equal(output, expected)
  302. def test_correlate21(self):
  303. array = numpy.array([[1, 2, 3],
  304. [2, 4, 6]])
  305. expected = [[5, 10, 15], [7, 14, 21]]
  306. weights = numpy.array([1, 2, 1])
  307. output = ndimage.correlate1d(array, weights, axis=0)
  308. assert_array_almost_equal(output, expected)
  309. output = ndimage.convolve1d(array, weights, axis=0)
  310. assert_array_almost_equal(output, expected)
  311. def test_correlate22(self):
  312. weights = numpy.array([1, 2, 1])
  313. expected = [[6, 12, 18], [6, 12, 18]]
  314. for type1 in self.types:
  315. array = numpy.array([[1, 2, 3],
  316. [2, 4, 6]], type1)
  317. for type2 in self.types:
  318. output = numpy.zeros((2, 3), type2)
  319. ndimage.correlate1d(array, weights, axis=0,
  320. mode='wrap', output=output)
  321. assert_array_almost_equal(output, expected)
  322. ndimage.convolve1d(array, weights, axis=0,
  323. mode='wrap', output=output)
  324. assert_array_almost_equal(output, expected)
  325. def test_correlate23(self):
  326. weights = numpy.array([1, 2, 1])
  327. expected = [[5, 10, 15], [7, 14, 21]]
  328. for type1 in self.types:
  329. array = numpy.array([[1, 2, 3],
  330. [2, 4, 6]], type1)
  331. for type2 in self.types:
  332. output = numpy.zeros((2, 3), type2)
  333. ndimage.correlate1d(array, weights, axis=0,
  334. mode='nearest', output=output)
  335. assert_array_almost_equal(output, expected)
  336. ndimage.convolve1d(array, weights, axis=0,
  337. mode='nearest', output=output)
  338. assert_array_almost_equal(output, expected)
  339. def test_correlate24(self):
  340. weights = numpy.array([1, 2, 1])
  341. tcor = [[7, 14, 21], [8, 16, 24]]
  342. tcov = [[4, 8, 12], [5, 10, 15]]
  343. for type1 in self.types:
  344. array = numpy.array([[1, 2, 3],
  345. [2, 4, 6]], type1)
  346. for type2 in self.types:
  347. output = numpy.zeros((2, 3), type2)
  348. ndimage.correlate1d(array, weights, axis=0,
  349. mode='nearest', output=output, origin=-1)
  350. assert_array_almost_equal(output, tcor)
  351. ndimage.convolve1d(array, weights, axis=0,
  352. mode='nearest', output=output, origin=-1)
  353. assert_array_almost_equal(output, tcov)
  354. def test_correlate25(self):
  355. weights = numpy.array([1, 2, 1])
  356. tcor = [[4, 8, 12], [5, 10, 15]]
  357. tcov = [[7, 14, 21], [8, 16, 24]]
  358. for type1 in self.types:
  359. array = numpy.array([[1, 2, 3],
  360. [2, 4, 6]], type1)
  361. for type2 in self.types:
  362. output = numpy.zeros((2, 3), type2)
  363. ndimage.correlate1d(array, weights, axis=0,
  364. mode='nearest', output=output, origin=1)
  365. assert_array_almost_equal(output, tcor)
  366. ndimage.convolve1d(array, weights, axis=0,
  367. mode='nearest', output=output, origin=1)
  368. assert_array_almost_equal(output, tcov)
  369. def test_gauss01(self):
  370. input = numpy.array([[1, 2, 3],
  371. [2, 4, 6]], numpy.float32)
  372. output = ndimage.gaussian_filter(input, 0)
  373. assert_array_almost_equal(output, input)
  374. def test_gauss02(self):
  375. input = numpy.array([[1, 2, 3],
  376. [2, 4, 6]], numpy.float32)
  377. output = ndimage.gaussian_filter(input, 1.0)
  378. assert_equal(input.dtype, output.dtype)
  379. assert_equal(input.shape, output.shape)
  380. def test_gauss03(self):
  381. # single precision data"
  382. input = numpy.arange(100 * 100).astype(numpy.float32)
  383. input.shape = (100, 100)
  384. output = ndimage.gaussian_filter(input, [1.0, 1.0])
  385. assert_equal(input.dtype, output.dtype)
  386. assert_equal(input.shape, output.shape)
  387. # input.sum() is 49995000.0. With single precision floats, we can't
  388. # expect more than 8 digits of accuracy, so use decimal=0 in this test.
  389. assert_almost_equal(output.sum(dtype='d'), input.sum(dtype='d'),
  390. decimal=0)
  391. assert_(sumsq(input, output) > 1.0)
  392. def test_gauss04(self):
  393. input = numpy.arange(100 * 100).astype(numpy.float32)
  394. input.shape = (100, 100)
  395. otype = numpy.float64
  396. output = ndimage.gaussian_filter(input, [1.0, 1.0], output=otype)
  397. assert_equal(output.dtype.type, numpy.float64)
  398. assert_equal(input.shape, output.shape)
  399. assert_(sumsq(input, output) > 1.0)
  400. def test_gauss05(self):
  401. input = numpy.arange(100 * 100).astype(numpy.float32)
  402. input.shape = (100, 100)
  403. otype = numpy.float64
  404. output = ndimage.gaussian_filter(input, [1.0, 1.0],
  405. order=1, output=otype)
  406. assert_equal(output.dtype.type, numpy.float64)
  407. assert_equal(input.shape, output.shape)
  408. assert_(sumsq(input, output) > 1.0)
  409. def test_gauss06(self):
  410. input = numpy.arange(100 * 100).astype(numpy.float32)
  411. input.shape = (100, 100)
  412. otype = numpy.float64
  413. output1 = ndimage.gaussian_filter(input, [1.0, 1.0], output=otype)
  414. output2 = ndimage.gaussian_filter(input, 1.0, output=otype)
  415. assert_array_almost_equal(output1, output2)
  416. def test_prewitt01(self):
  417. for type_ in self.types:
  418. array = numpy.array([[3, 2, 5, 1, 4],
  419. [5, 8, 3, 7, 1],
  420. [5, 6, 9, 3, 5]], type_)
  421. t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 0)
  422. t = ndimage.correlate1d(t, [1.0, 1.0, 1.0], 1)
  423. output = ndimage.prewitt(array, 0)
  424. assert_array_almost_equal(t, output)
  425. def test_prewitt02(self):
  426. for type_ in self.types:
  427. array = numpy.array([[3, 2, 5, 1, 4],
  428. [5, 8, 3, 7, 1],
  429. [5, 6, 9, 3, 5]], type_)
  430. t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 0)
  431. t = ndimage.correlate1d(t, [1.0, 1.0, 1.0], 1)
  432. output = numpy.zeros(array.shape, type_)
  433. ndimage.prewitt(array, 0, output)
  434. assert_array_almost_equal(t, output)
  435. def test_prewitt03(self):
  436. for type_ in self.types:
  437. array = numpy.array([[3, 2, 5, 1, 4],
  438. [5, 8, 3, 7, 1],
  439. [5, 6, 9, 3, 5]], type_)
  440. t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 1)
  441. t = ndimage.correlate1d(t, [1.0, 1.0, 1.0], 0)
  442. output = ndimage.prewitt(array, 1)
  443. assert_array_almost_equal(t, output)
  444. def test_prewitt04(self):
  445. for type_ in self.types:
  446. array = numpy.array([[3, 2, 5, 1, 4],
  447. [5, 8, 3, 7, 1],
  448. [5, 6, 9, 3, 5]], type_)
  449. t = ndimage.prewitt(array, -1)
  450. output = ndimage.prewitt(array, 1)
  451. assert_array_almost_equal(t, output)
  452. def test_sobel01(self):
  453. for type_ in self.types:
  454. array = numpy.array([[3, 2, 5, 1, 4],
  455. [5, 8, 3, 7, 1],
  456. [5, 6, 9, 3, 5]], type_)
  457. t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 0)
  458. t = ndimage.correlate1d(t, [1.0, 2.0, 1.0], 1)
  459. output = ndimage.sobel(array, 0)
  460. assert_array_almost_equal(t, output)
  461. def test_sobel02(self):
  462. for type_ in self.types:
  463. array = numpy.array([[3, 2, 5, 1, 4],
  464. [5, 8, 3, 7, 1],
  465. [5, 6, 9, 3, 5]], type_)
  466. t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 0)
  467. t = ndimage.correlate1d(t, [1.0, 2.0, 1.0], 1)
  468. output = numpy.zeros(array.shape, type_)
  469. ndimage.sobel(array, 0, output)
  470. assert_array_almost_equal(t, output)
  471. def test_sobel03(self):
  472. for type_ in self.types:
  473. array = numpy.array([[3, 2, 5, 1, 4],
  474. [5, 8, 3, 7, 1],
  475. [5, 6, 9, 3, 5]], type_)
  476. t = ndimage.correlate1d(array, [-1.0, 0.0, 1.0], 1)
  477. t = ndimage.correlate1d(t, [1.0, 2.0, 1.0], 0)
  478. output = numpy.zeros(array.shape, type_)
  479. output = ndimage.sobel(array, 1)
  480. assert_array_almost_equal(t, output)
  481. def test_sobel04(self):
  482. for type_ in self.types:
  483. array = numpy.array([[3, 2, 5, 1, 4],
  484. [5, 8, 3, 7, 1],
  485. [5, 6, 9, 3, 5]], type_)
  486. t = ndimage.sobel(array, -1)
  487. output = ndimage.sobel(array, 1)
  488. assert_array_almost_equal(t, output)
  489. def test_laplace01(self):
  490. for type_ in [numpy.int32, numpy.float32, numpy.float64]:
  491. array = numpy.array([[3, 2, 5, 1, 4],
  492. [5, 8, 3, 7, 1],
  493. [5, 6, 9, 3, 5]], type_) * 100
  494. tmp1 = ndimage.correlate1d(array, [1, -2, 1], 0)
  495. tmp2 = ndimage.correlate1d(array, [1, -2, 1], 1)
  496. output = ndimage.laplace(array)
  497. assert_array_almost_equal(tmp1 + tmp2, output)
  498. def test_laplace02(self):
  499. for type_ in [numpy.int32, numpy.float32, numpy.float64]:
  500. array = numpy.array([[3, 2, 5, 1, 4],
  501. [5, 8, 3, 7, 1],
  502. [5, 6, 9, 3, 5]], type_) * 100
  503. tmp1 = ndimage.correlate1d(array, [1, -2, 1], 0)
  504. tmp2 = ndimage.correlate1d(array, [1, -2, 1], 1)
  505. output = numpy.zeros(array.shape, type_)
  506. ndimage.laplace(array, output=output)
  507. assert_array_almost_equal(tmp1 + tmp2, output)
  508. def test_gaussian_laplace01(self):
  509. for type_ in [numpy.int32, numpy.float32, numpy.float64]:
  510. array = numpy.array([[3, 2, 5, 1, 4],
  511. [5, 8, 3, 7, 1],
  512. [5, 6, 9, 3, 5]], type_) * 100
  513. tmp1 = ndimage.gaussian_filter(array, 1.0, [2, 0])
  514. tmp2 = ndimage.gaussian_filter(array, 1.0, [0, 2])
  515. output = ndimage.gaussian_laplace(array, 1.0)
  516. assert_array_almost_equal(tmp1 + tmp2, output)
  517. def test_gaussian_laplace02(self):
  518. for type_ in [numpy.int32, numpy.float32, numpy.float64]:
  519. array = numpy.array([[3, 2, 5, 1, 4],
  520. [5, 8, 3, 7, 1],
  521. [5, 6, 9, 3, 5]], type_) * 100
  522. tmp1 = ndimage.gaussian_filter(array, 1.0, [2, 0])
  523. tmp2 = ndimage.gaussian_filter(array, 1.0, [0, 2])
  524. output = numpy.zeros(array.shape, type_)
  525. ndimage.gaussian_laplace(array, 1.0, output)
  526. assert_array_almost_equal(tmp1 + tmp2, output)
  527. def test_generic_laplace01(self):
  528. def derivative2(input, axis, output, mode, cval, a, b):
  529. sigma = [a, b / 2.0]
  530. input = numpy.asarray(input)
  531. order = [0] * input.ndim
  532. order[axis] = 2
  533. return ndimage.gaussian_filter(input, sigma, order,
  534. output, mode, cval)
  535. for type_ in self.types:
  536. array = numpy.array([[3, 2, 5, 1, 4],
  537. [5, 8, 3, 7, 1],
  538. [5, 6, 9, 3, 5]], type_)
  539. output = numpy.zeros(array.shape, type_)
  540. tmp = ndimage.generic_laplace(array, derivative2,
  541. extra_arguments=(1.0,),
  542. extra_keywords={'b': 2.0})
  543. ndimage.gaussian_laplace(array, 1.0, output)
  544. assert_array_almost_equal(tmp, output)
  545. def test_gaussian_gradient_magnitude01(self):
  546. for type_ in [numpy.int32, numpy.float32, numpy.float64]:
  547. array = numpy.array([[3, 2, 5, 1, 4],
  548. [5, 8, 3, 7, 1],
  549. [5, 6, 9, 3, 5]], type_) * 100
  550. tmp1 = ndimage.gaussian_filter(array, 1.0, [1, 0])
  551. tmp2 = ndimage.gaussian_filter(array, 1.0, [0, 1])
  552. output = ndimage.gaussian_gradient_magnitude(array, 1.0)
  553. expected = tmp1 * tmp1 + tmp2 * tmp2
  554. expected = numpy.sqrt(expected).astype(type_)
  555. assert_array_almost_equal(expected, output)
  556. def test_gaussian_gradient_magnitude02(self):
  557. for type_ in [numpy.int32, numpy.float32, numpy.float64]:
  558. array = numpy.array([[3, 2, 5, 1, 4],
  559. [5, 8, 3, 7, 1],
  560. [5, 6, 9, 3, 5]], type_) * 100
  561. tmp1 = ndimage.gaussian_filter(array, 1.0, [1, 0])
  562. tmp2 = ndimage.gaussian_filter(array, 1.0, [0, 1])
  563. output = numpy.zeros(array.shape, type_)
  564. ndimage.gaussian_gradient_magnitude(array, 1.0, output)
  565. expected = tmp1 * tmp1 + tmp2 * tmp2
  566. expected = numpy.sqrt(expected).astype(type_)
  567. assert_array_almost_equal(expected, output)
  568. def test_generic_gradient_magnitude01(self):
  569. array = numpy.array([[3, 2, 5, 1, 4],
  570. [5, 8, 3, 7, 1],
  571. [5, 6, 9, 3, 5]], numpy.float64)
  572. def derivative(input, axis, output, mode, cval, a, b):
  573. sigma = [a, b / 2.0]
  574. input = numpy.asarray(input)
  575. order = [0] * input.ndim
  576. order[axis] = 1
  577. return ndimage.gaussian_filter(input, sigma, order,
  578. output, mode, cval)
  579. tmp1 = ndimage.gaussian_gradient_magnitude(array, 1.0)
  580. tmp2 = ndimage.generic_gradient_magnitude(
  581. array, derivative, extra_arguments=(1.0,),
  582. extra_keywords={'b': 2.0})
  583. assert_array_almost_equal(tmp1, tmp2)
  584. def test_uniform01(self):
  585. array = numpy.array([2, 4, 6])
  586. size = 2
  587. output = ndimage.uniform_filter1d(array, size, origin=-1)
  588. assert_array_almost_equal([3, 5, 6], output)
  589. def test_uniform02(self):
  590. array = numpy.array([1, 2, 3])
  591. filter_shape = [0]
  592. output = ndimage.uniform_filter(array, filter_shape)
  593. assert_array_almost_equal(array, output)
  594. def test_uniform03(self):
  595. array = numpy.array([1, 2, 3])
  596. filter_shape = [1]
  597. output = ndimage.uniform_filter(array, filter_shape)
  598. assert_array_almost_equal(array, output)
  599. def test_uniform04(self):
  600. array = numpy.array([2, 4, 6])
  601. filter_shape = [2]
  602. output = ndimage.uniform_filter(array, filter_shape)
  603. assert_array_almost_equal([2, 3, 5], output)
  604. def test_uniform05(self):
  605. array = []
  606. filter_shape = [1]
  607. output = ndimage.uniform_filter(array, filter_shape)
  608. assert_array_almost_equal([], output)
  609. def test_uniform06(self):
  610. filter_shape = [2, 2]
  611. for type1 in self.types:
  612. array = numpy.array([[4, 8, 12],
  613. [16, 20, 24]], type1)
  614. for type2 in self.types:
  615. output = ndimage.uniform_filter(
  616. array, filter_shape, output=type2)
  617. assert_array_almost_equal([[4, 6, 10], [10, 12, 16]], output)
  618. assert_equal(output.dtype.type, type2)
  619. def test_minimum_filter01(self):
  620. array = numpy.array([1, 2, 3, 4, 5])
  621. filter_shape = numpy.array([2])
  622. output = ndimage.minimum_filter(array, filter_shape)
  623. assert_array_almost_equal([1, 1, 2, 3, 4], output)
  624. def test_minimum_filter02(self):
  625. array = numpy.array([1, 2, 3, 4, 5])
  626. filter_shape = numpy.array([3])
  627. output = ndimage.minimum_filter(array, filter_shape)
  628. assert_array_almost_equal([1, 1, 2, 3, 4], output)
  629. def test_minimum_filter03(self):
  630. array = numpy.array([3, 2, 5, 1, 4])
  631. filter_shape = numpy.array([2])
  632. output = ndimage.minimum_filter(array, filter_shape)
  633. assert_array_almost_equal([3, 2, 2, 1, 1], output)
  634. def test_minimum_filter04(self):
  635. array = numpy.array([3, 2, 5, 1, 4])
  636. filter_shape = numpy.array([3])
  637. output = ndimage.minimum_filter(array, filter_shape)
  638. assert_array_almost_equal([2, 2, 1, 1, 1], output)
  639. def test_minimum_filter05(self):
  640. array = numpy.array([[3, 2, 5, 1, 4],
  641. [7, 6, 9, 3, 5],
  642. [5, 8, 3, 7, 1]])
  643. filter_shape = numpy.array([2, 3])
  644. output = ndimage.minimum_filter(array, filter_shape)
  645. assert_array_almost_equal([[2, 2, 1, 1, 1],
  646. [2, 2, 1, 1, 1],
  647. [5, 3, 3, 1, 1]], output)
  648. def test_minimum_filter06(self):
  649. array = numpy.array([[3, 2, 5, 1, 4],
  650. [7, 6, 9, 3, 5],
  651. [5, 8, 3, 7, 1]])
  652. footprint = [[1, 1, 1], [1, 1, 1]]
  653. output = ndimage.minimum_filter(array, footprint=footprint)
  654. assert_array_almost_equal([[2, 2, 1, 1, 1],
  655. [2, 2, 1, 1, 1],
  656. [5, 3, 3, 1, 1]], output)
  657. def test_minimum_filter07(self):
  658. array = numpy.array([[3, 2, 5, 1, 4],
  659. [7, 6, 9, 3, 5],
  660. [5, 8, 3, 7, 1]])
  661. footprint = [[1, 0, 1], [1, 1, 0]]
  662. output = ndimage.minimum_filter(array, footprint=footprint)
  663. assert_array_almost_equal([[2, 2, 1, 1, 1],
  664. [2, 3, 1, 3, 1],
  665. [5, 5, 3, 3, 1]], output)
  666. def test_minimum_filter08(self):
  667. array = numpy.array([[3, 2, 5, 1, 4],
  668. [7, 6, 9, 3, 5],
  669. [5, 8, 3, 7, 1]])
  670. footprint = [[1, 0, 1], [1, 1, 0]]
  671. output = ndimage.minimum_filter(array, footprint=footprint, origin=-1)
  672. assert_array_almost_equal([[3, 1, 3, 1, 1],
  673. [5, 3, 3, 1, 1],
  674. [3, 3, 1, 1, 1]], output)
  675. def test_minimum_filter09(self):
  676. array = numpy.array([[3, 2, 5, 1, 4],
  677. [7, 6, 9, 3, 5],
  678. [5, 8, 3, 7, 1]])
  679. footprint = [[1, 0, 1], [1, 1, 0]]
  680. output = ndimage.minimum_filter(array, footprint=footprint,
  681. origin=[-1, 0])
  682. assert_array_almost_equal([[2, 3, 1, 3, 1],
  683. [5, 5, 3, 3, 1],
  684. [5, 3, 3, 1, 1]], output)
  685. def test_maximum_filter01(self):
  686. array = numpy.array([1, 2, 3, 4, 5])
  687. filter_shape = numpy.array([2])
  688. output = ndimage.maximum_filter(array, filter_shape)
  689. assert_array_almost_equal([1, 2, 3, 4, 5], output)
  690. def test_maximum_filter02(self):
  691. array = numpy.array([1, 2, 3, 4, 5])
  692. filter_shape = numpy.array([3])
  693. output = ndimage.maximum_filter(array, filter_shape)
  694. assert_array_almost_equal([2, 3, 4, 5, 5], output)
  695. def test_maximum_filter03(self):
  696. array = numpy.array([3, 2, 5, 1, 4])
  697. filter_shape = numpy.array([2])
  698. output = ndimage.maximum_filter(array, filter_shape)
  699. assert_array_almost_equal([3, 3, 5, 5, 4], output)
  700. def test_maximum_filter04(self):
  701. array = numpy.array([3, 2, 5, 1, 4])
  702. filter_shape = numpy.array([3])
  703. output = ndimage.maximum_filter(array, filter_shape)
  704. assert_array_almost_equal([3, 5, 5, 5, 4], output)
  705. def test_maximum_filter05(self):
  706. array = numpy.array([[3, 2, 5, 1, 4],
  707. [7, 6, 9, 3, 5],
  708. [5, 8, 3, 7, 1]])
  709. filter_shape = numpy.array([2, 3])
  710. output = ndimage.maximum_filter(array, filter_shape)
  711. assert_array_almost_equal([[3, 5, 5, 5, 4],
  712. [7, 9, 9, 9, 5],
  713. [8, 9, 9, 9, 7]], output)
  714. def test_maximum_filter06(self):
  715. array = numpy.array([[3, 2, 5, 1, 4],
  716. [7, 6, 9, 3, 5],
  717. [5, 8, 3, 7, 1]])
  718. footprint = [[1, 1, 1], [1, 1, 1]]
  719. output = ndimage.maximum_filter(array, footprint=footprint)
  720. assert_array_almost_equal([[3, 5, 5, 5, 4],
  721. [7, 9, 9, 9, 5],
  722. [8, 9, 9, 9, 7]], output)
  723. def test_maximum_filter07(self):
  724. array = numpy.array([[3, 2, 5, 1, 4],
  725. [7, 6, 9, 3, 5],
  726. [5, 8, 3, 7, 1]])
  727. footprint = [[1, 0, 1], [1, 1, 0]]
  728. output = ndimage.maximum_filter(array, footprint=footprint)
  729. assert_array_almost_equal([[3, 5, 5, 5, 4],
  730. [7, 7, 9, 9, 5],
  731. [7, 9, 8, 9, 7]], output)
  732. def test_maximum_filter08(self):
  733. array = numpy.array([[3, 2, 5, 1, 4],
  734. [7, 6, 9, 3, 5],
  735. [5, 8, 3, 7, 1]])
  736. footprint = [[1, 0, 1], [1, 1, 0]]
  737. output = ndimage.maximum_filter(array, footprint=footprint, origin=-1)
  738. assert_array_almost_equal([[7, 9, 9, 5, 5],
  739. [9, 8, 9, 7, 5],
  740. [8, 8, 7, 7, 7]], output)
  741. def test_maximum_filter09(self):
  742. array = numpy.array([[3, 2, 5, 1, 4],
  743. [7, 6, 9, 3, 5],
  744. [5, 8, 3, 7, 1]])
  745. footprint = [[1, 0, 1], [1, 1, 0]]
  746. output = ndimage.maximum_filter(array, footprint=footprint,
  747. origin=[-1, 0])
  748. assert_array_almost_equal([[7, 7, 9, 9, 5],
  749. [7, 9, 8, 9, 7],
  750. [8, 8, 8, 7, 7]], output)
  751. def test_rank01(self):
  752. array = numpy.array([1, 2, 3, 4, 5])
  753. output = ndimage.rank_filter(array, 1, size=2)
  754. assert_array_almost_equal(array, output)
  755. output = ndimage.percentile_filter(array, 100, size=2)
  756. assert_array_almost_equal(array, output)
  757. output = ndimage.median_filter(array, 2)
  758. assert_array_almost_equal(array, output)
  759. def test_rank02(self):
  760. array = numpy.array([1, 2, 3, 4, 5])
  761. output = ndimage.rank_filter(array, 1, size=[3])
  762. assert_array_almost_equal(array, output)
  763. output = ndimage.percentile_filter(array, 50, size=3)
  764. assert_array_almost_equal(array, output)
  765. output = ndimage.median_filter(array, (3,))
  766. assert_array_almost_equal(array, output)
  767. def test_rank03(self):
  768. array = numpy.array([3, 2, 5, 1, 4])
  769. output = ndimage.rank_filter(array, 1, size=[2])
  770. assert_array_almost_equal([3, 3, 5, 5, 4], output)
  771. output = ndimage.percentile_filter(array, 100, size=2)
  772. assert_array_almost_equal([3, 3, 5, 5, 4], output)
  773. def test_rank04(self):
  774. array = numpy.array([3, 2, 5, 1, 4])
  775. expected = [3, 3, 2, 4, 4]
  776. output = ndimage.rank_filter(array, 1, size=3)
  777. assert_array_almost_equal(expected, output)
  778. output = ndimage.percentile_filter(array, 50, size=3)
  779. assert_array_almost_equal(expected, output)
  780. output = ndimage.median_filter(array, size=3)
  781. assert_array_almost_equal(expected, output)
  782. def test_rank05(self):
  783. array = numpy.array([3, 2, 5, 1, 4])
  784. expected = [3, 3, 2, 4, 4]
  785. output = ndimage.rank_filter(array, -2, size=3)
  786. assert_array_almost_equal(expected, output)
  787. def test_rank06(self):
  788. array = numpy.array([[3, 2, 5, 1, 4],
  789. [5, 8, 3, 7, 1],
  790. [5, 6, 9, 3, 5]])
  791. expected = [[2, 2, 1, 1, 1],
  792. [3, 3, 2, 1, 1],
  793. [5, 5, 3, 3, 1]]
  794. output = ndimage.rank_filter(array, 1, size=[2, 3])
  795. assert_array_almost_equal(expected, output)
  796. output = ndimage.percentile_filter(array, 17, size=(2, 3))
  797. assert_array_almost_equal(expected, output)
  798. def test_rank07(self):
  799. array = numpy.array([[3, 2, 5, 1, 4],
  800. [5, 8, 3, 7, 1],
  801. [5, 6, 9, 3, 5]])
  802. expected = [[3, 5, 5, 5, 4],
  803. [5, 5, 7, 5, 4],
  804. [6, 8, 8, 7, 5]]
  805. output = ndimage.rank_filter(array, -2, size=[2, 3])
  806. assert_array_almost_equal(expected, output)
  807. def test_rank08(self):
  808. array = numpy.array([[3, 2, 5, 1, 4],
  809. [5, 8, 3, 7, 1],
  810. [5, 6, 9, 3, 5]])
  811. expected = [[3, 3, 2, 4, 4],
  812. [5, 5, 5, 4, 4],
  813. [5, 6, 7, 5, 5]]
  814. output = ndimage.percentile_filter(array, 50.0, size=(2, 3))
  815. assert_array_almost_equal(expected, output)
  816. output = ndimage.rank_filter(array, 3, size=(2, 3))
  817. assert_array_almost_equal(expected, output)
  818. output = ndimage.median_filter(array, size=(2, 3))
  819. assert_array_almost_equal(expected, output)
  820. def test_rank09(self):
  821. expected = [[3, 3, 2, 4, 4],
  822. [3, 5, 2, 5, 1],
  823. [5, 5, 8, 3, 5]]
  824. footprint = [[1, 0, 1], [0, 1, 0]]
  825. for type_ in self.types:
  826. array = numpy.array([[3, 2, 5, 1, 4],
  827. [5, 8, 3, 7, 1],
  828. [5, 6, 9, 3, 5]], type_)
  829. output = ndimage.rank_filter(array, 1, footprint=footprint)
  830. assert_array_almost_equal(expected, output)
  831. output = ndimage.percentile_filter(array, 35, footprint=footprint)
  832. assert_array_almost_equal(expected, output)
  833. def test_rank10(self):
  834. array = numpy.array([[3, 2, 5, 1, 4],
  835. [7, 6, 9, 3, 5],
  836. [5, 8, 3, 7, 1]])
  837. expected = [[2, 2, 1, 1, 1],
  838. [2, 3, 1, 3, 1],
  839. [5, 5, 3, 3, 1]]
  840. footprint = [[1, 0, 1], [1, 1, 0]]
  841. output = ndimage.rank_filter(array, 0, footprint=footprint)
  842. assert_array_almost_equal(expected, output)
  843. output = ndimage.percentile_filter(array, 0.0, footprint=footprint)
  844. assert_array_almost_equal(expected, output)
  845. def test_rank11(self):
  846. array = numpy.array([[3, 2, 5, 1, 4],
  847. [7, 6, 9, 3, 5],
  848. [5, 8, 3, 7, 1]])
  849. expected = [[3, 5, 5, 5, 4],
  850. [7, 7, 9, 9, 5],
  851. [7, 9, 8, 9, 7]]
  852. footprint = [[1, 0, 1], [1, 1, 0]]
  853. output = ndimage.rank_filter(array, -1, footprint=footprint)
  854. assert_array_almost_equal(expected, output)
  855. output = ndimage.percentile_filter(array, 100.0, footprint=footprint)
  856. assert_array_almost_equal(expected, output)
  857. def test_rank12(self):
  858. expected = [[3, 3, 2, 4, 4],
  859. [3, 5, 2, 5, 1],
  860. [5, 5, 8, 3, 5]]
  861. footprint = [[1, 0, 1], [0, 1, 0]]
  862. for type_ in self.types:
  863. array = numpy.array([[3, 2, 5, 1, 4],
  864. [5, 8, 3, 7, 1],
  865. [5, 6, 9, 3, 5]], type_)
  866. output = ndimage.rank_filter(array, 1, footprint=footprint)
  867. assert_array_almost_equal(expected, output)
  868. output = ndimage.percentile_filter(array, 50.0,
  869. footprint=footprint)
  870. assert_array_almost_equal(expected, output)
  871. output = ndimage.median_filter(array, footprint=footprint)
  872. assert_array_almost_equal(expected, output)
  873. def test_rank13(self):
  874. expected = [[5, 2, 5, 1, 1],
  875. [5, 8, 3, 5, 5],
  876. [6, 6, 5, 5, 5]]
  877. footprint = [[1, 0, 1], [0, 1, 0]]
  878. for type_ in self.types:
  879. array = numpy.array([[3, 2, 5, 1, 4],
  880. [5, 8, 3, 7, 1],
  881. [5, 6, 9, 3, 5]], type_)
  882. output = ndimage.rank_filter(array, 1, footprint=footprint,
  883. origin=-1)
  884. assert_array_almost_equal(expected, output)
  885. def test_rank14(self):
  886. expected = [[3, 5, 2, 5, 1],
  887. [5, 5, 8, 3, 5],
  888. [5, 6, 6, 5, 5]]
  889. footprint = [[1, 0, 1], [0, 1, 0]]
  890. for type_ in self.types:
  891. array = numpy.array([[3, 2, 5, 1, 4],
  892. [5, 8, 3, 7, 1],
  893. [5, 6, 9, 3, 5]], type_)
  894. output = ndimage.rank_filter(array, 1, footprint=footprint,
  895. origin=[-1, 0])
  896. assert_array_almost_equal(expected, output)
  897. def test_rank15(self):
  898. "rank filter 15"
  899. expected = [[2, 3, 1, 4, 1],
  900. [5, 3, 7, 1, 1],
  901. [5, 5, 3, 3, 3]]
  902. footprint = [[1, 0, 1], [0, 1, 0]]
  903. for type_ in self.types:
  904. array = numpy.array([[3, 2, 5, 1, 4],
  905. [5, 8, 3, 7, 1],
  906. [5, 6, 9, 3, 5]], type_)
  907. output = ndimage.rank_filter(array, 0, footprint=footprint,
  908. origin=[-1, 0])
  909. assert_array_almost_equal(expected, output)
  910. def test_generic_filter1d01(self):
  911. weights = numpy.array([1.1, 2.2, 3.3])
  912. def _filter_func(input, output, fltr, total):
  913. fltr = fltr / total
  914. for ii in range(input.shape[0] - 2):
  915. output[ii] = input[ii] * fltr[0]
  916. output[ii] += input[ii + 1] * fltr[1]
  917. output[ii] += input[ii + 2] * fltr[2]
  918. for type_ in self.types:
  919. a = numpy.arange(12, dtype=type_)
  920. a.shape = (3, 4)
  921. r1 = ndimage.correlate1d(a, weights / weights.sum(), 0, origin=-1)
  922. r2 = ndimage.generic_filter1d(
  923. a, _filter_func, 3, axis=0, origin=-1,
  924. extra_arguments=(weights,),
  925. extra_keywords={'total': weights.sum()})
  926. assert_array_almost_equal(r1, r2)
  927. def test_generic_filter01(self):
  928. filter_ = numpy.array([[1.0, 2.0], [3.0, 4.0]])
  929. footprint = numpy.array([[1, 0], [0, 1]])
  930. cf = numpy.array([1., 4.])
  931. def _filter_func(buffer, weights, total=1.0):
  932. weights = cf / total
  933. return (buffer * weights).sum()
  934. for type_ in self.types:
  935. a = numpy.arange(12, dtype=type_)
  936. a.shape = (3, 4)
  937. r1 = ndimage.correlate(a, filter_ * footprint)
  938. if type_ in self.float_types:
  939. r1 /= 5
  940. else:
  941. r1 //= 5
  942. r2 = ndimage.generic_filter(
  943. a, _filter_func, footprint=footprint, extra_arguments=(cf,),
  944. extra_keywords={'total': cf.sum()})
  945. assert_array_almost_equal(r1, r2)
  946. def test_extend01(self):
  947. array = numpy.array([1, 2, 3])
  948. weights = numpy.array([1, 0])
  949. expected_values = [[1, 1, 2],
  950. [3, 1, 2],
  951. [1, 1, 2],
  952. [2, 1, 2],
  953. [0, 1, 2]]
  954. for mode, expected_value in zip(self.modes, expected_values):
  955. output = ndimage.correlate1d(array, weights, 0,
  956. mode=mode, cval=0)
  957. assert_array_equal(output, expected_value)
  958. def test_extend02(self):
  959. array = numpy.array([1, 2, 3])
  960. weights = numpy.array([1, 0, 0, 0, 0, 0, 0, 0])
  961. expected_values = [[1, 1, 1],
  962. [3, 1, 2],
  963. [3, 3, 2],
  964. [1, 2, 3],
  965. [0, 0, 0]]
  966. for mode, expected_value in zip(self.modes, expected_values):
  967. output = ndimage.correlate1d(array, weights, 0,
  968. mode=mode, cval=0)
  969. assert_array_equal(output, expected_value)
  970. def test_extend03(self):
  971. array = numpy.array([1, 2, 3])
  972. weights = numpy.array([0, 0, 1])
  973. expected_values = [[2, 3, 3],
  974. [2, 3, 1],
  975. [2, 3, 3],
  976. [2, 3, 2],
  977. [2, 3, 0]]
  978. for mode, expected_value in zip(self.modes, expected_values):
  979. output = ndimage.correlate1d(array, weights, 0,
  980. mode=mode, cval=0)
  981. assert_array_equal(output, expected_value)
  982. def test_extend04(self):
  983. array = numpy.array([1, 2, 3])
  984. weights = numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 1])
  985. expected_values = [[3, 3, 3],
  986. [2, 3, 1],
  987. [2, 1, 1],
  988. [1, 2, 3],
  989. [0, 0, 0]]
  990. for mode, expected_value in zip(self.modes, expected_values):
  991. output = ndimage.correlate1d(array, weights, 0,
  992. mode=mode, cval=0)
  993. assert_array_equal(output, expected_value)
  994. def test_extend05(self):
  995. array = numpy.array([[1, 2, 3],
  996. [4, 5, 6],
  997. [7, 8, 9]])
  998. weights = numpy.array([[1, 0], [0, 0]])
  999. expected_values = [[[1, 1, 2], [1, 1, 2], [4, 4, 5]],
  1000. [[9, 7, 8], [3, 1, 2], [6, 4, 5]],
  1001. [[1, 1, 2], [1, 1, 2], [4, 4, 5]],
  1002. [[5, 4, 5], [2, 1, 2], [5, 4, 5]],
  1003. [[0, 0, 0], [0, 1, 2], [0, 4, 5]]]
  1004. for mode, expected_value in zip(self.modes, expected_values):
  1005. output = ndimage.correlate(array, weights,
  1006. mode=mode, cval=0)
  1007. assert_array_equal(output, expected_value)
  1008. def test_extend06(self):
  1009. array = numpy.array([[1, 2, 3],
  1010. [4, 5, 6],
  1011. [7, 8, 9]])
  1012. weights = numpy.array([[0, 0, 0], [0, 0, 0], [0, 0, 1]])
  1013. expected_values = [[[5, 6, 6], [8, 9, 9], [8, 9, 9]],
  1014. [[5, 6, 4], [8, 9, 7], [2, 3, 1]],
  1015. [[5, 6, 6], [8, 9, 9], [8, 9, 9]],
  1016. [[5, 6, 5], [8, 9, 8], [5, 6, 5]],
  1017. [[5, 6, 0], [8, 9, 0], [0, 0, 0]]]
  1018. for mode, expected_value in zip(self.modes, expected_values):
  1019. output = ndimage.correlate(array, weights,
  1020. mode=mode, cval=0)
  1021. assert_array_equal(output, expected_value)
  1022. def test_extend07(self):
  1023. array = numpy.array([1, 2, 3])
  1024. weights = numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 1])
  1025. expected_values = [[3, 3, 3],
  1026. [2, 3, 1],
  1027. [2, 1, 1],
  1028. [1, 2, 3],
  1029. [0, 0, 0]]
  1030. for mode, expected_value in zip(self.modes, expected_values):
  1031. output = ndimage.correlate(array, weights, mode=mode, cval=0)
  1032. assert_array_equal(output, expected_value)
  1033. def test_extend08(self):
  1034. array = numpy.array([[1], [2], [3]])
  1035. weights = numpy.array([[0], [0], [0], [0], [0], [0], [0], [0], [1]])
  1036. expected_values = [[[3], [3], [3]],
  1037. [[2], [3], [1]],
  1038. [[2], [1], [1]],
  1039. [[1], [2], [3]],
  1040. [[0], [0], [0]]]
  1041. for mode, expected_value in zip(self.modes, expected_values):
  1042. output = ndimage.correlate(array, weights, mode=mode, cval=0)
  1043. assert_array_equal(output, expected_value)
  1044. def test_extend09(self):
  1045. array = numpy.array([1, 2, 3])
  1046. weights = numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 1])
  1047. expected_values = [[3, 3, 3],
  1048. [2, 3, 1],
  1049. [2, 1, 1],
  1050. [1, 2, 3],
  1051. [0, 0, 0]]
  1052. for mode, expected_value in zip(self.modes, expected_values):
  1053. output = ndimage.correlate(array, weights,
  1054. mode=mode, cval=0)
  1055. assert_array_equal(output, expected_value)
  1056. def test_extend10(self):
  1057. array = numpy.array([[1], [2], [3]])
  1058. weights = numpy.array([[0], [0], [0], [0], [0], [0], [0], [0], [1]])
  1059. expected_values = [[[3], [3], [3]],
  1060. [[2], [3], [1]],
  1061. [[2], [1], [1]],
  1062. [[1], [2], [3]],
  1063. [[0], [0], [0]]]
  1064. for mode, expected_value in zip(self.modes, expected_values):
  1065. output = ndimage.correlate(array, weights,
  1066. mode=mode, cval=0)
  1067. assert_array_equal(output, expected_value)
  1068. def test_boundaries(self):
  1069. def shift(x):
  1070. return (x[0] + 0.5,)
  1071. data = numpy.array([1, 2, 3, 4.])
  1072. expected = {'constant': [1.5, 2.5, 3.5, -1, -1, -1, -1],
  1073. 'wrap': [1.5, 2.5, 3.5, 1.5, 2.5, 3.5, 1.5],
  1074. 'mirror': [1.5, 2.5, 3.5, 3.5, 2.5, 1.5, 1.5],
  1075. 'nearest': [1.5, 2.5, 3.5, 4, 4, 4, 4]}
  1076. for mode in expected:
  1077. assert_array_equal(
  1078. expected[mode],
  1079. ndimage.geometric_transform(data, shift, cval=-1, mode=mode,
  1080. output_shape=(7,), order=1))
  1081. def test_boundaries2(self):
  1082. def shift(x):
  1083. return (x[0] - 0.9,)
  1084. data = numpy.array([1, 2, 3, 4])
  1085. expected = {'constant': [-1, 1, 2, 3],
  1086. 'wrap': [3, 1, 2, 3],
  1087. 'mirror': [2, 1, 2, 3],
  1088. 'nearest': [1, 1, 2, 3]}
  1089. for mode in expected:
  1090. assert_array_equal(
  1091. expected[mode],
  1092. ndimage.geometric_transform(data, shift, cval=-1, mode=mode,
  1093. output_shape=(4,)))
  1094. def test_fourier_gaussian_real01(self):
  1095. for shape in [(32, 16), (31, 15)]:
  1096. for type_, dec in zip([numpy.float32, numpy.float64], [6, 14]):
  1097. a = numpy.zeros(shape, type_)
  1098. a[0, 0] = 1.0
  1099. a = fft.rfft(a, shape[0], 0)
  1100. a = fft.fft(a, shape[1], 1)
  1101. a = ndimage.fourier_gaussian(a, [5.0, 2.5], shape[0], 0)
  1102. a = fft.ifft(a, shape[1], 1)
  1103. a = fft.irfft(a, shape[0], 0)
  1104. assert_almost_equal(ndimage.sum(a), 1, decimal=dec)
  1105. def test_fourier_gaussian_complex01(self):
  1106. for shape in [(32, 16), (31, 15)]:
  1107. for type_, dec in zip([numpy.complex64, numpy.complex128], [6, 14]):
  1108. a = numpy.zeros(shape, type_)
  1109. a[0, 0] = 1.0
  1110. a = fft.fft(a, shape[0], 0)
  1111. a = fft.fft(a, shape[1], 1)
  1112. a = ndimage.fourier_gaussian(a, [5.0, 2.5], -1, 0)
  1113. a = fft.ifft(a, shape[1], 1)
  1114. a = fft.ifft(a, shape[0], 0)
  1115. assert_almost_equal(ndimage.sum(a.real), 1.0, decimal=dec)
  1116. def test_fourier_uniform_real01(self):
  1117. for shape in [(32, 16), (31, 15)]:
  1118. for type_, dec in zip([numpy.float32, numpy.float64], [6, 14]):
  1119. a = numpy.zeros(shape, type_)
  1120. a[0, 0] = 1.0
  1121. a = fft.rfft(a, shape[0], 0)
  1122. a = fft.fft(a, shape[1], 1)
  1123. a = ndimage.fourier_uniform(a, [5.0, 2.5], shape[0], 0)
  1124. a = fft.ifft(a, shape[1], 1)
  1125. a = fft.irfft(a, shape[0], 0)
  1126. assert_almost_equal(ndimage.sum(a), 1.0, decimal=dec)
  1127. def test_fourier_uniform_complex01(self):
  1128. for shape in [(32, 16), (31, 15)]:
  1129. for type_, dec in zip([numpy.complex64, numpy.complex128], [6, 14]):
  1130. a = numpy.zeros(shape, type_)
  1131. a[0, 0] = 1.0
  1132. a = fft.fft(a, shape[0], 0)
  1133. a = fft.fft(a, shape[1], 1)
  1134. a = ndimage.fourier_uniform(a, [5.0, 2.5], -1, 0)
  1135. a = fft.ifft(a, shape[1], 1)
  1136. a = fft.ifft(a, shape[0], 0)
  1137. assert_almost_equal(ndimage.sum(a.real), 1.0, decimal=dec)
  1138. def test_fourier_shift_real01(self):
  1139. for shape in [(32, 16), (31, 15)]:
  1140. for type_, dec in zip([numpy.float32, numpy.float64], [4, 11]):
  1141. expected = numpy.arange(shape[0] * shape[1], dtype=type_)
  1142. expected.shape = shape
  1143. a = fft.rfft(expected, shape[0], 0)
  1144. a = fft.fft(a, shape[1], 1)
  1145. a = ndimage.fourier_shift(a, [1, 1], shape[0], 0)
  1146. a = fft.ifft(a, shape[1], 1)
  1147. a = fft.irfft(a, shape[0], 0)
  1148. assert_array_almost_equal(a[1:, 1:], expected[:-1, :-1],
  1149. decimal=dec)
  1150. assert_array_almost_equal(a.imag, numpy.zeros(shape),
  1151. decimal=dec)
  1152. def test_fourier_shift_complex01(self):
  1153. for shape in [(32, 16), (31, 15)]:
  1154. for type_, dec in zip([numpy.complex64, numpy.complex128], [4, 11]):
  1155. expected = numpy.arange(shape[0] * shape[1], dtype=type_)
  1156. expected.shape = shape
  1157. a = fft.fft(expected, shape[0], 0)
  1158. a = fft.fft(a, shape[1], 1)
  1159. a = ndimage.fourier_shift(a, [1, 1], -1, 0)
  1160. a = fft.ifft(a, shape[1], 1)
  1161. a = fft.ifft(a, shape[0], 0)
  1162. assert_array_almost_equal(a.real[1:, 1:], expected[:-1, :-1],
  1163. decimal=dec)
  1164. assert_array_almost_equal(a.imag, numpy.zeros(shape),
  1165. decimal=dec)
  1166. def test_fourier_ellipsoid_real01(self):
  1167. for shape in [(32, 16), (31, 15)]:
  1168. for type_, dec in zip([numpy.float32, numpy.float64], [5, 14]):
  1169. a = numpy.zeros(shape, type_)
  1170. a[0, 0] = 1.0
  1171. a = fft.rfft(a, shape[0], 0)
  1172. a = fft.fft(a, shape[1], 1)
  1173. a = ndimage.fourier_ellipsoid(a, [5.0, 2.5],
  1174. shape[0], 0)
  1175. a = fft.ifft(a, shape[1], 1)
  1176. a = fft.irfft(a, shape[0], 0)
  1177. assert_almost_equal(ndimage.sum(a), 1.0, decimal=dec)
  1178. def test_fourier_ellipsoid_complex01(self):
  1179. for shape in [(32, 16), (31, 15)]:
  1180. for type_, dec in zip([numpy.complex64, numpy.complex128],
  1181. [5, 14]):
  1182. a = numpy.zeros(shape, type_)
  1183. a[0, 0] = 1.0
  1184. a = fft.fft(a, shape[0], 0)
  1185. a = fft.fft(a, shape[1], 1)
  1186. a = ndimage.fourier_ellipsoid(a, [5.0, 2.5], -1, 0)
  1187. a = fft.ifft(a, shape[1], 1)
  1188. a = fft.ifft(a, shape[0], 0)
  1189. assert_almost_equal(ndimage.sum(a.real), 1.0, decimal=dec)
  1190. def test_spline01(self):
  1191. for type_ in self.types:
  1192. data = numpy.ones([], type_)
  1193. for order in range(2, 6):
  1194. out = ndimage.spline_filter(data, order=order)
  1195. assert_array_almost_equal(out, 1)
  1196. def test_spline02(self):
  1197. for type_ in self.types:
  1198. data = numpy.array([1], type_)
  1199. for order in range(2, 6):
  1200. out = ndimage.spline_filter(data, order=order)
  1201. assert_array_almost_equal(out, [1])
  1202. def test_spline03(self):
  1203. for type_ in self.types:
  1204. data = numpy.ones([], type_)
  1205. for order in range(2, 6):
  1206. out = ndimage.spline_filter(data, order,
  1207. output=type_)
  1208. assert_array_almost_equal(out, 1)
  1209. def test_spline04(self):
  1210. for type_ in self.types:
  1211. data = numpy.ones([4], type_)
  1212. for order in range(2, 6):
  1213. out = ndimage.spline_filter(data, order)
  1214. assert_array_almost_equal(out, [1, 1, 1, 1])
  1215. def test_spline05(self):
  1216. for type_ in self.types:
  1217. data = numpy.ones([4, 4], type_)
  1218. for order in range(2, 6):
  1219. out = ndimage.spline_filter(data, order=order)
  1220. assert_array_almost_equal(out, [[1, 1, 1, 1],
  1221. [1, 1, 1, 1],
  1222. [1, 1, 1, 1],
  1223. [1, 1, 1, 1]])
  1224. def test_geometric_transform01(self):
  1225. data = numpy.array([1])
  1226. def mapping(x):
  1227. return x
  1228. for order in range(0, 6):
  1229. out = ndimage.geometric_transform(data, mapping, data.shape,
  1230. order=order)
  1231. assert_array_almost_equal(out, [1])
  1232. def test_geometric_transform02(self):
  1233. data = numpy.ones([4])
  1234. def mapping(x):
  1235. return x
  1236. for order in range(0, 6):
  1237. out = ndimage.geometric_transform(data, mapping, data.shape,
  1238. order=order)
  1239. assert_array_almost_equal(out, [1, 1, 1, 1])
  1240. def test_geometric_transform03(self):
  1241. data = numpy.ones([4])
  1242. def mapping(x):
  1243. return (x[0] - 1,)
  1244. for order in range(0, 6):
  1245. out = ndimage.geometric_transform(data, mapping, data.shape,
  1246. order=order)
  1247. assert_array_almost_equal(out, [0, 1, 1, 1])
  1248. def test_geometric_transform04(self):
  1249. data = numpy.array([4, 1, 3, 2])
  1250. def mapping(x):
  1251. return (x[0] - 1,)
  1252. for order in range(0, 6):
  1253. out = ndimage.geometric_transform(data, mapping, data.shape,
  1254. order=order)
  1255. assert_array_almost_equal(out, [0, 4, 1, 3])
  1256. def test_geometric_transform05(self):
  1257. data = numpy.array([[1, 1, 1, 1],
  1258. [1, 1, 1, 1],
  1259. [1, 1, 1, 1]])
  1260. def mapping(x):
  1261. return (x[0], x[1] - 1)
  1262. for order in range(0, 6):
  1263. out = ndimage.geometric_transform(data, mapping, data.shape,
  1264. order=order)
  1265. assert_array_almost_equal(out, [[0, 1, 1, 1],
  1266. [0, 1, 1, 1],
  1267. [0, 1, 1, 1]])
  1268. def test_geometric_transform06(self):
  1269. data = numpy.array([[4, 1, 3, 2],
  1270. [7, 6, 8, 5],
  1271. [3, 5, 3, 6]])
  1272. def mapping(x):
  1273. return (x[0], x[1] - 1)
  1274. for order in range(0, 6):
  1275. out = ndimage.geometric_transform(data, mapping, data.shape,
  1276. order=order)
  1277. assert_array_almost_equal(out, [[0, 4, 1, 3],
  1278. [0, 7, 6, 8],
  1279. [0, 3, 5, 3]])
  1280. def test_geometric_transform07(self):
  1281. data = numpy.array([[4, 1, 3, 2],
  1282. [7, 6, 8, 5],
  1283. [3, 5, 3, 6]])
  1284. def mapping(x):
  1285. return (x[0] - 1, x[1])
  1286. for order in range(0, 6):
  1287. out = ndimage.geometric_transform(data, mapping, data.shape,
  1288. order=order)
  1289. assert_array_almost_equal(out, [[0, 0, 0, 0],
  1290. [4, 1, 3, 2],
  1291. [7, 6, 8, 5]])
  1292. def test_geometric_transform08(self):
  1293. data = numpy.array([[4, 1, 3, 2],
  1294. [7, 6, 8, 5],
  1295. [3, 5, 3, 6]])
  1296. def mapping(x):
  1297. return (x[0] - 1, x[1] - 1)
  1298. for order in range(0, 6):
  1299. out = ndimage.geometric_transform(data, mapping, data.shape,
  1300. order=order)
  1301. assert_array_almost_equal(out, [[0, 0, 0, 0],
  1302. [0, 4, 1, 3],
  1303. [0, 7, 6, 8]])
  1304. def test_geometric_transform10(self):
  1305. data = numpy.array([[4, 1, 3, 2],
  1306. [7, 6, 8, 5],
  1307. [3, 5, 3, 6]])
  1308. def mapping(x):
  1309. return (x[0] - 1, x[1] - 1)
  1310. for order in range(0, 6):
  1311. if (order > 1):
  1312. filtered = ndimage.spline_filter(data, order=order)
  1313. else:
  1314. filtered = data
  1315. out = ndimage.geometric_transform(filtered, mapping, data.shape,
  1316. order=order, prefilter=False)
  1317. assert_array_almost_equal(out, [[0, 0, 0, 0],
  1318. [0, 4, 1, 3],
  1319. [0, 7, 6, 8]])
  1320. def test_geometric_transform13(self):
  1321. data = numpy.ones([2], numpy.float64)
  1322. def mapping(x):
  1323. return (x[0] // 2,)
  1324. for order in range(0, 6):
  1325. out = ndimage.geometric_transform(data, mapping, [4], order=order)
  1326. assert_array_almost_equal(out, [1, 1, 1, 1])
  1327. def test_geometric_transform14(self):
  1328. data = [1, 5, 2, 6, 3, 7, 4, 4]
  1329. def mapping(x):
  1330. return (2 * x[0],)
  1331. for order in range(0, 6):
  1332. out = ndimage.geometric_transform(data, mapping, [4], order=order)
  1333. assert_array_almost_equal(out, [1, 2, 3, 4])
  1334. def test_geometric_transform15(self):
  1335. data = [1, 2, 3, 4]
  1336. def mapping(x):
  1337. return (x[0] / 2,)
  1338. for order in range(0, 6):
  1339. out = ndimage.geometric_transform(data, mapping, [8], order=order)
  1340. assert_array_almost_equal(out[::2], [1, 2, 3, 4])
  1341. def test_geometric_transform16(self):
  1342. data = [[1, 2, 3, 4],
  1343. [5, 6, 7, 8],
  1344. [9.0, 10, 11, 12]]
  1345. def mapping(x):
  1346. return (x[0], x[1] * 2)
  1347. for order in range(0, 6):
  1348. out = ndimage.geometric_transform(data, mapping, (3, 2),
  1349. order=order)
  1350. assert_array_almost_equal(out, [[1, 3], [5, 7], [9, 11]])
  1351. def test_geometric_transform17(self):
  1352. data = [[1, 2, 3, 4],
  1353. [5, 6, 7, 8],
  1354. [9, 10, 11, 12]]
  1355. def mapping(x):
  1356. return (x[0] * 2, x[1])
  1357. for order in range(0, 6):
  1358. out = ndimage.geometric_transform(data, mapping, (1, 4),
  1359. order=order)
  1360. assert_array_almost_equal(out, [[1, 2, 3, 4]])
  1361. def test_geometric_transform18(self):
  1362. data = [[1, 2, 3, 4],
  1363. [5, 6, 7, 8],
  1364. [9, 10, 11, 12]]
  1365. def mapping(x):
  1366. return (x[0] * 2, x[1] * 2)
  1367. for order in range(0, 6):
  1368. out = ndimage.geometric_transform(data, mapping, (1, 2),
  1369. order=order)
  1370. assert_array_almost_equal(out, [[1, 3]])
  1371. def test_geometric_transform19(self):
  1372. data = [[1, 2, 3, 4],
  1373. [5, 6, 7, 8],
  1374. [9, 10, 11, 12]]
  1375. def mapping(x):
  1376. return (x[0], x[1] / 2)
  1377. for order in range(0, 6):
  1378. out = ndimage.geometric_transform(data, mapping, (3, 8),
  1379. order=order)
  1380. assert_array_almost_equal(out[..., ::2], data)
  1381. def test_geometric_transform20(self):
  1382. data = [[1, 2, 3, 4],
  1383. [5, 6, 7, 8],
  1384. [9, 10, 11, 12]]
  1385. def mapping(x):
  1386. return (x[0] / 2, x[1])
  1387. for order in range(0, 6):
  1388. out = ndimage.geometric_transform(data, mapping, (6, 4),
  1389. order=order)
  1390. assert_array_almost_equal(out[::2, ...], data)
  1391. def test_geometric_transform21(self):
  1392. data = [[1, 2, 3, 4],
  1393. [5, 6, 7, 8],
  1394. [9, 10, 11, 12]]
  1395. def mapping(x):
  1396. return (x[0] / 2, x[1] / 2)
  1397. for order in range(0, 6):
  1398. out = ndimage.geometric_transform(data, mapping, (6, 8),
  1399. order=order)
  1400. assert_array_almost_equal(out[::2, ::2], data)
  1401. def test_geometric_transform22(self):
  1402. data = numpy.array([[1, 2, 3, 4],
  1403. [5, 6, 7, 8],
  1404. [9, 10, 11, 12]], numpy.float64)
  1405. def mapping1(x):
  1406. return (x[0] / 2, x[1] / 2)
  1407. def mapping2(x):
  1408. return (x[0] * 2, x[1] * 2)
  1409. for order in range(0, 6):
  1410. out = ndimage.geometric_transform(data, mapping1,
  1411. (6, 8), order=order)
  1412. out = ndimage.geometric_transform(out, mapping2,
  1413. (3, 4), order=order)
  1414. assert_array_almost_equal(out, data)
  1415. def test_geometric_transform23(self):
  1416. data = [[1, 2, 3, 4],
  1417. [5, 6, 7, 8],
  1418. [9, 10, 11, 12]]
  1419. def mapping(x):
  1420. return (1, x[0] * 2)
  1421. for order in range(0, 6):
  1422. out = ndimage.geometric_transform(data, mapping, (2,), order=order)
  1423. out = out.astype(numpy.int32)
  1424. assert_array_almost_equal(out, [5, 7])
  1425. def test_geometric_transform24(self):
  1426. data = [[1, 2, 3, 4],
  1427. [5, 6, 7, 8],
  1428. [9, 10, 11, 12]]
  1429. def mapping(x, a, b):
  1430. return (a, x[0] * b)
  1431. for order in range(0, 6):
  1432. out = ndimage.geometric_transform(
  1433. data, mapping, (2,), order=order, extra_arguments=(1,),
  1434. extra_keywords={'b': 2})
  1435. assert_array_almost_equal(out, [5, 7])
  1436. def test_geometric_transform_endianness_with_output_parameter(self):
  1437. # geometric transform given output ndarray or dtype with
  1438. # non-native endianness. see issue #4127
  1439. data = numpy.array([1])
  1440. def mapping(x):
  1441. return x
  1442. for out in [data.dtype, data.dtype.newbyteorder(),
  1443. numpy.empty_like(data),
  1444. numpy.empty_like(data).astype(data.dtype.newbyteorder())]:
  1445. returned = ndimage.geometric_transform(data, mapping, data.shape,
  1446. output=out)
  1447. result = out if returned is None else returned
  1448. assert_array_almost_equal(result, [1])
  1449. def test_geometric_transform_with_string_output(self):
  1450. data = numpy.array([1])
  1451. def mapping(x):
  1452. return x
  1453. out = ndimage.geometric_transform(data, mapping, output='f')
  1454. assert_(out.dtype is numpy.dtype('f'))
  1455. assert_array_almost_equal(out, [1])
  1456. def test_map_coordinates01(self):
  1457. data = numpy.array([[4, 1, 3, 2],
  1458. [7, 6, 8, 5],
  1459. [3, 5, 3, 6]])
  1460. idx = numpy.indices(data.shape)
  1461. idx -= 1
  1462. for order in range(0, 6):
  1463. out = ndimage.map_coordinates(data, idx, order=order)
  1464. assert_array_almost_equal(out, [[0, 0, 0, 0],
  1465. [0, 4, 1, 3],
  1466. [0, 7, 6, 8]])
  1467. def test_map_coordinates02(self):
  1468. data = numpy.array([[4, 1, 3, 2],
  1469. [7, 6, 8, 5],
  1470. [3, 5, 3, 6]])
  1471. idx = numpy.indices(data.shape, numpy.float64)
  1472. idx -= 0.5
  1473. for order in range(0, 6):
  1474. out1 = ndimage.shift(data, 0.5, order=order)
  1475. out2 = ndimage.map_coordinates(data, idx, order=order)
  1476. assert_array_almost_equal(out1, out2)
  1477. def test_map_coordinates03(self):
  1478. data = numpy.array([[4, 1, 3, 2],
  1479. [7, 6, 8, 5],
  1480. [3, 5, 3, 6]], order='F')
  1481. idx = numpy.indices(data.shape) - 1
  1482. out = ndimage.map_coordinates(data, idx)
  1483. assert_array_almost_equal(out, [[0, 0, 0, 0],
  1484. [0, 4, 1, 3],
  1485. [0, 7, 6, 8]])
  1486. assert_array_almost_equal(out, ndimage.shift(data, (1, 1)))
  1487. idx = numpy.indices(data[::2].shape) - 1
  1488. out = ndimage.map_coordinates(data[::2], idx)
  1489. assert_array_almost_equal(out, [[0, 0, 0, 0],
  1490. [0, 4, 1, 3]])
  1491. assert_array_almost_equal(out, ndimage.shift(data[::2], (1, 1)))
  1492. idx = numpy.indices(data[:, ::2].shape) - 1
  1493. out = ndimage.map_coordinates(data[:, ::2], idx)
  1494. assert_array_almost_equal(out, [[0, 0], [0, 4], [0, 7]])
  1495. assert_array_almost_equal(out, ndimage.shift(data[:, ::2], (1, 1)))
  1496. def test_map_coordinates_endianness_with_output_parameter(self):
  1497. # output parameter given as array or dtype with either endianness
  1498. # see issue #4127
  1499. data = numpy.array([[1, 2], [7, 6]])
  1500. expected = numpy.array([[0, 0], [0, 1]])
  1501. idx = numpy.indices(data.shape)
  1502. idx -= 1
  1503. for out in [data.dtype, data.dtype.newbyteorder(), numpy.empty_like(expected),
  1504. numpy.empty_like(expected).astype(expected.dtype.newbyteorder())]:
  1505. returned = ndimage.map_coordinates(data, idx, output=out)
  1506. result = out if returned is None else returned
  1507. assert_array_almost_equal(result, expected)
  1508. def test_map_coordinates_with_string_output(self):
  1509. data = numpy.array([[1]])
  1510. idx = numpy.indices(data.shape)
  1511. out = ndimage.map_coordinates(data, idx, output='f')
  1512. assert_(out.dtype is numpy.dtype('f'))
  1513. assert_array_almost_equal(out, [[1]])
  1514. @pytest.mark.skipif('win32' in sys.platform or numpy.intp(0).itemsize < 8,
  1515. reason="do not run on 32 bit or windows (no sparse memory)")
  1516. def test_map_coordinates_large_data(self):
  1517. # check crash on large data
  1518. try:
  1519. n = 30000
  1520. a = numpy.empty(n**2, dtype=numpy.float32).reshape(n, n)
  1521. # fill the part we might read
  1522. a[n-3:, n-3:] = 0
  1523. ndimage.map_coordinates(a, [[n - 1.5], [n - 1.5]], order=1)
  1524. except MemoryError:
  1525. raise pytest.skip("Not enough memory available")
  1526. def test_affine_transform01(self):
  1527. data = numpy.array([1])
  1528. for order in range(0, 6):
  1529. out = ndimage.affine_transform(data, [[1]], order=order)
  1530. assert_array_almost_equal(out, [1])
  1531. def test_affine_transform02(self):
  1532. data = numpy.ones([4])
  1533. for order in range(0, 6):
  1534. out = ndimage.affine_transform(data, [[1]], order=order)
  1535. assert_array_almost_equal(out, [1, 1, 1, 1])
  1536. def test_affine_transform03(self):
  1537. data = numpy.ones([4])
  1538. for order in range(0, 6):
  1539. out = ndimage.affine_transform(data, [[1]], -1, order=order)
  1540. assert_array_almost_equal(out, [0, 1, 1, 1])
  1541. def test_affine_transform04(self):
  1542. data = numpy.array([4, 1, 3, 2])
  1543. for order in range(0, 6):
  1544. out = ndimage.affine_transform(data, [[1]], -1, order=order)
  1545. assert_array_almost_equal(out, [0, 4, 1, 3])
  1546. def test_affine_transform05(self):
  1547. data = numpy.array([[1, 1, 1, 1],
  1548. [1, 1, 1, 1],
  1549. [1, 1, 1, 1]])
  1550. for order in range(0, 6):
  1551. out = ndimage.affine_transform(data, [[1, 0], [0, 1]],
  1552. [0, -1], order=order)
  1553. assert_array_almost_equal(out, [[0, 1, 1, 1],
  1554. [0, 1, 1, 1],
  1555. [0, 1, 1, 1]])
  1556. def test_affine_transform06(self):
  1557. data = numpy.array([[4, 1, 3, 2],
  1558. [7, 6, 8, 5],
  1559. [3, 5, 3, 6]])
  1560. for order in range(0, 6):
  1561. out = ndimage.affine_transform(data, [[1, 0], [0, 1]],
  1562. [0, -1], order=order)
  1563. assert_array_almost_equal(out, [[0, 4, 1, 3],
  1564. [0, 7, 6, 8],
  1565. [0, 3, 5, 3]])
  1566. def test_affine_transform07(self):
  1567. data = numpy.array([[4, 1, 3, 2],
  1568. [7, 6, 8, 5],
  1569. [3, 5, 3, 6]])
  1570. for order in range(0, 6):
  1571. out = ndimage.affine_transform(data, [[1, 0], [0, 1]],
  1572. [-1, 0], order=order)
  1573. assert_array_almost_equal(out, [[0, 0, 0, 0],
  1574. [4, 1, 3, 2],
  1575. [7, 6, 8, 5]])
  1576. def test_affine_transform08(self):
  1577. data = numpy.array([[4, 1, 3, 2],
  1578. [7, 6, 8, 5],
  1579. [3, 5, 3, 6]])
  1580. for order in range(0, 6):
  1581. out = ndimage.affine_transform(data, [[1, 0], [0, 1]],
  1582. [-1, -1], order=order)
  1583. assert_array_almost_equal(out, [[0, 0, 0, 0],
  1584. [0, 4, 1, 3],
  1585. [0, 7, 6, 8]])
  1586. def test_affine_transform09(self):
  1587. data = numpy.array([[4, 1, 3, 2],
  1588. [7, 6, 8, 5],
  1589. [3, 5, 3, 6]])
  1590. for order in range(0, 6):
  1591. if (order > 1):
  1592. filtered = ndimage.spline_filter(data, order=order)
  1593. else:
  1594. filtered = data
  1595. out = ndimage.affine_transform(filtered, [[1, 0], [0, 1]],
  1596. [-1, -1], order=order,
  1597. prefilter=False)
  1598. assert_array_almost_equal(out, [[0, 0, 0, 0],
  1599. [0, 4, 1, 3],
  1600. [0, 7, 6, 8]])
  1601. def test_affine_transform10(self):
  1602. data = numpy.ones([2], numpy.float64)
  1603. for order in range(0, 6):
  1604. out = ndimage.affine_transform(data, [[0.5]], output_shape=(4,),
  1605. order=order)
  1606. assert_array_almost_equal(out, [1, 1, 1, 0])
  1607. def test_affine_transform11(self):
  1608. data = [1, 5, 2, 6, 3, 7, 4, 4]
  1609. for order in range(0, 6):
  1610. out = ndimage.affine_transform(data, [[2]], 0, (4,), order=order)
  1611. assert_array_almost_equal(out, [1, 2, 3, 4])
  1612. def test_affine_transform12(self):
  1613. data = [1, 2, 3, 4]
  1614. for order in range(0, 6):
  1615. out = ndimage.affine_transform(data, [[0.5]], 0, (8,), order=order)
  1616. assert_array_almost_equal(out[::2], [1, 2, 3, 4])
  1617. def test_affine_transform13(self):
  1618. data = [[1, 2, 3, 4],
  1619. [5, 6, 7, 8],
  1620. [9.0, 10, 11, 12]]
  1621. for order in range(0, 6):
  1622. out = ndimage.affine_transform(data, [[1, 0], [0, 2]], 0, (3, 2),
  1623. order=order)
  1624. assert_array_almost_equal(out, [[1, 3], [5, 7], [9, 11]])
  1625. def test_affine_transform14(self):
  1626. data = [[1, 2, 3, 4],
  1627. [5, 6, 7, 8],
  1628. [9, 10, 11, 12]]
  1629. for order in range(0, 6):
  1630. out = ndimage.affine_transform(data, [[2, 0], [0, 1]], 0, (1, 4),
  1631. order=order)
  1632. assert_array_almost_equal(out, [[1, 2, 3, 4]])
  1633. def test_affine_transform15(self):
  1634. data = [[1, 2, 3, 4],
  1635. [5, 6, 7, 8],
  1636. [9, 10, 11, 12]]
  1637. for order in range(0, 6):
  1638. out = ndimage.affine_transform(data, [[2, 0], [0, 2]], 0, (1, 2),
  1639. order=order)
  1640. assert_array_almost_equal(out, [[1, 3]])
  1641. def test_affine_transform16(self):
  1642. data = [[1, 2, 3, 4],
  1643. [5, 6, 7, 8],
  1644. [9, 10, 11, 12]]
  1645. for order in range(0, 6):
  1646. out = ndimage.affine_transform(data, [[1, 0.0], [0, 0.5]], 0,
  1647. (3, 8), order=order)
  1648. assert_array_almost_equal(out[..., ::2], data)
  1649. def test_affine_transform17(self):
  1650. data = [[1, 2, 3, 4],
  1651. [5, 6, 7, 8],
  1652. [9, 10, 11, 12]]
  1653. for order in range(0, 6):
  1654. out = ndimage.affine_transform(data, [[0.5, 0], [0, 1]], 0,
  1655. (6, 4), order=order)
  1656. assert_array_almost_equal(out[::2, ...], data)
  1657. def test_affine_transform18(self):
  1658. data = [[1, 2, 3, 4],
  1659. [5, 6, 7, 8],
  1660. [9, 10, 11, 12]]
  1661. for order in range(0, 6):
  1662. out = ndimage.affine_transform(data, [[0.5, 0], [0, 0.5]], 0,
  1663. (6, 8), order=order)
  1664. assert_array_almost_equal(out[::2, ::2], data)
  1665. def test_affine_transform19(self):
  1666. data = numpy.array([[1, 2, 3, 4],
  1667. [5, 6, 7, 8],
  1668. [9, 10, 11, 12]], numpy.float64)
  1669. for order in range(0, 6):
  1670. out = ndimage.affine_transform(data, [[0.5, 0], [0, 0.5]], 0,
  1671. (6, 8), order=order)
  1672. out = ndimage.affine_transform(out, [[2.0, 0], [0, 2.0]], 0,
  1673. (3, 4), order=order)
  1674. assert_array_almost_equal(out, data)
  1675. def test_affine_transform20(self):
  1676. data = [[1, 2, 3, 4],
  1677. [5, 6, 7, 8],
  1678. [9, 10, 11, 12]]
  1679. for order in range(0, 6):
  1680. out = ndimage.affine_transform(data, [[0], [2]], 0, (2,),
  1681. order=order)
  1682. assert_array_almost_equal(out, [1, 3])
  1683. def test_affine_transform21(self):
  1684. data = [[1, 2, 3, 4],
  1685. [5, 6, 7, 8],
  1686. [9, 10, 11, 12]]
  1687. for order in range(0, 6):
  1688. out = ndimage.affine_transform(data, [[2], [0]], 0, (2,),
  1689. order=order)
  1690. assert_array_almost_equal(out, [1, 9])
  1691. def test_affine_transform22(self):
  1692. # shift and offset interaction; see issue #1547
  1693. data = numpy.array([4, 1, 3, 2])
  1694. for order in range(0, 6):
  1695. out = ndimage.affine_transform(data, [[2]], [-1], (3,),
  1696. order=order)
  1697. assert_array_almost_equal(out, [0, 1, 2])
  1698. def test_affine_transform23(self):
  1699. # shift and offset interaction; see issue #1547
  1700. data = numpy.array([4, 1, 3, 2])
  1701. for order in range(0, 6):
  1702. out = ndimage.affine_transform(data, [[0.5]], [-1], (8,),
  1703. order=order)
  1704. assert_array_almost_equal(out[::2], [0, 4, 1, 3])
  1705. def test_affine_transform24(self):
  1706. # consistency between diagonal and non-diagonal case; see issue #1547
  1707. data = numpy.array([4, 1, 3, 2])
  1708. for order in range(0, 6):
  1709. with suppress_warnings() as sup:
  1710. sup.filter(UserWarning,
  1711. "The behaviour of affine_transform with a one-dimensional array .* has changed")
  1712. out1 = ndimage.affine_transform(data, [2], -1, order=order)
  1713. out2 = ndimage.affine_transform(data, [[2]], -1, order=order)
  1714. assert_array_almost_equal(out1, out2)
  1715. def test_affine_transform25(self):
  1716. # consistency between diagonal and non-diagonal case; see issue #1547
  1717. data = numpy.array([4, 1, 3, 2])
  1718. for order in range(0, 6):
  1719. with suppress_warnings() as sup:
  1720. sup.filter(UserWarning,
  1721. "The behaviour of affine_transform with a one-dimensional array .* has changed")
  1722. out1 = ndimage.affine_transform(data, [0.5], -1, order=order)
  1723. out2 = ndimage.affine_transform(data, [[0.5]], -1, order=order)
  1724. assert_array_almost_equal(out1, out2)
  1725. def test_affine_transform26(self):
  1726. # test homogeneous coordinates
  1727. data = numpy.array([[4, 1, 3, 2],
  1728. [7, 6, 8, 5],
  1729. [3, 5, 3, 6]])
  1730. for order in range(0, 6):
  1731. if (order > 1):
  1732. filtered = ndimage.spline_filter(data, order=order)
  1733. else:
  1734. filtered = data
  1735. tform_original = numpy.eye(2)
  1736. offset_original = -numpy.ones((2, 1))
  1737. tform_h1 = numpy.hstack((tform_original, offset_original))
  1738. tform_h2 = numpy.vstack((tform_h1, [[0, 0, 1]]))
  1739. out1 = ndimage.affine_transform(filtered, tform_original,
  1740. offset_original.ravel(),
  1741. order=order, prefilter=False)
  1742. out2 = ndimage.affine_transform(filtered, tform_h1, order=order,
  1743. prefilter=False)
  1744. out3 = ndimage.affine_transform(filtered, tform_h2, order=order,
  1745. prefilter=False)
  1746. for out in [out1, out2, out3]:
  1747. assert_array_almost_equal(out, [[0, 0, 0, 0],
  1748. [0, 4, 1, 3],
  1749. [0, 7, 6, 8]])
  1750. def test_affine_transform27(self):
  1751. # test valid homogeneous transformation matrix
  1752. data = numpy.array([[4, 1, 3, 2],
  1753. [7, 6, 8, 5],
  1754. [3, 5, 3, 6]])
  1755. tform_h1 = numpy.hstack((numpy.eye(2), -numpy.ones((2, 1))))
  1756. tform_h2 = numpy.vstack((tform_h1, [[5, 2, 1]]))
  1757. assert_raises(ValueError, ndimage.affine_transform, data, tform_h2)
  1758. def test_affine_transform_1d_endianness_with_output_parameter(self):
  1759. # 1d affine transform given output ndarray or dtype with
  1760. # either endianness. see issue #7388
  1761. data = numpy.ones((2, 2))
  1762. for out in [numpy.empty_like(data),
  1763. numpy.empty_like(data).astype(data.dtype.newbyteorder()),
  1764. data.dtype, data.dtype.newbyteorder()]:
  1765. with suppress_warnings() as sup:
  1766. sup.filter(UserWarning,
  1767. "The behaviour of affine_transform with a one-dimensional array .* has changed")
  1768. returned = ndimage.affine_transform(data, [1, 1], output=out)
  1769. result = out if returned is None else returned
  1770. assert_array_almost_equal(result, [[1, 1], [1, 1]])
  1771. def test_affine_transform_multi_d_endianness_with_output_parameter(self):
  1772. # affine transform given output ndarray or dtype with either endianness
  1773. # see issue #4127
  1774. data = numpy.array([1])
  1775. for out in [data.dtype, data.dtype.newbyteorder(),
  1776. numpy.empty_like(data),
  1777. numpy.empty_like(data).astype(data.dtype.newbyteorder())]:
  1778. returned = ndimage.affine_transform(data, [[1]], output=out)
  1779. result = out if returned is None else returned
  1780. assert_array_almost_equal(result, [1])
  1781. def test_affine_transform_with_string_output(self):
  1782. data = numpy.array([1])
  1783. out = ndimage.affine_transform(data, [[1]], output='f')
  1784. assert_(out.dtype is numpy.dtype('f'))
  1785. assert_array_almost_equal(out, [1])
  1786. def test_shift01(self):
  1787. data = numpy.array([1])
  1788. for order in range(0, 6):
  1789. out = ndimage.shift(data, [1], order=order)
  1790. assert_array_almost_equal(out, [0])
  1791. def test_shift02(self):
  1792. data = numpy.ones([4])
  1793. for order in range(0, 6):
  1794. out = ndimage.shift(data, [1], order=order)
  1795. assert_array_almost_equal(out, [0, 1, 1, 1])
  1796. def test_shift03(self):
  1797. data = numpy.ones([4])
  1798. for order in range(0, 6):
  1799. out = ndimage.shift(data, -1, order=order)
  1800. assert_array_almost_equal(out, [1, 1, 1, 0])
  1801. def test_shift04(self):
  1802. data = numpy.array([4, 1, 3, 2])
  1803. for order in range(0, 6):
  1804. out = ndimage.shift(data, 1, order=order)
  1805. assert_array_almost_equal(out, [0, 4, 1, 3])
  1806. def test_shift05(self):
  1807. data = numpy.array([[1, 1, 1, 1],
  1808. [1, 1, 1, 1],
  1809. [1, 1, 1, 1]])
  1810. for order in range(0, 6):
  1811. out = ndimage.shift(data, [0, 1], order=order)
  1812. assert_array_almost_equal(out, [[0, 1, 1, 1],
  1813. [0, 1, 1, 1],
  1814. [0, 1, 1, 1]])
  1815. def test_shift06(self):
  1816. data = numpy.array([[4, 1, 3, 2],
  1817. [7, 6, 8, 5],
  1818. [3, 5, 3, 6]])
  1819. for order in range(0, 6):
  1820. out = ndimage.shift(data, [0, 1], order=order)
  1821. assert_array_almost_equal(out, [[0, 4, 1, 3],
  1822. [0, 7, 6, 8],
  1823. [0, 3, 5, 3]])
  1824. def test_shift07(self):
  1825. data = numpy.array([[4, 1, 3, 2],
  1826. [7, 6, 8, 5],
  1827. [3, 5, 3, 6]])
  1828. for order in range(0, 6):
  1829. out = ndimage.shift(data, [1, 0], order=order)
  1830. assert_array_almost_equal(out, [[0, 0, 0, 0],
  1831. [4, 1, 3, 2],
  1832. [7, 6, 8, 5]])
  1833. def test_shift08(self):
  1834. data = numpy.array([[4, 1, 3, 2],
  1835. [7, 6, 8, 5],
  1836. [3, 5, 3, 6]])
  1837. for order in range(0, 6):
  1838. out = ndimage.shift(data, [1, 1], order=order)
  1839. assert_array_almost_equal(out, [[0, 0, 0, 0],
  1840. [0, 4, 1, 3],
  1841. [0, 7, 6, 8]])
  1842. def test_shift09(self):
  1843. data = numpy.array([[4, 1, 3, 2],
  1844. [7, 6, 8, 5],
  1845. [3, 5, 3, 6]])
  1846. for order in range(0, 6):
  1847. if (order > 1):
  1848. filtered = ndimage.spline_filter(data, order=order)
  1849. else:
  1850. filtered = data
  1851. out = ndimage.shift(filtered, [1, 1], order=order, prefilter=False)
  1852. assert_array_almost_equal(out, [[0, 0, 0, 0],
  1853. [0, 4, 1, 3],
  1854. [0, 7, 6, 8]])
  1855. def test_zoom1(self):
  1856. for order in range(0, 6):
  1857. for z in [2, [2, 2]]:
  1858. arr = numpy.array(list(range(25))).reshape((5, 5)).astype(float)
  1859. arr = ndimage.zoom(arr, z, order=order)
  1860. assert_equal(arr.shape, (10, 10))
  1861. assert_(numpy.all(arr[-1, :] != 0))
  1862. assert_(numpy.all(arr[-1, :] >= (20 - eps)))
  1863. assert_(numpy.all(arr[0, :] <= (5 + eps)))
  1864. assert_(numpy.all(arr >= (0 - eps)))
  1865. assert_(numpy.all(arr <= (24 + eps)))
  1866. def test_zoom2(self):
  1867. arr = numpy.arange(12).reshape((3, 4))
  1868. out = ndimage.zoom(ndimage.zoom(arr, 2), 0.5)
  1869. assert_array_equal(out, arr)
  1870. def test_zoom3(self):
  1871. arr = numpy.array([[1, 2]])
  1872. out1 = ndimage.zoom(arr, (2, 1))
  1873. out2 = ndimage.zoom(arr, (1, 2))
  1874. assert_array_almost_equal(out1, numpy.array([[1, 2], [1, 2]]))
  1875. assert_array_almost_equal(out2, numpy.array([[1, 1, 2, 2]]))
  1876. def test_zoom_affine01(self):
  1877. data = [[1, 2, 3, 4],
  1878. [5, 6, 7, 8],
  1879. [9, 10, 11, 12]]
  1880. for order in range(0, 6):
  1881. with suppress_warnings() as sup:
  1882. sup.filter(UserWarning,
  1883. "The behaviour of affine_transform with a one-dimensional array .* has changed")
  1884. out = ndimage.affine_transform(data, [0.5, 0.5], 0,
  1885. (6, 8), order=order)
  1886. assert_array_almost_equal(out[::2, ::2], data)
  1887. def test_zoom_infinity(self):
  1888. # Ticket #1419 regression test
  1889. dim = 8
  1890. ndimage.zoom(numpy.zeros((dim, dim)), 1./dim, mode='nearest')
  1891. def test_zoom_zoomfactor_one(self):
  1892. # Ticket #1122 regression test
  1893. arr = numpy.zeros((1, 5, 5))
  1894. zoom = (1.0, 2.0, 2.0)
  1895. out = ndimage.zoom(arr, zoom, cval=7)
  1896. ref = numpy.zeros((1, 10, 10))
  1897. assert_array_almost_equal(out, ref)
  1898. def test_zoom_output_shape_roundoff(self):
  1899. arr = numpy.zeros((3, 11, 25))
  1900. zoom = (4.0 / 3, 15.0 / 11, 29.0 / 25)
  1901. with suppress_warnings() as sup:
  1902. sup.filter(UserWarning,
  1903. "From scipy 0.13.0, the output shape of zoom.. is calculated with round.. instead of int")
  1904. out = ndimage.zoom(arr, zoom)
  1905. assert_array_equal(out.shape, (4, 15, 29))
  1906. def test_rotate01(self):
  1907. data = numpy.array([[0, 0, 0, 0],
  1908. [0, 1, 1, 0],
  1909. [0, 0, 0, 0]], dtype=numpy.float64)
  1910. for order in range(0, 6):
  1911. out = ndimage.rotate(data, 0)
  1912. assert_array_almost_equal(out, data)
  1913. def test_rotate02(self):
  1914. data = numpy.array([[0, 0, 0, 0],
  1915. [0, 1, 0, 0],
  1916. [0, 0, 0, 0]], dtype=numpy.float64)
  1917. expected = numpy.array([[0, 0, 0],
  1918. [0, 0, 0],
  1919. [0, 1, 0],
  1920. [0, 0, 0]], dtype=numpy.float64)
  1921. for order in range(0, 6):
  1922. out = ndimage.rotate(data, 90)
  1923. assert_array_almost_equal(out, expected)
  1924. def test_rotate03(self):
  1925. data = numpy.array([[0, 0, 0, 0, 0],
  1926. [0, 1, 1, 0, 0],
  1927. [0, 0, 0, 0, 0]], dtype=numpy.float64)
  1928. expected = numpy.array([[0, 0, 0],
  1929. [0, 0, 0],
  1930. [0, 1, 0],
  1931. [0, 1, 0],
  1932. [0, 0, 0]], dtype=numpy.float64)
  1933. for order in range(0, 6):
  1934. out = ndimage.rotate(data, 90)
  1935. assert_array_almost_equal(out, expected)
  1936. def test_rotate04(self):
  1937. data = numpy.array([[0, 0, 0, 0, 0],
  1938. [0, 1, 1, 0, 0],
  1939. [0, 0, 0, 0, 0]], dtype=numpy.float64)
  1940. expected = numpy.array([[0, 0, 0, 0, 0],
  1941. [0, 0, 1, 0, 0],
  1942. [0, 0, 1, 0, 0]], dtype=numpy.float64)
  1943. for order in range(0, 6):
  1944. out = ndimage.rotate(data, 90, reshape=False)
  1945. assert_array_almost_equal(out, expected)
  1946. def test_rotate05(self):
  1947. data = numpy.empty((4, 3, 3))
  1948. for i in range(3):
  1949. data[:, :, i] = numpy.array([[0, 0, 0],
  1950. [0, 1, 0],
  1951. [0, 1, 0],
  1952. [0, 0, 0]], dtype=numpy.float64)
  1953. expected = numpy.array([[0, 0, 0, 0],
  1954. [0, 1, 1, 0],
  1955. [0, 0, 0, 0]], dtype=numpy.float64)
  1956. for order in range(0, 6):
  1957. out = ndimage.rotate(data, 90)
  1958. for i in range(3):
  1959. assert_array_almost_equal(out[:, :, i], expected)
  1960. def test_rotate06(self):
  1961. data = numpy.empty((3, 4, 3))
  1962. for i in range(3):
  1963. data[:, :, i] = numpy.array([[0, 0, 0, 0],
  1964. [0, 1, 1, 0],
  1965. [0, 0, 0, 0]], dtype=numpy.float64)
  1966. expected = numpy.array([[0, 0, 0],
  1967. [0, 1, 0],
  1968. [0, 1, 0],
  1969. [0, 0, 0]], dtype=numpy.float64)
  1970. for order in range(0, 6):
  1971. out = ndimage.rotate(data, 90)
  1972. for i in range(3):
  1973. assert_array_almost_equal(out[:, :, i], expected)
  1974. def test_rotate07(self):
  1975. data = numpy.array([[[0, 0, 0, 0, 0],
  1976. [0, 1, 1, 0, 0],
  1977. [0, 0, 0, 0, 0]]] * 2, dtype=numpy.float64)
  1978. data = data.transpose()
  1979. expected = numpy.array([[[0, 0, 0],
  1980. [0, 1, 0],
  1981. [0, 1, 0],
  1982. [0, 0, 0],
  1983. [0, 0, 0]]] * 2, dtype=numpy.float64)
  1984. expected = expected.transpose([2, 1, 0])
  1985. for order in range(0, 6):
  1986. out = ndimage.rotate(data, 90, axes=(0, 1))
  1987. assert_array_almost_equal(out, expected)
  1988. def test_rotate08(self):
  1989. data = numpy.array([[[0, 0, 0, 0, 0],
  1990. [0, 1, 1, 0, 0],
  1991. [0, 0, 0, 0, 0]]] * 2, dtype=numpy.float64)
  1992. data = data.transpose()
  1993. expected = numpy.array([[[0, 0, 1, 0, 0],
  1994. [0, 0, 1, 0, 0],
  1995. [0, 0, 0, 0, 0]]] * 2, dtype=numpy.float64)
  1996. expected = expected.transpose()
  1997. for order in range(0, 6):
  1998. out = ndimage.rotate(data, 90, axes=(0, 1), reshape=False)
  1999. assert_array_almost_equal(out, expected)
  2000. def test_watershed_ift01(self):
  2001. data = numpy.array([[0, 0, 0, 0, 0, 0, 0],
  2002. [0, 1, 1, 1, 1, 1, 0],
  2003. [0, 1, 0, 0, 0, 1, 0],
  2004. [0, 1, 0, 0, 0, 1, 0],
  2005. [0, 1, 0, 0, 0, 1, 0],
  2006. [0, 1, 1, 1, 1, 1, 0],
  2007. [0, 0, 0, 0, 0, 0, 0],
  2008. [0, 0, 0, 0, 0, 0, 0]], numpy.uint8)
  2009. markers = numpy.array([[-1, 0, 0, 0, 0, 0, 0],
  2010. [0, 0, 0, 0, 0, 0, 0],
  2011. [0, 0, 0, 0, 0, 0, 0],
  2012. [0, 0, 0, 1, 0, 0, 0],
  2013. [0, 0, 0, 0, 0, 0, 0],
  2014. [0, 0, 0, 0, 0, 0, 0],
  2015. [0, 0, 0, 0, 0, 0, 0],
  2016. [0, 0, 0, 0, 0, 0, 0]], numpy.int8)
  2017. out = ndimage.watershed_ift(data, markers, structure=[[1, 1, 1],
  2018. [1, 1, 1],
  2019. [1, 1, 1]])
  2020. expected = [[-1, -1, -1, -1, -1, -1, -1],
  2021. [-1, 1, 1, 1, 1, 1, -1],
  2022. [-1, 1, 1, 1, 1, 1, -1],
  2023. [-1, 1, 1, 1, 1, 1, -1],
  2024. [-1, 1, 1, 1, 1, 1, -1],
  2025. [-1, 1, 1, 1, 1, 1, -1],
  2026. [-1, -1, -1, -1, -1, -1, -1],
  2027. [-1, -1, -1, -1, -1, -1, -1]]
  2028. assert_array_almost_equal(out, expected)
  2029. def test_watershed_ift02(self):
  2030. data = numpy.array([[0, 0, 0, 0, 0, 0, 0],
  2031. [0, 1, 1, 1, 1, 1, 0],
  2032. [0, 1, 0, 0, 0, 1, 0],
  2033. [0, 1, 0, 0, 0, 1, 0],
  2034. [0, 1, 0, 0, 0, 1, 0],
  2035. [0, 1, 1, 1, 1, 1, 0],
  2036. [0, 0, 0, 0, 0, 0, 0],
  2037. [0, 0, 0, 0, 0, 0, 0]], numpy.uint8)
  2038. markers = numpy.array([[-1, 0, 0, 0, 0, 0, 0],
  2039. [0, 0, 0, 0, 0, 0, 0],
  2040. [0, 0, 0, 0, 0, 0, 0],
  2041. [0, 0, 0, 1, 0, 0, 0],
  2042. [0, 0, 0, 0, 0, 0, 0],
  2043. [0, 0, 0, 0, 0, 0, 0],
  2044. [0, 0, 0, 0, 0, 0, 0],
  2045. [0, 0, 0, 0, 0, 0, 0]], numpy.int8)
  2046. out = ndimage.watershed_ift(data, markers)
  2047. expected = [[-1, -1, -1, -1, -1, -1, -1],
  2048. [-1, -1, 1, 1, 1, -1, -1],
  2049. [-1, 1, 1, 1, 1, 1, -1],
  2050. [-1, 1, 1, 1, 1, 1, -1],
  2051. [-1, 1, 1, 1, 1, 1, -1],
  2052. [-1, -1, 1, 1, 1, -1, -1],
  2053. [-1, -1, -1, -1, -1, -1, -1],
  2054. [-1, -1, -1, -1, -1, -1, -1]]
  2055. assert_array_almost_equal(out, expected)
  2056. def test_watershed_ift03(self):
  2057. data = numpy.array([[0, 0, 0, 0, 0, 0, 0],
  2058. [0, 1, 1, 1, 1, 1, 0],
  2059. [0, 1, 0, 1, 0, 1, 0],
  2060. [0, 1, 0, 1, 0, 1, 0],
  2061. [0, 1, 0, 1, 0, 1, 0],
  2062. [0, 1, 1, 1, 1, 1, 0],
  2063. [0, 0, 0, 0, 0, 0, 0]], numpy.uint8)
  2064. markers = numpy.array([[0, 0, 0, 0, 0, 0, 0],
  2065. [0, 0, 0, 0, 0, 0, 0],
  2066. [0, 0, 0, 0, 0, 0, 0],
  2067. [0, 0, 2, 0, 3, 0, 0],
  2068. [0, 0, 0, 0, 0, 0, 0],
  2069. [0, 0, 0, 0, 0, 0, 0],
  2070. [0, 0, 0, 0, 0, 0, -1]], numpy.int8)
  2071. out = ndimage.watershed_ift(data, markers)
  2072. expected = [[-1, -1, -1, -1, -1, -1, -1],
  2073. [-1, -1, 2, -1, 3, -1, -1],
  2074. [-1, 2, 2, 3, 3, 3, -1],
  2075. [-1, 2, 2, 3, 3, 3, -1],
  2076. [-1, 2, 2, 3, 3, 3, -1],
  2077. [-1, -1, 2, -1, 3, -1, -1],
  2078. [-1, -1, -1, -1, -1, -1, -1]]
  2079. assert_array_almost_equal(out, expected)
  2080. def test_watershed_ift04(self):
  2081. data = numpy.array([[0, 0, 0, 0, 0, 0, 0],
  2082. [0, 1, 1, 1, 1, 1, 0],
  2083. [0, 1, 0, 1, 0, 1, 0],
  2084. [0, 1, 0, 1, 0, 1, 0],
  2085. [0, 1, 0, 1, 0, 1, 0],
  2086. [0, 1, 1, 1, 1, 1, 0],
  2087. [0, 0, 0, 0, 0, 0, 0]], numpy.uint8)
  2088. markers = numpy.array([[0, 0, 0, 0, 0, 0, 0],
  2089. [0, 0, 0, 0, 0, 0, 0],
  2090. [0, 0, 0, 0, 0, 0, 0],
  2091. [0, 0, 2, 0, 3, 0, 0],
  2092. [0, 0, 0, 0, 0, 0, 0],
  2093. [0, 0, 0, 0, 0, 0, 0],
  2094. [0, 0, 0, 0, 0, 0, -1]],
  2095. numpy.int8)
  2096. out = ndimage.watershed_ift(data, markers,
  2097. structure=[[1, 1, 1],
  2098. [1, 1, 1],
  2099. [1, 1, 1]])
  2100. expected = [[-1, -1, -1, -1, -1, -1, -1],
  2101. [-1, 2, 2, 3, 3, 3, -1],
  2102. [-1, 2, 2, 3, 3, 3, -1],
  2103. [-1, 2, 2, 3, 3, 3, -1],
  2104. [-1, 2, 2, 3, 3, 3, -1],
  2105. [-1, 2, 2, 3, 3, 3, -1],
  2106. [-1, -1, -1, -1, -1, -1, -1]]
  2107. assert_array_almost_equal(out, expected)
  2108. def test_watershed_ift05(self):
  2109. data = numpy.array([[0, 0, 0, 0, 0, 0, 0],
  2110. [0, 1, 1, 1, 1, 1, 0],
  2111. [0, 1, 0, 1, 0, 1, 0],
  2112. [0, 1, 0, 1, 0, 1, 0],
  2113. [0, 1, 0, 1, 0, 1, 0],
  2114. [0, 1, 1, 1, 1, 1, 0],
  2115. [0, 0, 0, 0, 0, 0, 0]], numpy.uint8)
  2116. markers = numpy.array([[0, 0, 0, 0, 0, 0, 0],
  2117. [0, 0, 0, 0, 0, 0, 0],
  2118. [0, 0, 0, 0, 0, 0, 0],
  2119. [0, 0, 3, 0, 2, 0, 0],
  2120. [0, 0, 0, 0, 0, 0, 0],
  2121. [0, 0, 0, 0, 0, 0, 0],
  2122. [0, 0, 0, 0, 0, 0, -1]],
  2123. numpy.int8)
  2124. out = ndimage.watershed_ift(data, markers,
  2125. structure=[[1, 1, 1],
  2126. [1, 1, 1],
  2127. [1, 1, 1]])
  2128. expected = [[-1, -1, -1, -1, -1, -1, -1],
  2129. [-1, 3, 3, 2, 2, 2, -1],
  2130. [-1, 3, 3, 2, 2, 2, -1],
  2131. [-1, 3, 3, 2, 2, 2, -1],
  2132. [-1, 3, 3, 2, 2, 2, -1],
  2133. [-1, 3, 3, 2, 2, 2, -1],
  2134. [-1, -1, -1, -1, -1, -1, -1]]
  2135. assert_array_almost_equal(out, expected)
  2136. def test_watershed_ift06(self):
  2137. data = numpy.array([[0, 1, 0, 0, 0, 1, 0],
  2138. [0, 1, 0, 0, 0, 1, 0],
  2139. [0, 1, 0, 0, 0, 1, 0],
  2140. [0, 1, 1, 1, 1, 1, 0],
  2141. [0, 0, 0, 0, 0, 0, 0],
  2142. [0, 0, 0, 0, 0, 0, 0]], numpy.uint8)
  2143. markers = numpy.array([[-1, 0, 0, 0, 0, 0, 0],
  2144. [0, 0, 0, 1, 0, 0, 0],
  2145. [0, 0, 0, 0, 0, 0, 0],
  2146. [0, 0, 0, 0, 0, 0, 0],
  2147. [0, 0, 0, 0, 0, 0, 0],
  2148. [0, 0, 0, 0, 0, 0, 0]], numpy.int8)
  2149. out = ndimage.watershed_ift(data, markers,
  2150. structure=[[1, 1, 1],
  2151. [1, 1, 1],
  2152. [1, 1, 1]])
  2153. expected = [[-1, 1, 1, 1, 1, 1, -1],
  2154. [-1, 1, 1, 1, 1, 1, -1],
  2155. [-1, 1, 1, 1, 1, 1, -1],
  2156. [-1, 1, 1, 1, 1, 1, -1],
  2157. [-1, -1, -1, -1, -1, -1, -1],
  2158. [-1, -1, -1, -1, -1, -1, -1]]
  2159. assert_array_almost_equal(out, expected)
  2160. def test_watershed_ift07(self):
  2161. shape = (7, 6)
  2162. data = numpy.zeros(shape, dtype=numpy.uint8)
  2163. data = data.transpose()
  2164. data[...] = numpy.array([[0, 1, 0, 0, 0, 1, 0],
  2165. [0, 1, 0, 0, 0, 1, 0],
  2166. [0, 1, 0, 0, 0, 1, 0],
  2167. [0, 1, 1, 1, 1, 1, 0],
  2168. [0, 0, 0, 0, 0, 0, 0],
  2169. [0, 0, 0, 0, 0, 0, 0]], numpy.uint8)
  2170. markers = numpy.array([[-1, 0, 0, 0, 0, 0, 0],
  2171. [0, 0, 0, 1, 0, 0, 0],
  2172. [0, 0, 0, 0, 0, 0, 0],
  2173. [0, 0, 0, 0, 0, 0, 0],
  2174. [0, 0, 0, 0, 0, 0, 0],
  2175. [0, 0, 0, 0, 0, 0, 0]], numpy.int8)
  2176. out = numpy.zeros(shape, dtype=numpy.int16)
  2177. out = out.transpose()
  2178. ndimage.watershed_ift(data, markers,
  2179. structure=[[1, 1, 1],
  2180. [1, 1, 1],
  2181. [1, 1, 1]],
  2182. output=out)
  2183. expected = [[-1, 1, 1, 1, 1, 1, -1],
  2184. [-1, 1, 1, 1, 1, 1, -1],
  2185. [-1, 1, 1, 1, 1, 1, -1],
  2186. [-1, 1, 1, 1, 1, 1, -1],
  2187. [-1, -1, -1, -1, -1, -1, -1],
  2188. [-1, -1, -1, -1, -1, -1, -1]]
  2189. assert_array_almost_equal(out, expected)
  2190. def test_distance_transform_bf01(self):
  2191. # brute force (bf) distance transform
  2192. for type_ in self.types:
  2193. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2194. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2195. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2196. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2197. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2198. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2199. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2200. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2201. [0, 0, 0, 0, 0, 0, 0, 0, 0]], type_)
  2202. out, ft = ndimage.distance_transform_bf(data, 'euclidean',
  2203. return_indices=True)
  2204. expected = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2205. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2206. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2207. [0, 0, 1, 2, 4, 2, 1, 0, 0],
  2208. [0, 0, 1, 4, 8, 4, 1, 0, 0],
  2209. [0, 0, 1, 2, 4, 2, 1, 0, 0],
  2210. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2211. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2212. [0, 0, 0, 0, 0, 0, 0, 0, 0]]
  2213. assert_array_almost_equal(out * out, expected)
  2214. expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2215. [1, 1, 1, 1, 1, 1, 1, 1, 1],
  2216. [2, 2, 2, 2, 1, 2, 2, 2, 2],
  2217. [3, 3, 3, 2, 1, 2, 3, 3, 3],
  2218. [4, 4, 4, 4, 6, 4, 4, 4, 4],
  2219. [5, 5, 6, 6, 7, 6, 6, 5, 5],
  2220. [6, 6, 6, 7, 7, 7, 6, 6, 6],
  2221. [7, 7, 7, 7, 7, 7, 7, 7, 7],
  2222. [8, 8, 8, 8, 8, 8, 8, 8, 8]],
  2223. [[0, 1, 2, 3, 4, 5, 6, 7, 8],
  2224. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  2225. [0, 1, 2, 2, 4, 6, 6, 7, 8],
  2226. [0, 1, 1, 2, 4, 6, 7, 7, 8],
  2227. [0, 1, 1, 1, 6, 7, 7, 7, 8],
  2228. [0, 1, 2, 2, 4, 6, 6, 7, 8],
  2229. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  2230. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  2231. [0, 1, 2, 3, 4, 5, 6, 7, 8]]]
  2232. assert_array_almost_equal(ft, expected)
  2233. def test_distance_transform_bf02(self):
  2234. for type_ in self.types:
  2235. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2236. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2237. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2238. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2239. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2240. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2241. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2242. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2243. [0, 0, 0, 0, 0, 0, 0, 0, 0]], type_)
  2244. out, ft = ndimage.distance_transform_bf(data, 'cityblock',
  2245. return_indices=True)
  2246. expected = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2247. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2248. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2249. [0, 0, 1, 2, 2, 2, 1, 0, 0],
  2250. [0, 0, 1, 2, 3, 2, 1, 0, 0],
  2251. [0, 0, 1, 2, 2, 2, 1, 0, 0],
  2252. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2253. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2254. [0, 0, 0, 0, 0, 0, 0, 0, 0]]
  2255. assert_array_almost_equal(out, expected)
  2256. expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2257. [1, 1, 1, 1, 1, 1, 1, 1, 1],
  2258. [2, 2, 2, 2, 1, 2, 2, 2, 2],
  2259. [3, 3, 3, 3, 1, 3, 3, 3, 3],
  2260. [4, 4, 4, 4, 7, 4, 4, 4, 4],
  2261. [5, 5, 6, 7, 7, 7, 6, 5, 5],
  2262. [6, 6, 6, 7, 7, 7, 6, 6, 6],
  2263. [7, 7, 7, 7, 7, 7, 7, 7, 7],
  2264. [8, 8, 8, 8, 8, 8, 8, 8, 8]],
  2265. [[0, 1, 2, 3, 4, 5, 6, 7, 8],
  2266. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  2267. [0, 1, 2, 2, 4, 6, 6, 7, 8],
  2268. [0, 1, 1, 1, 4, 7, 7, 7, 8],
  2269. [0, 1, 1, 1, 4, 7, 7, 7, 8],
  2270. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  2271. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  2272. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  2273. [0, 1, 2, 3, 4, 5, 6, 7, 8]]]
  2274. assert_array_almost_equal(expected, ft)
  2275. def test_distance_transform_bf03(self):
  2276. for type_ in self.types:
  2277. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2278. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2279. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2280. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2281. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2282. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2283. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2284. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2285. [0, 0, 0, 0, 0, 0, 0, 0, 0]], type_)
  2286. out, ft = ndimage.distance_transform_bf(data, 'chessboard',
  2287. return_indices=True)
  2288. expected = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2289. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2290. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2291. [0, 0, 1, 1, 2, 1, 1, 0, 0],
  2292. [0, 0, 1, 2, 2, 2, 1, 0, 0],
  2293. [0, 0, 1, 1, 2, 1, 1, 0, 0],
  2294. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2295. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2296. [0, 0, 0, 0, 0, 0, 0, 0, 0]]
  2297. assert_array_almost_equal(out, expected)
  2298. expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2299. [1, 1, 1, 1, 1, 1, 1, 1, 1],
  2300. [2, 2, 2, 2, 1, 2, 2, 2, 2],
  2301. [3, 3, 4, 2, 2, 2, 4, 3, 3],
  2302. [4, 4, 5, 6, 6, 6, 5, 4, 4],
  2303. [5, 5, 6, 6, 7, 6, 6, 5, 5],
  2304. [6, 6, 6, 7, 7, 7, 6, 6, 6],
  2305. [7, 7, 7, 7, 7, 7, 7, 7, 7],
  2306. [8, 8, 8, 8, 8, 8, 8, 8, 8]],
  2307. [[0, 1, 2, 3, 4, 5, 6, 7, 8],
  2308. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  2309. [0, 1, 2, 2, 5, 6, 6, 7, 8],
  2310. [0, 1, 1, 2, 6, 6, 7, 7, 8],
  2311. [0, 1, 1, 2, 6, 7, 7, 7, 8],
  2312. [0, 1, 2, 2, 6, 6, 7, 7, 8],
  2313. [0, 1, 2, 4, 5, 6, 6, 7, 8],
  2314. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  2315. [0, 1, 2, 3, 4, 5, 6, 7, 8]]]
  2316. assert_array_almost_equal(ft, expected)
  2317. def test_distance_transform_bf04(self):
  2318. for type_ in self.types:
  2319. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2320. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2321. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2322. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2323. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2324. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2325. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2326. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2327. [0, 0, 0, 0, 0, 0, 0, 0, 0]], type_)
  2328. tdt, tft = ndimage.distance_transform_bf(data, return_indices=1)
  2329. dts = []
  2330. fts = []
  2331. dt = numpy.zeros(data.shape, dtype=numpy.float64)
  2332. ndimage.distance_transform_bf(data, distances=dt)
  2333. dts.append(dt)
  2334. ft = ndimage.distance_transform_bf(
  2335. data, return_distances=False, return_indices=1)
  2336. fts.append(ft)
  2337. ft = numpy.indices(data.shape, dtype=numpy.int32)
  2338. ndimage.distance_transform_bf(
  2339. data, return_distances=False, return_indices=True, indices=ft)
  2340. fts.append(ft)
  2341. dt, ft = ndimage.distance_transform_bf(
  2342. data, return_indices=1)
  2343. dts.append(dt)
  2344. fts.append(ft)
  2345. dt = numpy.zeros(data.shape, dtype=numpy.float64)
  2346. ft = ndimage.distance_transform_bf(
  2347. data, distances=dt, return_indices=True)
  2348. dts.append(dt)
  2349. fts.append(ft)
  2350. ft = numpy.indices(data.shape, dtype=numpy.int32)
  2351. dt = ndimage.distance_transform_bf(
  2352. data, return_indices=True, indices=ft)
  2353. dts.append(dt)
  2354. fts.append(ft)
  2355. dt = numpy.zeros(data.shape, dtype=numpy.float64)
  2356. ft = numpy.indices(data.shape, dtype=numpy.int32)
  2357. ndimage.distance_transform_bf(
  2358. data, distances=dt, return_indices=True, indices=ft)
  2359. dts.append(dt)
  2360. fts.append(ft)
  2361. for dt in dts:
  2362. assert_array_almost_equal(tdt, dt)
  2363. for ft in fts:
  2364. assert_array_almost_equal(tft, ft)
  2365. def test_distance_transform_bf05(self):
  2366. for type_ in self.types:
  2367. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2368. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2369. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2370. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2371. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2372. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2373. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2374. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2375. [0, 0, 0, 0, 0, 0, 0, 0, 0]], type_)
  2376. out, ft = ndimage.distance_transform_bf(
  2377. data, 'euclidean', return_indices=True, sampling=[2, 2])
  2378. expected = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2379. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2380. [0, 0, 0, 4, 4, 4, 0, 0, 0],
  2381. [0, 0, 4, 8, 16, 8, 4, 0, 0],
  2382. [0, 0, 4, 16, 32, 16, 4, 0, 0],
  2383. [0, 0, 4, 8, 16, 8, 4, 0, 0],
  2384. [0, 0, 0, 4, 4, 4, 0, 0, 0],
  2385. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2386. [0, 0, 0, 0, 0, 0, 0, 0, 0]]
  2387. assert_array_almost_equal(out * out, expected)
  2388. expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2389. [1, 1, 1, 1, 1, 1, 1, 1, 1],
  2390. [2, 2, 2, 2, 1, 2, 2, 2, 2],
  2391. [3, 3, 3, 2, 1, 2, 3, 3, 3],
  2392. [4, 4, 4, 4, 6, 4, 4, 4, 4],
  2393. [5, 5, 6, 6, 7, 6, 6, 5, 5],
  2394. [6, 6, 6, 7, 7, 7, 6, 6, 6],
  2395. [7, 7, 7, 7, 7, 7, 7, 7, 7],
  2396. [8, 8, 8, 8, 8, 8, 8, 8, 8]],
  2397. [[0, 1, 2, 3, 4, 5, 6, 7, 8],
  2398. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  2399. [0, 1, 2, 2, 4, 6, 6, 7, 8],
  2400. [0, 1, 1, 2, 4, 6, 7, 7, 8],
  2401. [0, 1, 1, 1, 6, 7, 7, 7, 8],
  2402. [0, 1, 2, 2, 4, 6, 6, 7, 8],
  2403. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  2404. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  2405. [0, 1, 2, 3, 4, 5, 6, 7, 8]]]
  2406. assert_array_almost_equal(ft, expected)
  2407. def test_distance_transform_bf06(self):
  2408. for type_ in self.types:
  2409. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2410. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2411. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2412. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2413. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2414. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2415. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2416. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2417. [0, 0, 0, 0, 0, 0, 0, 0, 0]], type_)
  2418. out, ft = ndimage.distance_transform_bf(
  2419. data, 'euclidean', return_indices=True, sampling=[2, 1])
  2420. expected = [[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2421. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2422. [0, 0, 0, 1, 4, 1, 0, 0, 0],
  2423. [0, 0, 1, 4, 8, 4, 1, 0, 0],
  2424. [0, 0, 1, 4, 9, 4, 1, 0, 0],
  2425. [0, 0, 1, 4, 8, 4, 1, 0, 0],
  2426. [0, 0, 0, 1, 4, 1, 0, 0, 0],
  2427. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2428. [0, 0, 0, 0, 0, 0, 0, 0, 0]]
  2429. assert_array_almost_equal(out * out, expected)
  2430. expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2431. [1, 1, 1, 1, 1, 1, 1, 1, 1],
  2432. [2, 2, 2, 2, 2, 2, 2, 2, 2],
  2433. [3, 3, 3, 3, 2, 3, 3, 3, 3],
  2434. [4, 4, 4, 4, 4, 4, 4, 4, 4],
  2435. [5, 5, 5, 5, 6, 5, 5, 5, 5],
  2436. [6, 6, 6, 6, 7, 6, 6, 6, 6],
  2437. [7, 7, 7, 7, 7, 7, 7, 7, 7],
  2438. [8, 8, 8, 8, 8, 8, 8, 8, 8]],
  2439. [[0, 1, 2, 3, 4, 5, 6, 7, 8],
  2440. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  2441. [0, 1, 2, 2, 6, 6, 6, 7, 8],
  2442. [0, 1, 1, 1, 6, 7, 7, 7, 8],
  2443. [0, 1, 1, 1, 7, 7, 7, 7, 8],
  2444. [0, 1, 1, 1, 6, 7, 7, 7, 8],
  2445. [0, 1, 2, 2, 4, 6, 6, 7, 8],
  2446. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  2447. [0, 1, 2, 3, 4, 5, 6, 7, 8]]]
  2448. assert_array_almost_equal(ft, expected)
  2449. def test_distance_transform_cdt01(self):
  2450. # chamfer type distance (cdt) transform
  2451. for type_ in self.types:
  2452. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2453. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2454. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2455. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2456. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2457. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2458. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2459. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2460. [0, 0, 0, 0, 0, 0, 0, 0, 0]], type_)
  2461. out, ft = ndimage.distance_transform_cdt(
  2462. data, 'cityblock', return_indices=True)
  2463. bf = ndimage.distance_transform_bf(data, 'cityblock')
  2464. assert_array_almost_equal(bf, out)
  2465. expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2466. [1, 1, 1, 1, 1, 1, 1, 1, 1],
  2467. [2, 2, 2, 1, 1, 1, 2, 2, 2],
  2468. [3, 3, 2, 1, 1, 1, 2, 3, 3],
  2469. [4, 4, 4, 4, 1, 4, 4, 4, 4],
  2470. [5, 5, 5, 5, 7, 7, 6, 5, 5],
  2471. [6, 6, 6, 6, 7, 7, 6, 6, 6],
  2472. [7, 7, 7, 7, 7, 7, 7, 7, 7],
  2473. [8, 8, 8, 8, 8, 8, 8, 8, 8]],
  2474. [[0, 1, 2, 3, 4, 5, 6, 7, 8],
  2475. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  2476. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  2477. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  2478. [0, 1, 1, 1, 4, 7, 7, 7, 8],
  2479. [0, 1, 1, 1, 4, 5, 6, 7, 8],
  2480. [0, 1, 2, 2, 4, 5, 6, 7, 8],
  2481. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  2482. [0, 1, 2, 3, 4, 5, 6, 7, 8]]]
  2483. assert_array_almost_equal(ft, expected)
  2484. def test_distance_transform_cdt02(self):
  2485. for type_ in self.types:
  2486. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2487. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2488. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2489. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2490. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2491. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2492. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2493. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2494. [0, 0, 0, 0, 0, 0, 0, 0, 0]], type_)
  2495. out, ft = ndimage.distance_transform_cdt(data, 'chessboard',
  2496. return_indices=True)
  2497. bf = ndimage.distance_transform_bf(data, 'chessboard')
  2498. assert_array_almost_equal(bf, out)
  2499. expected = [[[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2500. [1, 1, 1, 1, 1, 1, 1, 1, 1],
  2501. [2, 2, 2, 1, 1, 1, 2, 2, 2],
  2502. [3, 3, 2, 2, 1, 2, 2, 3, 3],
  2503. [4, 4, 3, 2, 2, 2, 3, 4, 4],
  2504. [5, 5, 4, 6, 7, 6, 4, 5, 5],
  2505. [6, 6, 6, 6, 7, 7, 6, 6, 6],
  2506. [7, 7, 7, 7, 7, 7, 7, 7, 7],
  2507. [8, 8, 8, 8, 8, 8, 8, 8, 8]],
  2508. [[0, 1, 2, 3, 4, 5, 6, 7, 8],
  2509. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  2510. [0, 1, 2, 2, 3, 4, 6, 7, 8],
  2511. [0, 1, 1, 2, 2, 6, 6, 7, 8],
  2512. [0, 1, 1, 1, 2, 6, 7, 7, 8],
  2513. [0, 1, 1, 2, 6, 6, 7, 7, 8],
  2514. [0, 1, 2, 2, 5, 6, 6, 7, 8],
  2515. [0, 1, 2, 3, 4, 5, 6, 7, 8],
  2516. [0, 1, 2, 3, 4, 5, 6, 7, 8]]]
  2517. assert_array_almost_equal(ft, expected)
  2518. def test_distance_transform_cdt03(self):
  2519. for type_ in self.types:
  2520. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2521. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2522. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2523. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2524. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2525. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2526. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2527. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2528. [0, 0, 0, 0, 0, 0, 0, 0, 0]], type_)
  2529. tdt, tft = ndimage.distance_transform_cdt(data, return_indices=True)
  2530. dts = []
  2531. fts = []
  2532. dt = numpy.zeros(data.shape, dtype=numpy.int32)
  2533. ndimage.distance_transform_cdt(data, distances=dt)
  2534. dts.append(dt)
  2535. ft = ndimage.distance_transform_cdt(
  2536. data, return_distances=False, return_indices=True)
  2537. fts.append(ft)
  2538. ft = numpy.indices(data.shape, dtype=numpy.int32)
  2539. ndimage.distance_transform_cdt(
  2540. data, return_distances=False, return_indices=True, indices=ft)
  2541. fts.append(ft)
  2542. dt, ft = ndimage.distance_transform_cdt(
  2543. data, return_indices=True)
  2544. dts.append(dt)
  2545. fts.append(ft)
  2546. dt = numpy.zeros(data.shape, dtype=numpy.int32)
  2547. ft = ndimage.distance_transform_cdt(
  2548. data, distances=dt, return_indices=True)
  2549. dts.append(dt)
  2550. fts.append(ft)
  2551. ft = numpy.indices(data.shape, dtype=numpy.int32)
  2552. dt = ndimage.distance_transform_cdt(
  2553. data, return_indices=True, indices=ft)
  2554. dts.append(dt)
  2555. fts.append(ft)
  2556. dt = numpy.zeros(data.shape, dtype=numpy.int32)
  2557. ft = numpy.indices(data.shape, dtype=numpy.int32)
  2558. ndimage.distance_transform_cdt(data, distances=dt,
  2559. return_indices=True, indices=ft)
  2560. dts.append(dt)
  2561. fts.append(ft)
  2562. for dt in dts:
  2563. assert_array_almost_equal(tdt, dt)
  2564. for ft in fts:
  2565. assert_array_almost_equal(tft, ft)
  2566. def test_distance_transform_edt01(self):
  2567. # euclidean distance transform (edt)
  2568. for type_ in self.types:
  2569. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2570. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2571. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2572. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2573. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2574. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2575. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2576. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2577. [0, 0, 0, 0, 0, 0, 0, 0, 0]], type_)
  2578. out, ft = ndimage.distance_transform_edt(data, return_indices=True)
  2579. bf = ndimage.distance_transform_bf(data, 'euclidean')
  2580. assert_array_almost_equal(bf, out)
  2581. dt = ft - numpy.indices(ft.shape[1:], dtype=ft.dtype)
  2582. dt = dt.astype(numpy.float64)
  2583. numpy.multiply(dt, dt, dt)
  2584. dt = numpy.add.reduce(dt, axis=0)
  2585. numpy.sqrt(dt, dt)
  2586. assert_array_almost_equal(bf, dt)
  2587. def test_distance_transform_edt02(self):
  2588. for type_ in self.types:
  2589. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2590. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2591. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2592. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2593. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2594. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2595. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2596. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2597. [0, 0, 0, 0, 0, 0, 0, 0, 0]], type_)
  2598. tdt, tft = ndimage.distance_transform_edt(data, return_indices=True)
  2599. dts = []
  2600. fts = []
  2601. dt = numpy.zeros(data.shape, dtype=numpy.float64)
  2602. ndimage.distance_transform_edt(data, distances=dt)
  2603. dts.append(dt)
  2604. ft = ndimage.distance_transform_edt(
  2605. data, return_distances=0, return_indices=True)
  2606. fts.append(ft)
  2607. ft = numpy.indices(data.shape, dtype=numpy.int32)
  2608. ndimage.distance_transform_edt(
  2609. data, return_distances=False, return_indices=True, indices=ft)
  2610. fts.append(ft)
  2611. dt, ft = ndimage.distance_transform_edt(
  2612. data, return_indices=True)
  2613. dts.append(dt)
  2614. fts.append(ft)
  2615. dt = numpy.zeros(data.shape, dtype=numpy.float64)
  2616. ft = ndimage.distance_transform_edt(
  2617. data, distances=dt, return_indices=True)
  2618. dts.append(dt)
  2619. fts.append(ft)
  2620. ft = numpy.indices(data.shape, dtype=numpy.int32)
  2621. dt = ndimage.distance_transform_edt(
  2622. data, return_indices=True, indices=ft)
  2623. dts.append(dt)
  2624. fts.append(ft)
  2625. dt = numpy.zeros(data.shape, dtype=numpy.float64)
  2626. ft = numpy.indices(data.shape, dtype=numpy.int32)
  2627. ndimage.distance_transform_edt(
  2628. data, distances=dt, return_indices=True, indices=ft)
  2629. dts.append(dt)
  2630. fts.append(ft)
  2631. for dt in dts:
  2632. assert_array_almost_equal(tdt, dt)
  2633. for ft in fts:
  2634. assert_array_almost_equal(tft, ft)
  2635. def test_distance_transform_edt03(self):
  2636. for type_ in self.types:
  2637. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2638. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2639. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2640. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2641. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2642. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2643. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2644. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2645. [0, 0, 0, 0, 0, 0, 0, 0, 0]], type_)
  2646. ref = ndimage.distance_transform_bf(data, 'euclidean', sampling=[2, 2])
  2647. out = ndimage.distance_transform_edt(data, sampling=[2, 2])
  2648. assert_array_almost_equal(ref, out)
  2649. def test_distance_transform_edt4(self):
  2650. for type_ in self.types:
  2651. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0, 0],
  2652. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2653. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2654. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2655. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2656. [0, 0, 1, 1, 1, 1, 1, 0, 0],
  2657. [0, 0, 0, 1, 1, 1, 0, 0, 0],
  2658. [0, 0, 0, 0, 0, 0, 0, 0, 0],
  2659. [0, 0, 0, 0, 0, 0, 0, 0, 0]], type_)
  2660. ref = ndimage.distance_transform_bf(data, 'euclidean', sampling=[2, 1])
  2661. out = ndimage.distance_transform_edt(data, sampling=[2, 1])
  2662. assert_array_almost_equal(ref, out)
  2663. def test_distance_transform_edt5(self):
  2664. # Ticket #954 regression test
  2665. out = ndimage.distance_transform_edt(False)
  2666. assert_array_almost_equal(out, [0.])
  2667. def test_generate_structure01(self):
  2668. struct = ndimage.generate_binary_structure(0, 1)
  2669. assert_array_almost_equal(struct, 1)
  2670. def test_generate_structure02(self):
  2671. struct = ndimage.generate_binary_structure(1, 1)
  2672. assert_array_almost_equal(struct, [1, 1, 1])
  2673. def test_generate_structure03(self):
  2674. struct = ndimage.generate_binary_structure(2, 1)
  2675. assert_array_almost_equal(struct, [[0, 1, 0],
  2676. [1, 1, 1],
  2677. [0, 1, 0]])
  2678. def test_generate_structure04(self):
  2679. struct = ndimage.generate_binary_structure(2, 2)
  2680. assert_array_almost_equal(struct, [[1, 1, 1],
  2681. [1, 1, 1],
  2682. [1, 1, 1]])
  2683. def test_iterate_structure01(self):
  2684. struct = [[0, 1, 0],
  2685. [1, 1, 1],
  2686. [0, 1, 0]]
  2687. out = ndimage.iterate_structure(struct, 2)
  2688. assert_array_almost_equal(out, [[0, 0, 1, 0, 0],
  2689. [0, 1, 1, 1, 0],
  2690. [1, 1, 1, 1, 1],
  2691. [0, 1, 1, 1, 0],
  2692. [0, 0, 1, 0, 0]])
  2693. def test_iterate_structure02(self):
  2694. struct = [[0, 1],
  2695. [1, 1],
  2696. [0, 1]]
  2697. out = ndimage.iterate_structure(struct, 2)
  2698. assert_array_almost_equal(out, [[0, 0, 1],
  2699. [0, 1, 1],
  2700. [1, 1, 1],
  2701. [0, 1, 1],
  2702. [0, 0, 1]])
  2703. def test_iterate_structure03(self):
  2704. struct = [[0, 1, 0],
  2705. [1, 1, 1],
  2706. [0, 1, 0]]
  2707. out = ndimage.iterate_structure(struct, 2, 1)
  2708. expected = [[0, 0, 1, 0, 0],
  2709. [0, 1, 1, 1, 0],
  2710. [1, 1, 1, 1, 1],
  2711. [0, 1, 1, 1, 0],
  2712. [0, 0, 1, 0, 0]]
  2713. assert_array_almost_equal(out[0], expected)
  2714. assert_equal(out[1], [2, 2])
  2715. def test_binary_erosion01(self):
  2716. for type_ in self.types:
  2717. data = numpy.ones([], type_)
  2718. out = ndimage.binary_erosion(data)
  2719. assert_array_almost_equal(out, 1)
  2720. def test_binary_erosion02(self):
  2721. for type_ in self.types:
  2722. data = numpy.ones([], type_)
  2723. out = ndimage.binary_erosion(data, border_value=1)
  2724. assert_array_almost_equal(out, 1)
  2725. def test_binary_erosion03(self):
  2726. for type_ in self.types:
  2727. data = numpy.ones([1], type_)
  2728. out = ndimage.binary_erosion(data)
  2729. assert_array_almost_equal(out, [0])
  2730. def test_binary_erosion04(self):
  2731. for type_ in self.types:
  2732. data = numpy.ones([1], type_)
  2733. out = ndimage.binary_erosion(data, border_value=1)
  2734. assert_array_almost_equal(out, [1])
  2735. def test_binary_erosion05(self):
  2736. for type_ in self.types:
  2737. data = numpy.ones([3], type_)
  2738. out = ndimage.binary_erosion(data)
  2739. assert_array_almost_equal(out, [0, 1, 0])
  2740. def test_binary_erosion06(self):
  2741. for type_ in self.types:
  2742. data = numpy.ones([3], type_)
  2743. out = ndimage.binary_erosion(data, border_value=1)
  2744. assert_array_almost_equal(out, [1, 1, 1])
  2745. def test_binary_erosion07(self):
  2746. for type_ in self.types:
  2747. data = numpy.ones([5], type_)
  2748. out = ndimage.binary_erosion(data)
  2749. assert_array_almost_equal(out, [0, 1, 1, 1, 0])
  2750. def test_binary_erosion08(self):
  2751. for type_ in self.types:
  2752. data = numpy.ones([5], type_)
  2753. out = ndimage.binary_erosion(data, border_value=1)
  2754. assert_array_almost_equal(out, [1, 1, 1, 1, 1])
  2755. def test_binary_erosion09(self):
  2756. for type_ in self.types:
  2757. data = numpy.ones([5], type_)
  2758. data[2] = 0
  2759. out = ndimage.binary_erosion(data)
  2760. assert_array_almost_equal(out, [0, 0, 0, 0, 0])
  2761. def test_binary_erosion10(self):
  2762. for type_ in self.types:
  2763. data = numpy.ones([5], type_)
  2764. data[2] = 0
  2765. out = ndimage.binary_erosion(data, border_value=1)
  2766. assert_array_almost_equal(out, [1, 0, 0, 0, 1])
  2767. def test_binary_erosion11(self):
  2768. for type_ in self.types:
  2769. data = numpy.ones([5], type_)
  2770. data[2] = 0
  2771. struct = [1, 0, 1]
  2772. out = ndimage.binary_erosion(data, struct, border_value=1)
  2773. assert_array_almost_equal(out, [1, 0, 1, 0, 1])
  2774. def test_binary_erosion12(self):
  2775. for type_ in self.types:
  2776. data = numpy.ones([5], type_)
  2777. data[2] = 0
  2778. struct = [1, 0, 1]
  2779. out = ndimage.binary_erosion(data, struct, border_value=1,
  2780. origin=-1)
  2781. assert_array_almost_equal(out, [0, 1, 0, 1, 1])
  2782. def test_binary_erosion13(self):
  2783. for type_ in self.types:
  2784. data = numpy.ones([5], type_)
  2785. data[2] = 0
  2786. struct = [1, 0, 1]
  2787. out = ndimage.binary_erosion(data, struct, border_value=1,
  2788. origin=1)
  2789. assert_array_almost_equal(out, [1, 1, 0, 1, 0])
  2790. def test_binary_erosion14(self):
  2791. for type_ in self.types:
  2792. data = numpy.ones([5], type_)
  2793. data[2] = 0
  2794. struct = [1, 1]
  2795. out = ndimage.binary_erosion(data, struct, border_value=1)
  2796. assert_array_almost_equal(out, [1, 1, 0, 0, 1])
  2797. def test_binary_erosion15(self):
  2798. for type_ in self.types:
  2799. data = numpy.ones([5], type_)
  2800. data[2] = 0
  2801. struct = [1, 1]
  2802. out = ndimage.binary_erosion(data, struct, border_value=1,
  2803. origin=-1)
  2804. assert_array_almost_equal(out, [1, 0, 0, 1, 1])
  2805. def test_binary_erosion16(self):
  2806. for type_ in self.types:
  2807. data = numpy.ones([1, 1], type_)
  2808. out = ndimage.binary_erosion(data, border_value=1)
  2809. assert_array_almost_equal(out, [[1]])
  2810. def test_binary_erosion17(self):
  2811. for type_ in self.types:
  2812. data = numpy.ones([1, 1], type_)
  2813. out = ndimage.binary_erosion(data)
  2814. assert_array_almost_equal(out, [[0]])
  2815. def test_binary_erosion18(self):
  2816. for type_ in self.types:
  2817. data = numpy.ones([1, 3], type_)
  2818. out = ndimage.binary_erosion(data)
  2819. assert_array_almost_equal(out, [[0, 0, 0]])
  2820. def test_binary_erosion19(self):
  2821. for type_ in self.types:
  2822. data = numpy.ones([1, 3], type_)
  2823. out = ndimage.binary_erosion(data, border_value=1)
  2824. assert_array_almost_equal(out, [[1, 1, 1]])
  2825. def test_binary_erosion20(self):
  2826. for type_ in self.types:
  2827. data = numpy.ones([3, 3], type_)
  2828. out = ndimage.binary_erosion(data)
  2829. assert_array_almost_equal(out, [[0, 0, 0],
  2830. [0, 1, 0],
  2831. [0, 0, 0]])
  2832. def test_binary_erosion21(self):
  2833. for type_ in self.types:
  2834. data = numpy.ones([3, 3], type_)
  2835. out = ndimage.binary_erosion(data, border_value=1)
  2836. assert_array_almost_equal(out, [[1, 1, 1],
  2837. [1, 1, 1],
  2838. [1, 1, 1]])
  2839. def test_binary_erosion22(self):
  2840. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  2841. [0, 0, 0, 0, 0, 0, 0, 0],
  2842. [0, 0, 0, 0, 0, 0, 0, 0],
  2843. [0, 0, 0, 0, 0, 1, 0, 0],
  2844. [0, 0, 0, 1, 1, 0, 0, 0],
  2845. [0, 0, 1, 0, 0, 1, 0, 0],
  2846. [0, 0, 0, 0, 0, 0, 0, 0],
  2847. [0, 0, 0, 0, 0, 0, 0, 0]]
  2848. for type_ in self.types:
  2849. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  2850. [0, 1, 0, 0, 0, 0, 0, 0],
  2851. [0, 0, 0, 0, 0, 1, 1, 1],
  2852. [0, 0, 1, 1, 1, 1, 1, 1],
  2853. [0, 0, 1, 1, 1, 1, 0, 0],
  2854. [0, 1, 1, 1, 1, 1, 1, 0],
  2855. [0, 1, 1, 0, 0, 1, 1, 0],
  2856. [0, 0, 0, 0, 0, 0, 0, 0]], type_)
  2857. out = ndimage.binary_erosion(data, border_value=1)
  2858. assert_array_almost_equal(out, expected)
  2859. def test_binary_erosion23(self):
  2860. struct = ndimage.generate_binary_structure(2, 2)
  2861. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  2862. [0, 0, 0, 0, 0, 0, 0, 0],
  2863. [0, 0, 0, 0, 0, 0, 0, 0],
  2864. [0, 0, 0, 0, 0, 0, 0, 0],
  2865. [0, 0, 0, 1, 1, 0, 0, 0],
  2866. [0, 0, 0, 0, 0, 0, 0, 0],
  2867. [0, 0, 0, 0, 0, 0, 0, 0],
  2868. [0, 0, 0, 0, 0, 0, 0, 0]]
  2869. for type_ in self.types:
  2870. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  2871. [0, 1, 0, 0, 0, 0, 0, 0],
  2872. [0, 0, 0, 0, 0, 1, 1, 1],
  2873. [0, 0, 1, 1, 1, 1, 1, 1],
  2874. [0, 0, 1, 1, 1, 1, 0, 0],
  2875. [0, 1, 1, 1, 1, 1, 1, 0],
  2876. [0, 1, 1, 0, 0, 1, 1, 0],
  2877. [0, 0, 0, 0, 0, 0, 0, 0]], type_)
  2878. out = ndimage.binary_erosion(data, struct, border_value=1)
  2879. assert_array_almost_equal(out, expected)
  2880. def test_binary_erosion24(self):
  2881. struct = [[0, 1],
  2882. [1, 1]]
  2883. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  2884. [0, 0, 0, 0, 0, 0, 0, 0],
  2885. [0, 0, 0, 0, 0, 0, 0, 0],
  2886. [0, 0, 0, 0, 0, 1, 1, 1],
  2887. [0, 0, 0, 1, 1, 1, 0, 0],
  2888. [0, 0, 1, 1, 1, 1, 0, 0],
  2889. [0, 0, 1, 0, 0, 0, 1, 0],
  2890. [0, 0, 0, 0, 0, 0, 0, 0]]
  2891. for type_ in self.types:
  2892. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  2893. [0, 1, 0, 0, 0, 0, 0, 0],
  2894. [0, 0, 0, 0, 0, 1, 1, 1],
  2895. [0, 0, 1, 1, 1, 1, 1, 1],
  2896. [0, 0, 1, 1, 1, 1, 0, 0],
  2897. [0, 1, 1, 1, 1, 1, 1, 0],
  2898. [0, 1, 1, 0, 0, 1, 1, 0],
  2899. [0, 0, 0, 0, 0, 0, 0, 0]], type_)
  2900. out = ndimage.binary_erosion(data, struct, border_value=1)
  2901. assert_array_almost_equal(out, expected)
  2902. def test_binary_erosion25(self):
  2903. struct = [[0, 1, 0],
  2904. [1, 0, 1],
  2905. [0, 1, 0]]
  2906. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  2907. [0, 0, 0, 0, 0, 0, 0, 0],
  2908. [0, 0, 0, 0, 0, 0, 0, 0],
  2909. [0, 0, 0, 0, 0, 1, 0, 0],
  2910. [0, 0, 0, 1, 0, 0, 0, 0],
  2911. [0, 0, 1, 0, 0, 1, 0, 0],
  2912. [0, 0, 0, 0, 0, 0, 0, 0],
  2913. [0, 0, 0, 0, 0, 0, 0, 0]]
  2914. for type_ in self.types:
  2915. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  2916. [0, 1, 0, 0, 0, 0, 0, 0],
  2917. [0, 0, 0, 0, 0, 1, 1, 1],
  2918. [0, 0, 1, 1, 1, 0, 1, 1],
  2919. [0, 0, 1, 0, 1, 1, 0, 0],
  2920. [0, 1, 0, 1, 1, 1, 1, 0],
  2921. [0, 1, 1, 0, 0, 1, 1, 0],
  2922. [0, 0, 0, 0, 0, 0, 0, 0]], type_)
  2923. out = ndimage.binary_erosion(data, struct, border_value=1)
  2924. assert_array_almost_equal(out, expected)
  2925. def test_binary_erosion26(self):
  2926. struct = [[0, 1, 0],
  2927. [1, 0, 1],
  2928. [0, 1, 0]]
  2929. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  2930. [0, 0, 0, 0, 0, 0, 0, 1],
  2931. [0, 0, 0, 0, 1, 0, 0, 1],
  2932. [0, 0, 1, 0, 0, 0, 0, 0],
  2933. [0, 1, 0, 0, 1, 0, 0, 0],
  2934. [0, 0, 0, 0, 0, 0, 0, 0],
  2935. [0, 0, 0, 0, 0, 0, 0, 0],
  2936. [0, 0, 0, 0, 0, 0, 0, 1]]
  2937. for type_ in self.types:
  2938. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  2939. [0, 1, 0, 0, 0, 0, 0, 0],
  2940. [0, 0, 0, 0, 0, 1, 1, 1],
  2941. [0, 0, 1, 1, 1, 0, 1, 1],
  2942. [0, 0, 1, 0, 1, 1, 0, 0],
  2943. [0, 1, 0, 1, 1, 1, 1, 0],
  2944. [0, 1, 1, 0, 0, 1, 1, 0],
  2945. [0, 0, 0, 0, 0, 0, 0, 0]], type_)
  2946. out = ndimage.binary_erosion(data, struct, border_value=1,
  2947. origin=(-1, -1))
  2948. assert_array_almost_equal(out, expected)
  2949. def test_binary_erosion27(self):
  2950. struct = [[0, 1, 0],
  2951. [1, 1, 1],
  2952. [0, 1, 0]]
  2953. expected = [[0, 0, 0, 0, 0, 0, 0],
  2954. [0, 0, 0, 0, 0, 0, 0],
  2955. [0, 0, 0, 0, 0, 0, 0],
  2956. [0, 0, 0, 1, 0, 0, 0],
  2957. [0, 0, 0, 0, 0, 0, 0],
  2958. [0, 0, 0, 0, 0, 0, 0],
  2959. [0, 0, 0, 0, 0, 0, 0]]
  2960. data = numpy.array([[0, 0, 0, 0, 0, 0, 0],
  2961. [0, 0, 0, 1, 0, 0, 0],
  2962. [0, 0, 1, 1, 1, 0, 0],
  2963. [0, 1, 1, 1, 1, 1, 0],
  2964. [0, 0, 1, 1, 1, 0, 0],
  2965. [0, 0, 0, 1, 0, 0, 0],
  2966. [0, 0, 0, 0, 0, 0, 0]], bool)
  2967. out = ndimage.binary_erosion(data, struct, border_value=1,
  2968. iterations=2)
  2969. assert_array_almost_equal(out, expected)
  2970. def test_binary_erosion28(self):
  2971. struct = [[0, 1, 0],
  2972. [1, 1, 1],
  2973. [0, 1, 0]]
  2974. expected = [[0, 0, 0, 0, 0, 0, 0],
  2975. [0, 0, 0, 0, 0, 0, 0],
  2976. [0, 0, 0, 0, 0, 0, 0],
  2977. [0, 0, 0, 1, 0, 0, 0],
  2978. [0, 0, 0, 0, 0, 0, 0],
  2979. [0, 0, 0, 0, 0, 0, 0],
  2980. [0, 0, 0, 0, 0, 0, 0]]
  2981. data = numpy.array([[0, 0, 0, 0, 0, 0, 0],
  2982. [0, 0, 0, 1, 0, 0, 0],
  2983. [0, 0, 1, 1, 1, 0, 0],
  2984. [0, 1, 1, 1, 1, 1, 0],
  2985. [0, 0, 1, 1, 1, 0, 0],
  2986. [0, 0, 0, 1, 0, 0, 0],
  2987. [0, 0, 0, 0, 0, 0, 0]], bool)
  2988. out = numpy.zeros(data.shape, bool)
  2989. ndimage.binary_erosion(data, struct, border_value=1,
  2990. iterations=2, output=out)
  2991. assert_array_almost_equal(out, expected)
  2992. def test_binary_erosion29(self):
  2993. struct = [[0, 1, 0],
  2994. [1, 1, 1],
  2995. [0, 1, 0]]
  2996. expected = [[0, 0, 0, 0, 0, 0, 0],
  2997. [0, 0, 0, 0, 0, 0, 0],
  2998. [0, 0, 0, 0, 0, 0, 0],
  2999. [0, 0, 0, 1, 0, 0, 0],
  3000. [0, 0, 0, 0, 0, 0, 0],
  3001. [0, 0, 0, 0, 0, 0, 0],
  3002. [0, 0, 0, 0, 0, 0, 0]]
  3003. data = numpy.array([[0, 0, 0, 1, 0, 0, 0],
  3004. [0, 0, 1, 1, 1, 0, 0],
  3005. [0, 1, 1, 1, 1, 1, 0],
  3006. [1, 1, 1, 1, 1, 1, 1],
  3007. [0, 1, 1, 1, 1, 1, 0],
  3008. [0, 0, 1, 1, 1, 0, 0],
  3009. [0, 0, 0, 1, 0, 0, 0]], bool)
  3010. out = ndimage.binary_erosion(data, struct,
  3011. border_value=1, iterations=3)
  3012. assert_array_almost_equal(out, expected)
  3013. def test_binary_erosion30(self):
  3014. struct = [[0, 1, 0],
  3015. [1, 1, 1],
  3016. [0, 1, 0]]
  3017. expected = [[0, 0, 0, 0, 0, 0, 0],
  3018. [0, 0, 0, 0, 0, 0, 0],
  3019. [0, 0, 0, 0, 0, 0, 0],
  3020. [0, 0, 0, 1, 0, 0, 0],
  3021. [0, 0, 0, 0, 0, 0, 0],
  3022. [0, 0, 0, 0, 0, 0, 0],
  3023. [0, 0, 0, 0, 0, 0, 0]]
  3024. data = numpy.array([[0, 0, 0, 1, 0, 0, 0],
  3025. [0, 0, 1, 1, 1, 0, 0],
  3026. [0, 1, 1, 1, 1, 1, 0],
  3027. [1, 1, 1, 1, 1, 1, 1],
  3028. [0, 1, 1, 1, 1, 1, 0],
  3029. [0, 0, 1, 1, 1, 0, 0],
  3030. [0, 0, 0, 1, 0, 0, 0]], bool)
  3031. out = numpy.zeros(data.shape, bool)
  3032. ndimage.binary_erosion(data, struct, border_value=1,
  3033. iterations=3, output=out)
  3034. assert_array_almost_equal(out, expected)
  3035. def test_binary_erosion31(self):
  3036. struct = [[0, 1, 0],
  3037. [1, 1, 1],
  3038. [0, 1, 0]]
  3039. expected = [[0, 0, 1, 0, 0, 0, 0],
  3040. [0, 1, 1, 1, 0, 0, 0],
  3041. [1, 1, 1, 1, 1, 0, 1],
  3042. [0, 1, 1, 1, 0, 0, 0],
  3043. [0, 0, 1, 0, 0, 0, 0],
  3044. [0, 0, 0, 0, 0, 0, 0],
  3045. [0, 0, 1, 0, 0, 0, 1]]
  3046. data = numpy.array([[0, 0, 0, 1, 0, 0, 0],
  3047. [0, 0, 1, 1, 1, 0, 0],
  3048. [0, 1, 1, 1, 1, 1, 0],
  3049. [1, 1, 1, 1, 1, 1, 1],
  3050. [0, 1, 1, 1, 1, 1, 0],
  3051. [0, 0, 1, 1, 1, 0, 0],
  3052. [0, 0, 0, 1, 0, 0, 0]], bool)
  3053. out = numpy.zeros(data.shape, bool)
  3054. ndimage.binary_erosion(data, struct, border_value=1,
  3055. iterations=1, output=out, origin=(-1, -1))
  3056. assert_array_almost_equal(out, expected)
  3057. def test_binary_erosion32(self):
  3058. struct = [[0, 1, 0],
  3059. [1, 1, 1],
  3060. [0, 1, 0]]
  3061. expected = [[0, 0, 0, 0, 0, 0, 0],
  3062. [0, 0, 0, 0, 0, 0, 0],
  3063. [0, 0, 0, 0, 0, 0, 0],
  3064. [0, 0, 0, 1, 0, 0, 0],
  3065. [0, 0, 0, 0, 0, 0, 0],
  3066. [0, 0, 0, 0, 0, 0, 0],
  3067. [0, 0, 0, 0, 0, 0, 0]]
  3068. data = numpy.array([[0, 0, 0, 0, 0, 0, 0],
  3069. [0, 0, 0, 1, 0, 0, 0],
  3070. [0, 0, 1, 1, 1, 0, 0],
  3071. [0, 1, 1, 1, 1, 1, 0],
  3072. [0, 0, 1, 1, 1, 0, 0],
  3073. [0, 0, 0, 1, 0, 0, 0],
  3074. [0, 0, 0, 0, 0, 0, 0]], bool)
  3075. out = ndimage.binary_erosion(data, struct,
  3076. border_value=1, iterations=2)
  3077. assert_array_almost_equal(out, expected)
  3078. def test_binary_erosion33(self):
  3079. struct = [[0, 1, 0],
  3080. [1, 1, 1],
  3081. [0, 1, 0]]
  3082. expected = [[0, 0, 0, 0, 0, 1, 1],
  3083. [0, 0, 0, 0, 0, 0, 1],
  3084. [0, 0, 0, 0, 0, 0, 0],
  3085. [0, 0, 0, 0, 0, 0, 0],
  3086. [0, 0, 0, 0, 0, 0, 0],
  3087. [0, 0, 0, 0, 0, 0, 0],
  3088. [0, 0, 0, 0, 0, 0, 0]]
  3089. mask = [[1, 1, 1, 1, 1, 0, 0],
  3090. [1, 1, 1, 1, 1, 1, 0],
  3091. [1, 1, 1, 1, 1, 1, 1],
  3092. [1, 1, 1, 1, 1, 1, 1],
  3093. [1, 1, 1, 1, 1, 1, 1],
  3094. [1, 1, 1, 1, 1, 1, 1],
  3095. [1, 1, 1, 1, 1, 1, 1]]
  3096. data = numpy.array([[0, 0, 0, 0, 0, 1, 1],
  3097. [0, 0, 0, 1, 0, 0, 1],
  3098. [0, 0, 1, 1, 1, 0, 0],
  3099. [0, 0, 1, 1, 1, 0, 0],
  3100. [0, 0, 1, 1, 1, 0, 0],
  3101. [0, 0, 0, 1, 0, 0, 0],
  3102. [0, 0, 0, 0, 0, 0, 0]], bool)
  3103. out = ndimage.binary_erosion(data, struct,
  3104. border_value=1, mask=mask, iterations=-1)
  3105. assert_array_almost_equal(out, expected)
  3106. def test_binary_erosion34(self):
  3107. struct = [[0, 1, 0],
  3108. [1, 1, 1],
  3109. [0, 1, 0]]
  3110. expected = [[0, 0, 0, 0, 0, 0, 0],
  3111. [0, 0, 0, 1, 0, 0, 0],
  3112. [0, 0, 0, 1, 0, 0, 0],
  3113. [0, 1, 1, 1, 1, 1, 0],
  3114. [0, 0, 0, 1, 0, 0, 0],
  3115. [0, 0, 0, 1, 0, 0, 0],
  3116. [0, 0, 0, 0, 0, 0, 0]]
  3117. mask = [[0, 0, 0, 0, 0, 0, 0],
  3118. [0, 0, 0, 0, 0, 0, 0],
  3119. [0, 0, 1, 1, 1, 0, 0],
  3120. [0, 0, 1, 0, 1, 0, 0],
  3121. [0, 0, 1, 1, 1, 0, 0],
  3122. [0, 0, 0, 0, 0, 0, 0],
  3123. [0, 0, 0, 0, 0, 0, 0]]
  3124. data = numpy.array([[0, 0, 0, 0, 0, 0, 0],
  3125. [0, 0, 0, 1, 0, 0, 0],
  3126. [0, 0, 1, 1, 1, 0, 0],
  3127. [0, 1, 1, 1, 1, 1, 0],
  3128. [0, 0, 1, 1, 1, 0, 0],
  3129. [0, 0, 0, 1, 0, 0, 0],
  3130. [0, 0, 0, 0, 0, 0, 0]], bool)
  3131. out = ndimage.binary_erosion(data, struct,
  3132. border_value=1, mask=mask)
  3133. assert_array_almost_equal(out, expected)
  3134. def test_binary_erosion35(self):
  3135. struct = [[0, 1, 0],
  3136. [1, 1, 1],
  3137. [0, 1, 0]]
  3138. mask = [[0, 0, 0, 0, 0, 0, 0],
  3139. [0, 0, 0, 0, 0, 0, 0],
  3140. [0, 0, 1, 1, 1, 0, 0],
  3141. [0, 0, 1, 0, 1, 0, 0],
  3142. [0, 0, 1, 1, 1, 0, 0],
  3143. [0, 0, 0, 0, 0, 0, 0],
  3144. [0, 0, 0, 0, 0, 0, 0]]
  3145. data = numpy.array([[0, 0, 0, 1, 0, 0, 0],
  3146. [0, 0, 1, 1, 1, 0, 0],
  3147. [0, 1, 1, 1, 1, 1, 0],
  3148. [1, 1, 1, 1, 1, 1, 1],
  3149. [0, 1, 1, 1, 1, 1, 0],
  3150. [0, 0, 1, 1, 1, 0, 0],
  3151. [0, 0, 0, 1, 0, 0, 0]], bool)
  3152. tmp = [[0, 0, 1, 0, 0, 0, 0],
  3153. [0, 1, 1, 1, 0, 0, 0],
  3154. [1, 1, 1, 1, 1, 0, 1],
  3155. [0, 1, 1, 1, 0, 0, 0],
  3156. [0, 0, 1, 0, 0, 0, 0],
  3157. [0, 0, 0, 0, 0, 0, 0],
  3158. [0, 0, 1, 0, 0, 0, 1]]
  3159. expected = numpy.logical_and(tmp, mask)
  3160. tmp = numpy.logical_and(data, numpy.logical_not(mask))
  3161. expected = numpy.logical_or(expected, tmp)
  3162. out = numpy.zeros(data.shape, bool)
  3163. ndimage.binary_erosion(data, struct, border_value=1,
  3164. iterations=1, output=out,
  3165. origin=(-1, -1), mask=mask)
  3166. assert_array_almost_equal(out, expected)
  3167. def test_binary_erosion36(self):
  3168. struct = [[0, 1, 0],
  3169. [1, 0, 1],
  3170. [0, 1, 0]]
  3171. mask = [[0, 0, 0, 0, 0, 0, 0, 0],
  3172. [0, 0, 0, 0, 0, 0, 0, 0],
  3173. [0, 0, 1, 1, 1, 0, 0, 0],
  3174. [0, 0, 1, 0, 1, 0, 0, 0],
  3175. [0, 0, 1, 1, 1, 0, 0, 0],
  3176. [0, 0, 1, 1, 1, 0, 0, 0],
  3177. [0, 0, 1, 1, 1, 0, 0, 0],
  3178. [0, 0, 0, 0, 0, 0, 0, 0]]
  3179. tmp = [[0, 0, 0, 0, 0, 0, 0, 0],
  3180. [0, 0, 0, 0, 0, 0, 0, 1],
  3181. [0, 0, 0, 0, 1, 0, 0, 1],
  3182. [0, 0, 1, 0, 0, 0, 0, 0],
  3183. [0, 1, 0, 0, 1, 0, 0, 0],
  3184. [0, 0, 0, 0, 0, 0, 0, 0],
  3185. [0, 0, 0, 0, 0, 0, 0, 0],
  3186. [0, 0, 0, 0, 0, 0, 0, 1]]
  3187. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  3188. [0, 1, 0, 0, 0, 0, 0, 0],
  3189. [0, 0, 0, 0, 0, 1, 1, 1],
  3190. [0, 0, 1, 1, 1, 0, 1, 1],
  3191. [0, 0, 1, 0, 1, 1, 0, 0],
  3192. [0, 1, 0, 1, 1, 1, 1, 0],
  3193. [0, 1, 1, 0, 0, 1, 1, 0],
  3194. [0, 0, 0, 0, 0, 0, 0, 0]])
  3195. expected = numpy.logical_and(tmp, mask)
  3196. tmp = numpy.logical_and(data, numpy.logical_not(mask))
  3197. expected = numpy.logical_or(expected, tmp)
  3198. out = ndimage.binary_erosion(data, struct, mask=mask,
  3199. border_value=1, origin=(-1, -1))
  3200. assert_array_almost_equal(out, expected)
  3201. def test_binary_erosion37(self):
  3202. a = numpy.array([[1, 0, 1],
  3203. [0, 1, 0],
  3204. [1, 0, 1]], dtype=bool)
  3205. b = numpy.zeros_like(a)
  3206. out = ndimage.binary_erosion(a, structure=a, output=b, iterations=0,
  3207. border_value=True, brute_force=True)
  3208. assert_(out is b)
  3209. assert_array_equal(
  3210. ndimage.binary_erosion(a, structure=a, iterations=0,
  3211. border_value=True),
  3212. b)
  3213. def test_binary_dilation01(self):
  3214. for type_ in self.types:
  3215. data = numpy.ones([], type_)
  3216. out = ndimage.binary_dilation(data)
  3217. assert_array_almost_equal(out, 1)
  3218. def test_binary_dilation02(self):
  3219. for type_ in self.types:
  3220. data = numpy.zeros([], type_)
  3221. out = ndimage.binary_dilation(data)
  3222. assert_array_almost_equal(out, 0)
  3223. def test_binary_dilation03(self):
  3224. for type_ in self.types:
  3225. data = numpy.ones([1], type_)
  3226. out = ndimage.binary_dilation(data)
  3227. assert_array_almost_equal(out, [1])
  3228. def test_binary_dilation04(self):
  3229. for type_ in self.types:
  3230. data = numpy.zeros([1], type_)
  3231. out = ndimage.binary_dilation(data)
  3232. assert_array_almost_equal(out, [0])
  3233. def test_binary_dilation05(self):
  3234. for type_ in self.types:
  3235. data = numpy.ones([3], type_)
  3236. out = ndimage.binary_dilation(data)
  3237. assert_array_almost_equal(out, [1, 1, 1])
  3238. def test_binary_dilation06(self):
  3239. for type_ in self.types:
  3240. data = numpy.zeros([3], type_)
  3241. out = ndimage.binary_dilation(data)
  3242. assert_array_almost_equal(out, [0, 0, 0])
  3243. def test_binary_dilation07(self):
  3244. for type_ in self.types:
  3245. data = numpy.zeros([3], type_)
  3246. data[1] = 1
  3247. out = ndimage.binary_dilation(data)
  3248. assert_array_almost_equal(out, [1, 1, 1])
  3249. def test_binary_dilation08(self):
  3250. for type_ in self.types:
  3251. data = numpy.zeros([5], type_)
  3252. data[1] = 1
  3253. data[3] = 1
  3254. out = ndimage.binary_dilation(data)
  3255. assert_array_almost_equal(out, [1, 1, 1, 1, 1])
  3256. def test_binary_dilation09(self):
  3257. for type_ in self.types:
  3258. data = numpy.zeros([5], type_)
  3259. data[1] = 1
  3260. out = ndimage.binary_dilation(data)
  3261. assert_array_almost_equal(out, [1, 1, 1, 0, 0])
  3262. def test_binary_dilation10(self):
  3263. for type_ in self.types:
  3264. data = numpy.zeros([5], type_)
  3265. data[1] = 1
  3266. out = ndimage.binary_dilation(data, origin=-1)
  3267. assert_array_almost_equal(out, [0, 1, 1, 1, 0])
  3268. def test_binary_dilation11(self):
  3269. for type_ in self.types:
  3270. data = numpy.zeros([5], type_)
  3271. data[1] = 1
  3272. out = ndimage.binary_dilation(data, origin=1)
  3273. assert_array_almost_equal(out, [1, 1, 0, 0, 0])
  3274. def test_binary_dilation12(self):
  3275. for type_ in self.types:
  3276. data = numpy.zeros([5], type_)
  3277. data[1] = 1
  3278. struct = [1, 0, 1]
  3279. out = ndimage.binary_dilation(data, struct)
  3280. assert_array_almost_equal(out, [1, 0, 1, 0, 0])
  3281. def test_binary_dilation13(self):
  3282. for type_ in self.types:
  3283. data = numpy.zeros([5], type_)
  3284. data[1] = 1
  3285. struct = [1, 0, 1]
  3286. out = ndimage.binary_dilation(data, struct, border_value=1)
  3287. assert_array_almost_equal(out, [1, 0, 1, 0, 1])
  3288. def test_binary_dilation14(self):
  3289. for type_ in self.types:
  3290. data = numpy.zeros([5], type_)
  3291. data[1] = 1
  3292. struct = [1, 0, 1]
  3293. out = ndimage.binary_dilation(data, struct, origin=-1)
  3294. assert_array_almost_equal(out, [0, 1, 0, 1, 0])
  3295. def test_binary_dilation15(self):
  3296. for type_ in self.types:
  3297. data = numpy.zeros([5], type_)
  3298. data[1] = 1
  3299. struct = [1, 0, 1]
  3300. out = ndimage.binary_dilation(data, struct,
  3301. origin=-1, border_value=1)
  3302. assert_array_almost_equal(out, [1, 1, 0, 1, 0])
  3303. def test_binary_dilation16(self):
  3304. for type_ in self.types:
  3305. data = numpy.ones([1, 1], type_)
  3306. out = ndimage.binary_dilation(data)
  3307. assert_array_almost_equal(out, [[1]])
  3308. def test_binary_dilation17(self):
  3309. for type_ in self.types:
  3310. data = numpy.zeros([1, 1], type_)
  3311. out = ndimage.binary_dilation(data)
  3312. assert_array_almost_equal(out, [[0]])
  3313. def test_binary_dilation18(self):
  3314. for type_ in self.types:
  3315. data = numpy.ones([1, 3], type_)
  3316. out = ndimage.binary_dilation(data)
  3317. assert_array_almost_equal(out, [[1, 1, 1]])
  3318. def test_binary_dilation19(self):
  3319. for type_ in self.types:
  3320. data = numpy.ones([3, 3], type_)
  3321. out = ndimage.binary_dilation(data)
  3322. assert_array_almost_equal(out, [[1, 1, 1],
  3323. [1, 1, 1],
  3324. [1, 1, 1]])
  3325. def test_binary_dilation20(self):
  3326. for type_ in self.types:
  3327. data = numpy.zeros([3, 3], type_)
  3328. data[1, 1] = 1
  3329. out = ndimage.binary_dilation(data)
  3330. assert_array_almost_equal(out, [[0, 1, 0],
  3331. [1, 1, 1],
  3332. [0, 1, 0]])
  3333. def test_binary_dilation21(self):
  3334. struct = ndimage.generate_binary_structure(2, 2)
  3335. for type_ in self.types:
  3336. data = numpy.zeros([3, 3], type_)
  3337. data[1, 1] = 1
  3338. out = ndimage.binary_dilation(data, struct)
  3339. assert_array_almost_equal(out, [[1, 1, 1],
  3340. [1, 1, 1],
  3341. [1, 1, 1]])
  3342. def test_binary_dilation22(self):
  3343. expected = [[0, 1, 0, 0, 0, 0, 0, 0],
  3344. [1, 1, 1, 0, 0, 0, 0, 0],
  3345. [0, 1, 0, 0, 0, 1, 0, 0],
  3346. [0, 0, 0, 1, 1, 1, 1, 0],
  3347. [0, 0, 1, 1, 1, 1, 0, 0],
  3348. [0, 1, 1, 1, 1, 1, 1, 0],
  3349. [0, 0, 1, 0, 0, 1, 0, 0],
  3350. [0, 0, 0, 0, 0, 0, 0, 0]]
  3351. for type_ in self.types:
  3352. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  3353. [0, 1, 0, 0, 0, 0, 0, 0],
  3354. [0, 0, 0, 0, 0, 0, 0, 0],
  3355. [0, 0, 0, 0, 0, 1, 0, 0],
  3356. [0, 0, 0, 1, 1, 0, 0, 0],
  3357. [0, 0, 1, 0, 0, 1, 0, 0],
  3358. [0, 0, 0, 0, 0, 0, 0, 0],
  3359. [0, 0, 0, 0, 0, 0, 0, 0]], type_)
  3360. out = ndimage.binary_dilation(data)
  3361. assert_array_almost_equal(out, expected)
  3362. def test_binary_dilation23(self):
  3363. expected = [[1, 1, 1, 1, 1, 1, 1, 1],
  3364. [1, 1, 1, 0, 0, 0, 0, 1],
  3365. [1, 1, 0, 0, 0, 1, 0, 1],
  3366. [1, 0, 0, 1, 1, 1, 1, 1],
  3367. [1, 0, 1, 1, 1, 1, 0, 1],
  3368. [1, 1, 1, 1, 1, 1, 1, 1],
  3369. [1, 0, 1, 0, 0, 1, 0, 1],
  3370. [1, 1, 1, 1, 1, 1, 1, 1]]
  3371. for type_ in self.types:
  3372. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  3373. [0, 1, 0, 0, 0, 0, 0, 0],
  3374. [0, 0, 0, 0, 0, 0, 0, 0],
  3375. [0, 0, 0, 0, 0, 1, 0, 0],
  3376. [0, 0, 0, 1, 1, 0, 0, 0],
  3377. [0, 0, 1, 0, 0, 1, 0, 0],
  3378. [0, 0, 0, 0, 0, 0, 0, 0],
  3379. [0, 0, 0, 0, 0, 0, 0, 0]], type_)
  3380. out = ndimage.binary_dilation(data, border_value=1)
  3381. assert_array_almost_equal(out, expected)
  3382. def test_binary_dilation24(self):
  3383. expected = [[1, 1, 0, 0, 0, 0, 0, 0],
  3384. [1, 0, 0, 0, 1, 0, 0, 0],
  3385. [0, 0, 1, 1, 1, 1, 0, 0],
  3386. [0, 1, 1, 1, 1, 0, 0, 0],
  3387. [1, 1, 1, 1, 1, 1, 0, 0],
  3388. [0, 1, 0, 0, 1, 0, 0, 0],
  3389. [0, 0, 0, 0, 0, 0, 0, 0],
  3390. [0, 0, 0, 0, 0, 0, 0, 0]]
  3391. for type_ in self.types:
  3392. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  3393. [0, 1, 0, 0, 0, 0, 0, 0],
  3394. [0, 0, 0, 0, 0, 0, 0, 0],
  3395. [0, 0, 0, 0, 0, 1, 0, 0],
  3396. [0, 0, 0, 1, 1, 0, 0, 0],
  3397. [0, 0, 1, 0, 0, 1, 0, 0],
  3398. [0, 0, 0, 0, 0, 0, 0, 0],
  3399. [0, 0, 0, 0, 0, 0, 0, 0]], type_)
  3400. out = ndimage.binary_dilation(data, origin=(1, 1))
  3401. assert_array_almost_equal(out, expected)
  3402. def test_binary_dilation25(self):
  3403. expected = [[1, 1, 0, 0, 0, 0, 1, 1],
  3404. [1, 0, 0, 0, 1, 0, 1, 1],
  3405. [0, 0, 1, 1, 1, 1, 1, 1],
  3406. [0, 1, 1, 1, 1, 0, 1, 1],
  3407. [1, 1, 1, 1, 1, 1, 1, 1],
  3408. [0, 1, 0, 0, 1, 0, 1, 1],
  3409. [1, 1, 1, 1, 1, 1, 1, 1],
  3410. [1, 1, 1, 1, 1, 1, 1, 1]]
  3411. for type_ in self.types:
  3412. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  3413. [0, 1, 0, 0, 0, 0, 0, 0],
  3414. [0, 0, 0, 0, 0, 0, 0, 0],
  3415. [0, 0, 0, 0, 0, 1, 0, 0],
  3416. [0, 0, 0, 1, 1, 0, 0, 0],
  3417. [0, 0, 1, 0, 0, 1, 0, 0],
  3418. [0, 0, 0, 0, 0, 0, 0, 0],
  3419. [0, 0, 0, 0, 0, 0, 0, 0]], type_)
  3420. out = ndimage.binary_dilation(data, origin=(1, 1), border_value=1)
  3421. assert_array_almost_equal(out, expected)
  3422. def test_binary_dilation26(self):
  3423. struct = ndimage.generate_binary_structure(2, 2)
  3424. expected = [[1, 1, 1, 0, 0, 0, 0, 0],
  3425. [1, 1, 1, 0, 0, 0, 0, 0],
  3426. [1, 1, 1, 0, 1, 1, 1, 0],
  3427. [0, 0, 1, 1, 1, 1, 1, 0],
  3428. [0, 1, 1, 1, 1, 1, 1, 0],
  3429. [0, 1, 1, 1, 1, 1, 1, 0],
  3430. [0, 1, 1, 1, 1, 1, 1, 0],
  3431. [0, 0, 0, 0, 0, 0, 0, 0]]
  3432. for type_ in self.types:
  3433. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  3434. [0, 1, 0, 0, 0, 0, 0, 0],
  3435. [0, 0, 0, 0, 0, 0, 0, 0],
  3436. [0, 0, 0, 0, 0, 1, 0, 0],
  3437. [0, 0, 0, 1, 1, 0, 0, 0],
  3438. [0, 0, 1, 0, 0, 1, 0, 0],
  3439. [0, 0, 0, 0, 0, 0, 0, 0],
  3440. [0, 0, 0, 0, 0, 0, 0, 0]], type_)
  3441. out = ndimage.binary_dilation(data, struct)
  3442. assert_array_almost_equal(out, expected)
  3443. def test_binary_dilation27(self):
  3444. struct = [[0, 1],
  3445. [1, 1]]
  3446. expected = [[0, 1, 0, 0, 0, 0, 0, 0],
  3447. [1, 1, 0, 0, 0, 0, 0, 0],
  3448. [0, 0, 0, 0, 0, 1, 0, 0],
  3449. [0, 0, 0, 1, 1, 1, 0, 0],
  3450. [0, 0, 1, 1, 1, 1, 0, 0],
  3451. [0, 1, 1, 0, 1, 1, 0, 0],
  3452. [0, 0, 0, 0, 0, 0, 0, 0],
  3453. [0, 0, 0, 0, 0, 0, 0, 0]]
  3454. for type_ in self.types:
  3455. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  3456. [0, 1, 0, 0, 0, 0, 0, 0],
  3457. [0, 0, 0, 0, 0, 0, 0, 0],
  3458. [0, 0, 0, 0, 0, 1, 0, 0],
  3459. [0, 0, 0, 1, 1, 0, 0, 0],
  3460. [0, 0, 1, 0, 0, 1, 0, 0],
  3461. [0, 0, 0, 0, 0, 0, 0, 0],
  3462. [0, 0, 0, 0, 0, 0, 0, 0]], type_)
  3463. out = ndimage.binary_dilation(data, struct)
  3464. assert_array_almost_equal(out, expected)
  3465. def test_binary_dilation28(self):
  3466. expected = [[1, 1, 1, 1],
  3467. [1, 0, 0, 1],
  3468. [1, 0, 0, 1],
  3469. [1, 1, 1, 1]]
  3470. for type_ in self.types:
  3471. data = numpy.array([[0, 0, 0, 0],
  3472. [0, 0, 0, 0],
  3473. [0, 0, 0, 0],
  3474. [0, 0, 0, 0]], type_)
  3475. out = ndimage.binary_dilation(data, border_value=1)
  3476. assert_array_almost_equal(out, expected)
  3477. def test_binary_dilation29(self):
  3478. struct = [[0, 1],
  3479. [1, 1]]
  3480. expected = [[0, 0, 0, 0, 0],
  3481. [0, 0, 0, 1, 0],
  3482. [0, 0, 1, 1, 0],
  3483. [0, 1, 1, 1, 0],
  3484. [0, 0, 0, 0, 0]]
  3485. data = numpy.array([[0, 0, 0, 0, 0],
  3486. [0, 0, 0, 0, 0],
  3487. [0, 0, 0, 0, 0],
  3488. [0, 0, 0, 1, 0],
  3489. [0, 0, 0, 0, 0]], bool)
  3490. out = ndimage.binary_dilation(data, struct, iterations=2)
  3491. assert_array_almost_equal(out, expected)
  3492. def test_binary_dilation30(self):
  3493. struct = [[0, 1],
  3494. [1, 1]]
  3495. expected = [[0, 0, 0, 0, 0],
  3496. [0, 0, 0, 1, 0],
  3497. [0, 0, 1, 1, 0],
  3498. [0, 1, 1, 1, 0],
  3499. [0, 0, 0, 0, 0]]
  3500. data = numpy.array([[0, 0, 0, 0, 0],
  3501. [0, 0, 0, 0, 0],
  3502. [0, 0, 0, 0, 0],
  3503. [0, 0, 0, 1, 0],
  3504. [0, 0, 0, 0, 0]], bool)
  3505. out = numpy.zeros(data.shape, bool)
  3506. ndimage.binary_dilation(data, struct, iterations=2, output=out)
  3507. assert_array_almost_equal(out, expected)
  3508. def test_binary_dilation31(self):
  3509. struct = [[0, 1],
  3510. [1, 1]]
  3511. expected = [[0, 0, 0, 1, 0],
  3512. [0, 0, 1, 1, 0],
  3513. [0, 1, 1, 1, 0],
  3514. [1, 1, 1, 1, 0],
  3515. [0, 0, 0, 0, 0]]
  3516. data = numpy.array([[0, 0, 0, 0, 0],
  3517. [0, 0, 0, 0, 0],
  3518. [0, 0, 0, 0, 0],
  3519. [0, 0, 0, 1, 0],
  3520. [0, 0, 0, 0, 0]], bool)
  3521. out = ndimage.binary_dilation(data, struct, iterations=3)
  3522. assert_array_almost_equal(out, expected)
  3523. def test_binary_dilation32(self):
  3524. struct = [[0, 1],
  3525. [1, 1]]
  3526. expected = [[0, 0, 0, 1, 0],
  3527. [0, 0, 1, 1, 0],
  3528. [0, 1, 1, 1, 0],
  3529. [1, 1, 1, 1, 0],
  3530. [0, 0, 0, 0, 0]]
  3531. data = numpy.array([[0, 0, 0, 0, 0],
  3532. [0, 0, 0, 0, 0],
  3533. [0, 0, 0, 0, 0],
  3534. [0, 0, 0, 1, 0],
  3535. [0, 0, 0, 0, 0]], bool)
  3536. out = numpy.zeros(data.shape, bool)
  3537. ndimage.binary_dilation(data, struct, iterations=3, output=out)
  3538. assert_array_almost_equal(out, expected)
  3539. def test_binary_dilation33(self):
  3540. struct = [[0, 1, 0],
  3541. [1, 1, 1],
  3542. [0, 1, 0]]
  3543. expected = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
  3544. [0, 0, 0, 0, 0, 0, 0, 0],
  3545. [0, 0, 0, 0, 0, 0, 0, 0],
  3546. [0, 0, 0, 0, 1, 1, 0, 0],
  3547. [0, 0, 1, 1, 1, 0, 0, 0],
  3548. [0, 1, 1, 0, 1, 1, 0, 0],
  3549. [0, 0, 0, 0, 0, 0, 0, 0],
  3550. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  3551. mask = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
  3552. [0, 0, 0, 0, 0, 0, 0, 0],
  3553. [0, 0, 0, 0, 0, 0, 1, 0],
  3554. [0, 0, 0, 0, 1, 1, 0, 0],
  3555. [0, 0, 1, 1, 1, 0, 0, 0],
  3556. [0, 1, 1, 0, 1, 1, 0, 0],
  3557. [0, 0, 0, 0, 0, 0, 0, 0],
  3558. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  3559. data = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
  3560. [0, 0, 0, 0, 0, 0, 0, 0],
  3561. [0, 0, 0, 0, 0, 0, 0, 0],
  3562. [0, 0, 0, 0, 0, 0, 0, 0],
  3563. [0, 0, 0, 0, 0, 0, 0, 0],
  3564. [0, 1, 0, 0, 0, 0, 0, 0],
  3565. [0, 0, 0, 0, 0, 0, 0, 0],
  3566. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  3567. out = ndimage.binary_dilation(data, struct, iterations=-1,
  3568. mask=mask, border_value=0)
  3569. assert_array_almost_equal(out, expected)
  3570. def test_binary_dilation34(self):
  3571. struct = [[0, 1, 0],
  3572. [1, 1, 1],
  3573. [0, 1, 0]]
  3574. expected = [[0, 1, 0, 0, 0, 0, 0, 0],
  3575. [0, 1, 1, 0, 0, 0, 0, 0],
  3576. [0, 0, 1, 0, 0, 0, 0, 0],
  3577. [0, 0, 0, 0, 0, 0, 0, 0],
  3578. [0, 0, 0, 0, 0, 0, 0, 0],
  3579. [0, 0, 0, 0, 0, 0, 0, 0],
  3580. [0, 0, 0, 0, 0, 0, 0, 0],
  3581. [0, 0, 0, 0, 0, 0, 0, 0]]
  3582. mask = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
  3583. [0, 1, 1, 0, 0, 0, 0, 0],
  3584. [0, 0, 1, 0, 0, 0, 0, 0],
  3585. [0, 0, 0, 0, 0, 1, 0, 0],
  3586. [0, 0, 0, 1, 1, 0, 0, 0],
  3587. [0, 0, 1, 0, 0, 1, 0, 0],
  3588. [0, 0, 0, 0, 0, 0, 0, 0],
  3589. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  3590. data = numpy.zeros(mask.shape, bool)
  3591. out = ndimage.binary_dilation(data, struct, iterations=-1,
  3592. mask=mask, border_value=1)
  3593. assert_array_almost_equal(out, expected)
  3594. def test_binary_dilation35(self):
  3595. tmp = [[1, 1, 0, 0, 0, 0, 1, 1],
  3596. [1, 0, 0, 0, 1, 0, 1, 1],
  3597. [0, 0, 1, 1, 1, 1, 1, 1],
  3598. [0, 1, 1, 1, 1, 0, 1, 1],
  3599. [1, 1, 1, 1, 1, 1, 1, 1],
  3600. [0, 1, 0, 0, 1, 0, 1, 1],
  3601. [1, 1, 1, 1, 1, 1, 1, 1],
  3602. [1, 1, 1, 1, 1, 1, 1, 1]]
  3603. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  3604. [0, 1, 0, 0, 0, 0, 0, 0],
  3605. [0, 0, 0, 0, 0, 0, 0, 0],
  3606. [0, 0, 0, 0, 0, 1, 0, 0],
  3607. [0, 0, 0, 1, 1, 0, 0, 0],
  3608. [0, 0, 1, 0, 0, 1, 0, 0],
  3609. [0, 0, 0, 0, 0, 0, 0, 0],
  3610. [0, 0, 0, 0, 0, 0, 0, 0]])
  3611. mask = [[0, 0, 0, 0, 0, 0, 0, 0],
  3612. [0, 0, 0, 0, 0, 0, 0, 0],
  3613. [0, 0, 0, 0, 0, 0, 0, 0],
  3614. [0, 0, 1, 1, 1, 1, 0, 0],
  3615. [0, 0, 1, 1, 1, 1, 0, 0],
  3616. [0, 0, 1, 1, 1, 1, 0, 0],
  3617. [0, 0, 0, 0, 0, 0, 0, 0],
  3618. [0, 0, 0, 0, 0, 0, 0, 0]]
  3619. expected = numpy.logical_and(tmp, mask)
  3620. tmp = numpy.logical_and(data, numpy.logical_not(mask))
  3621. expected = numpy.logical_or(expected, tmp)
  3622. for type_ in self.types:
  3623. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  3624. [0, 1, 0, 0, 0, 0, 0, 0],
  3625. [0, 0, 0, 0, 0, 0, 0, 0],
  3626. [0, 0, 0, 0, 0, 1, 0, 0],
  3627. [0, 0, 0, 1, 1, 0, 0, 0],
  3628. [0, 0, 1, 0, 0, 1, 0, 0],
  3629. [0, 0, 0, 0, 0, 0, 0, 0],
  3630. [0, 0, 0, 0, 0, 0, 0, 0]], type_)
  3631. out = ndimage.binary_dilation(data, mask=mask,
  3632. origin=(1, 1), border_value=1)
  3633. assert_array_almost_equal(out, expected)
  3634. def test_binary_propagation01(self):
  3635. struct = [[0, 1, 0],
  3636. [1, 1, 1],
  3637. [0, 1, 0]]
  3638. expected = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
  3639. [0, 0, 0, 0, 0, 0, 0, 0],
  3640. [0, 0, 0, 0, 0, 0, 0, 0],
  3641. [0, 0, 0, 0, 1, 1, 0, 0],
  3642. [0, 0, 1, 1, 1, 0, 0, 0],
  3643. [0, 1, 1, 0, 1, 1, 0, 0],
  3644. [0, 0, 0, 0, 0, 0, 0, 0],
  3645. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  3646. mask = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
  3647. [0, 0, 0, 0, 0, 0, 0, 0],
  3648. [0, 0, 0, 0, 0, 0, 1, 0],
  3649. [0, 0, 0, 0, 1, 1, 0, 0],
  3650. [0, 0, 1, 1, 1, 0, 0, 0],
  3651. [0, 1, 1, 0, 1, 1, 0, 0],
  3652. [0, 0, 0, 0, 0, 0, 0, 0],
  3653. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  3654. data = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
  3655. [0, 0, 0, 0, 0, 0, 0, 0],
  3656. [0, 0, 0, 0, 0, 0, 0, 0],
  3657. [0, 0, 0, 0, 0, 0, 0, 0],
  3658. [0, 0, 0, 0, 0, 0, 0, 0],
  3659. [0, 1, 0, 0, 0, 0, 0, 0],
  3660. [0, 0, 0, 0, 0, 0, 0, 0],
  3661. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  3662. out = ndimage.binary_propagation(data, struct,
  3663. mask=mask, border_value=0)
  3664. assert_array_almost_equal(out, expected)
  3665. def test_binary_propagation02(self):
  3666. struct = [[0, 1, 0],
  3667. [1, 1, 1],
  3668. [0, 1, 0]]
  3669. expected = [[0, 1, 0, 0, 0, 0, 0, 0],
  3670. [0, 1, 1, 0, 0, 0, 0, 0],
  3671. [0, 0, 1, 0, 0, 0, 0, 0],
  3672. [0, 0, 0, 0, 0, 0, 0, 0],
  3673. [0, 0, 0, 0, 0, 0, 0, 0],
  3674. [0, 0, 0, 0, 0, 0, 0, 0],
  3675. [0, 0, 0, 0, 0, 0, 0, 0],
  3676. [0, 0, 0, 0, 0, 0, 0, 0]]
  3677. mask = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
  3678. [0, 1, 1, 0, 0, 0, 0, 0],
  3679. [0, 0, 1, 0, 0, 0, 0, 0],
  3680. [0, 0, 0, 0, 0, 1, 0, 0],
  3681. [0, 0, 0, 1, 1, 0, 0, 0],
  3682. [0, 0, 1, 0, 0, 1, 0, 0],
  3683. [0, 0, 0, 0, 0, 0, 0, 0],
  3684. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  3685. data = numpy.zeros(mask.shape, bool)
  3686. out = ndimage.binary_propagation(data, struct,
  3687. mask=mask, border_value=1)
  3688. assert_array_almost_equal(out, expected)
  3689. def test_binary_opening01(self):
  3690. expected = [[0, 1, 0, 0, 0, 0, 0, 0],
  3691. [1, 1, 1, 0, 0, 0, 0, 0],
  3692. [0, 1, 0, 0, 0, 1, 0, 0],
  3693. [0, 0, 0, 0, 1, 1, 1, 0],
  3694. [0, 0, 1, 0, 0, 1, 0, 0],
  3695. [0, 1, 1, 1, 1, 1, 1, 0],
  3696. [0, 0, 1, 0, 0, 1, 0, 0],
  3697. [0, 0, 0, 0, 0, 0, 0, 0]]
  3698. for type_ in self.types:
  3699. data = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
  3700. [1, 1, 1, 0, 0, 0, 0, 0],
  3701. [0, 1, 0, 0, 0, 1, 0, 0],
  3702. [0, 0, 0, 1, 1, 1, 1, 0],
  3703. [0, 0, 1, 1, 0, 1, 0, 0],
  3704. [0, 1, 1, 1, 1, 1, 1, 0],
  3705. [0, 0, 1, 0, 0, 1, 0, 0],
  3706. [0, 0, 0, 0, 0, 0, 0, 0]], type_)
  3707. out = ndimage.binary_opening(data)
  3708. assert_array_almost_equal(out, expected)
  3709. def test_binary_opening02(self):
  3710. struct = ndimage.generate_binary_structure(2, 2)
  3711. expected = [[1, 1, 1, 0, 0, 0, 0, 0],
  3712. [1, 1, 1, 0, 0, 0, 0, 0],
  3713. [1, 1, 1, 0, 0, 0, 0, 0],
  3714. [0, 0, 0, 0, 0, 0, 0, 0],
  3715. [0, 1, 1, 1, 0, 0, 0, 0],
  3716. [0, 1, 1, 1, 0, 0, 0, 0],
  3717. [0, 1, 1, 1, 0, 0, 0, 0],
  3718. [0, 0, 0, 0, 0, 0, 0, 0]]
  3719. for type_ in self.types:
  3720. data = numpy.array([[1, 1, 1, 0, 0, 0, 0, 0],
  3721. [1, 1, 1, 0, 0, 0, 0, 0],
  3722. [1, 1, 1, 1, 1, 1, 1, 0],
  3723. [0, 0, 1, 1, 1, 1, 1, 0],
  3724. [0, 1, 1, 1, 0, 1, 1, 0],
  3725. [0, 1, 1, 1, 1, 1, 1, 0],
  3726. [0, 1, 1, 1, 1, 1, 1, 0],
  3727. [0, 0, 0, 0, 0, 0, 0, 0]], type_)
  3728. out = ndimage.binary_opening(data, struct)
  3729. assert_array_almost_equal(out, expected)
  3730. def test_binary_closing01(self):
  3731. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  3732. [0, 1, 1, 0, 0, 0, 0, 0],
  3733. [0, 1, 1, 1, 0, 1, 0, 0],
  3734. [0, 0, 1, 1, 1, 1, 1, 0],
  3735. [0, 0, 1, 1, 1, 1, 0, 0],
  3736. [0, 1, 1, 1, 1, 1, 1, 0],
  3737. [0, 0, 1, 0, 0, 1, 0, 0],
  3738. [0, 0, 0, 0, 0, 0, 0, 0]]
  3739. for type_ in self.types:
  3740. data = numpy.array([[0, 1, 0, 0, 0, 0, 0, 0],
  3741. [1, 1, 1, 0, 0, 0, 0, 0],
  3742. [0, 1, 0, 0, 0, 1, 0, 0],
  3743. [0, 0, 0, 1, 1, 1, 1, 0],
  3744. [0, 0, 1, 1, 0, 1, 0, 0],
  3745. [0, 1, 1, 1, 1, 1, 1, 0],
  3746. [0, 0, 1, 0, 0, 1, 0, 0],
  3747. [0, 0, 0, 0, 0, 0, 0, 0]], type_)
  3748. out = ndimage.binary_closing(data)
  3749. assert_array_almost_equal(out, expected)
  3750. def test_binary_closing02(self):
  3751. struct = ndimage.generate_binary_structure(2, 2)
  3752. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  3753. [0, 1, 1, 0, 0, 0, 0, 0],
  3754. [0, 1, 1, 1, 1, 1, 1, 0],
  3755. [0, 1, 1, 1, 1, 1, 1, 0],
  3756. [0, 1, 1, 1, 1, 1, 1, 0],
  3757. [0, 1, 1, 1, 1, 1, 1, 0],
  3758. [0, 1, 1, 1, 1, 1, 1, 0],
  3759. [0, 0, 0, 0, 0, 0, 0, 0]]
  3760. for type_ in self.types:
  3761. data = numpy.array([[1, 1, 1, 0, 0, 0, 0, 0],
  3762. [1, 1, 1, 0, 0, 0, 0, 0],
  3763. [1, 1, 1, 1, 1, 1, 1, 0],
  3764. [0, 0, 1, 1, 1, 1, 1, 0],
  3765. [0, 1, 1, 1, 0, 1, 1, 0],
  3766. [0, 1, 1, 1, 1, 1, 1, 0],
  3767. [0, 1, 1, 1, 1, 1, 1, 0],
  3768. [0, 0, 0, 0, 0, 0, 0, 0]], type_)
  3769. out = ndimage.binary_closing(data, struct)
  3770. assert_array_almost_equal(out, expected)
  3771. def test_binary_fill_holes01(self):
  3772. expected = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  3773. [0, 0, 1, 1, 1, 1, 0, 0],
  3774. [0, 0, 1, 1, 1, 1, 0, 0],
  3775. [0, 0, 1, 1, 1, 1, 0, 0],
  3776. [0, 0, 1, 1, 1, 1, 0, 0],
  3777. [0, 0, 1, 1, 1, 1, 0, 0],
  3778. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  3779. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  3780. [0, 0, 1, 1, 1, 1, 0, 0],
  3781. [0, 0, 1, 0, 0, 1, 0, 0],
  3782. [0, 0, 1, 0, 0, 1, 0, 0],
  3783. [0, 0, 1, 0, 0, 1, 0, 0],
  3784. [0, 0, 1, 1, 1, 1, 0, 0],
  3785. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  3786. out = ndimage.binary_fill_holes(data)
  3787. assert_array_almost_equal(out, expected)
  3788. def test_binary_fill_holes02(self):
  3789. expected = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  3790. [0, 0, 0, 1, 1, 0, 0, 0],
  3791. [0, 0, 1, 1, 1, 1, 0, 0],
  3792. [0, 0, 1, 1, 1, 1, 0, 0],
  3793. [0, 0, 1, 1, 1, 1, 0, 0],
  3794. [0, 0, 0, 1, 1, 0, 0, 0],
  3795. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  3796. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  3797. [0, 0, 0, 1, 1, 0, 0, 0],
  3798. [0, 0, 1, 0, 0, 1, 0, 0],
  3799. [0, 0, 1, 0, 0, 1, 0, 0],
  3800. [0, 0, 1, 0, 0, 1, 0, 0],
  3801. [0, 0, 0, 1, 1, 0, 0, 0],
  3802. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  3803. out = ndimage.binary_fill_holes(data)
  3804. assert_array_almost_equal(out, expected)
  3805. def test_binary_fill_holes03(self):
  3806. expected = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  3807. [0, 0, 1, 0, 0, 0, 0, 0],
  3808. [0, 1, 1, 1, 0, 1, 1, 1],
  3809. [0, 1, 1, 1, 0, 1, 1, 1],
  3810. [0, 1, 1, 1, 0, 1, 1, 1],
  3811. [0, 0, 1, 0, 0, 1, 1, 1],
  3812. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  3813. data = numpy.array([[0, 0, 0, 0, 0, 0, 0, 0],
  3814. [0, 0, 1, 0, 0, 0, 0, 0],
  3815. [0, 1, 0, 1, 0, 1, 1, 1],
  3816. [0, 1, 0, 1, 0, 1, 0, 1],
  3817. [0, 1, 0, 1, 0, 1, 0, 1],
  3818. [0, 0, 1, 0, 0, 1, 1, 1],
  3819. [0, 0, 0, 0, 0, 0, 0, 0]], bool)
  3820. out = ndimage.binary_fill_holes(data)
  3821. assert_array_almost_equal(out, expected)
  3822. def test_grey_erosion01(self):
  3823. array = numpy.array([[3, 2, 5, 1, 4],
  3824. [7, 6, 9, 3, 5],
  3825. [5, 8, 3, 7, 1]])
  3826. footprint = [[1, 0, 1], [1, 1, 0]]
  3827. output = ndimage.grey_erosion(array, footprint=footprint)
  3828. assert_array_almost_equal([[2, 2, 1, 1, 1],
  3829. [2, 3, 1, 3, 1],
  3830. [5, 5, 3, 3, 1]], output)
  3831. def test_grey_erosion02(self):
  3832. array = numpy.array([[3, 2, 5, 1, 4],
  3833. [7, 6, 9, 3, 5],
  3834. [5, 8, 3, 7, 1]])
  3835. footprint = [[1, 0, 1], [1, 1, 0]]
  3836. structure = [[0, 0, 0], [0, 0, 0]]
  3837. output = ndimage.grey_erosion(array, footprint=footprint,
  3838. structure=structure)
  3839. assert_array_almost_equal([[2, 2, 1, 1, 1],
  3840. [2, 3, 1, 3, 1],
  3841. [5, 5, 3, 3, 1]], output)
  3842. def test_grey_erosion03(self):
  3843. array = numpy.array([[3, 2, 5, 1, 4],
  3844. [7, 6, 9, 3, 5],
  3845. [5, 8, 3, 7, 1]])
  3846. footprint = [[1, 0, 1], [1, 1, 0]]
  3847. structure = [[1, 1, 1], [1, 1, 1]]
  3848. output = ndimage.grey_erosion(array, footprint=footprint,
  3849. structure=structure)
  3850. assert_array_almost_equal([[1, 1, 0, 0, 0],
  3851. [1, 2, 0, 2, 0],
  3852. [4, 4, 2, 2, 0]], output)
  3853. def test_grey_dilation01(self):
  3854. array = numpy.array([[3, 2, 5, 1, 4],
  3855. [7, 6, 9, 3, 5],
  3856. [5, 8, 3, 7, 1]])
  3857. footprint = [[0, 1, 1], [1, 0, 1]]
  3858. output = ndimage.grey_dilation(array, footprint=footprint)
  3859. assert_array_almost_equal([[7, 7, 9, 9, 5],
  3860. [7, 9, 8, 9, 7],
  3861. [8, 8, 8, 7, 7]], output)
  3862. def test_grey_dilation02(self):
  3863. array = numpy.array([[3, 2, 5, 1, 4],
  3864. [7, 6, 9, 3, 5],
  3865. [5, 8, 3, 7, 1]])
  3866. footprint = [[0, 1, 1], [1, 0, 1]]
  3867. structure = [[0, 0, 0], [0, 0, 0]]
  3868. output = ndimage.grey_dilation(array, footprint=footprint,
  3869. structure=structure)
  3870. assert_array_almost_equal([[7, 7, 9, 9, 5],
  3871. [7, 9, 8, 9, 7],
  3872. [8, 8, 8, 7, 7]], output)
  3873. def test_grey_dilation03(self):
  3874. array = numpy.array([[3, 2, 5, 1, 4],
  3875. [7, 6, 9, 3, 5],
  3876. [5, 8, 3, 7, 1]])
  3877. footprint = [[0, 1, 1], [1, 0, 1]]
  3878. structure = [[1, 1, 1], [1, 1, 1]]
  3879. output = ndimage.grey_dilation(array, footprint=footprint,
  3880. structure=structure)
  3881. assert_array_almost_equal([[8, 8, 10, 10, 6],
  3882. [8, 10, 9, 10, 8],
  3883. [9, 9, 9, 8, 8]], output)
  3884. def test_grey_opening01(self):
  3885. array = numpy.array([[3, 2, 5, 1, 4],
  3886. [7, 6, 9, 3, 5],
  3887. [5, 8, 3, 7, 1]])
  3888. footprint = [[1, 0, 1], [1, 1, 0]]
  3889. tmp = ndimage.grey_erosion(array, footprint=footprint)
  3890. expected = ndimage.grey_dilation(tmp, footprint=footprint)
  3891. output = ndimage.grey_opening(array, footprint=footprint)
  3892. assert_array_almost_equal(expected, output)
  3893. def test_grey_opening02(self):
  3894. array = numpy.array([[3, 2, 5, 1, 4],
  3895. [7, 6, 9, 3, 5],
  3896. [5, 8, 3, 7, 1]])
  3897. footprint = [[1, 0, 1], [1, 1, 0]]
  3898. structure = [[0, 0, 0], [0, 0, 0]]
  3899. tmp = ndimage.grey_erosion(array, footprint=footprint,
  3900. structure=structure)
  3901. expected = ndimage.grey_dilation(tmp, footprint=footprint,
  3902. structure=structure)
  3903. output = ndimage.grey_opening(array, footprint=footprint,
  3904. structure=structure)
  3905. assert_array_almost_equal(expected, output)
  3906. def test_grey_closing01(self):
  3907. array = numpy.array([[3, 2, 5, 1, 4],
  3908. [7, 6, 9, 3, 5],
  3909. [5, 8, 3, 7, 1]])
  3910. footprint = [[1, 0, 1], [1, 1, 0]]
  3911. tmp = ndimage.grey_dilation(array, footprint=footprint)
  3912. expected = ndimage.grey_erosion(tmp, footprint=footprint)
  3913. output = ndimage.grey_closing(array, footprint=footprint)
  3914. assert_array_almost_equal(expected, output)
  3915. def test_grey_closing02(self):
  3916. array = numpy.array([[3, 2, 5, 1, 4],
  3917. [7, 6, 9, 3, 5],
  3918. [5, 8, 3, 7, 1]])
  3919. footprint = [[1, 0, 1], [1, 1, 0]]
  3920. structure = [[0, 0, 0], [0, 0, 0]]
  3921. tmp = ndimage.grey_dilation(array, footprint=footprint,
  3922. structure=structure)
  3923. expected = ndimage.grey_erosion(tmp, footprint=footprint,
  3924. structure=structure)
  3925. output = ndimage.grey_closing(array, footprint=footprint,
  3926. structure=structure)
  3927. assert_array_almost_equal(expected, output)
  3928. def test_morphological_gradient01(self):
  3929. array = numpy.array([[3, 2, 5, 1, 4],
  3930. [7, 6, 9, 3, 5],
  3931. [5, 8, 3, 7, 1]])
  3932. footprint = [[1, 0, 1], [1, 1, 0]]
  3933. structure = [[0, 0, 0], [0, 0, 0]]
  3934. tmp1 = ndimage.grey_dilation(array, footprint=footprint,
  3935. structure=structure)
  3936. tmp2 = ndimage.grey_erosion(array, footprint=footprint,
  3937. structure=structure)
  3938. expected = tmp1 - tmp2
  3939. output = numpy.zeros(array.shape, array.dtype)
  3940. ndimage.morphological_gradient(array, footprint=footprint,
  3941. structure=structure, output=output)
  3942. assert_array_almost_equal(expected, output)
  3943. def test_morphological_gradient02(self):
  3944. array = numpy.array([[3, 2, 5, 1, 4],
  3945. [7, 6, 9, 3, 5],
  3946. [5, 8, 3, 7, 1]])
  3947. footprint = [[1, 0, 1], [1, 1, 0]]
  3948. structure = [[0, 0, 0], [0, 0, 0]]
  3949. tmp1 = ndimage.grey_dilation(array, footprint=footprint,
  3950. structure=structure)
  3951. tmp2 = ndimage.grey_erosion(array, footprint=footprint,
  3952. structure=structure)
  3953. expected = tmp1 - tmp2
  3954. output = ndimage.morphological_gradient(array, footprint=footprint,
  3955. structure=structure)
  3956. assert_array_almost_equal(expected, output)
  3957. def test_morphological_laplace01(self):
  3958. array = numpy.array([[3, 2, 5, 1, 4],
  3959. [7, 6, 9, 3, 5],
  3960. [5, 8, 3, 7, 1]])
  3961. footprint = [[1, 0, 1], [1, 1, 0]]
  3962. structure = [[0, 0, 0], [0, 0, 0]]
  3963. tmp1 = ndimage.grey_dilation(array, footprint=footprint,
  3964. structure=structure)
  3965. tmp2 = ndimage.grey_erosion(array, footprint=footprint,
  3966. structure=structure)
  3967. expected = tmp1 + tmp2 - 2 * array
  3968. output = numpy.zeros(array.shape, array.dtype)
  3969. ndimage.morphological_laplace(array, footprint=footprint,
  3970. structure=structure, output=output)
  3971. assert_array_almost_equal(expected, output)
  3972. def test_morphological_laplace02(self):
  3973. array = numpy.array([[3, 2, 5, 1, 4],
  3974. [7, 6, 9, 3, 5],
  3975. [5, 8, 3, 7, 1]])
  3976. footprint = [[1, 0, 1], [1, 1, 0]]
  3977. structure = [[0, 0, 0], [0, 0, 0]]
  3978. tmp1 = ndimage.grey_dilation(array, footprint=footprint,
  3979. structure=structure)
  3980. tmp2 = ndimage.grey_erosion(array, footprint=footprint,
  3981. structure=structure)
  3982. expected = tmp1 + tmp2 - 2 * array
  3983. output = ndimage.morphological_laplace(array, footprint=footprint,
  3984. structure=structure)
  3985. assert_array_almost_equal(expected, output)
  3986. def test_white_tophat01(self):
  3987. array = numpy.array([[3, 2, 5, 1, 4],
  3988. [7, 6, 9, 3, 5],
  3989. [5, 8, 3, 7, 1]])
  3990. footprint = [[1, 0, 1], [1, 1, 0]]
  3991. structure = [[0, 0, 0], [0, 0, 0]]
  3992. tmp = ndimage.grey_opening(array, footprint=footprint,
  3993. structure=structure)
  3994. expected = array - tmp
  3995. output = numpy.zeros(array.shape, array.dtype)
  3996. ndimage.white_tophat(array, footprint=footprint,
  3997. structure=structure, output=output)
  3998. assert_array_almost_equal(expected, output)
  3999. def test_white_tophat02(self):
  4000. array = numpy.array([[3, 2, 5, 1, 4],
  4001. [7, 6, 9, 3, 5],
  4002. [5, 8, 3, 7, 1]])
  4003. footprint = [[1, 0, 1], [1, 1, 0]]
  4004. structure = [[0, 0, 0], [0, 0, 0]]
  4005. tmp = ndimage.grey_opening(array, footprint=footprint,
  4006. structure=structure)
  4007. expected = array - tmp
  4008. output = ndimage.white_tophat(array, footprint=footprint,
  4009. structure=structure)
  4010. assert_array_almost_equal(expected, output)
  4011. def test_white_tophat03(self):
  4012. array = numpy.array([[1, 0, 0, 0, 0, 0, 0],
  4013. [0, 1, 1, 1, 1, 1, 0],
  4014. [0, 1, 1, 1, 1, 1, 0],
  4015. [0, 1, 1, 1, 1, 1, 0],
  4016. [0, 1, 1, 1, 0, 1, 0],
  4017. [0, 1, 1, 1, 1, 1, 0],
  4018. [0, 0, 0, 0, 0, 0, 1]], dtype=numpy.bool_)
  4019. structure = numpy.ones((3, 3), dtype=numpy.bool_)
  4020. expected = numpy.array([[0, 1, 1, 0, 0, 0, 0],
  4021. [1, 0, 0, 1, 1, 1, 0],
  4022. [1, 0, 0, 1, 1, 1, 0],
  4023. [0, 1, 1, 0, 0, 0, 1],
  4024. [0, 1, 1, 0, 1, 0, 1],
  4025. [0, 1, 1, 0, 0, 0, 1],
  4026. [0, 0, 0, 1, 1, 1, 1]], dtype=numpy.bool_)
  4027. output = ndimage.white_tophat(array, structure=structure)
  4028. assert_array_equal(expected, output)
  4029. def test_white_tophat04(self):
  4030. array = numpy.eye(5, dtype=numpy.bool_)
  4031. structure = numpy.ones((3, 3), dtype=numpy.bool_)
  4032. # Check that type mismatch is properly handled
  4033. output = numpy.empty_like(array, dtype=numpy.float)
  4034. ndimage.white_tophat(array, structure=structure, output=output)
  4035. def test_black_tophat01(self):
  4036. array = numpy.array([[3, 2, 5, 1, 4],
  4037. [7, 6, 9, 3, 5],
  4038. [5, 8, 3, 7, 1]])
  4039. footprint = [[1, 0, 1], [1, 1, 0]]
  4040. structure = [[0, 0, 0], [0, 0, 0]]
  4041. tmp = ndimage.grey_closing(array, footprint=footprint,
  4042. structure=structure)
  4043. expected = tmp - array
  4044. output = numpy.zeros(array.shape, array.dtype)
  4045. ndimage.black_tophat(array, footprint=footprint,
  4046. structure=structure, output=output)
  4047. assert_array_almost_equal(expected, output)
  4048. def test_black_tophat02(self):
  4049. array = numpy.array([[3, 2, 5, 1, 4],
  4050. [7, 6, 9, 3, 5],
  4051. [5, 8, 3, 7, 1]])
  4052. footprint = [[1, 0, 1], [1, 1, 0]]
  4053. structure = [[0, 0, 0], [0, 0, 0]]
  4054. tmp = ndimage.grey_closing(array, footprint=footprint,
  4055. structure=structure)
  4056. expected = tmp - array
  4057. output = ndimage.black_tophat(array, footprint=footprint,
  4058. structure=structure)
  4059. assert_array_almost_equal(expected, output)
  4060. def test_black_tophat03(self):
  4061. array = numpy.array([[1, 0, 0, 0, 0, 0, 0],
  4062. [0, 1, 1, 1, 1, 1, 0],
  4063. [0, 1, 1, 1, 1, 1, 0],
  4064. [0, 1, 1, 1, 1, 1, 0],
  4065. [0, 1, 1, 1, 0, 1, 0],
  4066. [0, 1, 1, 1, 1, 1, 0],
  4067. [0, 0, 0, 0, 0, 0, 1]], dtype=numpy.bool_)
  4068. structure = numpy.ones((3, 3), dtype=numpy.bool_)
  4069. expected = numpy.array([[0, 1, 1, 1, 1, 1, 1],
  4070. [1, 0, 0, 0, 0, 0, 1],
  4071. [1, 0, 0, 0, 0, 0, 1],
  4072. [1, 0, 0, 0, 0, 0, 1],
  4073. [1, 0, 0, 0, 1, 0, 1],
  4074. [1, 0, 0, 0, 0, 0, 1],
  4075. [1, 1, 1, 1, 1, 1, 0]], dtype=numpy.bool_)
  4076. output = ndimage.black_tophat(array, structure=structure)
  4077. assert_array_equal(expected, output)
  4078. def test_black_tophat04(self):
  4079. array = numpy.eye(5, dtype=numpy.bool_)
  4080. structure = numpy.ones((3, 3), dtype=numpy.bool_)
  4081. # Check that type mismatch is properly handled
  4082. output = numpy.empty_like(array, dtype=numpy.float)
  4083. ndimage.black_tophat(array, structure=structure, output=output)
  4084. def test_hit_or_miss01(self):
  4085. struct = [[0, 1, 0],
  4086. [1, 1, 1],
  4087. [0, 1, 0]]
  4088. expected = [[0, 0, 0, 0, 0],
  4089. [0, 1, 0, 0, 0],
  4090. [0, 0, 0, 0, 0],
  4091. [0, 0, 0, 0, 0],
  4092. [0, 0, 0, 0, 0],
  4093. [0, 0, 0, 0, 0],
  4094. [0, 0, 0, 0, 0],
  4095. [0, 0, 0, 0, 0]]
  4096. for type_ in self.types:
  4097. data = numpy.array([[0, 1, 0, 0, 0],
  4098. [1, 1, 1, 0, 0],
  4099. [0, 1, 0, 1, 1],
  4100. [0, 0, 1, 1, 1],
  4101. [0, 1, 1, 1, 0],
  4102. [0, 1, 1, 1, 1],
  4103. [0, 1, 1, 1, 1],
  4104. [0, 0, 0, 0, 0]], type_)
  4105. out = numpy.zeros(data.shape, bool)
  4106. ndimage.binary_hit_or_miss(data, struct, output=out)
  4107. assert_array_almost_equal(expected, out)
  4108. def test_hit_or_miss02(self):
  4109. struct = [[0, 1, 0],
  4110. [1, 1, 1],
  4111. [0, 1, 0]]
  4112. expected = [[0, 0, 0, 0, 0, 0, 0, 0],
  4113. [0, 1, 0, 0, 0, 0, 0, 0],
  4114. [0, 0, 0, 0, 0, 0, 0, 0],
  4115. [0, 0, 0, 0, 0, 0, 0, 0]]
  4116. for type_ in self.types:
  4117. data = numpy.array([[0, 1, 0, 0, 1, 1, 1, 0],
  4118. [1, 1, 1, 0, 0, 1, 0, 0],
  4119. [0, 1, 0, 1, 1, 1, 1, 0],
  4120. [0, 0, 0, 0, 0, 0, 0, 0]], type_)
  4121. out = ndimage.binary_hit_or_miss(data, struct)
  4122. assert_array_almost_equal(expected, out)
  4123. def test_hit_or_miss03(self):
  4124. struct1 = [[0, 0, 0],
  4125. [1, 1, 1],
  4126. [0, 0, 0]]
  4127. struct2 = [[1, 1, 1],
  4128. [0, 0, 0],
  4129. [1, 1, 1]]
  4130. expected = [[0, 0, 0, 0, 0, 1, 0, 0],
  4131. [0, 0, 0, 0, 0, 0, 0, 0],
  4132. [0, 0, 0, 0, 0, 0, 0, 0],
  4133. [0, 0, 0, 0, 0, 0, 0, 0],
  4134. [0, 0, 0, 0, 0, 0, 0, 0],
  4135. [0, 0, 0, 0, 0, 0, 0, 0],
  4136. [0, 0, 1, 0, 0, 0, 0, 0],
  4137. [0, 0, 0, 0, 0, 0, 0, 0]]
  4138. for type_ in self.types:
  4139. data = numpy.array([[0, 1, 0, 0, 1, 1, 1, 0],
  4140. [1, 1, 1, 0, 0, 0, 0, 0],
  4141. [0, 1, 0, 1, 1, 1, 1, 0],
  4142. [0, 0, 1, 1, 1, 1, 1, 0],
  4143. [0, 1, 1, 1, 0, 1, 1, 0],
  4144. [0, 0, 0, 0, 1, 1, 1, 0],
  4145. [0, 1, 1, 1, 1, 1, 1, 0],
  4146. [0, 0, 0, 0, 0, 0, 0, 0]], type_)
  4147. out = ndimage.binary_hit_or_miss(data, struct1, struct2)
  4148. assert_array_almost_equal(expected, out)
  4149. class TestDilateFix:
  4150. def setup_method(self):
  4151. # dilation related setup
  4152. self.array = numpy.array([[0, 0, 0, 0, 0],
  4153. [0, 0, 0, 0, 0],
  4154. [0, 0, 0, 1, 0],
  4155. [0, 0, 1, 1, 0],
  4156. [0, 0, 0, 0, 0]], dtype=numpy.uint8)
  4157. self.sq3x3 = numpy.ones((3, 3))
  4158. dilated3x3 = ndimage.binary_dilation(self.array, structure=self.sq3x3)
  4159. self.dilated3x3 = dilated3x3.view(numpy.uint8)
  4160. def test_dilation_square_structure(self):
  4161. result = ndimage.grey_dilation(self.array, structure=self.sq3x3)
  4162. # +1 accounts for difference between grey and binary dilation
  4163. assert_array_almost_equal(result, self.dilated3x3 + 1)
  4164. def test_dilation_scalar_size(self):
  4165. result = ndimage.grey_dilation(self.array, size=3)
  4166. assert_array_almost_equal(result, self.dilated3x3)
  4167. class TestBinaryOpeningClosing:
  4168. def setup_method(self):
  4169. a = numpy.zeros((5,5), dtype=bool)
  4170. a[1:4, 1:4] = True
  4171. a[4,4] = True
  4172. self.array = a
  4173. self.sq3x3 = numpy.ones((3,3))
  4174. self.opened_old = ndimage.binary_opening(self.array, self.sq3x3,
  4175. 1, None, 0)
  4176. self.closed_old = ndimage.binary_closing(self.array, self.sq3x3,
  4177. 1, None, 0)
  4178. def test_opening_new_arguments(self):
  4179. opened_new = ndimage.binary_opening(self.array, self.sq3x3, 1, None,
  4180. 0, None, 0, False)
  4181. assert_array_equal(opened_new, self.opened_old)
  4182. def test_closing_new_arguments(self):
  4183. closed_new = ndimage.binary_closing(self.array, self.sq3x3, 1, None,
  4184. 0, None, 0, False)
  4185. assert_array_equal(closed_new, self.closed_old)