{"id":2368,"date":"2019-05-10T11:45:01","date_gmt":"2019-05-09T23:45:01","guid":{"rendered":"http:\/\/blogs.lincoln.ac.nz\/gis\/?p=2368"},"modified":"2023-05-07T03:24:43","modified_gmt":"2023-05-07T03:24:43","slug":"deleting-smokers-part-1","status":"publish","type":"post","link":"https:\/\/blogs.lincoln.ac.nz\/gis\/deleting-smokers-part-1\/","title":{"rendered":"Deleting Smokers &#8211; Part 1"},"content":{"rendered":"<p><em>This is the first post in a series covering the development of a Python script to carry out some analysis.\u00a0 We\u00a0cover the idea behind the script and develop some pseudo-code used to flesh it out.<\/em><\/p>\n<p>For those of you having trouble sleeping, this post may be the one for you.\u00a0 In several previous posts, we&#8217;ve covered some of the analysis done in looking at the link between <a href=\"http:\/\/blogs.lincoln.ac.nz\/gis\/the-spatial-smoking-gun-part-1\/\">tobacco outlets and secondary schools<\/a>.\u00a0 Some of those analyses required the writing of a <a href=\"http:\/\/blogs.lincoln.ac.nz\/gis\/a-smokin-script\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python script<\/a> to automate some complex analysis.\u00a0 There&#8217;s been a move underfoot\u00a0internationally to look at setting minimum distances between tobacco retailers and\u00a0 my colleagues at Otago have been keen to follow this up.\u00a0 We&#8217;ve been looking at <a href=\"https:\/\/www.sciencedirect.com\/science\/article\/pii\/S0091743515000274\" target=\"_blank\" rel=\"noopener noreferrer\">a paper<\/a> that employed a Python script to systematically look over a dataset of retailers, determine their distances apart and then randomly remove retailers until there are none within a given distance of each other.\u00a0 Once all the retailers within a given distance are removed, we can run some statistics to evaluate the effects on policy. Then we do that several more times to get a representative sample size.\u00a0 Different countries have looked at different distances &#8211; the paper we&#8217;ve been looking at uses 500 ft <em>(heathens)<\/em>.<\/p>\n<p>We contacted the paper&#8217;s authors, hoping that they might share their script with us, but alas, we&#8217;ve had no response.\u00a0 So if we&#8217;re going to carry out a similar analysis in NZ, we&#8217;ll have to write our own script, which will probably be an unpleasant experience, but it may work out that I can reuse some of the code another time.\u00a0 (I do slightly exaggerate things&#8230;I actually do like coding, but it can often be a frustrating experience.)<\/p>\n<p>In this series of posts I&#8217;ll step through the whole process, from development, to testing, to debugging and, hopefully, to a fully fledged, error-free, working script.\u00a0 This will either inspire you to start (or continue) your own coding, or more likely, convince you that coding is for someone else to do (I hope it&#8217;s the former).\u00a0 To my way of thinking, there are\u00a0four stages to scripting: conceptualising the script, writing the script, debugging the script and finally, running the script error free.\u00a0 I won&#8217;t say this is universal, but just my approach to it.<\/p>\n<p><em>Disclaimer: (ed. you seem to be doing a lot of that lately&#8230;) I wouldn&#8217;t call myself a Python expert.\u00a0 I know enough to make me dangerous.\u00a0 I&#8217;m sure some readers out there will be kilometres ahead of me and will easily pick out my errors.\u00a0\u00a0<\/em><\/p>\n<p><strong>Stage 1 &#8211; conceptualising the script<\/strong><\/p>\n<p>A first step in writing a script is to be sure you&#8217;re clear about what you want to achieve.\u00a0 And it doubly helps if you can be as crystal clear as possible.\u00a0 Any ambiguity will rear its ugly head and force you to rethink things &#8211; that&#8217;s not a bad thing but if you can be as clear as possible going into it, you can often speed up the process.\u00a0 So a good first cut is to list the important tasks that need to be done as well as the order in which they have to happen if order is important.<\/p>\n<p>Here&#8217;s what they say in the paper about their script: &#8220;ArcMap was used to identify all tobacco retailers within 500 ft\u00a0from another tobacco retailer.\u00a0 A custom script was written in Python to randomly select one tobacco retailer to be deleted from the list.\u00a0 The process continued iteratively until the list contained zero tobacco retail outlets from within 500 ft of another retailer.\u00a0 This random -choice analysis\u00a0<em>(sic)<\/em> yields different results each the process is run (see Figs. 1 and 2).\u00a0 Thus the process was run 1000 times and the mean number of retailers was removed from each list.&#8221;\u00a0 (Myers <em>et al.<\/em>, 2015).\u00a0 I include figure 1 to help explain this:<\/p>\n<p><a href=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2019\/05\/Fig1Myers.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2392\" src=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2019\/05\/Fig1Myers.png\" alt=\"\" width=\"809\" height=\"815\" srcset=\"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2019\/05\/Fig1Myers.png 809w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2019\/05\/Fig1Myers-298x300.png 298w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2019\/05\/Fig1Myers-150x150.png 150w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2019\/05\/Fig1Myers-768x774.png 768w\" sizes=\"auto, (max-width: 809px) 100vw, 809px\" \/><\/a><\/p>\n<p>It&#8217;s interesting to note the figure above focuses on picking &#8220;proximity relationships&#8221; rather than points.\u00a0 After a bit of thought this make sense when you consider two points close to each other.\u00a0 Each has a point within a given distance and so would be of interest, but there&#8217;s only one relationship &#8211; this makes the whole process a bit easier, I think.\u00a0 That&#8217;s already got me thinking of a potential tool I could use &#8211; an <a href=\"http:\/\/desktop.arcgis.com\/en\/arcmap\/latest\/extensions\/network-analyst\/od-cost-matrix.htm\" target=\"_blank\" rel=\"noopener noreferrer\">OD Cost Matrix<\/a>.\u00a0 Not sure yet that that&#8217;s the right tool.<\/p>\n<p>That&#8217;s an overview in sentence form &#8211; next I&#8217;ll see if I can list those steps in order with more of a GIS slant:<\/p>\n<ul>\n<li>Take a set of points and find those that are within a given distance of each other and add them to a list<\/li>\n<li>Pick one at random and then randomly select a relationship\u00a0with one of the close points &#8211; delete one of them at random<\/li>\n<li>Review the list.\u00a0 If there&#8217;s still a proximity relationship delete one of the points at random.<\/li>\n<li>Continue until there are no more proximity relationships<\/li>\n<li>Calculate the statistics<\/li>\n<li>Do\u00a0this 999 more times. working with a different set of randomly selected proximity relationships and points.<\/li>\n<\/ul>\n<p>Technically, I would refer to the list above as pseudocode.\u00a0 This is nothing more than trying to write the script (we could also call this the algorithm) in plain english that &#8220;anyone&#8221; could understand.\u00a0 Later, this pseudocode will be translated into a specific language, Python in this case.\u00a0 It&#8217;s a good place to start<\/p>\n<p>What I&#8217;ve tried to do here is set out as clearly as I can the tasks that need to be done &#8211; and it&#8217;s a work in progress.\u00a0 Looking over the list, there are some tasks that need to be done repeatedly at different points in the script.\u00a0 These are good places for a loop &#8211; a set of instructions that take different inputs but do the same thing over and over again.\u00a0 There&#8217;s one internal loop that keep deleting points until there are no points within the set distance.\u00a0 And of course a big loop that makes this happen 1000 times in total.<\/p>\n<p>At this point, my brain hurts so in a follow up post we&#8217;ll look at this pseudo-code with some loops thrown in.\u00a0 Bet you can&#8217;t wait&#8230;<\/p>\n<p>One thing you may have noticed in this post is that there are no pretty pictures.\u00a0 It&#8217;s hard to find good images to go along with coding, but let&#8217;s try one in honour of Abel Mamboleo, who has just successfully defended his PhD thesis on geosimulation of human-elephant conflicts &#8211; he definitely had to learn some coding for this work.\u00a0 And he was happy to finish:<\/p>\n<p><a href=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2019\/05\/elephant.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2393\" src=\"https:\/\/d-blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2019\/05\/elephant.jpg\" alt=\"\" width=\"1121\" height=\"655\" srcset=\"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2019\/05\/elephant.jpg 1121w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2019\/05\/elephant-300x175.jpg 300w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2019\/05\/elephant-1024x598.jpg 1024w, https:\/\/blogs.lincoln.ac.nz\/gis\/wp-content\/uploads\/sites\/3\/2019\/05\/elephant-768x449.jpg 768w\" sizes=\"auto, (max-width: 1121px) 100vw, 1121px\" \/><\/a><\/p>\n<p>C<\/p>\n<p><a href=\"https:\/\/www.sciencedirect.com\/science\/article\/pii\/S0091743515000274\" target=\"_blank\" rel=\"noopener noreferrer\">Myers, A.E., Hall, M.G., Isgett, L.F. and Ribisl, K.M.\u00a0 2015.\u00a0 A comparison of three policy approaches for tobacco retailer reduction.\u00a0 Preventive Medicine 74:67-73<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is the first post in a series covering the development of a Python script to carry out some analysis.\u00a0 We\u00a0cover the idea behind the script and develop some pseudo-code used to flesh it out. For those of you having trouble sleeping, this post may be the one for you.\u00a0 In several previous posts, we&#8217;ve [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2368","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-json\/wp\/v2\/posts\/2368","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-json\/wp\/v2\/comments?post=2368"}],"version-history":[{"count":1,"href":"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-json\/wp\/v2\/posts\/2368\/revisions"}],"predecessor-version":[{"id":4110,"href":"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-json\/wp\/v2\/posts\/2368\/revisions\/4110"}],"wp:attachment":[{"href":"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-json\/wp\/v2\/media?parent=2368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-json\/wp\/v2\/categories?post=2368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.lincoln.ac.nz\/gis\/wp-json\/wp\/v2\/tags?post=2368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}