Skip to content

Commit 828953e

Browse files
authored
Determine total memory without psutil (#12787)
1 parent 5cd5299 commit 828953e

4 files changed

Lines changed: 58 additions & 7 deletions

File tree

doc/changes/devel/12787.other.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use custom code in :func:`mne.sys_info` to get the amount of physical memory and a more informative CPU name instead of using the ``psutil`` package, by `Clemens Brunner`_.

mne/utils/config.py

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
_temp_home_dir = None
3131

3232

33+
class UnknownPlatformError(Exception):
34+
"""Exception raised for unknown platforms."""
35+
36+
3337
def set_cache_dir(cache_dir):
3438
"""Set the directory to be used for temporary file storage.
3539
@@ -605,6 +609,47 @@ def _get_gpu_info():
605609
return out
606610

607611

612+
def _get_total_memory():
613+
"""Return the total memory of the system in bytes."""
614+
if platform.system() == "Windows":
615+
o = subprocess.check_output(
616+
[
617+
"powershell.exe",
618+
"(Get-CimInstance Win32_ComputerSystem).TotalPhysicalMemory",
619+
]
620+
).decode()
621+
total_memory = int(o)
622+
elif platform.system() == "Linux":
623+
o = subprocess.check_output(["free", "-b"]).decode()
624+
total_memory = int(o.splitlines()[1].split()[1])
625+
elif platform.system() == "Darwin":
626+
o = subprocess.check_output(["sysctl", "hw.memsize"]).decode()
627+
total_memory = int(o.split(":")[1].strip())
628+
else:
629+
raise UnknownPlatformError("Could not determine total memory")
630+
631+
return total_memory
632+
633+
634+
def _get_cpu_brand():
635+
"""Return the CPU brand string."""
636+
if platform.system() == "Windows":
637+
o = subprocess.check_output(
638+
["powershell.exe", "(Get-CimInstance Win32_Processor).Name"]
639+
).decode()
640+
cpu_brand = o.strip().splitlines()[-1]
641+
elif platform.system() == "Linux":
642+
o = subprocess.check_output(["grep", "model name", "/proc/cpuinfo"]).decode()
643+
cpu_brand = o.splitlines()[0].split(": ")[1]
644+
elif platform.system() == "Darwin":
645+
o = subprocess.check_output(["sysctl", "machdep.cpu"]).decode()
646+
cpu_brand = o.split("brand_string: ")[1].strip()
647+
else:
648+
cpu_brand = "?"
649+
650+
return cpu_brand
651+
652+
608653
def sys_info(
609654
fid=None,
610655
show_paths=False,
@@ -656,15 +701,20 @@ def sys_info(
656701
out("Platform".ljust(ljust) + platform_str + "\n")
657702
out("Python".ljust(ljust) + str(sys.version).replace("\n", " ") + "\n")
658703
out("Executable".ljust(ljust) + sys.executable + "\n")
659-
out("CPU".ljust(ljust) + f"{platform.processor()} ")
704+
try:
705+
cpu_brand = _get_cpu_brand()
706+
except Exception:
707+
cpu_brand = "?"
708+
out("CPU".ljust(ljust) + f"{cpu_brand} ")
660709
out(f"({multiprocessing.cpu_count()} cores)\n")
661710
out("Memory".ljust(ljust))
662711
try:
663-
import psutil
664-
except ImportError:
665-
out('Unavailable (requires "psutil" package)')
712+
total_memory = _get_total_memory()
713+
except UnknownPlatformError:
714+
total_memory = "?"
666715
else:
667-
out(f"{psutil.virtual_memory().total / float(2 ** 30):0.1f} GB\n")
716+
total_memory = f"{total_memory / 1024**3:.1f}" # convert to GiB
717+
out(f"{total_memory} GiB\n")
668718
out("\n")
669719
ljust -= 3 # account for +/- symbols
670720
libs = _get_numpy_libs()

mne/utils/tests/test_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def test_sys_info_basic():
110110
assert "numpy" in out
111111
# replace all in-line whitespace with single space
112112
out = "\n".join(" ".join(o.split()) for o in out.splitlines())
113-
113+
assert "? GiB" not in out
114114
if platform.system() == "Darwin":
115115
assert "Platform macOS-" in out
116116
elif platform.system() == "Linux":

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ full-no-qt = [
7878
"jupyter",
7979
"python-picard",
8080
"joblib",
81-
"psutil",
8281
"dipy",
8382
"vtk",
8483
"nilearn",
@@ -175,6 +174,7 @@ doc = [
175174
"intersphinx_registry>=0.2405.27",
176175
# https://github.com/sphinx-contrib/sphinxcontrib-towncrier/issues/92
177176
"towncrier<24.7",
177+
"psutil",
178178
]
179179
dev = ["mne[test,doc]", "rcssmin"]
180180

0 commit comments

Comments
 (0)