Skip to content

Commit

Permalink
changed absolute paths to relative paths
Browse files Browse the repository at this point in the history
Former-commit-id: e1e5d0d
  • Loading branch information
fishingguy456 committed May 25, 2022
1 parent 156d5d4 commit a783dca
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
6 changes: 6 additions & 0 deletions imgtools/io/dataset.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from genericpath import exists
import os
import pathlib
from typing import List, Sequence, Optional, Callable, Iterable, Dict,Tuple
from tqdm import tqdm

Expand Down Expand Up @@ -44,6 +45,11 @@ def load_from_nrrd(
if not os.path.exists(path_metadata):
raise ValueError("The specified path has no file name {}".format(path_metadata))
df_metadata = pd.read_csv(path_metadata,index_col=0)

for col in df_metadata.columns:
if col.startswith("folder"):
df_metadata[col] = df_metadata[col].apply(lambda x: pathlib.Path(os.path.split(os.path.dirname(path))[0], x).as_posix()) #input folder joined with the rel path

output_streams = [("_").join(cols.split("_")[1:]) for cols in df_metadata.columns if cols.split("_")[0] == "folder"]
imp_metadata = [cols for cols in df_metadata.columns if cols.split("_")[0] in ("metadata")]
#Ignores multiple connection to single modality
Expand Down
9 changes: 4 additions & 5 deletions imgtools/io/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,13 @@ def read_dicom_pet(path,series=None):
def read_dicom_auto(path, series=None):
if path is None:
return None
dcms = glob.glob(os.path.join(path, "*.dcm"))
dcms = glob.glob(pathlib.Path(path, "*.dcm").as_posix())
meta = dcmread(dcms[0])
modality = meta.Modality
if modality == 'CT' or modality == 'MR':
return read_dicom_series(path,series)
elif modality == 'PT':
return read_dicom_pet(path,series)
# elif len(dcms) == 1:
# meta = dcmread(dcms[0])
# modality = meta.Modality
elif modality == 'RTSTRUCT':
return read_dicom_rtstruct(dcms[0])
elif modality == 'RTDOSE':
Expand Down Expand Up @@ -141,7 +138,6 @@ def __init__(self,

self.colnames = colnames
self.seriesnames = seriesnames

if isinstance(csv_path_or_dataframe, str):
if id_column is not None and id_column not in colnames:
colnames.append(id_column)
Expand All @@ -168,6 +164,9 @@ def __getitem__(self, subject_id):
if self.expand_paths:
# paths = {col: glob.glob(path)[0] for col, path in paths.items()}
paths = {col: glob.glob(path)[0] if pd.notna(path) else None for col, path in paths.items()}
for col, path in paths.items():
print(path)

outputs = {col: self.readers[i](path,series["series_"+("_").join(col.split("_")[1:])]) for i, (col, path) in enumerate(paths.items())}
return self.output_tuple(**outputs)

Expand Down
6 changes: 6 additions & 0 deletions imgtools/modules/datagraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np
import pandas as pd
from tqdm import tqdm
import pathlib


class DataGraph:
Expand Down Expand Up @@ -276,6 +277,11 @@ def parser(self, query_string: str) -> pd.DataFrame:
final_df["index_chng"] = final_df.index.astype(str) + "_" + final_df["patient_ID"]
final_df.set_index("index_chng", inplace=True)
final_df.rename_axis(None, inplace=True)

#change relative paths to absolute paths
for col in final_df.columns:
if col.startswith("folder"):
final_df[col] = final_df[col].apply(lambda x: pathlib.Path(os.path.split(os.path.dirname(self.edge_path))[0], x).as_posix()) #input folder joined with the rel path
return final_df

def graph_query(self,
Expand Down
8 changes: 4 additions & 4 deletions imgtools/ops/ops.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import List, TypeVar, Sequence, Union, Tuple, Optional, Any
from cv2 import DFT_COMPLEX_INPUT

import numpy as np
import SimpleITK as sitk
Expand Down Expand Up @@ -98,6 +99,7 @@ def __init__(self,
graph = DataGraph(path_crawl=path_crawl, edge_path=edge_path, visualize=visualize)
print(f"Forming the graph based on the given modalities: {self.modalities}")
self.df_combined = graph.parser(self.modalities)
print(self.df_combined.head())
self.output_streams = [("_").join(cols.split("_")[1:]) for cols in self.df_combined.columns if cols.split("_")[0] == "folder"]
self.column_names = [cols for cols in self.df_combined.columns if cols.split("_")[0] == "folder"]
self.series_names = [cols for cols in self.df_combined.columns if cols.split("_")[0] == "series"]
Expand All @@ -110,7 +112,7 @@ def __init__(self,
colnames=self.column_names,
seriesnames=self.series_names,
id_column=None,
expand_paths=True,
expand_paths=False,
readers=self.readers)
super().__init__(loader)

Expand Down Expand Up @@ -297,9 +299,7 @@ def __init__(self,
self.create_dirs = create_dirs
self.compress = compress

writer_class = BaseSubjectWriter

writer = writer_class(self.root_directory,
writer = BaseSubjectWriter(self.root_directory,
self.filename_format,
self.create_dirs,
self.compress)
Expand Down
8 changes: 6 additions & 2 deletions imgtools/utils/crawl.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from argparse import ArgumentParser
import os
import os, pathlib
import glob
import json

Expand Down Expand Up @@ -64,6 +64,9 @@ def crawl_one(folder):
if study not in database[patient]:
database[patient][study] = {'description': study_description}
if series not in database[patient][study]:
parent, child = os.path.split(folder)
print(folder, path)
rel_path = pathlib.Path(os.path.split(parent)[1], os.path.relpath(path, parent)).as_posix()
database[patient][study][series] = {'instances': [],
'instance_uid': instance,
'modality': meta.Modality,
Expand All @@ -72,7 +75,7 @@ def crawl_one(folder):
'reference_rs': reference_rs,
'reference_pl': reference_pl,
'reference_frame': reference_frame,
'folder': path}
'folder': rel_path}
database[patient][study][series]['instances'].append(instance)
except:
pass
Expand Down Expand Up @@ -102,6 +105,7 @@ def to_df(database_dict):

def crawl(top,
n_jobs: int = -1):
#top is the input directory in the argument parser from autotest.py
database_list = []
folders = glob.glob(os.path.join(top, "*"))

Expand Down

0 comments on commit a783dca

Please sign in to comment.