Skip to content

Commit

Permalink
cleanup imports;pathing, drop rivgraph as an overall dep
Browse files Browse the repository at this point in the history
  • Loading branch information
jsta committed Mar 12, 2024
1 parent a70f0db commit 9f86ec5
Show file tree
Hide file tree
Showing 4 changed files with 311 additions and 137 deletions.
195 changes: 88 additions & 107 deletions hydropop/dev/end_to_end_new.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,39 @@
# -*- coding: utf-8 -*-
"""
Created on Wed May 25 09:30:14 2022
@author: 318596
"""

import os
import sys
sys.path.append(r'X:\Research\CIMMID\hydropop\make_hpus')
import hp_class as hpc
import hp_utils as hut
import gee_stats as gee
from osgeo import gdal
import rabpro
import pandas as pd
from osgeo import gdal
import geopandas as gpd
from rivgraph.io_utils import write_geotiff as wg
import rabpro
from matplotlib import pyplot as plt

sys.path.append("hydropop")
import hp_class as hpc
import hp_utils as hut
import gee_stats as gee
import rivgraph_ports as wg

""" Adjustable parameters """
# HPU creation parameters
### """ Adjustable parameters """
## HPU creation parameters
# fmt: off
pop_breaks = [-11, -10, -4, 0, 100] # coarse = [-11, -10, -4, 0, 100], fine = [-11, -10, -4, -1, 1, 2, 100]
hthi_breaks = [-.01, .4, .7, 1.01] # coarse = [-.01, .4, .7, 1.01], fine = [-.01, 0.3, 0.55, 0.75, 0.9, 1.01]
# fmt: on
min_hpu_size = 20 # in pixels - each HPU will have at least this many pixels
target_hpu_size = 300 # in pixels - not guaranteed, but will try to make each HPU this size

# Path parameters
path_bounding_box = r"X:\Research\CIMMID\Data\Hydropop Layers\Finals\toronto_coarse\roi.gpkg" # shapefile of ROI
path_results = r'X:\Research\CIMMID\Data\Hydropop Layers\Finals\toronto_new_hpu_method' # folder to store results
## Path parameters
path_bounding_box = r"data/roi.gpkg" # shapefile of ROI
path_results = r'results' # folder to store results
run_name = 'toronto_new_method' # string to prepend to exports
gee_asset = 'projects/cimmid/assets/toronto_coarse_hpus' # the asset path to the hydropop shapefile--this might not be known beforehand but is created upon asset loading to GEE
gdrive_folder_name = 'CIMMID_{}'.format(run_name)

""" Pseduo-fixed parameters/variables """
## Pseduo-fixed parameters/variables """
# Paths to data
path_hthi = r"X:\Research\CIMMID\Data\Hydropop Layers\Hydrotopo Index\hydrotopo_hab_index.tif"
path_pop = r"X:\Research\CIMMID\Data\Hydropop Layers\pop_density_americas.tif"
path_gee_csvs = r'X:\Research\CIMMID\Data\Hydropop Layers\Finals\toronto_new_hpu_method\gee'
path_hthi = r"data/hydrotopo_hab_index.tif"
path_pop = r"data/pop_density_americas.tif"
path_gee_csvs = r'results/toronto_new_hpu_method/gee'

""" Here we go """
## Here we go
paths = hut.prepare_export_paths(path_results, run_name)

# Ensure results folder exists
Expand Down Expand Up @@ -154,86 +148,73 @@
watersheds = gpd.read_file(path_watersheds)
df = hut.overlay_watersheds(hpus, watersheds)
df.to_csv(paths['gages'], index=False)





"""
Creates a random colormap to be used together with matplotlib. Useful for segmentation tasks
:param nlabels: Number of labels (size of colormap)
:param type: 'bright' for strong colors, 'soft' for pastel colors
:param first_color_black: Option to use first color as black, True or False
:param last_color_black: Option to use last color as black, True or False
:param verbose: Prints the number of labels and shows the colormap. True or False
:return: colormap for matplotlib
"""
from matplotlib.colors import LinearSegmentedColormap
import colorsys
import numpy as np


if type not in ('bright', 'soft'):
print ('Please choose "bright" or "soft" for type')
return

if verbose:
print('Number of labels: ' + str(nlabels))

# Generate color map for bright colors, based on hsv
if type == 'bright':
randHSVcolors = [(np.random.uniform(low=0.0, high=1),
np.random.uniform(low=0.2, high=1),
np.random.uniform(low=0.9, high=1)) for i in range(nlabels)]

# Convert HSV list to RGB
randRGBcolors = []
for HSVcolor in randHSVcolors:
randRGBcolors.append(colorsys.hsv_to_rgb(HSVcolor[0], HSVcolor[1], HSVcolor[2]))

if first_color_black:
randRGBcolors[0] = [0, 0, 0]

if last_color_black:
randRGBcolors[-1] = [0, 0, 0]

random_colormap = LinearSegmentedColormap.from_list('new_map', randRGBcolors, N=nlabels)

# Generate soft pastel colors, by limiting the RGB spectrum
if type == 'soft':
low = 0.6
high = 0.95
randRGBcolors = [(np.random.uniform(low=low, high=high),
np.random.uniform(low=low, high=high),
np.random.uniform(low=low, high=high)) for i in range(nlabels)]

if first_color_black:
randRGBcolors[0] = [0, 0, 0]

if last_color_black:
randRGBcolors[-1] = [0, 0, 0]
random_colormap = LinearSegmentedColormap.from_list('new_map', randRGBcolors, N=nlabels)

# Display colorbar
if verbose:
from matplotlib import colors, colorbar
from matplotlib import pyplot as plt
fig, ax = plt.subplots(1, 1, figsize=(15, 0.5))

bounds = np.linspace(0, nlabels, nlabels + 1)
norm = colors.BoundaryNorm(bounds, nlabels)

cb = colorbar.ColorbarBase(ax, cmap=random_colormap, norm=norm, spacing='proportional', ticks=None,
boundaries=bounds, format='%1i', orientation=u'horizontal')

return random_colormap










# from matplotlib import pyplot as plt
# """
# Creates a random colormap to be used together with matplotlib. Useful for segmentation tasks
# :param nlabels: Number of labels (size of colormap)
# :param type: 'bright' for strong colors, 'soft' for pastel colors
# :param first_color_black: Option to use first color as black, True or False
# :param last_color_black: Option to use last color as black, True or False
# :param verbose: Prints the number of labels and shows the colormap. True or False
# :return: colormap for matplotlib
# """
# from matplotlib.colors import LinearSegmentedColormap
# import colorsys
# import numpy as np


# if type not in ('bright', 'soft'):
# print ('Please choose "bright" or "soft" for type')
# return

# if verbose:
# print('Number of labels: ' + str(nlabels))

# # Generate color map for bright colors, based on hsv
# if type == 'bright':
# randHSVcolors = [(np.random.uniform(low=0.0, high=1),
# np.random.uniform(low=0.2, high=1),
# np.random.uniform(low=0.9, high=1)) for i in range(nlabels)]

# # Convert HSV list to RGB
# randRGBcolors = []
# for HSVcolor in randHSVcolors:
# randRGBcolors.append(colorsys.hsv_to_rgb(HSVcolor[0], HSVcolor[1], HSVcolor[2]))

# if first_color_black:
# randRGBcolors[0] = [0, 0, 0]

# if last_color_black:
# randRGBcolors[-1] = [0, 0, 0]

# random_colormap = LinearSegmentedColormap.from_list('new_map', randRGBcolors, N=nlabels)

# # Generate soft pastel colors, by limiting the RGB spectrum
# if type == 'soft':
# low = 0.6
# high = 0.95
# randRGBcolors = [(np.random.uniform(low=low, high=high),
# np.random.uniform(low=low, high=high),
# np.random.uniform(low=low, high=high)) for i in range(nlabels)]

# if first_color_black:
# randRGBcolors[0] = [0, 0, 0]

# if last_color_black:
# randRGBcolors[-1] = [0, 0, 0]
# random_colormap = LinearSegmentedColormap.from_list('new_map', randRGBcolors, N=nlabels)

# # Display colorbar
# if verbose:
# from matplotlib import colors, colorbar
# from matplotlib import pyplot as plt
# fig, ax = plt.subplots(1, 1, figsize=(15, 0.5))

# bounds = np.linspace(0, nlabels, nlabels + 1)
# norm = colors.BoundaryNorm(bounds, nlabels)

# cb = colorbar.ColorbarBase(ax, cmap=random_colormap, norm=norm, spacing='proportional', ticks=None,
# boundaries=bounds, format='%1i', orientation=u'horizontal')

# return random_colormap
18 changes: 7 additions & 11 deletions hydropop/hp_class.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 9 13:37:22 2020
@author: Jon
"""
import os
import numpy as np
import pandas as pd
import geopandas as gpd
import hydropop.hp_utils as hut
import rivgraph.io_utils as io
from osgeo import gdal
import os
import geopandas as gpd
from scipy.cluster.vq import kmeans2
from skimage.future.graph import RAG
from skimage.graph import RAG
#
import hp_utils as hut
import rivgraph_ports as io


"""
The current HTHI has both large negative integers and nans as nodata.
Expand Down
34 changes: 15 additions & 19 deletions hydropop/hp_utils.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 21 14:22:19 2019
@author: Jon
env: hp
"""
import os
from osgeo import gdal, ogr
import subprocess
import sys
import json
import scipy
import platform
import subprocess
import numpy as np
import pandas as pd
import geopandas as gpd
from pyproj import CRS
from shapely.geometry import MultiPolygon, shape
from shapely.validation import make_valid
from shapely.ops import unary_union
import json
import rivgraph.im_utils as iu
import scipy
import geopandas as gpd
#
from osgeo import gdal, ogr
from math import floor, ceil
from rasterstats import zonal_stats
from rabpro import utils as ru

from rasterstats import zonal_stats
#
from shapely.ops import unary_union
from shapely.validation import make_valid
from shapely.geometry import MultiPolygon, shape
#
sys.path.append("hydropop")
import rivgraph_ports as iu

def hp_paths(basepath, basename):

Expand Down
Loading

0 comments on commit 9f86ec5

Please sign in to comment.