Skip to content

Commit

Permalink
Removed animation function, not compatible with GaussianPuff simulati…
Browse files Browse the repository at this point in the history
…on, use static graphics options
  • Loading branch information
kaklise committed Nov 18, 2023
1 parent fccc342 commit 01ff73f
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 118 deletions.
230 changes: 115 additions & 115 deletions chama/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,16 @@
signal_convexhull
signal_xsection
animate_puffs
sensor_locations
"""
from __future__ import print_function, division
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.collections import PatchCollection
from matplotlib.animation import FuncAnimation
except:
plt = None
from scipy.spatial import ConvexHull

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

Expand Down Expand Up @@ -247,114 +242,6 @@ def contour_data(temp, threshold, log_flag):
fig.show()


def animate_puffs(puff, x_range=(None, None), y_range=(None, None)):
"""
Plots the horizontal movement of puffs from a GaussianPuff simulation
over time. Each puff is represented as a circle centered at the puff
center location with radius equal to the standard deviation in the
horizontal direction (sigmaY).
Parameters
------------------
puff: pandas DataFrame
The puff DataFrame created by a GaussianPuff object
x_range: tuple (xmin, xmax) (optional)
The x-axis limits for the plot
y_range: tuple (ymin, ymax) (optional)
The y-axis limits for the plot
"""

def circles(x, y, s, c='b', vmin=None, vmax=None, **kwargs):
"""
Make a scatter plot of circles.
Similar to plt.scatter, but the size of circles are in data scale.
Parameters
----------
x, y : scalar or array_like, shape (n, )
Input data
s : scalar or array_like, shape (n, )
Radius of circles.
c : color or sequence of color, optional, default : 'b'
`c` can be a single color format string, or a sequence of color
specifications of length `N`, or a sequence of `N` numbers to be
mapped to colors using the `cmap` and `norm` specified via kwargs.
Note that `c` should not be a single numeric RGB or RGBA sequence
because that is indistinguishable from an array of values
to be colormapped. (If you insist, use `color` instead.)
`c` can be a 2-D array in which the rows are RGB or RGBA, however.
vmin, vmax : scalar, optional, default: None
`vmin` and `vmax` are used in conjunction with `norm` to normalize
luminance data. If either are `None`, the min and max of the
color array is used.
kwargs : `~matplotlib.collections.Collection` properties
Eg. alpha, edgecolor(ec), facecolor(fc), linewidth(lw),
linestyle(ls), norm, cmap, transform, etc.
Returns
-------
paths : `~matplotlib.collections.PathCollection`
"""

if np.isscalar(c):
kwargs.setdefault('color', c)
c = None

if 'fc' in kwargs:
kwargs.setdefault('facecolor', kwargs.pop('fc'))
if 'ec' in kwargs:
kwargs.setdefault('edgecolor', kwargs.pop('ec'))
if 'ls' in kwargs:
kwargs.setdefault('linestyle', kwargs.pop('ls'))
if 'lw' in kwargs:
kwargs.setdefault('linewidth', kwargs.pop('lw'))
# You can set `facecolor` with an array for each patch,
# while you can only set `facecolors` with a value for all.

zipped = np.broadcast(x, y, s)
patches = [Circle((x_, y_), s_)
for x_, y_, s_ in zipped]
collection = PatchCollection(patches, **kwargs)
if c is not None:
c = np.broadcast_to(c, zipped.shape).ravel()
collection.set_array(c)
collection.set_clim(vmin, vmax)

ax = plt.gca()
ax.add_collection(collection)
ax.autoscale_view()
plt.draw_if_interactive()
if c is not None:
plt.sci(collection)
return collection

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

fig, ax = plt.subplots()
# ln, = plt.plot([],[],animated=True)

def update(time):
plt.cla()
ax.set_xlim(x_range[0], x_range[1])
ax.set_ylim(y_range[0], y_range[1])
ax.set_title('T = %6.2f' % time)
ax.set_xlabel('X')
ax.set_ylabel('Y')

temp = puff.loc[puff['T'] == 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')

plt.show()


def sensor_locations(sensors, x_range=(None, None), y_range=(None, None),
z_range=(None, None), legend=False,
colors=None, markers=None):
Expand Down Expand Up @@ -415,3 +302,116 @@ def sensor_locations(sensors, x_range=(None, None), y_range=(None, None),
if legend:
ax.legend()
fig.show()

# Animation function is no longer working
# Use convex hull or xsection on individual timesteps
# def animate_puffs(puff, x_range=(None, None), y_range=(None, None)):
# """
# Plots the horizontal movement of puffs from a GaussianPuff simulation
# over time. Each puff is represented as a circle centered at the puff
# center location with radius equal to the standard deviation in the
# horizontal direction (sigmaY).

