Example #1
0
    def frames(self):
        print("TRIAL NUMBER " + str(self.trial))
        fieldsize_x = self.size_x * self.density
        fieldsize_y = self.size_y * self.density
        libpath = (
            visual_stimulus.__file__.replace("/visual_stimulus.pyc", "") +
            "/textureLib")  # path to the image processing library
        octave.addpath(libpath)
        im = octave.textureBasedStimulus(self.texture_path, self.stats_type,
                                         self.seed, fieldsize_x, fieldsize_y,
                                         libpath)

        im = im / 255 * 2 * self.background_luminance
        # scipy.misc.toimage(im, cmin=0.0, cmax=2*self.background_luminance).save('/home/kaktus/Documents/mozaik/examples/img' + str(self.trial) + '.jpg')
        scipy.misc.toimage(im, cmin=0.0,
                           cmax=2 * self.background_luminance).save(
                               "/home/kaktus/Documents/mozaik/examples/img" +
                               str(len(self.texture_path)) + "type" +
                               str(self.stats_type) + ".jpg")

        assert im.shape == (
            fieldsize_x, fieldsize_y
        ), "Image dimensions do not correspond to visual field size"
        while True:
            yield (im, [0])
Example #2
0
    def setUp(self):
        octave.restart()
        octave.addpath('./octave')
        octave.addpath('./test/octave')
        octave.eval('pkg load signal')  # load signal package

        # Test data
        v = 1/np.sqrt(2)

        self.in1 = np.array([1, 0, 0, 0, 0, 0, 0, 0], dtype=np.complex64)
        self.out1 = np.array([1, 1, 1, 1, 1, 1, 1, 1], dtype=np.complex64)

        self.in2 = np.array([0, 1, 0, 0, 0, 0, 0, 0], dtype=np.complex64)
        self.out2 = np.array([1, v-1j*v, -1j, -v-1j*v, -1, -v+1j*v, 1j, v+1j*v], dtype=np.complex64)

        self.in3 = np.array([0, 0, 1, 0, 0, 0, 0, 0], dtype=np.complex64)
        self.out3 = np.array([1, -1j, -1, 1j, 1, -1j, -1, 1j], dtype=np.complex64)

        self.in4 = np.array([0, 0, 0, 1, 0, 0, 0, 0], dtype=np.complex64)
        self.out4 = np.array([1, -v-1j*v, 1j, v-1j*v, -1, v+1j*v, -1j, -v+1j*v], dtype=np.complex64)

        self.in5 = np.array([0, 0, 0, 0, 1, 0, 0, 0], dtype=np.complex64)
        self.out5 = np.array([1, -1, 1, -1, 1, -1, 1, -1], dtype=np.complex64)

        self.in6 = np.array([0, 0, 0, 0, 0, 1, 0, 0], dtype=np.complex64)
        self.out6 = np.array([1, -v+1j*v, -1j, v+1j*v, -1, v-1j*v, 1j, -v-1j*v], dtype=np.complex64)

        self.in7 = np.array([0, 0, 0, 0, 0, 0, 1, 0], dtype=np.complex64)
        self.out7 = np.array([1, 1j, -1, -1j, 1, 1j, -1, -1j], dtype=np.complex64)

        self.in8 = np.array([0, 0, 0, 0, 0, 0, 0, 1], dtype=np.complex64)
        self.out8 = np.array([1, v+1j*v, 1j, -v+1j*v, -1, -v-1j*v, -1j, v-1j*v], dtype=np.complex64)
def run_method(data, method, default_method_configs):
    octave.restart()

    if method == "SPEC":
        return run_SPEC(data, default_method_configs)
    elif method == "MCFS":
        return run_MCFS(data)
    elif method == "LS":
        return run_lap_score(data, default_method_configs)
    elif method == "NDFS":
        return run_NDFS(data)
    elif method == "UDFS":
        return run_UDFS(data)
    elif method == "iDetect":
        # octave.addpath("algorithms/matlab")
        return run_iDetect(data, default_method_configs)
    elif method == "GLSPFS":
        # octave.addpath("algorithms/matlab/GLSPFS")
        return run_GLSPFS(data, default_method_configs)
    elif method == "FSASL":
        octave.addpath("algorithms/matlab/FSASL")
        return run_FSASL(data)
    elif method == "JELSR":
        octave.addpath("algorithms/matlab/JELSR")
        return run_JELSR(data)
Example #4
0
def read_activity_file(model_activity_fpath, openpv_path = '/home/mteti/OpenPV/mlab/util'):
    '''
    Read in the <model_layer_name>_A.pvp file containing the activations / feature maps of each neuron.
    
    Args:
        model_activity_fpath (str): Path to the <model_layer_name>_A.pvp where the
            activations of each neuron are contained in.
        openpv_path (str): Path to the openpv matlab utility directory (*/OpenPV/mlab/util).
        
    Returns:
        acts (np.ndarray): A B x H x W x # Neurons array of feature maps. 
    '''
    
    # add OpenPV matlab utility dir to octave path
    octave.addpath(openpv_path)
    
    # Read in the activity file and get the number of batch samples
    # Should be a list of length batch size, where each item in the list is an array 
    # of shape (input_width / stride x) x (input height / stride y) x # Neurons
    # with the activations for that particular batch sample
    act_data = octave.readpvpfile(model_activity_fpath)
    n_batch = len(act_data)
    
    # concatenate across batch samples to produce a single array of shape 
    # batch size x (input width / stride x) x (input height / stride y) x # Neurons
    acts = np.concatenate([act_data[b]['values'][None, ...] for b in range(n_batch)], 0)
    
    # transpose the 2nd and third dimensions to make it B x H x W x # Neurons
    acts = acts.transpose([0, 2, 1, 3])
    
    return acts
 def post(self):
     array = self.request.body[self.request.body.index('[') + 1: self.request.body.index(']')].split(',')
     octave.addpath(os.path.abspath('.') + '/octave')
     octave.savepath()
     oc = oct2py.Oct2Py()
     oc.predictDigit(array)
     self.write('pred = ' + str(int(oc.predictDigit(array))))
Example #6
0
def run_octave_script2(pk):
	out = None
	obj = ScriptCode.objects.get(pk=pk)
	user_folder = obj.get_user_folder()
	
	from oct2py import octave as oc
	import oct2py
	oc.addpath(user_folder)

	lst = tuple([2, 3])
	try:
		out = oc.feval(obj.get_file_name(), *lst, plot_dir=user_folder + '/images/', timeout=3)
	except oct2py.Oct2PyError:
		print('Octave Error happened')
		self.update_state(state='FAILED',
			meta={'message': 'Octave Error Happened'})
		oc.exit()
		oct2py.kill_octave()
		return None
	except TypeError:
		self.update_state(state='FAILED',
			meta={'message': 'type Error Happened'})
		oc.exit()
		oct2py.kill_octave()
		return None

	oc.exit()
	oct2py.kill_octave()

	result = {}
	result['somethings'] = 0
	result['out'] = out
	return result
Example #7
0
def read_simple_cell_weight_files(fpaths, n_features_x, n_features_y, 
                                  openpv_path = '/home/mteti/OpenPV/mlab/util'):
    '''
    Reads in .pvp shared weight files, where the features are the same size as the inputs,
    and stride_x and stride_y equal the input width and height, respectively. 

    Args:
        fpaths (list): List of file paths corresponding to the .pvp weight files to read.
        n_features_x (int): Number of simple cell features to display along grid width.
        n_features_y (int): Number of simple cell features to display down grid height. 
        openpv_path (str): Path to */OpenPV/mlab/util. 

    Returns:
        weights_agg (list): List of len(fpaths) arrays of shape n_neurons x in_c x 
            n_features_y x n_features_x x kh x kw. 
    '''

    octave.addpath(openpv_path)

    weights_agg = []
    for fpath in fpaths:
        weights = octave.readpvpfile(fpath)[0]['values'][0]
        w_x, w_y, w_in, w_out = weights.shape
        n_neurons = w_out // n_features_x // n_features_y
        
        # reshape to the original shape of the unshared weight tensor 
        # and reverse the x and y dims for image writing
        weights = weights.reshape([w_x, w_y, w_in, n_neurons, n_features_x, n_features_y])
        weights = weights.transpose([3, 2, 5, 4, 1, 0])
        weights_agg.append(weights)

    return weights_agg
Example #8
0
    def __init__(
        self,
        rank=40,
        kappa=0.99,
        centering=1,
        delta=40,
        eps=1e-20,
    ):
        """
        :param rank: (``int``) Maximal decomposition rank.

        :param kappa: (``float``) Trade-off accuracy vs. predictive gain.

        :param centering: (``int``)  0 or 1, Centering of the kernel matrix.

        :param delta: (``int``) Number of look-ahead columns.

        :param eps: (``float``)  Tolerance lower bound.

        """
        self.rank = rank
        self.delta = delta
        self.kappa = kappa
        self.centering = centering
        self.tol = eps
        self.I = list()
        self.active_set_ = list()
        self.trained = False
        try:
            octave.addpath(join(dirname(realpath(__file__)), 'csi'))
        except NameError:
            raise NameError("Install module 'oct2py' to use the CSI method.")
def resample_folder(input_folder, output_folder, fs, regex):
    filedir = os.path.dirname(os.path.realpath(__file__))
    octave.addpath(filedir)
    # add the matlab functions to octave dir here

    files = glob.glob(os.path.join(input_folder, regex), recursive=True)
    for f in tqdm.tqdm(files):

        audio, fs_read = sf.read(f)
        audio = signal.resample(audio, int((fs_read / fs) * len(audio)))

        tmp = octave.activlev(audio.tolist(), fs_read, "n")
        audio, _ = tmp[:-1].squeeze(), tmp[-1]

        os.makedirs(
            Path(
                os.path.join(output_folder,
                             Path(f).relative_to(Path(input_folder)))).parent,
            exist_ok=True,
        )

        sf.write(
            os.path.join(output_folder,
                         Path(f).relative_to(Path(input_folder))),
            audio,
            format="WAV",
            subtype="FLOAT",
            samplerate=fs,
        )
Example #10
0
    def frames(self):
        fieldsize_x = self.size_x * self.density
        fieldsize_y = self.size_y * self.density
        libpath = visual_stimulus.__file__.replace(
            "/visual_stimulus.pyc",
            "") + "/textureLib"  #path to the image processing library
        matlabPyrToolspath = os.path.join(libpath, "textureSynth",
                                          "matlabPyrTools")
        if not os.path.isdir(matlabPyrToolspath):
            raise IOError(
                "matlabPyrTools should be downloaded from https://github.com/LabForComputationalVision/matlabPyrTools and its content should be put in the directory "
                + matlabPyrToolspath)

        octave.addpath(libpath)
        im = octave.textureBasedStimulus(self.texture_path, self.stats_type,
                                         self.seed, fieldsize_x, fieldsize_y,
                                         libpath)

        im = im / 255 * 2 * self.background_luminance
        #scipy.misc.toimage(im, cmin=0.0, cmax=2*self.background_luminance).save('/home/kaktus/Documents/mozaik/examples/img' + str(self.trial) + '.jpg')
        scipy.misc.toimage(
            im, cmin=0.0, cmax=2 *
            self.background_luminance).save('img' +
                                            str(len(self.texture_path)) +
                                            "type" + str(self.stats_type) +
                                            '.jpg')
        assert (im.shape == (fieldsize_x, fieldsize_y)
                ), "Image dimensions do not correspond to visual field size"
        while True:
            yield (im, [0])
Example #11
0
def create_data():
	octave.addpath('./matlab/')
	octave.eval("whiteNorm = powernoise(0,4096,'normalize')")
	octave.eval("whiteRand = powernoise(0,4096,'randpower')")
	octave.eval("pinkNorm = powernoise(1,4096,'normalize')")
	octave.eval("pinkRand = powernoise(1,4096,'randpower')")
	octave.eval("redNorm = powernoise(2,4096,'normalize')")
	octave.eval("redRand = powernoise(2,4096,'randpower')")

	octave.eval("s8 = pmodel(4096,0.52,-1.66)")
	octave.eval("s9 = pmodel(4096,0.62,-0.45)")
	octave.eval("s10 = pmodel(4096,0.72,-0.75)")

	# Salvando dados
	octave.eval("dlmwrite ('./data/s1_white_noise.csv',whiteNorm)")
	octave.eval("dlmwrite ('./data/s2_pink_noise.csv',pinkNorm)")
	octave.eval("dlmwrite ('./data/s3_red_noise.csv',redNorm)")

	octave.eval("dlmwrite ('./data/s8.csv',s8)")
	octave.eval("dlmwrite ('./data/s9.csv',s9)")
	octave.eval("dlmwrite ('./data/s10.csv',s10)")
	

	#octave.eval("dlmwrite ('./data/whiteRand.csv',whiteRand)")
	#octave.eval("dlmwrite ('./data/pinkRand.csv',pinkRand)")
	#octave.eval("dlmwrite ('./data/redRand.csv',redRand)")
Example #12
0
    def frames(self):
        fieldsize_x = self.size_x * self.density
        fieldsize_y = self.size_y * self.density
        folder_name = Global.root_directory + "/TextureImagesStimuli"
        #libpath = visual_stimulus.__file__.replace("/visual_stimulus.pyc", "") + "/textureLib" #path to the image processing library
        libpath = __file__.replace(
            "/texture_based.py",
            "") + "/textureLib"  #path to the image processing library
        matlabPyrToolspath = os.path.join(libpath, "textureSynth",
                                          "matlabPyrTools")
        if not os.path.isdir(matlabPyrToolspath):
            raise IOError(
                "matlabPyrTools should be downloaded from https://github.com/LabForComputationalVision/matlabPyrTools and its content should be put in the directory "
                + matlabPyrToolspath)

        octave.addpath(libpath)
        im = octave.textureBasedStimulus(self.texture_path, self.stats_type,
                                         self.seed, fieldsize_x, fieldsize_y,
                                         libpath)
        scale = 2. * self.background_luminance / (numpy.max(im) -
                                                  numpy.min(im))
        im = (im - numpy.min(im)) * scale
        im = im.astype(numpy.uint8)

        if not os.path.exists(folder_name):
            os.mkdir(folder_name)

        IM = Image.fromarray(im)
        IM.save(folder_name + "/" + self.texture + "sample" +
                str(self.sample) + "type" + str(self.stats_type) + '.pgm')
        assert (im.shape == (fieldsize_x, fieldsize_y)
                ), "Image dimensions do not correspond to visual field size"

        while True:
            yield (im, [0])
Example #13
0
def bisect3(p, t, marked):
    """
    Wrapper to iFEM.
    """
    import copy
    octave.addpath('pyadaptive')
    P, T = octave.bisect3(p.T, t.T+1, marked+1, nout=2)
    return P.T, T.T.astype(np.int64)-1
Example #14
0
def set_memls_path(path):
    """set the path where MEMLS archive has been uncompressed, i.e. where the file `memlsmain.m` is located."""
    global _memls_path

    if path != _memls_path:
        #octave.restoredefaultpath() # risk of bad interference with DMRT_QMS
        octave.addpath(path)
        octave.addpath(os.path.dirname(__file__))
        _memls_path = path
Example #15
0
    def setUp(self):
        octave.restart()
        octave.addpath('./octave')
        octave.addpath('./test/octave')
        octave.eval('pkg load signal')  # load signal package

        # Test data
        self.in1 = np.ones(8, dtype=np.complex64)
        self.out1 = np.copy(self.in1)
Example #16
0
def espgame_dump_labels(filename,
                        outfile,
                        attrb='labels',
                        dirname='data/ESP-Game/'):
    # Add ESP-game dataset to octave path
    octave.addpath(dirname)
    # Use vec_read octave-function to load data
    info = octave.vec_read(filename)
    # Dump data into a standard format
    utils.h5py_save(outfile, **{attrb: info.T})
Example #17
0
    def setUp(self):
        octave.restart()
        octave.addpath('./octave')
        octave.addpath('./test/octave')
        octave.eval('pkg load signal')  # load signal package

        # Test data
        N = 64
        self.in1 = _create_sine(N)
        self.out1 = np.squeeze(octave.fft(self.in1, N))
Example #18
0
def setup_octave():
    """Setup `oct2py` library to execute wltp's MATLAB sources. """
    from oct2py import octave as oc

    # See https://nbviewer.jupyter.org/github/blink1073/oct2py/blob/master/example/octavemagic_extension.ipynb?create=1
    # %load_ext oct2py.ipython

    wltp_mat_sources_path = str(Path("src").absolute())
    mat_path = set(oc.path().split(osp.pathsep))
    if wltp_mat_sources_path not in mat_path:
        oc.addpath(wltp_mat_sources_path)
Example #19
0
def get_predictions_matlab(script_location, X, Theta1, Theta2):

    import os
    from oct2py import octave

    cwd = os.getcwd()

    octave.addpath(cwd + script_location)
    predict_descrete, predict_continu = octave.predict(Theta1, Theta2, X)

    return (predict_descrete, predict_continu)
Example #20
0
def feature_vect_from_text_matlab(script_location):

    import os
    from oct2py import octave

    cwd = os.getcwd()

    octave.addpath(cwd + script_location)
    features = octave.data_convert()

    return features
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--matlab_path', type=str, required=True)
    parser.add_argument('--out_path', type=str, required=True)
    parser.add_argument('--matrices_path', type=str, required=True)
    parser.add_argument('--config_file', type=str, required=True)

    args = parser.parse_args()
    matlab_path = args.matlab_path
    out_path = args.out_path
    matrices_path = args.matrices_path
    config_file = args.config_file

    octave.addpath(matlab_path)

    with open(config_file, 'r') as msm_f:
        msm_configs = eval(msm_f.read())

    # run linear and logarithmic variants
    # matlab function format: [m] = msm(run, p, q, r, s)
    for cfg in msm_configs.items():
        cfg_id = cfg[0]
        p, q, r, s = cfg[1]

        out_file = os.path.join(out_path, cfg_id + '.eval')
        with io.open(out_file, 'w', encoding='utf8') as out_f:
            out_f.write('dataset\tyear\trun\ttopic\t' + cfg_id + '_lin'
                        '\t' + cfg_id + '_log\n'  #.decode('utf-8')
                        )

            topic_runs = [
                f for f in os.listdir(matrices_path) if f.endswith('m')
            ]
            for topic_run in topic_runs:
                tr = topic_run.split('__')
                track = tr[0]
                year = tr[1]
                run = tr[2]
                rel = tr[3]
                topic = tr[4][:-2]

                if rel == 'max': continue

                print(topic_run)
                f = open(matrices_path + topic_run, 'rb')
                topic_matrix = pickle.load(f)
                f.close()

                # score
                m_lin = octave.msm_lin(topic_matrix, p, q, r, s)
                m_log = octave.msm_log(topic_matrix, p, q, r, s)

                write_measurements(out_f, track, year, run, topic, m_lin,
                                   m_log)
Example #22
0
    def setUp(self):
        octave.restart()
        octave.addpath('./octave')
        octave.addpath('./test/octave')
        octave.eval('pkg load signal')  # load signal package

        # Test data
        self.in1 = np.array([1, 0], dtype=np.complex64)
        self.out1 = np.array([1, 1], dtype=np.complex64)

        self.in2 = np.array([0, 1], dtype=np.complex64)
        self.out2 = np.array([1, -1], dtype=np.complex64)
Example #23
0
def train_NN_matlab(script_location, X_train, y_train_i, y_train_sm):

    import os
    from oct2py import octave

    cwd = os.getcwd()

    octave.addpath(cwd + script_location)
    theta1_i, theta2_i, theta1_sm, theta2_sm = octave.main_NN(
        X_train, y_train_i, y_train_sm)

    return (theta1_i, theta2_i, theta1_sm, theta2_sm)
Example #24
0
    def frames(self):
        fieldsize_x = self.size_x * self.density
        fieldsize_y = self.size_y * self.density
        folder_name = Global.root_directory + "/TextureImagesStimuli"
        libpath = __file__.replace(
            "/texture_based.py",
            "") + "/textureLib"  #path to the image processing library
        matlabPyrToolspath = os.path.join(libpath, "textureSynth",
                                          "matlabPyrTools")
        if not os.path.isdir(matlabPyrToolspath):
            raise IOError(
                "matlabPyrTools should be downloaded from https://github.com/LabForComputationalVision/matlabPyrTools and its content should be put in the directory "
                + matlabPyrToolspath)

        octave.addpath(libpath)
        im = octave.textureBasedStimulus(self.texture_path, self.stats_type,
                                         self.seed, fieldsize_x, fieldsize_y,
                                         libpath)
        scale = 2. * self.background_luminance / (numpy.max(im) -
                                                  numpy.min(im))
        im = (im - numpy.min(im)) * scale

        if not os.path.exists(folder_name):
            os.mkdir(folder_name)

        assert (im.shape == (fieldsize_x, fieldsize_y)
                ), "Image dimensions do not correspond to visual field size"

        b = imagen.Constant(scale=self.background_luminance,
                            bounds=BoundingBox(radius=self.size_x / 2),
                            xdensity=self.density,
                            ydensity=self.density)()
        c = imagen.Disk(smoothing=0.0,
                        size=self.radius * 2,
                        scale=1.0,
                        bounds=BoundingBox(radius=self.size_x / 2),
                        xdensity=self.density,
                        ydensity=self.density)()

        d1 = numpy.multiply(im, c)
        d2 = numpy.multiply(b, -(c - 1.0))
        d = numpy.add.reduce([d1, d2])

        d = d.astype(numpy.uint8)
        IM = Image.fromarray(d)
        IM.save(folder_name + "/" + self.texture + "sample" +
                str(self.sample) + "type" + str(self.stats_type) + 'radius' +
                str(self.radius) + '.pgm')

        while True:
            yield (d, [0])
Example #25
0
def get_densities(idx_list, victims, attackers):
    from oct2py import octave
    from scipy import sparse
    from itertools import product
    # TODO: add the right to the folder CA_python, e.g.
    octave.addpath('/home/andrew/collsec/soldo/CA_python/')

    import time
    import numpy as np

    tick = time.time()

    k, l, Nx, Ny, Qx, Qy, Dnz = octave.cross_association(idx_list + 1)

    print "CA is done in {} sec".format(time.time() - tick)

    k, l = int(k), int(l)
    Qx = Qx.ravel().astype(np.int) - 1
    Qy = Qy.ravel().astype(np.int) - 1

    Nx = Nx.ravel()
    Ny = Ny.ravel()

    Dnz = Dnz.reshape((k, l)).astype(np.float)

    Nz_x, Nz_y = Dnz.nonzero()

    x_idx = {}
    y_idx = {}

    for i in np.unique(Nz_x):
        x_idx[i] = np.where(Qx == i)[0]

    for j in np.unique(Nz_y):
        y_idx[j] = np.where(Qy == j)[0]

    values = []
    inds_row = []
    inds_col = []
    for i in np.unique(Nz_x):
        for j in np.unique(Nz_y):
            #print x_idx[i], y_idx[j]
            temp = np.array(list(product(x_idx[i], y_idx[j])))
            inds_row += list(temp[:, 0])
            inds_col += list(temp[:, 1])
            values += [Dnz[i, j] / (Nx[i] * Ny[j])] * temp.shape[0]

    sp_mat = sparse.csc_matrix((values, (inds_row, inds_col)),
                               shape=(victims, attackers))

    return sp_mat
Example #26
0
def resample_folder(input_folder, output_folder, fs, regex):
    """Resamples the wav files within an input folder.

    Arguments
    ---------
    input_folder : path
        Path of the folder to resample.
    output_folder : path
        Path of the output folder with the resampled data.
    fs : int
        Target sampling frequency.
    reg_exp: str
        Regular expression for search.
    """
    filedir = os.path.dirname(os.path.realpath(__file__))
    octave.addpath(filedir)
    # add the matlab functions to octave dir here

    files = glob.glob(os.path.join(input_folder, regex), recursive=True)
    for f in tqdm.tqdm(files):

        audio, fs_read = torchaudio.load(f)
        audio = audio[0].numpy()
        audio = signal.resample_poly(audio, fs, fs_read)

        tmp = octave.activlev(audio.tolist(), fs, "n")
        audio, _ = tmp[:-1].squeeze(), tmp[-1]

        peak = np.max(np.abs(audio))
        audio = audio / peak
        audio = torch.from_numpy(audio)

        relative_path = os.path.join(
            Path(f).relative_to(Path(input_folder)).parent,
            Path(f).relative_to(Path(input_folder)).stem +
            "_peak_{}.wav".format(peak),
        )

        os.makedirs(
            Path(
                os.path.join(output_folder,
                             Path(f).relative_to(Path(input_folder)))).parent,
            exist_ok=True,
        )

        torchaudio.save(
            os.path.join(output_folder, relative_path),
            audio,
            fs,
        )
Example #27
0
def main(args):

    with open(args.config) as f:
        configuration_object = json.load(f)

    octave.addpath(args.octave_method_path)

    print 'Making cytometer'
    cytometer = Cytometer(configuration_object['cytometer_configuration'])

    print 'Making color channel'

    color_channels = []
    color_files = []
    color_channels_octave_array = -1  # -1 is the initial state of the array for creast_color_channel_array function
    for channel in configuration_object['color_model']['channels']:
        c = Channel(channel, cytometer)
        color_channels.append(c)
    cm = ColorModel(configuration_object['color_model'], color_channels)

    bead_file = cm.obj['bead_file']
    blank_file = cm.obj['blank_file']
    bead_model = cm.obj['bead_model']

    octave_channel_objects = []
    channel_files = []
    for c in color_channels:
        octave_channel_objects.append(c.octave_obj)

    pprint.pprint(configuration_object['experiment'])
    output = Experiment(configuration_object['experiment'])

    print "bead file: {}".format(bead_file)
    print "\n\n"
    print "blank file: {}".format(blank_file)
    print "\n\n"
    print "channels: {}".format(pprint.pprint(octave_channel_objects))
    print "\n\n"
    print "color_files: {}".format(pprint.pprint(cm.color_channel_files))
    print "color_pair_files: {}".format(pprint.pprint(cm.color_pairs))

    octave.process_experiment(
        octave_channel_objects, cm.octave_object, cm.color_channel_files,
        cm.color_pairs, output.octave_output_object,
        output.octave_readings_object)  #,configuration_object['experiment'])
Example #28
0
 def __init__(self, signal1, signal2):
     HOME = os.path.expanduser('~')
     mFiles = HOME + '/.piwavelet/wtc/'
     self.wtcPath = octave.addpath(mFiles)
     self.signal1 = signal1
     self.signal2 = signal2
     self.Rqs, self.period, self.scale, self.coi,\
     self.wtcsig = self.__wtc(self.signal1, self.signal2)
     self.freqs = 1.0 / self.period
Example #29
0
    def OCT_compute_torques(self, q, q_target, ctrl_err_derivates):
        from oct2py import octave
        octave.addpath("./internal/octave/")

        u, self.ctrl_err_int, self.antiwindup = octave.controlJoints(
            q.reshape(-1, 1),
            q_target.reshape(-1, 1),
            ctrl_err_derivates.reshape(-1, 1),
            self.dt,
            self.kp.reshape(-1, 1),
            self.ki.reshape(-1, 1),
            self.kd.reshape(-1, 1),
            self.awgain,
            self.umax.reshape(-1, 1),
            nout=3)
        self.ctrl_err_int = self.ctrl_err_int.squeeze()
        self.antiwindup = self.antiwindup.squeeze()
        return u
Example #30
0
 def __init__(self, signal1, signal2):
     HOME = os.path.expanduser('~')
     mFiles = HOME + '/.piwavelet/wtc/'
     self.wtcPath = octave.addpath(mFiles)
     self.signal1 = signal1
     self.signal2 = signal2
     self.Rqs, self.period, self.scale, self.coi,\
     self.wtcsig = self.__wtc(self.signal1, self.signal2)
     self.freqs = 1.0 / self.period
Example #31
0
def set_dmrt_qms_path(path):
    """set the path where dmrt_qms archive has been uncompressed, i.e. where the file `dmrt_qmsmain.m` is located."""
    global _dmrt_qms_path

    if path != _dmrt_qms_path:
        #octave.restoredefaultpath() # risk of bad interference with MEMLS
        octave.addpath(os.path.join(path, 'passive'))
        octave.addpath(os.path.join(path, 'active'))
        octave.addpath(os.path.join(path, 'common'))
        octave.addpath(os.path.dirname(__file__))
        _dmrt_qms_path = path
Example #32
0
def main(args):


  with open(args.config) as f:
    configuration_object = json.load(f) 

  octave.addpath(args.octave_method_path)

  print 'Making cytometer'
  cytometer = Cytometer(configuration_object['cytometer_configuration'])

  print 'Making color channel'

  color_channels = []
  color_files = []
  color_channels_octave_array = -1 # -1 is the initial state of the array for creast_color_channel_array function
  for channel in configuration_object['color_model']['channels']:
    c = Channel(channel,cytometer)
    color_channels.append(c)
  cm = ColorModel(configuration_object['color_model'],color_channels)

  bead_file = cm.obj['bead_file']
  blank_file = cm.obj['blank_file']
  bead_model = cm.obj['bead_model']

  octave_channel_objects = []
  channel_files = []
  for c in color_channels:
     octave_channel_objects.append(c.octave_obj)

  pprint.pprint(configuration_object['experiment'])
  output = Experiment(configuration_object['experiment'])


  print "bead file: {}".format(bead_file)
  print "\n\n"
  print "blank file: {}".format(blank_file)
  print "\n\n"
  print "channels: {}".format(pprint.pprint(octave_channel_objects))
  print "\n\n"
  print "color_files: {}".format(pprint.pprint(cm.color_channel_files))
  print "color_pair_files: {}".format(pprint.pprint(cm.color_pairs))
 
  octave.process_experiment(octave_channel_objects,cm.octave_object,cm.color_channel_files,cm.color_pairs,output.octave_output_object,output.octave_readings_object)#,configuration_object['experiment']) 
Example #33
0
 def __init__(self, wave, dt, freqs, dj, scale):
     self.wave = wave
     self.dt = dt
     self.period = 1.0 / freqs
     self.freqs = freqs
     self.dj = dj
     self.scale = scale
     HOME = os.path.expanduser('~')
     mFiles = HOME + '/.piwavelet/wtc/'
     self.wtcPath = octave.addpath(mFiles)
Example #34
0
 def __init__(self, wave, dt, freqs, dj, scale):
     self.wave = wave
     self.dt = dt
     self.period = 1.0 / freqs
     self.freqs = freqs
     self.dj = dj
     self.scale = scale
     HOME = os.path.expanduser('~')
     mFiles = HOME + '/.piwavelet/wtc/'
     self.wtcPath = octave.addpath(mFiles)
Example #35
0
def lvglasso(emp_cov, alpha, tau, rho=1, verbose=False, use_octave=True):
    """Wrapper for LVGLASSO in R.
    
    If emp_cov.ndim > 2, then emp_cov.shape[0] == emp_cov.shape[1].
    In the temporal case, this means that the time is the last dimension.
    """
    lvglasso_path = os.path.join(os.path.abspath(os.path.dirname(__file__)),
                                 'matlab', 'lvglasso')
    if use_octave:
        from oct2py import octave
        octave.addpath(lvglasso_path, nout=0)
        result = octave.LVGLASSO(emp_cov, alpha, tau, rho)
    else:
        global matlab_engine
        check_matlab_engine(verbose=verbose)
        matlab_engine.addpath(lvglasso_path, nargout=0)
        result = matlab_engine.LVGLASSO(matlab.double(emp_cov.tolist()),
                                        float(alpha), float(tau), float(rho))
    return result
Example #36
0
    def frames(self):
        print("TRIAL NUMBER " + str(self.trial))
        fieldsize_x = self.size_x * self.density
        fieldsize_y = self.size_y * self.density
        libpath = visual_stimulus.__file__.replace("/visual_stimulus.pyc", "") + "/textureLib" #path to the image processing library
        octave.addpath(libpath)     
        im = octave.textureBasedStimulus(self.texture_path,
                                         self.stats_type,
                                         self.seed, 
                                         fieldsize_x, 
                                         fieldsize_y,
                                         libpath)

        im = im / 255* 2*self.background_luminance
        #scipy.misc.toimage(im, cmin=0.0, cmax=2*self.background_luminance).save('/home/kaktus/Documents/mozaik/examples/img' + str(self.trial) + '.jpg')
        scipy.misc.toimage(im, cmin=0.0, cmax=2*self.background_luminance).save('/home/kaktus/Documents/mozaik/examples/img' + str(len(self.texture_path)) + "type" + str(self.stats_type) + '.jpg')
        
        assert (im.shape == (fieldsize_x, fieldsize_y)), "Image dimensions do not correspond to visual field size"
        while True:
            yield (im, [0])
Example #37
0
def read_input_and_recon_files(fpaths, openpv_path = '/home/mteti/OpenPV/mlab/util'):
    '''
    Reads inputs and recons from .pvp files. 

    Args:
        fpaths (list): List of filepaths to read data from.
        openpv_path (str): Path to */OpenPV/mlab/util.

    Returns:
        data_agg (np.ndarray): Array of shape B x F x H x W, where F is len(fpaths).
    '''

    octave.addpath(openpv_path)

    data_agg = []
    for fpath_num, fpath in enumerate(fpaths):
        batch = [sample['values'] for sample in octave.readpvpfile(fpath)]
        data_agg.append(batch)

    return np.asarray(data_agg).transpose([1, 0, 3, 2])
Example #38
0
def genFreqRatio(fname, unit, num):
    data = []
    with open(fname, 'rb') as csvfile:
        reader = csv.reader(csvfile, delimiter=',', quotechar='|')
        next(reader, None)  #skip header
        #srt= sorted(reader, key=lambda x: x[3])
        for row in reader:
            data.append(row[2])

    x = np.array(data, dtype=float)[np.newaxis]
    x = np.transpose(x)

    octave.addpath('.')
    res = octave.genFrequencyRatio(x, unit, num)
    outfname = os.path.splitext(fname)[0]+\
            "_freq_{}_{}".format(unit,num)+os.path.splitext(fname)[1]

    with open(outfname, 'w') as csvfile:
        writer = csv.writer(csvfile, delimiter=',', quotechar='|')
        for row in res:
            writer.writerow(row)
Example #39
0
def genNormDaily(fname, interval):
    data = []
    with open(fname, 'rb') as csvfile:
        reader = csv.reader(csvfile, delimiter=',', quotechar='|')
        next(reader, None)  #skip header
        #srt= sorted(reader, key=lambda x: x[3])
        for row in reader:
            data.append(row[2])

    x = np.array(data, dtype=int)[np.newaxis]
    x = np.transpose(x)

    octave.addpath('.')
    res = octave.genNormalizedDaily(x, interval)
    outfname = os.path.splitext(fname)[0]+\
            "_norm_{}".format(interval)+os.path.splitext(fname)[1]

    with open(outfname, 'w') as csvfile:
        writer = csv.writer(csvfile, delimiter=',', quotechar='|')
        for row in res:
            writer.writerow(row)
Example #40
0
    def inverse_dynamics_2d(self, left_leg_markers, right_leg_markers,
                            left_leg_forces, right_leg_forces, body_mass,
                            low_pass_cutoff):
        """Computes the hip, knee, and ankle angles, angular rates, joint
        moments, and joint forces and adds them as columns to the data
        frame.

        Parameters
        ----------
        left_leg_markers : list of strings, len(12)
            The names of the columns that give the X and Y marker
            coordinates for six markers.
        right_leg_markers : list of strings, len(12)
            The names of the columns that give the X and Y marker
            coordinates for six markers.
        left_leg_forces : list of strings, len(3)
            The names of the columns of the ground reaction forces and
            moments (Fx, Fy, Mz).
        right_leg_forces : list of strings, len(3)
            The names of the columns of the ground reaction forces and
            moments (Fx, Fy, Mz).
        body_mass : float
            The mass, in kilograms, of the subject.
        low_pass_cutoff : float
            The cutoff frequency in hertz.

        Returns
        -------
        data_frame : pandas.DataFrame
            The main data frame now with columns for the new variables. Note
            that the force coordinates labels (X, Y) are relative to the
            coordinate system described herein.

        Notes
        ------

        This computation assumes the following coordinate system::

           Y
            ^ _ o _
            |   |   ---> v
            |  / \
            -----> x

        where X is forward (direction of walking) and Y is up.

        Make sure the sign conventions of the columns you pass in are
        correct!

        The markers should be in the following order:
            1. Shoulder
            2. Greater trochanter
            3. Lateral epicondyle of knee
            4. Lateral malleolus
            5. Heel (placed at same height as marker 6)
            6. Head of 5th metatarsal

        The underlying function low pass filters the data before computing
        the inverse dynamics. You should pass in unfiltered data.

        """
        this_files_dir = os.path.split(__file__)[0]
        m_file_directory = os.path.abspath(os.path.join(this_files_dir,
                                                        'octave',
                                                        '2d_inverse_dynamics'))
        octave.addpath(m_file_directory)

        options = {'freq': low_pass_cutoff}

        time = self.data.index.values.astype(float)
        time = time.reshape((len(time), 1))  # octave wants a column vector

        marker_sets = [left_leg_markers, right_leg_markers]
        force_sets = [left_leg_forces, right_leg_forces]

        side_labels = ['Left', 'Right']
        joint_labels = ['Hip', 'Knee', 'Ankle']
        sign_labels = ['Flexion', 'Flexion', 'PlantarFlexion']
        dynamic_labels = ['Angle', 'Rate', 'Moment', 'Force']
        scale_factors = [1.0, 1.0, body_mass, body_mass]

        for side_label, markers, forces in zip(side_labels, marker_sets,
                                               force_sets):

            marker_array = self.data[markers].values.copy()
            normalized_force_array = \
                self.data[forces].values.copy() / body_mass

            # oct2py doesn't allow multiple outputs to be stored in a tuple
            # like python, so you have to output each variable
            # independently
            angles, velocities, moments, forces = \
                octave.leg2d(time, marker_array, normalized_force_array,
                             options)

            dynamics = angles, velocities, moments, forces

            fours = zip(dynamics, dynamic_labels, scale_factors)

            for array, dynamic_label, scale_factor in fours:

                if dynamic_label == 'Force':

                    # array is N x 6, (Fx, Fy) for each joint

                    a = array[:, :2], array[:, 2:4], array[:, 4:]

                    for joint_label, vectors in zip(joint_labels, a):

                        for slab, vector in zip(('X', 'Y'), vectors.T):

                            label = '.'.join([side_label, joint_label, slab,
                                              dynamic_label])

                            self.data[label] = scale_factor * vector

                else:

                    for joint_label, sign_label, vector in zip(joint_labels,
                                                               sign_labels,
                                                               array.T):

                        label = '.'.join([side_label, joint_label,
                                          sign_label, dynamic_label])

                        self.data[label] = scale_factor * vector

        return self.data
Example #41
0
from oct2py import octave
import sys
import os
import scipy.io as sio
import matplotlib.pyplot as plt
sys.path.append('../')
from SSMTopological import *
from DGMTools import *
from multiprocessing import Pool
octave.addpath('../') #Needed to use SSM and CSM

PERSTHRESH = 0.025

def getRowDists(args):
    (dgm1, dgm2, dgm3, dgm4) = args
    (matchidx, dist, D) = getWassersteinDist(dgm1, dgm2)
    #Reflect dgm3 and dgm4 across the diagonal
    if dgm3.size > 0:
        dgm3 = dgm3[:, [1, 0]]
    if dgm4.size > 0:
        dgm4 = dgm4[:, [1, 0]]
    dist += getWassersteinDist(dgm3, dgm4)[1]
    #sys.stdout.write(".")
    #sys.stdout.flush()
    return dist

if __name__ == '__main__':
    Ds1 = sio.loadmat('Ds1.mat')
    Ds1 = Ds1['D']
    Ds2 = sio.loadmat('Ds2.mat')
    Ds2 = Ds2['D']
def addOctaveToPath():
	octave.addpath(getOctavePath())
Example #43
0
from oct2py import octave
import logging

octave.addpath("/Users/ruhansa/Desktop/MRI/src/gvf_matlab_src/examples/")
logging.basicConfig(level=logging.DEBUG)
octave.logger = logging.getLogger()
import numpy as np
from src.utils.io import filename2binary

img = filename2binary("/Users/ruhansa/Desktop/u64.jpg").astype("float")
img = np.flipud(img) + 1
x, y = octave.gvf_spine(img)

print "end"
Example #44
0
from PIL import Image, ImageDraw, ImageFilter, ImageEnhance
import random
from oct2py import octave
import numpy
from scipy.spatial import distance
import math
import sys
octave.addpath('octave/')

def average_color(vertices, mask, source):
  x0 = min(p[0] for p in vertices)
  x1 = max(p[0] for p in vertices)
  y0 = min(p[1] for p in vertices)
  y1 = max(p[1] for p in vertices)

  valids = map(source.getpixel, filter(lambda x: mask.getpixel(x)==1, [ (x,y) for x in range(x0, x1+1) for y in range(y0, y1+1) ]) )
#print valids
  result = tuple([sum(x)/len(x) for x in zip(*valids)])
#print result
  return result

def triangulation(x, y, draw, source):
  T = map(lambda x:map(lambda x:int(x)-1,x),octave.delaunay(x,y))
  mask = Image.new('L', source.size, 0)
  mask_draw = ImageDraw.Draw(mask)
  for triangle in T:
#for i in range(0,3):
#draw.line(((x[triangle[i]],y[triangle[i]]),(x[triangle[(i+1)%3]],y[triangle[(i+1)%3]])), fill="grey")
    vertices = [ (x[v], y[v]) for v in triangle[:3] ]
#center = (lambda x: (x[0]/3,x[1]/3))( reduce(lambda x,y: (x[0]+y[0],x[1]+y[1]), vertices)) 
#draw.polygon( vertices, fill = source.getpixel( center ) ) 
Example #45
0
from oct2py  import octave
from numpy import matrix
from numpy import linalg
from numpy import ma
from numpy import sum
#script ouputs possible combos of boggle board
#arbitrary point arithmetic is nice
octave.addpath('.')
octave.eval('boggleAdjentMatrix')
AdjentMatrix = octave.pull('boggleAdj')
AdjentMatrix = matrix(AdjentMatrix)

SumAdjentMatrix = matrix(ma.zeros((16,16),dtype=int))

for n in range(2,16): #len(word) >= 3 and len(word) == len(path+1)
	SumAdjentMatrix+=AdjentMatrix**n
	
NPaths = sum(SumAdjentMatrix)
print(NPaths)
# -*- coding: utf-8 -*-
"""
Spyder Editor

This temporary script file is located here:
/home/george/.spyder2/.temp.py
"""
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
from oct2py import octave

filename = "/home/george/Desktop/pwctools/singlePuff1.txt"
y = np.loadtxt(filename,skiprows=0,usecols=(0,))

octave.addpath('/home/george/Desktop/pwctools')

bilateralFilter = octave.pwc_bilateral(y,1,200.0,5)

fig = plt.plot(y)
fig2 = plt.plot(bilateralFilter)

Example #47
0
def readpvpfile(filename,
                progressPeriod=0,
                lastFrame=float('inf'),
                startFrame=0,
                skipFrames=1):
    import os
    assert startFrame >= 0
    assert lastFrame >= 1
    assert progressPeriod >= 0
    assert skipFrames >= 1
    assert startFrame < lastFrame
    dataTypeSwitch = {1: np.uint8,
                      2: np.int32,
                      3: np.float32,
                      4: np.int32}
    with open(filename,'rb') as stream:
        header = readpvpheader(stream)

        dataType = dataTypeSwitch[header['datatype']]
        #lastFrame = min(lastFrame, header['nbands'])

        #Initialize data structure
        data = {}
        data["header"] = header

# Deprecated filetype not implemented in python (requires oct2py):

        # PVP FILE (deprecated)
        if header['filetype'] == 1:
            from oct2py import octave
            import re
            octave.addpath(re.match('(.*)(python)',__file__).group(0) + '/mlab/util')
            raw_data = octave.readpvpfile(filename)
            data["values"] = raw_data
            return data
# Supported older filetypes, not fully tested in Python 2

        # SPIKING ACTIVITY FILE
        #TODO filetype 6 has fixed the frame indexing. It's probably broken here.
        if header['filetype'] == 2:
            lastFrame = min(lastFrame, header['nbands'])

            framesList = []
            idxList = []
            timeList = []

            frameNum = 0
            for frame in range(lastFrame):
                if frame < startFrame or (frame % skipFrames):
                    numActive = np.fromfile(stream,np.uint32,3)[-1]
                    stream.seek(np.dtype(np.uint32).itemsize * numActive,
                                os.SEEK_CUR)
                    continue
                else:
                    time = np.fromfile(stream,np.float64,1)[0]
                    timeList.append(time)

                    numActive = np.fromfile(stream,np.uint32,1)
                    dataIdx = np.fromfile(stream,np.uint32,numActive)

                    idxList.extend(dataIdx)

                    framesList.extend(np.ones((len(dataIdx)))*frameNum)
                    frameNum += 1

                    if progressPeriod:
                        if not frame % progressPeriod and frame:
                            print("File "+filename+": frame "+str(frame)+" of "+str(lastFrame))

            #Make coosparsematrix
            data["time"] = np.array(timeList)
            #Values for sparse matrix are all 1's
            data["values"] = sp.coo_matrix((np.ones((len(framesList))), (framesList, idxList)), shape=(frameNum, header["nx"]*header["ny"]*header["nf"]))

            return data

        # NON-KERNEL WEIGHT FILE
        #TODO
        elif header['filetype'] == 3:
        #    fileSize = os.path.getsize(filename)
        #    frameSize = header['recordsize'] * header['nbands'] + header['headersize']
        #    lastFrame = min(lastFrame,fileSize/frameSize)
        #    shape = (header['nxp'], header['nyp'], header['nfp'])
        #    patchPattern = np.dtype([('nx', np.uint16),
        #                             ('ny', np.uint16),
        #                             ('offset', np.uint32),
        #                             ('values', dataType, shape)])
        #    stream.seek(0)

        #    #Initialize values and time arrays
        #    frameList = range(startFrame, lastFrame, skipFrames)
        #    data["values"] = np.zeros((len(frameList), header['nbands'], header['numpatches']
        #    for frame in frameList:
        #         stream.seek(frame*frameSize)
        #         time = np.fromfile(stream,extendedHeaderPattern,1)['time'][0]


        #         data.append(DataFrame(time,[]))
        #         for arbor in range(header['nbands']):
        #             currentData = np.fromfile(stream,
        #                                       patchPattern,
        #                                       header['numpatches'])['values']
        #             data[frame].values.append(np.squeeze(np.transpose(currentData,
        #                                                               [2,1,3,0])))
        #             if progressPeriod:
        #                 if not frame % progressPeriod and frame:
        #                     print("File "+filename+": frame "+str(frame)+" of "+str(lastFrame))
        #    return PV_Object(data,header)
           assert(0)

        # DENSE, NON-SPIKING ACTIVITY FILE
        #Tested as of 2/15/16
        elif header['filetype'] == 4:
            lastFrame = min(lastFrame, header['nbands'])
            shape = (header['ny'], header['nx'], header['nf'])
            pattern = np.dtype([('time', np.float64),
                                ('values', dataType, shape)])
            frameSize = pattern.itemsize


            frameRange = range(startFrame, lastFrame, skipFrames)
            data["values"] = np.zeros((len(frameRange), header['ny'], header['nx'], header['nf']))
            data["time"] = np.zeros((len(frameRange)))

            for (i, frame) in enumerate(frameRange):
                 stream.seek(header['headersize'] + frame*frameSize)
                 currentData = np.fromfile(stream, pattern, 1)
                 time = currentData['time'][0]
                 values = currentData['values'][0]
                 data["time"][i] = time
                 data["values"][i, :, :, :] = values

                 if progressPeriod:
                     if not i % progressPeriod and frame:
                         print("File "+filename+": frame "+str(frame)+" of "+str(lastFrame))

            return data

        # KERNEL WEIGHT FILE #TODO test
        elif header['filetype'] == 5:
            fileSize = os.path.getsize(filename)
            frameSize = header['recordsize'] * header['nbands'] + header['headersize']
            lastFrame = min(lastFrame,fileSize/frameSize)
            shape = (header['nyp'], header['nxp'], header['nfp'])
            patchPattern = np.dtype([('nx', np.uint16),
                                     ('ny', np.uint16),
                                     ('offset', np.uint32),
                                     ('values', dataType, shape)])
            stream.seek(0)

            frameRange = range(startFrame, lastFrame, skipFrames)
            data["values"] = np.zeros((len(frameRange), header['nbands'], header['numpatches'], header['nyp'], header['nxp'], header['nfp']))
            data["time"] = np.zeros((len(frameRange)));

            for (i, frame) in enumerate(frameRange):
                 stream.seek(frame*frameSize)
                 time = np.fromfile(stream,extendedHeaderPattern,1)['time'][0]
                 data["time"][i] = time
                 for arbor in range(header['nbands']):
                     currentData = np.fromfile(stream,
                                               patchPattern,
                                               header['numpatches'])['values']

                     data["values"][i, arbor, :, :, :, :] = currentData

                 if progressPeriod:
                     if not i % progressPeriod and frame:
                         print("File "+filename+": frame "+str(frame)+" of "+str(lastFrame))
            return data

        # SPARSE ACTIVITY FILE
        #TODO
        elif header['filetype'] == 6:
            lastFrame = min(lastFrame, header['nbands'])
            entryPattern = np.dtype([('index', np.int32),
                                     ('activation', np.float32)])
            valuesList = []
            framesList = []
            idxList = []
            timeList = []

            frameNum = 0
            for frame in range(lastFrame):
                if frame in range(startFrame, lastFrame, skipFrames):
                    time = np.fromfile(stream,np.float64,1)[0]
                    timeList.append(time)
                    numActive = np.fromfile(stream,np.uint32,1)
                    currentData = np.fromfile(stream,entryPattern,numActive)
                    dataIdx = currentData['index']
                    dataValues = currentData['activation']
                    idxList.extend(dataIdx)
                    valuesList.extend(dataValues)
                    framesList.extend(np.ones((len(dataIdx)))*frameNum)
                    frameNum += 1
                else:
                    numActive = np.fromfile(stream,np.uint32,3)[-1]
                    stream.seek(entryPattern.itemsize * numActive, os.SEEK_CUR)
                    continue

                if progressPeriod:
                    if frame in range(startFrame, lastFrame, progressPeriod):
                        print("File "+filename+": frame "+str(frame)+" of "+str(lastFrame))

            #Make coosparsematrix
            data["time"] = np.array(timeList)
            data["values"] = sp.coo_matrix((valuesList, (framesList, idxList)), shape=(frameNum, header["nx"]*header["ny"]*header["nf"]))

            return data

from __future__ import division

import os
import unittest

import numpy as np
from oct2py import octave
#from pandas import read_csv

import seawater as sw
from seawater.constants import c3515

path = os.path.join(os.path.dirname(__file__), 'seawater_v3_3')
_ = octave.addpath(octave.genpath(path))


def compare_results(name, function, args, values):
    args = [values.get(arg) for arg in args]
    res = function(*args)  # Python call.
    # FIXME: Test only the first output when multiple outputs are present.
    if isinstance(res, tuple):
        nout = len(res)
        res = res[0]
    else:
        nout = 1
    val = octave.call('sw_%s' % name, *args, nout=nout)  # Octave call.
    if nout > 1:
        val = val[0]
    val, res = val.squeeze(), res.squeeze()
Example #49
0
Created on Fri Oct  2 21:10:16 2015

