Commit 7d9ae5ded8a51f3a372f817dba13baad462767f2
1 parent
cc5b7023
Exists in
master
and in
67 other branches
ADD: function calculate_resizing_tofitmemory
Showing
1 changed file
with
45 additions
and
0 deletions
Show diff stats
invesalius/utils.py
| ... | ... | @@ -22,6 +22,7 @@ import sigar |
| 22 | 22 | import sys |
| 23 | 23 | import re |
| 24 | 24 | import locale |
| 25 | +import math | |
| 25 | 26 | |
| 26 | 27 | def format_time(value): |
| 27 | 28 | sp1 = value.split(".") |
| ... | ... | @@ -203,6 +204,49 @@ def frange(start, end=None, inc=None): |
| 203 | 204 | return L |
| 204 | 205 | |
| 205 | 206 | |
| 207 | + | |
| 208 | +def calculate_resizing_tofitmemory(x_size,y_size,n_slices,byte): | |
| 209 | + """ | |
| 210 | + Predicts the percentage (between 0 and 1) to resize the image to fit the memory, | |
| 211 | + giving the following information: | |
| 212 | + x_size, y_size: image size | |
| 213 | + n_slices: number of slices | |
| 214 | + byte: bytes allocated for each pixel sample | |
| 215 | + """ | |
| 216 | + imagesize = x_size * y_size * n_slices * byte | |
| 217 | + | |
| 218 | + sg = sigar.open() | |
| 219 | + ram_free = sg.mem().actual_free() | |
| 220 | + ram_total = sg.mem().actual_free() | |
| 221 | + swap_free = sg.swap().free() | |
| 222 | + sg.close() | |
| 223 | + | |
| 224 | + if (sys.platform == 'win32'): | |
| 225 | + if (platform.architecture()[0] == '32bit'): | |
| 226 | + if ram_free>1400000000: | |
| 227 | + ram_free=1400000000 | |
| 228 | + if ram_total>1400000000: | |
| 229 | + ram_total=1400000000 | |
| 230 | + | |
| 231 | + if (sys.platform == 'linux2'): | |
| 232 | + if (platform.architecture()[0] == '32bit'): | |
| 233 | + if ram_free>3500000000: | |
| 234 | + ram_free=3500000000 | |
| 235 | + if ram_total>3500000000: | |
| 236 | + ram_total=3500000000 | |
| 237 | + | |
| 238 | + if (swap_free>ram_total): | |
| 239 | + swap_free=ram_total | |
| 240 | + resize = (float((ram_free+0.5*swap_free)/imagesize)) | |
| 241 | + resize=math.sqrt(resize) # this gives the "resize" for each axis x and y | |
| 242 | + if (resize>1): | |
| 243 | + resize=1 | |
| 244 | + return (100*resize) | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 206 | 250 | def predict_memory(nfiles, x, y, p): |
| 207 | 251 | """ |
| 208 | 252 | Predict how much memory will be used, giving the following |
| ... | ... | @@ -294,6 +338,7 @@ def get_physical_memory(): |
| 294 | 338 | return int(mem.total()) |
| 295 | 339 | |
| 296 | 340 | |
| 341 | + | |
| 297 | 342 | def get_system_encoding(): |
| 298 | 343 | if (sys.platform == 'win32'): |
| 299 | 344 | return locale.getdefaultlocale()[1] | ... | ... |