Skip to content

Commit

Permalink
Add flake8 to earthpy (#520)
Browse files Browse the repository at this point in the history
* add flake 8

* update change log

* black

* not in

* flake 8

* docstring update
  • Loading branch information
Leah Wasser committed Mar 26, 2020
1 parent e4bc8c5 commit 96657a1
Show file tree
Hide file tree
Showing 18 changed files with 140 additions and 94 deletions.
7 changes: 6 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ repos:
rev: stable
hooks:
- id: black
language_version: python3.6
language_version: python3.7
- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.7
hooks:
- id: flake8
language: python_venv
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
* Add flake 8 (@lwasser, #519)
* Remove conda envt for RTD build (@lwasser, #518)
* Deprecate clip function from earthpy given it's moved to geopandas now (@nkorinek, #510)
* Fix twitter flood data key in get_data (@lwasser, #512)
Expand Down
2 changes: 2 additions & 0 deletions earthpy/clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"""

import sys


# @deprecate
def _clip_points(shp, clip_obj):
Expand Down
11 changes: 9 additions & 2 deletions earthpy/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

# Data URLs, structured as {'week_name': [(URL, FILENAME, FILETYPE)]}
# If zipfile, tarfile, etc, unzip to a folder w/ the name

DATA_URLS = {
"co-flood-extras": [
(
Expand Down Expand Up @@ -51,12 +52,18 @@
"zip",
),
"cs-test-naip": (
"https://ndownloader.figshare.com/files/10960211?private_link=18f892d9f3645344b2fe",
(
"https://ndownloader.figshare.com/files/10960211?"
"private_link=18f892d9f3645344b2fe"
),
".",
"zip",
),
"cs-test-landsat": (
"https://ndownloader.figshare.com/files/10960214?private_link=fbba903d00e1848b423e",
(
"https://ndownloader.figshare.com/files/10960214?private_link"
"=fbba903d00e1848b423e"
),
".",
"zip",
),
Expand Down
23 changes: 13 additions & 10 deletions earthpy/mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,16 @@ def _create_mask(mask_arr, vals):
Returns
-----------
arr : numpy array
A numpy array populated with 1's where the mask is applied (a Boolean True)
and a 0 where no masking will be done.
A numpy array populated with 1's where the mask is applied (a Boolean
True) and a 0 where no masking will be done.
"""

try:
vals.sort()
except AttributeError:
raise AttributeError("Values should be provided as a list")

# For some reason if you don't copy this here, it magically changes the input
# For some reason if you don't copy this here, it changes the input
# qa layer to a boolean in the main environment.
new_mask_arr = mask_arr.copy()
unique_vals = np.unique(new_mask_arr).tolist()
Expand Down Expand Up @@ -152,7 +152,8 @@ def _apply_mask(arr, input_mask):
Returns
-----------
numpy array
The original numpy array with the mask applied to cover up issue pixels.
The original numpy array with the mask applied to cover up issue
pixels.
"""

# Test if input_mask is numpy array w values == 1 for masked
Expand All @@ -172,10 +173,11 @@ def _apply_mask(arr, input_mask):
def mask_pixels(arr, mask_arr, vals=None):
"""Apply a mask to an input array.
Masks values in an n-dimensional input array (arr) based on input 1-dimensional
array (mask_arr). If mask_arr is provided in a boolean format, it is used as a mask.
If mask_arr is provided as a non-boolean format and values to mask (vals) are provided,
a Boolean masked array is created from the mask_arr and the indicated vals to mask, and
Masks values in an n-dimensional input array (arr) based on input
1-dimensional array (mask_arr). If mask_arr is provided in a boolean
format, it is used as a mask. If mask_arr is provided as a non-boolean
format and values to mask (vals) are provided, a Boolean masked array is
created from the mask_arr and the indicated vals to mask, and
then this new 1-dimensional masked array is used to mask the input arr.
This function is useful when masking cloud and other unwanted pixels.
Expand All @@ -194,8 +196,9 @@ def mask_pixels(arr, mask_arr, vals=None):
Returns
-------
arr : numpy array
A numpy array populated with 1's where the mask is applied (a Boolean True)
and the original numpy array's value where no masking was done.
A numpy array populated with 1's where the mask is applied (a
Boolean True) and the original numpy array's value where no masking
was done.
Example
-------
Expand Down
51 changes: 28 additions & 23 deletions earthpy/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
def colorbar(mapobj, size="3%", pad=0.09):
"""Adjust colorbar height to match the matplotlib axis height.
NOTE: This function requires matplotlib v 3.0.1 or greater or v 2.9 or lower to run properly.
NOTE: This function requires matplotlib v 3.0.1 or greater or v 2.9 or
lower to run properly.
Parameters
----------
mapobj : matplotlib axis object
The image that the colorbar will be representing as a matplotlib axis object.
The image that the colorbar will be representing as a matplotlib axis
object.
size : char (default = "3%")
The percent width of the colorbar relative to the plot.
pad : int (default = 0.09)
Expand Down Expand Up @@ -109,13 +111,13 @@ def _plot_image(
ax : Matplotlib axes object (Optional)
Matplotlib axis object to plot image.
alpha : float (optional)
The alpha value for the plot. This will help adjust the transparency of the
plot to the desired level.
The alpha value for the plot. This will help adjust the transparency of
the plot to the desired level.
norm : matplotlib Normalize object (Optional)
The normalized boundaries for custom values coloring. NOTE: For this argument
to work, the scale argument MUST be set to false. Otherwise, the values will
just be scaled from 0-255
The normalized boundaries for custom values coloring. NOTE: For this
argument to work, the scale argument MUST be set to false. Otherwise,
the values will just be scaled from 0-255
Returns
----------
ax : matplotlib.axes object
Expand Down Expand Up @@ -185,13 +187,13 @@ def plot_bands(
vmax : Int (Optional)
Specify the vmax to scale imshow() plots.
alpha : float (optional)
The alpha value for the plot. This will help adjust the transparency of the
plot to the desired level.
The alpha value for the plot. This will help adjust the transparency
of the plot to the desired level.
norm : matplotlib Normalize object (Optional)
The normalized boundaries for custom values coloring. NOTE: For this argument
to work, the scale argument MUST be set to false. Because of this, the
function will automatically set scale to false, even if the user manually
sets scale to true.
The normalized boundaries for custom values coloring. NOTE: For this
argument to work, the scale argument MUST be set to false. Because
of this, the function will automatically set scale to false,
even if the user manually sets scale to true.
Returns
----------
Expand Down Expand Up @@ -366,7 +368,8 @@ def plot_rgb(
title : string (optional)
The intended title of the plot.
stretch : Boolean (optional)
Application of a linear stretch. If set to True, a linear stretch will be applied.
Application of a linear stretch. If set to True, a linear stretch will
be applied.
Returns
----------
Expand All @@ -384,7 +387,7 @@ def plot_rgb(
>>> from earthpy.io import path_to_example
>>> with rio.open(path_to_example('rmnp-rgb.tif')) as src:
... img_array = src.read()
>>> # Before you plot, ensure that the input array does not have nodata values like -9999
>>> # Ensure the input array doesn't have nodata values like -9999
>>> ep.plot_rgb(img_array)
<matplotlib.axes._subplots.AxesSubplot object at 0x...
Expand Down Expand Up @@ -468,12 +471,12 @@ def hist(
ylabel : str (optional)
The text to print on the y axis.
hist_range : tuple (optional)
The lower and upper range of the bins. Lower and upper outliers are ignored.
If not provided, range is (x.min(), x.max()).
The lower and upper range of the bins. Lower and upper outliers are
ignored. If not provided, range is (x.min(), x.max()).
Range has no effect if bins is a sequence.
alpha : float (optional)
The alpha value for the plot. This will help adjust the transparency of the
plot to the desired level.
The alpha value for the plot. This will help adjust the transparency
of the plot to the desired level.
Returns
----------
Expand Down Expand Up @@ -566,7 +569,8 @@ def hist(
if title:
if len(title) > 1:
raise ValueError(
"You have one array to plot but more than one title. Please provide a single title value."
"You have one array to plot but more than one title. "
"Please provide a single title value."
)

# Plot all bands
Expand All @@ -591,7 +595,8 @@ def hist(

def make_col_list(unique_vals, nclasses=None, cmap=None):
"""
Convert a matplotlib named colormap into a discrete list of n-colors in RGB format.
Convert a matplotlib named colormap into a discrete list of n-colors in
RGB format.
Parameters
----------
Expand Down Expand Up @@ -732,7 +737,7 @@ def draw_legend(im_ax, bbox=(1.05, 1), titles=None, cmap=None, classes=None):
)

patches = [
mpatches.Patch(color=colors[i], label="{l}".format(l=titles[i]))
mpatches.Patch(color=colors[i], label="{lab}".format(lab=titles[i]))
for i in range(len(titles))
]
# Get the axis for the legend
Expand Down
54 changes: 30 additions & 24 deletions earthpy/spatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,22 @@ def extent_to_json(ext_obj):


def normalized_diff(b1, b2):
"""Take two n-dimensional numpy arrays and calculate the normalized difference.
"""Take two n-dimensional numpy arrays and calculate the normalized
difference.
Math will be calculated (b1-b2) / (b1 + b2).
Parameters
----------
b1, b2 : numpy arrays
Two numpy arrays that will be used to calculate the normalized difference.
Math will be calculated (b1-b2) / (b1+b2).
Two numpy arrays that will be used to calculate the normalized
difference. Math will be calculated (b1-b2) / (b1+b2).
Returns
----------
n_diff : numpy array
The element-wise result of (b1-b2) / (b1+b2) calculation. Inf values are set
to nan. Array returned as masked if result includes nan values.
The element-wise result of (b1-b2) / (b1+b2) calculation. Inf values
are set to nan. Array returned as masked if result includes nan values.
Examples
--------
Expand Down Expand Up @@ -106,7 +107,8 @@ def normalized_diff(b1, b2):
# Set inf values to nan and provide custom warning
if np.isinf(n_diff).any():
warnings.warn(
"Divide by zero produced infinity values that will be replaced with nan values",
"Divide by zero produced infinity values that will be replaced "
"with nan values",
Warning,
)
n_diff[np.isinf(n_diff)] = np.nan
Expand All @@ -130,7 +132,8 @@ def stack(band_paths, out_path="", nodata=None):
A path with a file name for the output stacked raster
tif file.
nodata : numeric (optional)
A value (int or float) that represents invalid or missing values to mask in the output.
A value (int or float) that represents invalid or missing values to
mask in the output.
Returns
----------
Expand All @@ -139,8 +142,8 @@ def stack(band_paths, out_path="", nodata=None):
numpy array
N-dimensional array created by stacking the raster files provided.
rasterio profile object
A rasterio profile object containing the updated spatial metadata for
the stacked numpy array.
A rasterio profile object containing the updated spatial metadata
for the stacked numpy array.
Example
-------
Expand Down Expand Up @@ -171,7 +174,8 @@ def stack(band_paths, out_path="", nodata=None):

if len(band_paths) < 2:
raise ValueError(
"The list of file paths is empty. You need at least 2 files to create a stack."
"The list of file paths is empty. You need at least 2 files to "
"create a stack."
)

# Invalid filename specified and write_raster == True.
Expand Down Expand Up @@ -211,7 +215,8 @@ def stack(band_paths, out_path="", nodata=None):

if not len(set(dest_shps)) == 1:
raise ValueError(
"Please ensure all source rasters have same dimensions (nrows, ncols)."
"Please ensure all source rasters have same dimensions "
"(nrows, ncols)."
)

# Update band count
Expand Down Expand Up @@ -240,13 +245,12 @@ def stack(band_paths, out_path="", nodata=None):
# Valid output path checked above
file_fmt = os.path.basename(out_path).split(".")[-1]

# Check if the file format for output is the same as the source driver
# Check that file format for output is the same as source driver
rio_driver = sources[0].profile["driver"]
if not file_fmt in rio_driver.lower():
if file_fmt not in rio_driver.lower():
raise ValueError(
"Source data is {}. Please specify corresponding output extension.".format(
rio_driver
)
"Source data is {}. Please specify corresponding output "
"extension.".format(rio_driver)
)

# Write stacked gtif file
Expand Down Expand Up @@ -299,7 +303,7 @@ def _stack_bands(sources, write_raster=False, dest=None):
for src in sources:
src.profile

except AttributeError as ae:
except AttributeError:
raise AttributeError("The sources object should be Dataset Reader")
sys.exit()

Expand Down Expand Up @@ -441,10 +445,10 @@ def crop_all(
>>> import geopandas as gpd
>>> from earthpy.io import path_to_example
>>> band_fnames = ["red.tif", "green.tif", "blue.tif"]
>>> band_paths = [path_to_example(fname) for fname in band_fnames]
>>> paths = [path_to_example(fname) for fname in band_fnames]
>>> rmnp = gpd.read_file(path_to_example("rmnp.shp"))
>>> output_dir = os.path.commonpath(band_paths)
>>> output_files = es.crop_all(band_paths, output_dir, rmnp, overwrite=True)
>>> out_dir = os.path.commonpath(paths)
>>> output_files = es.crop_all(paths, out_dir, rmnp, overwrite=True)
>>> len(output_files)
3
>>> os.path.isfile(output_files[0])
Expand Down Expand Up @@ -545,12 +549,14 @@ def bytescale(data, high=255, low=0, cmin=None, cmax=None):
raise ValueError("`cmax` should be larger than `cmin`.")
elif crange == 0:
raise ValueError(
"`cmax` and `cmin` should not be the same value. Please specify `cmax` > `cmin`"
"`cmax` and `cmin` should not be the same value. Please specify "
"`cmax` > `cmin`"
)

scale = float(high - low) / crange

# If cmax is less than the data max, then this scale parameter will create data > 1.0. clip the data to cmax first.
# If cmax is less than the data max, then this scale parameter will create
# data > 1.0. clip the data to cmax first.
data[data > cmax] = cmax
bytedata = (data - cmin) * scale + low
return (bytedata.clip(low, high) + 0.5).astype("uint8")
Expand All @@ -562,7 +568,7 @@ def hillshade(arr, azimuth=30, altitude=30):
Parameters
----------
arr : numpy array of shape (rows, columns)
Numpy array containing elevation values to be used to created hillshade.
Numpy array with elevation values to be used to created hillshade.
azimuth : float (default=30)
The desired azimuth for the hillshade.
altitude : float (default=30)
Expand Down Expand Up @@ -594,7 +600,7 @@ def hillshade(arr, azimuth=30, altitude=30):
"""
try:
x, y = np.gradient(arr)
except:
except ValueError:
raise ValueError("Input array should be two-dimensional")

if azimuth <= 360.0:
Expand Down

0 comments on commit 96657a1

Please sign in to comment.