Verified Commit e1673575 authored by Qusai Al Shidi's avatar Qusai Al Shidi 💬
Browse files

Add untested routines

parent d41da915
Pipeline #28526 passed with stage
in 53 seconds
......@@ -31,7 +31,7 @@ setuptools.setup(
"tecplottools": "tecplot",
"tecplot": "tecplot",
"tecplottools": "h5py",
"hmi": "drms",
......@@ -8,6 +8,118 @@ __email__ = ''
import datetime as dt
import numpy as np
def limit_growth(vector, factor=1.3, look_ahead=1, extensively=False):
"""This is a growth limiter for 1D vectors. It helps clean out spikes.
Use this to clean out spikes in IMF data for example.
vector (numpy.array):
A 1D array to clean.
factor (float):
The factor in which to limit growth. (default 1.3)
look_ahead (int):
How many frames to look ahead for growth.
extensively (bool):
Will keep refining until fully limited instead of one pass through.
(default False)
(numpy.array): Of limited vector.
ValueError: If not given 1D array.
from datetime import datetime
import matplotlib.pyplot as plt
from swmfpy.web import get_omni_data
from import limit_growth
times = (datetime(2011, 8, 6), datetime(2011, 8, 7))
data = get_omni_data(*times)
plt.plot(data['times'], data['density'])
plt.plot(data['times'], limit_growth(data['density'],
if sum(vector.shape) != vector.size:
raise ValueError('limit_growth() only handles 1D arrays')
limited = np.copy(vector)
for i in range(look_ahead, len(limited)):
if limited[i] > 0 and limited[i-look_ahead] > 0:
limited[i] = min(limited[i-look_ahead]*factor,
max(limited[i], limited[i-look_ahead]/factor)
if limited[i] < 0 and limited[i-look_ahead] < 0:
limited[i] = min(limited[i-look_ahead]/factor,
max(limited[i], limited[i-look_ahead]*factor)
return limited
def limit_changes(vector, change, constrictor=None, change2=None, look_ahead=1):
"""Limit the changes (jumps) of an array.
This is different from #limit_growth() because growth works on a
multiplication factor but `limit_change()` works on absolute changes.
vector (numpy.array):
Array in which to limit changes.
change ((float, float)):
Changes to limit `vector` by.
constrictor (numpy.array):
Array of same shape as `vector` in which if it is not growing
then constrict further. This is useful for limiting compression
behind shocks. If None, this won't apply (default None)
change2 ((float, float)):
Just like `change` but after constrictor has not grown. If None,
this won't apply (default None)
look_ahead (int):
How many indeces ahead to check (default 1)
(numpy.array): Of constricted `vector`.
ValueError: If arrays are of different shapes or not 1D each.
ValueError: If `change` tuple is not (negative, positive)
if sum(vector.shape) != vector.size:
raise ValueError('vector must be 1D')
if any(constrictor) and sum(constrictor.shape) != constrictor.size:
raise ValueError('constrictor must be 1D')
if change[0] > 0 or change[1] < 0:
raise ValueError('change must be (negative, positive)')
if change2[0] > 0 or change2[1] < 0:
raise ValueError('change2 must be (negative, positive)')
limited = np.copy(vector)
for i in range(look_ahead, len(vector)):
limited[i] = min(limited[i-1]+change[0],
if any(constrictor):
for i in range(look_ahead, len(vector)):
if constrictor[i] <= constrictor[i-1]:
limited[i] = min(limited[i-1]+change2[0],
return limited
def interp_nans(x_vals, y_vals):
"""Returns a numpy array with NaNs interpolated.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment