<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Python Rolling Average for Road Sections in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-rolling-average-for-road-sections/m-p/1081583#M61844</link>
    <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;I have a road network dataset with 100m sections with a condition score and need an rolling average of the score for the previous 10 segments. This is for road maintenance planning &amp;amp; prioritisation.&lt;/P&gt;&lt;P&gt;I would like an efficient way to do this in python (to be incorporated into model builder). In a geoprocessing&lt;SPAN&gt;&amp;nbsp;model I have achieved an average using a iterative selected distance and summary stats but it takes a very long time (over a month) to process the +100K sections. I have been shown it can be done in seconds in excel. To incorporate the process into an automated workflow, python is the go but I lack the experience.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;The data example I have attached consists of:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Road section - Road ID (RID) which identifies the road,&lt;/LI&gt;&lt;LI&gt;Tdist&amp;nbsp; start and end - Through distance / chainage, each section is identified by a start and end,&lt;/LI&gt;&lt;LI&gt;C Score - Condition score.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;FONT face="inherit"&gt;I have access to the numpy &lt;/FONT&gt;library&lt;FONT face="inherit"&gt;&amp;nbsp;also. Hopefully some data science guru can help me.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="inherit"&gt;I appreciate any help and thanks in advance &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Example sections" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/19097iF4D68A72C6A291E8/image-size/large?v=v2&amp;amp;px=999" role="button" title="2021-07-22 08_30_26-Test_Iterate - Map - ArcGIS Pro.png" alt="Example sections" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Example sections&lt;/span&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 21 Jul 2021 23:06:36 GMT</pubDate>
    <dc:creator>DanH7</dc:creator>
    <dc:date>2021-07-21T23:06:36Z</dc:date>
    <item>
      <title>Python Rolling Average for Road Sections</title>
      <link>https://community.esri.com/t5/python-questions/python-rolling-average-for-road-sections/m-p/1081583#M61844</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;I have a road network dataset with 100m sections with a condition score and need an rolling average of the score for the previous 10 segments. This is for road maintenance planning &amp;amp; prioritisation.&lt;/P&gt;&lt;P&gt;I would like an efficient way to do this in python (to be incorporated into model builder). In a geoprocessing&lt;SPAN&gt;&amp;nbsp;model I have achieved an average using a iterative selected distance and summary stats but it takes a very long time (over a month) to process the +100K sections. I have been shown it can be done in seconds in excel. To incorporate the process into an automated workflow, python is the go but I lack the experience.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;The data example I have attached consists of:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Road section - Road ID (RID) which identifies the road,&lt;/LI&gt;&lt;LI&gt;Tdist&amp;nbsp; start and end - Through distance / chainage, each section is identified by a start and end,&lt;/LI&gt;&lt;LI&gt;C Score - Condition score.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;FONT face="inherit"&gt;I have access to the numpy &lt;/FONT&gt;library&lt;FONT face="inherit"&gt;&amp;nbsp;also. Hopefully some data science guru can help me.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="inherit"&gt;I appreciate any help and thanks in advance &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Example sections" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/19097iF4D68A72C6A291E8/image-size/large?v=v2&amp;amp;px=999" role="button" title="2021-07-22 08_30_26-Test_Iterate - Map - ArcGIS Pro.png" alt="Example sections" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Example sections&lt;/span&gt;&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jul 2021 23:06:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-rolling-average-for-road-sections/m-p/1081583#M61844</guid>
      <dc:creator>DanH7</dc:creator>
      <dc:date>2021-07-21T23:06:36Z</dc:date>
    </item>
    <item>
      <title>Re: Python Rolling Average for Road Sections</title>
      <link>https://community.esri.com/t5/python-questions/python-rolling-average-for-road-sections/m-p/1081592#M61846</link>
      <description>&lt;P&gt;Projected data only.&lt;/P&gt;&lt;P&gt;Skip the shapefile, use a local gdb featureclass.&lt;/P&gt;&lt;P&gt;Running mean/rolling average&amp;nbsp; &amp;nbsp; same&lt;/P&gt;&lt;P&gt;arcpy.da.ExtendTable to join an array back to a featureclass table&lt;/P&gt;&lt;P&gt;Lesson to emulate, example 20 numbers with a rolling 5 average, hence the first 2 and last 2 will be nodata (nan = not a number)&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import numpy as np
