[6fe801]: / utils / filemanager.py

Download this file

126 lines (98 with data), 3.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import os
import sys
import pandas as pd
from glob import glob
from .logger import logger
import re
def extract_parameter(text):
'''
Extract the parameter number from the text.
Args:
text ('str'): Text to search for the parameter number.
Returns:
parameter_number ('str'): Parameter number.
'''
match = re.search(r'Par(\w+)', text)
if match:
return 'Par' + match.group(1)
else:
return None
def get_points_paths(base_path, suffix, num_occurrences = 2):
'''
Get the paths of the transformed points.
Args:
args ('argparse.Namespace'): Command line arguments.
suffix ('str'): Suffix of the file name.
num_occurrences ('int'): Number of occurrences of the suffix in the path.
Returns:
paths ('list'): List of paths.
'''
paths = [path.replace('\\', '/') for path in sorted(glob(os.path.join(base_path, *["***"] * num_occurrences , f"*{suffix}.txt"), recursive=True))]
return paths
def get_paths(args, suffix):
'''
Get the paths of the volumes and segmentations.
Args:
args ('argparse.Namespace'): Command line arguments.
suffix ('str'): Suffix of the file name.
Returns:
paths ('list'): List of paths.
'''
paths = [path.replace('\\', '/') for path in sorted(glob(os.path.join(args.dataset_path, "***" , f"*{suffix}.nii.gz"), recursive=True))]
return paths
def check_paths(args, paths, message):
'''
Check if the paths exist.
Args:
args ('argparse.Namespace'): Command line arguments.
paths ('list'): List of paths.
message ('str'): Message to display in the logger.
Returns:
None
'''
if len(paths) == 0:
logger.error(f"No {message} found in {args.dataset_path} directory.")
sys.exit(1)
def create_directory_if_not_exists(path):
'''
Create a directory if it does not exist.
Args:
path ('str'): Directory path.
'''
if not os.path.exists(path):
os.makedirs(path)
def replace_text_in_file(file_path, search_text, replacement_text):
'''
Replace text in a text file.
Args:
file_path ('str'): Path to the text file.
search_text ('str'): Text to search for in the file.
replacement_text ('str'): Text to replace the searched text with.
'''
try:
# Read the file
with open(file_path, 'r') as file:
content = file.read()
# Replace the search_text with replacement_text
modified_content = content.replace(search_text, replacement_text)
# Write the modified content back to the file
with open(file_path, 'w') as file:
file.write(modified_content)
# print(f"Text replaced in {file_path} and saved.")
except FileNotFoundError:
print(f"File not found: {file_path}")
# except Exception as e:
# print(f"An error occurred: {e}")
def add_and_delete_rows(file_path, row1, row2):
# Add two rows at the beginning of the file
with open(file_path, 'r+') as file:
content = file.read()
file.seek(0, 0)
file.write(row1 + '\n' + row2 + '\n' + content)
def delete_added_rows(file_path):
# Delete the first two rows from the file
with open(file_path, 'r') as file:
lines = file.readlines()
# Handle different newline characters when writing back to the file
with open(file_path, 'w', newline=os.linesep) as file:
file.writelines(lines[2:])