Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/USEPA/WNTR into swntr
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklise committed Oct 1, 2024
2 parents 84d5888 + 576dc01 commit d1d0f2d
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 102 deletions.
78 changes: 23 additions & 55 deletions .github/workflows/build_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ jobs:
os: [windows-latest, macOS-13, ubuntu-latest]
fail-fast: false
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
Expand All @@ -37,7 +37,7 @@ jobs:
python setup.py bdist_wheel
ls dist/*
- name: Save wheel
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: wntr_${{ matrix.python-version }}_${{ matrix.os }}.whl
path: dist/wntr*
Expand All @@ -52,11 +52,11 @@ jobs:
fail-fast: false
steps:
- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Download wheel
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: wntr_${{ matrix.python-version }}_${{ matrix.os }}.whl
- name: Install wntr
Expand All @@ -76,9 +76,9 @@ jobs:
os: [windows-latest, macOS-13, ubuntu-latest]
fail-fast: false
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
Expand All @@ -94,33 +94,35 @@ jobs:
coverage run --context=${{ matrix.os }}.py${{ matrix.python-version }} --source=wntr --omit="*/tests/*","*/sim/network_isolation/network_isolation.py","*/sim/aml/evaluator.py" --append -m pytest --doctest-glob="*.rst" documentation
env:
COVERAGE_FILE: .coverage.${{ matrix.python-version }}.${{ matrix.os }}

- name: Save coverage
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: coverage
name: .coverage.${{ matrix.python-version }}.${{ matrix.os }}
path: .coverage.${{ matrix.python-version }}.${{ matrix.os }}
include-hidden-files: true

combine_reports:
needs: [ pytest_coverage ]
runs-on: ubuntu-latest
steps:
- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: 3.11
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Install coverage
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
python -m pip install -e .
# pip install coveralls
pip install coveralls
- name: Download coverage artifacts from test matrix
uses: actions/download-artifact@v2
uses: actions/download-artifact@v4
with:
name: coverage
pattern: .coverage.*.ubuntu-latest # coverage from other OS cause problems
- name: Setup coverage and combine reports
run: coverage combine .coverage.*.ubuntu-latest
- name: Create coverage report
run: |
echo "[paths]" > .coveragerc
echo "source = " >> .coveragerc
Expand All @@ -129,55 +131,21 @@ jobs:
echo " D:\\a\\WNTR\\WNTR\\wntr" >> .coveragerc
echo " /home/runner/work/WNTR/WNTR/wntr" >> .coveragerc
echo " /Users/runner/work/WNTR/WNTR/wntr" >> .coveragerc
coverage combine
- name: Create coverage report
run: |
echo " ${{ github.workspace }}/wntr" >> .coveragerc
coverage report
coverage json --pretty-print
coverage html --show-contexts
- name: Save coverage JSON
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: coverage
name: coverage-json
path: coverage.json
- name: Save coverage html
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: coverage
name: coverage-html
path: htmlcov

combine_reports_upload_coveralls:
needs: [ pytest_coverage ]
runs-on: ubuntu-latest
continue-on-error: true
steps:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.11
- uses: actions/checkout@v2
- name: Install coverage
run: |
python -m pip install --upgrade pip
pip install coveralls
pip install -r requirements.txt
python -m pip install -e .
- name: Download coverage artifacts from test matrix
uses: actions/download-artifact@v2
with:
name: coverage
- name: Setup coverage and combine reports
run: |
echo "[paths]" > .coveragerc
echo "source = " >> .coveragerc
echo " wntr/" >> .coveragerc
echo " wntr\\" >> .coveragerc
echo " D:\\a\\WNTR\\WNTR\\wntr" >> .coveragerc
echo " /home/runner/work/WNTR/WNTR/wntr" >> .coveragerc
echo " /Users/runner/work/WNTR/WNTR/wntr" >> .coveragerc
coverage combine
- name: Push to coveralls
run: |
coveralls --service=github
run: coveralls --service=github --rcfile=.coveragerc
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
54 changes: 39 additions & 15 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,72 @@ on:
jobs:

wheels:
name: Build distribution 📦 on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-2019, macOS-13, ubuntu-20.04]
os: [windows-latest, macOS-13, macos-13, ubuntu-latest]
steps:
- uses: actions/checkout@v3
- name: Harden Runner
uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
with:
egress-policy: audit
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- name: Build wheels
uses: pypa/cibuildwheel@v2.11.1
uses: pypa/cibuildwheel@79b0dd328794e1180a7268444d46cdf12e1abd01 # v2.21.0
env:
CIBW_ENVIRONMENT: BUILD_WNTR_EXTENSIONS='true'
CIBW_BUILD: cp37-* cp38-* cp39-* cp310-* cp311-*
CIBW_BUILD: cp38-* cp39-* cp310-* cp311-* cp312-*
CIBW_SKIP: "*-win32 *-manylinux_i686 pp* *-musllinux*"
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4.4.0
with:
name: cibw-wheels-${{ matrix.os }}-${{ strategy.job-index }}
path: ./wheelhouse/*.whl

source:
name: Make SDist artifact 📦
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- name: Harden Runner
uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
with:
egress-policy: audit

- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: build the sdist
run: |
python -m pip install --upgrade build
python -m build --sdist
- uses: actions/upload-artifact@v3

- name: Build SDist
run: pipx run build --sdist

- uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4.4.0
with:
name: cibw-sdist
path: dist/*.tar.gz

publish-to-pypi:
name: Publish Python 🐍 distribution 📦 to PyPI
needs: [wheels, source]
runs-on: ubuntu-latest
environment:
name: release
permissions:
id-token: write
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
steps:
- uses: actions/download-artifact@v3
- name: Harden Runner
uses: step-security/harden-runner@91182cccc01eb5e619899d80e4e971d6181294a7 # v2.10.1
with:
egress-policy: audit

- uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4.1.8
with:
name: artifact
pattern: cibw-*
path: dist
merge-multiple: true

- uses: pypa/gh-action-pypi-publish@release/v1
- uses: pypa/gh-action-pypi-publish@0ab0b79471669eb3a4d647e625009c62f9f3b241 # release/v1
with:
user: __token__
password: ${{ secrets.PYPI_WNTR_API_TOKEN }}
41 changes: 22 additions & 19 deletions documentation/gis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,13 @@ For example, the junctions GeoDataFrame contains the following information:
:skipif: gpd is None

>>> print(wn_gis.junctions.head())
node_type elevation initial_quality geometry
10 Junction 216.408 5.000e-04 POINT (20.00000 70.00000)
11 Junction 216.408 5.000e-04 POINT (30.00000 70.00000)
12 Junction 213.360 5.000e-04 POINT (50.00000 70.00000)
13 Junction 211.836 5.000e-04 POINT (70.00000 70.00000)
21 Junction 213.360 5.000e-04 POINT (30.00000 40.00000)
node_type elevation initial_quality geometry
name
10 Junction 216.408 5.000e-04 POINT (20.00000 70.00000)
11 Junction 216.408 5.000e-04 POINT (30.00000 70.00000)
12 Junction 213.360 5.000e-04 POINT (50.00000 70.00000)
13 Junction 211.836 5.000e-04 POINT (70.00000 70.00000)
21 Junction 213.360 5.000e-04 POINT (30.00000 40.00000)

Each GeoDataFrame contains attributes and geometry:

Expand Down Expand Up @@ -335,21 +336,23 @@ and then translates the GeoDataFrames coordinates to EPSG:3857.

>>> wn_gis = wntr.network.to_gis(wn, crs='EPSG:4326')
>>> print(wn_gis.junctions.head())
node_type elevation initial_quality geometry
10 Junction 216.408 5.000e-04 POINT (20.00000 70.00000)
11 Junction 216.408 5.000e-04 POINT (30.00000 70.00000)
12 Junction 213.360 5.000e-04 POINT (50.00000 70.00000)
13 Junction 211.836 5.000e-04 POINT (70.00000 70.00000)
21 Junction 213.360 5.000e-04 POINT (30.00000 40.00000)

node_type elevation initial_quality geometry
name
10 Junction 216.408 5.000e-04 POINT (20.00000 70.00000)
11 Junction 216.408 5.000e-04 POINT (30.00000 70.00000)
12 Junction 213.360 5.000e-04 POINT (50.00000 70.00000)
13 Junction 211.836 5.000e-04 POINT (70.00000 70.00000)
21 Junction 213.360 5.000e-04 POINT (30.00000 40.00000)

>>> wn_gis.to_crs('EPSG:3857')
>>> print(wn_gis.junctions.head())
node_type elevation initial_quality geometry
10 Junction 216.408 5.000e-04 POINT (2226389.816 11068715.659)
11 Junction 216.408 5.000e-04 POINT (3339584.724 11068715.659)
12 Junction 213.360 5.000e-04 POINT (5565974.540 11068715.659)
13 Junction 211.836 5.000e-04 POINT (7792364.356 11068715.659)
21 Junction 213.360 5.000e-04 POINT (3339584.724 4865942.280)
node_type elevation initial_quality geometry
name
10 Junction 216.408 5.000e-04 POINT (2226389.816 11068715.659)
11 Junction 216.408 5.000e-04 POINT (3339584.724 11068715.659)
12 Junction 213.360 5.000e-04 POINT (5565974.540 11068715.659)
13 Junction 211.836 5.000e-04 POINT (7792364.356 11068715.659)
21 Junction 213.360 5.000e-04 POINT (3339584.724 4865942.280)

Snap point geometries to the nearest point or line
----------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ folium
utm
openpyxl
geopandas<1.0
fiona<1.10
rtree
pyswmm
swmmio
Expand Down
5 changes: 2 additions & 3 deletions wntr/gis/geospatial.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ def snap(A, B, tolerance):
assert A.crs == B.crs

# Modify B to include "indexB" as a separate column
B = B.reset_index()
B.rename(columns={'index':'indexB'}, inplace=True)
B = B.reset_index(names='indexB')

# Define the coordinate reference system, based on B
crs = B.crs
Expand Down Expand Up @@ -228,7 +227,7 @@ def intersect(A, B, B_value=None, include_background=False, background_value=0):

n = intersects.groupby('_tmp_index_name')['geometry'].count()
B_indices = intersects.groupby('_tmp_index_name')['index_right'].apply(list)
stats = pd.DataFrame(index=A.index, data={'intersections': B_indices,
stats = pd.DataFrame(index=A.index.copy(), data={'intersections': B_indices,
'n': n,})
stats['n'] = stats['n'].fillna(0)
stats['n'] = stats['n'].apply(int)
Expand Down
7 changes: 3 additions & 4 deletions wntr/gis/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ def _extract_geodataframe(df, crs=None, links_as_points=False):
# Set index
if len(df) > 0:
df.set_index('name', inplace=True)
df.index.name = None

df = gpd.GeoDataFrame(df, crs=crs, geometry=geom)
else:
Expand Down Expand Up @@ -300,7 +299,7 @@ def add_link_attributes(self, values, name):
self.pumps[name] = np.nan
self.pumps.loc[link_name, name] = value

def _read(self, files, index_col='index'):
def _read(self, files, index_col='name'):

if 'junctions' in files.keys():
data = gpd.read_file(files['junctions']).set_index(index_col)
Expand All @@ -321,7 +320,7 @@ def _read(self, files, index_col='index'):
data = gpd.read_file(files['valves']).set_index(index_col)
self.valves = pd.concat([self.valves, data])

def read_geojson(self, files, index_col='index'):
def read_geojson(self, files, index_col='name'):
"""
Append information from GeoJSON files to a WaterNetworkGIS object
Expand All @@ -336,7 +335,7 @@ def read_geojson(self, files, index_col='index'):
"""
self._read(files, index_col)

def read_shapefile(self, files, index_col='index'):
def read_shapefile(self, files, index_col='name'):
"""
Append information from Esri Shapefiles to a WaterNetworkGIS object
Expand Down
7 changes: 3 additions & 4 deletions wntr/network/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,6 @@ def write_inpfile(wn, filename: str, units=None, version: float = 2.2,
"""
if wn._inpfile is None:
logger.warning("Writing a minimal INP file without saved non-WNTR options (energy, etc.)")
wn._inpfile = wntr.epanet.InpFile()
if units is None:
units = wn._options.hydraulic.inpfile_units
Expand Down Expand Up @@ -551,7 +550,7 @@ def write_geojson(wn, prefix: str, crs=None, pumps_as_points=True,
wn_gis.write_geojson(prefix=prefix)


def read_geojson(files, index_col='index', append=None):
def read_geojson(files, index_col='name', append=None):
"""
Create or append a WaterNetworkModel from GeoJSON files
Expand Down Expand Up @@ -612,7 +611,7 @@ def write_shapefile(wn, prefix: str, crs=None, pumps_as_points=True,
valves_as_points=valves_as_points)
wn_gis.write_shapefile(prefix=prefix)

def read_shapefile(files, index_col='index', append=None):
def read_shapefile(files, index_col='name', append=None):
"""
Create or append a WaterNetworkModel from Esri Shapefiles
Expand All @@ -635,7 +634,7 @@ def read_shapefile(files, index_col='index', append=None):
"""
gis_data = WaterNetworkGIS()
gis_data.read_shapefile(files, index_col='index')
gis_data.read_shapefile(files,index_col=index_col)
wn = gis_data._create_wn(append=append)

return wn
Expand Down
Loading

0 comments on commit d1d0f2d

Please sign in to comment.