-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aac9fd7
commit 547da24
Showing
12 changed files
with
339 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,4 +162,6 @@ cython_debug/ | |
docs | ||
|
||
examples/tmp | ||
examples/simulated_data | ||
examples/simulated_data | ||
|
||
poetry.lock |
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,49 @@ | ||
import os | ||
import pandas as pd | ||
import numpy as np | ||
|
||
# Define the folder containing the .txt files | ||
folder_path = ('/spm-data/vault-data3/mmll/data/HCP/100_nodes_times_series/3T_HCP1200_MSMAll_d100_ts2') | ||
|
||
# Initialize a list to store correlation matrices | ||
correlation_matrices = [] | ||
|
||
# Initialize a list to store subject IDs | ||
subject_ids = [] | ||
|
||
file_list = [f for f in os.listdir(folder_path) if f.endswith('.txt')] | ||
|
||
# Sort the filenames by subject ID (assuming subject IDs are in the filename before .txt) | ||
file_list.sort() # This will sort alphabetically, which works if subject IDs are formatted consistently | ||
|
||
# Loop through all files in the specified folder | ||
for filename in file_list: | ||
print(filename) | ||
# Construct the full file path | ||
file_path = os.path.join(folder_path, filename) | ||
|
||
# Read the text file into a DataFrame | ||
df = pd.read_csv(file_path, sep='\s+', header=None) | ||
|
||
# Calculate the correlation matrix | ||
correlation_matrix = df.corr() | ||
|
||
# Append the correlation matrix to the list | ||
correlation_matrices.append(correlation_matrix.values) | ||
|
||
# Extract subject ID from the filename (remove the .txt extension) | ||
subject_id = filename[:-4] # Exclude the last 4 characters ('.txt') | ||
subject_ids.append(subject_id) | ||
|
||
# Convert the list of correlation matrices into a 3D numpy array | ||
correlation_array = np.array(correlation_matrices) | ||
|
||
# Create a DataFrame for subject IDs | ||
subject_ids_df = pd.DataFrame(subject_ids, columns=['Subject_ID']) | ||
|
||
# Save the numpy array and DataFrame to disk | ||
np.save('/spm-data/vault-data3/mmll/data/HCP/100_nodes_times_series/functional_connectivity.npy', correlation_array) # Save as .npy file | ||
subject_ids_df.to_csv('/spm-data/vault-data3/mmll/data/HCP/100_nodes_times_series/subject_ids.csv', index=False) # Save as .csv file | ||
|
||
|
||
print() |
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,44 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
|
||
def simulate_scenario(scenario, n=1000, noise_level=0.1): | ||
np.random.seed(42) # For reproducibility | ||
if scenario == 1: | ||
# Scenario 1: x and y associated, z independent | ||
x = np.random.normal(0, 1, n) | ||
y = 2 * x + np.random.normal(0, noise_level, n) | ||
z = np.random.normal(0, 1, n) # Independent of x and y | ||
elif scenario == 2: | ||
# Scenario 2: x and y spurious due to z | ||
z = np.random.normal(0, 1, n) | ||
x = 2 * z + np.random.normal(0, noise_level, n) | ||
y = -3 * z + np.random.normal(0, noise_level, n) | ||
elif scenario == 3: | ||
# Scenario 3: x and y associated; z partially explains it | ||
z = np.random.normal(0, 1, n) | ||
x = 2 * z + np.random.normal(0, noise_level, n) | ||
y = 3 * x - 1.5 * z + np.random.normal(0, noise_level, n) | ||
else: | ||
raise ValueError("Invalid scenario. Choose 1, 2, or 3.") | ||
return x, y, z | ||
|
||
|
||
# Simulate and plot results for each scenario | ||
for scenario in range(1, 4): | ||
x, y, z = simulate_scenario(scenario, noise_level=2) | ||
|
||
print(f"Scenario {scenario} correlations:") | ||
print(f" Corr(x, y): {np.corrcoef(x, y)[0, 1]:.2f}") | ||
print(f" Corr(x, z): {np.corrcoef(x, z)[0, 1]:.2f}") | ||
print(f" Corr(y, z): {np.corrcoef(y, z)[0, 1]:.2f}") | ||
|
||
plt.figure() | ||
plt.scatter(x, y, alpha=0.5, label="x vs y") | ||
plt.scatter(x, z, alpha=0.5, label="x vs z") | ||
plt.scatter(y, z, alpha=0.5, label="y vs z") | ||
plt.legend() | ||
plt.title(f"Scenario {scenario}") | ||
plt.xlabel("Variable") | ||
plt.ylabel("Variable") | ||
plt.show() |
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,15 @@ | ||
import numpy as np | ||
|
||
|
||
assoc_strength = 0.5 | ||
|
||
x = np.random.normal(0, 1, 1000) | ||
e = np.random.normal(0, 1, 1000) | ||
y = assoc_strength * x + e | ||
|
||
print(f"var({assoc_strength}*x)={np.var(assoc_strength * x)}") | ||
print(f"var(noise)={np.var(e)}") | ||
print(f"var(y)={np.var(y)}") | ||
|
||
print(f"r={np.corrcoef(x, y)[0, 1]:.2f}") | ||
print(f"Explained variance: {np.square(np.corrcoef(x, y)[0, 1]):.2f}") |
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
Oops, something went wrong.