-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
testing files and modified defaults to absolute paths
- Loading branch information
1 parent
bf33206
commit d3803cb
Showing
18 changed files
with
155 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import importlib | ||
from matplotlib.pyplot import close | ||
|
||
figureSuffixes = ["2", "4", "5", "6", "A1", "A2", "S1", "S2", "S3", | ||
"S4", "S5"] | ||
|
||
figModules = [] | ||
for _ in figureSuffixes: | ||
figModules.append(importlib.import_module("figure"+_)) | ||
# print(figModules) | ||
|
||
|
||
for figure in figModules[2:4]: | ||
if hasattr(figure, "makeFigure"): | ||
print(f"Calling: {figure.__name__}.makefigure()") | ||
print(figure) | ||
fig = figure.makeFigure() | ||
fig.savefig(f"./JamesIter{figure.__name__}.png") | ||
|
||
|
||
""" | ||
There's a bug with figure6.makeFigure(). It fails to make the figure if | ||
the loop had just previously generated figure5. Somehow, there's a leftover | ||
'<' somewhere in the data stream which causes predict.py to fail | ||
Figure S5 takes a long time to make with no print statements indicating | ||
progress. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import numpy as np | ||
|
||
def mode_n_product(tensor:np.ndarray, matrix:np.ndarray, mode:int): | ||
""" | ||
Perform mode-n product of a tensor with a matrix. | ||
Parameters: | ||
tensor (ndarray): Input tensor of shape (I1, I2, ..., In, ..., IN) | ||
matrix (ndarray): Matrix to multiply with, of shape (J, In) | ||
mode (int): Mode along which to multiply (1-based index) | ||
Returns: | ||
ndarray: Resulting tensor after mode-n multiplication | ||
""" | ||
# Convert to 0-based index for internal operations | ||
mode -= 1 | ||
|
||
# Check if dimensions are compatible for multiplication | ||
if tensor.shape[mode] != matrix.shape[1]: | ||
raise ValueError(f"Cannot multiply: tensor's mode-{mode+1} dimension ({tensor.shape[mode]}) " | ||
f"does not match matrix's second dimension ({matrix.shape[1]}).") | ||
|
||
# Move the mode-n dimension to the first dimension | ||
tensor = np.moveaxis(tensor, mode, 0) | ||
|
||
# Unfold the tensor into a matrix | ||
unfolded_tensor = tensor.reshape(tensor.shape[0], -1) | ||
|
||
# Perform matrix multiplication | ||
result:np.ndarray = np.dot(matrix, unfolded_tensor) | ||
|
||
# Fold the resulting matrix back into a tensor | ||
# new_shape = list(tensor.shape) | ||
# new_shape[0] = matrix.shape[0] | ||
# new_shape = tuple(new_shape) | ||
# result_tensor = result.reshape(new_shape) | ||
result_tensor = result.reshape((matrix.shape[0], tensor.shape[1], tensor.shape[2])) | ||
|
||
# Move the dimensions back to the original order | ||
result_tensor = np.moveaxis(result_tensor, 0, mode) | ||
return result_tensor | ||
|
||
# Example tensor (2 x 3 x 4) | ||
tensor = np.random.rand(2, 3, 4) | ||
print("Original Tensor:") | ||
print(tensor) | ||
|
||
# Example matrix (5 x 3) to multiply along mode-2 (1-based index) | ||
matrix = np.random.rand(5, 3) | ||
print("\nMatrix:") | ||
print(matrix) | ||
|
||
try: | ||
# Perform mode-2 (1-based index) multiplication | ||
result_tensor = mode_n_product(tensor, matrix, 2) | ||
print("\nResulting Tensor after mode-2 multiplication:") | ||
print(result_tensor) | ||
except ValueError as e: | ||
print(f"\nError: {e}") | ||
|
||
# Example of incompatible matrix (5 x 7) | ||
incompatible_matrix = np.random.rand(5, 7) | ||
print("\nIncompatible Matrix:") | ||
print(incompatible_matrix) | ||
|
||
try: | ||
# Attempt to perform mode-2 multiplication with an incompatible matrix | ||
result_tensor = mode_n_product(tensor, incompatible_matrix, 2) | ||
print("\nResulting Tensor after mode-2 multiplication with incompatible matrix:") | ||
print(result_tensor) | ||
except ValueError as e: | ||
print(f"\nError: {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters