Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 58 additions & 7 deletions freesurfer_post/interfaces/tabular.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
with open(stats_fname) as fo:
data = fo.readlines()

idx = [i for i, l in enumerate(data) if l.startswith('# ColHeaders ')]

Check failure on line 21 in freesurfer_post/interfaces/tabular.py

View workflow job for this annotation

GitHub Actions / lint (3.12)

Ruff (E741)

freesurfer_post/interfaces/tabular.py:21:21: E741 Ambiguous variable name: `l`
assert len(idx) == 1
idx = idx[0]

Expand Down Expand Up @@ -137,11 +137,42 @@
output_dir = Path(self.inputs.output_dir) / subject_id
output_dir.mkdir(parents=True, exist_ok=True)
output_prefix = f'{subject_id}_{session_id}' if session_id else subject_id
cleaned_atlas_name = (
atlas.replace('.', '').replace('_order', '').replace('_', '')
)

cleaned_atlas_name = atlas.replace('.', '').replace('_order', '').replace('_', '')

# Convert column names to snake case
out_df.columns = [col.lower().replace('-', '_').replace('.', '_') for col in out_df.columns]
# Rename subject_id to participant_id
out_df = out_df.rename(columns={'subject_id': 'participant_id'})
# Reorder columns to have participant_id first
cols = out_df.columns.tolist()
cols.remove('participant_id')
out_df = out_df[['participant_id'] + cols]
# Replace missing values with "n/a"
out_df = out_df.fillna('n/a')
Comment on lines +143 to +152
Copy link

Copilot AI Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The transformation steps (converting column names, renaming, reordering, and filling missing values) are repeated for both out_df and data_df. Consider refactoring these into a common helper function to improve maintainability.

Suggested change
# Convert column names to snake case
out_df.columns = [col.lower().replace('-', '_').replace('.', '_') for col in out_df.columns]
# Rename subject_id to participant_id
out_df = out_df.rename(columns={'subject_id': 'participant_id'})
# Reorder columns to have participant_id first
cols = out_df.columns.tolist()
cols.remove('participant_id')
out_df = out_df[['participant_id'] + cols]
# Replace missing values with "n/a"
out_df = out_df.fillna('n/a')
# Apply transformations to the DataFrame
out_df = transform_dataframe(out_df)

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tientong98 is this a good idea?


# Create metadata with the same column names
metadata = {}
for col in out_df.columns:
if col == 'participant_id':
metadata[col] = {'Description': 'BIDS participant ID'}
elif col == 'session_id':
metadata[col] = {'Description': 'BIDS session ID'}
elif col == 'hemisphere':
metadata[col] = {'Description': 'Brain hemisphere (lh or rh)'}
elif col == 'atlas':
metadata[col] = {'Description': 'Atlas used for parcellation'}
else:
metadata[col] = {'Description': f'Surface statistic: {col}'}

# Save metadata
out_json = output_dir / f'{output_prefix}_seg-{cleaned_atlas_name}_surfacestats.json'
with out_json.open('w') as jsonf:
json.dump(metadata, jsonf, indent=2)

# Save data
out_df.to_csv(
output_dir / f'{output_prefix}_atlas-{cleaned_atlas_name}_surfacestats.tsv',
output_dir / f'{output_prefix}_seg-{cleaned_atlas_name}_surfacestats.tsv',
sep='\t',
index=False,
)
Expand Down Expand Up @@ -361,11 +392,31 @@
out_tsv = output_dir / f'{output_prefix}_brainmeasures.tsv'
out_json = output_dir / f'{output_prefix}_brainmeasures.json'

metadata = {key: value['meta'] for key, value in fs_audit.items()}
# Extract just the values from the audit data
data_value = {key: value['value'] for key, value in fs_audit.items()}
data_df = pd.DataFrame([data_value])
# Convert column names to snake case
data_df.columns = [col.lower().replace('-', '_').replace('.', '_') for col in data_df.columns]
# Rename subject_id to participant_id
data_df = data_df.rename(columns={'subject_id': 'participant_id'})
# Reorder columns to have participant_id first
cols = data_df.columns.tolist()
cols.remove('participant_id')
data_df = data_df[['participant_id'] + cols]
# Replace missing values with "n/a"
data_df = data_df.fillna('n/a')

# Create metadata with the same column names as the TSV
metadata = {}
for key, value in fs_audit.items():
# Convert the key to match TSV column name
new_key = key.lower().replace('-', '_').replace('.', '_')
if key == 'subject_id':
new_key = 'participant_id'
metadata[new_key] = {'Description': value['meta']}

with out_json.open('w') as jsonf:
json.dump(metadata, jsonf, indent=2)

real_data = {key: value['value'] for key, value in fs_audit.items()}
data_df = pd.DataFrame([real_data])
data_df.to_csv(out_tsv, sep='\t', index=False)
return runtime
2 changes: 1 addition & 1 deletion freesurfer_post/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def find_freesurfer_dir(
if warn_no_session:
warnings.warn(
f'{subjects_dir}/{subject_id}_{session_id} not found in {subjects_dir}'
f'using {subjects_dir}/{subject_id} instead',
f' using {subjects_dir}/{subject_id} instead',
stacklevel=2,
)
return subjects_dir / subject_id
Expand Down
Loading