from numpy.lib.stride_tricks import sliding_window_view as swv
# create an empty array with 20 rows and 3 columns (int, float, float)
z = np.empty(shape=(20,), dtype=[("ids", 'i8'), ("vals", "f8"), ("rolling_mean", "f8")])
z["ids"] = np.arange(20)
z["vals"] = np.random.randint(0, 10, 20) * 1.0
z["rolling_mean"] = np.repeat(np.nan, 20)
rolling_win = swv(z["vals"], 5)
means_ = np.mean(rolling_win, axis=1)
z["rolling_mean"][2:18] = means_  # do the slicey thing since there are 16 vals
z
array([( 0, 1., nan), ( 1, 7., nan), ( 2, 7., 5.4), ( 3, 8., 5.2),
       ( 4, 4., 4. ), ( 5, 0., 2.6), ( 6, 1., 1.4), ( 7, 0., 1.4),
       ( 8, 2., 2.8), ( 9, 4., 3.2), (10, 7., 4. ), (11, 3., 4.2),
       (12, 4., 4. ), (13, 3., 3. ), (14, 3., 2.6), (15, 2., 2. ),
       (16, 1., 1.6), (17, 1., 1.4), (18, 1., nan), (19, 2., nan)],
      dtype=[('ids', '&amp;lt;i8'), ('vals', '&amp;lt;f8'), ('rolling_mean', '&amp;lt;f8')])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Jul 2021 23:49:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-rolling-average-for-road-sections/m-p/1081592#M61846</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-07-21T23:49:09Z</dc:date>
    </item>
    <item>
      <title>Re: Python Rolling Average for Road Sections</title>
      <link>https://community.esri.com/t5/python-questions/python-rolling-average-for-road-sections/m-p/1086453#M61960</link>
      <description>&lt;P&gt;Thanks Dan. I have been scratching my head over your reply for a couple of weeks. I appreciate the teach a man to fish philosophy. I still cant get past this error&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class="ansi-red-fg"&gt;ImportError&lt;/SPAN&gt;: cannot import name 'sliding_window_view'&amp;nbsp;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;I'm obviously way out of my depth with this!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help.&lt;/P&gt;</description>
      <pubDate>Fri, 06 Aug 2021 04:42:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-rolling-average-for-road-sections/m-p/1086453#M61960</guid>
      <dc:creator>DanH7</dc:creator>
      <dc:date>2021-08-06T04:42:17Z</dc:date>
    </item>
    <item>
      <title>Re: Python Rolling Average for Road Sections</title>
      <link>https://community.esri.com/t5/python-questions/python-rolling-average-for-road-sections/m-p/1086482#M61961</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from numpy.lib.stride_tricks import sliding_window_view as swv&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;sliding_window_view is a function in the module stride_tricks which is inside the&amp;nbsp;numpy.lib folder of the package numpy.&amp;nbsp; Use windows explorer, find the&lt;/P&gt;&lt;P&gt;C:\... your arcgis_pro_install_folder ...\bin\Python\envs\arcgispro-py3\Lib\site-packages&lt;/P&gt;&lt;P&gt;Locate the numpy folder, then the lib folder and open up the stride_tricks.py file, do the scroll thing until you see the sliding_windows_view function.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;import as .... means import the function with an abbreviation of swv&lt;/P&gt;&lt;P&gt;Now !&amp;nbsp; if you mean you can't get past it because it errors out, then you aren't using ArcGIS Pro OR you don't have the appropriate version of ArcGIS Pro.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ArcGIS uses specific versions of python and numpy for each rollout.&amp;nbsp; If you are using ArcMap,&amp;nbsp; their version of python is using 10+ year old versions.&lt;/P&gt;&lt;P&gt;from the function help topic....&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Create a sliding window view into the array with the given window shape.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Also known as rolling or moving window, the window slides across all dimensions of the array and extracts subsets of the array at all window positions.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;New in version &lt;STRONG&gt;1.20.0&lt;/STRONG&gt;.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;Which might mean that you aren't using Arcgis Pro which uses python 3 and Pro 2.8 uses numpy version 1.20.*.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 06 Aug 2021 09:33:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-rolling-average-for-road-sections/m-p/1086482#M61961</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-08-06T09:33:51Z</dc:date>
    </item>
  </channel>
</rss>

