Skip to content

Commit

Permalink
save features as csv
Browse files Browse the repository at this point in the history
  • Loading branch information
kapoorlab committed Sep 25, 2023
1 parent c74cb7a commit 3fa3980
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
41 changes: 41 additions & 0 deletions src/napatrackmater/Trackvector.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from scipy.cluster.hierarchy import linkage, fcluster
from sklearn.ensemble import RandomForestClassifier
from sklearn.cluster import KMeans
import csv


class TrackVector(TrackMate):
Expand Down Expand Up @@ -672,6 +673,15 @@ def perform_cosine_similarity(
)
sorted_feature_names, sorted_importances = zip(*sorted_features)
print("Sorted Feature Importances:")
_save_feature_importance(
sorted_feature_names,
normalized_importances,
csv_file_name_original,
track_arrays_array_names,
track_arrays_array,
track_arrays,
)

for feature, importance in zip(sorted_feature_names, normalized_importances):
print(f"{feature}: {importance}")

Expand All @@ -694,6 +704,28 @@ def perform_cosine_similarity(
result_dataframe.to_csv(csv_file_name, index=False)


def _save_feature_importance(
sorted_feature_names,
normalized_importances,
csv_file_name_original,
track_arrays_array_names,
track_arrays_array,
track_arrays,
):
data = list(zip(sorted_feature_names, normalized_importances))
csv_file_name = (
csv_file_name_original
+ track_arrays_array_names[track_arrays_array.index(track_arrays)]
+ "_feature_importance"
+ ".csv"
)
with open(csv_file_name, mode="w", newline="") as csv_file:
writer = csv.writer(csv_file)
writer.writerow(["Feature", "Importance"])
for feature, importance in data:
writer.writerow([feature, importance])


def _perform_pca_clustering(track_arrays, num_clusters, num_components=3):
pca = PCA(n_components=num_components)
reduced_data = pca.fit_transform(track_arrays)
Expand Down Expand Up @@ -794,8 +826,17 @@ def perform_pca(
)
sorted_feature_names, sorted_importances = zip(*sorted_features)
print("Sorted Feature Importances:")
_save_feature_importance(
sorted_feature_names,
normalized_importances,
csv_file_name_original,
track_arrays_array_names,
track_arrays_array,
track_arrays,
)
for feature, importance in zip(sorted_feature_names, normalized_importances):
print(f"{feature}: {importance}")

print(f"filtered tracks {len(filtered_track_ids), len(cluster_labels)}")
track_id_to_cluster = {
track_id: cluster_label
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.2.5"
__version_tuple__ = version_tuple = (4, 2, 5)
__version__ = version = "4.2.6"
__version_tuple__ = version_tuple = (4, 2, 6)

0 comments on commit 3fa3980

Please sign in to comment.