Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dennissergeev/e2fa7af9726123da0c7f5746191a9af7 to your computer and use it in GitHub Desktop.
Save dennissergeev/e2fa7af9726123da0c7f5746191a9af7 to your computer and use it in GitHub Desktop.
Masked_Arrays_Gradient_MetPy.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import metpy.calc as metcalc\nimport numpy as np",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "from collections import namedtuple",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def deriv_2d_data():\n \"\"\"Return 2-dimensional data for analytic function for testing derivative functions.\"\"\"\n ret = namedtuple('D_2D_Test_Data', 'x y x0 y0 a b f')(\n np.array([0., 2., 7.]), np.array([1., 5., 11., 13.]), 3, 1.5, 0.5, 0.25, 0)\n\n # Makes a value array with y changing along rows (axis 0) and x along columns (axis 1)\n return ret._replace(f=ret.a * (ret.x - ret.x0)**2 + ret.b * (ret.y[:, None] - ret.y0)**2)",
"execution_count": 3,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "res = metcalc.kinematics.gradient(deriv_2d_data().f, x=(deriv_2d_data().y, deriv_2d_data().x))",
"execution_count": 4,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "res",
"execution_count": 5,
"outputs": [
{
"data": {
"text/plain": "(<Quantity([[-0.25 -0.25 -0.25]\n [ 1.75 1.75 1.75]\n [ 4.75 4.75 4.75]\n [ 5.75 5.75 5.75]], 'dimensionless')>, <Quantity([[-3. -1. 4.]\n [-3. -1. 4.]\n [-3. -1. 4.]\n [-3. -1. 4.]], 'dimensionless')>)"
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def deriv_2d_data_masked():\n \"\"\"Return 2-dimensional data for analytic function for testing derivative functions.\"\"\"\n ret = namedtuple('D_2D_Test_Data', 'x y x0 y0 a b f')(\n np.array([0., 2., 7.]), np.array([1., 5., 11., 13.]), 3, 1.5, 0.5, 0.25, 0)\n\n # Makes a value array with y changing along rows (axis 0) and x along columns (axis 1)\n return ret._replace(f=np.ma.masked_outside(ret.a * (ret.x - ret.x0)**2 + ret.b * (ret.y[:, None] - ret.y0)**2, 5, 30))",
"execution_count": 6,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "res_masked = metcalc.kinematics.gradient(deriv_2d_data_masked().f, x=(deriv_2d_data().y, deriv_2d_data().x))",
"execution_count": 7,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "deriv_2d_data_masked().f",
"execution_count": 8,
"outputs": [
{
"data": {
"text/plain": "masked_array(\n data=[[--, --, 8.0625],\n [7.5625, --, 11.0625],\n [27.0625, 23.0625, --],\n [--, --, --]],\n mask=[[ True, True, False],\n [False, True, False],\n [False, False, True],\n [ True, True, True]],\n fill_value=1e+20)"
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "res_masked",
"execution_count": 9,
"outputs": [
{
"data": {
"text/plain": "(<Quantity([[ -0.35 -0.35 1.7875 ]\n [ -0.15 -0.15 -0.2875 ]\n [ -9.3359375 -0.04166667 -0.4609375 ]\n [-17.7265625 0.04166667 0.4609375 ]], 'dimensionless')>,\n <Quantity([[-0.64285714 -0.35714286 0.35714286]\n [-4.86160714 -2.70089286 2.70089286]\n [-1.25357143 -2.74642857 -6.47857143]\n [-0.64285714 -0.35714286 0.35714286]], 'dimensionless')>)"
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "res_masked[0].m",
"execution_count": 10,
"outputs": [
{
"data": {
"text/plain": "masked_array(\n data=[[ -0.35 , -0.35 , 1.7875 ],\n [ -0.15 , -0.15 , -0.2875 ],\n [ -9.3359375 , -0.04166667, -0.4609375 ],\n [-17.7265625 , 0.04166667, 0.4609375 ]],\n mask=False,\n fill_value=1e+20)"
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "So instead of having half of the values masked, the result has erroneous values like -0.35..."
}
],
"metadata": {
"kernelspec": {
"name": "conda-env-phd-py",
"display_name": "Python [conda env:phd]",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.6.5",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "Masked_Arrays_Gradient_MetPy.ipynb",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment