[mlpack-svn] r15547 - in mlpack/conf/jenkins-conf/benchmark: . benchmark methods/scikit methods/shogun util

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Thu Jul 25 16:35:03 EDT 2013


Author: marcus
Date: Thu Jul 25 16:35:03 2013
New Revision: 15547

Log:
Update the code to be compatible with python 3.

Modified:
   mlpack/conf/jenkins-conf/benchmark/Makefile
   mlpack/conf/jenkins-conf/benchmark/benchmark/run_benchmark.py
   mlpack/conf/jenkins-conf/benchmark/methods/scikit/kmeans.py
   mlpack/conf/jenkins-conf/benchmark/methods/scikit/nmf.py
   mlpack/conf/jenkins-conf/benchmark/methods/shogun/kmeans.py
   mlpack/conf/jenkins-conf/benchmark/util/convert.py
   mlpack/conf/jenkins-conf/benchmark/util/log.py
   mlpack/conf/jenkins-conf/benchmark/util/parser.py
   mlpack/conf/jenkins-conf/benchmark/util/system.py

Modified: mlpack/conf/jenkins-conf/benchmark/Makefile
==============================================================================
--- mlpack/conf/jenkins-conf/benchmark/Makefile	(original)
+++ mlpack/conf/jenkins-conf/benchmark/Makefile	Thu Jul 25 16:35:03 2013
@@ -1,5 +1,5 @@
-PYTHON_BIN := $(shell which python)
-PYTHON_VERSION := $(shell expr `$(PYTHON_BIN) -c 'import sys; print sys.version[:3]'` \>= 2.7)
+PYTHON_BIN := $(shell which python3.3)
+PYTHON_VERSION := $(shell expr `$(PYTHON_BIN) -c 'import sys; print(sys.version[:3])'` \>= 2.7)
 YAML_INSTALLED := $(shell $(PYTHON_BIN) -c 'import sys, yaml;' 2>&1)
 
 CONFIG := config.yaml

Modified: mlpack/conf/jenkins-conf/benchmark/benchmark/run_benchmark.py
==============================================================================
--- mlpack/conf/jenkins-conf/benchmark/benchmark/run_benchmark.py	(original)
+++ mlpack/conf/jenkins-conf/benchmark/benchmark/run_benchmark.py	Thu Jul 25 16:35:03 2013
@@ -41,7 +41,7 @@
 @return Normalized dataset name.
 '''
 def NormalizeDatasetName(dataset):
-  if not isinstance(dataset, basestring):
+  if not isinstance(dataset, str):
     return os.path.splitext(os.path.basename(dataset[0]))[0].split('_')[0]
   else:
     return os.path.splitext(os.path.basename(dataset))[0].split('_')[0]
@@ -69,7 +69,7 @@
 '''
 def GetDataset(dataset, format):
   # Check if the given dataset is a list or a single dataset.
-  if not isinstance(dataset, basestring):
+  if not isinstance(dataset, str):
     datasetList = []
     modifiedList = []
 
@@ -107,7 +107,7 @@
 @para dataset - File or list of file which should be deleted.
 '''
 def RemoveDataset(dataset):
-  if isinstance(dataset, basestring):
+  if isinstance(dataset, str):
     dataset = [dataset]
 
   for f in dataset:
@@ -182,8 +182,8 @@
       datasetCount = CountLibrariesDatasets(libraries)
 
       # Create the matrix which contains the time and dataset informations.
-      dataMatrix = [['-' for x in xrange(len(libraries) + 1)] for x in 
-          xrange(datasetCount)] 
+      dataMatrix = [['-' for x in range(len(libraries) + 1)] for x in 
+          range(datasetCount)] 
 
       col = 1
       for libary in libraries:
@@ -200,8 +200,9 @@
         try:
           module = Loader.ImportModuleFromPath(script)
           methodCall = getattr(module, method)
-        except Exception, e:
+        except Exception as e:
           Log.Fatal("Could not load the script: " + script)
+          Log.Fatal("Exception: " + str(e))
           continue
 
         for dataset in datsets:  
@@ -215,8 +216,9 @@
 
           try:
             instance = methodCall(modifiedDataset[0], verbose=False)
-          except Exception, e:
+          except Exception as e:
             Log.Fatal("Could not call the constructor: " + script)
+            Log.Fatal("Exception: " + str(e))
             continue
 
           time = 0
@@ -247,4 +249,4 @@
 
   if args:
     SystemInformation()
-    Main(args.config)
\ No newline at end of file
+    Main(args.config)

Modified: mlpack/conf/jenkins-conf/benchmark/methods/scikit/kmeans.py
==============================================================================
--- mlpack/conf/jenkins-conf/benchmark/methods/scikit/kmeans.py	(original)
+++ mlpack/conf/jenkins-conf/benchmark/methods/scikit/kmeans.py	Thu Jul 25 16:35:03 2013
@@ -65,7 +65,7 @@
     if not clusters and len(self.dataset) != 2:
       Log.Fatal("Required option: Number of clusters or cluster locations.")
       return -1
-    elif (not clusters or clusters.group(1) < 1) and len(self.dataset) != 2:
+    elif (not clusters or int(clusters.group(1)) < 1) and len(self.dataset) != 2:
       Log.Fatal("Invalid number of clusters requested! Must be greater than or "
           + "equal to 1.")
       return -1

Modified: mlpack/conf/jenkins-conf/benchmark/methods/scikit/nmf.py
==============================================================================
--- mlpack/conf/jenkins-conf/benchmark/methods/scikit/nmf.py	(original)
+++ mlpack/conf/jenkins-conf/benchmark/methods/scikit/nmf.py	Thu Jul 25 16:35:03 2013
@@ -52,22 +52,11 @@
 
     with totalTimer:      
       # Gather parameters.
-      rank = re.search("-r (\d+)", options)
       seed = re.search("-s (\d+)", options)
       maxIterations = re.search("-m (\d+)", options)
       minResidue = re.search("-e ([^\s]+)", options)
       updateRule = re.search("-u ([^\s]+)", options)
 
-      # Validate rank.
-      if not rank:
-        Log.Fatal("Required option: Rank of the factorization.")
-        return -1
-      else:
-        rank = rank.group(1)
-        if rank < 1:
-          Log.Fatal("The rank of the factorization cannot be less than 1.")
-          return -1
-
       m = 10000 if not maxIterations else int(maxIterations.group(1))
       e = 1e-05 if not maxIterations else int(minResidue.group(1))
 

Modified: mlpack/conf/jenkins-conf/benchmark/methods/shogun/kmeans.py
==============================================================================
--- mlpack/conf/jenkins-conf/benchmark/methods/shogun/kmeans.py	(original)
+++ mlpack/conf/jenkins-conf/benchmark/methods/shogun/kmeans.py	Thu Jul 25 16:35:03 2013
@@ -57,7 +57,7 @@
     if not clusters and len(self.dataset) != 2:
       Log.Fatal("Required option: Number of clusters or cluster locations.")
       return -1
-    elif (not clusters or clusters.group(1) < 1):
+    elif (not clusters or int(clusters.group(1)) < 1):
       Log.Fatal("Invalid number of clusters requested! Must be greater than or "
           + "equal to 1.")
       return -1
@@ -77,7 +77,7 @@
           + str(maxIterations))
       try:
         s = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=False) 
-      except Exception, e:
+      except Exception as e:
         Log.Fatal("Could not execute command: " + str(cmd))
         return -1
 
@@ -139,7 +139,7 @@
   def parseTimer(self, data):
     # Compile the regular expression pattern into a regular expression object to
     # parse the timer data.
-    pattern = re.compile(r"""
+    pattern = re.compile(br"""
         .*?total_time: (?P<total_time>.*?)s.*?
         """, re.VERBOSE|re.MULTILINE|re.DOTALL)
     

Modified: mlpack/conf/jenkins-conf/benchmark/util/convert.py
==============================================================================
--- mlpack/conf/jenkins-conf/benchmark/util/convert.py	(original)
+++ mlpack/conf/jenkins-conf/benchmark/util/convert.py	Thu Jul 25 16:35:03 2013
@@ -62,7 +62,7 @@
 
     # Read the first to get the attributes count.
     fid = open(data)
-    head = [fid.next() for x in xrange(1)]
+    head = [next(fid) for x in range(1)]
     fid.close()
     
     # We can convert files with ' ' and ',' as seperator.

Modified: mlpack/conf/jenkins-conf/benchmark/util/log.py
==============================================================================
--- mlpack/conf/jenkins-conf/benchmark/util/log.py	(original)
+++ mlpack/conf/jenkins-conf/benchmark/util/log.py	Thu Jul 25 16:35:03 2013
@@ -21,39 +21,39 @@
 	else:
 		BASH_RED = '\033[0;31m'
 		BASH_GREEN = '\033[0;32m'
-  		BASH_YELLOW = '\033[0;33m'
-  		BASH_CYAN = '\033[0;36m'
-  		BASH_CLEAR = '\033[0m'
+		BASH_YELLOW = '\033[0;33m'
+		BASH_CYAN = '\033[0;36m'
+		BASH_CLEAR = '\033[0m'
 
   	#! Prints debug output with the appropriate tag: [DEBUG].
 	@staticmethod
 	def Debug(line, verbose=True):
 		if verbose:
-			print >> sys.stdout, Log.BASH_CYAN + '[DEBUG] ' + Log.BASH_CLEAR + Log.WrapLine(line)
+			print(Log.BASH_CYAN + '[DEBUG] ' + Log.BASH_CLEAR + Log.WrapLine(line), file=sys.stdout)
 
 	#! Prints informational messages prefixed with [INFO ].
 	@staticmethod
 	def Info(line, verbose=True):
 		if verbose:
-			print >> sys.stdout, Log.BASH_GREEN + '[INFO ] ' + Log.BASH_CLEAR + Log.WrapLine(line)
+			print(Log.BASH_GREEN + '[INFO ] ' + Log.BASH_CLEAR + Log.WrapLine(line), file=sys.stdout)
 
 	#! Prints warning messages prefixed with [WARN ].
 	@staticmethod
 	def Warn(line, verbose=True):
 		if verbose:
-			print >> sys.stdout, Log.BASH_YELLOW + '[WARN ] ' + Log.BASH_CLEAR + Log.WrapLine(line)
+			print(Log.BASH_YELLOW + '[WARN ] ' + Log.BASH_CLEAR + Log.WrapLine(line), file=sys.stdout)
 
 	#! Prints fatal messages prefixed with [FATAL].
 	@staticmethod
 	def Fatal(line, verbose=True):
 		if verbose:
-			print >> sys.stderr, Log.BASH_RED + '[FATAL] ' + Log.BASH_CLEAR + Log.WrapLine(line)
+			print(Log.BASH_RED + '[FATAL] ' + Log.BASH_CLEAR + Log.WrapLine(line), file=sys.stdout)
 
 	#! Prints messages without any prefixed.
 	@staticmethod
 	def Notice(line, verbose=True):
 		if verbose:
-			print >> sys.stdout, Log.WrapLine(line)
+			print(Log.WrapLine(line), file=sys.stdout)
 
 	# Truncate the String into lines of 80 characters.
 	@staticmethod
@@ -72,9 +72,8 @@
 			colPaddings.append(MaxWidth(table, i))
 
 		for row in table:
-			print >> sys.stdout, row[0].ljust(colPaddings[0] + 1),
-
+			print(row[0].ljust(colPaddings[0] + 1), end=" ", file=sys.stdout)
 			for i in range(1, len(row)):
-				col = str(row[i]).rjust(colPaddings[i] + 2)
-				print >> sys.stdout, col,
-			print >> sys.stdout
\ No newline at end of file
+				col = format(row[i]).rjust(colPaddings[i] + 1)
+				print(col, end=" ", file=sys.stdout)
+			print(file=sys.stdout)

Modified: mlpack/conf/jenkins-conf/benchmark/util/parser.py
==============================================================================
--- mlpack/conf/jenkins-conf/benchmark/util/parser.py	(original)
+++ mlpack/conf/jenkins-conf/benchmark/util/parser.py	Thu Jul 25 16:35:03 2013
@@ -36,10 +36,11 @@
   def __init__(self, config, verbose=True):
     self.verbose = verbose
     self.config = config
+    self.mc = 0
 
     # Default values.
     self.RUN = True
-    self.ITERATION = 1
+    self.ITERATION = 3
     self.OPTIONS = ''
 
     try:
@@ -47,9 +48,9 @@
       streams = yaml.load_all(open(config))
       self.streams = streams
 
-    except IOError, e:
+    except IOError as e:
       Log.Fatal("Could not load config file: " + config)
-    except yaml.YAMLError, exc:
+    except yaml.YAMLError as exc:
       if hasattr(exc, "problem_mark"):
         mark = exc.problem_mark
         Log.Fatal("Error at position: (%s:%s)" % (mark.line+1, mark.column+1))
@@ -61,13 +62,13 @@
   '''
   def GetConfigLibraryMethods(self):
     try:
-      stream = self.streams.next()
-    except StopIteration, e:
+      stream = next(self.streams)
+    except StopIteration as e:
       # We have to catch the exception to stop at the end. There exists no 
       # hasNext().
       return False
 
-    if not stream.has_key("library"):
+    if not "library" in stream:
         return self.KeyErrorMsg("library", streamNum)
     else:
       libraryName = stream["library"]
@@ -75,7 +76,7 @@
 
     attr = collections.namedtuple("attributes", ["libraryName", "methods"])
       
-    return attr(libraryName, stream["methods"].iteritems())
+    return attr(libraryName, stream["methods"].items())
 
   '''
   This method return the attributes of a given method.
@@ -84,8 +85,9 @@
   '''
   def GetConfigMethod(self, methods):
     try:
-      method = methods.next()   
-    except StopIteration, e:
+      method = list(methods)[self.mc]
+      self.mc = self.mc + 1 
+    except IndexError as e:
       # We have to catch the exception to stop at the end. There exists no 
       # hasNext().
       return False
@@ -96,37 +98,37 @@
     attributes = method[1]
 
     # First check the required attributes. 
-    if attributes.has_key("script"):
+    if "script" in attributes:
       script = attributes["script"]
       Log.Info("Script: " + script, self.verbose)
     else:
       return self.KeyErrorMsg("script")
 
-    if attributes.has_key("format"):
+    if "format" in attributes:
       format = attributes["format"]
       Log.Info("Format: " + str(format), self.verbose)
     else:
       return self.gKeyErrorMsg('format')
 
-    if attributes.has_key("datasets"):
+    if "datasets" in attributes:
       datasets = attributes['datasets']
       for dataset in datasets:
         Log.Info("Dataset: " + str(dataset["files"]), self.verbose)
-        if not dataset.has_key("options"):
+        if not "options" in dataset:
           dataset["options"] = self.OPTIONS
 
     else:
       return self.KeyErrorMsg("datasets")
 
     # Check the optional attributes. 
-    if attributes.has_key("run"):
+    if "run" in attributes:
       run = attributes["run"]
       Log.Info("Run: " + str(run), self.verbose)
     else:
       self.KeyWarnMsg("run")
       run = self.RUN
 
-    if attributes.has_key("iteration"):
+    if "iteration" in attributes:
       iteration = attributes["iteration"]
       Log.Info("Iteration: " + str(iteration), self.verbose)
     else:
@@ -229,7 +231,7 @@
 
     for datasets in files:
       # Check if the value datasets is a list of datasets.
-      if not isinstance(datasets, basestring):
+      if not isinstance(datasets, str):
         for dataset in datasets:
           if not CheckDataset(dataset):
             return False        
@@ -253,27 +255,27 @@
     for stream in self.streams:
       streamNum += 1
 
-      if not stream.has_key("library"):
+      if not "library" in stream:
         return self.KeyErrorMsg("library", streamNum)
-      elif not stream.has_key("methods"):
+      elif not "methods" in stream:
         return self.KeyErrorMsg("methods", streamNum)
       else:
         try:
-          for key, value in stream["methods"].iteritems():
+          for key, value in stream["methods"].items():
 
-            if not value.has_key("script"):
+            if not "script" in value:
               return self.KeyErrorMsg("script", streamNum)              
 
-            if not value.has_key("format"):
+            if not "format" in value:
               return self.KeyErrorMsg("format", streamNum)
 
-            if not value.has_key("run"):
+            if not "run" in value:
               self.KeyWarnMsg("run", streamNum)
 
-            if not value.has_key("iteration"):
+            if not "iteration" in value:
               self.KeyWarnMsg("iteration", streamNum)
 
-            if value.has_key("datasets"):
+            if "datasets" in value:
               if not value["datasets"]:
                 return self.EmptyErrorMsg("datasets", streamNum)
               else:
@@ -282,7 +284,7 @@
                   if not self.CheckIfAvailable(dataset["files"]):
                     return False
 
-                  if not dataset.has_key("options"):
+                  if not "options" in dataset:
                     self.KeyWarnMsg("options", streamNum)
             else:
               return self.KeyErrorMsg("datasets", streamNum)
@@ -290,7 +292,7 @@
             if not self.CheckIfCallable(key, value["script"]):
               return self.CallableMethodErroMsg(key, value["script"], streamNum)
 
-        except AttributeError, e:
+        except AttributeError as e:
           return self.KeyErrorMsg("methods", streamNum)
 
     Log.Info("Config file check: successful", self.verbose)
@@ -308,7 +310,7 @@
     libraryMapping = self.GetConfigLibraryMethods()
     while libraryMapping: 
       # Iterate through all methods.
-      methodMapping = self.GetConfigMethod(libraryMapping.methods)      
+      methodMapping = self.GetConfigMethod(libraryMapping.methods)
       while methodMapping and libraryMapping:
         # Collect data only from method with run value = true.
         if methodMapping.run:
@@ -337,6 +339,7 @@
 
         methodMapping = self.GetConfigMethod(libraryMapping.methods)
       libraryMapping = self.GetConfigLibraryMethods()
+      self.mc = 0
 
     return streamData
 

Modified: mlpack/conf/jenkins-conf/benchmark/util/system.py
==============================================================================
--- mlpack/conf/jenkins-conf/benchmark/util/system.py	(original)
+++ mlpack/conf/jenkins-conf/benchmark/util/system.py	Thu Jul 25 16:35:03 2013
@@ -1,8 +1,8 @@
 '''
-	@file system.py
-	@author Marcus Edel
+  @file system.py
+  @author Marcus Edel
 
-	Contains functions to get system informations.
+  Contains functions to get system informations.
 '''
 
 import os
@@ -12,9 +12,9 @@
 # import the util path, this method even works if the path contains
 # symlinks to modules.
 cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(
-	os.path.split(inspect.getfile(inspect.currentframe()))[0], '')))
+  os.path.split(inspect.getfile(inspect.currentframe()))[0], '')))
 if cmd_subfolder not in sys.path:
-	sys.path.insert(0, cmd_subfolder)
+  sys.path.insert(0, cmd_subfolder)
 
 from log import *
 
@@ -27,115 +27,115 @@
 
 class SystemInfo(object):
 
-	# Returns the available memory of this machine.
-	@staticmethod
-	def GetMemory():
-		if sys.platform.startswith('posix') or sys.platform.startswith('linux'):
-			cmd = 'free -m'
-			s = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
-
-			mem = s.split('\n')[1].split()[1]
-			return str(float(mem) / 1024) + ' GB'
-
-		elif sys.platform.startswith('darwin'):
-			cmd = shlex.split("sysctl -n hw.memsize")
-		 	mem = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=False).strip()
-
-		 	return str(float(mem) / 1024 / 1024 / 1000.0) + ' GB'
-
-		else:
-			return 'N/A'			
-
-	# Returns the CPU model name of this machine.
-	@staticmethod
-	def GetCPUModel():
-		if sys.platform.startswith('posix') or sys.platform.startswith('linux'):
-		 	cmd = 'cat /proc/cpuinfo'
-		 	s = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True).strip()
-		 	for line in s.split("\n"):
-		 		if "model name" in line:
-		 			modelName = re.sub( ".*model name.*:", "", line, 1)
-		 			return modelName	
-		 		elif "Processor" in line:
-		 			modelName = re.sub( ".*Processor.*:", "", line, 1)
-		 			return modelName	
-		 	return 'N/A'
-
-		elif sys.platform.startswith('darwin'):
-		 	cmd = shlex.split("sysctl -n machdep.cpu.brand_string")
-		 	modelName = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=False).strip()
-		 	return modelName
-
-		elif sys.platform.startswith('win'):
-	 		modelName = platform.processor()
-	 		return modelName
-
-	 	else:
-	 		Log.Fatal('Could not get the OS name')
-	 		return 'N/A'
-
-	# Returns the distribution name of this machine.
-	@staticmethod
-	def GetDistribution():
-		if sys.platform.startswith('posix') or sys.platform.startswith('linux'):
-		 	try:
-		 		osInfo = platform.linux_distribution()
-		 		if len(osInfo) != 0:
-		 			return osInfo[0] + ' ' + osInfo[1]
-
-		 	except:
-		 		osInfo = platform.dist()
-		 		if len(osInfo) != 0:
-		 			return osInfo[0] + ' ' + osInfo[1]
-
-		elif sys.platform.startswith('darwin'):
-		 	osInfo = platform.mac_ver()
-		 	dist = 'Mac OS X'
-
-		 	if osInfo[0].startswith('10.6'):
-		 		dist += ' ' + osInfo[0] + ' (Snow Leopard)'
-		 	elif osInfo[0].startswith('10.7'):
-		 		dist += ' ' + osInfo[0] + ' (Lion)'
-		 	elif osInfo[0].startswith('10.8'):
-		 		dist += ' ' + osInfo[0] + ' (Mountain Lion)'
-		 	elif osInfo[0].startswith('10.9'):
-		 		dist += ' ' + osInfo[0] + ' (Mavericks)'
-
-		 	return dist
-
-		elif sys.platform.startswith('win'):
-			pass
-		else:
-			Log.Fatal('Could not get the OS name')
-			return 'N/A'
-
-	# Returns the CPU core count of this machine.
-	@staticmethod
-	def GetCPUCores():
-		# Python 2.6+ use multiprocessing module.
-	  try:
-	  	import multiprocessing
-	  	return str(multiprocessing.cpu_count())
-	  except (ImportError, NotImplementedError):
-	  	pass
-
-	  # Python < 2.6.
-		if sys.platform.startswith('posix') or sys.platform.startswith('linux') or sys.platform.startswith('darwin'):
-			res = int(os.sysconf('SC_NPROCESSORS_ONLN'))
-
-			if res > 0:
-				return str(res)
-
-		elif sys.platform.startswith('win'):
-			res = int(os.environ['NUMBER_OF_PROCESSORS'])
-
-			if res > 0:
-				return res
-		else:
-			Log.Fatal('Could not specify the OS name')
-			return 'N/A'
-			
-	# Returns the plattform of this machine (e.g. x86_64).
-	@staticmethod
-	def GetPlatform():
-	 	return platform.machine()
\ No newline at end of file
+  # Returns the available memory of this machine.
+  @staticmethod
+  def GetMemory():
+    if sys.platform.startswith('posix') or sys.platform.startswith('linux'):
+      cmd = 'free -m'
+      s = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
+
+      mem = s.decode().split('\n')[1].split()[1]
+      return str(float(mem) / 1024) + ' GB'
+
+    elif sys.platform.startswith('darwin'):
+      cmd = shlex.split("sysctl -n hw.memsize")
+      mem = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=False).strip()
+
+      return str(float(mem) / 1024 / 1024 / 1000.0) + ' GB'
+
+    else:
+      return 'N/A'      
+
+  # Returns the CPU model name of this machine.
+  @staticmethod
+  def GetCPUModel():
+    if sys.platform.startswith('posix') or sys.platform.startswith('linux'):
+      cmd = 'cat /proc/cpuinfo'
+      s = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True).strip()
+      for line in s.decode().split("\n"):
+        if "model name" in line:
+          modelName = re.sub( ".*model name.*:", "", line, 1)
+          return modelName  
+        elif "Processor" in line:
+          modelName = re.sub( ".*Processor.*:", "", line, 1)
+          return modelName  
+      return 'N/A'
+
+    elif sys.platform.startswith('darwin'):
+      cmd = shlex.split("sysctl -n machdep.cpu.brand_string")
+      modelName = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=False).strip()
+      return modelName
+
+    elif sys.platform.startswith('win'):
+      modelName = platform.processor()
+      return modelName
+
+    else:
+      Log.Fatal('Could not get the OS name')
+      return 'N/A'
+
+  # Returns the distribution name of this machine.
+  @staticmethod
+  def GetDistribution():
+    if sys.platform.startswith('posix') or sys.platform.startswith('linux'):
+      try:
+        osInfo = platform.linux_distribution()
+        if len(osInfo) != 0:
+          return osInfo[0] + ' ' + osInfo[1]
+
+      except:
+        osInfo = platform.dist()
+        if len(osInfo) != 0:
+          return osInfo[0] + ' ' + osInfo[1]
+
+    elif sys.platform.startswith('darwin'):
+      osInfo = platform.mac_ver()
+      dist = 'Mac OS X'
+
+      if osInfo[0].startswith('10.6'):
+        dist += ' ' + osInfo[0] + ' (Snow Leopard)'
+      elif osInfo[0].startswith('10.7'):
+        dist += ' ' + osInfo[0] + ' (Lion)'
+      elif osInfo[0].startswith('10.8'):
+        dist += ' ' + osInfo[0] + ' (Mountain Lion)'
+      elif osInfo[0].startswith('10.9'):
+        dist += ' ' + osInfo[0] + ' (Mavericks)'
+
+      return dist
+
+    elif sys.platform.startswith('win'):
+      pass
+    else:
+      Log.Fatal('Could not get the OS name')
+      return 'N/A'
+
+  # Returns the CPU core count of this machine.
+  @staticmethod
+  def GetCPUCores():
+    # Python 2.6+ use multiprocessing module.
+    try:
+      import multiprocessing
+      return str(multiprocessing.cpu_count())
+    except (ImportError, NotImplementedError):
+      pass
+
+    # Python < 2.6.
+    if sys.platform.startswith('posix') or sys.platform.startswith('linux') or sys.platform.startswith('darwin'):
+      res = int(os.sysconf('SC_NPROCESSORS_ONLN'))
+
+      if res > 0:
+        return str(res)
+
+    elif sys.platform.startswith('win'):
+      res = int(os.environ['NUMBER_OF_PROCESSORS'])
+
+      if res > 0:
+        return res
+    else:
+      Log.Fatal('Could not specify the OS name')
+      return 'N/A'
+      
+  # Returns the plattform of this machine (e.g. x86_64).
+  @staticmethod
+  def GetPlatform():
+    return platform.machine()



More information about the mlpack-svn mailing list