Skip to content

Instantly share code, notes, and snippets.

@golobor
Last active March 6, 2023 20:32
Show Gist options
  • Save golobor/cdf86509191515b6e6ae3e4cd6a2f086 to your computer and use it in GitHub Desktop.
Save golobor/cdf86509191515b6e6ae3e4cd6a2f086 to your computer and use it in GitHub Desktop.
230306-symmetrize-stack
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 2., 1., 0.],\n",
" [nan, 2., 1.],\n",
" [nan, nan, 2.]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mat = np.ones(shape=(3,3))\n",
"mat[np.tril_indices_from(mat)] = np.nan\n",
"mat[np.diag_indices_from(mat)] = 2\n",
"mat[0,2] = 0\n",
"mat"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 2., nan, 0.],\n",
" [nan, nan, nan],\n",
" [nan, nan, 2.]])"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"mat2 = np.copy(mat)\n",
"mat2[1,:] = np.nan\n",
"mat2[:,1] = np.nan\n",
"display(mat2)\n",
"\n",
"stack = np.dstack([mat,mat2])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[4., 0., 0.],\n",
" [0., 0., 0.],\n",
" [0., 0., 4.]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"stack_symm_curr = np.nansum([stack, np.transpose(stack, axes=(1, 0, 2))], axis=0)\n",
"stack_symm_curr[:,:,1]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 2., nan, 0.],\n",
" [nan, nan, nan],\n",
" [ 0., nan, 2.]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"stack_symm_new = np.fmax(stack, np.transpose(stack, axes=(1, 0, 2)))\n",
"stack_symm_new[:,:,1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "1585499299b002ec9c89ca3445b6d7c3e932de065394905b4c9ba34440c7ad89"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment