Skip to content

Commit 2799c29

Browse files
committed
🔀 Merge branch 'datetime-none' into 'organism'
2 parents a08f167 + d5fe464 commit 2799c29

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

CPAC/distortion_correction/distortion_correction.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
3-
# Copyright (C) 2017-2022 C-PAC Developers
3+
# Copyright (C) 2017-2025 C-PAC Developers
44

55
# This file is part of C-PAC.
66

@@ -16,6 +16,8 @@
1616

1717
# You should have received a copy of the GNU Lesser General Public
1818
# License along with C-PAC. If not, see <https://www.gnu.org/licenses/>.
19+
"""Distortion correction in C-PAC."""
20+
1921
import os
2022
import subprocess
2123

@@ -663,7 +665,7 @@ def distcor_blip_fsl_topup(wf, cfg, strat_pool, pipe_num, opt=None):
663665
"import os",
664666
"import subprocess",
665667
"import numpy as np",
666-
"import nibabel",
668+
"import nibabel as nib",
667669
"import sys",
668670
]
669671
phase_encoding = pe.Node(

CPAC/distortion_correction/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2021-2023 C-PAC Developers
1+
# Copyright (C) 2021-2025 C-PAC Developers
22

33
# This file is part of C-PAC.
44

@@ -14,6 +14,8 @@
1414

1515
# You should have received a copy of the GNU Lesser General Public
1616
# License along with C-PAC. If not, see <https://www.gnu.org/licenses/>.
17+
"""Distortion correction utilities."""
18+
1719
import os
1820
import subprocess
1921
import sys

CPAC/utils/monitoring/monitoring.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import os
2424
import socketserver
2525
import threading
26-
from typing import Optional, TypeAlias
26+
from typing import Any, Optional, TypeAlias
2727

2828
import networkx as nx
2929
from traits.trait_base import Undefined
@@ -124,6 +124,23 @@ def __str__(self) -> str:
124124
return super().__str__()
125125

126126

127+
class DatetimeJSONEncoder(json.JSONEncoder):
128+
"""JSON encoder that handles DatetimeWithSafeNone instances."""
129+
130+
def default(self, o: Any) -> str:
131+
"""Convert datetime objects to ISO format."""
132+
if isinstance(o, datetime):
133+
return o.isoformat()
134+
if o is None or o is NoTime:
135+
return ""
136+
return super().default(o)
137+
138+
139+
def json_dumps(obj: Any, **kwargs) -> str:
140+
"""Convert an object to a JSON string."""
141+
return json.dumps(obj, cls=DatetimeJSONEncoder, **kwargs)
142+
143+
127144
OptionalDatetime: TypeAlias = Optional[datetime | str | DatetimeWithSafeNone | _NoTime]
128145
"""Type alias for a datetime, ISO-format string or None."""
129146

@@ -144,7 +161,7 @@ def recurse_nodes(workflow, prefix=""):
144161
def log_nodes_initial(workflow):
145162
logger = getLogger("callback")
146163
for node in recurse_nodes(workflow):
147-
logger.debug(json.dumps(node))
164+
logger.debug(json_dumps(node))
148165

149166

150167
def log_nodes_cb(node, status):
@@ -242,7 +259,7 @@ def log_nodes_cb(node, status):
242259
):
243260
status_dict["error"] = True
244261

245-
logger.debug(json.dumps(status_dict))
262+
logger.debug(json_dumps(status_dict))
246263

247264

248265
log_nodes_cb.__doc__ = f"""{_nipype_log_nodes_cb.__doc__}
@@ -299,7 +316,7 @@ def handle(self):
299316
tree = {s: t for s, t in tree.items() if t}
300317

301318
headers = "HTTP/1.1 200 OK\nConnection: close\n\n"
302-
self.request.sendall(headers + json.dumps(tree) + "\n")
319+
self.request.sendall(headers + json_dumps(tree) + "\n")
303320

304321

305322
class LoggingHTTPServer(socketserver.ThreadingTCPServer, object):

0 commit comments

Comments
 (0)