Skip to content

Instantly share code, notes, and snippets.

@beckermr
Created August 4, 2021 18:15
Show Gist options
  • Save beckermr/7b87afcfe2af5d6ea2500232753299ff to your computer and use it in GitHub Desktop.
Save beckermr/7b87afcfe2af5d6ea2500232753299ff to your computer and use it in GitHub Desktop.
boolean_masks.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "v = True\nw = False",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "v and v",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": "True"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "v and w",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "False"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "v or v",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "True"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "v or w",
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": "True"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import numpy as np",
"execution_count": 6,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = np.arange(10)",
"execution_count": 7,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x",
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 8,
"data": {
"text/plain": "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "msk = (x < 5)",
"execution_count": 9,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "msk",
"execution_count": 10,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 10,
"data": {
"text/plain": "array([ True, True, True, True, True, False, False, False, False,\n False])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "msk.dtype",
"execution_count": 11,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 11,
"data": {
"text/plain": "dtype('bool')"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x[msk]",
"execution_count": 12,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 12,
"data": {
"text/plain": "array([0, 1, 2, 3, 4])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "msk = (x > 1) | (x < 5)",
"execution_count": 15,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "msk",
"execution_count": 16,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 16,
"data": {
"text/plain": "array([ True, True, True, True, True, True, True, True, True,\n True])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x = np.arange(20).reshape(4, 5)",
"execution_count": 18,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "msk = (x > 5)",
"execution_count": 20,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "msk",
"execution_count": 21,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 21,
"data": {
"text/plain": "array([[False, False, False, False, False],\n [False, True, True, True, True],\n [ True, True, True, True, True],\n [ True, True, True, True, True]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "x[msk]",
"execution_count": 22,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 22,
"data": {
"text/plain": "array([ 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "msk = (\n (x > 5)\n & (x < 10)\n)",
"execution_count": 23,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "msk",
"execution_count": 24,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 24,
"data": {
"text/plain": "array([[False, False, False, False, False],\n [False, True, True, True, True],\n [False, False, False, False, False],\n [False, False, False, False, False]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "y = np.arange(20).reshape(4, 5) + 10",
"execution_count": 25,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "y",
"execution_count": 26,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 26,
"data": {
"text/plain": "array([[10, 11, 12, 13, 14],\n [15, 16, 17, 18, 19],\n [20, 21, 22, 23, 24],\n [25, 26, 27, 28, 29]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "msk_y = (y > 10)",
"execution_count": 27,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "msk_combined = msk & msk_y",
"execution_count": 28,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "msk_y",
"execution_count": 29,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 29,
"data": {
"text/plain": "array([[False, True, True, True, True],\n [ True, True, True, True, True],\n [ True, True, True, True, True],\n [ True, True, True, True, True]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "msk_combined",
"execution_count": 30,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 30,
"data": {
"text/plain": "array([[False, False, False, False, False],\n [False, True, True, True, True],\n [False, False, False, False, False],\n [False, False, False, False, False]])"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "msk_combined.astype(np.int32)",
"execution_count": 31,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 31,
"data": {
"text/plain": "array([[0, 0, 0, 0, 0],\n [0, 1, 1, 1, 1],\n [0, 0, 0, 0, 0],\n [0, 0, 0, 0, 0]], dtype=int32)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import matplotlib.pyplot as plt",
"execution_count": 32,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "plt.figure()\nplt.pcolormesh(msk_combined)",
"execution_count": 34,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 34,
"data": {
"text/plain": "<matplotlib.collections.QuadMesh at 0x138b6a550>"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": "<Figure size 432x288 with 1 Axes>",
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8rg+JYAAAACXBIWXMAAAsTAAALEwEAmpwYAAAPVElEQVR4nO3d74te9ZnH8fdn41RrawgSWbNJqn0QCrXQ6g5REZZg211NQ7MPfBChFWRhUCxYtlDaPrD0HyjFtRiGVlppqRTtSnDjdpUqVdioSZqkjWl3Q+nikECo3SYOijXdax/M2c1wO5P7zMyd3PGb9wsOc35cc+6LQ/jk8J3vOXeqCklSW/5i3A1IkkbPcJekBhnuktQgw12SGmS4S1KDDHdJalDvcE+yKskvkjy1wLEkeTDJ0SSHktww2jYlSUuxlDv3+4Ejixy7HdjULVPAwyvsS5K0Ar3CPckG4DPAdxYp2Q48WnP2AGuSrBtRj5KkJbqkZ923gC8DVyxyfD3w2rztmW7f8flFSaaYu7NnFav++nJWL6VXSbrovcF//76qrhpWNzTck2wDTlTVviRbFitbYN+73mtQVdPANMDqXFk35pPDPl6SNM+z9fh/9anrMyxzC/DZJL8DHgNuTfKDgZoZYOO87Q3AsT4NSJJGb2i4V9VXq2pDVV0L7AB+VlWfGyjbBdzVzZq5CThZVccHzy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+BbwZeB/xtzHhaKAf0uyr3udy6LGEe69XlWgi1OSDwJPAF+sqlPj7mdcqurPVfUJ5p723pzkohu2S7INOFFV+8bdywXklqq6gbk38d7XDe0uaBzh7qsKtKBufPkJ4IdV9ZNx93MhqKo/As8Dt423k7G4BfhsN878GHBrkh+Mt6Xxqqpj3c8TwD8zN8y9oHGEe5/XGegi0/0R8bvAkar65rj7GackVyVZ062/H/gU8OuxNjUGVfXVqtpQVdcylxM/q6rPjbmtsUnygW6yAUk+APwtsOhMu/Me7lV1Gvi/1xkcAX5cVYfPdx8XgiQ/Av4d+EiSmST/MO6exugW4PPM3Z0d6Jat425qTNYBzyU5xNzN0DNVddFPAxR/CbyY5CDwMvAvVfWvixX7+gFJapBPqEpSgwx3SWqQ4S5JDTLcJalBhrskNchwl6QGGe6S1KD/BZ/vGm1GZ56LAAAAAElFTkSuQmCC\n"
},
"metadata": {
"needs_background": "light"
}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "conda-env-anl-py",
"display_name": "Python [conda env:anl] *",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.8.10",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "boolean_masks.ipynb",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment