utils - Generic helper utilities¶
This module contains generic helper functions, not related to imaging specifically
-
class
neural.utils.Beacon(app_name=None, instance_name=None, packet_path=None, poll_time=0.5)[source]¶ Class to easily handle running multiple threads simultaneously. Communicates through a lockfile in an arbitrary file path, so communicating across different computers that have shared file systems is relatively easy (just choose a file path on the shared drive).
Options:
app_name: Arbitrary name of the script. Will use script filename if Noneinstance_name: Arbitrary name of this instance (e.g., subject #) packet_path: Path to put the lock file in (defaults to the system temp directory) poll_time: How often (in seconds) to ping the lock file Example of usage:
b = Beacon('my_analysis','subject_4') if not b.exists(): # there are no "my_analysis" scripts running "subject_4" right now with b.thread_safe(): # lock this, so other scripts will fail when they run b.exists() on this subject # do the analysis here...
-
run()[source]¶ Method representing the thread’s activity.
You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.
-
-
class
neural.utils.RunResult(output=None, return_code=None, output_filename=None)[source]¶ result of calling
run()when used as a string, will try to reasonably return the filename of the primary output of the command (if known)
-
class
neural.utils.ThreadSafe(beacon)[source]¶ wrapper class to handle starting and stopping for the
thread_safe()method ofBeacon
-
neural.utils.archive_basename(filename)[source]¶ returns the basename (name without extension) of a recognized archive file
-
neural.utils.is_archive(filename)[source]¶ returns boolean of whether this filename looks like an archive
-
neural.utils.numberize(string)[source]¶ Turns a string into a number (
intorfloat) if it’s only a number (ignoring spaces), otherwise returns the string. For example,"5 "becomes5and"2 ton"remains"2 ton"
-
neural.utils.run(command, products=None, working_directory='.', force_local=False, stderr=True, quiet=False)[source]¶ wrapper to run external programs
Command: list containing command and parameters (formatted the same as subprocess; must contain only strings) Products: string or list of files that are the products of this command if all products exist, the command will not be run, and False returned Working_directory: will chdir to this directory Force_local: when used with neural.scheduler, setting to Truewill disable all job distribution functionsStderr: forward stderrinto the outputTruewill combinestderrandstdoutFalsewill returnstdoutand letstderrprint to the consoleNonewill returnstdoutand suppressstderrQuiet: False(default) will print friendly messagesTruewill suppress everything but errorsNonewill suppress all outputReturns result in form of
RunResult
-
class
neural.utils.run_in(working_directory, create=False)[source]¶ temporarily changes into another directory
If the directory name you pass doesn’t exist, but matches the current directory you are in, it will silently ignore your silliness. Otherwise, it will raise an
OSError. If the argumentcreateis True, it will create the directory instead of raising an error.- Example::
- with run_in(‘another_directory’):
- do_some_stuff_there()
-
class
neural.utils.run_in_tmp(inputs=[], products=[])[source]¶ creates a temporary directory to run the code block in
-
class
neural.utils.simple_timer[source]¶ a simple way to time a single run of a function
- Example::
- with simple_timer():
- do_stuff()
-
neural.utils.strip_rows(array, invalid=None)[source]¶ takes a
listoflist``s and removes corresponding indices containing the invalid value (default ``None).
-
neural.utils.universal_read(fname)[source]¶ Will open and read a file with universal line endings, trying to decode whatever format it’s in (e.g., utf8 or utf16)
-
neural.utils.which(program)[source]¶ returns full path to program name or
Noneif not found taken from: http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python