Skip to content

Commit

Permalink
Merge pull request #2 from noaa-ocs-modeling/develop
Browse files Browse the repository at this point in the history
workflow updates and link fixes
  • Loading branch information
zacharyburnett authored Mar 9, 2021
2 parents bb70d72 + 4fd2fbc commit b45a5bc
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 41 deletions.
16 changes: 7 additions & 9 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ on:

jobs:
build_wheels:
name: build wheel on ${{ matrix.os }}
name: Python ${{ matrix.python-version }} wheel on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ ubuntu-latest ]
python-version: [ 3.8 ]
python-version: [ 3.9 ]
steps:
- name: Checkout repository
uses: actions/checkout@v2
Expand All @@ -24,8 +24,8 @@ jobs:
uses: actions/cache@v2
with:
path: ~/.cache/pip
key: ${{ matrix.os }}-pip-${{ matrix.python-version }}-${{ hashFiles('setup.py') }}
restore-keys: ${{ matrix.os }}-pip-${{ matrix.python-version }}-
key: ${{ runner.os }}-pip-${{ matrix.python-version }}-${{ hashFiles('setup.py') }}
restore-keys: ${{ runner.os }}-pip-${{ matrix.python-version }}-
- name: Install dependencies
run: pip install --upgrade pip setuptools wheel
- name: Build wheel
Expand All @@ -42,25 +42,23 @@ jobs:
uses: actions/checkout@v2
- name: Install Python
uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: Package source
run: python setup.py sdist
- name: Save source package
uses: actions/upload-artifact@v2
with:
path: ./dist/*.tar.gz
upload_pypi:
name: publish wheel to PyPI
name: publish to PyPI
needs: [ build_wheels, build_sdist ]
runs-on: ubuntu-latest
steps:
- name: Retrieve wheel
- name: Retrieve wheel(s) and source
uses: actions/download-artifact@v2
with:
name: artifact
path: dist
- name: Upload wheel
- name: Upload wheel(s) and source
uses: pypa/gh-action-pypi-publish@master
with:
user: __token__
Expand Down
15 changes: 9 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ on: [ push ]

jobs:
tests:
name: Python ${{ matrix.python-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ ubuntu-latest, windows-latest ]
python-version: [ 3.8 ]
name: test in Python ${{ matrix.python-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
python-version: [ 3.8, 3.9 ]
steps:
- name: Checkout repository
uses: actions/checkout@v2
Expand All @@ -25,10 +25,13 @@ jobs:
restore-keys: ${{ runner.os }}-pip-${{ matrix.python-version }}-
- name: Update pip
run: python -m pip install --upgrade pip
- name: Install wheel
run: pip install wheel
- name: Run setup.py on Windows
if: contains(matrix.os, 'windows')
run: python setup.py install
- name: Install dependencies
run: |
pip install wheel
pip install -e .[testing]
run: pip install -e .[testing]
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
Expand Down
17 changes: 17 additions & 0 deletions examples/descriptions_within_bounds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import numpy

from seabed.ngdc import NGDCSeabedDescriptions

if __name__ == '__main__':
# define bounds
bounds = numpy.array([[-77, 39], [-75, 40]])

# retrieve a list of the first 5 surveys
first_5_surveys = NGDCSeabedDescriptions.all_surveys()[:5]

# get surveys within specific bounds
seabed_object = NGDCSeabedDescriptions(surveys=first_5_surveys, bounds=bounds)
surveys_within_bounds = seabed_object.surveys

print(f'all surveys: {first_5_surveys}')
print(f'surveys within transformed bounds: {surveys_within_bounds}')
28 changes: 28 additions & 0 deletions examples/descriptions_within_transformed_bounds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import numpy
from pyproj import CRS, Transformer

from seabed.ngdc import NGDCSeabedDescriptions

if __name__ == '__main__':
# define bounds
bounds = numpy.array([[-77, 39], [-75, 40]])

# transform bounds to desired CRS
source_crs = CRS.from_epsg(4326)
transformed_crs = CRS.from_epsg(32618)
transformed_bounds = numpy.ravel(
numpy.stack(
Transformer.from_crs(source_crs, transformed_crs).transform(bounds[:, 0], bounds[:, 1]),
axis=1,
)
)

# retrieve a list of the first 5 surveys
first_5_surveys = NGDCSeabedDescriptions.all_surveys()[:5]

# get surveys within specific bounds
seabed_object = NGDCSeabedDescriptions(surveys=first_5_surveys, bounds=transformed_bounds, crs=transformed_crs)
surveys_within_bounds = seabed_object.surveys

print(f'all surveys: {first_5_surveys}')
print(f'surveys within transformed bounds: {surveys_within_bounds}')
41 changes: 34 additions & 7 deletions seabed/base.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from abc import ABC, abstractmethod
import os
from os import PathLike
from pathlib import Path
from typing import Union

from geopandas import GeoDataFrame
import numpy
from pyproj import CRS

from .utilities import get_logger
Expand All @@ -22,8 +24,29 @@ def __init__(
crs: CRS = None,
):
self.bounds = bounds
self.__surveys = surveys
self.crs = CRS.from_user_input(crs) if crs is not None else None
self.surveys = surveys
self.crs = crs

@property
def bounds(self) -> (float, float, float, float):
return self.__bounds

@bounds.setter
def bounds(self, bounds: (float, float, float, float)):
if isinstance(bounds, numpy.ndarray):
if len(bounds.shape) > 1:
bounds = bounds.ravel()
else:
bounds = numpy.asarray(bounds)
self.__bounds = bounds

@property
def crs(self) -> CRS:
return self.__crs

@crs.setter
def crs(self, crs: Union[CRS, str]):
self.__crs = CRS.from_user_input(crs) if crs is not None else None

@classmethod
@abstractmethod
Expand All @@ -32,10 +55,12 @@ def all_surveys(cls) -> [str]:

@property
def surveys(self) -> [str]:
if self.__surveys is None:
self.__surveys = self.__class__.all_surveys()
return self.__surveys

@surveys.setter
def surveys(self, surveys: [str]):
self.__surveys = surveys if surveys is not None else self.__class__.all_surveys()

def __getitem__(self, survey: str) -> GeoDataFrame:
raise NotImplementedError

Expand Down Expand Up @@ -64,7 +89,9 @@ def write(self, filename: PathLike, **kwargs):
'.xml': 'GML',
}

extension = os.path.splitext(filename)[-1]
kwargs['driver'] = drivers[extension]
if not isinstance(filename, Path):
filename = Path(filename)

kwargs['driver'] = drivers[filename.suffix]

self.data.to_file(str(filename), **kwargs)
1 change: 0 additions & 1 deletion seabed/ngdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def __init__(
@classmethod
def all_surveys(cls) -> [str]:
response = requests.get(URL)
print(response.status_code)
if response.status_code != 200:
raise ConnectionError(
f'abnormal status "{response.status_code}" when connecting to {URL}'
Expand Down
65 changes: 58 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,72 @@
#!/usr/bin/env python
import importlib
import logging
import os
from pathlib import Path
import subprocess
import sys

from setuptools import config, find_packages, setup

BUILT_PACKAGES = {'fiona': ['gdal'], 'geopandas': ['gdal', 'fiona'], 'numpy': [], 'pyproj': ['proj']}
is_conda = (Path(sys.prefix) / 'conda-meta').exists()

if is_conda:
conda_packages = []
for conda_package in BUILT_PACKAGES:
try:
importlib.import_module(conda_package)
except:
conda_packages.append(conda_package)
if len(conda_packages) > 0:
subprocess.check_call(['conda', 'install', '-y', *conda_packages])

if os.name == 'nt':
for required_package, pipwin_dependencies in BUILT_PACKAGES.items():
try:
importlib.import_module(required_package)
except:
try:
import pipwin
except:
subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'pipwin'])

failed_pipwin_packages = []
for pipwin_package in pipwin_dependencies + [required_package]:
try:
subprocess.check_call([sys.executable, '-m', 'pipwin', 'install', pipwin_package.lower()])
except subprocess.CalledProcessError:
failed_pipwin_packages.append(pipwin_package)

if len(failed_pipwin_packages) > 0:
raise RuntimeError(
f'failed to download or install non-conda Windows build(s) of {" and ".join(failed_pipwin_packages)}; you can either\n'
'1) install within an Anaconda environment, or\n'
f'2) `pip install <file>.whl`, with `<file>.whl` downloaded from {" and ".join("https://www.lfd.uci.edu/~gohlke/pythonlibs/#" + value.lower() for value in failed_pipwin_packages)} for your Python version'
)

try:
from dunamai import Version
except ImportError:
import sys
import subprocess
try:
from dunamai import Version
except:
import sys
import subprocess

subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'dunamai'])
from dunamai import Version

version = Version.from_any_vcs().serialize()
except RuntimeError as error:
logging.exception(error)
version = '0.0.0'

subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'dunamai'])
from dunamai import Version
logging.info(f'using version {version}')

metadata = config.read_configuration('setup.cfg')['metadata']

setup(
name=metadata['name'],
version=Version.from_any_vcs().serialize(),
version=version,
author=metadata['author'],
author_email=metadata['author_email'],
description=metadata['description'],
Expand Down
37 changes: 26 additions & 11 deletions tests/test_seabed.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,35 @@

from seabed.ngdc import NGDCSeabedDescriptions

CRS = CRS.from_epsg(32618)

BOUNDS = numpy.array([[-77, 39], [-75, 40]])
BOUNDS = numpy.ravel(
numpy.stack(
Transformer.from_crs(CRS.from_epsg(4326), CRS).transform(BOUNDS[:, 0], BOUNDS[:, 1]),
axis=1,
)
)
def test_surveys():
surveys = NGDCSeabedDescriptions.all_surveys()

assert len(surveys) > 0


def test_seabed_descriptions_within_bounds():
bounds = numpy.array([[-77, 39], [-75, 40]])
first_5_surveys = NGDCSeabedDescriptions.all_surveys()[:5]
seabed = NGDCSeabedDescriptions(surveys=first_5_surveys, bounds=bounds)

assert seabed.data.shape[0] > 0
assert seabed.data.shape[1] == 14
assert len(seabed.descriptions) > 0
assert any(seabed.data['Survey'].isin(['492']))


def test_seabed_descriptions():
surveys = NGDCSeabedDescriptions.all_surveys()[:5]
seabed = NGDCSeabedDescriptions(bounds=BOUNDS, surveys=surveys, crs=CRS)
def test_seabed_descriptions_within_transformed_bounds():
bounds = numpy.array([[-77, 39], [-75, 40]])
transformed_crs = CRS.from_epsg(32618)
transformed_bounds = numpy.ravel(
numpy.stack(
Transformer.from_crs(CRS.from_epsg(4326), transformed_crs).transform(bounds[:, 0], bounds[:, 1]),
axis=1,
)
)
first_5_surveys = NGDCSeabedDescriptions.all_surveys()[:5]
seabed = NGDCSeabedDescriptions(surveys=first_5_surveys, bounds=transformed_bounds, crs=transformed_crs)

assert seabed.data.shape[0] > 0
assert seabed.data.shape[1] == 14
Expand Down

0 comments on commit b45a5bc

Please sign in to comment.