Skip to content

Commit

Permalink
Merge pull request #168 from dkav/time-clock-fix
Browse files Browse the repository at this point in the history
Replace time.clock with time.perf_counter
  • Loading branch information
johngallo authored Jan 19, 2023
2 parents d24add2 + c297c54 commit 6583aa4
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 32 deletions.
11 changes: 8 additions & 3 deletions toolbox/scripts/iterate_barriers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
import sys
import os
import shutil
import time

# Add support for Python 2. Try to import Python 3 module first.
try:
from time import perf_counter
except ImportError:
from time import clock as perf_counter

import numpy as npy
import arcpy
Expand All @@ -28,7 +33,7 @@ def main(argv=None):
if argv is None:
argv = sys.argv # Get parameters from ArcGIS tool dialog

start_time = time.clock()
start_time = perf_counter()

# USER SETTINGS ######################################################

Expand Down Expand Up @@ -180,7 +185,7 @@ def main(argv=None):
all_restored_areas_ras = ''

for cur_iter in range(1, iterations + 1):
start_time1 = time.clock()
start_time1 = perf_counter()

# Some env settings get changed by linkage mapper and must be
# reset here
Expand Down
15 changes: 9 additions & 6 deletions toolbox/scripts/lm_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
import sys
import subprocess
from datetime import datetime as dt
import time
import traceback
import platform

# Support configparser in Python 2 and 3
# Add support for Python 2. Try to import Python 3 modules first.
try:
from configparser import RawConfigParser # Python 3
from configparser import RawConfigParser
except ImportError:
from ConfigParser import RawConfigParser # Python 2
from ConfigParser import RawConfigParser
try:
from time import perf_counter
except ImportError:
from time import clock as perf_counter

import shutil
import gc
Expand Down Expand Up @@ -327,7 +330,7 @@ def run_time(stime):

def elapsed_time(start_time):
"""Print elapsed time given a start time and return a new start time."""
now = time.clock()
now = perf_counter()
hours, minutes, seconds = s2hhmmss(now - start_time)
msg = "Task took:"
if minutes == 0:
Expand Down Expand Up @@ -419,7 +422,7 @@ def get_adj_using_shift_method(alloc):
arcpy.env.workspace = cfg.SCRATCHDIR

gprint('Calculating adjacencies crossing allocation boundaries...')
start_time = time.clock()
start_time = perf_counter()
arcpy.Shift_management(alloc, "alloc_r", posShift, "0")

alloc_r = "alloc_r"
Expand Down
13 changes: 9 additions & 4 deletions toolbox/scripts/s1_getAdjacencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
"""

from os import path
import time

# Add support for Python 2. Try to import Python 3 modules first.
try:
from time import perf_counter
except ImportError:
from time import clock as perf_counter

import numpy as npy
import arcpy
Expand Down Expand Up @@ -116,7 +121,7 @@ def cwadjacency():
arcpy.env.extent = arcpy.Describe(cfg.RESRAST).extent
if cfg.BUFFERDIST is not None:
# Clip resistance raster using bounding circle
start_time = time.clock()
start_time = perf_counter()
arcpy.env.cellSize = arcpy.Describe(cfg.RESRAST).MeanCellHeight
arcpy.env.extent = arcpy.Describe(cfg.RESRAST).Extent
bResistance = arcpy.sa.ExtractByMask(cfg.RESRAST, cfg.BNDCIR)
Expand All @@ -126,7 +131,7 @@ def cwadjacency():
else:
bResistance = cfg.RESRAST

start_time = time.clock()
start_time = perf_counter()
gprint('Starting cost-weighted distance allocation...')

if cfg.TMAXCWDIST is not None:
Expand Down Expand Up @@ -202,7 +207,7 @@ def euadjacency():
if cfg.BUFFERDIST is not None:
arcpy.env.extent = arcpy.Describe(cfg.BNDCIR).extent

start_time = time.clock()
start_time = perf_counter()

arcpy.env.scratchWorkspace = cfg.ARCSCRATCHDIR
outDistanceRaster = path.join(cfg.ADJACENCYDIR, "euc")
Expand Down
9 changes: 7 additions & 2 deletions toolbox/scripts/s2_buildNetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
"""

from os import path
import time

# Add support for Python 2. Try to import Python 3 module first.
try:
from time import perf_counter
except ImportError:
from time import clock as perf_counter

import numpy as npy
import arcpy
Expand Down Expand Up @@ -313,7 +318,7 @@ def generate_distance_file():
gprint('There are ' + str(len(adjList)) + ' adjacent core pairs to '
'process.')
pctDone = 0
start_time = time.clock()
start_time = perf_counter()
for x in range(0, len(adjList)):

pctDone = lu.report_pct_done(x, len(adjList), pctDone)
Expand Down
21 changes: 13 additions & 8 deletions toolbox/scripts/s3_calcCwds.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@


from os import path
import time

# Add support for Python 2. Try to import Python 3 module first.
try:
from time import perf_counter
except ImportError:
from time import clock as perf_counter

