-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #234 from GhazalehManj/old_containers
Old containers
- Loading branch information
Showing
7 changed files
with
28 additions
and
292 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,33 +1,35 @@ | ||
import pandas as pd | ||
import json | ||
import csv | ||
import os | ||
import glob | ||
|
||
# Get the SCRATCH directory | ||
scratch_dir = os.getenv('SCRATCH') | ||
# Define the path to the JSON file and the output CSV file | ||
json_file_path = 'data/local/qsiprep/dwiqc.json' | ||
csv_file_path = 'data/local/qsiprep/qsiprep_metrics.csv' | ||
|
||
# Expand the path with the SCRATCH directory | ||
search_pattern = f"{scratch_dir}/SCanD_project/data/local/qsiprep/*/*/dwi/*desc-ImageQC_dwi.csv" | ||
# Create the directory for the CSV file if it doesn't exist | ||
os.makedirs(os.path.dirname(csv_file_path), exist_ok=True) | ||
|
||
# Get list of all relevant CSV files | ||
dwi_metrics_files = glob.glob(search_pattern, recursive=True) | ||
# Load JSON data from the file | ||
with open(json_file_path, 'r') as json_file: | ||
data = json.load(json_file) | ||
|
||
# Function to read each CSV and add the filename (without .csv extension) | ||
def read_and_add_filename(filepath): | ||
df = pd.read_csv(filepath) | ||
df['filename'] = os.path.basename(filepath).replace('.csv', '') | ||
return df | ||
# Extract the list of subjects from the JSON data | ||
subjects = data.get('subjects', []) | ||
|
||
# Read all CSV files into a single DataFrame | ||
dwi_metrics = pd.concat([read_and_add_filename(f) for f in dwi_metrics_files], ignore_index=True) | ||
# Check if there are subjects in the data | ||
if subjects: | ||
# Open a CSV file to write the data | ||
with open(csv_file_path, 'w', newline='') as csv_file: | ||
writer = csv.writer(csv_file) | ||
|
||
# Separate 'filename' column into 'subject' and 'session' | ||
# Extract subject and session from the filename | ||
dwi_metrics['subject'] = dwi_metrics['filename'].str.split('_').str[0].replace('sub-', '') | ||
dwi_metrics['session'] = dwi_metrics['filename'].str.split('_').str[1].replace('ses-', '') | ||
# Extract headers (keys) from the first subject dictionary | ||
headers = list(subjects[0].keys()) | ||
writer.writerow(headers) # Write header row | ||
|
||
# Iterate through each subject and write its values to the CSV | ||
for subject in subjects: | ||
writer.writerow([subject.get(header, '') for header in headers]) # Write row values for each subject | ||
|
||
output_dir = f"{scratch_dir}/SCanD_project/data/local/qsiprep" | ||
|
||
# Write the combined DataFrame to a new CSV file | ||
output_file = os.path.join(output_dir, "qsiprep_metrics.csv") | ||
dwi_metrics.to_csv(output_file, index=False) | ||
print(f"Data has been written to {csv_file_path}") | ||
else: | ||
print("No subjects found in the JSON data.") |
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