Example #1
0
 def dot(self, other):
     """Function to compute dot product between two vectors"""
     # Checking whether the input is a vector or not
     if not isinstance(other, VectorOC):
         raise TypeError("ERROR! Provided input vector not a %s!" %
                         self.whoami)
     # Checking size (must have same number of elements)
     if self.size != other.size:
         raise ValueError(
             "ERROR! Vector size mismatching: self = %s; other = %s" %
             (self.size, other.size))
     # Checking dimensionality
     if not self.checkSame(other):
         raise ValueError(
             "ERROR! Vector dimensionality mismatching: self = %s; other = %s"
             % (self.shape, other.shape))
     # Running Solver_ops to compute norm value
     cmd = "Solver_ops file1=%s file2=%s op=dot" % (self.vecfile,
                                                    other.vecfile)
     find = re_dpr.search(RunShellCmd(cmd, get_stat=False)[0])
     if find:
         return float(find.group(1))
     else:
         raise ValueError("ERROR! Trouble parsing dot product!")
     return float(out_dot)
Example #2
0
 def scale(self, sc):
     """Function to scale a vector"""
     RunShellCmd("Solver_ops file1=%s scale1_r=%s op=scale" %
                 (self.vecfile, sc),
                 get_stat=False,
                 get_output=False)
     return
Example #3
0
 def zero(self):
     RunShellCmd("head -c %s </dev/zero > %s" %
                 (self.size * 4, self.binfile),
                 get_stat=False,
                 get_output=False)
     # sys_util.RunShellCmd("Solver_ops file1=%s op=zero"%(self.vecfile),get_stat=False,get_output=False)
     return
Example #4
0
 def clone(self):
     # First performing a deep copy of the vector
     vec_clone = deepcopy(self)
     if vec_clone.vecfile is None:
         # Creating header and binary files from vector space
         # Placing temporary file into datapath folder
         tmp_vec = sep.datapath + "clone_tmp_vector" + str(
             int(time() * 1000000)) + ".H"
         axis_file = ""
         for iaxis, naxis in enumerate(tuple(reversed(vec_clone.shape))):
             axis_file += "n%s=%s " % (iaxis + 1, naxis)
         # Creating temporary vector file
         cmd = "Spike %s | Add scale=0.0 > %s" % (axis_file, tmp_vec)
         RunShellCmd(cmd, get_stat=False, get_output=False)
         vec_clone.vecfile = tmp_vec
         vec_clone.binfile = sep.get_binary(vec_clone.vecfile)
     else:
         # Creating a temporary file with similar name but computer time at the end
         tmp_vec = self.vecfile.split(".H")[0].split("/")[
             -1]  # Getting filename only
         # Placing temporary file into datapath folder
         tmp_vec = sep.datapath + tmp_vec + "_clone_" + str(
             int(time() * 1000000)) + ".H"
         tmp_bin = tmp_vec + "@"
         # Copying header and binary files and setting pointers to new file
         copyfile(self.vecfile, tmp_vec)  # Copying header
         copyfile(self.binfile, tmp_bin)  # Copying binary
         vec_clone.vecfile = tmp_vec
         vec_clone.binfile = tmp_bin
         # "Fixing" header file
         with open(vec_clone.vecfile, "a") as fid:
             fid.write("in='%s\n'" % tmp_bin)
     # By default the clone file is going to be removed once the vector is deleted
     vec_clone.remove_file = True
     return vec_clone
Example #5
0
 def rand(self, snr=1.0):
     """Fill vector with random number (~U[1,-1]) with a given SNR"""
     # Computing RMS amplitude of the vector
     rms = RunShellCmd("Attr < %s want=rms param=1 maxsize=5000" %
                       (self.vecfile),
                       get_stat=False)[0]
     rms = float(rms.split("=")[1])  # Standard deviation of the signal
     amp_noise = 1.0
     if rms != 0.:
         amp_noise = np.sqrt(3.0 / snr) * rms  # sqrt(3*Power_signal/SNR)
     # Filling file with random number with the proper scale
     RunShellCmd(
         "Noise file=%s rep=1 type=0 var=0.3333333333; Solver_ops file1=%s scale1_r=%s op=scale"
         % (self.vecfile, self.vecfile, amp_noise),
         get_stat=False,
         get_output=False)
     return
Example #6
0
 def randn(self, mean: float = 0., std: float = 1.):
     # Computing RMS amplitude of the vector
     RunShellCmd(
         "Noise file=%s rep=1 type=0 var=0.3333333333; Solver_ops file1=%s scale1_r=%s op=scale"
         % (self.vecfile, self.vecfile, std),
         get_stat=False,
         get_output=False)
     return
Example #7
0
 def norm(self, N=2):
     if N != 2:
         raise NotImplementedError(
             "Norm different than L2 not currently supported")
     # Running Solver_ops to compute norm value
     find = re_dpr.search(
         RunShellCmd("Solver_ops file1=%s op=dot" % self.vecfile,
                     get_stat=False)[0])
     if find:
         return np.sqrt(float(find.group(1)))
     else:
         raise ValueError("ERROR! Trouble parsing dot product!")
     return
Example #8
0
 def scaleAdd(self, other, sc1=1.0, sc2=1.0):
     # Checking whether the input is a vector or not
     if not isinstance(other, VectorOC):
         raise TypeError("ERROR! Provided input vector not a vectorOC!")
     # Checking dimensionality
     if not self.checkSame(other):
         raise ValueError(
             "ERROR! Vector dimensionality mismatching: self = %s; other = %s"
             % (self.shape, other.shape))
     # Performing scaling and addition
     cmd = "Solver_ops file1=%s scale1_r=%s file2=%s scale2_r=%s op=scale_addscale" % (
         self.vecfile, sc1, other.vecfile, sc2)
     RunShellCmd(cmd, get_stat=False, get_output=False)
     return
Example #9
0
 def multiply(self, other):
     # Checking whether the input is a vector or not
     if not isinstance(other, VectorOC):
         raise TypeError("ERROR! Provided input vector not a %s!" %
                         self.whoami)
     # Checking size (must have same number of elements)
     if self.size != other.size:
         raise ValueError(
             "ERROR! Vector size mismatching: self = %s; other = %s" %
             (self.size, other.size))
     # Checking dimensionality
     if not self.checkSame(other):
         raise ValueError(
             "ERROR! Vector dimensionality mismatching: self = %s; other = %s"
             % (self.shape, other.shape))
     # Performing scaling and addition
     cmd = "Solver_ops file1=%s file2=%s op=multiply" % (self.vecfile,
                                                         other.vecfile)
     RunShellCmd(cmd, get_stat=False, get_output=False)
     return
Example #10
0
import re

import numpy as np

from occamypy.utils.os import RunShellCmd
from occamypy.vector.axis_info import AxInfo

# Assigning datapath
HOME = os.environ["HOME"]
datapath = None
# Checking environment definition first
if "DATAPATH" in os.environ:
    datapath = os.environ["DATAPATH"]
# Checking local directory
elif os.path.isfile('.datapath'):
    out = (RunShellCmd("cat .datapath | head -N 1", check_code=False, get_stat=False)[0]).rstrip()
    datapath = out.split("=")[1]
# Checking whether the local host has a datapath
else:
    if os.path.isfile(HOME + "/.datapath"):
        out = RunShellCmd("cat $HOME/.datapath | grep $HOST", check_code=False, get_stat=False)[0]
        if len(out) == 0:
            out = (RunShellCmd("cat $HOME/.datapath | head -N 1", check_code=False, get_stat=False)[0]).rstrip()
        datapath = out.split("=")[1]

# Checking if datapath was found
_already_warned = False
if datapath is None:
    if os.path.isdir("/tmp/"):
        datapath = "/tmp/"
        if not _already_warned:
Example #11
0
 def scale(self, sc):
     RunShellCmd("Solver_ops file1=%s scale1_r=%s op=scale" %
                 (self.vecfile, sc),
                 get_stat=False,
                 get_output=False)
     return