Skip to content

Commit

Permalink
Merge pull request #49 from xylar/add_github_actions
Browse files Browse the repository at this point in the history
Improve code quality and CI
  • Loading branch information
xylar authored Feb 6, 2023
2 parents 550b8ad + 6a83cb5 commit 9199add
Show file tree
Hide file tree
Showing 21 changed files with 283 additions and 127 deletions.
98 changes: 98 additions & 0 deletions .github/workflows/build_workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: CI/CD Build Workflow

on:
push:
branches: [main]

pull_request:
branches: [main]

workflow_dispatch:

env:
CANCEL_OTHERS: true
PATHS_IGNORE: '["**/README.md", "**/docs/**", "**/examples/**"]'

jobs:
pre-commit-hooks:
runs-on: ubuntu-latest
timeout-minutes: 2
steps:
- id: skip_check
uses: fkirc/skip-duplicate-actions@master
with:
cancel_others: ${{ env.CANCEL_OTHERS }}
paths_ignore: ${{ env.PATHS_IGNORE }}

- if: ${{ steps.skip_check.outputs.should_skip != 'true' }}
name: Checkout Code Repository
uses: actions/checkout@v3

- if: ${{ steps.skip_check.outputs.should_skip != 'true' }}
name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"

- if: ${{ steps.skip_check.outputs.should_skip != 'true' }}
# Run all pre-commit hooks on all the files.
# Getting only staged files can be tricky in case a new PR is opened
# since the action is run on a branch in detached head state
name: Install and Run Pre-commit
uses: pre-commit/[email protected]

build:
name: Build (Python ${{ matrix.python-version }})
runs-on: "ubuntu-latest"
timeout-minutes: 20
defaults:
run:
shell: bash -l {0}
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11"]
steps:
- id: skip_check
uses: fkirc/skip-duplicate-actions@master
with:
cancel_others: ${{ env.CANCEL_OTHERS }}
paths_ignore: ${{ env.PATHS_IGNORE }}

- if: ${{ steps.skip_check.outputs.should_skip != 'true' }}
uses: actions/checkout@v3

- if: ${{ steps.skip_check.outputs.should_skip != 'true' }}
name: Cache Conda
uses: actions/cache@v3
env:
# Increase this value to reset cache if conda-env/ci.yml has not changed in the workflow
CACHE_NUMBER: 0
with:
path: ~/conda_pkgs_dir
key:
${{ runner.os }}-${{ matrix.python-version }}-conda-${{ env.CACHE_NUMBER }}-${{
hashFiles('conda-env/dev.yml') }}

- if: ${{ steps.skip_check.outputs.should_skip != 'true' }}
name: Set up Conda Environment
uses: conda-incubator/setup-miniconda@v2
with:
activate-environment: "pyremap_ci"
channel-priority: strict
auto-update-conda: true
# IMPORTANT: This needs to be set for caching to work properly!
use-only-tar-bz2: true
python-version: ${{ matrix.python-version }}
run: mamba install --file dev-spec.txt


- if: ${{ steps.skip_check.outputs.should_skip != 'true' }}
name: Install pyremap
run: python -m pip install .

- if: ${{ steps.skip_check.outputs.should_skip != 'true' }}
name: Run Tests
env:
CHECK_IMAGES: False
run: pytest --pyargs pyremap

25 changes: 0 additions & 25 deletions .github/workflows/lint.yml

This file was deleted.

27 changes: 27 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
exclude: "docs|ci|.git|examples|licenses|conda/meta.yaml"
default_stages: [commit]
fail_fast: true

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml

# Can run individually with `pre-commit run isort --all-files`
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort

# Can run individually with `pre-commit run flake8 --all-files`
# Need to use flake8 GitHub mirror due to CentOS git issue with GitLab
# https://github.com/pre-commit/pre-commit/issues/1206
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
args: ["--config=setup.cfg"]
additional_dependencies: [flake8-isort]
3 changes: 3 additions & 0 deletions dev-spec.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ xarray >=0.10.0
# Development
pip
pytest
isort
flake8
pre-commit

# Documentation
mock
Expand Down
1 change: 0 additions & 1 deletion flake8.cfg

This file was deleted.

16 changes: 10 additions & 6 deletions pyremap/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from pyremap.descriptor import MpasMeshDescriptor, MpasEdgeMeshDescriptor,\
LatLonGridDescriptor, LatLon2DGridDescriptor, ProjectionGridDescriptor, \
PointCollectionDescriptor, get_lat_lon_descriptor

from pyremap.descriptor import (
LatLon2DGridDescriptor,
LatLonGridDescriptor,
MpasEdgeMeshDescriptor,
MpasMeshDescriptor,
PointCollectionDescriptor,
ProjectionGridDescriptor,
get_lat_lon_descriptor,
)
from pyremap.polar import get_polar_descriptor, get_polar_descriptor_from_file
from pyremap.remapper import Remapper

from pyremap.polar import get_polar_descriptor_from_file, get_polar_descriptor

__version_info__ = (0, 0, 15)
__version__ = '.'.join(str(vi) for vi in __version_info__)
30 changes: 18 additions & 12 deletions pyremap/descriptor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@
# distributed with this code, or at
# https://raw.githubusercontent.com/MPAS-Dev/pyremap/main/LICENSE

from pyremap.descriptor.lat_lon_2d_grid_descriptor import (
LatLon2DGridDescriptor,
)
from pyremap.descriptor.lat_lon_grid_descriptor import (
LatLonGridDescriptor,
get_lat_lon_descriptor,
)
from pyremap.descriptor.mesh_descriptor import MeshDescriptor
from pyremap.descriptor.mpas_edge_mesh_descriptor import MpasEdgeMeshDescriptor
from pyremap.descriptor.mpas_mesh_descriptor import MpasMeshDescriptor
from pyremap.descriptor.mpas_edge_mesh_descriptor import \
MpasEdgeMeshDescriptor
from pyremap.descriptor.lat_lon_grid_descriptor import LatLonGridDescriptor, \
get_lat_lon_descriptor
from pyremap.descriptor.lat_lon_2d_grid_descriptor import \
LatLon2DGridDescriptor
from pyremap.descriptor.projection_grid_descriptor import \
ProjectionGridDescriptor
from pyremap.descriptor.point_collection_descriptor import \
PointCollectionDescriptor
from pyremap.descriptor.utility import interp_extrap_corner, \
interp_extrap_corners_2d
from pyremap.descriptor.point_collection_descriptor import (
PointCollectionDescriptor,
)
from pyremap.descriptor.projection_grid_descriptor import (
ProjectionGridDescriptor,
)
from pyremap.descriptor.utility import (
interp_extrap_corner,
interp_extrap_corners_2d,
)
11 changes: 8 additions & 3 deletions pyremap/descriptor/lat_lon_2d_grid_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@
# distributed with this code, or at
# https://raw.githubusercontent.com/MPAS-Dev/pyremap/main/LICENSE

import sys

import netCDF4
import numpy
import sys
import xarray

from pyremap.descriptor.mesh_descriptor import MeshDescriptor
from pyremap.descriptor.utility import interp_extrap_corners_2d, \
create_scrip, unwrap_corners, round_res
from pyremap.descriptor.utility import (
create_scrip,
interp_extrap_corners_2d,
round_res,
unwrap_corners,
)


class LatLon2DGridDescriptor(MeshDescriptor):
Expand Down
11 changes: 8 additions & 3 deletions pyremap/descriptor/lat_lon_grid_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@
# distributed with this code, or at
# https://raw.githubusercontent.com/MPAS-Dev/pyremap/main/LICENSE

import sys

import netCDF4
import numpy
import sys
import xarray

from pyremap.descriptor.mesh_descriptor import MeshDescriptor
from pyremap.descriptor.utility import interp_extrap_corner, \
create_scrip, unwrap_corners, round_res
from pyremap.descriptor.utility import (
create_scrip,
interp_extrap_corner,
round_res,
unwrap_corners,
)


def get_lat_lon_descriptor(dLon, dLat, lonMin=-180., lonMax=180., latMin=-90.,
Expand Down
6 changes: 4 additions & 2 deletions pyremap/descriptor/mesh_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def to_scrip(self, scripFileName):
scripFileName : str
The path to which the SCRIP file should be written
"""
raise NotImplemented('to_scrip is not implemented for this descriptor')
raise NotImplementedError(
'to_scrip is not implemented for this descriptor')

def to_esmf(self, esmfFileName):
"""
Expand All @@ -73,4 +74,5 @@ def to_esmf(self, esmfFileName):
esmfFileName : str
The path to which the ESMF mesh file should be written
"""
raise NotImplemented('to_esmf is not implemented for this descriptor')
raise NotImplementedError(
'to_esmf is not implemented for this descriptor')
13 changes: 7 additions & 6 deletions pyremap/descriptor/mpas_edge_mesh_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
# distributed with this code, or at
# https://raw.githubusercontent.com/MPAS-Dev/pyremap/main/LICENSE

import sys

import netCDF4
import numpy as np
import sys
import xarray as xr

from pyremap.descriptor.mesh_descriptor import MeshDescriptor
Expand Down Expand Up @@ -104,7 +105,7 @@ def to_scrip(self, scripFileName):
grid_area = outFile.createVariable('grid_area', 'f8', ('grid_size',))
grid_area.units = 'radian^2'
# a triangle if there is 1 valid cell and a diamond if there are 2
areaEdge = 0.5*validCellsOnEdge*dcEdge*dvEdge
areaEdge = 0.5 * validCellsOnEdge * dcEdge * dvEdge
# SCRIP uses square radians
grid_area[:] = areaEdge[:] / (sphereRadius**2)

Expand All @@ -118,12 +119,12 @@ def to_scrip(self, scripFileName):
grid_corner_lat = np.zeros((nEdges, 4))

# start by repeating vertices, since these always exist
vertices = verticesOnEdge[:, 0]-1
vertices = verticesOnEdge[:, 0] - 1
grid_corner_lon[:, 0] = lonVertex[vertices]
grid_corner_lat[:, 0] = latVertex[vertices]
grid_corner_lon[:, 1] = lonVertex[vertices]
grid_corner_lat[:, 1] = latVertex[vertices]
vertices = verticesOnEdge[:, 1]-1
vertices = verticesOnEdge[:, 1] - 1
grid_corner_lon[:, 2] = lonVertex[vertices]
grid_corner_lat[:, 2] = latVertex[vertices]
grid_corner_lon[:, 3] = lonVertex[vertices]
Expand Down Expand Up @@ -165,7 +166,7 @@ def to_esmf(self, esmfFileName):
with xr.open_dataset(self.fileName) as ds:

nCells = ds.sizes['nCells']
nodeCount = nCells+ds.sizes['nVertices']
nodeCount = nCells + ds.sizes['nVertices']
elementCount = ds.sizes['nEdges']
coordDim = 2

Expand Down Expand Up @@ -193,7 +194,7 @@ def to_esmf(self, esmfFileName):
for vertex in range(3):
# swap -1 value until they're at the end
mask = elementConn[:, vertex] == -1
elementConn[mask, vertex] = elementConn[mask, vertex+1]
elementConn[mask, vertex] = elementConn[mask, vertex + 1]
elementConn[mask, vertex + 1] = -1

numElementConn = np.sum((elementConn != -1), axis=1).astype(np.byte)
Expand Down
3 changes: 2 additions & 1 deletion pyremap/descriptor/mpas_mesh_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
# distributed with this code, or at
# https://raw.githubusercontent.com/MPAS-Dev/pyremap/main/LICENSE

import sys

import netCDF4
import numpy
import sys
import xarray

from pyremap.descriptor.mesh_descriptor import MeshDescriptor
Expand Down
5 changes: 3 additions & 2 deletions pyremap/descriptor/point_collection_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
# distributed with this code, or at
# https://raw.githubusercontent.com/MPAS-Dev/pyremap/main/LICENSE

import sys

import netCDF4
import numpy
import sys
import xarray

from pyremap.descriptor.mesh_descriptor import MeshDescriptor
Expand Down Expand Up @@ -129,7 +130,7 @@ def to_esmf(self, esmfFileName):
nPoints = len(self.lat)

elementCount = nPoints
nodeCount = 3*nPoints
nodeCount = 3 * nPoints
coordDim = 2
maxNodePElement = 3

Expand Down
10 changes: 7 additions & 3 deletions pyremap/descriptor/projection_grid_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@
# distributed with this code, or at
# https://raw.githubusercontent.com/MPAS-Dev/pyremap/main/LICENSE

import sys

import netCDF4
import numpy
import sys
import pyproj
import xarray

from pyremap.descriptor.mesh_descriptor import MeshDescriptor
from pyremap.descriptor.utility import interp_extrap_corner, \
create_scrip, unwrap_corners
from pyremap.descriptor.utility import (
create_scrip,
interp_extrap_corner,
unwrap_corners,
)


class ProjectionGridDescriptor(MeshDescriptor):
Expand Down
Loading

0 comments on commit 9199add

Please sign in to comment.