Skip to content

Commit

Permalink
Merge pull request #33 from kaklise/build
Browse files Browse the repository at this point in the history
Updates to support Pandas 2.0 and test against additional versions of Python
  • Loading branch information
kaklise authored Nov 20, 2023
2 parents 98b81ad + bc2bbe6 commit b344fd1
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 100 deletions.
112 changes: 59 additions & 53 deletions .github/workflows/build_tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This workflow will install Python dependencies, run tests and lint with a single version of Python
# This workflow will install Python dependencies, run tests and coverage
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: build
Expand All @@ -10,17 +10,23 @@ on:
pull_request:
branches:
- '**'

schedule:
- cron: '0 0 1 * *'

jobs:

build:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: 3.8
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python --version
Expand All @@ -32,42 +38,41 @@ jobs:
python setup.py bdist_wheel
ls dist/*
- name: Save wheel
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: wheel
path: dist/chama*.whl
name: chama_${{ matrix.python-version }}_${{ matrix.os }}.whl
path: dist/chama*

#test:
# name: Import test
# needs: build
# runs-on: ${{ matrix.os }}
# strategy:
# matrix:
# python-version: [3.7, 3.8, 3.9]
# os: [ubuntu-latest]
# steps:
# - name: Set up Python
# uses: actions/setup-python@v2
# with:
# python-version: ${{ matrix.python-version }}
# - name: Download wheel
# uses: actions/download-artifact@v2
# with:
# name: wheel
# - name: Install chama
# run: |
# python -m pip install --upgrade pip
# pip install wheel
# pip install --find-links=. chama
# - name: Import test
# run: |
# python -c "import chama"
import:
needs: build
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
os: [ubuntu-latest]
steps:
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Download wheel
uses: actions/download-artifact@v3
with:
name: chama_${{ matrix.python-version }}_${{ matrix.os }}.whl
- name: Install chama
run: |
python -m pip install --upgrade pip
pip install wheel pyomo pandas numpy scipy
pip install --no-index --pre --find-links=. chama
- name: Import chama
run: |
python -c "import chama"
linux:
pytest_coverage:
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: [3.7, 3.8, 3.9]
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v2
Expand All @@ -84,37 +89,38 @@ jobs:
pip install -r requirements.txt
conda install -y -c conda-forge glpk
pip install coverage coveralls wntr
python setup.py develop
- name: Run Tests
python -m pip install -e .
- name: Run tests and coverage
run: |
export PATH=/usr/share/miniconda/bin:$PATH
coverage erase
coverage run --context=${{ matrix.os }}.py${{ matrix.python-version }} --source=chama --omit="*/tests/*" -m nose -v --nologcapture --with-doctest --doctest-extension=.rst --traverse-namespace documentation/*.rst chama
coverage run --context=${{ matrix.os }}.py${{ matrix.python-version }} --source=chama --omit="*/tests/*" -m pytest --doctest-modules --doctest-glob="*.rst" chama
coverage run --context=${{ matrix.os }}.py${{ matrix.python-version }} --source=chama --omit="*/tests/*" --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@v3
with:
name: coverage
path: .coverage.${{ matrix.python-version }}.${{ matrix.os }}

coverage:
needs: [ linux ]
coverage_reports:
needs: [ pytest_coverage ]
runs-on: ubuntu-latest
steps:
- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: 3.8
python-version: 3.9
- uses: actions/checkout@v2
- name: Install coverage
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install coveralls
python setup.py develop
- name: Download coverage artifacts from test matrix
uses: actions/download-artifact@v2
python -m pip install -e .
- name: Download coverage artifacts
uses: actions/download-artifact@v3
with:
name: coverage
- name: Setup coverage and combine reports
Expand All @@ -133,34 +139,34 @@ jobs:
coverage json --pretty-print
coverage html --show-contexts
- name: Save coverage JSON
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: coverage
path: coverage.json
- name: Save coverage html
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: coverage
path: htmlcov

coveralls:
needs: [ linux ]
needs: [ pytest_coverage ]
runs-on: ubuntu-latest
continue-on-error: true
steps:
- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: 3.8
python-version: 3.9
- uses: actions/checkout@v2
- name: Install coverage
run: |
python -m pip install --upgrade pip
pip install coveralls
pip install -r requirements.txt
python setup.py develop
python -m pip install -e .
- name: Download coverage artifacts from test matrix
uses: actions/download-artifact@v2
uses: actions/download-artifact@v3
with:
name: coverage
- name: Setup coverage and combine reports
Expand Down
2 changes: 1 addition & 1 deletion chama/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from chama import optimize
from chama import graphics

__version__ = '0.2.0'
__version__ = '0.3.0'

__copyright__ = """Copyright 2016 National Technology & Engineering
Solutions of Sandia, LLC (NTESS). Under the terms of Contract
Expand Down
82 changes: 50 additions & 32 deletions chama/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@
try:
import matplotlib.pyplot as plt
from matplotlib import ticker
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.patches import Circle, Ellipse, Rectangle
from matplotlib.patches import Circle
from matplotlib.collections import PatchCollection
from matplotlib.animation import FuncAnimation
except:
pass
from scipy.spatial import ConvexHull
plt = None

from scipy.spatial import ConvexHull
import numpy as np
from chama.sensors import Mobile


def signal_convexhull(signal, scenarios, threshold, timesteps=None,
colormap=plt.cm.viridis,
def signal_convexhull(signal, scenarios, threshold, timesteps=None,
colormap=None,
x_range=(None, None), y_range=(None, None),
z_range=(None, None)):
"""
Expand All @@ -54,6 +53,11 @@ def signal_convexhull(signal, scenarios, threshold, timesteps=None,
z_range: tuple (optional)
The z-axis limits for the plot
"""
if plt is None:
raise ImportError('matplotlib is required for graphics')
if colormap is None:
colormap = plt.get_cmap('viridis')

t_col = 'T'
x_col = 'X'
y_col = 'Y'
Expand All @@ -69,29 +73,24 @@ def signal_convexhull(signal, scenarios, threshold, timesteps=None,
for scenario in scenarios:
i = 0
for timestep in timesteps:
color = colormap(i)
i += 1 / float(len(timesteps))

signal_t = signal[signal[t_col] == timestep]
conc_filter = signal_t[scenario] > threshold

data = signal_t[[x_col, y_col, z_col]][conc_filter]
data = data.values
try:
color = colormap(i)
i += 1 / float(len(timesteps))

signal_t = signal[signal[t_col] == timestep]
conc_filter = signal_t[scenario] > threshold

# plot points
# data = signal_t[[x_col,y_col,z_col,scenario]][conc_filter]
# data = data.as_matrix()
# ax.scatter(data[:,0], data[:,1], data[:,2], c=data[:,3],s=30)

data = signal_t[[x_col, y_col, z_col]][conc_filter]
data = data.as_matrix()
hull = ConvexHull(data)
ax.plot_trisurf(data[:, 0], data[:, 1], data[:, 2],
triangles=hull.simplices,
edgecolor='none',
shade=False,
color=color)
except:
pass

print('Convex Hull not created for scenario %s timestep %d' % (scenario, timestep))
ax.set_xlabel(x_col)
ax.set_ylabel(y_col)
ax.set_zlabel(z_col)
Expand All @@ -104,7 +103,7 @@ def signal_convexhull(signal, scenarios, threshold, timesteps=None,

def signal_xsection(signal, signal_name, threshold=None, timesteps=None,
x_value=None, y_value=None, z_value=None, log_flag=False,
colormap=plt.cm.viridis, alpha=0.7, N=5,
colormap=None, alpha=0.7, N=5,
x_range=(None, None), y_range=(None, None),
z_range=(None, None)):
"""
Expand Down Expand Up @@ -147,7 +146,11 @@ def signal_xsection(signal, signal_name, threshold=None, timesteps=None,
z_range: tuple (optional)
The z-axis limits for the plot
"""

if plt is None:
raise ImportError('matplotlib is required for graphics')
if colormap is None:
colormap = plt.get_cmap('viridis')

t_col = 'T'
x_col = 'X'
y_col = 'Y'
Expand Down Expand Up @@ -238,7 +241,7 @@ def contour_data(temp, threshold, log_flag):
fig.show()


def animate_puffs(puff, x_range=(None, None), y_range=(None, None)):
def animate_puffs(puff, x_range=(None, None), y_range=(None, None), repeat=True):
"""
Plots the horizontal movement of puffs from a GaussianPuff simulation
over time. Each puff is represented as a circle centered at the puff
Expand All @@ -253,6 +256,8 @@ def animate_puffs(puff, x_range=(None, None), y_range=(None, None)):
The x-axis limits for the plot
y_range: tuple (ymin, ymax) (optional)
The y-axis limits for the plot
repeat : bool, optional
If True, the animation will repeat
"""

def circles(x, y, s, c='b', vmin=None, vmax=None, **kwargs):
Expand Down Expand Up @@ -304,7 +309,7 @@ def circles(x, y, s, c='b', vmin=None, vmax=None, **kwargs):

zipped = np.broadcast(x, y, s)
patches = [Circle((x_, y_), s_)
for x_, y_, s_ in zipped]
for x_, y_, s_ in zipped]
collection = PatchCollection(patches, **kwargs)
if c is not None:
c = np.broadcast_to(c, zipped.shape).ravel()
Expand All @@ -318,6 +323,17 @@ def circles(x, y, s, c='b', vmin=None, vmax=None, **kwargs):
if c is not None:
plt.sci(collection)
return collection

if plt is None:
raise ImportError('matplotlib is required for graphics')

buffer = puff['sigmaY'].max() + puff['sigmaY'].max()/5
xmin = min([v for v in [puff['X'].min() - buffer, x_range[0]] if v is not None])
xmax = max([v for v in [puff['X'].max() + buffer, x_range[1]] if v is not None])
ymin = min([v for v in [puff['Y'].min() - buffer, y_range[0]] if v is not None])
ymax = max([v for v in [puff['Y'].max() + buffer, y_range[1]] if v is not None])
x_range = (xmin, xmax)
y_range = (ymin, ymax)

fig, ax = plt.subplots()
# ln, = plt.plot([],[],animated=True)
Expand All @@ -334,13 +350,13 @@ def update(time):
out = circles(temp['X'], temp['Y'], temp['sigmaY'], alpha=0.5,
edgecolor='none')
return out

ani = FuncAnimation(fig, update, frames=puff['T'].unique())

# Need a coder like ffmpeg installed in order to save
# ani.save('puff.mp4')

anim = FuncAnimation(fig, update, frames=puff['T'].unique(), interval=50,
blit=False, repeat=repeat)

plt.show()

return anim


def sensor_locations(sensors, x_range=(None, None), y_range=(None, None),
Expand Down Expand Up @@ -371,7 +387,9 @@ def sensor_locations(sensors, x_range=(None, None), y_range=(None, None),
sensor. The key:value pairs are {'sensor name' : String
representing the marker to be passed to the plot function)
"""

if plt is None:
raise ImportError('matplotlib is required for graphics')

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

Expand Down
Loading

0 comments on commit b344fd1

Please sign in to comment.