@author: sthomp
"""

import numpy as np
import matplotlib.pyplot as plt
from oct2py import octave
import time as timer
import calcLPPoctave as lpp
#%%

t0=timer.time()
mapfile='/home/sthomp/DAVE/origLPP/maps/mapQ1Q17DR24-DVMed6084.mat'

octave.addpath('/home/sthomp/DAVE/origLPP/transitLike/')
octave.addpath('/home/sthomp/DAVE/origLPP/createLightCurves/')
octave.addpath('/home/sthomp/DAVE/origLPP/drtoolbox/')
octave.addpath('/home/sthomp/DAVE/origLPP/drtoolbox/techniques/')
octave.addpath('/home/sthomp/DAVE/origLPP/stats/')
octave.addpath('/home/sthomp/DAVE/origLPP/createMatrix/')
#%%
time=np.arange(1,80,.04167)
flux=10*(np.sin(2*np.pi*time/20))**4;

period=10
phase=15;
dur=15;

plt.figure();
plt.plot(time,flux,'.')
Example #50
0

from oct2py import octave
import os
import numpy as np
import utilities.cute_dfo_functions as funs


octave.addpath(os.path.join(os.path.dirname(os.path.realpath(__file__))), '../octave')

x = np.random.random(3)
res = octave.dfovec(2, 2, x, 1)

print(res)

res = funs.dfovec(2, 2, x, 1)

print(res)
Example #51
0
import itertools
import numpy as np
from sklearn.cross_validation import StratifiedKFold
from sklearn.svm import SVC
from oct2py import octave

EVAL_DIR, allClipsNoExt = sys.argv[1], map(lambda l: os.path.splitext(l[:-1])[0], open(sys.argv[2]))
all_k = np.loadtxt(sys.stdin)

CS = np.logspace(-6, 2, 10)

classLabels = sorted(set([x.split('_')[0] for x in os.listdir(EVAL_DIR) if re.match('[A-Z].+_(train|test)\.txt', x)]))
hwd2_labels = lambda pref, classLabel: map(np.array, zip(*np.genfromtxt(os.path.join(EVAL_DIR, '%s_%s.txt' % (classLabel, pref)), dtype = None)))
slice_kernel = lambda inds1, inds2: all_k[np.ix_(map(allClipsNoExt.index, inds1), map(allClipsNoExt.index, inds2))]

octave.addpath('vlfeat-0.9.19/toolbox/misc')
octave.addpath('vlfeat-0.9.19/toolbox/plotop')

def svm_train_test(train_k, test_k, ytrain, ytest, REG_C):
	ytrain = list(ytrain)

	model = SVC(kernel = 'precomputed', C = REG_C, max_iter = 10000)
	model.fit(train_k, ytrain)

	flatten = lambda ls: list(itertools.chain(*ls))
	train_conf, test_conf = map(flatten, map(model.decision_function, [train_k, test_k]))
	
	rec, prec, trainInfo = octave.vl_pr(ytrain, train_conf)
	rec, prec, testInfo = octave.vl_pr(ytest, test_conf)

	return trainInfo['auc_pa08'], testInfo['auc_pa08']
Example #52
0
def readpvpfile(filename,
                progressPeriod=0,
                lastFrame=float('inf'),
                startFrame=0,
                skipFrames=1):
    import os
    assert startFrame >= 0
    assert lastFrame >= 1
    assert progressPeriod >= 0
    assert skipFrames >= 1
    assert startFrame < lastFrame
    dataTypeSwitch = {1: np.uint8,
                      2: np.int32,
                      3: np.float32,
                      4: np.int32}
    with open(filename,'rb') as stream:
        header = readpvpheader(stream)
        dataType = dataTypeSwitch[header['datatype']]
        lastFrame = min(lastFrame, header['nbands'])

# Deprecated filetype not implemented in python (requires oct2py):

        # PVP FILE (deprecated)
        if header['filetype'] == 1:
            from oct2py import octave
            import re
            octave.addpath(re.match('(.*)(python)',__file__).group(0) + '/mlab/util')
            raw_data = octave.readpvpfile(filename)
            return raw_data

# Supported older filetypes, not fully tested in Python 2

        # SPIKING ACTIVITY FILE
        if header['filetype'] == 2:
            lastFrame = min(lastFrame, header['nbands'])
            data = []
            for frame in range(lastFrame):
                if frame < startFrame or (frame % skipFrames):
                    numActive = np.fromfile(stream,np.uint32,3)[-1]
                    stream.seek(np.dtype(np.uint32).itemsize * numActive,
                                os.SEEK_CUR)
                    continue
                else:
                    time = np.fromfile(stream,np.float64,1)[0]
                    numActive = np.fromfile(stream,np.uint32,1)
                    currentData = np.fromfile(stream,np.uint32,numActive)
                    data.append(DataFrame(time, currentData))
                    if progressPeriod:
                        if not frame % progressPeriod and frame:
                            print("File "+filename+": frame "+str(frame)+" of "+str(lastFrame))
            return PV_Object(data,header)

        # NON-KERNEL WEIGHT FILE
        elif header['filetype'] == 3:
            fileSize = os.path.getsize(filename)
            frameSize = header['recordsize'] * header['nbands'] + header['headersize']
            lastFrame = min(lastFrame,fileSize/frameSize)
            shape = (header['nxp'], header['nyp'], header['nfp'])
            patchPattern = np.dtype([('nx', np.uint16),
                                     ('ny', np.uint16),
                                     ('offset', np.uint32),
                                     ('values', dataType, shape)])
            data = []
            stream.seek(0)
            for frame in range(startFrame, lastFrame):
                if not frame % skipFrames:
                    stream.seek(frame*frameSize)
                    time = np.fromfile(stream,extendedHeaderPattern,1)['time'][0]
                    data.append(DataFrame(time,[]))
                    for arbor in range(header['nbands']):
                        currentData = np.fromfile(stream,
                                                  patchPattern,
                                                  header['numpatches'])['values']
                        data[frame].values.append(np.squeeze(np.transpose(currentData,
                                                                          [2,1,3,0])))
                        if progressPeriod:
                            if not frame % progressPeriod and frame:
                                print("File "+filename+": frame "+str(frame)+" of "+str(lastFrame))
                    return PV_Object(data,header)

# Supported newer filetypes, fully implemented in python and tested as of 2/2/2016

        # DENSE, NON-SPIKING ACTIVITY FILE
        elif header['filetype'] == 4:
            lastFrame = min(lastFrame, header['nbands'])
            shape = (header['ny'], header['nx'], header['nf'])
            pattern = np.dtype([('time', np.float64),
                                ('values', dataType, shape)])
            frameSize = pattern.itemsize
            data = []
            for frame in range(startFrame, lastFrame):
                if not frame % skipFrames:
                    stream.seek(header['headersize'] + frame*frameSize)
                    currentData = np.fromfile(stream, pattern, 1)
                    data.append(DataFrame(currentData['time'][0],
                                          currentData['values'][0]))
                    if progressPeriod:
                        if not frame % progressPeriod and frame:
                            print("File "+filename+": frame "+str(frame)+" of "+str(lastFrame))
            return PV_Object(data,header)

        # KERNEL WEIGHT FILE
        elif header['filetype'] == 5:
            fileSize = os.path.getsize(filename)
            frameSize = header['recordsize'] * header['nbands'] + header['headersize']
            lastFrame = min(lastFrame,fileSize/frameSize)
            shape = (header['nxp'], header['nyp'], header['nfp'])
            patchPattern = np.dtype([('nx', np.uint16),
                                     ('ny', np.uint16),
                                     ('offset', np.uint32),
                                     ('values', dataType, shape)])
            data = []
            stream.seek(0)
            for frame in range(startFrame, lastFrame):
                if not frame % skipFrames:
                    stream.seek(frame*frameSize)
                    time = np.fromfile(stream,extendedHeaderPattern,1)['time'][0]
                    data.append(DataFrame(time,[]))
                    for arbor in range(header['nbands']):
                        currentData = np.fromfile(stream,
                                                  patchPattern,
                                                  header['numpatches'])['values']
                        data[frame].values.append(np.squeeze(np.transpose(currentData,
                                                                          [2,1,3,0])))
                        if progressPeriod:
                            if not frame % progressPeriod and frame:
                                print("File "+filename+": frame "+str(frame)+" of "+str(lastFrame))
                    return PV_Object(data,header)

        # SPARSE ACTIVITY FILE
        elif header['filetype'] == 6:
            lastFrame = min(lastFrame, header['nbands'])
            entryPattern = np.dtype([('index', np.int32),
                                     ('activation', np.float32)])
            data = []
            for frame in range(lastFrame):
                if frame < startFrame or (frame % skipFrames):
                    numActive = np.fromfile(stream,np.uint32,3)[-1]
                    stream.seek(entryPattern.itemsize * numActive,
                                os.SEEK_CUR)
                    continue
                else:
                    time = np.fromfile(stream,np.float64,1)[0]
                    numActive = np.fromfile(stream,np.uint32,1)
                    currentData = np.fromfile(stream,entryPattern,numActive)
                    data.append(DataFrame(time,zip(currentData['index'],
                                                   currentData['activation'])))
                    if progressPeriod:
                        if not frame % progressPeriod and frame:
                            print("File "+filename+": frame "+str(frame)+" of "+str(lastFrame))
            return PV_Object(data,header)
Example #53
0
from nltk.tokenize.punkt import PunktLanguageVars
from cltk.stop.greek.stops import STOPS_LIST
from nltk.tokenize import RegexpTokenizer
from nltk.corpus import stopwords
from collections import Counter
from nltk.util import ngrams
from oct2py import octave
import matplotlib.pyplot as plt
from oct2py.utils import Oct2PyError
import numpy.matlib
import numpy as np
import argparse
import codecs
import nltk
import re
octave.addpath('src/octave')


#documentos y sus identificadores que se cargaran y procesaran como vectores
docs = ['he', 'ro', 'ph', 'cl', 'ga', 'ep', 'co2', 'co', 'jo1','jo2', 'jo3', 'pe2', 'ja', 'pe1', 'ju', 'ma', 'mr', 'lu', 'jn', 'ac', 're']

#documentos utilizados para crear la matriz con la que se resolvera el sistema
docsMatrix = ['ro', 'ph', 'cl', 'ga', 'ep', 'co2', 'co', 'jo1','jo2', 'jo3', 'pe2', 'ja', 'pe1', 'ju', 'ma', 'mr', 'lu', 'jn', 'ac', 're']

#identidad de cada autor en el mismo orden que en la lista docsMatrix
ids = ['Paul','Paul','Paul','Paul','Paul','Paul','Paul','John','John','John','Peter', 'James', 'Peter', 'Judas', 'Matthew', 'Mark', 'Luke', 'John', 'Paul', 'John']

#documento desconocido hebrews epistle
y_unknown = ['he']

#TEST
Example #54
0
                except KeyError: stars=''
                entry='| $'+float_fmt+stars+'$  '
                s+=entry % df.loc[i,j]
            s+='|\n|'
            for j in df.columns: # Now standard errors
                s+='  '
                try:
                    se='$(' + float_fmt % sedf.loc[i,j] + ')$' 
                except KeyError: se=''
                entry='| '+se+'  '
                s+=entry 
            s+='|\n'
        return s
import numpy as np
from oct2py import octave
octave.addpath('../utils/IncPACK')

def mysvd(X):
    """Wrap np.linalg.svd so that output is "thin" and X=usv.T.
    """
    u,s,vt = np.linalg.svd(X,full_matrices=False)
    s=np.diag(s)
    v = vt.T
    return u,s,v

def svd_missing(X):
    [u,s,v]=octave.svd_missing(X)
    s=np.matrix(s)
    u=np.matrix(u)
    v=np.matrix(v)
    return u,s,v
Example #55
0
#! encoding: utf8

from unittest import TestCase, main
from proto.ils import qrmcp, reduction, search, ils
from numpy.random import rand, randint
from numpy import array, dot
from numpy.testing import assert_allclose, assert_array_equal

from oct2py import octave, Oct2Py
from os.path import expanduser as eu
octave.addpath(eu('~')+'/code/pylgrim/ils')
oc = Oct2Py()


m, n = 5, 3
print "Initital size of matrix B: %d × %d" % (m, n)
# B = rand(m, n)
B = array([[-1.38483, 0.53704, 0.14925],  # ans:
           [1.05734, 0.61432, 0.94116],  # v1    v2
           [-0.33438, -0.13293, -0.60755],  # 1     1
           [0.26814, 0.41059, -0.52649],  # -2    -1
           [-0.66335, -1.42715, -0.97412]])  # 3     2
z_true = array([1, -2, 3]).reshape(3, 1)  # column vector


class TestQrmcp(TestCase):
    def test_qrmcp(self):
        y = dot(B, z_true) + 1e-3 * rand(m, 1)
        R_qrmcp, piv, y = qrmcp(B, y)
        R_true = [[-1.58217452, -1.20917892, -0.94591479],
                  [0, 1.19444506, -0.11444606],
Example #56
0
# coding: utf-8

# # This is to test GVF in ASM for spine dicom

# In[ ]:

import os
os.chdir('/Users/ruhansa/Dropbox/spine/')
import numpy as np
import pickle
from src.util import read_jpg
fn = '/Users/ruhansa/Desktop/ex.jpg'
arr = read_jpg(fn)
from oct2py import octave
octave.addpath('/Users/ruhansa/Desktop/gvf_dist_v4.2c/examples/')
octave.gvf_spine(arr)


# In[ ]:



Example #57
0
from oct2py import octave
import os 
dir_path = os.path.dirname(os.path.realpath(__file__))
octave.addpath(dir_path)
def TC_Simulate(Mode,initialCondition,time_bound):
	ret = octave.neuronSim(initialCondition, time_bound)

	return list(ret)

if __name__ == "__main__":
	ret = TC_Simulate("default", [-0.0280794, -0.0280874, -0.0337083, -0.0280171, -0.0272915, -0.0337002, -0.0161361, -0.0091623, -0.0239873], 0.06)
	print ret
Example #58
0

from oct2py import octave
import os
from numpy import asmatrix
from utilities import trust
from numpy import array
from numpy.linalg import norm
from numpy import random


octave.addpath(os.path.realpath(os.path.dirname(os.path.realpath(__file__)) + '/../utilities/'))


def testIt(g, H, delta):
	s1, val1, posdef1, count1, lmbda1 = trust.trust(g, H, delta)
	s2, val2, posdef2, count2, lmbda2 = octave.trust(g, H, delta)

	if False:
		print(s1)
		print(s2)
		print(val1)
		print(val2)
		print(posdef1)
		print(posdef2)
		print(count1)
		print(count2)
		print(lmbda1)
		print(lmbda2)
		print('---------------')
def getDenseSiftData(path,binSize,stepSize,reSz):
    cwd       = os.path.dirname(os.path.realpath(__file__));
    OctDir    = os.path.join(cwd, './octave/');
    octave.addpath(OctDir);
    
    CharsDir  = os.path.join(cwd, '../bin/');
    CharsDir  = os.path.join(CharsDir, path);
    CharsList = os.listdir(CharsDir);
    CharsList.sort();
    
    counter = 1;
    
    SaveDir = os.path.join(cwd, '../bin/AlphabetData/');

    try:
        os.stat(SaveDir);
    except:
        os.mkdir(SaveDir);
    
    DataDir = os.path.join(SaveDir, path);
    try:
        os.stat(DataDir);
    except:
        os.mkdir(DataDir);
    
    shutil.rmtree(DataDir);
    os.mkdir(DataDir);
    
    FileName = path+'.set';
    
    for alpha in CharsList:                        
        wname = alpha.split('.');
        ch    = alpha.split('_');
        
        fname = os.path.join(CharsDir,alpha);
        
        shutil.copy2(fname, cwd);
        
        SDES  = wname[0]+'.dsift';
        SiftDes  = open(SDES, 'w');
        
        img = Image.open(alpha).convert('L');
        img = img.resize(reSz, Image.ANTIALIAS);
        img = np.array(img);
        #cv2.imwrite(alpha,img);
        
        #loc,des = octave.dsift(img,'Size',binSize,'Step',stepSize,1.5);
        loc,des = octave.dsift(img,binSize,stepSize,1.5);
        loc     = loc.astype(np.int);
        feat    = (np.vstack((loc,des))).T;
        feat    = feat.tolist();
        
        for item in feat:
            xCor,yCor = item[:2];
            if( not((0 <= xCor) and (xCor <= reSz[0]-binSize)) ):
                print('ERROR: Position');
            if( not((0 <= yCor) and (yCor <= reSz[1]-binSize)) ):
                print('ERROR: Position');
            
            llist= ' '.join(str(x) for x in item);
            
            if(np.min(img[yCor:yCor+binSize,xCor:xCor+binSize]) == 255):
                llist = str(0) + ' ' + llist;
            else:
                llist =  ch[0] + ' ' + llist;
            
            SiftDes.write("%s\n" % llist);
            SiftDes.flush();
                
        SiftDes.close();
        shutil.copy2(SDES, DataDir);
        
        
        with open(FileName, 'wb') as ff:
            pickle.dump(CharsList, ff);
        
        shutil.copy2(FileName, SaveDir);
        
        os.remove(alpha);
        os.remove(SDES);
        os.remove(FileName);

        print("================ %d. Dense SIFT done for alphabet: %s ================\n" % (counter,alpha, ));
        counter += 1;