From feefeb932353dc013839d6c9f819bc0c5af94a4d Mon Sep 17 00:00:00 2001 From: Jeremy Myers Date: Tue, 19 Sep 2023 14:26:18 -0700 Subject: [PATCH 01/17] Created ttensor/reconstruct tutorial in master. --- .../source/tutorial/TTENSOR_RECONSTRUCT.ipynb | 276 ++++++++++++++++++ 1 file changed, 276 insertions(+) create mode 100644 docs/source/tutorial/TTENSOR_RECONSTRUCT.ipynb diff --git a/docs/source/tutorial/TTENSOR_RECONSTRUCT.ipynb b/docs/source/tutorial/TTENSOR_RECONSTRUCT.ipynb new file mode 100644 index 0000000..caca73d --- /dev/null +++ b/docs/source/tutorial/TTENSOR_RECONSTRUCT.ipynb @@ -0,0 +1,276 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Partial Reconstruction of a Tucker Tensor\n", + "```\n", + "Copyright 2022 National Technology & Engineering Solutions of Sandia,\n", + "LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the\n", + "U.S. Government retains certain rights in this software.\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Benefits of Partial Reconstruction\n", + "An advantage of Tucker decomposition is that the tensor can be partially reconstructed without ever forming the *full* tensor. The `reconstruct()` member function does this, resulting in significant time and memory savings, as we demonstrate below." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import pyttb as ttb\n", + "import numpy as np" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## TODO: Decide if we want to use Miranda density tensor dataset\n", + "If so, the Miranda data is available at https://gitlab.com/tensors/tensor_data_miranda_sim. It loads a tensor `X` of size $384 \\times 384 \\times 256$." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## For now, we will use random data." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "shape = (20, 30, 50)\n", + "X = ttb.tenrand(shape)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Compute HOSVD\n", + "We compute the Tucker decomposition using ST-HOSVD with target relative error `0.001`." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Computing HOSVD...\n", + "\n", + " Size of core: (20, 30, 50)\n", + "||X-T||/||X|| = 6.1027e-15 <= 0.001000 (tol)\n", + "CPU times: user 29.6 ms, sys: 63.5 ms, total: 93.1 ms\n", + "Wall time: 9.54 ms\n" + ] + } + ], + "source": [ + "%%time\n", + "T = ttb.hosvd(X, tol=0.001)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Compression: 0.8875739644970414 x\n" + ] + } + ], + "source": [ + "# Note: If the result is < 1.0 x, it will be unsurprising \n", + "# since the random generation process below wasn't expected \n", + "# to return a low-rank approximation\n", + "print(\n", + " f\"Compression: {X.data.nbytes/(T.core.data.nbytes + np.sum([i.nbytes for i in T.factor_matrices]))} x\"\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Full reconstruction\n", + "We can create a full reconstruction of the data using the full command. Not only is this expensive in computational time but also in memory. Now, let's see how long it takes to reconstruct the approximation to `X`." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPU times: user 9.22 ms, sys: 16.9 ms, total: 26.1 ms\n", + "Wall time: 1.93 ms\n" + ] + } + ], + "source": [ + "%%time\n", + "Xf = T.full()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Partial reconstruction\n", + "If we really only want part of the tensor, we can reconstruct just that part. Suppose we only want the `[:,15,:]` slice. The reconstruct function can do this much more efficiently with no loss in accuracy." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPU times: user 6.28 ms, sys: 7.37 ms, total: 13.7 ms\n", + "Wall time: 1.88 ms\n" + ] + } + ], + "source": [ + "%%time\n", + "Xslice = T.reconstruct(modes=[1],samples=[15])" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Compression: 30.0 x\n" + ] + } + ], + "source": [ + "print(\n", + " f\"Compression: {Xf.data.nbytes/Xslice.data.nbytes} x\"\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Down-sampling\n", + "Additionally, we may want to downsample high-dimensional data to something lower resolution. For example, here we downsample in modes 1 and 2 by a factor of 2 and see even further speed-up and memory savings. There is no loss of accuracy as compared to downsampling after constructing the full tensor." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "S0 = np.kron(np.eye(int(shape[0]/2)), 0.5*np.ones((1,2)))\n", + "S2 = np.kron(np.eye(int(shape[2]/2)), 0.5*np.ones((1,2)))\n", + "S1 = np.array([15])" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPU times: user 6.18 ms, sys: 8.22 ms, total: 14.4 ms\n", + "Wall time: 1.78 ms\n" + ] + } + ], + "source": [ + "%%time\n", + "Xds = T.reconstruct(modes=[0,1,2], samples=[S0,S1,S2])" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Compression: 120.0 x\n" + ] + } + ], + "source": [ + "print(\n", + " f\"Compression: {Xf.data.nbytes/Xds.data.nbytes} x\"\n", + ")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## TODO: Compare visualizations\n", + "We can compare the results of reconstruction. There is no degredation in doing only a partial reconstruction. Downsampling is obviously lower resolution, but the same result as first doing the full reconstruction and then downsampling." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.11.3 ('.venv': venv)", + "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.11.3" + }, + "vscode": { + "interpreter": { + "hash": "6eee181e9c1d5c2b029214a9c9f230f0c0d2ab9f8894b5fd17e4d1b01872cf94" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From 5b3f607441cda8e94bbb94f63cb9f6356427e1ba Mon Sep 17 00:00:00 2001 From: Jeremy Myers Date: Thu, 21 Sep 2023 14:22:29 -0700 Subject: [PATCH 02/17] autoformat --- .../source/tutorial/TTENSOR_RECONSTRUCT.ipynb | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/docs/source/tutorial/TTENSOR_RECONSTRUCT.ipynb b/docs/source/tutorial/TTENSOR_RECONSTRUCT.ipynb index caca73d..1983f53 100644 --- a/docs/source/tutorial/TTENSOR_RECONSTRUCT.ipynb +++ b/docs/source/tutorial/TTENSOR_RECONSTRUCT.ipynb @@ -100,8 +100,8 @@ } ], "source": [ - "# Note: If the result is < 1.0 x, it will be unsurprising \n", - "# since the random generation process below wasn't expected \n", + "# Note: If the result is < 1.0 x, it will be unsurprising\n", + "# since the random generation process below wasn't expected\n", "# to return a low-rank approximation\n", "print(\n", " f\"Compression: {X.data.nbytes/(T.core.data.nbytes + np.sum([i.nbytes for i in T.factor_matrices]))} x\"\n", @@ -159,7 +159,7 @@ ], "source": [ "%%time\n", - "Xslice = T.reconstruct(modes=[1],samples=[15])" + "Xslice = T.reconstruct(modes=[1], samples=[15])" ] }, { @@ -176,9 +176,7 @@ } ], "source": [ - "print(\n", - " f\"Compression: {Xf.data.nbytes/Xslice.data.nbytes} x\"\n", - ")" + "print(f\"Compression: {Xf.data.nbytes/Xslice.data.nbytes} x\")" ] }, { @@ -195,8 +193,8 @@ "metadata": {}, "outputs": [], "source": [ - "S0 = np.kron(np.eye(int(shape[0]/2)), 0.5*np.ones((1,2)))\n", - "S2 = np.kron(np.eye(int(shape[2]/2)), 0.5*np.ones((1,2)))\n", + "S0 = np.kron(np.eye(int(shape[0] / 2)), 0.5 * np.ones((1, 2)))\n", + "S2 = np.kron(np.eye(int(shape[2] / 2)), 0.5 * np.ones((1, 2)))\n", "S1 = np.array([15])" ] }, @@ -216,7 +214,7 @@ ], "source": [ "%%time\n", - "Xds = T.reconstruct(modes=[0,1,2], samples=[S0,S1,S2])" + "Xds = T.reconstruct(modes=[0, 1, 2], samples=[S0, S1, S2])" ] }, { @@ -233,9 +231,7 @@ } ], "source": [ - "print(\n", - " f\"Compression: {Xf.data.nbytes/Xds.data.nbytes} x\"\n", - ")" + "print(f\"Compression: {Xf.data.nbytes/Xds.data.nbytes} x\")" ] }, { From 05a37a1c8c36074e3f351b0cc8d785162c8c5581 Mon Sep 17 00:00:00 2001 From: Jeremy Myers Date: Thu, 21 Sep 2023 14:25:43 -0700 Subject: [PATCH 03/17] Removed from main until completed in branch 248-tutorial-partial-reconstruction-of-a-tucker-tensor. --- .../source/tutorial/TTENSOR_RECONSTRUCT.ipynb | 272 ------------------ 1 file changed, 272 deletions(-) delete mode 100644 docs/source/tutorial/TTENSOR_RECONSTRUCT.ipynb diff --git a/docs/source/tutorial/TTENSOR_RECONSTRUCT.ipynb b/docs/source/tutorial/TTENSOR_RECONSTRUCT.ipynb deleted file mode 100644 index 1983f53..0000000 --- a/docs/source/tutorial/TTENSOR_RECONSTRUCT.ipynb +++ /dev/null @@ -1,272 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Partial Reconstruction of a Tucker Tensor\n", - "```\n", - "Copyright 2022 National Technology & Engineering Solutions of Sandia,\n", - "LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the\n", - "U.S. Government retains certain rights in this software.\n", - "```" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Benefits of Partial Reconstruction\n", - "An advantage of Tucker decomposition is that the tensor can be partially reconstructed without ever forming the *full* tensor. The `reconstruct()` member function does this, resulting in significant time and memory savings, as we demonstrate below." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [], - "source": [ - "import pyttb as ttb\n", - "import numpy as np" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## TODO: Decide if we want to use Miranda density tensor dataset\n", - "If so, the Miranda data is available at https://gitlab.com/tensors/tensor_data_miranda_sim. It loads a tensor `X` of size $384 \\times 384 \\times 256$." - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## For now, we will use random data." - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "shape = (20, 30, 50)\n", - "X = ttb.tenrand(shape)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Compute HOSVD\n", - "We compute the Tucker decomposition using ST-HOSVD with target relative error `0.001`." - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Computing HOSVD...\n", - "\n", - " Size of core: (20, 30, 50)\n", - "||X-T||/||X|| = 6.1027e-15 <= 0.001000 (tol)\n", - "CPU times: user 29.6 ms, sys: 63.5 ms, total: 93.1 ms\n", - "Wall time: 9.54 ms\n" - ] - } - ], - "source": [ - "%%time\n", - "T = ttb.hosvd(X, tol=0.001)" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Compression: 0.8875739644970414 x\n" - ] - } - ], - "source": [ - "# Note: If the result is < 1.0 x, it will be unsurprising\n", - "# since the random generation process below wasn't expected\n", - "# to return a low-rank approximation\n", - "print(\n", - " f\"Compression: {X.data.nbytes/(T.core.data.nbytes + np.sum([i.nbytes for i in T.factor_matrices]))} x\"\n", - ")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Full reconstruction\n", - "We can create a full reconstruction of the data using the full command. Not only is this expensive in computational time but also in memory. Now, let's see how long it takes to reconstruct the approximation to `X`." - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "CPU times: user 9.22 ms, sys: 16.9 ms, total: 26.1 ms\n", - "Wall time: 1.93 ms\n" - ] - } - ], - "source": [ - "%%time\n", - "Xf = T.full()" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Partial reconstruction\n", - "If we really only want part of the tensor, we can reconstruct just that part. Suppose we only want the `[:,15,:]` slice. The reconstruct function can do this much more efficiently with no loss in accuracy." - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "CPU times: user 6.28 ms, sys: 7.37 ms, total: 13.7 ms\n", - "Wall time: 1.88 ms\n" - ] - } - ], - "source": [ - "%%time\n", - "Xslice = T.reconstruct(modes=[1], samples=[15])" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Compression: 30.0 x\n" - ] - } - ], - "source": [ - "print(f\"Compression: {Xf.data.nbytes/Xslice.data.nbytes} x\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Down-sampling\n", - "Additionally, we may want to downsample high-dimensional data to something lower resolution. For example, here we downsample in modes 1 and 2 by a factor of 2 and see even further speed-up and memory savings. There is no loss of accuracy as compared to downsampling after constructing the full tensor." - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [], - "source": [ - "S0 = np.kron(np.eye(int(shape[0] / 2)), 0.5 * np.ones((1, 2)))\n", - "S2 = np.kron(np.eye(int(shape[2] / 2)), 0.5 * np.ones((1, 2)))\n", - "S1 = np.array([15])" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "CPU times: user 6.18 ms, sys: 8.22 ms, total: 14.4 ms\n", - "Wall time: 1.78 ms\n" - ] - } - ], - "source": [ - "%%time\n", - "Xds = T.reconstruct(modes=[0, 1, 2], samples=[S0, S1, S2])" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Compression: 120.0 x\n" - ] - } - ], - "source": [ - "print(f\"Compression: {Xf.data.nbytes/Xds.data.nbytes} x\")" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## TODO: Compare visualizations\n", - "We can compare the results of reconstruction. There is no degredation in doing only a partial reconstruction. Downsampling is obviously lower resolution, but the same result as first doing the full reconstruction and then downsampling." - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3.11.3 ('.venv': venv)", - "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.11.3" - }, - "vscode": { - "interpreter": { - "hash": "6eee181e9c1d5c2b029214a9c9f230f0c0d2ab9f8894b5fd17e4d1b01872cf94" - } - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} From b3101151339973d03ac4b4a8f75bb523d9767ab4 Mon Sep 17 00:00:00 2001 From: Jeremy Myers Date: Thu, 28 Sep 2023 11:44:05 -0700 Subject: [PATCH 04/17] Initial commit --- docs/source/for_matlab_users.rst | 314 +++++++++++++++++++++++++++++++ docs/source/getting_started.rst | 18 +- docs/source/index.rst | 7 +- 3 files changed, 332 insertions(+), 7 deletions(-) create mode 100644 docs/source/for_matlab_users.rst diff --git a/docs/source/for_matlab_users.rst b/docs/source/for_matlab_users.rst new file mode 100644 index 0000000..71eab29 --- /dev/null +++ b/docs/source/for_matlab_users.rst @@ -0,0 +1,314 @@ +pyttb for MATLAB users +====================== + + Already familiar with MATLAB Tensor Toolbox? To assist transitions from the + Tensor Toolbox for MATLAB to pyttb, this guide documents the mapping between + implementations and important differences. + +``ktensor`` +----------------- + +properties +^^^^^^^^^^ ++-----------------+------------------+-----------------------------------------------------------+ +| Matlab name | ``pyttb`` name | Description | ++=================+==================+===========================================================+ +|``ndims`` | ``ndims`` | Number of dimensions for a ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``size`` | ``shape`` | Size of ktensor, following ``numpy`` naming convention | ++-----------------+------------------+-----------------------------------------------------------+ + +methods +^^^^^^^ ++-----------------+------------------+-----------------------------------------------------------+ +| Matlab name | ``pyttb`` name | Description | ++=================+==================+===========================================================+ +|``arrange`` | ``arrange`` | Arranges the rank-1 components of a ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``datadisp`` | | Special display of a ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``disp`` | ``__str__`` | Command window display for a ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``display`` | ``__repr__`` | Command window display for a ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``double`` | ``double`` | Convert a ktensor to a double array | ++-----------------+------------------+-----------------------------------------------------------+ +|``end`` | ``end`` | Last index of indexing expression for ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``extract`` | ``extract`` | Creates a new ktensor with only the specified components | ++-----------------+------------------+-----------------------------------------------------------+ +|``fixsigns`` | ``extract`` | Fix sign ambiguity of a ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``full`` | ``full`` | Convert a ktensor to a (dense) tensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``innerprod`` | ``innerprod`` | Efficient inner product with a ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``isequal`` | ``isequal`` | True if each datum of two ktensor's are numerically equal | ++-----------------+------------------+-----------------------------------------------------------+ +|``isscalar`` | ``isscalar`` | False for ktensors | ++-----------------+------------------+-----------------------------------------------------------+ +|``issymmetric`` | ``issymmetric`` | Verify that a ktensor X is symmetric in all modes | ++-----------------+------------------+-----------------------------------------------------------+ +|``ktensor`` | ``ktensor`` | Tensor stored as a Kruskal operator (decomposed) | ++-----------------+------------------+-----------------------------------------------------------+ +|``mask`` | ``mask`` | Extract values as specified by a mask tensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``minus`` | ``__sub__`` | Binary subtraction for ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``mtimes`` | ``__mul__`` | Implement A\*B (scalar multiply) for ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``mttkrp`` | ``mttkrp`` | Matricized tensor times Khatri-Rao product for ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``ncomponents`` | ``ncomponents`` | Number of components for a ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``norm`` | ``norm`` | Frobenius norm of a ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``normalize`` | ``normalize`` | Normalizes the columns of the factor matrices | ++-----------------+------------------+-----------------------------------------------------------+ +|``nvecs`` | ``nvecs`` | Compute the leading mode-n vectors for a ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``permute`` | ``permute`` | Permute dimensions of a ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``plus`` | ``__add__`` | Binary addition for ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``redistribute`` | ``redistribute`` | Distribute lambda values to a specified mode | ++-----------------+------------------+-----------------------------------------------------------+ +|``score`` | ``score`` | Checks if two ktensors match except for permutation | ++-----------------+------------------+-----------------------------------------------------------+ +|``subsasgn`` | ``__setitem__`` | Subscripted assignment for ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``subsref`` | ``__getitem__`` | Subscripted reference for a ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``symmetrize`` | ``symmetrize`` | Symmetrize a ktensor X in all modes | ++-----------------+------------------+-----------------------------------------------------------+ +|``times`` | ``__mul__`` | Element-wise multiplication for ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``tocell`` | | Convert X to a cell array | ++-----------------+------------------+-----------------------------------------------------------+ +|``tovec`` | ``tovec`` | Convert Ktensor to vector | ++-----------------+------------------+-----------------------------------------------------------+ +|``ttm`` | ``ttm`` | Tensor times matrix for ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``ttv`` | ``ttv`` | Tensor times vector for ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``uminus`` | ``__neg__`` | Unary minus for ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``update`` | ``update`` | Update one or more modes of the ktensor with new data | ++-----------------+------------------+-----------------------------------------------------------+ +|``uplus`` | ``__pos__`` | Unary plus for a ktensor | ++-----------------+------------------+-----------------------------------------------------------+ +|``viz`` | | Visualize a ktensor | ++-----------------+------------------+-----------------------------------------------------------+ + +``tensor`` +------------------ + +properties +^^^^^^^^^^ ++-------------+----------------+----------------------------------------------------------+ +| Matlab name | ``pyttb`` name | Description | ++=============+================+==========================================================+ +| ``ndims`` | ``ndims`` | Return the number of dimensions of a tensor | ++-------------+----------------+----------------------------------------------------------+ +| ``nnz`` | ``nnz`` | Number of nonzeros for tensors | ++-------------+----------------+----------------------------------------------------------+ +| ``size`` | ``shape`` | Tensor dimensions, following ``numpy`` naming convention | ++-------------+----------------+----------------------------------------------------------+ + +methods +^^^^^^^ ++-----------------+-------------------------+-------------------------------------------------------------------+ +| Matlab name | ``pyttb`` name | Description | ++=================+=========================+===================================================================+ +| ``and`` | ``logical_and`` | Logical AND (&) for tensors | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``collapse`` | ``collapse`` | Collapse tensor along specified dimensions | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``contract`` | ``contract`` | Contract tensor along two dimensions (array trace) | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``disp`` | ``__str__`` | Command window display of a tensor | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``display`` | ``__repr__`` | Command window display of a tensor | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``double`` | ``double`` | Convert tensor to double array | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``end`` | ``end`` | Last index of indexing expression for tensor | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``eq`` | ``__eq__`` | Equal (==) for tensors | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``exp`` | ``exp`` | Exponential for tensors | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``find`` | ``find`` | Find subscripts of nonzero elements in a tensor | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``full`` | ``full`` | Convert to a (dense) tensor | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``ge`` | ``__ge__`` | Greater than or equal (>=) for tensors | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``gt`` | ``__gt__`` | Greater than (>) for tensors | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``innerprod`` | ``innerprod`` | Efficient inner product with a tensor | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``isequal`` | ``isequal`` | Verify equality for tensors | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``isscalar`` | N/A | False for tensors | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``issymmetric`` | ``issymmetric`` | Verify that a tensor X is symmetric in specified modes | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``ldivide`` | ``__truediv__`` | Left array divide for tensor | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``le`` | ``__le__`` | Less than or equal (<=) for tensor | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``lt`` | ``__lt__`` | Less than (<) for tensor | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``mask`` | ``mask`` | Extract values as specified by a mask tensor | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``minus`` | ``__sub__`` | Binary subtraction (-) for tensors | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``mldivide`` | ``__truediv__`` | Slash left division for tensors | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``mrdivide`` | ``__rtruediv__`` | Slash right division for tensors | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``mtimes`` | ``__rmul__`` | tensor-scalar multiplication | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``mttkrp`` | ``mttkrp`` | Matricized tensor times Khatri-Rao product for tensor | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``ne`` | ``__ne__`` | Not equal (~=) for tensors | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``norm`` | ``norm`` | Frobenius norm of a tensor | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``not`` | ``logical_not`` | Logical NOT (~) for tensors | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``nvecs`` | ``nvecs`` | Compute the leading mode-n vectors for a tensor. | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``or`` | ``logical_or`` | Logical OR (|) for tensors | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``permute`` | ``permute`` | Permute tensor dimensions | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``plus`` | ``__add__`` | Binary addition (+) for tensors | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``power`` | ``__pow__`` | Elementwise power (.^) operator for a tensor | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``rdivide`` | ``__rtruediv__`` | Right array divide for tensors | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``reshape`` | ``reshape`` | Change tensor size | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``scale`` | ``scale`` | Scale along specified dimensions of tensor | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``squeeze`` | ``squeeze`` | Remove singleton dimensions from a tensor | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``subsasgn`` | ``__setitem__`` | Subscripted assignment for a tensor | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``subsref`` | ``__getitem__`` | Subscripted reference for tensors | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``symmetrize`` | ``symmetrize`` | Symmetrize a tensor X in specified modes | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``tenfun`` | ``tt_tenfun`` | Apply a function to each element in a tensor (in ``pyttb_utils``) | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``tensor`` | 1. ``from_data`` | 1. Create from: ``np.ndarray`` and ``shape`` | +| | 2. ``from_tensor_type`` | 2. Create from other ``pyttb`` tensor type: | +| | | - ``tensor`` | +| | | - ``ktensor`` | +| | | - ``ttensor`` | +| | | - ``sptensor`` | +| | | - ``sumtensor`` | +| | | - ``symtensor`` | +| | | - ``symktensor`` | +| | | - ``tenmat`` | +| | 3. ``from_function`` | 3. Create from: function handle and ``shape`` | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``times`` | ``__mul__`` | Array multiplication for tensors | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``transpose`` | N/A | not defined on tensors (removed) | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``ttm`` | ``ttm`` | Tensor times matrix | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``ttsv`` | ``ttsv`` | Tensor times same vector in multiple modes | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``ttt`` | ``ttt`` | Tensor multiplication (tensor times tensor) | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``ttv`` | ``ttv`` | Tensor times vector | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``uminus`` | ``__neg__`` | Unary minus (-) for tensors | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``uplus`` | ``__pos__`` | Unary plus (+) for tensors | ++-----------------+-------------------------+-------------------------------------------------------------------+ +| ``xor`` | ``logical_xor`` | Logical EXCLUSIVE OR for tensors | ++-----------------+-------------------------+-------------------------------------------------------------------+ + +``sptensor`` +---------------- + +properties +^^^^^^^^^^ + +methods +^^^^^^^ + +``tenmat`` +---------------- + +properties +^^^^^^^^^^ + +methods +^^^^^^^ + +``ttensor`` +----------------- + +properties +^^^^^^^^^^ + +methods +^^^^^^^ + +``sumtensor`` +------------------- + +properties +^^^^^^^^^^ + +methods +^^^^^^^ + +``symtensor`` +------------------- + +properties +^^^^^^^^^^ + +methods +^^^^^^^ + +``symktensor`` +-------------------- + +properties +^^^^^^^^^^ + +methods +^^^^^^^ + +``cp_als`` +-------------------- + +``cp_apr`` +-------------------- + +``gcp_opt`` +--------------------- + +``hosvd`` +------------------- + +``khatrirao`` +----------------------- + +``sptenmat`` +---------------------- + +``tenmat`` +-------------------- + +``tucker_als`` +------------------------ diff --git a/docs/source/getting_started.rst b/docs/source/getting_started.rst index 1e4f0c3..3579e9d 100644 --- a/docs/source/getting_started.rst +++ b/docs/source/getting_started.rst @@ -1,6 +1,18 @@ -:orphan: - Getting Started *************** -In construction \ No newline at end of file +Tutorials +========= + +.. toctree:: + :maxdepth: 1 + + tutorials.rst + +Coming from MATLAB +================== + +.. toctree:: + :maxdepth: 1 + + for_matlab_users.rst \ No newline at end of file diff --git a/docs/source/index.rst b/docs/source/index.rst index eac5b58..90b84da 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -37,14 +37,13 @@ algorithms for computing low-rank tensor models. .. _Algorithms: algorithms.html -Tutorials -========= +Getting Started +=============== .. toctree:: :maxdepth: 1 - tutorials.rst - + getting_started.rst Python API ================ From 38ec12b077f0a86131be027310a0de9616d66d85 Mon Sep 17 00:00:00 2001 From: Jeremy Myers Date: Thu, 28 Sep 2023 15:26:06 -0700 Subject: [PATCH 05/17] Basic matlab to pyttb mapping. TODOs: 1. insert hyperlinks to both ttb and pyttb documentation, 2. add algorithms. --- docs/source/for_matlab_users.rst | 316 +----------------------------- docs/source/matlab/common.rst | 83 ++++++++ docs/source/matlab/ktensor.rst | 26 +++ docs/source/matlab/sptensor.rst | 14 ++ docs/source/matlab/sumtensor.rst | 8 + docs/source/matlab/symktensor.rst | 8 + docs/source/matlab/symtensor.rst | 8 + docs/source/matlab/tenmat.rst | 20 ++ docs/source/matlab/tensor.rst | 12 ++ docs/source/matlab/ttensor.rst | 21 ++ 10 files changed, 208 insertions(+), 308 deletions(-) create mode 100644 docs/source/matlab/common.rst create mode 100644 docs/source/matlab/ktensor.rst create mode 100644 docs/source/matlab/sptensor.rst create mode 100644 docs/source/matlab/sumtensor.rst create mode 100644 docs/source/matlab/symktensor.rst create mode 100644 docs/source/matlab/symtensor.rst create mode 100644 docs/source/matlab/tenmat.rst create mode 100644 docs/source/matlab/tensor.rst create mode 100644 docs/source/matlab/ttensor.rst diff --git a/docs/source/for_matlab_users.rst b/docs/source/for_matlab_users.rst index 71eab29..7789661 100644 --- a/docs/source/for_matlab_users.rst +++ b/docs/source/for_matlab_users.rst @@ -1,314 +1,14 @@ pyttb for MATLAB users ====================== - Already familiar with MATLAB Tensor Toolbox? To assist transitions from the - Tensor Toolbox for MATLAB to pyttb, this guide documents the mapping between - implementations and important differences. +Already familiar with MATLAB Tensor Toolbox? To assist transitions from the +Tensor Toolbox for MATLAB to pyttb, this guide documents some key differences. -``ktensor`` ------------------ +In the ``pyttb`` calling conventions below we'll use the following notation: + - ``pyttb.``: ``X`` (and ``Y`` for binary operators) + - ````: ``a`` for scalars. -properties -^^^^^^^^^^ -+-----------------+------------------+-----------------------------------------------------------+ -| Matlab name | ``pyttb`` name | Description | -+=================+==================+===========================================================+ -|``ndims`` | ``ndims`` | Number of dimensions for a ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``size`` | ``shape`` | Size of ktensor, following ``numpy`` naming convention | -+-----------------+------------------+-----------------------------------------------------------+ +.. toctree:: + :glob: -methods -^^^^^^^ -+-----------------+------------------+-----------------------------------------------------------+ -| Matlab name | ``pyttb`` name | Description | -+=================+==================+===========================================================+ -|``arrange`` | ``arrange`` | Arranges the rank-1 components of a ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``datadisp`` | | Special display of a ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``disp`` | ``__str__`` | Command window display for a ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``display`` | ``__repr__`` | Command window display for a ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``double`` | ``double`` | Convert a ktensor to a double array | -+-----------------+------------------+-----------------------------------------------------------+ -|``end`` | ``end`` | Last index of indexing expression for ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``extract`` | ``extract`` | Creates a new ktensor with only the specified components | -+-----------------+------------------+-----------------------------------------------------------+ -|``fixsigns`` | ``extract`` | Fix sign ambiguity of a ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``full`` | ``full`` | Convert a ktensor to a (dense) tensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``innerprod`` | ``innerprod`` | Efficient inner product with a ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``isequal`` | ``isequal`` | True if each datum of two ktensor's are numerically equal | -+-----------------+------------------+-----------------------------------------------------------+ -|``isscalar`` | ``isscalar`` | False for ktensors | -+-----------------+------------------+-----------------------------------------------------------+ -|``issymmetric`` | ``issymmetric`` | Verify that a ktensor X is symmetric in all modes | -+-----------------+------------------+-----------------------------------------------------------+ -|``ktensor`` | ``ktensor`` | Tensor stored as a Kruskal operator (decomposed) | -+-----------------+------------------+-----------------------------------------------------------+ -|``mask`` | ``mask`` | Extract values as specified by a mask tensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``minus`` | ``__sub__`` | Binary subtraction for ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``mtimes`` | ``__mul__`` | Implement A\*B (scalar multiply) for ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``mttkrp`` | ``mttkrp`` | Matricized tensor times Khatri-Rao product for ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``ncomponents`` | ``ncomponents`` | Number of components for a ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``norm`` | ``norm`` | Frobenius norm of a ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``normalize`` | ``normalize`` | Normalizes the columns of the factor matrices | -+-----------------+------------------+-----------------------------------------------------------+ -|``nvecs`` | ``nvecs`` | Compute the leading mode-n vectors for a ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``permute`` | ``permute`` | Permute dimensions of a ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``plus`` | ``__add__`` | Binary addition for ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``redistribute`` | ``redistribute`` | Distribute lambda values to a specified mode | -+-----------------+------------------+-----------------------------------------------------------+ -|``score`` | ``score`` | Checks if two ktensors match except for permutation | -+-----------------+------------------+-----------------------------------------------------------+ -|``subsasgn`` | ``__setitem__`` | Subscripted assignment for ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``subsref`` | ``__getitem__`` | Subscripted reference for a ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``symmetrize`` | ``symmetrize`` | Symmetrize a ktensor X in all modes | -+-----------------+------------------+-----------------------------------------------------------+ -|``times`` | ``__mul__`` | Element-wise multiplication for ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``tocell`` | | Convert X to a cell array | -+-----------------+------------------+-----------------------------------------------------------+ -|``tovec`` | ``tovec`` | Convert Ktensor to vector | -+-----------------+------------------+-----------------------------------------------------------+ -|``ttm`` | ``ttm`` | Tensor times matrix for ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``ttv`` | ``ttv`` | Tensor times vector for ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``uminus`` | ``__neg__`` | Unary minus for ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``update`` | ``update`` | Update one or more modes of the ktensor with new data | -+-----------------+------------------+-----------------------------------------------------------+ -|``uplus`` | ``__pos__`` | Unary plus for a ktensor | -+-----------------+------------------+-----------------------------------------------------------+ -|``viz`` | | Visualize a ktensor | -+-----------------+------------------+-----------------------------------------------------------+ - -``tensor`` ------------------- - -properties -^^^^^^^^^^ -+-------------+----------------+----------------------------------------------------------+ -| Matlab name | ``pyttb`` name | Description | -+=============+================+==========================================================+ -| ``ndims`` | ``ndims`` | Return the number of dimensions of a tensor | -+-------------+----------------+----------------------------------------------------------+ -| ``nnz`` | ``nnz`` | Number of nonzeros for tensors | -+-------------+----------------+----------------------------------------------------------+ -| ``size`` | ``shape`` | Tensor dimensions, following ``numpy`` naming convention | -+-------------+----------------+----------------------------------------------------------+ - -methods -^^^^^^^ -+-----------------+-------------------------+-------------------------------------------------------------------+ -| Matlab name | ``pyttb`` name | Description | -+=================+=========================+===================================================================+ -| ``and`` | ``logical_and`` | Logical AND (&) for tensors | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``collapse`` | ``collapse`` | Collapse tensor along specified dimensions | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``contract`` | ``contract`` | Contract tensor along two dimensions (array trace) | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``disp`` | ``__str__`` | Command window display of a tensor | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``display`` | ``__repr__`` | Command window display of a tensor | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``double`` | ``double`` | Convert tensor to double array | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``end`` | ``end`` | Last index of indexing expression for tensor | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``eq`` | ``__eq__`` | Equal (==) for tensors | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``exp`` | ``exp`` | Exponential for tensors | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``find`` | ``find`` | Find subscripts of nonzero elements in a tensor | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``full`` | ``full`` | Convert to a (dense) tensor | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``ge`` | ``__ge__`` | Greater than or equal (>=) for tensors | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``gt`` | ``__gt__`` | Greater than (>) for tensors | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``innerprod`` | ``innerprod`` | Efficient inner product with a tensor | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``isequal`` | ``isequal`` | Verify equality for tensors | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``isscalar`` | N/A | False for tensors | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``issymmetric`` | ``issymmetric`` | Verify that a tensor X is symmetric in specified modes | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``ldivide`` | ``__truediv__`` | Left array divide for tensor | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``le`` | ``__le__`` | Less than or equal (<=) for tensor | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``lt`` | ``__lt__`` | Less than (<) for tensor | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``mask`` | ``mask`` | Extract values as specified by a mask tensor | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``minus`` | ``__sub__`` | Binary subtraction (-) for tensors | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``mldivide`` | ``__truediv__`` | Slash left division for tensors | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``mrdivide`` | ``__rtruediv__`` | Slash right division for tensors | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``mtimes`` | ``__rmul__`` | tensor-scalar multiplication | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``mttkrp`` | ``mttkrp`` | Matricized tensor times Khatri-Rao product for tensor | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``ne`` | ``__ne__`` | Not equal (~=) for tensors | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``norm`` | ``norm`` | Frobenius norm of a tensor | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``not`` | ``logical_not`` | Logical NOT (~) for tensors | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``nvecs`` | ``nvecs`` | Compute the leading mode-n vectors for a tensor. | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``or`` | ``logical_or`` | Logical OR (|) for tensors | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``permute`` | ``permute`` | Permute tensor dimensions | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``plus`` | ``__add__`` | Binary addition (+) for tensors | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``power`` | ``__pow__`` | Elementwise power (.^) operator for a tensor | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``rdivide`` | ``__rtruediv__`` | Right array divide for tensors | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``reshape`` | ``reshape`` | Change tensor size | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``scale`` | ``scale`` | Scale along specified dimensions of tensor | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``squeeze`` | ``squeeze`` | Remove singleton dimensions from a tensor | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``subsasgn`` | ``__setitem__`` | Subscripted assignment for a tensor | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``subsref`` | ``__getitem__`` | Subscripted reference for tensors | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``symmetrize`` | ``symmetrize`` | Symmetrize a tensor X in specified modes | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``tenfun`` | ``tt_tenfun`` | Apply a function to each element in a tensor (in ``pyttb_utils``) | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``tensor`` | 1. ``from_data`` | 1. Create from: ``np.ndarray`` and ``shape`` | -| | 2. ``from_tensor_type`` | 2. Create from other ``pyttb`` tensor type: | -| | | - ``tensor`` | -| | | - ``ktensor`` | -| | | - ``ttensor`` | -| | | - ``sptensor`` | -| | | - ``sumtensor`` | -| | | - ``symtensor`` | -| | | - ``symktensor`` | -| | | - ``tenmat`` | -| | 3. ``from_function`` | 3. Create from: function handle and ``shape`` | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``times`` | ``__mul__`` | Array multiplication for tensors | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``transpose`` | N/A | not defined on tensors (removed) | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``ttm`` | ``ttm`` | Tensor times matrix | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``ttsv`` | ``ttsv`` | Tensor times same vector in multiple modes | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``ttt`` | ``ttt`` | Tensor multiplication (tensor times tensor) | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``ttv`` | ``ttv`` | Tensor times vector | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``uminus`` | ``__neg__`` | Unary minus (-) for tensors | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``uplus`` | ``__pos__`` | Unary plus (+) for tensors | -+-----------------+-------------------------+-------------------------------------------------------------------+ -| ``xor`` | ``logical_xor`` | Logical EXCLUSIVE OR for tensors | -+-----------------+-------------------------+-------------------------------------------------------------------+ - -``sptensor`` ----------------- - -properties -^^^^^^^^^^ - -methods -^^^^^^^ - -``tenmat`` ----------------- - -properties -^^^^^^^^^^ - -methods -^^^^^^^ - -``ttensor`` ------------------ - -properties -^^^^^^^^^^ - -methods -^^^^^^^ - -``sumtensor`` -------------------- - -properties -^^^^^^^^^^ - -methods -^^^^^^^ - -``symtensor`` -------------------- - -properties -^^^^^^^^^^ - -methods -^^^^^^^ - -``symktensor`` --------------------- - -properties -^^^^^^^^^^ - -methods -^^^^^^^ - -``cp_als`` --------------------- - -``cp_apr`` --------------------- - -``gcp_opt`` ---------------------- - -``hosvd`` -------------------- - -``khatrirao`` ------------------------ - -``sptenmat`` ----------------------- - -``tenmat`` --------------------- - -``tucker_als`` ------------------------- + matlab/* \ No newline at end of file diff --git a/docs/source/matlab/common.rst b/docs/source/matlab/common.rst new file mode 100644 index 0000000..0caad94 --- /dev/null +++ b/docs/source/matlab/common.rst @@ -0,0 +1,83 @@ +General key differences +----------------------- + +Data members +^^^^^^^^^^^^ ++-----------------+----------------------+------------------------------------------------------------------------+ +| MATLAB name | ``pyttb`` name | Calling convention | ++=================+======================+========================================================================+ +| ``size`` | ``shape`` | ``X.shape`` | ++-----------------+----------------------+------------------------------------------------------------------------+ + +Methods +^^^^^^^ ++-----------------+----------------------+------------------------------------------------------------------------+ +| MATLAB name | ``pyttb`` name | Calling convention | ++=================+======================+========================================================================+ +| ``and`` | ``logical_and`` | ``X.logical_and(Y)`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``disp`` | ``__str__`` | ``X`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``display`` | ``__repr__`` | ``print(X)`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``eq`` | ``__eq__`` | ``X == Y`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``ge`` | ``__ge__`` | ``X >= Y`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``gt`` | ``__gt__`` | ``X > Y`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``ldivide`` | ``__truediv__`` | ``X / Y`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``le`` | ``__le__`` | ``X <= Y`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``lt`` | ``__lt__`` | ``X < Y`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``minus`` | ``__sub__`` | ``X - Y`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``mldivide`` | ``__truediv__`` | ``X / Y`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``mrdivide`` | ``__rtruediv__`` | ``X / Y`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``mtimes`` | ``__rmul__`` | ``a * X`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``ne`` | ``__ne__`` | ``X != Y`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``not`` | ``logical_not`` | ``X.logical_not(Y)`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``or`` | ``logical_or`` | ``X.logical_or(Y)`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``plus`` | ``__add__`` | ``X + Y`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``power`` | ``__pow__`` | ``X ** a`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``rdivide`` | ``__rtruediv__`` | ``X / Y`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``subsasgn`` | ``__setitem__`` | ``X[index] = a`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``subsref`` | ``__getitem__`` | ``X[index]`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``tenfun`` | ``tt_tenfun`` | e.g., ``pyttb.tt_tenfun(lambda x: x + 1, A)`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``times`` | ``__mul__`` | ``X * Y`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``uminus`` | ``__neg__`` | ``-X`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``uplus`` | ``__pos__`` | ``+X`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``xor`` | ``logical_xor`` | ``X.logical_xor(Y)`` | ++-----------------+----------------------+------------------------------------------------------------------------+ + +Copying +^^^^^^^^^^^^^^^^^^^^ +Copying a ``pyttb`` tensor works differently than MATLAB. For example in MATLAB, copying a tensor ``Y`` +as ``Y = X`` returns a tensor ``Y`` that is independent of ``X``. Changing the value of ``Y`` does not +change the value of ``X``. However, the same syntax in ``pyttb``, ``Y = X``, returns a *shallow copy* of ``X``; +the shallow copy ``Y`` is a *reference* to ``X``. For that reason, each ``pyttb`` tensor class provides a ``copy()`` +method that returns a *deep copy* ``Y`` that is independent of ``X``, which is called as ``Y = X.copy()``. + +MATLAB methods not included in ``pyttb`` +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +- ``datadisp`` +- ``isscalar`` +- ``transpose`` +- ``tocell`` \ No newline at end of file diff --git a/docs/source/matlab/ktensor.rst b/docs/source/matlab/ktensor.rst new file mode 100644 index 0000000..580b618 --- /dev/null +++ b/docs/source/matlab/ktensor.rst @@ -0,0 +1,26 @@ +``ktensor`` +----------------- + +Data members +^^^^^^^^^^^^ ++-----------------+----------------------+------------------------------------------------------------------------+ +| MATLAB name | ``pyttb`` name | Calling convention | ++=================+======================+========================================================================+ +| ``lambda`` | ``weights`` | ``X.weights`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``U`` | ``factor_matrices`` | ``X.factor_matrices`` | ++-----------------+----------------------+------------------------------------------------------------------------+ + +Methods +^^^^^^^ ++-----------------+----------------------+------------------------------------------------------------------------+ +| MATLAB name | ``pyttb`` name | Calling convention | ++=================+======================+========================================================================+ +| | ``from_vector`` | ``ttb.ktensor.from_vector(data[:], shape)`` | +| ``ktensor`` +----------------------+------------------------------------------------------------------------+ +| | ``from_function`` | ``ttb.tensor.from_function(, shape)`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``tensor`` | ``to_tensor`` | ``X.to_tensor()`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``viz`` | | | ++-----------------+----------------------+------------------------------------------------------------------------+ \ No newline at end of file diff --git a/docs/source/matlab/sptensor.rst b/docs/source/matlab/sptensor.rst new file mode 100644 index 0000000..d9bf99f --- /dev/null +++ b/docs/source/matlab/sptensor.rst @@ -0,0 +1,14 @@ +``sptensor`` +---------------- + +Methods +^^^^^^^ ++-----------------+----------------------+------------------------------------------------------------------------+ +| MATLAB name | ``pyttb`` name | Calling convention | ++=================+======================+========================================================================+ +| | ``from_function`` | ``ttb.sptensor.from_function(, shape)`` | +| ``sptensor`` +----------------------+------------------------------------------------------------------------+ +| | ``from_aggregator`` | ``ttb.sptensor.from_aggregator(subs, vals, shape, function_handle)`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``tensor`` | ``to_tensor`` | ``X.to_tensor()`` | ++-----------------+----------------------+------------------------------------------------------------------------+ \ No newline at end of file diff --git a/docs/source/matlab/sumtensor.rst b/docs/source/matlab/sumtensor.rst new file mode 100644 index 0000000..38448c0 --- /dev/null +++ b/docs/source/matlab/sumtensor.rst @@ -0,0 +1,8 @@ +``sumtensor`` +------------------- + +Data members +^^^^^^^^^^^^ + +Methods +^^^^^^^ \ No newline at end of file diff --git a/docs/source/matlab/symktensor.rst b/docs/source/matlab/symktensor.rst new file mode 100644 index 0000000..19e215a --- /dev/null +++ b/docs/source/matlab/symktensor.rst @@ -0,0 +1,8 @@ +``symktensor`` +-------------------- + +Data members +^^^^^^^^^^^^ + +Methods +^^^^^^^ \ No newline at end of file diff --git a/docs/source/matlab/symtensor.rst b/docs/source/matlab/symtensor.rst new file mode 100644 index 0000000..8d673c3 --- /dev/null +++ b/docs/source/matlab/symtensor.rst @@ -0,0 +1,8 @@ +``symtensor`` +------------------- + +Data members +^^^^^^^^^^^^ + +Methods +^^^^^^^ \ No newline at end of file diff --git a/docs/source/matlab/tenmat.rst b/docs/source/matlab/tenmat.rst new file mode 100644 index 0000000..28c3cf3 --- /dev/null +++ b/docs/source/matlab/tenmat.rst @@ -0,0 +1,20 @@ +``tenmat`` +---------------- + +Data members +^^^^^^^^^^^^ + ++-----------------+----------------------+------------------------------------------------------------------------+ +| MATLAB name | ``pyttb`` name | Calling convention | ++=================+======================+========================================================================+ +| | ``tshape`` | ``X.tshape`` | ++-----------------+----------------------+------------------------------------------------------------------------+ + +Methods +^^^^^^^ + ++-----------------+----------------------+------------------------------------------------------------------------+ +| MATLAB name | ``pyttb`` name | Calling convention | ++=================+======================+========================================================================+ +| ``tensor`` | ``to_tensor`` | ``X.to_tensor()`` | ++-----------------+----------------------+------------------------------------------------------------------------+ \ No newline at end of file diff --git a/docs/source/matlab/tensor.rst b/docs/source/matlab/tensor.rst new file mode 100644 index 0000000..9a8a7d3 --- /dev/null +++ b/docs/source/matlab/tensor.rst @@ -0,0 +1,12 @@ +``tensor`` +------------------ + +Methods +^^^^^^^ ++-----------------+----------------------+------------------------------------------------------------------------+ +| MATLAB name | ``pyttb`` name | Calling convention | ++=================+======================+========================================================================+ +| ``tensor`` | ``from_function`` | ``ttb.tensor.from_function(, shape)`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``sptensor`` | ``to_sptensor`` | ``X.to_sptensor()`` | ++-----------------+----------------------+------------------------------------------------------------------------+ \ No newline at end of file diff --git a/docs/source/matlab/ttensor.rst b/docs/source/matlab/ttensor.rst new file mode 100644 index 0000000..bcd92cc --- /dev/null +++ b/docs/source/matlab/ttensor.rst @@ -0,0 +1,21 @@ +``ttensor`` +----------------- + +Data members +^^^^^^^^^^^^ ++-----------------+----------------------+------------------------------------------------------------------------+ +| MATLAB name | ``pyttb`` name | Calling convention | ++=================+======================+========================================================================+ +| ``U`` | ``factors`` | ``X.factors`` | ++-----------------+----------------------+------------------------------------------------------------------------+ + +Methods +^^^^^^^ + ++-----------------+----------------------+------------------------------------------------------------------------+ +| MATLAB name | ``pyttb`` name | Calling convention | ++=================+======================+========================================================================+ +| ``size`` | ``shape`` | ``X.shape()`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``tensor`` | ``to_tensor`` | ``X.to_tensor()`` | ++-----------------+----------------------+------------------------------------------------------------------------+ From ccef927ad4dfcfa7555ba7fed8bd1758b24f1561 Mon Sep 17 00:00:00 2001 From: Jeremy Myers Date: Mon, 2 Oct 2023 07:47:00 -0700 Subject: [PATCH 06/17] nbstripout --- docs/source/tutorial/algorithm_gcp_opt.ipynb | 25 +------------------- 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/docs/source/tutorial/algorithm_gcp_opt.ipynb b/docs/source/tutorial/algorithm_gcp_opt.ipynb index 875040e..cb22fbc 100644 --- a/docs/source/tutorial/algorithm_gcp_opt.ipynb +++ b/docs/source/tutorial/algorithm_gcp_opt.ipynb @@ -597,30 +597,7 @@ ] } ], - "metadata": { - "kernelspec": { - "display_name": "Python 3.6.8 64-bit", - "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.9.6" - }, - "vscode": { - "interpreter": { - "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" - } - } - }, + "metadata": {}, "nbformat": 4, "nbformat_minor": 4 } From a7b595fcc1253be9cd67b69d4e9a95771ea21c3f Mon Sep 17 00:00:00 2001 From: Jeremy Myers Date: Tue, 12 Dec 2023 10:38:25 -0800 Subject: [PATCH 07/17] Initial version ready. --- docs/source/matlab/ktensor.rst | 6 ++++-- docs/source/matlab/sptenmat.rst | 30 ++++++++++++++++++++++++++++++ docs/source/matlab/sumtensor.rst | 8 +++++--- docs/source/matlab/tenmat.rst | 8 +++++--- 4 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 docs/source/matlab/sptenmat.rst diff --git a/docs/source/matlab/ktensor.rst b/docs/source/matlab/ktensor.rst index 580b618..e645c27 100644 --- a/docs/source/matlab/ktensor.rst +++ b/docs/source/matlab/ktensor.rst @@ -22,5 +22,7 @@ Methods +-----------------+----------------------+------------------------------------------------------------------------+ | ``tensor`` | ``to_tensor`` | ``X.to_tensor()`` | +-----------------+----------------------+------------------------------------------------------------------------+ -| ``viz`` | | | -+-----------------+----------------------+------------------------------------------------------------------------+ \ No newline at end of file + +MATLAB methods not included in ``pyttb`` +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +* ``viz`` \ No newline at end of file diff --git a/docs/source/matlab/sptenmat.rst b/docs/source/matlab/sptenmat.rst new file mode 100644 index 0000000..bffbc76 --- /dev/null +++ b/docs/source/matlab/sptenmat.rst @@ -0,0 +1,30 @@ +``sptenmat`` +------------ + +Data members +^^^^^^^^^^^^ ++-----------------+----------------------+------------------------------------------------------------------------+ +| MATLAB name | ``pyttb`` name | Calling convention | ++=================+======================+========================================================================+ +| ``size`` | ``shape`` | ``X.shape`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``tsize`` | ``tshape`` | ``X.tshape`` | ++-----------------+----------------------+------------------------------------------------------------------------+ + +Methods +^^^^^^^ ++-----------------+----------------------+------------------------------------------------------------------------+ +| MATLAB name | ``pyttb`` name | Calling convention | ++=================+======================+========================================================================+ +| | ``from_data`` | ``A = ttb.sptenmat.from_data(subs, vals, rdims, cdims, tshape)`` | +| +----------------------+------------------------------------------------------------------------+ +| ``sptenmat`` | ``from_tensor_type`` | ``A = ttb.sptenmat.from_tensor_type(X, np.array([0]))`` | +| +----------------------+------------------------------------------------------------------------+ +| | ``from_array`` | ``A = ttb.sptenmat.from_data(B, rdims, cdims, tshape)`` | ++-----------------+----------------------+------------------------------------------------------------------------+ +| ``sptensor`` | ``to_sptensor`` | ``X.to_sptensor()`` | ++-----------------+----------------------+------------------------------------------------------------------------+ + +MATLAB methods not included in ``pyttb`` +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +* ``aatx`` \ No newline at end of file diff --git a/docs/source/matlab/sumtensor.rst b/docs/source/matlab/sumtensor.rst index 38448c0..4f96e9b 100644 --- a/docs/source/matlab/sumtensor.rst +++ b/docs/source/matlab/sumtensor.rst @@ -3,6 +3,8 @@ Data members ^^^^^^^^^^^^ - -Methods -^^^^^^^ \ No newline at end of file ++-----------------+----------------------+------------------------------------------------------------------------+ +| MATLAB name | ``pyttb`` name | Calling convention | ++=================+======================+========================================================================+ +| ``part`` | ``parts`` | ``X.parts`` | ++-----------------+----------------------+------------------------------------------------------------------------+ \ No newline at end of file diff --git a/docs/source/matlab/tenmat.rst b/docs/source/matlab/tenmat.rst index 28c3cf3..10b84d5 100644 --- a/docs/source/matlab/tenmat.rst +++ b/docs/source/matlab/tenmat.rst @@ -3,18 +3,20 @@ Data members ^^^^^^^^^^^^ - +-----------------+----------------------+------------------------------------------------------------------------+ | MATLAB name | ``pyttb`` name | Calling convention | +=================+======================+========================================================================+ -| | ``tshape`` | ``X.tshape`` | +| ``tsize`` | ``tshape`` | ``X.tshape`` | +-----------------+----------------------+------------------------------------------------------------------------+ Methods ^^^^^^^ - +-----------------+----------------------+------------------------------------------------------------------------+ | MATLAB name | ``pyttb`` name | Calling convention | +=================+======================+========================================================================+ +| | ``from_data`` | ``B = ttb.tenmat.from_data(A.data, A.rindices, A.cindices, A.tshape)`` | +| ``tenmat`` +----------------------+------------------------------------------------------------------------+ +| | ``from_tensor_type`` | ``A = ttb.tenmat.from_tensor_type(X, np.array([1]))`` | ++-----------------+----------------------+------------------------------------------------------------------------+ | ``tensor`` | ``to_tensor`` | ``X.to_tensor()`` | +-----------------+----------------------+------------------------------------------------------------------------+ \ No newline at end of file From 75c0f1b6ffed2b7208cb7af8c1106d35468aa429 Mon Sep 17 00:00:00 2001 From: Jeremy Myers Date: Wed, 13 Dec 2023 10:32:00 -0800 Subject: [PATCH 08/17] Updates to ruff --- pyttb/cp_apr.py | 4 ++-- pyttb/gcp/fg_est.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pyttb/cp_apr.py b/pyttb/cp_apr.py index 083d3e7..33588df 100644 --- a/pyttb/cp_apr.py +++ b/pyttb/cp_apr.py @@ -1137,7 +1137,7 @@ def tt_cp_apr_pqnr( # noqa: PLR0912,PLR0913,PLR0915 @overload -def tt_calcpi_prowsubprob( # noqa: PLR0913 +def tt_calcpi_prowsubprob( Data: ttb.sptensor, Model: ttb.ktensor, rank: int, @@ -1150,7 +1150,7 @@ def tt_calcpi_prowsubprob( # noqa: PLR0913 @overload -def tt_calcpi_prowsubprob( # noqa: PLR0913 +def tt_calcpi_prowsubprob( Data: ttb.tensor, Model: ttb.ktensor, rank: int, diff --git a/pyttb/gcp/fg_est.py b/pyttb/gcp/fg_est.py index d0e3c29..a22caf9 100644 --- a/pyttb/gcp/fg_est.py +++ b/pyttb/gcp/fg_est.py @@ -13,7 +13,7 @@ @overload -def estimate( # noqa: PLR0913 +def estimate( model: ttb.ktensor, data_subs: np.ndarray, data_vals: np.ndarray, @@ -27,7 +27,7 @@ def estimate( # noqa: PLR0913 @overload -def estimate( # noqa: PLR0913 +def estimate( model: ttb.ktensor, data_subs: np.ndarray, data_vals: np.ndarray, @@ -41,7 +41,7 @@ def estimate( # noqa: PLR0913 @overload -def estimate( # noqa: PLR0913 +def estimate( model: ttb.ktensor, data_subs: np.ndarray, data_vals: np.ndarray, From ea3613ed55fae09858fcace5e84959465ae014a1 Mon Sep 17 00:00:00 2001 From: Jeremy Myers Date: Wed, 13 Dec 2023 10:32:00 -0800 Subject: [PATCH 09/17] Updates to ruff --- pyproject.toml | 2 +- pyttb/cp_apr.py | 4 ++-- pyttb/gcp/fg_est.py | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 25c9d46..d6a08ef 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,7 +36,7 @@ dev = [ "nbstripout", "pytest", "pytest-cov", - "ruff", + "ruff==0.0.284", "pre-commit", ] doc = [ diff --git a/pyttb/cp_apr.py b/pyttb/cp_apr.py index 083d3e7..33588df 100644 --- a/pyttb/cp_apr.py +++ b/pyttb/cp_apr.py @@ -1137,7 +1137,7 @@ def tt_cp_apr_pqnr( # noqa: PLR0912,PLR0913,PLR0915 @overload -def tt_calcpi_prowsubprob( # noqa: PLR0913 +def tt_calcpi_prowsubprob( Data: ttb.sptensor, Model: ttb.ktensor, rank: int, @@ -1150,7 +1150,7 @@ def tt_calcpi_prowsubprob( # noqa: PLR0913 @overload -def tt_calcpi_prowsubprob( # noqa: PLR0913 +def tt_calcpi_prowsubprob( Data: ttb.tensor, Model: ttb.ktensor, rank: int, diff --git a/pyttb/gcp/fg_est.py b/pyttb/gcp/fg_est.py index d0e3c29..a22caf9 100644 --- a/pyttb/gcp/fg_est.py +++ b/pyttb/gcp/fg_est.py @@ -13,7 +13,7 @@ @overload -def estimate( # noqa: PLR0913 +def estimate( model: ttb.ktensor, data_subs: np.ndarray, data_vals: np.ndarray, @@ -27,7 +27,7 @@ def estimate( # noqa: PLR0913 @overload -def estimate( # noqa: PLR0913 +def estimate( model: ttb.ktensor, data_subs: np.ndarray, data_vals: np.ndarray, @@ -41,7 +41,7 @@ def estimate( # noqa: PLR0913 @overload -def estimate( # noqa: PLR0913 +def estimate( model: ttb.ktensor, data_subs: np.ndarray, data_vals: np.ndarray, From 7b71366a0aa6410af876912acc408a5a3eb23dca Mon Sep 17 00:00:00 2001 From: Jeremy Myers Date: Mon, 5 Feb 2024 10:53:42 -0800 Subject: [PATCH 10/17] Ruff fixes. --- pyttb/cp_apr.py | 4 ++-- pyttb/gcp/fg_est.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pyttb/cp_apr.py b/pyttb/cp_apr.py index 33588df..45264d0 100644 --- a/pyttb/cp_apr.py +++ b/pyttb/cp_apr.py @@ -1137,7 +1137,7 @@ def tt_cp_apr_pqnr( # noqa: PLR0912,PLR0913,PLR0915 @overload -def tt_calcpi_prowsubprob( +def tt_calcpi_prowsubprob( # noqa: PLR0913 Data: ttb.sptensor, Model: ttb.ktensor, rank: int, @@ -1150,7 +1150,7 @@ def tt_calcpi_prowsubprob( @overload -def tt_calcpi_prowsubprob( +def tt_calcpi_prowsubprob( # noqa: PLR0913 Data: ttb.tensor, Model: ttb.ktensor, rank: int, diff --git a/pyttb/gcp/fg_est.py b/pyttb/gcp/fg_est.py index a22caf9..3a581e0 100644 --- a/pyttb/gcp/fg_est.py +++ b/pyttb/gcp/fg_est.py @@ -13,7 +13,7 @@ @overload -def estimate( +def estimate( # noqa: PLR0913 model: ttb.ktensor, data_subs: np.ndarray, data_vals: np.ndarray, @@ -27,7 +27,7 @@ def estimate( @overload -def estimate( +def estimate( # noqa: PLR0913 model: ttb.ktensor, data_subs: np.ndarray, data_vals: np.ndarray, @@ -41,7 +41,7 @@ def estimate( @overload -def estimate( +def estimate( # noqa: PLR0913 model: ttb.ktensor, data_subs: np.ndarray, data_vals: np.ndarray, From 960fac82364bb010c070375136283d76598389ea Mon Sep 17 00:00:00 2001 From: Jeremy Myers Date: Mon, 5 Feb 2024 10:54:23 -0800 Subject: [PATCH 11/17] Ruff fixes. --- pyttb/cp_apr.py | 4 ++-- pyttb/gcp/fg_est.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pyttb/cp_apr.py b/pyttb/cp_apr.py index 45264d0..083d3e7 100644 --- a/pyttb/cp_apr.py +++ b/pyttb/cp_apr.py @@ -1137,7 +1137,7 @@ def tt_cp_apr_pqnr( # noqa: PLR0912,PLR0913,PLR0915 @overload -def tt_calcpi_prowsubprob( # noqa: PLR0913 +def tt_calcpi_prowsubprob( # noqa: PLR0913 Data: ttb.sptensor, Model: ttb.ktensor, rank: int, @@ -1150,7 +1150,7 @@ def tt_calcpi_prowsubprob( # noqa: PLR0913 @overload -def tt_calcpi_prowsubprob( # noqa: PLR0913 +def tt_calcpi_prowsubprob( # noqa: PLR0913 Data: ttb.tensor, Model: ttb.ktensor, rank: int, diff --git a/pyttb/gcp/fg_est.py b/pyttb/gcp/fg_est.py index 3a581e0..d0e3c29 100644 --- a/pyttb/gcp/fg_est.py +++ b/pyttb/gcp/fg_est.py @@ -13,7 +13,7 @@ @overload -def estimate( # noqa: PLR0913 +def estimate( # noqa: PLR0913 model: ttb.ktensor, data_subs: np.ndarray, data_vals: np.ndarray, @@ -27,7 +27,7 @@ def estimate( # noqa: PLR0913 @overload -def estimate( # noqa: PLR0913 +def estimate( # noqa: PLR0913 model: ttb.ktensor, data_subs: np.ndarray, data_vals: np.ndarray, @@ -41,7 +41,7 @@ def estimate( # noqa: PLR0913 @overload -def estimate( # noqa: PLR0913 +def estimate( # noqa: PLR0913 model: ttb.ktensor, data_subs: np.ndarray, data_vals: np.ndarray, From 3d0e8144f37aa1326ef5039baa6c820f410ac802 Mon Sep 17 00:00:00 2001 From: Jeremy Myers Date: Mon, 5 Feb 2024 13:36:08 -0800 Subject: [PATCH 12/17] Attempting pre-commit fixes with autoupdate. --- .pre-commit-config.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e51986f..ecc51ba 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,16 +1,16 @@ repos: - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.0.284 + rev: v0.2.0 hooks: - id: ruff args: [ --fix, --exit-non-zero-on-fix ] - repo: https://github.com/psf/black - rev: 23.3.0 + rev: 24.1.1 hooks: - id: black language_version: python - repo: https://github.com/kynan/nbstripout - rev: 0.6.1 + rev: 0.7.1 hooks: - id: nbstripout args: [ From a65d7cd960f03770a7d09f4f42a1acde8e15b22e Mon Sep 17 00:00:00 2001 From: Jeremy Myers Date: Mon, 5 Feb 2024 13:39:45 -0800 Subject: [PATCH 13/17] Attempting pre-commit fixes with autoupdate: part two, running pre-commit hooks. --- docs/source/tutorial/class_sptenmat.ipynb | 84 +++++++++++----------- docs/source/tutorial/class_sumtensor.ipynb | 62 ++++++++-------- pyttb/__init__.py | 1 + pyttb/cp_als.py | 1 + pyttb/cp_apr.py | 11 ++- pyttb/export_data.py | 1 + pyttb/gcp/fg.py | 9 +-- pyttb/gcp/fg_est.py | 15 ++-- pyttb/gcp/optimizers.py | 1 + pyttb/gcp/samplers.py | 4 +- pyttb/gcp_opt.py | 1 + pyttb/hosvd.py | 1 + pyttb/import_data.py | 1 + pyttb/khatrirao.py | 1 + pyttb/ktensor.py | 13 ++-- pyttb/pyttb_utils.py | 7 +- pyttb/sptensor.py | 33 +++++---- pyttb/sptensor3.py | 1 + pyttb/sumtensor.py | 1 + pyttb/symktensor.py | 1 + pyttb/symtensor.py | 1 + pyttb/tenmat.py | 3 +- pyttb/tensor.py | 13 ++-- pyttb/ttensor.py | 3 +- pyttb/tucker_als.py | 1 + tests/test_package.py | 1 + 26 files changed, 144 insertions(+), 127 deletions(-) diff --git a/docs/source/tutorial/class_sptenmat.ipynb b/docs/source/tutorial/class_sptenmat.ipynb index 3e5b65d..7cc2756 100644 --- a/docs/source/tutorial/class_sptenmat.ipynb +++ b/docs/source/tutorial/class_sptenmat.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "bd6c352a", + "id": "0", "metadata": {}, "source": [ "# Converting Sparse Tensors to Matrices and vice versa\n", @@ -15,7 +15,7 @@ }, { "cell_type": "markdown", - "id": "c73f1a3d", + "id": "1", "metadata": {}, "source": [ "We show how to convert an `sptensor` to a matrix stored in _coordinate_ format with extra information so that is can be convertered back to an `sptensor`." @@ -24,7 +24,7 @@ { "cell_type": "code", "execution_count": null, - "id": "77bd3fcc", + "id": "2", "metadata": {}, "outputs": [], "source": [ @@ -34,7 +34,7 @@ }, { "cell_type": "markdown", - "id": "5bcdd7a1", + "id": "3", "metadata": {}, "source": [ "## Creating an `sptenmat` (sparse tensor as sparse matrix) object\n", @@ -46,7 +46,7 @@ { "cell_type": "code", "execution_count": null, - "id": "708ea1cf", + "id": "4", "metadata": {}, "outputs": [], "source": [ @@ -57,7 +57,7 @@ }, { "cell_type": "markdown", - "id": "980a4581", + "id": "5", "metadata": {}, "source": [ "Similar options as `tenmat` are available for `sptenmat`." @@ -66,7 +66,7 @@ { "cell_type": "code", "execution_count": null, - "id": "f7f260aa", + "id": "6", "metadata": {}, "outputs": [], "source": [ @@ -77,7 +77,7 @@ { "cell_type": "code", "execution_count": null, - "id": "55e67cf1", + "id": "7", "metadata": {}, "outputs": [], "source": [ @@ -88,7 +88,7 @@ { "cell_type": "code", "execution_count": null, - "id": "84a1ce5c", + "id": "8", "metadata": {}, "outputs": [], "source": [ @@ -101,7 +101,7 @@ { "cell_type": "code", "execution_count": null, - "id": "42a84f89", + "id": "9", "metadata": {}, "outputs": [], "source": [ @@ -114,7 +114,7 @@ { "cell_type": "code", "execution_count": null, - "id": "11b543d2", + "id": "10", "metadata": {}, "outputs": [], "source": [ @@ -127,7 +127,7 @@ { "cell_type": "code", "execution_count": null, - "id": "dd329fc3", + "id": "11", "metadata": {}, "outputs": [], "source": [ @@ -140,7 +140,7 @@ { "cell_type": "code", "execution_count": null, - "id": "006533b3", + "id": "12", "metadata": {}, "outputs": [], "source": [ @@ -153,7 +153,7 @@ { "cell_type": "code", "execution_count": null, - "id": "26a3eae4", + "id": "13", "metadata": {}, "outputs": [], "source": [ @@ -165,7 +165,7 @@ }, { "cell_type": "markdown", - "id": "9340605f", + "id": "14", "metadata": {}, "source": [ "## Constituent parts of an `sptenmat`" @@ -174,7 +174,7 @@ { "cell_type": "code", "execution_count": null, - "id": "3ba3ef1e", + "id": "15", "metadata": {}, "outputs": [], "source": [ @@ -184,7 +184,7 @@ { "cell_type": "code", "execution_count": null, - "id": "ff21b5dc", + "id": "16", "metadata": {}, "outputs": [], "source": [ @@ -194,7 +194,7 @@ { "cell_type": "code", "execution_count": null, - "id": "75193c11", + "id": "17", "metadata": {}, "outputs": [], "source": [ @@ -204,7 +204,7 @@ { "cell_type": "code", "execution_count": null, - "id": "3210d52c", + "id": "18", "metadata": {}, "outputs": [], "source": [ @@ -214,7 +214,7 @@ { "cell_type": "code", "execution_count": null, - "id": "848e916c", + "id": "19", "metadata": {}, "outputs": [], "source": [ @@ -223,7 +223,7 @@ }, { "cell_type": "markdown", - "id": "640de7d7", + "id": "20", "metadata": {}, "source": [ "## Creating an `sptenmat` from its constituent parts" @@ -232,7 +232,7 @@ { "cell_type": "code", "execution_count": null, - "id": "6c178790", + "id": "21", "metadata": {}, "outputs": [], "source": [ @@ -244,7 +244,7 @@ }, { "cell_type": "markdown", - "id": "655152ac", + "id": "22", "metadata": {}, "source": [ "## Creating an `sptenmat` with no nonzeros" @@ -253,7 +253,7 @@ { "cell_type": "code", "execution_count": null, - "id": "a137cad4", + "id": "23", "metadata": {}, "outputs": [], "source": [ @@ -265,7 +265,7 @@ }, { "cell_type": "markdown", - "id": "f0ff0cee", + "id": "24", "metadata": {}, "source": [ "# Creating an empty sptenmat" @@ -274,7 +274,7 @@ { "cell_type": "code", "execution_count": null, - "id": "b951ba95", + "id": "25", "metadata": {}, "outputs": [], "source": [ @@ -284,7 +284,7 @@ }, { "cell_type": "markdown", - "id": "e0eae957", + "id": "26", "metadata": {}, "source": [ "## Use `double` to convert an `sptenmat` to a SciPy COO Matrix" @@ -293,7 +293,7 @@ { "cell_type": "code", "execution_count": null, - "id": "18e3878d", + "id": "27", "metadata": {}, "outputs": [], "source": [ @@ -305,7 +305,7 @@ { "cell_type": "code", "execution_count": null, - "id": "b0659ae7", + "id": "28", "metadata": {}, "outputs": [], "source": [ @@ -315,7 +315,7 @@ }, { "cell_type": "markdown", - "id": "5ac68973", + "id": "29", "metadata": {}, "source": [ "## Use `full` to convert an `sptenmat` to a `tenmat`" @@ -324,7 +324,7 @@ { "cell_type": "code", "execution_count": null, - "id": "ba2bf34b", + "id": "30", "metadata": {}, "outputs": [], "source": [ @@ -335,7 +335,7 @@ { "cell_type": "code", "execution_count": null, - "id": "0fed6882", + "id": "31", "metadata": {}, "outputs": [], "source": [ @@ -345,7 +345,7 @@ }, { "cell_type": "markdown", - "id": "c4c3139d", + "id": "32", "metadata": {}, "source": [ "## Use `to_sptensor` to convert an `sptenmat` to an `sptensor`." @@ -354,7 +354,7 @@ { "cell_type": "code", "execution_count": null, - "id": "4253e876", + "id": "33", "metadata": {}, "outputs": [], "source": [ @@ -365,7 +365,7 @@ { "cell_type": "code", "execution_count": null, - "id": "86ae431e", + "id": "34", "metadata": {}, "outputs": [], "source": [ @@ -375,7 +375,7 @@ { "cell_type": "code", "execution_count": null, - "id": "5b71f868", + "id": "35", "metadata": {}, "outputs": [], "source": [ @@ -385,7 +385,7 @@ { "cell_type": "code", "execution_count": null, - "id": "37b27807", + "id": "36", "metadata": {}, "outputs": [], "source": [ @@ -395,7 +395,7 @@ { "cell_type": "code", "execution_count": null, - "id": "8a173aff", + "id": "37", "metadata": {}, "outputs": [], "source": [ @@ -406,7 +406,7 @@ { "cell_type": "code", "execution_count": null, - "id": "d020a1fa", + "id": "38", "metadata": {}, "outputs": [], "source": [ @@ -416,7 +416,7 @@ { "cell_type": "code", "execution_count": null, - "id": "26a17ebd", + "id": "39", "metadata": {}, "outputs": [], "source": [ @@ -426,7 +426,7 @@ { "cell_type": "code", "execution_count": null, - "id": "16306c5d", + "id": "40", "metadata": {}, "outputs": [], "source": [ @@ -436,7 +436,7 @@ { "cell_type": "code", "execution_count": null, - "id": "8182869d", + "id": "41", "metadata": {}, "outputs": [], "source": [ diff --git a/docs/source/tutorial/class_sumtensor.ipynb b/docs/source/tutorial/class_sumtensor.ipynb index a554999..8230044 100644 --- a/docs/source/tutorial/class_sumtensor.ipynb +++ b/docs/source/tutorial/class_sumtensor.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "a2683b4e", + "id": "0", "metadata": {}, "source": [ "# Sum of Structured Tensors\n", @@ -15,7 +15,7 @@ }, { "cell_type": "markdown", - "id": "6c96f0b4", + "id": "1", "metadata": {}, "source": [ "When certain operations are performed on a tensor which is formed as a sum of tensors, it can be beneficial to avoid explicitly forming the sum. For example, if a tensor is formed as a sum of a low rank tensor and a sparse tensor, the structure of the summands can make storage, decomposition and operations with other tensors significantly more efficient. A `sumtensor` exploits this structure. Here we explain the basics of defining and using sumtensors." @@ -24,7 +24,7 @@ { "cell_type": "code", "execution_count": null, - "id": "8c2ad211", + "id": "2", "metadata": {}, "outputs": [], "source": [ @@ -36,7 +36,7 @@ { "cell_type": "code", "execution_count": null, - "id": "a808d286", + "id": "3", "metadata": {}, "outputs": [], "source": [ @@ -50,7 +50,7 @@ }, { "cell_type": "markdown", - "id": "d169e48b", + "id": "4", "metadata": {}, "source": [ "## Creating sumtensors\n", @@ -60,7 +60,7 @@ { "cell_type": "code", "execution_count": null, - "id": "75797e16", + "id": "5", "metadata": {}, "outputs": [], "source": [ @@ -76,7 +76,7 @@ }, { "cell_type": "markdown", - "id": "0a17f6a8", + "id": "6", "metadata": {}, "source": [ "## A Magnitude Example\n", @@ -86,7 +86,7 @@ { "cell_type": "code", "execution_count": null, - "id": "1215e3d2", + "id": "7", "metadata": {}, "outputs": [], "source": [ @@ -107,7 +107,7 @@ }, { "cell_type": "markdown", - "id": "9650dccc", + "id": "8", "metadata": {}, "source": [ "## Further examples of the sumtensor constructor\n", @@ -117,7 +117,7 @@ { "cell_type": "code", "execution_count": null, - "id": "36ea6eb5", + "id": "9", "metadata": {}, "outputs": [], "source": [ @@ -127,7 +127,7 @@ }, { "cell_type": "markdown", - "id": "346374a7", + "id": "10", "metadata": {}, "source": [ "`sumtensor` also supports a copy constructor." @@ -136,7 +136,7 @@ { "cell_type": "code", "execution_count": null, - "id": "5f309064", + "id": "11", "metadata": {}, "outputs": [], "source": [ @@ -146,7 +146,7 @@ }, { "cell_type": "markdown", - "id": "c28b7cc6", + "id": "12", "metadata": {}, "source": [ "## Ndims and shape for dimensions of a sumtensor\n", @@ -156,7 +156,7 @@ { "cell_type": "code", "execution_count": null, - "id": "8e484181", + "id": "13", "metadata": {}, "outputs": [], "source": [ @@ -165,7 +165,7 @@ }, { "cell_type": "markdown", - "id": "ec82fc57", + "id": "14", "metadata": {}, "source": [ "## Use full to convert sumtensor to a dense tensor\n", @@ -175,7 +175,7 @@ { "cell_type": "code", "execution_count": null, - "id": "d247884d", + "id": "15", "metadata": {}, "outputs": [], "source": [ @@ -184,7 +184,7 @@ }, { "cell_type": "markdown", - "id": "b1cfbffa", + "id": "16", "metadata": {}, "source": [ "## Use double to convert to a numpy array\n", @@ -194,7 +194,7 @@ { "cell_type": "code", "execution_count": null, - "id": "3be2ba03", + "id": "17", "metadata": {}, "outputs": [], "source": [ @@ -203,7 +203,7 @@ }, { "cell_type": "markdown", - "id": "bd9f0550", + "id": "18", "metadata": {}, "source": [ "## Matricized Khatri-Rao product of a sumtensor\n", @@ -217,7 +217,7 @@ { "cell_type": "code", "execution_count": null, - "id": "3dd198fa", + "id": "19", "metadata": {}, "outputs": [], "source": [ @@ -228,7 +228,7 @@ }, { "cell_type": "markdown", - "id": "dd35fc03", + "id": "20", "metadata": {}, "source": [ "## Innerproducts of sumtensors\n", @@ -238,7 +238,7 @@ { "cell_type": "code", "execution_count": null, - "id": "dfa6d728", + "id": "21", "metadata": {}, "outputs": [], "source": [ @@ -252,7 +252,7 @@ }, { "cell_type": "markdown", - "id": "802902bb", + "id": "22", "metadata": {}, "source": [ "## Norm compatibility interface\n", @@ -262,7 +262,7 @@ { "cell_type": "code", "execution_count": null, - "id": "f945dea9", + "id": "23", "metadata": {}, "outputs": [], "source": [ @@ -271,7 +271,7 @@ }, { "cell_type": "markdown", - "id": "4647329a", + "id": "24", "metadata": {}, "source": [ "## Use CP-ALS with sumtensor\n", @@ -281,7 +281,7 @@ { "cell_type": "code", "execution_count": null, - "id": "8d980252", + "id": "25", "metadata": {}, "outputs": [], "source": [ @@ -291,7 +291,7 @@ }, { "cell_type": "markdown", - "id": "b93b33f9", + "id": "26", "metadata": {}, "source": [ "It follows that in cases where $\\mathcal{T}$ is too large for its full expansion to be stored in memory, we may still be able find a CP decomposition by exploiting the sumtensor structure.\n", @@ -301,7 +301,7 @@ }, { "cell_type": "markdown", - "id": "3b3793eb", + "id": "27", "metadata": {}, "source": [ "## Addition with sumtensors\n", @@ -311,7 +311,7 @@ { "cell_type": "code", "execution_count": null, - "id": "3514aa4b", + "id": "28", "metadata": {}, "outputs": [], "source": [ @@ -322,7 +322,7 @@ }, { "cell_type": "markdown", - "id": "bd401c2d", + "id": "29", "metadata": {}, "source": [ "## Accessing sumtensor parts\n", @@ -332,7 +332,7 @@ { "cell_type": "code", "execution_count": null, - "id": "47938e4f", + "id": "30", "metadata": {}, "outputs": [], "source": [ diff --git a/pyttb/__init__.py b/pyttb/__init__.py index 8d0f7e6..06c061c 100644 --- a/pyttb/__init__.py +++ b/pyttb/__init__.py @@ -1,4 +1,5 @@ """Entrypoint for Python Tensor Toolbox""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/cp_als.py b/pyttb/cp_als.py index c7ae2c6..28c9227 100644 --- a/pyttb/cp_als.py +++ b/pyttb/cp_als.py @@ -1,4 +1,5 @@ """CP Decomposition via Alternating Least Squares""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/cp_apr.py b/pyttb/cp_apr.py index 083d3e7..7f7b5ed 100644 --- a/pyttb/cp_apr.py +++ b/pyttb/cp_apr.py @@ -1,4 +1,5 @@ """Non-negative CP decomposition with alternating Poisson regression""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. @@ -1137,7 +1138,7 @@ def tt_cp_apr_pqnr( # noqa: PLR0912,PLR0913,PLR0915 @overload -def tt_calcpi_prowsubprob( # noqa: PLR0913 +def tt_calcpi_prowsubprob( Data: ttb.sptensor, Model: ttb.ktensor, rank: int, @@ -1145,20 +1146,18 @@ def tt_calcpi_prowsubprob( # noqa: PLR0913 ndims: int, isSparse: Literal[True], sparse_indices: np.ndarray, -) -> np.ndarray: - ... # pragma: no cover see coveragepy/issues/970 +) -> np.ndarray: ... # pragma: no cover see coveragepy/issues/970 @overload -def tt_calcpi_prowsubprob( # noqa: PLR0913 +def tt_calcpi_prowsubprob( Data: ttb.tensor, Model: ttb.ktensor, rank: int, factorIndex: int, ndims: int, isSparse: Literal[False], -) -> np.ndarray: - ... # pragma: no cover see coveragepy/issues/970 +) -> np.ndarray: ... # pragma: no cover see coveragepy/issues/970 def tt_calcpi_prowsubprob( # noqa: PLR0913 diff --git a/pyttb/export_data.py b/pyttb/export_data.py index a95a38f..44f2ffd 100644 --- a/pyttb/export_data.py +++ b/pyttb/export_data.py @@ -1,4 +1,5 @@ """Utilities for saving tensor data""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/gcp/fg.py b/pyttb/gcp/fg.py index 18beab8..0b061ef 100644 --- a/pyttb/gcp/fg.py +++ b/pyttb/gcp/fg.py @@ -17,8 +17,7 @@ def evaluate( weights: Optional[np.ndarray], function_handle: Literal[None], gradient_handle: function_type, -) -> List[np.ndarray]: - ... # pragma: no cover see coveragepy/issues/970 +) -> List[np.ndarray]: ... # pragma: no cover see coveragepy/issues/970 @overload @@ -28,8 +27,7 @@ def evaluate( weights: Optional[np.ndarray], function_handle: function_type, gradient_handle: Literal[None], -) -> float: - ... # pragma: no cover see coveragepy/issues/970 +) -> float: ... # pragma: no cover see coveragepy/issues/970 @overload @@ -39,8 +37,7 @@ def evaluate( weights: Optional[np.ndarray], function_handle: function_type, gradient_handle: function_type, -) -> Tuple[float, List[np.ndarray]]: - ... # pragma: no cover see coveragepy/issues/970 +) -> Tuple[float, List[np.ndarray]]: ... # pragma: no cover see coveragepy/issues/970 def evaluate( diff --git a/pyttb/gcp/fg_est.py b/pyttb/gcp/fg_est.py index d0e3c29..abeb5b5 100644 --- a/pyttb/gcp/fg_est.py +++ b/pyttb/gcp/fg_est.py @@ -13,7 +13,7 @@ @overload -def estimate( # noqa: PLR0913 +def estimate( model: ttb.ktensor, data_subs: np.ndarray, data_vals: np.ndarray, @@ -22,12 +22,11 @@ def estimate( # noqa: PLR0913 gradient_handle: function_type, lambda_check: bool = True, crng: Optional[np.ndarray] = None, -) -> List[np.ndarray]: - ... # pragma: no cover see coveragepy/issues/970 +) -> List[np.ndarray]: ... # pragma: no cover see coveragepy/issues/970 @overload -def estimate( # noqa: PLR0913 +def estimate( model: ttb.ktensor, data_subs: np.ndarray, data_vals: np.ndarray, @@ -36,12 +35,11 @@ def estimate( # noqa: PLR0913 gradient_handle: Literal[None] = None, lambda_check: bool = False, crng: Optional[np.ndarray] = None, -) -> float: - ... # pragma: no cover see coveragepy/issues/970 +) -> float: ... # pragma: no cover see coveragepy/issues/970 @overload -def estimate( # noqa: PLR0913 +def estimate( model: ttb.ktensor, data_subs: np.ndarray, data_vals: np.ndarray, @@ -50,8 +48,7 @@ def estimate( # noqa: PLR0913 gradient_handle: function_type, lambda_check: bool, crng: Optional[np.ndarray], -) -> Tuple[float, List[np.ndarray]]: - ... # pragma: no cover see coveragepy/issues/970 +) -> Tuple[float, List[np.ndarray]]: ... # pragma: no cover see coveragepy/issues/970 def estimate( # noqa: PLR0913 diff --git a/pyttb/gcp/optimizers.py b/pyttb/gcp/optimizers.py index 38345f9..b31b4c2 100644 --- a/pyttb/gcp/optimizers.py +++ b/pyttb/gcp/optimizers.py @@ -1,4 +1,5 @@ """Optimizer Implementations for GCP""" + from __future__ import annotations import logging diff --git a/pyttb/gcp/samplers.py b/pyttb/gcp/samplers.py index 62ac14a..161571a 100644 --- a/pyttb/gcp/samplers.py +++ b/pyttb/gcp/samplers.py @@ -144,9 +144,7 @@ def _prepare_function_sampler( # noqa: PLR0913 elif function_sampler == Samplers.UNIFORM: if function_samples is None: tensor_size = int(np.prod(data.shape)) - function_samples = min( - max(ceil(tensor_size / 10), 10**6), tensor_size - ) + function_samples = min(max(ceil(tensor_size / 10), 10**6), tensor_size) if not isinstance(function_samples, int): raise ValueError( "Uniform sampling only accepts integers for number of samples" diff --git a/pyttb/gcp_opt.py b/pyttb/gcp_opt.py index 5ee51cf..f03da5c 100644 --- a/pyttb/gcp_opt.py +++ b/pyttb/gcp_opt.py @@ -1,4 +1,5 @@ """Generalized CP Decomposition""" + from __future__ import annotations import logging diff --git a/pyttb/hosvd.py b/pyttb/hosvd.py index 6da6c1e..e3b0487 100644 --- a/pyttb/hosvd.py +++ b/pyttb/hosvd.py @@ -1,4 +1,5 @@ """Higher Order SVD Implementation""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/import_data.py b/pyttb/import_data.py index 3989928..d640f58 100644 --- a/pyttb/import_data.py +++ b/pyttb/import_data.py @@ -1,4 +1,5 @@ """Utilities for importing tensor data""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/khatrirao.py b/pyttb/khatrirao.py index 35fe507..bfe88fc 100644 --- a/pyttb/khatrirao.py +++ b/pyttb/khatrirao.py @@ -1,4 +1,5 @@ """Khatri-Rao Product Implementation""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/ktensor.py b/pyttb/ktensor.py index 886c876..475f892 100644 --- a/pyttb/ktensor.py +++ b/pyttb/ktensor.py @@ -1,4 +1,5 @@ """Classes and functions for working with Kruskal tensors.""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. @@ -969,12 +970,14 @@ def isequal(self, other: ttb.ktensor) -> bool: return True @overload - def issymmetric(self, return_diffs: Literal[False]) -> bool: - ... # pragma: no cover see coveragepy/issues/970 + def issymmetric( + self, return_diffs: Literal[False] + ) -> bool: ... # pragma: no cover see coveragepy/issues/970 @overload - def issymmetric(self, return_diffs: Literal[True]) -> Tuple[bool, np.ndarray]: - ... # pragma: no cover see coveragepy/issues/970 + def issymmetric( + self, return_diffs: Literal[True] + ) -> Tuple[bool, np.ndarray]: ... # pragma: no cover see coveragepy/issues/970 def issymmetric( self, return_diffs: bool = False @@ -1961,7 +1964,7 @@ def ttv( if ( len(vector) > 0 and isinstance(vector, np.ndarray) - and isinstance(vector.squeeze()[0], (int, float, np.int_, np.float_)) + and isinstance(vector.squeeze()[0], (int, float, np.int_, np.float64)) ): return self.ttv([vector], dims, exclude_dims) diff --git a/pyttb/pyttb_utils.py b/pyttb/pyttb_utils.py index 06ea875..396aeb2 100644 --- a/pyttb/pyttb_utils.py +++ b/pyttb/pyttb_utils.py @@ -1,4 +1,5 @@ """PYTTB shared utilities across tensor types""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. @@ -135,8 +136,7 @@ def tt_dimscheck( M: None = None, dims: Optional[np.ndarray] = None, exclude_dims: Optional[np.ndarray] = None, -) -> Tuple[np.ndarray, None]: - ... # pragma: no cover see coveragepy/issues/970 +) -> Tuple[np.ndarray, None]: ... # pragma: no cover see coveragepy/issues/970 @overload @@ -145,8 +145,7 @@ def tt_dimscheck( M: int, dims: Optional[np.ndarray] = None, exclude_dims: Optional[np.ndarray] = None, -) -> Tuple[np.ndarray, np.ndarray]: - ... # pragma: no cover see coveragepy/issues/970 +) -> Tuple[np.ndarray, np.ndarray]: ... # pragma: no cover see coveragepy/issues/970 def tt_dimscheck( diff --git a/pyttb/sptensor.py b/pyttb/sptensor.py index 07cf642..fd4fb1b 100644 --- a/pyttb/sptensor.py +++ b/pyttb/sptensor.py @@ -1,4 +1,5 @@ """Classes and functions for working with sparse tensors.""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. @@ -892,12 +893,14 @@ def logical_not(self) -> sptensor: return sptensor(subs, trueVector, self.shape) @overload - def logical_or(self, other: Union[float, ttb.tensor]) -> ttb.tensor: - ... # pragma: no cover see coveragepy/issues/970 + def logical_or( + self, other: Union[float, ttb.tensor] + ) -> ttb.tensor: ... # pragma: no cover see coveragepy/issues/970 @overload - def logical_or(self, other: sptensor) -> sptensor: - ... # pragma: no cover see coveragepy/issues/970 + def logical_or( + self, other: sptensor + ) -> sptensor: ... # pragma: no cover see coveragepy/issues/970 def logical_or( self, other: Union[float, ttb.tensor, sptensor] @@ -960,12 +963,14 @@ def logical_or( assert False, "Sptensor Logical Or argument must be scalar or sptensor" @overload - def logical_xor(self, other: Union[float, ttb.tensor]) -> ttb.tensor: - ... # pragma: no cover see coveragepy/issues/970 + def logical_xor( + self, other: Union[float, ttb.tensor] + ) -> ttb.tensor: ... # pragma: no cover see coveragepy/issues/970 @overload - def logical_xor(self, other: sptensor) -> sptensor: - ... # pragma: no cover see coveragepy/issues/970 + def logical_xor( + self, other: sptensor + ) -> sptensor: ... # pragma: no cover see coveragepy/issues/970 def logical_xor( self, other: Union[float, ttb.tensor, sptensor] @@ -1797,7 +1802,7 @@ def ttv( # noqa: PLR0912 # Check that vector is a list of vectors, # if not place single vector as element in list - if len(vector) > 0 and isinstance(vector[0], (int, float, np.int_, np.float_)): + if len(vector) > 0 and isinstance(vector[0], (int, float, np.int_, np.float64)): return self.ttv(np.array([vector]), dims, exclude_dims) # Get sorted dims and index for multiplicands @@ -3472,12 +3477,14 @@ def ttm( return Ynt.to_tensor() @overload - def squash(self, return_inverse: Literal[False]) -> sptensor: - ... # pragma: no cover see coveragepy/issues/970 + def squash( + self, return_inverse: Literal[False] + ) -> sptensor: ... # pragma: no cover see coveragepy/issues/970 @overload - def squash(self, return_inverse: Literal[True]) -> Tuple[sptensor, Dict]: - ... # pragma: no cover see coveragepy/issues/970 + def squash( + self, return_inverse: Literal[True] + ) -> Tuple[sptensor, Dict]: ... # pragma: no cover see coveragepy/issues/970 def squash( self, return_inverse: bool = False diff --git a/pyttb/sptensor3.py b/pyttb/sptensor3.py index 1c734a0..a82eee7 100644 --- a/pyttb/sptensor3.py +++ b/pyttb/sptensor3.py @@ -1,4 +1,5 @@ """Sparse Tensor 3 Class Placeholder""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/sumtensor.py b/pyttb/sumtensor.py index ef73b23..c0683d5 100644 --- a/pyttb/sumtensor.py +++ b/pyttb/sumtensor.py @@ -1,4 +1,5 @@ """Sum Tensor Class Placeholder""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/symktensor.py b/pyttb/symktensor.py index d6f6b61..76df7f1 100644 --- a/pyttb/symktensor.py +++ b/pyttb/symktensor.py @@ -1,4 +1,5 @@ """Symmetric Kruskal Tensor Class Placeholder""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/symtensor.py b/pyttb/symtensor.py index 5f4c5d9..e8eb328 100644 --- a/pyttb/symtensor.py +++ b/pyttb/symtensor.py @@ -1,4 +1,5 @@ """Symmetric Tensor Class Placeholder""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/tenmat.py b/pyttb/tenmat.py index 75c5fc3..4d6da1b 100644 --- a/pyttb/tenmat.py +++ b/pyttb/tenmat.py @@ -1,4 +1,5 @@ """Matricized Tensor Representation""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. @@ -253,7 +254,7 @@ def double(self) -> np.ndarray: ------- Copy of tenmat data. """ - return self.data.astype(np.float_).copy() + return self.data.astype(np.float64).copy() @property def ndims(self) -> int: diff --git a/pyttb/tensor.py b/pyttb/tensor.py index d0da481..6ecc1fc 100644 --- a/pyttb/tensor.py +++ b/pyttb/tensor.py @@ -1,4 +1,5 @@ """Classes and functions for working with dense tensors.""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. @@ -364,7 +365,7 @@ def double(self) -> np.ndarray: array([[1., 1.], [1., 1.]]) """ - return self.data.astype(np.float_).copy() + return self.data.astype(np.float64).copy() def exp(self) -> tensor: """ @@ -514,8 +515,7 @@ def issymmetric( grps: Optional[np.ndarray], version: Optional[Any], return_details: Literal[False], - ) -> bool: - ... # pragma: no cover see coveragepy/issues/970 + ) -> bool: ... # pragma: no cover see coveragepy/issues/970 @overload def issymmetric( @@ -523,8 +523,9 @@ def issymmetric( grps: Optional[np.ndarray], version: Optional[Any], return_details: Literal[True], - ) -> Tuple[bool, np.ndarray, np.ndarray]: - ... # pragma: no cover see coveragepy/issues/970 + ) -> Tuple[ + bool, np.ndarray, np.ndarray + ]: ... # pragma: no cover see coveragepy/issues/970 # TODO: We should probably always return details and let caller drop them def issymmetric( # noqa: PLR0912 @@ -1522,7 +1523,7 @@ def ttv( # Check that vector is a list of vectors, if not place single vector as element # in list - if len(vector) > 0 and isinstance(vector[0], (int, float, np.int_, np.float_)): + if len(vector) > 0 and isinstance(vector[0], (int, float, np.int_, np.float64)): return self.ttv(np.array([vector]), dims, exclude_dims) # Get sorted dims and index for multiplicands diff --git a/pyttb/ttensor.py b/pyttb/ttensor.py index 83feef9..d769627 100644 --- a/pyttb/ttensor.py +++ b/pyttb/ttensor.py @@ -1,4 +1,5 @@ """Tucker Tensor Implementation""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. @@ -368,7 +369,7 @@ def ttv( if ( len(vector) > 0 and isinstance(vector, np.ndarray) - and isinstance(vector[0], (int, float, np.int_, np.float_)) + and isinstance(vector[0], (int, float, np.int_, np.float64)) ): return self.ttv([vector], dims, exclude_dims) diff --git a/pyttb/tucker_als.py b/pyttb/tucker_als.py index fdeeb93..dfc7ab1 100644 --- a/pyttb/tucker_als.py +++ b/pyttb/tucker_als.py @@ -1,4 +1,5 @@ """Tucker decomposition via Alternating Least Squares""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/tests/test_package.py b/tests/test_package.py index 4a55235..83fe82f 100644 --- a/tests/test_package.py +++ b/tests/test_package.py @@ -1,4 +1,5 @@ """Testing of general package properties such as linting and formatting""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. From 6d300c18048dd7cbaa498f2d206be858b2634bb8 Mon Sep 17 00:00:00 2001 From: Jeremy Myers Date: Mon, 5 Feb 2024 13:58:57 -0800 Subject: [PATCH 14/17] Attempting pre-commit fixes with autoupdate: attempt two, autoupdate black only. --- .pre-commit-config.yaml | 2 +- pyttb/__init__.py | 1 + pyttb/cp_als.py | 1 + pyttb/cp_apr.py | 11 +++++------ pyttb/export_data.py | 1 + pyttb/gcp/fg.py | 9 +++------ pyttb/gcp/fg_est.py | 15 ++++++--------- pyttb/gcp/optimizers.py | 1 + pyttb/gcp/samplers.py | 4 +--- pyttb/gcp_opt.py | 1 + pyttb/hosvd.py | 1 + pyttb/import_data.py | 1 + pyttb/khatrirao.py | 1 + pyttb/ktensor.py | 11 +++++++---- pyttb/pyttb_utils.py | 7 +++---- pyttb/sptensor.py | 31 +++++++++++++++++++------------ pyttb/sptensor3.py | 1 + pyttb/sumtensor.py | 1 + pyttb/symktensor.py | 1 + pyttb/symtensor.py | 1 + pyttb/tenmat.py | 1 + pyttb/tensor.py | 9 +++++---- pyttb/ttensor.py | 1 + pyttb/tucker_als.py | 1 + tests/test_package.py | 1 + 25 files changed, 66 insertions(+), 49 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e51986f..35c904e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,7 +5,7 @@ repos: - id: ruff args: [ --fix, --exit-non-zero-on-fix ] - repo: https://github.com/psf/black - rev: 23.3.0 + rev: 24.1.1 hooks: - id: black language_version: python diff --git a/pyttb/__init__.py b/pyttb/__init__.py index 8d0f7e6..06c061c 100644 --- a/pyttb/__init__.py +++ b/pyttb/__init__.py @@ -1,4 +1,5 @@ """Entrypoint for Python Tensor Toolbox""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/cp_als.py b/pyttb/cp_als.py index c7ae2c6..28c9227 100644 --- a/pyttb/cp_als.py +++ b/pyttb/cp_als.py @@ -1,4 +1,5 @@ """CP Decomposition via Alternating Least Squares""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/cp_apr.py b/pyttb/cp_apr.py index 45264d0..e0671e3 100644 --- a/pyttb/cp_apr.py +++ b/pyttb/cp_apr.py @@ -1,4 +1,5 @@ """Non-negative CP decomposition with alternating Poisson regression""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. @@ -1137,7 +1138,7 @@ def tt_cp_apr_pqnr( # noqa: PLR0912,PLR0913,PLR0915 @overload -def tt_calcpi_prowsubprob( # noqa: PLR0913 +def tt_calcpi_prowsubprob( # noqa: PLR0913 Data: ttb.sptensor, Model: ttb.ktensor, rank: int, @@ -1145,20 +1146,18 @@ def tt_calcpi_prowsubprob( # noqa: PLR0913 ndims: int, isSparse: Literal[True], sparse_indices: np.ndarray, -) -> np.ndarray: - ... # pragma: no cover see coveragepy/issues/970 +) -> np.ndarray: ... # pragma: no cover see coveragepy/issues/970 @overload -def tt_calcpi_prowsubprob( # noqa: PLR0913 +def tt_calcpi_prowsubprob( # noqa: PLR0913 Data: ttb.tensor, Model: ttb.ktensor, rank: int, factorIndex: int, ndims: int, isSparse: Literal[False], -) -> np.ndarray: - ... # pragma: no cover see coveragepy/issues/970 +) -> np.ndarray: ... # pragma: no cover see coveragepy/issues/970 def tt_calcpi_prowsubprob( # noqa: PLR0913 diff --git a/pyttb/export_data.py b/pyttb/export_data.py index a95a38f..44f2ffd 100644 --- a/pyttb/export_data.py +++ b/pyttb/export_data.py @@ -1,4 +1,5 @@ """Utilities for saving tensor data""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/gcp/fg.py b/pyttb/gcp/fg.py index 18beab8..0b061ef 100644 --- a/pyttb/gcp/fg.py +++ b/pyttb/gcp/fg.py @@ -17,8 +17,7 @@ def evaluate( weights: Optional[np.ndarray], function_handle: Literal[None], gradient_handle: function_type, -) -> List[np.ndarray]: - ... # pragma: no cover see coveragepy/issues/970 +) -> List[np.ndarray]: ... # pragma: no cover see coveragepy/issues/970 @overload @@ -28,8 +27,7 @@ def evaluate( weights: Optional[np.ndarray], function_handle: function_type, gradient_handle: Literal[None], -) -> float: - ... # pragma: no cover see coveragepy/issues/970 +) -> float: ... # pragma: no cover see coveragepy/issues/970 @overload @@ -39,8 +37,7 @@ def evaluate( weights: Optional[np.ndarray], function_handle: function_type, gradient_handle: function_type, -) -> Tuple[float, List[np.ndarray]]: - ... # pragma: no cover see coveragepy/issues/970 +) -> Tuple[float, List[np.ndarray]]: ... # pragma: no cover see coveragepy/issues/970 def evaluate( diff --git a/pyttb/gcp/fg_est.py b/pyttb/gcp/fg_est.py index 3a581e0..494ecaf 100644 --- a/pyttb/gcp/fg_est.py +++ b/pyttb/gcp/fg_est.py @@ -13,7 +13,7 @@ @overload -def estimate( # noqa: PLR0913 +def estimate( # noqa: PLR0913 model: ttb.ktensor, data_subs: np.ndarray, data_vals: np.ndarray, @@ -22,12 +22,11 @@ def estimate( # noqa: PLR0913 gradient_handle: function_type, lambda_check: bool = True, crng: Optional[np.ndarray] = None, -) -> List[np.ndarray]: - ... # pragma: no cover see coveragepy/issues/970 +) -> List[np.ndarray]: ... # pragma: no cover see coveragepy/issues/970 @overload -def estimate( # noqa: PLR0913 +def estimate( # noqa: PLR0913 model: ttb.ktensor, data_subs: np.ndarray, data_vals: np.ndarray, @@ -36,12 +35,11 @@ def estimate( # noqa: PLR0913 gradient_handle: Literal[None] = None, lambda_check: bool = False, crng: Optional[np.ndarray] = None, -) -> float: - ... # pragma: no cover see coveragepy/issues/970 +) -> float: ... # pragma: no cover see coveragepy/issues/970 @overload -def estimate( # noqa: PLR0913 +def estimate( # noqa: PLR0913 model: ttb.ktensor, data_subs: np.ndarray, data_vals: np.ndarray, @@ -50,8 +48,7 @@ def estimate( # noqa: PLR0913 gradient_handle: function_type, lambda_check: bool, crng: Optional[np.ndarray], -) -> Tuple[float, List[np.ndarray]]: - ... # pragma: no cover see coveragepy/issues/970 +) -> Tuple[float, List[np.ndarray]]: ... # pragma: no cover see coveragepy/issues/970 def estimate( # noqa: PLR0913 diff --git a/pyttb/gcp/optimizers.py b/pyttb/gcp/optimizers.py index 38345f9..b31b4c2 100644 --- a/pyttb/gcp/optimizers.py +++ b/pyttb/gcp/optimizers.py @@ -1,4 +1,5 @@ """Optimizer Implementations for GCP""" + from __future__ import annotations import logging diff --git a/pyttb/gcp/samplers.py b/pyttb/gcp/samplers.py index 62ac14a..161571a 100644 --- a/pyttb/gcp/samplers.py +++ b/pyttb/gcp/samplers.py @@ -144,9 +144,7 @@ def _prepare_function_sampler( # noqa: PLR0913 elif function_sampler == Samplers.UNIFORM: if function_samples is None: tensor_size = int(np.prod(data.shape)) - function_samples = min( - max(ceil(tensor_size / 10), 10**6), tensor_size - ) + function_samples = min(max(ceil(tensor_size / 10), 10**6), tensor_size) if not isinstance(function_samples, int): raise ValueError( "Uniform sampling only accepts integers for number of samples" diff --git a/pyttb/gcp_opt.py b/pyttb/gcp_opt.py index 5ee51cf..f03da5c 100644 --- a/pyttb/gcp_opt.py +++ b/pyttb/gcp_opt.py @@ -1,4 +1,5 @@ """Generalized CP Decomposition""" + from __future__ import annotations import logging diff --git a/pyttb/hosvd.py b/pyttb/hosvd.py index 6da6c1e..e3b0487 100644 --- a/pyttb/hosvd.py +++ b/pyttb/hosvd.py @@ -1,4 +1,5 @@ """Higher Order SVD Implementation""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/import_data.py b/pyttb/import_data.py index 3989928..d640f58 100644 --- a/pyttb/import_data.py +++ b/pyttb/import_data.py @@ -1,4 +1,5 @@ """Utilities for importing tensor data""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/khatrirao.py b/pyttb/khatrirao.py index 35fe507..bfe88fc 100644 --- a/pyttb/khatrirao.py +++ b/pyttb/khatrirao.py @@ -1,4 +1,5 @@ """Khatri-Rao Product Implementation""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/ktensor.py b/pyttb/ktensor.py index 886c876..c12c4c9 100644 --- a/pyttb/ktensor.py +++ b/pyttb/ktensor.py @@ -1,4 +1,5 @@ """Classes and functions for working with Kruskal tensors.""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. @@ -969,12 +970,14 @@ def isequal(self, other: ttb.ktensor) -> bool: return True @overload - def issymmetric(self, return_diffs: Literal[False]) -> bool: - ... # pragma: no cover see coveragepy/issues/970 + def issymmetric( + self, return_diffs: Literal[False] + ) -> bool: ... # pragma: no cover see coveragepy/issues/970 @overload - def issymmetric(self, return_diffs: Literal[True]) -> Tuple[bool, np.ndarray]: - ... # pragma: no cover see coveragepy/issues/970 + def issymmetric( + self, return_diffs: Literal[True] + ) -> Tuple[bool, np.ndarray]: ... # pragma: no cover see coveragepy/issues/970 def issymmetric( self, return_diffs: bool = False diff --git a/pyttb/pyttb_utils.py b/pyttb/pyttb_utils.py index 06ea875..396aeb2 100644 --- a/pyttb/pyttb_utils.py +++ b/pyttb/pyttb_utils.py @@ -1,4 +1,5 @@ """PYTTB shared utilities across tensor types""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. @@ -135,8 +136,7 @@ def tt_dimscheck( M: None = None, dims: Optional[np.ndarray] = None, exclude_dims: Optional[np.ndarray] = None, -) -> Tuple[np.ndarray, None]: - ... # pragma: no cover see coveragepy/issues/970 +) -> Tuple[np.ndarray, None]: ... # pragma: no cover see coveragepy/issues/970 @overload @@ -145,8 +145,7 @@ def tt_dimscheck( M: int, dims: Optional[np.ndarray] = None, exclude_dims: Optional[np.ndarray] = None, -) -> Tuple[np.ndarray, np.ndarray]: - ... # pragma: no cover see coveragepy/issues/970 +) -> Tuple[np.ndarray, np.ndarray]: ... # pragma: no cover see coveragepy/issues/970 def tt_dimscheck( diff --git a/pyttb/sptensor.py b/pyttb/sptensor.py index 07cf642..3c3cdf3 100644 --- a/pyttb/sptensor.py +++ b/pyttb/sptensor.py @@ -1,4 +1,5 @@ """Classes and functions for working with sparse tensors.""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. @@ -892,12 +893,14 @@ def logical_not(self) -> sptensor: return sptensor(subs, trueVector, self.shape) @overload - def logical_or(self, other: Union[float, ttb.tensor]) -> ttb.tensor: - ... # pragma: no cover see coveragepy/issues/970 + def logical_or( + self, other: Union[float, ttb.tensor] + ) -> ttb.tensor: ... # pragma: no cover see coveragepy/issues/970 @overload - def logical_or(self, other: sptensor) -> sptensor: - ... # pragma: no cover see coveragepy/issues/970 + def logical_or( + self, other: sptensor + ) -> sptensor: ... # pragma: no cover see coveragepy/issues/970 def logical_or( self, other: Union[float, ttb.tensor, sptensor] @@ -960,12 +963,14 @@ def logical_or( assert False, "Sptensor Logical Or argument must be scalar or sptensor" @overload - def logical_xor(self, other: Union[float, ttb.tensor]) -> ttb.tensor: - ... # pragma: no cover see coveragepy/issues/970 + def logical_xor( + self, other: Union[float, ttb.tensor] + ) -> ttb.tensor: ... # pragma: no cover see coveragepy/issues/970 @overload - def logical_xor(self, other: sptensor) -> sptensor: - ... # pragma: no cover see coveragepy/issues/970 + def logical_xor( + self, other: sptensor + ) -> sptensor: ... # pragma: no cover see coveragepy/issues/970 def logical_xor( self, other: Union[float, ttb.tensor, sptensor] @@ -3472,12 +3477,14 @@ def ttm( return Ynt.to_tensor() @overload - def squash(self, return_inverse: Literal[False]) -> sptensor: - ... # pragma: no cover see coveragepy/issues/970 + def squash( + self, return_inverse: Literal[False] + ) -> sptensor: ... # pragma: no cover see coveragepy/issues/970 @overload - def squash(self, return_inverse: Literal[True]) -> Tuple[sptensor, Dict]: - ... # pragma: no cover see coveragepy/issues/970 + def squash( + self, return_inverse: Literal[True] + ) -> Tuple[sptensor, Dict]: ... # pragma: no cover see coveragepy/issues/970 def squash( self, return_inverse: bool = False diff --git a/pyttb/sptensor3.py b/pyttb/sptensor3.py index 1c734a0..a82eee7 100644 --- a/pyttb/sptensor3.py +++ b/pyttb/sptensor3.py @@ -1,4 +1,5 @@ """Sparse Tensor 3 Class Placeholder""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/sumtensor.py b/pyttb/sumtensor.py index ef73b23..c0683d5 100644 --- a/pyttb/sumtensor.py +++ b/pyttb/sumtensor.py @@ -1,4 +1,5 @@ """Sum Tensor Class Placeholder""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/symktensor.py b/pyttb/symktensor.py index d6f6b61..76df7f1 100644 --- a/pyttb/symktensor.py +++ b/pyttb/symktensor.py @@ -1,4 +1,5 @@ """Symmetric Kruskal Tensor Class Placeholder""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/symtensor.py b/pyttb/symtensor.py index 5f4c5d9..e8eb328 100644 --- a/pyttb/symtensor.py +++ b/pyttb/symtensor.py @@ -1,4 +1,5 @@ """Symmetric Tensor Class Placeholder""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/tenmat.py b/pyttb/tenmat.py index 75c5fc3..a1ca072 100644 --- a/pyttb/tenmat.py +++ b/pyttb/tenmat.py @@ -1,4 +1,5 @@ """Matricized Tensor Representation""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/tensor.py b/pyttb/tensor.py index d0da481..190ec93 100644 --- a/pyttb/tensor.py +++ b/pyttb/tensor.py @@ -1,4 +1,5 @@ """Classes and functions for working with dense tensors.""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. @@ -514,8 +515,7 @@ def issymmetric( grps: Optional[np.ndarray], version: Optional[Any], return_details: Literal[False], - ) -> bool: - ... # pragma: no cover see coveragepy/issues/970 + ) -> bool: ... # pragma: no cover see coveragepy/issues/970 @overload def issymmetric( @@ -523,8 +523,9 @@ def issymmetric( grps: Optional[np.ndarray], version: Optional[Any], return_details: Literal[True], - ) -> Tuple[bool, np.ndarray, np.ndarray]: - ... # pragma: no cover see coveragepy/issues/970 + ) -> Tuple[ + bool, np.ndarray, np.ndarray + ]: ... # pragma: no cover see coveragepy/issues/970 # TODO: We should probably always return details and let caller drop them def issymmetric( # noqa: PLR0912 diff --git a/pyttb/ttensor.py b/pyttb/ttensor.py index 83feef9..3829acf 100644 --- a/pyttb/ttensor.py +++ b/pyttb/ttensor.py @@ -1,4 +1,5 @@ """Tucker Tensor Implementation""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/pyttb/tucker_als.py b/pyttb/tucker_als.py index fdeeb93..dfc7ab1 100644 --- a/pyttb/tucker_als.py +++ b/pyttb/tucker_als.py @@ -1,4 +1,5 @@ """Tucker decomposition via Alternating Least Squares""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. diff --git a/tests/test_package.py b/tests/test_package.py index 4a55235..83fe82f 100644 --- a/tests/test_package.py +++ b/tests/test_package.py @@ -1,4 +1,5 @@ """Testing of general package properties such as linting and formatting""" + # Copyright 2022 National Technology & Engineering Solutions of Sandia, # LLC (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the # U.S. Government retains certain rights in this software. From c42b3ab128de4ea6e388cb4535888699d83251fc Mon Sep 17 00:00:00 2001 From: Jeremy Myers Date: Mon, 5 Feb 2024 14:20:00 -0800 Subject: [PATCH 15/17] Attempting to fix pre-commit issues and noqas --- pyttb/cp_apr.py | 10 ++++++---- pyttb/gcp/fg.py | 9 ++++++--- pyttb/gcp/fg_est.py | 15 +++++++++------ pyttb/gcp/samplers.py | 4 +++- pyttb/ktensor.py | 10 ++++------ pyttb/pyttb_utils.py | 6 ++++-- pyttb/sptensor.py | 30 ++++++++++++------------------ pyttb/tensor.py | 8 ++++---- 8 files changed, 48 insertions(+), 44 deletions(-) diff --git a/pyttb/cp_apr.py b/pyttb/cp_apr.py index 7f7b5ed..8616609 100644 --- a/pyttb/cp_apr.py +++ b/pyttb/cp_apr.py @@ -1138,7 +1138,7 @@ def tt_cp_apr_pqnr( # noqa: PLR0912,PLR0913,PLR0915 @overload -def tt_calcpi_prowsubprob( +def tt_calcpi_prowsubprob( # noqa: PLR0913 Data: ttb.sptensor, Model: ttb.ktensor, rank: int, @@ -1146,18 +1146,20 @@ def tt_calcpi_prowsubprob( ndims: int, isSparse: Literal[True], sparse_indices: np.ndarray, -) -> np.ndarray: ... # pragma: no cover see coveragepy/issues/970 +) -> np.ndarray: + ... # pragma: no cover see coveragepy/issues/970 @overload -def tt_calcpi_prowsubprob( +def tt_calcpi_prowsubprob( # noqa: PLR0913 Data: ttb.tensor, Model: ttb.ktensor, rank: int, factorIndex: int, ndims: int, isSparse: Literal[False], -) -> np.ndarray: ... # pragma: no cover see coveragepy/issues/970 +) -> np.ndarray: + ... # pragma: no cover see coveragepy/issues/970 def tt_calcpi_prowsubprob( # noqa: PLR0913 diff --git a/pyttb/gcp/fg.py b/pyttb/gcp/fg.py index 0b061ef..18beab8 100644 --- a/pyttb/gcp/fg.py +++ b/pyttb/gcp/fg.py @@ -17,7 +17,8 @@ def evaluate( weights: Optional[np.ndarray], function_handle: Literal[None], gradient_handle: function_type, -) -> List[np.ndarray]: ... # pragma: no cover see coveragepy/issues/970 +) -> List[np.ndarray]: + ... # pragma: no cover see coveragepy/issues/970 @overload @@ -27,7 +28,8 @@ def evaluate( weights: Optional[np.ndarray], function_handle: function_type, gradient_handle: Literal[None], -) -> float: ... # pragma: no cover see coveragepy/issues/970 +) -> float: + ... # pragma: no cover see coveragepy/issues/970 @overload @@ -37,7 +39,8 @@ def evaluate( weights: Optional[np.ndarray], function_handle: function_type, gradient_handle: function_type, -) -> Tuple[float, List[np.ndarray]]: ... # pragma: no cover see coveragepy/issues/970 +) -> Tuple[float, List[np.ndarray]]: + ... # pragma: no cover see coveragepy/issues/970 def evaluate( diff --git a/pyttb/gcp/fg_est.py b/pyttb/gcp/fg_est.py index abeb5b5..d0e3c29 100644 --- a/pyttb/gcp/fg_est.py +++ b/pyttb/gcp/fg_est.py @@ -13,7 +13,7 @@ @overload -def estimate( +def estimate( # noqa: PLR0913 model: ttb.ktensor, data_subs: np.ndarray, data_vals: np.ndarray, @@ -22,11 +22,12 @@ def estimate( gradient_handle: function_type, lambda_check: bool = True, crng: Optional[np.ndarray] = None, -) -> List[np.ndarray]: ... # pragma: no cover see coveragepy/issues/970 +) -> List[np.ndarray]: + ... # pragma: no cover see coveragepy/issues/970 @overload -def estimate( +def estimate( # noqa: PLR0913 model: ttb.ktensor, data_subs: np.ndarray, data_vals: np.ndarray, @@ -35,11 +36,12 @@ def estimate( gradient_handle: Literal[None] = None, lambda_check: bool = False, crng: Optional[np.ndarray] = None, -) -> float: ... # pragma: no cover see coveragepy/issues/970 +) -> float: + ... # pragma: no cover see coveragepy/issues/970 @overload -def estimate( +def estimate( # noqa: PLR0913 model: ttb.ktensor, data_subs: np.ndarray, data_vals: np.ndarray, @@ -48,7 +50,8 @@ def estimate( gradient_handle: function_type, lambda_check: bool, crng: Optional[np.ndarray], -) -> Tuple[float, List[np.ndarray]]: ... # pragma: no cover see coveragepy/issues/970 +) -> Tuple[float, List[np.ndarray]]: + ... # pragma: no cover see coveragepy/issues/970 def estimate( # noqa: PLR0913 diff --git a/pyttb/gcp/samplers.py b/pyttb/gcp/samplers.py index 161571a..62ac14a 100644 --- a/pyttb/gcp/samplers.py +++ b/pyttb/gcp/samplers.py @@ -144,7 +144,9 @@ def _prepare_function_sampler( # noqa: PLR0913 elif function_sampler == Samplers.UNIFORM: if function_samples is None: tensor_size = int(np.prod(data.shape)) - function_samples = min(max(ceil(tensor_size / 10), 10**6), tensor_size) + function_samples = min( + max(ceil(tensor_size / 10), 10**6), tensor_size + ) if not isinstance(function_samples, int): raise ValueError( "Uniform sampling only accepts integers for number of samples" diff --git a/pyttb/ktensor.py b/pyttb/ktensor.py index 475f892..8b35057 100644 --- a/pyttb/ktensor.py +++ b/pyttb/ktensor.py @@ -970,14 +970,12 @@ def isequal(self, other: ttb.ktensor) -> bool: return True @overload - def issymmetric( - self, return_diffs: Literal[False] - ) -> bool: ... # pragma: no cover see coveragepy/issues/970 + def issymmetric(self, return_diffs: Literal[False]) -> bool: + ... # pragma: no cover see coveragepy/issues/970 @overload - def issymmetric( - self, return_diffs: Literal[True] - ) -> Tuple[bool, np.ndarray]: ... # pragma: no cover see coveragepy/issues/970 + def issymmetric(self, return_diffs: Literal[True]) -> Tuple[bool, np.ndarray]: + ... # pragma: no cover see coveragepy/issues/970 def issymmetric( self, return_diffs: bool = False diff --git a/pyttb/pyttb_utils.py b/pyttb/pyttb_utils.py index 396aeb2..9c064a3 100644 --- a/pyttb/pyttb_utils.py +++ b/pyttb/pyttb_utils.py @@ -136,7 +136,8 @@ def tt_dimscheck( M: None = None, dims: Optional[np.ndarray] = None, exclude_dims: Optional[np.ndarray] = None, -) -> Tuple[np.ndarray, None]: ... # pragma: no cover see coveragepy/issues/970 +) -> Tuple[np.ndarray, None]: + ... # pragma: no cover see coveragepy/issues/970 @overload @@ -145,7 +146,8 @@ def tt_dimscheck( M: int, dims: Optional[np.ndarray] = None, exclude_dims: Optional[np.ndarray] = None, -) -> Tuple[np.ndarray, np.ndarray]: ... # pragma: no cover see coveragepy/issues/970 +) -> Tuple[np.ndarray, np.ndarray]: + ... # pragma: no cover see coveragepy/issues/970 def tt_dimscheck( diff --git a/pyttb/sptensor.py b/pyttb/sptensor.py index fd4fb1b..0b0d62a 100644 --- a/pyttb/sptensor.py +++ b/pyttb/sptensor.py @@ -893,14 +893,12 @@ def logical_not(self) -> sptensor: return sptensor(subs, trueVector, self.shape) @overload - def logical_or( - self, other: Union[float, ttb.tensor] - ) -> ttb.tensor: ... # pragma: no cover see coveragepy/issues/970 + def logical_or(self, other: Union[float, ttb.tensor]) -> ttb.tensor: + ... # pragma: no cover see coveragepy/issues/970 @overload - def logical_or( - self, other: sptensor - ) -> sptensor: ... # pragma: no cover see coveragepy/issues/970 + def logical_or(self, other: sptensor) -> sptensor: + ... # pragma: no cover see coveragepy/issues/970 def logical_or( self, other: Union[float, ttb.tensor, sptensor] @@ -963,14 +961,12 @@ def logical_or( assert False, "Sptensor Logical Or argument must be scalar or sptensor" @overload - def logical_xor( - self, other: Union[float, ttb.tensor] - ) -> ttb.tensor: ... # pragma: no cover see coveragepy/issues/970 + def logical_xor(self, other: Union[float, ttb.tensor]) -> ttb.tensor: + ... # pragma: no cover see coveragepy/issues/970 @overload - def logical_xor( - self, other: sptensor - ) -> sptensor: ... # pragma: no cover see coveragepy/issues/970 + def logical_xor(self, other: sptensor) -> sptensor: + ... # pragma: no cover see coveragepy/issues/970 def logical_xor( self, other: Union[float, ttb.tensor, sptensor] @@ -3477,14 +3473,12 @@ def ttm( return Ynt.to_tensor() @overload - def squash( - self, return_inverse: Literal[False] - ) -> sptensor: ... # pragma: no cover see coveragepy/issues/970 + def squash(self, return_inverse: Literal[False]) -> sptensor: + ... # pragma: no cover see coveragepy/issues/970 @overload - def squash( - self, return_inverse: Literal[True] - ) -> Tuple[sptensor, Dict]: ... # pragma: no cover see coveragepy/issues/970 + def squash(self, return_inverse: Literal[True]) -> Tuple[sptensor, Dict]: + ... # pragma: no cover see coveragepy/issues/970 def squash( self, return_inverse: bool = False diff --git a/pyttb/tensor.py b/pyttb/tensor.py index 6ecc1fc..1e20d58 100644 --- a/pyttb/tensor.py +++ b/pyttb/tensor.py @@ -515,7 +515,8 @@ def issymmetric( grps: Optional[np.ndarray], version: Optional[Any], return_details: Literal[False], - ) -> bool: ... # pragma: no cover see coveragepy/issues/970 + ) -> bool: + ... # pragma: no cover see coveragepy/issues/970 @overload def issymmetric( @@ -523,9 +524,8 @@ def issymmetric( grps: Optional[np.ndarray], version: Optional[Any], return_details: Literal[True], - ) -> Tuple[ - bool, np.ndarray, np.ndarray - ]: ... # pragma: no cover see coveragepy/issues/970 + ) -> Tuple[bool, np.ndarray, np.ndarray]: + ... # pragma: no cover see coveragepy/issues/970 # TODO: We should probably always return details and let caller drop them def issymmetric( # noqa: PLR0912 From 4c35c299cfcd459d939341ea5a782ab61e99c9fd Mon Sep 17 00:00:00 2001 From: Jeremy Myers Date: Mon, 5 Feb 2024 14:28:44 -0800 Subject: [PATCH 16/17] Pin ruff, fix precommit --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ecc51ba..35c904e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.2.0 + rev: v0.0.284 hooks: - id: ruff args: [ --fix, --exit-non-zero-on-fix ] @@ -10,7 +10,7 @@ repos: - id: black language_version: python - repo: https://github.com/kynan/nbstripout - rev: 0.7.1 + rev: 0.6.1 hooks: - id: nbstripout args: [ From a9fe513a2fe892c62f1fc52433de0a7339f712b0 Mon Sep 17 00:00:00 2001 From: Jeremy Myers Date: Mon, 5 Feb 2024 14:30:45 -0800 Subject: [PATCH 17/17] Run precommit --- pyttb/cp_apr.py | 6 ++---- pyttb/gcp/fg.py | 9 +++------ pyttb/gcp/fg_est.py | 9 +++------ pyttb/gcp/samplers.py | 4 +--- pyttb/ktensor.py | 10 ++++++---- pyttb/pyttb_utils.py | 6 ++---- pyttb/sptensor.py | 30 ++++++++++++++++++------------ pyttb/tensor.py | 8 ++++---- 8 files changed, 39 insertions(+), 43 deletions(-) diff --git a/pyttb/cp_apr.py b/pyttb/cp_apr.py index 8616609..e0671e3 100644 --- a/pyttb/cp_apr.py +++ b/pyttb/cp_apr.py @@ -1146,8 +1146,7 @@ def tt_calcpi_prowsubprob( # noqa: PLR0913 ndims: int, isSparse: Literal[True], sparse_indices: np.ndarray, -) -> np.ndarray: - ... # pragma: no cover see coveragepy/issues/970 +) -> np.ndarray: ... # pragma: no cover see coveragepy/issues/970 @overload @@ -1158,8 +1157,7 @@ def tt_calcpi_prowsubprob( # noqa: PLR0913 factorIndex: int, ndims: int, isSparse: Literal[False], -) -> np.ndarray: - ... # pragma: no cover see coveragepy/issues/970 +) -> np.ndarray: ... # pragma: no cover see coveragepy/issues/970 def tt_calcpi_prowsubprob( # noqa: PLR0913 diff --git a/pyttb/gcp/fg.py b/pyttb/gcp/fg.py index 18beab8..0b061ef 100644 --- a/pyttb/gcp/fg.py +++ b/pyttb/gcp/fg.py @@ -17,8 +17,7 @@ def evaluate( weights: Optional[np.ndarray], function_handle: Literal[None], gradient_handle: function_type, -) -> List[np.ndarray]: - ... # pragma: no cover see coveragepy/issues/970 +) -> List[np.ndarray]: ... # pragma: no cover see coveragepy/issues/970 @overload @@ -28,8 +27,7 @@ def evaluate( weights: Optional[np.ndarray], function_handle: function_type, gradient_handle: Literal[None], -) -> float: - ... # pragma: no cover see coveragepy/issues/970 +) -> float: ... # pragma: no cover see coveragepy/issues/970 @overload @@ -39,8 +37,7 @@ def evaluate( weights: Optional[np.ndarray], function_handle: function_type, gradient_handle: function_type, -) -> Tuple[float, List[np.ndarray]]: - ... # pragma: no cover see coveragepy/issues/970 +) -> Tuple[float, List[np.ndarray]]: ... # pragma: no cover see coveragepy/issues/970 def evaluate( diff --git a/pyttb/gcp/fg_est.py b/pyttb/gcp/fg_est.py index d0e3c29..494ecaf 100644 --- a/pyttb/gcp/fg_est.py +++ b/pyttb/gcp/fg_est.py @@ -22,8 +22,7 @@ def estimate( # noqa: PLR0913 gradient_handle: function_type, lambda_check: bool = True, crng: Optional[np.ndarray] = None, -) -> List[np.ndarray]: - ... # pragma: no cover see coveragepy/issues/970 +) -> List[np.ndarray]: ... # pragma: no cover see coveragepy/issues/970 @overload @@ -36,8 +35,7 @@ def estimate( # noqa: PLR0913 gradient_handle: Literal[None] = None, lambda_check: bool = False, crng: Optional[np.ndarray] = None, -) -> float: - ... # pragma: no cover see coveragepy/issues/970 +) -> float: ... # pragma: no cover see coveragepy/issues/970 @overload @@ -50,8 +48,7 @@ def estimate( # noqa: PLR0913 gradient_handle: function_type, lambda_check: bool, crng: Optional[np.ndarray], -) -> Tuple[float, List[np.ndarray]]: - ... # pragma: no cover see coveragepy/issues/970 +) -> Tuple[float, List[np.ndarray]]: ... # pragma: no cover see coveragepy/issues/970 def estimate( # noqa: PLR0913 diff --git a/pyttb/gcp/samplers.py b/pyttb/gcp/samplers.py index 62ac14a..161571a 100644 --- a/pyttb/gcp/samplers.py +++ b/pyttb/gcp/samplers.py @@ -144,9 +144,7 @@ def _prepare_function_sampler( # noqa: PLR0913 elif function_sampler == Samplers.UNIFORM: if function_samples is None: tensor_size = int(np.prod(data.shape)) - function_samples = min( - max(ceil(tensor_size / 10), 10**6), tensor_size - ) + function_samples = min(max(ceil(tensor_size / 10), 10**6), tensor_size) if not isinstance(function_samples, int): raise ValueError( "Uniform sampling only accepts integers for number of samples" diff --git a/pyttb/ktensor.py b/pyttb/ktensor.py index 8b35057..475f892 100644 --- a/pyttb/ktensor.py +++ b/pyttb/ktensor.py @@ -970,12 +970,14 @@ def isequal(self, other: ttb.ktensor) -> bool: return True @overload - def issymmetric(self, return_diffs: Literal[False]) -> bool: - ... # pragma: no cover see coveragepy/issues/970 + def issymmetric( + self, return_diffs: Literal[False] + ) -> bool: ... # pragma: no cover see coveragepy/issues/970 @overload - def issymmetric(self, return_diffs: Literal[True]) -> Tuple[bool, np.ndarray]: - ... # pragma: no cover see coveragepy/issues/970 + def issymmetric( + self, return_diffs: Literal[True] + ) -> Tuple[bool, np.ndarray]: ... # pragma: no cover see coveragepy/issues/970 def issymmetric( self, return_diffs: bool = False diff --git a/pyttb/pyttb_utils.py b/pyttb/pyttb_utils.py index 9c064a3..396aeb2 100644 --- a/pyttb/pyttb_utils.py +++ b/pyttb/pyttb_utils.py @@ -136,8 +136,7 @@ def tt_dimscheck( M: None = None, dims: Optional[np.ndarray] = None, exclude_dims: Optional[np.ndarray] = None, -) -> Tuple[np.ndarray, None]: - ... # pragma: no cover see coveragepy/issues/970 +) -> Tuple[np.ndarray, None]: ... # pragma: no cover see coveragepy/issues/970 @overload @@ -146,8 +145,7 @@ def tt_dimscheck( M: int, dims: Optional[np.ndarray] = None, exclude_dims: Optional[np.ndarray] = None, -) -> Tuple[np.ndarray, np.ndarray]: - ... # pragma: no cover see coveragepy/issues/970 +) -> Tuple[np.ndarray, np.ndarray]: ... # pragma: no cover see coveragepy/issues/970 def tt_dimscheck( diff --git a/pyttb/sptensor.py b/pyttb/sptensor.py index 0b0d62a..fd4fb1b 100644 --- a/pyttb/sptensor.py +++ b/pyttb/sptensor.py @@ -893,12 +893,14 @@ def logical_not(self) -> sptensor: return sptensor(subs, trueVector, self.shape) @overload - def logical_or(self, other: Union[float, ttb.tensor]) -> ttb.tensor: - ... # pragma: no cover see coveragepy/issues/970 + def logical_or( + self, other: Union[float, ttb.tensor] + ) -> ttb.tensor: ... # pragma: no cover see coveragepy/issues/970 @overload - def logical_or(self, other: sptensor) -> sptensor: - ... # pragma: no cover see coveragepy/issues/970 + def logical_or( + self, other: sptensor + ) -> sptensor: ... # pragma: no cover see coveragepy/issues/970 def logical_or( self, other: Union[float, ttb.tensor, sptensor] @@ -961,12 +963,14 @@ def logical_or( assert False, "Sptensor Logical Or argument must be scalar or sptensor" @overload - def logical_xor(self, other: Union[float, ttb.tensor]) -> ttb.tensor: - ... # pragma: no cover see coveragepy/issues/970 + def logical_xor( + self, other: Union[float, ttb.tensor] + ) -> ttb.tensor: ... # pragma: no cover see coveragepy/issues/970 @overload - def logical_xor(self, other: sptensor) -> sptensor: - ... # pragma: no cover see coveragepy/issues/970 + def logical_xor( + self, other: sptensor + ) -> sptensor: ... # pragma: no cover see coveragepy/issues/970 def logical_xor( self, other: Union[float, ttb.tensor, sptensor] @@ -3473,12 +3477,14 @@ def ttm( return Ynt.to_tensor() @overload - def squash(self, return_inverse: Literal[False]) -> sptensor: - ... # pragma: no cover see coveragepy/issues/970 + def squash( + self, return_inverse: Literal[False] + ) -> sptensor: ... # pragma: no cover see coveragepy/issues/970 @overload - def squash(self, return_inverse: Literal[True]) -> Tuple[sptensor, Dict]: - ... # pragma: no cover see coveragepy/issues/970 + def squash( + self, return_inverse: Literal[True] + ) -> Tuple[sptensor, Dict]: ... # pragma: no cover see coveragepy/issues/970 def squash( self, return_inverse: bool = False diff --git a/pyttb/tensor.py b/pyttb/tensor.py index 1e20d58..6ecc1fc 100644 --- a/pyttb/tensor.py +++ b/pyttb/tensor.py @@ -515,8 +515,7 @@ def issymmetric( grps: Optional[np.ndarray], version: Optional[Any], return_details: Literal[False], - ) -> bool: - ... # pragma: no cover see coveragepy/issues/970 + ) -> bool: ... # pragma: no cover see coveragepy/issues/970 @overload def issymmetric( @@ -524,8 +523,9 @@ def issymmetric( grps: Optional[np.ndarray], version: Optional[Any], return_details: Literal[True], - ) -> Tuple[bool, np.ndarray, np.ndarray]: - ... # pragma: no cover see coveragepy/issues/970 + ) -> Tuple[ + bool, np.ndarray, np.ndarray + ]: ... # pragma: no cover see coveragepy/issues/970 # TODO: We should probably always return details and let caller drop them def issymmetric( # noqa: PLR0912