Skip to content

Commit

Permalink
testing files and modified defaults to absolute paths
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBones1102 committed Jun 11, 2024
1 parent bf33206 commit d3803cb
Show file tree
Hide file tree
Showing 18 changed files with 155 additions and 35 deletions.
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,21 @@ output/*
cache

.idea/
JamesFig2.png
JamesFig4.png
JamesFig5.png
JamesFig6.png
JamesFigA1.png
JamesFigA2.png
JamesFigS1.png
JamesIterfigure2.png
JamesIterfigure4.png
JamesIterfigure5.png
JamesIterfigure6.png
JamesIterfigureA1.png
JamesIterfigureA2.png
JamesIterfigureS1.png
JamesIterfigureS2.png
JamesIterfigureS3.png
JamesIterfigureS4.png
JamesIterfigureS5.png
2 changes: 1 addition & 1 deletion tfac/dataImport.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import scipy.cluster.hierarchy as sch
from sklearn.preprocessing import scale

from .cmtf import perform_CMTF
from tfac.cmtf import perform_CMTF

PATH_HERE = dirname(dirname(abspath(__file__)))
OPTIMAL_SCALING = 2 ** 7.0
Expand Down
28 changes: 28 additions & 0 deletions tfac/figures/JamesGenerator.py
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.
"""
9 changes: 5 additions & 4 deletions tfac/figures/figure2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import numpy as np
import pandas as pd

from .common import getSetup
from ..dataImport import form_tensor, get_factors
from ..predict import run_model
from ..cmtf import calcR2X, PCArand

from tfac.figures.common import getSetup
from tfac.dataImport import form_tensor, get_factors
from tfac.predict import run_model
from tfac.cmtf import calcR2X, PCArand


def get_r2x_results():
Expand Down
6 changes: 3 additions & 3 deletions tfac/figures/figure4.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
from sklearn.preprocessing import scale
from sklearn.utils import resample

from .common import getSetup
from ..dataImport import form_tensor, import_cytokines, get_factors, \
from tfac.figures.common import getSetup
from tfac.dataImport import form_tensor, import_cytokines, get_factors, \
reorder_table
from ..predict import run_model, predict_regression
from tfac.predict import run_model, predict_regression

N_BOOTSTRAP = 30
PATH_HERE = dirname(dirname(abspath(__file__)))
Expand Down
6 changes: 3 additions & 3 deletions tfac/figures/figure5.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import pandas as pd
from sklearn.metrics import roc_curve

from .common import getSetup
from ..dataImport import import_validation_patient_metadata, get_factors, \
from tfac.figures.common import getSetup
from tfac.dataImport import import_validation_patient_metadata, get_factors, \
import_cytokines, import_rna
from ..predict import get_accuracy, predict_known
from tfac.predict import get_accuracy, predict_known

COLOR_CYCLE = matplotlib.rcParams['axes.prop_cycle'].by_key()['color']
PATH_HERE = dirname(dirname(abspath(__file__)))
Expand Down
6 changes: 3 additions & 3 deletions tfac/figures/figure6.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import numpy as np
import pandas as pd

from .common import getSetup
from ..dataImport import get_factors
from ..predict import predict_known
from tfac.figures.common import getSetup
from tfac.dataImport import get_factors
from tfac.predict import predict_known


def makeFigure():
Expand Down
4 changes: 2 additions & 2 deletions tfac/figures/figureA1.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import pandas as pd
import numpy as np

from .common import subplotLabel, getSetup
from ..impute import evaluate_missing
from tfac.figures.common import subplotLabel, getSetup
from tfac.impute import evaluate_missing


def makeFigure():
Expand Down
4 changes: 2 additions & 2 deletions tfac/figures/figureA2.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import pandas as pd
import seaborn as sns

from .common import getSetup
from ..dataImport import get_factors, import_rna, reorder_table
from tfac.figures.common import getSetup
from tfac.dataImport import get_factors, import_rna, reorder_table

plt.rcParams["svg.fonttype"] = "none"

Expand Down
4 changes: 2 additions & 2 deletions tfac/figures/figureS1.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from scipy.stats import pearsonr
import seaborn as sns

from .common import getSetup
from ..dataImport import form_tensor, import_cytokines
from tfac.figures.common import getSetup
from tfac.dataImport import form_tensor, import_cytokines


def adjust_box_widths(g, fac):
Expand Down
4 changes: 2 additions & 2 deletions tfac/figures/figureS2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from matplotlib.patches import Patch
import numpy as np

from .common import getSetup
from ..dataImport import import_cytokines
from tfac.figures.common import getSetup
from tfac.dataImport import import_cytokines

COLOR_CYCLE = matplotlib.rcParams['axes.prop_cycle'].by_key()['color']

Expand Down
8 changes: 4 additions & 4 deletions tfac/figures/figureS3.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import pandas as pd
from sklearn.metrics import r2_score, roc_curve

from .common import getSetup
from ..cmtf import OPTIMAL_RANK
from ..dataImport import import_validation_patient_metadata, get_factors
from ..predict import get_accuracy, predict_known, predict_validation, \
from tfac.figures.common import getSetup
from tfac.cmtf import OPTIMAL_RANK
from tfac.dataImport import import_validation_patient_metadata, get_factors
from tfac.predict import get_accuracy, predict_known, predict_validation, \
predict_regression

COLOR_CYCLE = matplotlib.rcParams['axes.prop_cycle'].by_key()['color'][2:]
Expand Down
4 changes: 2 additions & 2 deletions tfac/figures/figureS4.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from scipy.stats import pearsonr
import seaborn as sns

from .common import getSetup
from ..dataImport import import_cytokines
from tfac.figures.common import getSetup
from tfac.dataImport import import_cytokines


def correlate_cytokines():
Expand Down
6 changes: 3 additions & 3 deletions tfac/figures/figureS5.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from os.path import abspath, dirname
import pandas as pd

from .common import getSetup
from ..dataImport import import_validation_patient_metadata, get_factors, import_cytokines, import_rna
from ..predict import get_accuracy, predict_known, predict_validation, predict_regression
from tfac.figures.common import getSetup
from tfac.dataImport import import_validation_patient_metadata, get_factors, import_cytokines, import_rna
from tfac.predict import get_accuracy, predict_known, predict_validation, predict_regression

COLOR_CYCLE = matplotlib.rcParams['axes.prop_cycle'].by_key()['color'][2:]
PATH_HERE = dirname(dirname(abspath(__file__)))
Expand Down
4 changes: 2 additions & 2 deletions tfac/impute.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import numpy as np
from statsmodels.multivariate.pca import PCA
from .dataImport import form_tensor
from .cmtf import perform_CMTF, calcR2X
from tfac.dataImport import form_tensor
from tfac.cmtf import perform_CMTF, calcR2X


def flatten_to_mat(tensor, matrix=None):
Expand Down
3 changes: 2 additions & 1 deletion tfac/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
RepeatedStratifiedKFold, StratifiedKFold
from sklearn.svm import SVC

from .dataImport import import_validation_patient_metadata
from tfac.dataImport import import_validation_patient_metadata

warnings.filterwarnings('ignore', category=UserWarning)

Expand Down Expand Up @@ -95,6 +95,7 @@ def predict_known(data, labels, method='predict', svc=False):
if svc:
_, model = run_svc(data, labels)
else:

_, model = run_model(data, labels)

if isinstance(data, pd.Series):
Expand Down
72 changes: 72 additions & 0 deletions tfac/tests/james_test.py
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}")
2 changes: 1 addition & 1 deletion tfac/tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
import numpy as np
import pandas as pd
from ..dataImport import import_patient_metadata, form_tensor, import_rna
from tfac.dataImport import import_patient_metadata, form_tensor, import_rna


@pytest.mark.parametrize("call", [import_patient_metadata, import_rna])
Expand Down

0 comments on commit d3803cb

Please sign in to comment.