Skip to content

Commit 72eaa5a

Browse files
Expose smart transducer loading as util #380 #381
"Smart transducer loading" means reading via Transducer or TransducerArray's serializer depending on the "type" field in the json file. This functionality was stuck inside of Databse, but we expose it as a general utility to support its use in SlicerOpenLIFU.
1 parent e1c26c6 commit 72eaa5a

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

src/openlifu/db/database.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from openlifu.plan import Protocol, Run, Solution
1616
from openlifu.util.json import PYFUSEncoder
1717
from openlifu.xdc import Transducer, TransducerArray
18+
from openlifu.xdc.util import load_transducer_from_file
1819

1920
from .session import Session
2021
from .subject import Subject
@@ -861,18 +862,10 @@ def load_transducer(self, transducer_id, convert_array:bool = True) -> Transduce
861862
Returns:
862863
Corresponding Transducer object
863864
"""
864-
transducer_filename = self.get_transducer_filename(transducer_id)
865-
with open(transducer_filename) as f:
866-
if not f:
867-
raise FileNotFoundError(f"Transducer file not found for ID: {transducer_id}")
868-
d = json.load(f)
869-
if "type" in d and d["type"] == "TransducerArray":
870-
transducer = TransducerArray.from_dict(d)
871-
if convert_array:
872-
transducer = transducer.to_transducer()
873-
else:
874-
transducer = Transducer.from_file(transducer_filename)
875-
return transducer
865+
return load_transducer_from_file(
866+
transducer_filepath=self.get_transducer_filename(transducer_id),
867+
convert_array=convert_array
868+
)
876869

877870
def load_transducer_standoff(self, trans, coords, options=None):
878871
raise NotImplementedError("Standoff is not yet implemented")

src/openlifu/xdc/util.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from __future__ import annotations
2+
3+
import json
4+
import os
5+
from typing import Union
6+
7+
from openlifu.xdc.transducer import Transducer
8+
from openlifu.xdc.transducerarray import TransducerArray
9+
10+
PathLike = Union[str,os.PathLike]
11+
12+
def load_transducer_from_file(transducer_filepath : PathLike, convert_array:bool = True) -> Transducer|TransducerArray:
13+
"""Load a Transducer or TransducerArray from file, depending on the "type" field in the file.
14+
Note: the transducer object includes the relative path to the affiliated transducer model data. `get_transducer_absolute_filepaths`, should
15+
be used to obtain the absolute data filepaths based on the Database directory path.
16+
Args:
17+
transducer_filepath: path to the transducer json file
18+
convert_array: When enabled, if a TransducerArray is encountered then it is converted to a Transducer.
19+
Returns: a Transducer if the json file defines a Transducer, or if the json file defines a TransducerArray and convert_array is enabled.
20+
Otherwise a TransducerArray.
21+
"""
22+
with open(transducer_filepath) as f:
23+
if not f:
24+
raise FileNotFoundError(f"Transducer file not found at: {transducer_filepath}")
25+
d = json.load(f)
26+
if "type" in d and d["type"] == "TransducerArray":
27+
transducer = TransducerArray.from_dict(d)
28+
if convert_array:
29+
transducer = transducer.to_transducer()
30+
else:
31+
transducer = Transducer.from_file(transducer_filepath)
32+
return transducer

0 commit comments

Comments
 (0)