# Parameters
# ------------------
# puff: pandas DataFrame
# The puff DataFrame created by a GaussianPuff object
# x_range: tuple (xmin, xmax) (optional)
# The x-axis limits for the plot
# y_range: tuple (ymin, ymax) (optional)
# The y-axis limits for the plot
# """

# def circles(x, y, s, c='b', vmin=None, vmax=None, **kwargs):
# """
# Make a scatter plot of circles.
# Similar to plt.scatter, but the size of circles are in data scale.

# Parameters
# ----------
# x, y : scalar or array_like, shape (n, )
# Input data
# s : scalar or array_like, shape (n, )
# Radius of circles.
# c : color or sequence of color, optional, default : 'b'
# `c` can be a single color format string, or a sequence of color
# specifications of length `N`, or a sequence of `N` numbers to be
# mapped to colors using the `cmap` and `norm` specified via kwargs.
# Note that `c` should not be a single numeric RGB or RGBA sequence
# because that is indistinguishable from an array of values
# to be colormapped. (If you insist, use `color` instead.)
# `c` can be a 2-D array in which the rows are RGB or RGBA, however.
# vmin, vmax : scalar, optional, default: None
# `vmin` and `vmax` are used in conjunction with `norm` to normalize
# luminance data. If either are `None`, the min and max of the
# color array is used.
# kwargs : `~matplotlib.collections.Collection` properties
# Eg. alpha, edgecolor(ec), facecolor(fc), linewidth(lw),
# linestyle(ls), norm, cmap, transform, etc.

# Returns
# -------
# paths : `~matplotlib.collections.PathCollection`
# """

# if np.isscalar(c):
# kwargs.setdefault('color', c)
# c = None

# if 'fc' in kwargs:
# kwargs.setdefault('facecolor', kwargs.pop('fc'))
# if 'ec' in kwargs:
# kwargs.setdefault('edgecolor', kwargs.pop('ec'))
# if 'ls' in kwargs:
# kwargs.setdefault('linestyle', kwargs.pop('ls'))
# if 'lw' in kwargs:
# kwargs.setdefault('linewidth', kwargs.pop('lw'))
# # You can set `facecolor` with an array for each patch,
# # while you can only set `facecolors` with a value for all.

# zipped = np.broadcast(x, y, s)
# patches = [Circle((x_, y_), s_)
# for x_, y_, s_ in zipped]
# collection = PatchCollection(patches, **kwargs)
# if c is not None:
# c = np.broadcast_to(c, zipped.shape).ravel()
# collection.set_array(c)
# collection.set_clim(vmin, vmax)

# ax = plt.gca()
# ax.add_collection(collection)
# ax.autoscale_view()
# plt.draw_if_interactive()
# if c is not None:
# plt.sci(collection)
# return collection

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

# from matplotlib.patches import Circle, Ellipse, Rectangle
# from matplotlib.collections import PatchCollection
# from matplotlib.animation import FuncAnimation

# fig, ax = plt.subplots()
# # ln, = plt.plot([],[],animated=True)

# def update(time):
# plt.cla()
# ax.set_xlim(x_range[0], x_range[1])
# ax.set_ylim(y_range[0], y_range[1])
# ax.set_title('T = %6.2f' % time)
# ax.set_xlabel('X')
# ax.set_ylabel('Y')

# temp = puff.loc[puff['T'] == 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')

# plt.show()
3 changes: 0 additions & 3 deletions chama/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ def test_signal_xsection(self):

self.assertTrue(isfile(filename))

def test_signal_animate(self):
pass


class TestSensorGraphics(unittest.TestCase):

Expand Down

0 comments on commit 01ff73f

Please sign in to comment.