import numpy as npy
import arcpy
Expand Down Expand Up @@ -149,7 +154,7 @@ def STEP3_calc_cwds():
# Bounding boxes
if (cfg.BUFFERDIST) is not None:
# create bounding boxes around cores
start_time = time.clock()
start_time = perf_counter()
gprint('Calculating bounding boxes for core areas.')
extentBoxList = npy.zeros((0,5), dtype='float32')
for x in range(len(coresToMap)):
Expand All @@ -163,7 +168,7 @@ def STEP3_calc_cwds():
# Bounding circle code
if cfg.BUFFERDIST is not None:
# Make a set of circles encompassing core areas we'll be connecting
start_time = time.clock()
start_time = perf_counter()
gprint('Calculating bounding circles around potential'
' corridors.')

Expand Down Expand Up @@ -291,7 +296,7 @@ def STEP3_calc_cwds():
endIndex = len(coresToMap)
linkTableMod = linkTable.copy()
while x < endIndex:
startTime1 = time.clock()
startTime1 = perf_counter()
# Modification of linkTable in function was causing problems. so
# make a copy:
linkTablePassed = linkTableMod.copy()
Expand Down Expand Up @@ -338,7 +343,7 @@ def STEP3_calc_cwds():
linkTableLogFile = path.join(cfg.LOGDIR, "linkTable_s3.csv")
lu.write_link_table(linkTable, linkTableLogFile)

start_time = time.clock()
start_time = perf_counter()
gprint('Creating shapefiles with linework for links...')
try:
lu.write_link_maps(outlinkTableFile, step=3)
Expand Down Expand Up @@ -429,7 +434,7 @@ def do_cwd_calcs(x, linkTable, coresToMap, lcpLoop, failures):
arcpy.MakeFeatureLayer_management(
cfg.BNDCIRS, "fGlobalBoundingFeat")

start_time = time.clock()
start_time = perf_counter()
# loop through targets and get bounding circles that
# contain focal core and target cores
arcpy.SelectLayerByAttribute_management(
Expand Down Expand Up @@ -485,7 +490,7 @@ def do_cwd_calcs(x, linkTable, coresToMap, lcpLoop, failures):
back_rast = "BACK"
lu.delete_data(path.join(coreDir, back_rast))
lu.delete_data(outDistanceRaster)
start_time = time.clock()
start_time = perf_counter()

# Create raster that just has source core in it
# Note: this seems faster than setnull with LI grid.
Expand Down Expand Up @@ -519,7 +524,7 @@ def do_cwd_calcs(x, linkTable, coresToMap, lcpLoop, failures):
else:
exec(statement)

start_time = time.clock()
start_time = perf_counter()
# Extract cost distances from source core to target cores
# Fixme: there will be redundant calls to b-a when already
# done a-b
Expand Down
9 changes: 7 additions & 2 deletions toolbox/scripts/s4_refineNetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
# Could use previous (now discarded) combo code to mosaic CWDS if wanted.

from os import path
import time

# Add support for Python 2. Try to import Python 3 module first.
try:
from time import perf_counter
except ImportError:
from time import clock as perf_counter

import numpy as npy
import arcpy
Expand Down Expand Up @@ -205,7 +210,7 @@ def STEP4_refine_network():
linkTableLogFile = path.join(cfg.LOGDIR, "linkTable_s4.csv")
lu.write_link_table(linkTable, linkTableLogFile)

start_time = time.clock()
start_time = perf_counter()
lu.update_lcp_shapefile(linkTable, lastStep=3, thisStep=4)
start_time = lu.elapsed_time(start_time)

Expand Down
11 changes: 8 additions & 3 deletions toolbox/scripts/s5_calcLccs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@

import json
from os import path
import time

# Add support for Python 2. Try to import Python 3 module first.
try:
from time import perf_counter
except ImportError:
from time import clock as perf_counter

import numpy as npy
import arcpy
Expand Down Expand Up @@ -142,7 +147,7 @@ def calc_lccs(normalize):
continue

linkCount = linkCount + 1
start_time = time.clock()
start_time = perf_counter()

linkId = str(int(linkTable[x, cfg.LTB_LINKID]))

Expand Down Expand Up @@ -249,7 +254,7 @@ def calc_lccs(normalize):
if not tryAgain:
exec(statement)
else: break
endTime = time.clock()
endTime = perf_counter()
processTime = round((endTime - start_time), 2)

if normalize == True:
Expand Down
9 changes: 7 additions & 2 deletions toolbox/scripts/s6_barriers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
"""

from os import path
import time

# Add support for Python 2. Try to import Python 3 module first.
try:
from time import perf_counter
except ImportError:
from time import clock as perf_counter

import numpy as npy
import arcpy
Expand Down Expand Up @@ -158,7 +163,7 @@ def step6_calc_barriers():
def do_radius_loop():
"""Do radius loop."""
link_table = link_table_tmp.copy()
start_time = time.clock()
start_time = perf_counter()
link_loop = 0
pct_done = 0
gprint('\nMapping barriers at a radius of ' + str(radius) +
Expand Down
9 changes: 7 additions & 2 deletions toolbox/scripts/s8_pinchpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
"""

from os import path
import time

# Add support for Python 2. Try to import Python 3 module first.
try:
from time import perf_counter
except ImportError:
from time import clock as perf_counter

import numpy as npy
import arcpy
Expand Down Expand Up @@ -150,7 +155,7 @@ def STEP8_calc_pinchpoints():
continue
restartFlag = False
lu.create_dir(linkDir)
start_time1 = time.clock()
start_time1 = perf_counter()

# source and target cores
corex=int(coreList[x,0])
Expand Down

0 comments on commit 6583aa4

Please sign in to comment.