File tree Expand file tree Collapse file tree 5 files changed +46
-1
lines changed Expand file tree Collapse file tree 5 files changed +46
-1
lines changed Original file line number Diff line number Diff line change 50
50
- name : Install minimal dependencies
51
51
run : |
52
52
$CONDA/bin/pip install -r min-requirements.txt
53
- $CONDA/bin/pip install .
54
53
$CONDA/bin/pip install .[tests]
55
54
56
55
- uses : actions/cache@v2
Original file line number Diff line number Diff line change @@ -227,6 +227,8 @@ class Coefficients2Warp(SimpleInterface):
227
227
output_spec = _Coefficients2WarpOutputSpec
228
228
229
229
def _run_interface (self , runtime ):
230
+ from ..utils .misc import get_free_mem
231
+
230
232
# Calculate the physical coordinates of target grid
231
233
targetnii = nb .load (self .inputs .in_target )
232
234
targetaff = targetnii .affine
@@ -237,11 +239,21 @@ def _run_interface(self, runtime):
237
239
weights = []
238
240
coeffs = []
239
241
blocksize = LOW_MEM_BLOCK_SIZE if self .inputs .low_mem else len (points )
242
+
240
243
for cname in self .inputs .in_coeff :
241
244
cnii = nb .load (cname )
242
245
cdata = cnii .get_fdata (dtype = "float32" )
243
246
coeffs .append (cdata .reshape (- 1 ))
244
247
248
+ # Try to probe the free memory
249
+ _free_mem = get_free_mem ()
250
+ suggested_blocksize = (
251
+ int (np .round ((_free_mem * 0.80 ) / (3 * 32 * cdata .size )))
252
+ if _free_mem
253
+ else blocksize
254
+ )
255
+ blocksize = min (blocksize , suggested_blocksize )
256
+
245
257
idx = 0
246
258
block_w = []
247
259
while True :
Original file line number Diff line number Diff line change @@ -35,3 +35,13 @@ def last(inlist):
35
35
if isinstance (inlist , (list , tuple )):
36
36
return inlist [- 1 ]
37
37
return inlist
38
+
39
+
40
+ def get_free_mem ():
41
+ """Probe the free memory right now."""
42
+ try :
43
+ from psutil import virtual_memory
44
+
45
+ return round (virtual_memory ().free , 1 )
46
+ except Exception :
47
+ return None
Original file line number Diff line number Diff line change
1
+ """Test miscellaneous utilities."""
2
+ import sys
3
+ from collections import namedtuple
4
+ import types
5
+ import pytest
6
+ from ..misc import get_free_mem
7
+
8
+
9
+ @pytest .mark .parametrize ("retval" , [None , 10 ])
10
+ def test_get_free_mem (monkeypatch , retval ):
11
+ """Test the get_free_mem utility."""
12
+
13
+ def mock_func ():
14
+ if retval is None :
15
+ raise ImportError
16
+ return namedtuple ("Mem" , ("free" ,))(free = retval )
17
+
18
+ psutil = types .ModuleType ("psutil" )
19
+ psutil .virtual_memory = mock_func
20
+ monkeypatch .setitem (sys .modules , "psutil" , psutil )
21
+ assert get_free_mem () == retval
Original file line number Diff line number Diff line change 56
56
sphinxcontrib-versioning
57
57
docs =
58
58
%(doc)s
59
+ mem =
60
+ psutil
59
61
tests =
60
62
pytest
61
63
pytest-xdist >= 2.0
@@ -64,6 +66,7 @@ tests =
64
66
coverage
65
67
all =
66
68
%(doc)s
69
+ %(mem)s
67
70
%(tests)s
68
71
69
72
[options.package_data]
You can’t perform that action at this time.
0 commit comments