Skip to content

Commit

Permalink
Add util to count axons + add docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
hermancollin committed Jul 8, 2024
1 parent 80d1d06 commit 8687cf3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
46 changes: 46 additions & 0 deletions stanford_pipeline/utils/count_axons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'''Reads myelinated + unmyelinated axon morphometric files and count axons.
'''
from pathlib import Path
from tqdm import tqdm
import argparse
import pandas as pd

def main():
parser = argparse.ArgumentParser(description='Aggregate morphometrics.')
parser.add_argument('input_dir', type=str, help='Path to the folder containing all morphometric files.')
parser.add_argument('output_name', type=str, help='Name of the output file.')

args = parser.parse_args()
input_dir = Path(args.input_dir)
out_name = args.output_name
assert input_dir.exists(), f'Directory {input_dir} does not exist.'

# find images except for the masks
mask_suffixes = (
'_seg-axon.png', '_seg-myelin.png', '_seg-uaxon.png', '_uaxon_index.png',
'_seg-axonmyelin.png', '_axonmyelin_index.png', '_index.png',
)
inputs = [f for f in input_dir.glob('*.png') if not f.name.endswith(mask_suffixes)]

counts = {'image': [], 'axon_count': [], 'uaxon_count': []}
axon_morph_suffix = '_axon_morphometrics.xlsx'
uaxon_morph_suffix = '_uaxon_morphometrics.xlsx'

for img in tqdm(inputs):
# read morphometric files
axon_count = len(pd.read_excel(str(img.with_suffix('')) + axon_morph_suffix))
uaxon_count = len(pd.read_excel(str(img.with_suffix('')) + uaxon_morph_suffix))

# add data
counts['image'].append(img.stem)
counts['axon_count'].append(axon_count)
counts['uaxon_count'].append(uaxon_count)

# save counts
df = pd.DataFrame(counts)
df.to_csv(out_name, index=False)



if __name__ == "__main__":
main()
4 changes: 4 additions & 0 deletions stanford_pipeline/utils/extract_axon_masks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
'''Extracts axon and myelin masks from nnunet raw predictions. This util writes
binary masks for axon, myelin and unmyelinated axon classes. The axon and myelin
masks are also merged.
'''
import cv2
import argparse
import numpy as np
Expand Down

0 comments on commit 8687cf3

Please sign in to comment.