Skip to content

Commit

Permalink
edits to evans index
Browse files Browse the repository at this point in the history
  • Loading branch information
wasserth committed Jan 28, 2025
1 parent 653b9c0 commit 88db8d9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 15 deletions.
22 changes: 8 additions & 14 deletions totalsegmentator/bin/totalseg_evans_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
from totalsegmentator.registration import calc_transform, apply_transform
from totalsegmentator.python_api import totalsegmentator
from totalsegmentator.nifti_ext_header import load_multilabel_nifti

from totalsegmentator.serialization_utils import decompress_and_deserialize, filestream_to_nifti, serialize_and_compress, convert_to_serializable
from totalsegmentator.dicom_io import dcm_to_nifti
from totalsegmentator.config import send_usage_stats_application


def run_models(ct_img):
Expand Down Expand Up @@ -97,8 +97,7 @@ def plot_slice_with_diameters(brain, start_b, end_b, start_v, end_v, evans_index
return buf.getvalue()


# def evans_index(brain_mask, ventricle_masks, skull_mask, ct_path, output_file, preview_file, models_parallel=False):
def evans_index(ct_bytes, output_file, preview_file, f_type):
def evans_index(ct_bytes, f_type):
"""
ct_bytes: path to nifti file or
bytes object of zip file or
Expand Down Expand Up @@ -215,27 +214,20 @@ def evans_index(ct_bytes, output_file, preview_file, f_type):


"""
# pip install antspyx
cd ~/Downloads/evans_index/31103170_rot_large
python ~/dev/TotalSegmentator/totalsegmentator/bin/totalseg_evans_index.py -b roi/brain.nii.gz -s roi/skull.nii.gz -v predicted/T552_ventricle_parts.nii.gz -c NR_01_SCHAEDEL/Sch_del_Syngo_1_0_Hr38_3_s003.nii -o evans_index_TEST.json -p evans_index_TEST.png
python ~/dev/TotalSegmentator/totalsegmentator/bin/totalseg_evans_index.py -i ct_sm.nii.gz -o evans_index_TEST.json -p evans_index.png
"""
if __name__ == "__main__":
"""
For more documentation see resources/evans_index.md
"""
parser = argparse.ArgumentParser(description="Calc evans index.")
parser.add_argument("-b", "--brain_mask", type=lambda p: Path(p).resolve(), required=True,
help="Path to brain mask file.")
parser.add_argument("-v", "--ventricle_masks", type=lambda p: Path(p).resolve(), required=True,
help="Path to ventricle part masks.")
parser.add_argument("-s", "--skull_mask", type=lambda p: Path(p).resolve(), required=True,
help="Path to skull masks.")
parser.add_argument("-c", "--ct_img", type=lambda p: Path(p).resolve(), required=True,
parser = argparse.ArgumentParser(description="Calculate evans index.")
parser.add_argument("-i", "--ct_img", type=lambda p: Path(p).resolve(), required=True,
help="Path to ct_img.")
parser.add_argument("-o", "--output_file", type=lambda p: Path(p).resolve(), required=True,
help="json output file.")
parser.add_argument("-p", "--preview_file", type=lambda p: Path(p).resolve(), required=True,
help="Preview file of evans index to check if correct.")
help="Preview PNG file of evans index to check if calculation is correct.")
args = parser.parse_args()

try:
Expand Down Expand Up @@ -265,3 +257,5 @@ def evans_index(ct_bytes, output_file, preview_file, f_type):
# Save report png
with open(args.preview_file, "wb") as f:
f.write(final_result["report_png"])

send_usage_stats_application("evans_index")
3 changes: 3 additions & 0 deletions totalsegmentator/bin/totalseg_get_modality.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import nibabel as nib
import numpy as np

from totalsegmentator.config import send_usage_stats_application

"""
Additional requirements for this script:
Expand Down Expand Up @@ -84,6 +85,8 @@ def main():
with open(args.output_file, "w") as f:
f.write(json.dumps(res, indent=4))

send_usage_stats_application("get_modality")


if __name__ == "__main__":
main()
4 changes: 3 additions & 1 deletion totalsegmentator/bin/totalseg_get_phase.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import numpy as np

from totalsegmentator.python_api import totalsegmentator

from totalsegmentator.config import send_usage_stats_application

"""
Additional requirements for this script:
Expand Down Expand Up @@ -153,6 +153,8 @@ def main():
with open(args.output_file, "w") as f:
f.write(json.dumps(res, indent=4))

send_usage_stats_application("get_phase")


if __name__ == "__main__":
main()
38 changes: 38 additions & 0 deletions totalsegmentator/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@ def increase_prediction_counter():
return config


def get_config():
totalseg_dir = get_totalseg_dir()
totalseg_config_file = totalseg_dir / "config.json"
if totalseg_config_file.exists():
with open(totalseg_config_file) as f:
config = json.load(f)
return config
else:
return None


def get_version():
try:
return pkg_resources.get_distribution("TotalSegmentator").version
Expand Down Expand Up @@ -238,3 +249,30 @@ def send_usage_stats(config, params):
except Exception as e:
# print(f"An Exception occurred: {e}")
pass


def send_usage_stats_application(application_name):
config = get_config()
if config is not None and config["send_usage_stats"]:

try:
st = time.time()
url = "http://backend.totalsegmentator.com:80/"
r = requests.post(url + "log_totalseg_application_run",
json={"totalseg_id": config["totalseg_id"],
"application": application_name,
"platform": platform.system(),
"machine": platform.machine(),
"version": get_version(),
"python_version": sys.version,
"cuda_available": torch.cuda.is_available()
}, timeout=5)
# if r.ok:
# print(f"status: {r.json()['status']}")
# else:
# print(f"status code: {r.status_code}")
# print(f"message: {r.json()['message']}")
# print(f"Request took {time.time()-st:.3f}s")
except Exception as e:
# print(f"An Exception occurred: {e}")
pass

0 comments on commit 88db8d9

Please sign in to comment.