Skip to content

Commit

Permalink
analysis vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
kapoorlab committed Dec 16, 2023
1 parent 69044f7 commit 1dbda32
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
25 changes: 13 additions & 12 deletions src/napatrackmater/Trackvector.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from joblib import dump
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from collections import Counter
from sklearn.metrics import silhouette_score
import torch
import torch.nn as nn
Expand All @@ -27,7 +26,6 @@
from typing import List, Union
from torchsummary import summary
from torch.nn.utils import clip_grad_norm_
from sklearn.utils.class_weight import compute_class_weight


class TrackVector(TrackMate):
Expand Down Expand Up @@ -464,9 +462,9 @@ def _final_morphological_dynamic_vectors(self, track_id):

def plot_mitosis_times(self, full_dataframe, save_path=""):

dividing_events = full_dataframe[full_dataframe['Dividing'] == 1]
dividing_events = full_dataframe[full_dataframe["Dividing"] == 1]

dividing_counts = dividing_events['t'].value_counts().sort_index()
dividing_counts = dividing_events["t"].value_counts().sort_index()

times = dividing_counts.index
counts = dividing_counts.values
Expand Down Expand Up @@ -692,12 +690,14 @@ def create_analysis_vectors_dict(global_shape_dynamic_dataframe: pd.DataFrame):

return analysis_vectors


def z_score_normalize(data):
mean = np.mean(data, axis=0)
std = np.std(data, axis=0)
normalized_data = (data - mean) / std
return normalized_data, mean, std


def create_mitosis_training_data(
shape_dynamic_track_arrays,
shape_track_arrays,
Expand Down Expand Up @@ -1348,7 +1348,12 @@ def convert_tracks_to_arrays(analysis_vectors, full_dataframe):
)
shape_covariance_2d = shape_covariance_3d.reshape(len(analysis_track_ids), -1)
dynamic_covariance_2d = dynamic_covariance_3d.reshape(len(analysis_track_ids), -1)
return (shape_dynamic_covariance_2d, shape_covariance_2d, dynamic_covariance_2d)
return (
shape_dynamic_covariance_2d,
shape_covariance_2d,
dynamic_covariance_2d,
analysis_track_ids,
)


def compute_covariance_matrix(track_arrays, shape_array=5, feature_array=None):
Expand Down Expand Up @@ -1533,7 +1538,6 @@ def __init__(self, in_channels=1, num_classes_1=1, num_classes_2=1):
nn.GroupNorm(1, start_channel),
nn.ReLU(inplace=True),
nn.MaxPool1d(kernel_size=3, stride=2, padding=1),

)

self.classifier_1 = nn.Linear(0, num_classes_1)
Expand Down Expand Up @@ -1651,7 +1655,6 @@ def train_mitosis_neural_net(
model.to(device)
summary(model, (1, input_size))


criterion_class1 = nn.CrossEntropyLoss()
criterion_class2 = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate, weight_decay=1e-5)
Expand Down Expand Up @@ -1885,19 +1888,17 @@ def predict_with_model(saved_model_path, saved_model_json, features_array):
)
model.to(device)
model.eval()




if len(features_array.shape) == 2:
for i, batch in enumerate(features_array):
batch = z_score_normalize(batch)[0]
features_array[i] = batch
else:
features_array = z_score_normalize(features_array)[0]
features_array = features_array.astype(np.float32)
features_array = features_array.astype(np.float32)
features_tensor = torch.tensor(features_array, dtype=torch.float32).to(device)
if len(features_tensor.shape) == 1:

new_data_with_channel = features_tensor.unsqueeze(0).unsqueeze(0)
if len(features_tensor.shape) == 2:
new_data_with_channel = features_tensor.unsqueeze(1)
Expand Down
4 changes: 2 additions & 2 deletions src/napatrackmater/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = version = "4.5.9"
__version_tuple__ = version_tuple = (4, 5, 9)
__version__ = version = "4.6.0"
__version_tuple__ = version_tuple = (4, 6, 0)

0 comments on commit 1dbda32

Please sign in to comment.