# HG changeset patch # User luoboiqingcai # Date 1350995132 -28800 # Node ID 9dbd5871223056023be1126eb777ca7cca4a0f03 # Parent cf10ec35adee9b32bdeeecf68e3735be9196fc6e finish river_map testing diff --git a/demfunctions.py b/demfunctions.py --- a/demfunctions.py +++ b/demfunctions.py @@ -264,10 +264,10 @@ c_max = len(data[0]) # exactly len(data[0]) - 1 water_matrix = [[0 for c in range(c_max)] for r in range(r_max)] def trackpath(r,c): - #if r > r_max-1 or r < 0 or c>c_max-1 or c < 0: - # return [] - if r>5 or r<0 or c>5 or c<0: - return[] + if r > r_max-1 or r < 0 or c>c_max-1 or c < 0: + return [] + #if r>5 or r<0 or c>5 or c<0: + # return[] node = data[r][c] path = [[r,c],] if node == 0: @@ -289,7 +289,7 @@ if node == 4: temp = trackpath(r+1,c-1) if len(temp) >0: - path.exten(temp) + path.extend(temp) if node == 5: temp = trackpath(r,c-1) if len(temp) >0: @@ -297,7 +297,7 @@ if node == 6: temp = trackpath(r-1,c-1) if len(temp) >0: - path.extend() + path.extend(temp) if node == 7: temp = trackpath(r-1,c) if len(temp) >0: @@ -309,3 +309,13 @@ for node in path: water_matrix[node[0]][node[1]] += 1 return water_matrix + +def river_map(data,threshold=5): + r_max = len(data) # exactly len(data) - 1 + c_max = len(data[0]) # exactly len(data[0]) - 1 + rivermap_matrix = [[0 for c in range(c_max)] for r in range(r_max)] + for r in xrange(len(data)): + for c in xrange(len(data[r])): + if data[r][c] >=threshold: + rivermap_matrix[r][c] = 1 + return rivermap_matrix diff --git a/test.py b/test.py --- a/test.py +++ b/test.py @@ -1,4 +1,5 @@ -from demfunctions import (fill_sinks,lift,get_vect_martrix,count_water) +from demfunctions import (fill_sinks,lift,get_vect_martrix,count_water,river_map) +import pprint #data_const = [[1,1,3,4,5,5,7,5,4], # [1,2,4,4,4,4,6,5,3], @@ -55,18 +56,36 @@ print test_get_vect_martrix() -#def test_count_water(): -# data = [[1,1,3,4,5,5,7,5,4], -# [1,2,4,4,4,4,6,5,3], -# [4,4,3,4,3,3,6,7,5], -# [3,3,2,3,2,2,4,6,7], -# [1,2,2,2,1,2,3,4,5], -# [3,3,2,1,1,2,3,6,6], -# [2,3,3,1,1,2,5,7,8], -# [1,2,3,3,3,3,4,8,6], -# [1,1,2,3,3,4,5,7,8]] -# data_modified = fill_sinks(data) -# data_lifted = lift(data,data_modified,1e-5) -# vect_martrix = get_vect_martrix(data_lifted,1,1) -# return count_water(vect_martrix) -#print test_count_water() +def test_count_water(): + data = [[1,1,3,4,5,5,7,5,4], + [1,2,4,4,4,4,6,5,3], + [4,4,3,4,3,3,6,7,5], + [3,3,2,3,2,2,4,6,7], + [1,2,2,2,1,2,3,4,5], + [3,3,2,1,1,2,3,6,6], + [2,3,3,1,1,2,5,7,8], + [1,2,3,3,3,3,4,8,6], + [1,1,2,3,3,4,5,7,8]] + data_modified = fill_sinks(data) + data_lifted = lift(data_modified,1e-5) + vect_martrix = get_vect_martrix(data_lifted,1,1) + return count_water(vect_martrix) +print test_count_water() + +def test_river_map(): + data = [[1,1,3,4,5,5,7,5,4], + [1,2,4,4,4,4,6,5,3], + [4,4,3,4,3,3,6,7,5], + [3,3,2,3,2,2,4,6,7], + [1,2,2,2,1,2,3,4,5], + [3,3,2,1,1,2,3,6,6], + [2,3,3,1,1,2,5,7,8], + [1,2,3,3,3,3,4,8,6], + [1,1,2,3,3,4,5,7,8]] + data_modified = fill_sinks(data) + data_lifted = lift(data_modified,1e-5) + vect_martrix = get_vect_martrix(data_lifted,1,1) + return river_map(count_water(vect_martrix),5) + +pprint.pprint(test_river_map()) +