Exemplo n.º 1
0
def plot_test(eng):
  print "Plot Testing Begin"
  eng.workspace['data'] =  \
    eng.randi(matlab.double([1,100]),matlab.double([30,2]))
  eng.eval("plot(data(:,1),'ro-')")
  eng.hold('on',nargout=0)
  eng.eval("plot(data(:,2),'bx--')")
  print "Plot testing end"
Exemplo n.º 2
0
def basic_test(eng):
  print "Basic Testing Begin"
  print "eng.power(100,2) = %d"%eng.power(100,2)
  print "eng.max(100,200) = %d"%eng.max(100,200)
  print "eng.rand(5,5) = "
  print eng.rand(5,5)
  print "eng.randi(matlab.double([1,100]),matlab.double([3,4]))"%\
    eng.randi(matlab.double([1,100]),matlab.double([3,4]))
  print "Basic Testing End"
Exemplo n.º 3
0
def fcn_tracker(vid_proto, anchor_frame_id, bbox, opts):
    # suppress caffe logs
    try:
        orig_loglevel = os.environ['GLOG_minloglevel']
    except KeyError:
        orig_loglevel = '0'
    os.environ['GLOG_minloglevel'] = '2'

    script = os.path.join(os.path.dirname(__file__),
        '../../External/fcn_tracker_matlab/fcn_tracker.m')
    fw_frames = frame_path_after(vid_proto, anchor_frame_id)
    bw_frames = frame_path_before(vid_proto, anchor_frame_id)[::-1]
    if hasattr(opts, 'max_frames') and opts.max_frames is not None:
        num_frames = int(math.ceil((opts.max_frames+1)/2.))
    else:
        num_frames = np.inf
    if hasattr(opts, 'step'):
        step = opts.step
    else:
        step = 1

    # down sample frame rates
    fw_frames = fw_frames[::step]
    bw_frames = bw_frames[::step]
    # track upto maximum frames
    fw_frames = fw_frames[:min(num_frames, len(fw_frames))]
    bw_frames = bw_frames[:min(num_frames, len(bw_frames))]

    tic = time.time()
    fw_trk = matlab_engine(script,
                [matlab.double(bbox),] + fw_frames + [opts.gpu,], opts.engine)
    if fw_trk is None:
        logging.error("Forward tracking failed: {}".format(sys.exc_info()[0]))
        fw_trk = [bbox+[1.]]

    bw_trk = matlab_engine(script,
                [matlab.double(bbox),] + bw_frames + [opts.gpu,], opts.engine)
    if bw_trk is None:
        logging.error("Backward tracking failed: {}".format(sys.exc_info()[0]))
        bw_trk = [bbox+[1.]]

    bw_trk = bw_trk[::-1]
    if len(fw_trk) > 1:
        trk = np.concatenate((bw_trk, fw_trk[1:]))
    else:
        trk = bw_trk
    toc = time.time()
    logging.info("Speed: {:02f} fps".format(len(trk) / (toc-tic)))
    start_frame = anchor_frame_id - step * (len(bw_trk) - 1);
    tracks_proto = tracks_proto_from_boxes(trk, vid_proto['video'],
            anchor_frame_id, start_frame, step)

    # reset log level
    os.environ['GLOG_minloglevel'] = orig_loglevel
    return tracks_proto
    def get_needle_points(self, image):
        """ Returns points along an ellipse that lines up the needle in the image """
        erosion_kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
        image = cv2.erode(image, erosion_kernel)
        image = cv2.erode(image, erosion_kernel)
        # image = cv2.erode(image, erosion_kernel)
        # image = cv2.dilate(image, erosion_kernel)
        # image = cv2.dilate(image, erosion_kernel)

        # segment the needle from the background
        hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

        # segment needle based on its hue
        segmented_image = cv2.inRange(hsv_image, np.array([20,90,50]),np.array([40,180,255]))

        # publish segmented image
        if self.DEBUG:
            color_image = cv2.cvtColor(segmented_image,cv2.COLOR_GRAY2RGB)
            self.image_publisher(color_image, self.color_seg_pub)

        # create a file containing all 2d positions of segemented points
        points = np.transpose(np.nonzero(segmented_image))
        # np.savetxt("needle_registration/input/needle_seg.txt", points, delimiter="   ")
        
        # result = gmmreg._core.run_ini("needle_registration/needle.ini")
        # after_tps = result[2]

        scene = matlab.double(points.tolist())
        model = matlab.double(self.model.tolist())
        
        result = self.eng.get_cpd_affine(model, scene)

        # affine transformation A*x+b
        A = np.array(result['R'])
        b = np.array(result['t'])
        x = np.transpose(self.dense_model)
        transformed_pts = np.transpose(np.dot(A, x) + b)

        # draw ellipse
        if self.DEBUG:
            color_image = image.copy()
            
            for coord in np.asarray(transformed_pts, dtype=np.uint32):
                coord = tuple(coord)[::-1]
                # color_image[coord] = np.array([0, 0, 255])
                cv2.circle(color_image, coord, 1, [0,0,255])
            self.image_publisher(color_image, self.registered_points_pub)

        return transformed_pts
Exemplo n.º 5
0
def ndarray_to_matlab(nd: np.ndarray) -> (matlab.double):
    r"""
    Convert ndarray to matlab matrix of float.

    Args:
        nd: ndarray of dtype int or float

    Returns:
        MATLAB matrix of integers.

    Raises:
        N/A

    Tests:
    >>> ndarray_to_matlab(np.arange(3))
    matlab.double([[0.0,1.0,2.0]])
    >>> ndarray_to_matlab(np.reshape(np.arange(6),(3,2)))
    matlab.double([[0.0,1.0],[2.0,3.0],[4.0,5.0]])
    """
    # create generator
    nditer = np.nditer(nd, order='F')
    # create list with comprehension then convert to MATLAB double
    row = matlab.double([float(i) for i in nditer])
    # turn row in matrix
    row.reshape(nd.shape)
    return row
Exemplo n.º 6
0
def matify(arr):
	temparr = []
	for element in arr:
		temparr.append(element[0])
	length = len(temparr)
	arr = matlab.double(temparr)
	arr.reshape((length, 1))
	return arr
 def run(self):
     while True:
         message = self.command_queue.get()
         if message == DEATH:
             for name, jobs in self.jobs.items():
                 for jobname, (job, q) in jobs.items():
                     q.put(DEATH)
                     job.join()
             break
         if message[0] == 'KILL':
             run_name = message[1]
             for jobname, (job, q) in self.jobs[run_name].items():
                 q.put(DEATH)
         elif message[0] == 'KILL BY DIR':
             dirname = message[1]
             for runname, jobs in self.jobs.items():
                 if dirname in list(jobs.keys()):
                     run_name = runname
                     break
             for jobname, (job, q) in self.jobs[run_name].items():
                 q.put(DEATH)
         elif message[0] == 'GET DIRNAMES':
             self.output_queue.put([list(j.keys()) for r, j in self.jobs.items()])
         else:
             # name here is the name of the job, not the two SIMS!!!! F**K
             name = message[0]
             self.jobs[name] = {}
             for profile in message[1]:
                 # message_queue = queue.Queue()
                 message_queue = Queue()
                 j = MATLABScript(message_queue,
                                  (float(profile['iter']),
                                   float(profile['wnum']),
                                   float(profile['zmax']),
                                   float(profile['tmax']),
                                   matlab.double(profile['domain'].tolist()),
                                   matlab.double(profile['area'].tolist())))
                 j.start()
                 jobname = DIRSTR.format(
                         profile['wnum'],
                         profile['tmax'],
                         profile['zmax'],
                         profile['Nz'],
                         profile['iter'])
                 self.jobs[name][jobname] = (j, message_queue)
Exemplo n.º 8
0
def mtspecgrampt(channel: np.ndarray, window_size: float=0.5,
                 window_step: float=0.5, tapers: int=5,
                 params: Dict={"tapers": matlab.double([4, 7]),
                               "Fs": 40000.0,
                               "fpass": matlab.double([1, 100]),
                               "pad": 1.0,
                               "trialave": 1.0}) -> (np.ndarray):
    r"""
    Multi-taper time-frequency spectrum - point process.
    """
    if tapers is not None:
        params['tapers'] = matlab.double([(tapers + 1) / 2, tapers])
    m.workspace['params'] = params
    m.workspace['win'] = matlab.double([window_size, window_step])
    m.workspace['channel'] = ndarray_to_matlab(channel)

    S = m.eval("mtspecgrampt(channel,win,params);", nargout=1)

    return np.array(S)
Exemplo n.º 9
0
def cca(W, Z):
	"""
	Canonical Correlation Analysis (CCA): Returns rows of Z which are maximally correlated to W.
	"""
	print "CCA....."
	import matlab
	import matlab.engine as mateng

	eng = mateng.start_matlab()	

	W_mat = matlab.double(W.tolist())
	Z_mat = matlab.double(Z.tolist())

	[A, B, r, U, V, stats] = eng.canoncorr(W_mat, Z_mat, nargout = 6)

	Z = np.array(V)

	eng.quit()

	return Z
Exemplo n.º 10
0
        def _invoke(args, nargout=1, bare=False, input_names=None, output_names=None, stdout=None, stderr=None):
            import openmdao.api
            import matlab
            import matlab.engine
            import matlab.mlarray

            def transcode(val):
                if numpy and isinstance(val, numpy.ndarray):
                    return val.tolist()
                if numpy and isinstance(val, (numpy.float16, numpy.float32, numpy.float64)):
                    return float(val)
                return val
            args = [transcode(val) for val in args]

            try:
                if bare:
                    nargout = 0
                    for i, input_name in enumerate(input_names):
                        arg = args[i]

                        if isinstance(args[i], list):
                            if len(arg) and isinstance(arg[0], six.string_types):
                                pass
                            else:
                                arg = matlab.double(arg)
                        self.engine.workspace[str(input_name)] = arg

                    getattr(self.engine, name)(nargout=nargout, stdout=stdout, stderr=stderr)
                    outputs = []

                    def convert_to_list(array, size):
                        if len(size) == 1:
                            return list(array)
                        return list((convert_to_list(l, size[1:]) for l in array))
                    for output_name in output_names:
                        output = self.engine.workspace[str(output_name)]
                        if isinstance(output, matlab.mlarray.double):
                            output = convert_to_list(output, output.size)
                        outputs.append(output)
                    # open('debug.txt', 'a').write(repr(outputs) + '\n')
                else:
                    outputs = getattr(self.engine, name)(*args, nargout=nargout, stdout=stdout, stderr=stderr)
            except Exception as e:
                if type(e).__module__ in ('matlab', 'matlab.engine'):
                    e = openmdao.api.AnalysisError(getattr(e, 'message', getattr(e, 'args', ['unknown MATLAB exception'])[0]))
                    raise e


            # print(repr(outputs))
            return outputs
Exemplo n.º 11
0
def decode(eng, arrEncoded_bin, n=7, k=4, strCoder="hamming/binary"):
    """
       decode encrypted message via matlab FEC coders
   """
    # convert to binary representation
    encoded = matlab.double(list(arrEncoded_bin))

    # decode
    decoded = eng.decode(encoded, n, k, strCoder, nargout=1)

    # convert to python array
    lsDecoded = []
    for row in decoded:
        for item in row:
            lsDecoded.append(int(item))
    return np.array(lsDecoded)
Exemplo n.º 12
0
    def invoke(self, name, args, nargout, bare, input_names, output_names):
        import matlab
        import matlab.engine
        import matlab.mlarray
        out = six.StringIO()
        err = six.StringIO()
        args = pickle.loads(b64decode(args))
        if bare:
            nargout = 0
            for i, input_name in enumerate(input_names):
                arg = args[i]

                if isinstance(args[i], list):
                    if len(arg) and isinstance(arg[0], six.string_types):
                        pass
                    else:
                        arg = matlab.double(arg)

                try:
                    self.engine.workspace[input_name.encode('ascii', 'ignore')] = arg
                except ValueError as e:
                    if e.message == 'invalid field for MATLAB struct':
                        for name in arg:
                            if not re.match('^[a-zA-Z][a-zA-Z0-9_]{0,62}$', name):
                                raise ValueError('invalid field for MATLAB struct "{0}". '.format(name) +
                                    'MATLAB fields must start with a letter and contain only letters, numbers, and underscores. ' +
                                    'MATLAB fields must contain 63 characters or fewer.')
                    raise

            getattr(self.engine, name)(nargout=nargout, stdout=out, stderr=err)
            outputs = []

            def convert_to_list(array, size):
                if len(size) == 1:
                    return list(array)
                return list((convert_to_list(l, size[1:]) for l in array))
            for output_name in output_names:
                output = self.engine.workspace[str(output_name)]
                if isinstance(output, matlab.mlarray.double):
                    output = convert_to_list(output, output.size)
                outputs.append(output)
            # open('debug.txt', 'a').write(repr(outputs) + '\n')
        else:
            outputs = getattr(self.engine, name)(*args, nargout=nargout, stdout=out, stderr=err)

        return {"output": pickle.dumps(outputs), "stdout": out.getvalue(), "stderr": err.getvalue()}
Exemplo n.º 13
0
t = np.arange(len(w))/sr

fig,ax = pl.subplots(2,sharex=True)
ax[0].plot(t,w)
ax[1].plot(t,g)

try: 
    import matlab.engine
    import matlab
    eng = matlab.engine.start_matlab()
except ImportError:
    pass
else:
    eng.addpath(eng.genpath('~/Devel/covarep/'))
    try:
        g_m, dg_m, vt_m, gf_m = eng.iaif_ola(matlab.double(w.tolist()),
                                     float(sr),
                                     nargout=4)
    except matlab.engine.MatlabExecutionError:
        pass
    else:
        ax[1].plot(t,np.array(g_m).flatten())
    finally:
        eng.quit()

pl.figure()
pl.specgram(w, Fs=sr, NFFT=2**10)
for ii in range(vt.shape[0]):
    t = len(w)/sr*ii/vt.shape[0]
    p,bw = lpcc2pole(vt[ii,:],sr)
    pl.scatter(np.ones(len(bw))*t, p, s=1/np.sqrt(bw), color='k')
Exemplo n.º 14
0
def _ml_refs(refpts, nelems):
    """Convert refpoints to Matlab"""
    uintrefs = uint32_refpts(refpts, nelems)
    return matlab.double([ref+1 for ref in uintrefs])
Exemplo n.º 15
0
DirMask = os.path.join(dirpath, 'Markings', 'ABO', LayerType, 'FinalGT')
DirSaveMask = os.path.join(dirpath, 'Results', 'ABO', 'Test Masks')
DirThresh = os.path.join(dirpath, 'Results', 'ABO', 'Thresholds')

## Check if save directories exist
if not os.path.exists(DirSaveMask):
    os.makedirs(DirSaveMask)
if not os.path.exists(DirSaveData):
    os.makedirs(DirSaveData)

#% Set parameters
pixSize = 0.78  #um
meanR = 5.85  # neuron radius in um
AvgArea = round(math.pi * (meanR / pixSize)**2)
Thresh = 0.5  # IoU threshold for matching
SZ = matlab.double([487, 487])  #x and y dimension of data

## read saved threshold values
optThresh = sio.loadmat(os.path.join(DirThresh, ThreshFile))
thresh = matlab.single([optThresh['ProbThresh'][0][ind]])
minArea = matlab.single([optThresh[AreaName][0][ind]])

## Check if HomoFiltered downsampled data is available
data_file = Path(
    os.path.join(DirSaveData, name[0] + '_dsCropped_HomoNorm.nii.gz'))
if not data_file.exists():
    data_file = os.path.join(DirData, name[0] + '_processed.nii.gz')
    s = 30
    matlabLib.HomoFilt_Normalize(data_file, DirSaveData, name[0], s, nargout=0)
#%%
## Run data through the trained network
Exemplo n.º 16
0
def evlauate(pre_y, Y_test, pre_score_2):
    # average_pre_score = average_precision_score(Y_test, pre_score_2, average='samples')
    # zero_one_loss_1 = zero_one_loss(Y_test, pre_y)
    # coverage_error_1 = coverage_error(Y_test, pre_score_2) - 1
    # label_ranking_loss_1 = label_ranking_loss(Y_test, pre_score_2)
    # ham_loss = hamming_loss(Y_test.T, pre_y.T)
    # acc_score = accuracy_score(Y_test, pre_y)
    average_pre_score = eng.Average_precision(matlab.double(pre_score_2.T.tolist()), matlab.double(Y_test.T.tolist()))
    zero_one_loss_1 = eng.One_error(matlab.double(pre_score_2.T.tolist()), matlab.double(Y_test.T.tolist()))
    coverage_error_1 = eng.coverage(matlab.double(pre_score_2.T.tolist()), matlab.double(Y_test.T.tolist()))
    label_ranking_loss_1 = eng.Ranking_loss(matlab.double(pre_score_2.T.tolist()), matlab.double(Y_test.T.tolist()))
    ham_loss = eng.Hamming_loss(matlab.double(pre_y.T.tolist()), matlab.double(Y_test.T.tolist()))
    acc_score = eng.Accuracy(matlab.double(pre_y.T.tolist()), matlab.double(Y_test.T.tolist()))

    return average_pre_score,  coverage_error_1, label_ranking_loss_1, ham_loss, zero_one_loss_1, acc_score
Exemplo n.º 17
0
def eval_separation(model, audio_list, valid_test, epoch_num, log_file, spk_to_idx, batch_size=1, spk_num=2
                    , unk_spk=False, supp_time=1, add_bgd_noise=False):
    if unk_spk:
        batch_size = 1
    if spk_num < 2:
        spk_num = 2
    batch_input_mix_fea = []
    batch_input_mix_spec = []
    batch_input_spk = []
    batch_input_len = []
    batch_mix_spec = []
    batch_mix_wav = []
    batch_target_wav = []
    batch_noise_wav = []
    batch_clean_wav = []
    batch_count = 0

    batch_sdr_0 = []
    batch_sir_0 = []
    batch_sar_0 = []
    batch_nsdr_0 = []
    batch_sdr = []
    batch_sir = []
    batch_sar = []
    batch_nsdr = []

    file_list_len = 0
    file_list = open(audio_list)
    for line in file_list:
        file_list_len += 1
    file_list.close()
    file_list = open(audio_list)
    time_start = time.time()
    for line_idx, line in enumerate(file_list):
        line = line.strip().split()
        if len(line) < 2:
            raise Exception('Wrong audio list file record in the line:', ''.join(line))
        file_tar_sounds_str = None
        if not unk_spk:
            # if not test unk_spk
            file_tar_str, file_bg_str, tar_spk_str = line
            file_bg_str = file_bg_str.strip().split(',')
        else:
            # if test unk_spk
            file_tar_str, file_bg_str, tar_spk_str, file_tar_sounds_str = line
            file_bg_str = [file_bg_str]
        wav_mix = None
        target_spk = None
        mix_len = 0
        target_sig = None
        noise_sig = None
        tar_supp_sig = None

        for file_str in ([file_tar_str]+file_bg_str)[:spk_num]:
            signal, rate = sf.read(file_str)
            if len(signal.shape) > 1:
                signal = signal[:, 0]
            if rate != config.FRAME_RATE:
                signal = resampy.resample(signal, rate, config.FRAME_RATE, filter='kaiser_best')
            signal = list(signal)
            if len(signal) > config.MAX_LEN:
                signal = signal[:config.MAX_LEN]
            if len(signal) > mix_len:
                mix_len = len(signal)

            signal = np.array(signal)
            signal -= np.mean(signal)
            signal /= np.max(np.abs(signal))

            signal = list(signal)
            if len(signal) < config.MAX_LEN:
                signal.extend(np.zeros(config.MAX_LEN - len(signal)))
            signal = np.array(signal)

            if wav_mix is None:
                wav_mix = signal
                target_sig = signal
                if not unk_spk:
                    tar_supp_sig = signal
                    batch_clean_wav.append(tar_supp_sig)
                    target_spk = spk_to_idx[tar_spk_str]
                else:
                    # idx of unk_spk: 0
                    target_spk = 0
            else:
                wav_mix = wav_mix + signal
                if noise_sig is None:
                    noise_sig = signal
                else:
                    noise_sig = noise_sig + signal

        if add_bgd_noise:
            bg_noise = config.BGD_NOISE_WAV[:config.MAX_LEN]
            bg_noise -= np.mean(bg_noise)
            bg_noise /= np.max(np.abs(bg_noise))

            wav_mix = wav_mix + bg_noise
            noise_sig = noise_sig + bg_noise

        if unk_spk:
            tmp_unk_spk_supp = 0
            for file_str in file_tar_sounds_str.strip().split(','):
                tmp_unk_spk_supp += 1
                if tmp_unk_spk_supp > config.UNK_SPK_SUPP:
                    break
                signal, rate = sf.read(file_str)
                if len(signal.shape) > 1:
                    signal = signal[:, 0]
                if rate != config.FRAME_RATE:
                    signal = resampy.resample(signal, rate, config.FRAME_RATE, filter='kaiser_best')
                signal = list(signal)
                if tar_supp_sig is None:
                    tar_supp_sig = signal
                else:
                    tar_supp_sig = tar_supp_sig + signal
            if len(tar_supp_sig) < supp_time*config.FRAME_RATE:
                raise Exception('the supp_time is too greater than the target supplemental sounds!')
            batch_clean_wav.append(tar_supp_sig[:int(supp_time * config.FRAME_RATE)])

        if config.IS_LOG_SPECTRAL:
            feature_mix = np.log(np.abs(np.transpose(librosa.core.spectrum.stft(wav_mix, config.FRAME_LENGTH,
                                                                                config.FRAME_SHIFT,
                                                                                window=config.WINDOWS)))
                                 + np.spacing(1))
        else:
            feature_mix = np.abs(np.transpose(librosa.core.spectrum.stft(wav_mix, config.FRAME_LENGTH,
                                                                         config.FRAME_SHIFT,
                                                                         window=config.WINDOWS)))

        spec_mix = np.transpose(librosa.core.spectrum.stft(wav_mix, config.FRAME_LENGTH,
                                                           config.FRAME_SHIFT,
                                                           window=config.WINDOWS))

        batch_input_mix_fea.append(feature_mix)
        batch_input_mix_spec.append(np.abs(spec_mix))
        batch_input_spk.append(target_spk)
        batch_input_len.append(mix_len)
        batch_mix_spec.append(spec_mix)
        batch_mix_wav.append(wav_mix)
        batch_target_wav.append(target_sig)
        batch_noise_wav.append(noise_sig)

        batch_count += 1

        if (batch_count == batch_size) or (line_idx == (file_list_len-1)):
            # mix_input_fea (batch_size, time_steps, feature_dim)
            _tmp_batch_size = len(batch_input_mix_fea)
            mix_input_fea = np.array(batch_input_mix_fea).reshape((_tmp_batch_size, ) + feature_mix.shape)
            # mix_input_spec (batch_size, time_steps, spectrum_dim)
            mix_input_spec = np.array(batch_input_mix_spec).reshape((_tmp_batch_size, ) + spec_mix.shape)
            # bg_input_mask = np.array(batch_input_silence_mask).reshape((_tmp_batch_size, ) + spec_mix.shape)
            # target_input_spk (batch_size, 1)
            target_input_spk = np.array(batch_input_spk).reshape((_tmp_batch_size, 1))
            # clean_input_fea (batch_size, time_steps, feature_dim)
            batch_input_clean_fea, inp_clean_shape = compute_batch_clean_fea(batch_clean_wav)
            clean_input_fea = np.array(batch_input_clean_fea).reshape((batch_size, ) + inp_clean_shape)
            if not unk_spk:
                clean_input_fea = np.log(np.zeros_like(clean_input_fea)+np.spacing(1))
            target_pred = model.predict({'input_mix_feature': mix_input_fea, 'input_mix_spectrum': mix_input_spec,
                                         'input_target_spk': target_input_spk, 'input_clean_feature': clean_input_fea})
            batch_idx = 0
            for _pred_output in list(target_pred):
                _mix_spec = batch_mix_spec[batch_idx]
                phase_mix = np.angle(_mix_spec)
                _pred_spec = _pred_output * np.exp(1j * phase_mix)
                _pred_wav = librosa.core.spectrum.istft(np.transpose(_pred_spec), config.FRAME_SHIFT,
                                                        window=config.WINDOWS)
                _target_wav = batch_target_wav[batch_idx]
                min_len = np.min((len(_target_wav), len(_pred_wav), batch_input_len[batch_idx]))
                _pred_wav = _pred_wav[:min_len]
                batch_target_wav[batch_idx] = _target_wav[:min_len]
                batch_noise_wav[batch_idx] = batch_noise_wav[batch_idx][:min_len]
                batch_mix_wav[batch_idx] = batch_mix_wav[batch_idx][:min_len]

                mix_wav = matlab.double(batch_mix_wav[batch_idx].tolist())
                target_wav = matlab.double(batch_target_wav[batch_idx].tolist())
                noise_wav = matlab.double(batch_noise_wav[batch_idx].tolist())
                pred_wav = matlab.double(_pred_wav.tolist())
                if epoch_num == 0:
                    # BSS_EVAL (truth_signal, truth_noise, pred_signal, mix)
                    bss_eval_resuts = config.MAT_ENG.BSS_EVAL(target_wav, noise_wav, mix_wav, mix_wav)
                    batch_sdr_0.append(bss_eval_resuts['SDR'])
                    batch_sir_0.append(bss_eval_resuts['SIR'])
                    batch_sar_0.append(bss_eval_resuts['SAR'])
                    batch_nsdr_0.append(bss_eval_resuts['NSDR'])
                if (line_idx < _tmp_batch_size) and (batch_idx == 0):
                    sf.write(config.TMP_PRED_WAV_FOLDER + '/test_pred_%s_ep%04d_bs%04d_idx%03d' %
                             (config.DATASET, (epoch_num+1), 1, (batch_idx+1)) +
                             '.wav', _pred_wav, config.FRAME_RATE)

                # BSS_EVAL (truth_signal, truth_noise, pred_signal, mix)
                bss_eval_resuts = config.MAT_ENG.BSS_EVAL(target_wav, noise_wav, pred_wav, mix_wav)
                batch_sdr.append(bss_eval_resuts['SDR'])
                batch_sir.append(bss_eval_resuts['SIR'])
                batch_sar.append(bss_eval_resuts['SAR'])
                batch_nsdr.append(bss_eval_resuts['NSDR'])
                batch_idx += 1

            time_end = time.time()
            sdr = np.float(np.mean(batch_sdr))
            sir = np.float(np.mean(batch_sir))
            sar = np.float(np.mean(batch_sar))
            nsdr = np.float(np.mean(batch_nsdr))
            print '\rCurrent predict:' + str(line_idx+1) + ' of ' + audio_list + \
                ' and cost time: %.4f sec. - GSDR:%f, GSIR:%f, GSAR:%f, GNSDR:%f' % ((time_end - time_start),
                                                                                     sdr, sir, sar, nsdr),
            if (line_idx+1) % 200 == 0:
                log_file.write('Have evaluated %05d mixture wavs, and cost time: %.4f sec\n'
                               % ((line_idx+1), (time_end - time_start)))
                log_file.flush()
            batch_input_mix_fea = []
            batch_input_mix_spec = []
            batch_input_spk = []
            batch_input_len = []
            batch_mix_spec = []
            batch_mix_wav = []
            batch_target_wav = []
            batch_noise_wav = []
            batch_clean_wav = []
            batch_count = 0

    if epoch_num == 0:
        sdr_0 = np.float(np.mean(batch_sdr_0))
        sir_0 = np.float(np.mean(batch_sir_0))
        sar_0 = np.float(np.mean(batch_sar_0))
        nsdr_0 = np.float(np.mean(batch_nsdr_0))
        print '\n[Epoch-%s: %d] - GSDR:%f, GSIR:%f, GSAR:%f, GNSDR:%f' % \
              (valid_test, epoch_num, sdr_0, sir_0, sar_0, nsdr_0)
        log_file.write('[Epoch-%s: %d] - GSDR:%f, GSIR:%f, GSAR:%f, GNSDR:%f\n' %
                       (valid_test, epoch_num, sdr_0, sir_0, sar_0, nsdr_0))
        log_file.flush()

    sdr = np.float(np.mean(batch_sdr))
    sir = np.float(np.mean(batch_sir))
    sar = np.float(np.mean(batch_sar))
    nsdr = np.float(np.mean(batch_nsdr))
    if epoch_num == 0:
        print '[Epoch-%s: %d] - GSDR:%f, GSIR:%f, GSAR:%f, GNSDR:%f' % \
              (valid_test, epoch_num+1, sdr, sir, sar, nsdr)
    else:
        print '\n[Epoch-%s: %d] - GSDR:%f, GSIR:%f, GSAR:%f, GNSDR:%f' % \
              (valid_test, epoch_num+1, sdr, sir, sar, nsdr)
    log_file.write('[Epoch-%s: %d] - GSDR:%f, GSIR:%f, GSAR:%f, GNSDR:%f\n' %
                   (valid_test, epoch_num+1, sdr, sir, sar, nsdr))
    log_file.flush()
    file_list.close()
Exemplo n.º 18
0
 def test_passes_matlab_data(self):
     self.assertTrue(mh._is_matlab(matlab.double([1])))
Exemplo n.º 19
0
        if name and name in sessions:
            print('connecting to a shared session by name: {0}'.format(name))
            self.engine = self._engine.connect_matlab(name)
            if self.engine:
                self.session_name = name
        else:
            print('connecting to an existing shared session')
            print('or starting a new session (this may take a few seconds)')
            self.engine = self._engine.connect_matlab()
            if self.engine:
                sessions = self._engine.find_matlab()
                self.session_name = sessions[0]
        print('connected to: {0}!'.format(self.session_name))
        print('+' * 80)
        print()


# ==============================================================================
# Debugging
# ==============================================================================

if __name__ == "__main__":

    matlab = MatlabEngine()
    matlab.connect()

    A = matlab.double([[1, 0, 1, 3], [2, 3, 4, 7], [-1, -3, -3, -4]])
    res = matlab.engine.rref(A, nargout=2)

    print(res)
Exemplo n.º 20
0
def convertCoord(coord, refloc, eng):
    coordtoconvert = matlab.double([coord[0], coord[1], 0])
    refloc = matlab.double([refloc[0][0], refloc[0][1]])
    newcoord = eng.lla2flat(coordtoconvert, refloc, 28.8, 0)
    return (newcoord[0][0], newcoord[0][1])
Exemplo n.º 21
0
def plot_voltage_spectrogram(file: file, time: Tuple=None, tapers: int=4,
                             average: bool=True,
                             channels: Any=[x for x in range(60)],
                             ignore_channels: List[int]=None,
                             window_size: float=0.5, window_step: float=0.125,
                             params: Dict={"tapers": matlab.double([4, 7]),
                                           "Fs": 40000.0,
                                           "fpass": matlab.double([1, 100]),
                                           "pad": 1.0,
                                           "trialave": 1.0}) -> (Any):
    r"""
    Use multitaper method to plot spectrogram.

    Expects HDF5 file exported from MCS. Calls mtspecgramc for computation.

    Returns:
        None; plots with matplotlib.

    Raises:
        N/A
    """
    if average is True or type(channels) is int:
        params['trialave'] = 1.0
        S = mtspecgramc(
            file, time, channels, tapers, window_size, window_step,
            ignore_channels, params)
        ax = plt.subplot('111')
        # extent will change the axis labels, with y reversed
        implot = ax.imshow(10 * np.log(np.transpose(S)), aspect='auto',
                           extent=[time[0], time[1], 100, 0])

        # invert y axis so 0 Hz is at bottom
        plt.gca().invert_yaxis()
        # show colorbar to see numeric values for intensity
        plt.colorbar(implot)
        plt.title("Spectrogram")
        plt.xlabel("Time (seconds)")
        plt.ylabel("Frequency (Hz)")

    else:
        params['trialave'] = 0.0
        # filter ignored channels
        if ignore_channels is not None:
            channels = list(
                filter(lambda x: x not in ignore_channels, channels))

        num_cols = 5
        # determine number of rows
        if len(channels) % num_cols == 0:
            num_rows = len(channels) // num_cols
        else:
            num_rows = len(channels) // num_cols + 1

        plt.rcParams['figure.figsize'] = (15.0, 2.5 * num_rows)

        for i, c in enumerate(channels):
            S = mtspecgramc(
                file, time, c, tapers, window_size, window_step, None, params)
            ax = plt.subplot(num_rows, num_cols, i + 1)
            implot = ax.imshow(10 * np.log(np.transpose(S)), aspect='auto',
                               extent=[time[0], time[1], 100, 0])
            plt.gca().invert_yaxis()
            plt.colorbar(implot)
            plt.title("Channel " + str(c))

        plt.rcParams['figure.figsize'] = (6.0, 4.0)
Exemplo n.º 22
0
    def execute(in_file, out_file):
        """Call the Matlab function dAEDalusSteadyModelInitializer() and store the resulting mass of the wingbox in the
        output XML file for each load case.

        The name of the Matlab engine is stored in CPACS. In this way it can be shared with all subsequent disciplines
        that need to use the same instance of Matlab in order to share the workspace.
        """
        doc_in = etree.parse(in_file)

        root = etree.Element('cpacs')
        doc_out = etree.ElementTree(root)

        n_lc = LoadCaseSpecific.get_n_loadcases(doc_in)
        engine_ids = []
        futures = n_lc * [None]
        for i in range(1, n_lc + 1):
            timeout = SteadyModelInitializer.MATLAB_TIMEOUT
            elem_timeout = doc_in.xpath(x_ml_timeout % i)
            if len(elem_timeout):
                timeout = float(elem_timeout[0].text)

            timestamp = 0.
            elem_timestamp = doc_in.xpath(x_ml_timestamp % i)
            if len(elem_timestamp):
                timestamp = float(elem_timestamp[0].text)

            # Obtain the current matlab engine if it is still valid and exists
            engine_id = 0
            mle = None
            elem_ml_id = doc_in.xpath(x_ml_id % i)
            if len(elem_ml_id):
                engine_id = int(float(elem_ml_id[0].text))
                if engine_id in _mles:
                    if time.time() - timestamp < timeout:
                        mle = _mles[engine_id]
                        mle.cd(dir_path)
                    else:
                        _mles.pop(engine_id)

            # If a matlab engine was not connected to, start a new one and reset the timestamp
            if mle is None:
                mle, engine_id, engine_name, timestamp = start_new_matlab_engine(
                )
                mle.cd(dir_path)
                _mles.update({engine_id: mle})

            engine_ids.append(engine_id)

            xml_safe_create_element(doc_out, x_ml_id % i, engine_id)
            xml_safe_create_element(doc_out, x_ml_timestamp % i, timestamp)

            futures[i - 1] = mle.dAEDalusSteadyModelInitializer(
                in_file,
                matlab.double([n_seg_x]),
                matlab.double([n_seg_y]),
                nargout=2,
                async=True)

        # Remove any instances of the Matlab engine which aren't used anymore to free up memory
        if len(_mles) > n_lc:
            _ids = _mles.keys()
            for _id in _ids:
                if _id not in engine_ids:
                    _mles.pop(_id)

        for i, future in enumerate(futures):
            if future is not None:
                try:
                    m_wing, initial_grid = future.result()
                    initial_grid = np.array(initial_grid)
                    xml_safe_create_element(doc_out, x_m_wing, m_wing)

                    for j in range(3):
                        xml_safe_create_element(doc_out,
                                                x_grid_initial[j] % (i + 1),
                                                initial_grid[j, :])
                except MatlabExecutionError:
                    break

        doc_out.write(out_file,
                      encoding='utf-8',
                      pretty_print=True,
                      xml_declaration=True)
Exemplo n.º 23
0
def matlab_SurfStatPeakClus(slm, mask, thresh, reselspvert=None, edg=None):
    # Finds peaks (local maxima) and clusters for surface data.
    # Usage: [ peak, clus, clusid ] = SurfStatPeakClus( slm, mask, thresh ...
    #                                [, reselspvert [, edg ] ] );
    # slm         = python dictionary
    # slm['t']    = numpy array of shape (l, v) 
    # slm['tri']  = numpy array of shape (t, 3) 
    # or
    # slm['lat']  = 3D numpy array
    # mask        = numpy 'bool' array of shape (1, v) vector
    # thresh      = float
    # reselspvert = numpy array of shape (1, v) 
    # edg         = numpy array of shape (e, 2) 
    # The following are optional:
    # slm['df']     
    # slm['k'] 
    
    slm_mat = slm.copy()
    for key in slm_mat.keys():
        if isinstance(slm_mat[key], np.ndarray):
            slm_mat[key] = matlab.double(slm_mat[key].tolist()) 
        else:
            slm_mat[key] = surfstat_eng.double(slm_mat[key])

    mask_mat = matlab.double(np.array(mask, dtype=int).tolist())
    mask_mat = matlab.logical(mask_mat)

    thresh_mat = surfstat_eng.double(thresh)
    
    if reselspvert is None and edg is None:
        peak, clus, clusid = surfstat_eng.SurfStatPeakClus(slm_mat, mask_mat, 
                                                           thresh_mat, 
                                                           nargout=3) 
    elif reselspvert is not None and edg is None:
        reselspvert_mat = matlab.double(reselspvert.tolist())
        peak, clus, clusid = surfstat_eng.SurfStatPeakClus(slm_mat, mask_mat, 
                                                           thresh_mat, 
                                                           reselspvert_mat, 
                                                           nargout=3)
    elif reselspvert is not None and edg is not None:
        reselspvert_mat = matlab.double(reselspvert.tolist())
        edg_mat = matlab.double(edg.tolist())
        peak, clus, clusid = surfstat_eng.SurfStatPeakClus(slm_mat, mask_mat, 
                                                           thresh_mat, 
                                                           reselspvert_mat,
                                                           edg_mat,
                                                           nargout=3)                 
    if isinstance(peak, matlab.double):
        peak_py = np.array(peak)
    elif isinstance(peak, dict):                  
        peak_py = {key: None for key in peak.keys()}
        for key in peak:
            peak_py[key] = np.array(peak[key])        
    if isinstance(clus, matlab.double):  
        clus_py = np.array(clus)
    elif isinstance(clus, dict):
        clus_py = {key: None for key in clus.keys()}
        for key in clus:
            clus_py[key] = np.array(clus[key])
    clusid_py = np.array(clusid)    
    
    return peak_py, clus_py, clusid_py 
Exemplo n.º 24
0
def test_ndarray_to_matlab():
    assert matlab.double([[[0, 1], [2, 3], [4, 5]], [[6, 7], [8, 9], [10, 11]]]) \
        == ndarray_to_matlab(np.reshape(np.arange(12), (2, 3, 2)))
Exemplo n.º 25
0
Arquivo: matlab.py Projeto: CBMM/CBaaS
def pyToMatlab(cbaas_val, ty):
  if ty=="Image":
    return matlab.double(a.tolist())
  else:
    return cbaas_val
Exemplo n.º 26
0
def convertCoord(coord, refloc, eng):
	coordtoconvert = matlab.double([coord[0],coord[1],0])
	refloc = matlab.double([refloc[0][0], refloc[0][1]])
	newcoord = eng.lla2flat(coordtoconvert, refloc, 28.8, 0)
	return (newcoord[0][0], newcoord[0][1])
Exemplo n.º 27
0
def cvtptsnp2ptsmat(pts3d):
    mx = matlab.double(pts3d[:, 0].tolist())
    my = matlab.double(pts3d[:, 1].tolist())
    mz = matlab.double(pts3d[:, 2].tolist())

    return mx, my, mz
Exemplo n.º 28
0
def mtspecgramc(file: file, time: Tuple=None,
                channels: Any=[x for x in range(60)], tapers: int=5,
                window_size: float=0.5,
                window_step: float=0.125,
                ignore_channels: List[int]=None,
                params: Dict={"tapers": matlab.double([4, 7]),
                              "Fs": 40000.0,
                              "fpass": matlab.double([1, 100]),
                              "pad": 1.0,
                              "trialave": 1.0}) -> (np.ndarray):
    r"""
    Multi-taper time-frequency spectrum - continuous process.

    A wrapper for the Chronux function mtspecgramc. Usage of this function
    requires Matlab--tested with 2015b. See 'The Chronux Manual'
    for further information on this function.

    Args:
        file: must be HDF5
        time: a tuple of form (start, end) in seconds
        channels: specify an individual channel (e.g. 0-59) or a List of
            channels, else use all
        window_size: size in seconds of window
        window_step: size in seconds of window step (often 25-50% of
            window_size)
        tapers: construct params with this number of tapers. Do not use with
            params.
        ignore_channels: exclude these channels from calculation
        params: for Chronux mtspecgramc

    Returns:
        S: power

    Raises:
        N/A
    """
    # construct string for matlab index of time
    if time is None:
        time_str = ':'
    else:
        time_str = '{start}:{end}'.format(start=time[0] * params['Fs'] + 1,
                                          end=time[1] * params['Fs'])
    # filter ignored channels
    if ignore_channels is not None:
        channels = list(filter(lambda x: x not in ignore_channels, channels))
    # construct string for matlab index of channel
    # adjust for 1-index
    if channels is None:
        channel_str = ':'
    elif type(channels) is list:
        channels = list(map(lambda x: x + 1, channels))
        channel_str = '[' + ','.join(map(str, channels)) + ']'
    elif type(channels) is int:
        channel_str = str(channels + 1)
    else:
        raise ValueError(
            "Unexpected type {t} for channels".format(t=type(channels)))

    if tapers is not None:
        params['tapers'] = matlab.double([(tapers + 1) / 2, tapers])

    m.workspace['params'] = params
    m.workspace['win'] = matlab.double([window_size, window_step])
    m.eval("""recording = double(hdf5read('{hdf5}',...
        '/Data/Recording_0/AnalogStream/Stream_0/ChannelData'));""".format(
        hdf5=file), nargout=0)

    S = m.eval("""mtspecgramc(recording({time_str},{channel_str}),...
               win,params);""".format(time_str=time_str,
                                      channel_str=channel_str), nargout=1)
    m.eval('clearvars params win recording', nargout=0)
    return np.array(S)
Exemplo n.º 29
0
    #

    wv = wt.Wavelet('db4')
    wv_str = 'db4'
    # #print(wt.swt_max_level(len(a)))
    # #aA2,aD2,aD1\
    # cA2,cD2,cD1=wt.wavedec(xdata[200],db1,level=2)
    #
    level = 3
    c, l = wavedec(xdata, wv, level=level)
    print('c in pywr', c)
    print('l in pywr', l)

    a2 = eng.wrcoef(
        'a',
        matlab.double(c[:].tolist())  #利用matlab对概要系数进行重构
        ,
        matlab.double(l[:]),
        wv_str,
        level)

    a2 = np.squeeze(np.array(
        a2._data))  #从matlab返回来的概要系数类型是mlarray,需要转换为numpy.array格式
    #print(type())
    eng.quit()
    print('a2 shape,a shape', np.shape(a2), np.shape(xdata))

    d = []
    for n in range(len(l) - 2):  #利用python版本的wrcoef进行细节系数重构
        D = wrcoef(c, l, wavelet=wv, level=n + 1)
        print(n)
eng = matlab.engine.start_matlab()  #可以为所欲为的调用matlab内置函数
eng.addpath('./matlab_src')
eng.addpath('./matlab_src/src')
print("eng is ok")

img_path_1 = "./img/14_r.jpg"
img_path_2 = "./img/14_s.jpg"
constant.SIFT_THRESHOLD = 0.6
pre_matches_1, pre_matches_2, des_1, des_2, partial_index_1, partial_index_2, img_1, img_2 = \
    create_pre_matches.get_pre_matches(img_path_1, img_path_2, is_unique=True, k=None, k_num=None)

pre_matches_1 = np.hstack((pre_matches_1, np.zeros([len(pre_matches_1), 1])))
pre_matches_2 = np.hstack((pre_matches_2, np.zeros([len(pre_matches_2), 1])))

plt.figure(num='Mpoints')
plt.scatter(pre_matches_1[:, 0], pre_matches_1[:, 1])
plt.figure(num='Fpoints')
plt.scatter(pre_matches_2[:, 0], pre_matches_2[:, 1])

Mpoints = matlab.double(pre_matches_1.tolist())
Fpoints = matlab.double(pre_matches_2.tolist())

result = eng.registration(Mpoints, Fpoints, nargout=1)
result = np.array(result)

plt.figure(num='result')
plt.scatter(result[:, 0], result[:, 1])
print(result)
plt.show()
Exemplo n.º 31
0
def eval_separation(model,
                    audio_list,
                    valid_test,
                    epoch_num,
                    log_file,
                    spk_to_idx,
                    batch_size=1,
                    spk_num=2,
                    unk_spk=False,
                    supp_time=1,
                    add_bgd_noise=False):
    if unk_spk:
        batch_size = 1
    if spk_num < 2:
        spk_num = 2
    batch_input_mix_fea = []
    batch_input_mix_spec = []
    batch_input_spk = []
    batch_input_len = []
    batch_mix_spec = []
    batch_mix_wav = []
    batch_target_wav = []
    batch_noise_wav = []
    batch_clean_wav = []
    batch_count = 0

    batch_sdr_0 = []
    batch_sir_0 = []
    batch_sar_0 = []
    batch_nsdr_0 = []
    batch_sdr = []
    batch_sir = []
    batch_sar = []
    batch_nsdr = []

    file_list_len = 0
    file_list = open(audio_list)
    for line in file_list:
        file_list_len += 1
    file_list.close()
    file_list = open(audio_list)
    time_start = time.time()
    for line_idx, line in enumerate(file_list):
        line = line.strip().split()
        if len(line) < 2:
            raise Exception('Wrong audio list file record in the line:',
                            ''.join(line))
        file_tar_sounds_str = None
        if not unk_spk:
            # if not test unk_spk
            file_tar_str, file_bg_str, tar_spk_str = line
            file_bg_str = file_bg_str.strip().split(',')
        else:
            # if test unk_spk
            file_tar_str, file_bg_str, tar_spk_str, file_tar_sounds_str = line
            file_bg_str = [file_bg_str]
        wav_mix = None
        target_spk = None
        mix_len = 0
        target_sig = None
        noise_sig = None
        tar_supp_sig = None

        for file_str in ([file_tar_str] + file_bg_str)[:spk_num]:
            signal, rate = sf.read(file_str)
            if len(signal.shape) > 1:
                signal = signal[:, 0]
            if rate != config.FRAME_RATE:
                signal = resampy.resample(signal,
                                          rate,
                                          config.FRAME_RATE,
                                          filter='kaiser_best')
            signal = list(signal)
            if len(signal) > config.MAX_LEN:
                signal = signal[:config.MAX_LEN]
            if len(signal) > mix_len:
                mix_len = len(signal)

            signal = np.array(signal)
            signal -= np.mean(signal)
            signal /= np.max(np.abs(signal))

            signal = list(signal)
            if len(signal) < config.MAX_LEN:
                signal.extend(np.zeros(config.MAX_LEN - len(signal)))
            signal = np.array(signal)

            if wav_mix is None:
                wav_mix = signal
                target_sig = signal
                if not unk_spk:
                    tar_supp_sig = signal
                    batch_clean_wav.append(tar_supp_sig)
                    target_spk = spk_to_idx[tar_spk_str]
                else:
                    # idx of unk_spk: 0
                    target_spk = 0
            else:
                wav_mix = wav_mix + signal
                if noise_sig is None:
                    noise_sig = signal
                else:
                    noise_sig = noise_sig + signal

        if add_bgd_noise:
            bg_noise = config.BGD_NOISE_WAV[:config.MAX_LEN]
            bg_noise -= np.mean(bg_noise)
            bg_noise /= np.max(np.abs(bg_noise))

            wav_mix = wav_mix + bg_noise
            noise_sig = noise_sig + bg_noise

        if unk_spk:
            tmp_unk_spk_supp = 0
            for file_str in file_tar_sounds_str.strip().split(','):
                tmp_unk_spk_supp += 1
                if tmp_unk_spk_supp > config.UNK_SPK_SUPP:
                    break
                signal, rate = sf.read(file_str)
                if len(signal.shape) > 1:
                    signal = signal[:, 0]
                if rate != config.FRAME_RATE:
                    signal = resampy.resample(signal,
                                              rate,
                                              config.FRAME_RATE,
                                              filter='kaiser_best')
                signal = list(signal)
                if tar_supp_sig is None:
                    tar_supp_sig = signal
                else:
                    tar_supp_sig = tar_supp_sig + signal
            if len(tar_supp_sig) < supp_time * config.FRAME_RATE:
                raise Exception(
                    'the supp_time is too greater than the target supplemental sounds!'
                )
            batch_clean_wav.append(tar_supp_sig[:int(supp_time *
                                                     config.FRAME_RATE)])

        if config.IS_LOG_SPECTRAL:
            feature_mix = np.log(
                np.abs(
                    np.transpose(
                        librosa.core.spectrum.stft(wav_mix,
                                                   config.FRAME_LENGTH,
                                                   config.FRAME_SHIFT,
                                                   window=config.WINDOWS))) +
                np.spacing(1))
        else:
            feature_mix = np.abs(
                np.transpose(
                    librosa.core.spectrum.stft(wav_mix,
                                               config.FRAME_LENGTH,
                                               config.FRAME_SHIFT,
                                               window=config.WINDOWS)))

        spec_mix = np.transpose(
            librosa.core.spectrum.stft(wav_mix,
                                       config.FRAME_LENGTH,
                                       config.FRAME_SHIFT,
                                       window=config.WINDOWS))

        batch_input_mix_fea.append(feature_mix)
        batch_input_mix_spec.append(np.abs(spec_mix))
        batch_input_spk.append(target_spk)
        batch_input_len.append(mix_len)
        batch_mix_spec.append(spec_mix)
        batch_mix_wav.append(wav_mix)
        batch_target_wav.append(target_sig)
        batch_noise_wav.append(noise_sig)

        batch_count += 1

        if (batch_count == batch_size) or (line_idx == (file_list_len - 1)):
            # mix_input_fea (batch_size, time_steps, feature_dim)
            _tmp_batch_size = len(batch_input_mix_fea)
            mix_input_fea = np.array(batch_input_mix_fea).reshape(
                (_tmp_batch_size, ) + feature_mix.shape)
            # mix_input_spec (batch_size, time_steps, spectrum_dim)
            mix_input_spec = np.array(batch_input_mix_spec).reshape(
                (_tmp_batch_size, ) + spec_mix.shape)
            # bg_input_mask = np.array(batch_input_silence_mask).reshape((_tmp_batch_size, ) + spec_mix.shape)
            # target_input_spk (batch_size, 1)
            target_input_spk = np.array(batch_input_spk).reshape(
                (_tmp_batch_size, 1))
            # clean_input_fea (batch_size, time_steps, feature_dim)
            batch_input_clean_fea, inp_clean_shape = compute_batch_clean_fea(
                batch_clean_wav)
            clean_input_fea = np.array(batch_input_clean_fea).reshape(
                (batch_size, ) + inp_clean_shape)
            if not unk_spk:
                clean_input_fea = np.log(
                    np.zeros_like(clean_input_fea) + np.spacing(1))
            target_pred = model.predict({
                'input_mix_feature': mix_input_fea,
                'input_mix_spectrum': mix_input_spec,
                'input_target_spk': target_input_spk,
                'input_clean_feature': clean_input_fea
            })
            batch_idx = 0
            for _pred_output in list(target_pred):
                _mix_spec = batch_mix_spec[batch_idx]
                phase_mix = np.angle(_mix_spec)
                _pred_spec = _pred_output * np.exp(1j * phase_mix)
                _pred_wav = librosa.core.spectrum.istft(
                    np.transpose(_pred_spec),
                    config.FRAME_SHIFT,
                    window=config.WINDOWS)
                _target_wav = batch_target_wav[batch_idx]
                min_len = np.min((len(_target_wav), len(_pred_wav),
                                  batch_input_len[batch_idx]))
                _pred_wav = _pred_wav[:min_len]
                batch_target_wav[batch_idx] = _target_wav[:min_len]
                batch_noise_wav[batch_idx] = batch_noise_wav[
                    batch_idx][:min_len]
                batch_mix_wav[batch_idx] = batch_mix_wav[batch_idx][:min_len]

                mix_wav = matlab.double(batch_mix_wav[batch_idx].tolist())
                target_wav = matlab.double(
                    batch_target_wav[batch_idx].tolist())
                noise_wav = matlab.double(batch_noise_wav[batch_idx].tolist())
                pred_wav = matlab.double(_pred_wav.tolist())
                if epoch_num == 0:
                    # BSS_EVAL (truth_signal, truth_noise, pred_signal, mix)
                    bss_eval_resuts = config.MAT_ENG.BSS_EVAL(
                        target_wav, noise_wav, mix_wav, mix_wav)
                    batch_sdr_0.append(bss_eval_resuts['SDR'])
                    batch_sir_0.append(bss_eval_resuts['SIR'])
                    batch_sar_0.append(bss_eval_resuts['SAR'])
                    batch_nsdr_0.append(bss_eval_resuts['NSDR'])
                if (line_idx < _tmp_batch_size) and (batch_idx == 0):
                    sf.write(
                        config.TMP_PRED_WAV_FOLDER +
                        '/test_pred_%s_ep%04d_bs%04d_idx%03d' %
                        (config.DATASET, (epoch_num + 1), 1,
                         (batch_idx + 1)) + '.wav', _pred_wav,
                        config.FRAME_RATE)

                # BSS_EVAL (truth_signal, truth_noise, pred_signal, mix)
                bss_eval_resuts = config.MAT_ENG.BSS_EVAL(
                    target_wav, noise_wav, pred_wav, mix_wav)
                batch_sdr.append(bss_eval_resuts['SDR'])
                batch_sir.append(bss_eval_resuts['SIR'])
                batch_sar.append(bss_eval_resuts['SAR'])
                batch_nsdr.append(bss_eval_resuts['NSDR'])
                batch_idx += 1

            time_end = time.time()
            sdr = np.float(np.mean(batch_sdr))
            sir = np.float(np.mean(batch_sir))
            sar = np.float(np.mean(batch_sar))
            nsdr = np.float(np.mean(batch_nsdr))
            print '\rCurrent predict:' + str(line_idx+1) + ' of ' + audio_list + \
                ' and cost time: %.4f sec. - GSDR:%f, GSIR:%f, GSAR:%f, GNSDR:%f' % ((time_end - time_start),
                                                                                     sdr, sir, sar, nsdr),
            if (line_idx + 1) % 200 == 0:
                log_file.write(
                    'Have evaluated %05d mixture wavs, and cost time: %.4f sec\n'
                    % ((line_idx + 1), (time_end - time_start)))
                log_file.flush()
            batch_input_mix_fea = []
            batch_input_mix_spec = []
            batch_input_spk = []
            batch_input_len = []
            batch_mix_spec = []
            batch_mix_wav = []
            batch_target_wav = []
            batch_noise_wav = []
            batch_clean_wav = []
            batch_count = 0

    if epoch_num == 0:
        sdr_0 = np.float(np.mean(batch_sdr_0))
        sir_0 = np.float(np.mean(batch_sir_0))
        sar_0 = np.float(np.mean(batch_sar_0))
        nsdr_0 = np.float(np.mean(batch_nsdr_0))
        print '\n[Epoch-%s: %d] - GSDR:%f, GSIR:%f, GSAR:%f, GNSDR:%f' % \
              (valid_test, epoch_num, sdr_0, sir_0, sar_0, nsdr_0)
        log_file.write(
            '[Epoch-%s: %d] - GSDR:%f, GSIR:%f, GSAR:%f, GNSDR:%f\n' %
            (valid_test, epoch_num, sdr_0, sir_0, sar_0, nsdr_0))
        log_file.flush()

    sdr = np.float(np.mean(batch_sdr))
    sir = np.float(np.mean(batch_sir))
    sar = np.float(np.mean(batch_sar))
    nsdr = np.float(np.mean(batch_nsdr))
    if epoch_num == 0:
        print '[Epoch-%s: %d] - GSDR:%f, GSIR:%f, GSAR:%f, GNSDR:%f' % \
              (valid_test, epoch_num+1, sdr, sir, sar, nsdr)
    else:
        print '\n[Epoch-%s: %d] - GSDR:%f, GSIR:%f, GSAR:%f, GNSDR:%f' % \
              (valid_test, epoch_num+1, sdr, sir, sar, nsdr)
    log_file.write('[Epoch-%s: %d] - GSDR:%f, GSIR:%f, GSAR:%f, GNSDR:%f\n' %
                   (valid_test, epoch_num + 1, sdr, sir, sar, nsdr))
    log_file.flush()
    file_list.close()
Exemplo n.º 32
0
    def check_depthMap(self, inputs):
        xx, yy = np.meshgrid(range(self.opt.width),
                             range(self.opt.height),
                             indexing='xy')
        xx = torch.from_numpy(xx).unsqueeze(0).unsqueeze(0).expand(
            [self.opt.batch_size, 1, -1, -1]).cuda().float()
        yy = torch.from_numpy(yy).unsqueeze(0).unsqueeze(0).expand(
            [self.opt.batch_size, 1, -1, -1]).cuda().float()
        pixelLocs = torch.cat([xx, yy], dim=1)
        predDepth = inputs['predDepth']
        pts3d = backProjTo3d(pixelLocs, predDepth, inputs['invcamK'])
        mono_projected2d, _, _ = project_3dptsTo2dpts(pts3d=pts3d,
                                                      camKs=inputs['camK'])
        mono_sampledColor = sampleImgs(inputs[('color', 0, 0)],
                                       mono_projected2d)

        downsample_rat = 10
        mapping = {'l': 'image_02', 'r': 'image_03'}
        for i in range(self.opt.batch_size):
            drawIndex = i

            draw_mono_sampledColor = mono_sampledColor[
                drawIndex, :, :, :].detach().cpu().view(3, -1).permute(
                    [1, 0]).numpy()[::downsample_rat, :]
            drawX_mono = pts3d[
                drawIndex,
                0, :, :].detach().cpu().numpy().flatten()[::downsample_rat]
            drawY_mono = pts3d[
                drawIndex,
                1, :, :].detach().cpu().numpy().flatten()[::downsample_rat]
            drawZ_mono = pts3d[
                drawIndex,
                2, :, :].detach().cpu().numpy().flatten()[::downsample_rat]
            draw_mono_sampledColor = matlab.double(
                draw_mono_sampledColor.tolist())
            drawX_mono = matlab.double(drawX_mono.tolist())
            drawY_mono = matlab.double(drawY_mono.tolist())
            drawZ_mono = matlab.double(drawZ_mono.tolist())
            # eng.eval('figure(\'visible\', \'off\')', nargout=0)
            eng.eval('figure()', nargout=0)
            h = eng.scatter3(drawX_mono,
                             drawY_mono,
                             drawZ_mono,
                             5,
                             draw_mono_sampledColor,
                             'filled',
                             nargout=0)
            eng.eval('axis equal', nargout=0)
            xlim = matlab.double([0, 50])
            ylim = matlab.double([-10, 10])
            zlim = matlab.double([-5, 5])
            eng.xlim(xlim, nargout=0)
            eng.ylim(ylim, nargout=0)
            eng.zlim(zlim, nargout=0)
            eng.eval('view([-79 17])', nargout=0)
            eng.eval('camzoom(1.2)', nargout=0)
            eng.eval('grid off', nargout=0)
            # eng.eval('set(gca,\'YTickLabel\',[]);', nargout=0)
            # eng.eval('set(gca,\'XTickLabel\',[]);', nargout=0)
            # eng.eval('set(gca,\'ZTickLabel\',[]);', nargout=0)
            eng.eval(
                'set(gca, \'XColor\', \'none\', \'YColor\', \'none\', \'ZColor\', \'none\')',
                nargout=0)

            entry_index = inputs['indicesRec'][i].cpu().numpy()
            comps = self.train_filenames[entry_index].split(' ')
            file_save_add = os.path.join(
                '/media/shengjie/other/Depins/Depins/visualization/monodepth2_3dvisualization',
                comps[0], mapping[comps[2]])
            os.makedirs(file_save_add, exist_ok=True)
            file_save_add = os.path.join(file_save_add, comps[1] + '.png')
            file_save_add = '\'' + file_save_add + '\''
            eng.eval('saveas(gcf,' + file_save_add + ')', nargout=0)
            eng.eval('close all', nargout=0)

            print("Img %d saved" % entry_index)
Exemplo n.º 33
0
    def dataPacking(self):
        """ Packaging of python RTQA data for following save
        """

        tsRTQA = dict.fromkeys(['rMean', 'rVar', 'rSNR',
                                'meanBas', 'varBas', 'meanCond', 'varCond', 'rCNR',
                                'excFDIndexes_1', 'excFDIndexes_2', 'excMDIndexes', 'FD', 'MD', 'rMSE'])

        tsRTQA['rMean'] = matlab.double(self.rMean.tolist())
        tsRTQA['rVar'] = matlab.double(self.rVar.tolist())
        tsRTQA['rSNR'] = matlab.double(self.rSNR.tolist())
        tsRTQA['meanBas'] = matlab.double(self.meanBas.tolist())
        tsRTQA['varBas'] = matlab.double(self.varBas.tolist())
        tsRTQA['meanCond'] = matlab.double(self.meanCond.tolist())
        tsRTQA['varCond'] = matlab.double(self.varCond.tolist())
        tsRTQA['rCNR'] = matlab.double(self.rCNR.tolist())
        tsRTQA['excFDIndexes_1'] = matlab.double(self._fd.excFDIndexes_1.tolist())
        tsRTQA['excFDIndexes_2'] = matlab.double(self._fd.excFDIndexes_2.tolist())
        tsRTQA['excMDIndexes'] = matlab.double(self._fd.excMDIndexes.tolist())
        tsRTQA['FD'] = matlab.double(self._fd.FD.tolist())
        tsRTQA['MD'] = matlab.double(self._fd.MD.tolist())
        tsRTQA['rMSE'] = matlab.double(self.rMSE.tolist())

        return tsRTQA
Exemplo n.º 34
0
image = data.camera()
shift = (-22.4895, 13.23774)

# The shift corresponds to the pixel offset relative to the reference image
offset_image = ndi.shift(image, shift)
print(f"Known offset (row, col): {shift}")

detected_shift, error, phase = phase_cross_correlation(image,
                                                       offset_image,
                                                       upsample_factor=100)

print(f"Detected pixel offset (row, col) with Python: {-detected_shift}")

engine = matlab.engine.start_matlab()

reference = matlab.double(np.fft.fft2(image).tolist(), is_complex=True)
moved = matlab.double(np.fft.fft2(offset_image).tolist(), is_complex=True)

output, Greg = engine.dftregistration(reference, moved, 100, nargout=2)
error, phase, row_shift, col_shift = output[0]

print(
    f"Detected pixel offset (row, col) with Matlab: {-row_shift, -col_shift}")

engine.quit()

fig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True, figsize=(8, 3))

ax1.imshow(image, cmap='gray')
ax1.set_axis_off()
ax1.set_title('Reference image')
Exemplo n.º 35
0
def test_dummy(engine):
    assert engine.isreal(matlab.double([1]))
    assert engine.isinteger(matlab.int8([1]))
Exemplo n.º 36
0
    def simulate(
            self,
            sim_states,
            T,
            property_checker=None,
            property_violated_flag=None,
            ):

        #print '='*100
        #print sim_states
        #print '='*100

        if property_checker is None:
            property_check = 0
        else:
            property_check = 1

        matlab = self.matlab

        m_t = matlab.double(sim_states.t.tolist())
        m_T = matlab.double([T])
        m_c = matlab.double(sim_states.cont_states.tolist())
        m_d = matlab.double(sim_states.discrete_states.tolist())
        m_p = matlab.double(sim_states.pvt_states.tolist())
        m_u = matlab.double(sim_states.controller_outputs.tolist())
        #m_p = matlab.double([0.0] * sim_states.cont_states.shape[0])
        m_pi = matlab.double(sim_states.plant_extraneous_inputs.tolist())
        #m_pc = matlab.double([property_check])
        m_pc = property_check

        #TAG:CLSS
        if self.sim_is_class:
            [T__, X__, D__, P__, pvf_] = self.eng.simulate_plant(
                self.sim_obj,
                m_t,
                m_T,
                m_c,
                m_d,
                m_p,
                m_u,
                m_pi,
                m_pc,
                )
        else:
            [T__, X__, D__, P__, pvf_] = self.eng.simulate_plant_fun(
                self.m_fun_str,
                m_t,
                m_T,
                m_c,
                m_d,
                m_p,
                m_u,
                m_pi,
                m_pc,
                )

        T_ = np.array(T__)
        X_ = np.array(X__)
        D_ = np.array(D__)
        P_ = np.array(P__)
        #print T__, X__
        #print T_, X_

        pvf = pvf_

        if property_checker is not None:
            property_violated_flag[0] = bool(pvf)

        # TODO: fix this weird matlab-numpy interfacing
        # FIXED: is it correct though?
        t_array = np.array(T_, ndmin=2)
        x = np.array(X_, ndmin=2)
        if D_.ndim <= 1:
            d = np.array(D_, ndmin=2).T
        else:
            d = D_
        if P_.ndim <= 1:
            pvt = np.array(P_, ndmin=2).T
        else:
            pvt = P_
        statearray = st.StateArray(
            t=t_array,
            x=x,
            d=d,
            pvt=pvt,
            s=sim_states.controller_states,
            u=sim_states.controller_outputs,
            pi=sim_states.plant_extraneous_inputs,
            ci=sim_states.controller_extraneous_inputs,
            )
        #print '='*100
        #print statearray
        #print '='*100
        return statearray
Exemplo n.º 37
0
def _convert_dict_dtypes(dictionary):
    """
    takes in dictionary of numpy/python dtypes, and returns 
    dictionary of nearly equivalent MATLAB dtypes.

    type 'list' is currently unsupported if they are mixed dtype.
    (though this could be easily implemented using MATLAB cell,
     it would not work well in the context of FEM_VGPy)
    """
    dict_out = {}
    
    for key in dictionary.keys():
        # walk through all keys, checking the type
        value = dictionary[key]

        # first if-elif ladder (initial checks)
        if value is None:
            #don't add it to the output
            continue
        elif type(value) is str:
            # matlab can automatically convert str to char
            dict_out[key] = value
            continue
        elif type(value) is list:
            # attempt to convert this to a tuple. element
            # dtypes will be taken care of in 2nd ladder
            value = tuple(value)
        
        # second if-elif ladder (convert dtypes)
        if type(value) is tuple:
            #iterable type; convert to array
            if type(value[0]) is int:
                dict_out[key] = matlab.int32(value)
            elif type(value[0]) is float:
                dict_out[key] = matlab.double(value)

        elif type(value) is numpy.ndarray:
            # we want to convert to an equivalent matlab matrix...

            #convert data to equivalent list
            data_list = value.tolist()
            
            #change dtype to matlab double and save
            dict_out[key] = matlab.double(data_list)
            
        elif type(value) is float:
            # matlab considers floats to be matrices as well.
            dict_out[key] = matlab.double([value])

        elif type(value) is int:
            # matlab considers ints to be matrices as well.
            dict_out[key] = matlab.int32([value])

        elif type(value) is dict:
            # use recursion to take care of this
            dict_out[key] = _convert_dict_dtypes(value)
            
        else:
            # undefined. alert user, then save as-is.
            # if there is a problem, matlab engine will
            # throw the proper exceptions
            print "\n!!! undefined type ",
            print type(value),
            print "... saving anyway\n"
            dict_out[key] = value
    
    return dict_out
Exemplo n.º 38
0
DirMask = os.path.join(dirpath, 'Markings', 'ABO', 'Layer175', 'FinalGT')
DirSaveMask = os.path.join(dirpath, 'Results', 'ABO', 'Test Masks')
DirThresh = os.path.join(dirpath, 'Results', networkType, 'Thresholds')

## Check if direcotries exist
if not os.path.exists(DirSaveMask):
    os.mkdir(DirSaveMask)
if not os.path.exists(DirSaveData):
    os.mkdir(DirSaveData)

## Set parameters
pixSize = 0.78  #um
meanR = 5.85  # neuron radius in um
AvgArea = round(math.pi * (meanR / pixSize)**2)
Thresh = 0.5  # IoU threshold for matching
SZ = matlab.double([487, 487])  #x and y dimension of data

## read saved threshold values
optThresh = sio.loadmat(os.path.join(DirThresh, ThreshFile))
thresh = matlab.double([optThresh['ProbThresh'][0][0]])
if networkType == 'Neurofinder':
    minArea = matlab.single([optThresh[AreaName][0][0] * 0.78**2])
else:
    minArea = matlab.single([optThresh[AreaName][0][0]])

#%%
# Check if HomoFiltered downsampled data is available
for ind in range(len(name)):
    data_file = Path(
        os.path.join(DirSaveData, name[ind] + '_dsCropped_HomoNorm.nii.gz'))
    if not data_file.exists():
Exemplo n.º 39
0
def as_matlab_type(array):
    with _matlab_paths():
        import MatlabAlgorithms.MsiAlgorithms as msi
        import matlab
    return matlab.double([list(map(float, array.ravel()))])
Exemplo n.º 40
0
bus = np.loadtxt('data/bus.txt')
line = np.loadtxt('data/line.txt')


time_start = time.time()

eng = matlab.engine.start_matlab()

time_end = time.time()
print(time_end - time_start)
time_start = time.time()

# y = eng.函数名(matlab.double([1,2]))
# out = eng.函数名(matlab.double(bus.tolist()),matlab.double(line.tolist()))

out1, out2 = eng.函数名(matlab.double(bus.tolist()),matlab.double(line.tolist()),nargout=2)
# eng.plot(matlab.double(out))
out1 = np.asarray(out1)

eng.quit()


time_end = time.time()
print(time_end - time_start)




# 从 Python 调用 MATLAB 函数

# 使用引擎从 Python 调用 MATLAB 的 sqrt 函数
Exemplo n.º 41
0
    def calculate_BEM_fields(self):
        """ Runs BEM simulation in Matlab using parameters initialized
            parameters.

            Current options for BEM simulations are
            selected by the class instance variable 'simulation_type';
                - 'disk' : disk with layer substrate, BROKEN.
                - 'bare_disk_JC' : Gold disk in water, JC data.
                - 'bare_disk_Drude' : Gold disk in water, Drude model.
                    built in to BEM.
            """
        if hasattr(self, 'BEM_images'):
            return self.BEM_images

        ## start background matlab instance before looping through images.
        print('starting Matlab...')
        eng = matlab.engine.start_matlab()

        # Add BEM to path
        eng.addpath(eng.genpath(project_path + '/matlab_bem', nargout=1),
                    nargout=0)

        if self.simulation_type is None:
            if self.simulation_file_name is not None:
                mnpbem_sim_fun = getattr(eng, self.simulation_file_name)
            else:
                raise ValueError('Give simulation file name')
        elif self.simulation_type == 'disk':
            mnpbem_sim_fun = eng.BEM_CurlyDisk_dipDrive_E
        elif self.simulation_type == 'bare_disk_JC':
            ## Just disk in water, included in package files.
            ## File reads JC data built in to BEM currently.
            mnpbem_sim_fun = eng.CurlyDiskJC_NoSub_dipDrive_E
        elif self.simulation_type == 'bare_disk_Drude':
            ## Just disk in water, included in package files.
            ## File reads JC data built in to BEM currently.
            mnpbem_sim_fun = eng.CurlyDiskDrudeNoSub_dipDrive_E
        elif self.simulation_type == 'rod':
            mnpbem_sim_fun = eng.AuNR_dipDrive_E

        # Initialize coordinates of points on hemisphere for field BEM field
        # calculation.
        sphere_points = fib.fib_alg_k_filter(num_points=self.lens_points,
                                             max_ang=self.max_theta)
        # Convert spherical coordinates to Caresian.
        cart_points_on_sph = fib.sphere_to_cart(
            sphere_points[:, 0], sphere_points[:, 1],
            self.obj_f * np.ones(np.shape(sphere_points[:, 0])))

        ## convert lens-integration coordinates to matlab variable
        matlab_cart_points_on_sph = matlab.double(cart_points_on_sph.tolist())

        # Setup values for field calculation
        drive_energy = self.drive_energy_eV
        number_of_molecules = self.mol_locations.shape[0]

        # Initialize outputs.
        self.BEM_images = np.zeros(
            (number_of_molecules, self.obs_points[0].shape[0]))
        self.bem_E = np.zeros(
            (number_of_molecules, self.obs_points[0].shape[0], 3),
            dtype=np.complex_)

        # Loop through molecules and run BEM calculation in MATLAB
        for i in range(number_of_molecules):
            print('{}th molecule'.format(int(i + 1)))
            mol_location = self.mol_locations[i]
            if np.atleast_1d(
                    self.mol_angles).shape[0] == (self.mol_locations.shape[0]):
                mol_angle = np.atleast_1d(self.mol_angles)[i]
            elif np.atleast_1d(self.mol_angles).shape[0] == 1:
                mol_angle = self.mol_angles
            mol_orientation = [
                np.cos(mol_angle),
                np.sin(mol_angle),
                0.,
            ]

            # print(f"\nmol_location input to BEM function:\n",
            #     mol_location
            #     )
            # print(f"\nmol_orientation input to BEM function:\n",
            #     mol_orientation
            #     )
            # print(f"\nmatlab_cart_points_on_sph input to BEM function:\n",
            #     matlab_cart_points_on_sph
            #     )

            self.matlab_cart_points_on_sph = matlab_cart_points_on_sph
            # Run BEM calculation, return fields and coords.
            [E, sph_p] = mnpbem_sim_fun(matlab.double(list(mol_location)),
                                        drive_energy,
                                        matlab.double(list(mol_orientation)),
                                        matlab_cart_points_on_sph,
                                        float(self.eps_b),
                                        nargout=2)

            # Format outputs for np
            self.BEM_scattered_E = np.asarray(E)
            # print('self.BEM_scattered_E.shape = ',self.BEM_scattered_E.shape)
            # BEM_scattered_H = np.asarray(H)
            # print('BEM_scattered_H.shape = ',BEM_scattered_H.shape)
            cart_sphere_points = np.asarray(sph_p)

            sph_sph_points = fib.cart_to_sphere(cart_sphere_points[:, 0],
                                                cart_sphere_points[:, 1],
                                                cart_sphere_points[:, 2]).T

            thetas_and_phis = sph_sph_points[:, 1:]

            # Calculate focused+diffracted fields
            print('calculating diffracted fields')
            diffracted_E_field = diffi.perform_integral(
                scattered_E=self.BEM_scattered_E,
                scattered_sph_coords=thetas_and_phis,
                obser_pts=self.obs_points[0] * np.array([[1, -1]]),
                z=0,
                obj_f=self.obj_f,
                tube_f=self.tube_f,
                k=(self.drive_energy_eV / hbar) * (np.sqrt(self.eps_b)) / c,
                alpha_1_max=self.max_theta)

            diffracted_power_flux = np.real(
                np.sum(np.multiply(diffracted_E_field,
                                   np.conj(diffracted_E_field)),
                       axis=-1))

            self.bem_E[i] = diffracted_E_field
            self.BEM_images[i] = diffracted_power_flux

        eng.exit()

        return self.BEM_images
Exemplo n.º 42
0
def np2mlarray(npa):
    """ Conversion of a numpy array to matlab mlarray
    
    The conversion is realised without copy for real data. First an empty initialization is realized.
    Then the numpy array is affected to the _data field. Thus the data field is not really an 
    array.array but a numpy array. Matlab doesn't see anything...
    For complex data, the strides seems to not work properly with matlab.double.
    
    Paramerters
    -----------
    npa : numpy array
        the array to convert
    Returns 
    -------
    ma : mlarray
        the converted array that can be passe to matlab

    Examples
    --------
    >>> npi=np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]],dtype=np.int64,order='C')
    >>> np2mlarray(npi)
    matlab.int64([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
    >>> npif=np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]],dtype=np.int64,order='F')
    >>> np2mlarray(npif)
    matlab.int64([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
    
    >>> npcf=np.array([[1,2+0.2j,3],[4,5,6],[7,8,9],[10+0.1j,11,12]],dtype=np.complex,order='F')
    >>> np2mlarray(npcf)
    matlab.double([[(1+0j),(2+0.2j),(3+0j)],[(4+0j),(5+0j),(6+0j)],[(7+0j),(8+0j),(9+0j)],[(10+0.1j),(11+0j),(12+0j)]], is_complex=True)


    
    References
    -----------
    https://scipy-lectures.org/advanced/advanced_numpy/ (strides)
    
    """
    # check numpy
    if 'ndarray' not in str(type(npa)):
        raise TypeError('Expect  numpy.ndarray. Got %s' % type(npa))

    # get shape
    shape = npa.shape
    # number of elements
    N = np.prod(shape)
    # compute strides (real case)
    if npa.flags.f_contiguous == True:
        strides = _getStridesF(shape)  # pour la sortie
        order = 'F'
    else:
        strides = _getStridesC(shape)  # ok, garde le même
        order = 'C'

    # complex case
    if npa.dtype in (np.complex128, np.complex):
        #  create empty matlab.mlarray
        ma = matlab.double(initializer=None, size=(1, N), is_complex=True)
        # associate the data
        """
         # associate the data (no copy), works on 2D array only...
         ma._real=npa.ravel(order=order) # allow to map real and imaginary part continuously!
         """
        cpx = npa.ravel(order='F')  # copy except for fortran like array
        ma._real = cpx.real.ravel(
        )  # second ravel to correct the strides 18->8
        ma._imag = cpx.imag.ravel()
        ma.reshape(shape)
        # ma._strides=strides
    # real case
    else:
        # create empty matlab.mlarray
        if npa.dtype == np.float64:
            ma = matlab.double(initializer=None, size=(1, N), is_complex=False)
        elif npa.dtype == np.int64:
            ma = matlab.int64(initializer=None, size=(1, N))
        elif npa.dtype == np.bool:
            ma = matlab.logical(initializer=None, size=(1, N))
        else:
            raise TypeError('Type %s is missing' % npa.dtype)

        # associate the data
        ma._data = npa.ravel(order=order)
        # print(ma._data.flags,ma._data,'\n') # control owner

        # back to original shape
        ma.reshape(shape)
        # array strides are in number of cell (numpy strides are in bytes)
        # if len(shape)==1 no need to change. Format pb because _stride expect (1,1) and stride = (1,)
        if len(shape) > 1:
            ma._strides = strides  # change stride (matlab use 'F' order ie [nc,1] )

    return ma
Exemplo n.º 43
0
def asiduj_test():
    """
    Test of the module

    It runs the doctest and create other tests with matlab engine calls."""
    import scipy.linalg as spl

    print("Run matlab engine...")
    if len(matlab.engine.find_matlab()) == 0:
        # si aucune session share, run
        eng = matlab.engine.start_matlab()
    else:
        # connect to a session
        eng = matlab.engine.connect_matlab(matlab.engine.find_matlab()[0])
        print("connected...")

    print("Further tests....\n")

    # create matlab data
    # ------------------------------------------------------------------------
    mf = eng.rand(3)
    mc = matlab.double([[1 + 1j, 0.3, 1j], [1.2j - 1, 0, 1 + 1j]],
                       is_complex=True)
    mi64 = matlab.int64([1, 2, 3])
    mi8 = matlab.int8([1, 2, 3])
    mb = matlab.logical([True, True, False])

    # Test conversion from matlab to numpy
    # ------------------------------------------------------------------------
    npf = matlab_to_ndarray(mf)  # no copy, if mf is changed, npf change!
    npc = matlab_to_ndarray(mc)  # still copy for complex (only)
    npi64 = matlab_to_ndarray(mi64)
    npi8 = matlab_to_ndarray(mi8)
    npb = matlab_to_ndarray(mb)

    # Test conversion from numpy to matlab
    # ------------------------------------------------------------------------
    npi = numpy.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],
                      dtype=numpy.int64,
                      order="F")
    mi = ndarray_to_matlab(npi)
    mc2 = ndarray_to_matlab(npc)
    mf2 = ndarray_to_matlab(
        npf)  # copy, because npf has 'F' order (comes from mlarray)
    mi64_2 = ndarray_to_matlab(npi)
    mb2 = ndarray_to_matlab(npb)

    # test orientation in the matlab workspace
    # ------------------------------------------------------------------------
    eng.workspace["mi"] = mi64_2
    eng.workspace["mc2"] = mc2

    # check results
    # ------------------------------------------------------------------------
    npcc = numpy.array(
        [
            [1.0, 1.1 + 1j],
            [
                1.12 + 0.13j,
                22.1,
            ],
        ],
        dtype=numpy.complex,
    )  # assume C
    mcc = ndarray_to_matlab(npcc)
    npcc_inv = spl.inv(npcc)
    mcc_inv = eng.inv(mcc)
    print("Are the inverse of matrix equal ?")
    print(mcc_inv)
    print(npcc_inv)

    #    # no copy check
    #    # ------------------------------------------------------------------------
    #    # complex
    #
    #    npcc[0,0]=0.25
    #    print("Are the data reuse ?", ", OWNDATA =", mcc._real.flags.owndata,
    #          "same base =", mcc._real.base is npcc,
    #          ', If one is modified, the other is modified =', mcc._real[0]==npcc[0,0])
    #

    # test sparse matrix requiert Recast4py.m
    K1, K2 = eng.sptest(3.0, nargout=2)
    Ksp1 = dict_to_sparse(K1)
    Ksp2 = dict_to_sparse(K2)
Exemplo n.º 44
0
        if len(matlab.engine.find_matlab()) == 0:
            #si aucune session share, run
            eng = matlab.engine.start_matlab()
        else:
            # connect to a session
            eng = matlab.engine.connect_matlab(matlab.engine.find_matlab()[0])
            print('connected...')
    else:
        print('Matlab engine is already runnig...')

    print('Further tests....\n')

    # create matlab data
    # ------------------------------------------------------------------------
    mf = eng.rand(3)
    mc = matlab.double([[1 + 1j, 0.3, 1j], [1.2j - 1, 0, 1 + 1j]],
                       is_complex=True)
    mi64 = matlab.int64([1, 2, 3])
    mi8 = matlab.int8([1, 2, 3])
    mb = matlab.logical([True, True, False])

    # Test conversion from matlab to numpy
    # ------------------------------------------------------------------------
    npf = mlarray2np(mf)  # no copy, if mf is changed, npf change!
    npc = mlarray2np(mc)  # still copy for complex (only)
    npi64 = mlarray2np(mi64)
    npi8 = mlarray2np(mi8)
    npb = mlarray2np(mb)

    # Test conversion from numpy to matlab
    # ------------------------------------------------------------------------
    npi = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],
Exemplo n.º 45
0
     gamma_list = [2.210416667,0.0375,1.566964286,3.971875,3.18375,2.459375,2.0025]
 elif filename == 'miRNA':
     # 众数g
     # gamma_list = [0.03125,0.03125,0.03125,0.03125,0.03125,0.03125,0.03125,0.03125]
     # 平均数g
     gamma_list = [0.5,0.0625,0.322916667,1.104166667,0.843098958,0.125,0.052083333]
 elif filename == 'mRNA':
     # 众数
     # gamma_list = [0.25,1,2,32,1,0.03125,4]
     # 平均数
     gamma_list = [2.979166667, 1, 3.5625, 16.89930556, 2.045277778, 12.89583333, 2.670138889]
 feature_id = [1,96,96,352,352,692,692,696,696,832,832,848,848,912]
 average_pre_score, coverage_error_1, label_ranking_loss_1, ham_loss, zero_one_loss_1, \
  acc_score, predict_result, predict_result_scores, mean_weight, ap_std,acc_std,cov_std,rl_std,hl_std,zo_std, average_precision_score_list,\
         acc_score_list, coverage_error_list, ranking_loss, hamming_loss_list, zero_one_loss_list = kfold_cross_validation(feature, label, c, gamma_list)
 all_average_precision_score = eng.Average_precision(matlab.double(np.array(predict_result_scores).T.tolist()), matlab.double(label.T.tolist()))
 all_accuracy_score = eng.Accuracy(matlab.double(np.array(predict_result).T.tolist()), matlab.double(label.T.tolist()))
 all_result = []
 every_fold_result = []
 every_fold_result.append(average_precision_score_list)
 every_fold_result.append(acc_score_list)
 every_fold_result.append(coverage_error_list)
 every_fold_result.append(ranking_loss)
 every_fold_result.append(hamming_loss_list)
 every_fold_result.append(zero_one_loss_list)
 # every_fold_result = np.array(every_fold_result)
 df = pd.DataFrame(every_fold_result).T
 df.to_csv('Every_fold_'+filename+'.csv', header=None, index=None)
 print(np.array(predict_result))
 print('all_average_precision_score=',all_average_precision_score)
 print('all_accuracy_score=', all_accuracy_score)
Exemplo n.º 46
0
def eval_separation(model, audio_list, valid_test, epoch_num, log_file, spk_to_idx, batch_size=1, spk_num=2
                    , unk_spk=False, supp_time=1, add_bgd_noise=False):
    if unk_spk:
        # 如果是评估unk_spk的话,不能进行批处理,unk_spk的记忆会出现混乱
        batch_size = 1
    if spk_num < 2:
        spk_num = 2
    batch_input_mix_fea = []
    batch_input_mix_spec = []
    # batch_input_silence_mask = []
    batch_input_spk = []
    batch_input_len = []  # 后面根据len进行输出语音裁剪
    batch_mix_spec = []
    batch_mix_wav = []
    batch_target_wav = []
    batch_noise_wav = []
    batch_clean_wav = []
    batch_count = 0

    batch_sdr_0 = []
    batch_sir_0 = []
    batch_sar_0 = []
    batch_nsdr_0 = []
    batch_sdr = []
    batch_sir = []
    batch_sar = []
    batch_nsdr = []
    # 遍历File_List文件
    file_list_len = 0
    file_list = open(audio_list)
    for line in file_list:
        file_list_len += 1
    file_list.close()
    file_list = open(audio_list)
    time_start = time.time()
    for line_idx, line in enumerate(file_list):
        line = line.strip().split()
        if len(line) < 2:
            raise Exception('Wrong audio list file record in the line:', ''.join(line))
        file_tar_sounds_str = None
        if not unk_spk:
            # 如果不是unk_spk测试
            file_tar_str, file_bg_str, tar_spk_str = line
            file_bg_str = file_bg_str.strip().split(',')
        else:
            # 如果是unk_spk测试
            file_tar_str, file_bg_str, tar_spk_str, file_tar_sounds_str = line
            file_bg_str = [file_bg_str]
        # 开始混叠语音
        wav_mix = None
        # 记录目标说话人
        target_spk = None
        # 记录混叠语音长度,用于测试阶段输出语音的裁剪
        mix_len = 0
        # 记录纯净目标语音
        target_sig = None
        # 记录纯净干扰语音
        noise_sig = None
        # 记录补充的目标语音
        tar_supp_sig = None

        for file_str in ([file_tar_str]+file_bg_str)[:spk_num]:
            signal, rate = sf.read(file_str)  # signal 是采样值,rate 是采样频率
            if len(signal.shape) > 1:
                signal = signal[:, 0]
            if rate != config.FRAME_RATE:
                # 如果频率不是设定的频率则需要进行转换
                signal = resampy.resample(signal, rate, config.FRAME_RATE, filter='kaiser_best')
            # 转成List
            signal = list(signal)
            if len(signal) > config.MAX_LEN:  # 根据最大长度裁剪
                signal = signal[:config.MAX_LEN]
            # 更新混叠语音长度
            if len(signal) > mix_len:
                mix_len = len(signal)

            # 转回Array, 进行信号预处理
            signal = np.array(signal)
            signal -= np.mean(signal)  # 语音信号预处理,先减去均值
            signal /= np.max(np.abs(signal))  # 波形幅值预处理,幅值归一化

            # 转成List
            signal = list(signal)
            if len(signal) < config.MAX_LEN:  # 根据最大长度用 0 补齐,
                signal.extend(np.zeros(config.MAX_LEN - len(signal)))
            # 转回Array
            signal = np.array(signal)

            if wav_mix is None:
                wav_mix = signal
                target_sig = signal  # 混叠前的纯净目标语音,只保存第一个说话人语音就好
                if not unk_spk:
                    # 如果不是unk_spk测试
                    tar_supp_sig = signal
                    batch_clean_wav.append(tar_supp_sig)
                    target_spk = spk_to_idx[tar_spk_str]  # 目标说话人
                else:
                    # unk_spk的idx为0
                    target_spk = 0
            else:
                wav_mix = wav_mix + signal  # 混叠后的语音
                if noise_sig is None:
                    noise_sig = signal
                else:
                    noise_sig = noise_sig + signal  # 混叠前的纯净干扰语音,其他说话人语音混叠为干扰语音

        if add_bgd_noise:
            bg_noise = config.BGD_NOISE_WAV[:config.MAX_LEN]
            bg_noise -= np.mean(bg_noise)  # 语音信号预处理,先减去均值
            bg_noise /= np.max(np.abs(bg_noise))  # 波形幅值预处理,幅值归一化

            wav_mix = wav_mix + bg_noise
            noise_sig = noise_sig + bg_noise

        if unk_spk:
            # 如果是unk_spk测试,则需要提取额外的语音集合
            tmp_unk_spk_supp = 0
            for file_str in file_tar_sounds_str.strip().split(','):
                tmp_unk_spk_supp += 1
                if tmp_unk_spk_supp > config.UNK_SPK_SUPP:
                    break
                signal, rate = sf.read(file_str)  # signal 是采样值,rate 是采样频率
                if len(signal.shape) > 1:
                    signal = signal[:, 0]
                if rate != config.FRAME_RATE:
                    # 如果频率不是设定的频率则需要进行转换
                    signal = resampy.resample(signal, rate, config.FRAME_RATE, filter='kaiser_best')
                signal = list(signal)
                if tar_supp_sig is None:
                    tar_supp_sig = signal
                else:
                    tar_supp_sig = tar_supp_sig + signal
            if len(tar_supp_sig) < supp_time*config.FRAME_RATE:
                raise Exception('the supp_time is too greater than the target supplemental sounds!')
            batch_clean_wav.append(tar_supp_sig[:int(supp_time * config.FRAME_RATE)])

        # log spectral, 以后可以考虑采用MFCC或GFCC特征做为输入
        if config.IS_LOG_SPECTRAL:
            feature_mix = np.log(np.abs(np.transpose(librosa.core.spectrum.stft(wav_mix, config.FRAME_LENGTH,
                                                                                config.FRAME_SHIFT,
                                                                                window=config.WINDOWS)))
                                 + np.spacing(1))
        else:
            feature_mix = np.abs(np.transpose(librosa.core.spectrum.stft(wav_mix, config.FRAME_LENGTH,
                                                                         config.FRAME_SHIFT,
                                                                         window=config.WINDOWS)))

        # 混叠的语谱图,用于masking输出 和 iSTFT变换时提取phase
        spec_mix = np.transpose(librosa.core.spectrum.stft(wav_mix, config.FRAME_LENGTH,
                                                           config.FRAME_SHIFT,
                                                           window=config.WINDOWS))

        # silence_mask = np.ones(spec_mix.shape)
        # if config.DB_THRESHOLD > 0:
        #     log_feature_mix = np.log10(np.abs(spec_mix))
        #     m_threshold = np.max(log_feature_mix) - config.DB_THRESHOLD/20.  # From dB to log10 power
        #     silence_mask[log_feature_mix < m_threshold] = 0.  # 这里的mask是为了把silence覆盖住,不参与模型的训练过程中

        # 加载到batch缓存中, 用于predict 模型预测
        batch_input_mix_fea.append(feature_mix)
        batch_input_mix_spec.append(np.abs(spec_mix))
        # batch_input_silence_mask.append(silence_mask)
        batch_input_spk.append(target_spk)
        # 加载到batch缓存中,用于预测语音的裁剪
        batch_input_len.append(mix_len)
        # 加载到batch缓存中,用于BSS_EVAL 评估
        batch_mix_spec.append(spec_mix)
        batch_mix_wav.append(wav_mix)
        batch_target_wav.append(target_sig)
        batch_noise_wav.append(noise_sig)

        batch_count += 1

        if (batch_count == batch_size) or (line_idx == (file_list_len-1)):
            # 混叠特征,mix_input_fea (batch_size, time_steps, feature_dim)
            _tmp_batch_size = len(batch_input_mix_fea)
            mix_input_fea = np.array(batch_input_mix_fea).reshape((_tmp_batch_size, ) + feature_mix.shape)
            # 混叠语谱,mix_input_spec (batch_size, time_steps, spectrum_dim)
            mix_input_spec = np.array(batch_input_mix_spec).reshape((_tmp_batch_size, ) + spec_mix.shape)
            # bg_input_mask = np.array(batch_input_silence_mask).reshape((_tmp_batch_size, ) + spec_mix.shape)
            # 目标说话人,target_input_spk (batch_size, 1)
            target_input_spk = np.array(batch_input_spk).reshape((_tmp_batch_size, 1))
            # 目标特征,clean_input_fea (batch_size, time_steps, feature_dim)
            batch_input_clean_fea, inp_clean_shape = compute_batch_clean_fea(batch_clean_wav)
            clean_input_fea = np.array(batch_input_clean_fea).reshape((batch_size, ) + inp_clean_shape)
            if not unk_spk:
                # 如果是unk_spk测试
                clean_input_fea = np.log(np.zeros_like(clean_input_fea)+np.spacing(1))
            # 进行预测
            target_pred = model.predict({'input_mix_feature': mix_input_fea, 'input_mix_spectrum': mix_input_spec,
                                         # 'input_bg_mask': bg_input_mask,
                                         'input_target_spk': target_input_spk, 'input_clean_feature': clean_input_fea})
            batch_idx = 0
            # 对预测结果进行评估
            for _pred_output in list(target_pred):
                _mix_spec = batch_mix_spec[batch_idx]
                phase_mix = np.angle(_mix_spec)
                _pred_spec = _pred_output * np.exp(1j * phase_mix)
                _pred_wav = librosa.core.spectrum.istft(np.transpose(_pred_spec), config.FRAME_SHIFT,
                                                        window=config.WINDOWS)
                _target_wav = batch_target_wav[batch_idx]
                # 进行语音长度裁剪
                min_len = np.min((len(_target_wav), len(_pred_wav), batch_input_len[batch_idx]))
                _pred_wav = _pred_wav[:min_len]
                batch_target_wav[batch_idx] = _target_wav[:min_len]
                batch_noise_wav[batch_idx] = batch_noise_wav[batch_idx][:min_len]
                batch_mix_wav[batch_idx] = batch_mix_wav[batch_idx][:min_len]

                # sf.write('test_mix_spk01_spk02.wav', batch_mix_wav[batch_idx], 8000)
                # sf.write('test_target_spk01.wav', batch_target_wav[batch_idx], 8000)
                # sf.write('test_target_spk02.wav', batch_noise_wav[batch_idx], 8000)
                # sf.write('test_pred_spk01.wav', _pred_wav, 8000)

                # 计算SDR, SAR, SIR(前面是true,后面是pred)
                mix_wav = matlab.double(batch_mix_wav[batch_idx].tolist())
                target_wav = matlab.double(batch_target_wav[batch_idx].tolist())
                noise_wav = matlab.double(batch_noise_wav[batch_idx].tolist())
                pred_wav = matlab.double(_pred_wav.tolist())
                if epoch_num == 0:
                    # BSS_EVAL (truth_signal, truth_noise, pred_signal, mix)
                    bss_eval_resuts = config.MAT_ENG.BSS_EVAL(target_wav, noise_wav, mix_wav, mix_wav)
                    batch_sdr_0.append(bss_eval_resuts['SDR'])
                    batch_sir_0.append(bss_eval_resuts['SIR'])
                    batch_sar_0.append(bss_eval_resuts['SAR'])
                    batch_nsdr_0.append(bss_eval_resuts['NSDR'])
                if (line_idx < _tmp_batch_size) and (batch_idx == 0):
                    # 把预测写到文件中
                    sf.write(config.TMP_PRED_WAV_FOLDER + '/test_pred_%s_ep%04d_bs%04d_idx%03d' %
                             (config.DATASET, (epoch_num+1), 1, (batch_idx+1)) +
                             '.wav', _pred_wav, config.FRAME_RATE)

                # BSS_EVAL (truth_signal, truth_noise, pred_signal, mix)
                bss_eval_resuts = config.MAT_ENG.BSS_EVAL(target_wav, noise_wav, pred_wav, mix_wav)
                batch_sdr.append(bss_eval_resuts['SDR'])
                batch_sir.append(bss_eval_resuts['SIR'])
                batch_sar.append(bss_eval_resuts['SAR'])
                batch_nsdr.append(bss_eval_resuts['NSDR'])
                batch_idx += 1

            time_end = time.time()
            # print '\rCurrent predict:' + str(line_idx+1) + ' of ' + audio_list + \
            #     ' and cost time: %.4f sec.' % (time_end - time_start),
            sdr = np.float(np.mean(batch_sdr))
            sir = np.float(np.mean(batch_sir))
            sar = np.float(np.mean(batch_sar))
            nsdr = np.float(np.mean(batch_nsdr))
            print '\rCurrent predict:' + str(line_idx+1) + ' of ' + audio_list + \
                ' and cost time: %.4f sec. - GSDR:%f, GSIR:%f, GSAR:%f, GNSDR:%f' % ((time_end - time_start),
                                                                                     sdr, sir, sar, nsdr),
            if (line_idx+1) % 200 == 0:
                log_file.write('Have evaluated %05d mixture wavs, and cost time: %.4f sec\n'
                               % ((line_idx+1), (time_end - time_start)))
                log_file.flush()
            batch_input_mix_fea = []
            batch_input_mix_spec = []
            # batch_input_silence_mask = []
            batch_input_spk = []
            batch_input_len = []  # 后面根据len进行输出语音裁剪
            batch_mix_spec = []
            batch_mix_wav = []
            batch_target_wav = []
            batch_noise_wav = []
            batch_clean_wav = []
            batch_count = 0

    if epoch_num == 0:
        sdr_0 = np.float(np.mean(batch_sdr_0))
        sir_0 = np.float(np.mean(batch_sir_0))
        sar_0 = np.float(np.mean(batch_sar_0))
        nsdr_0 = np.float(np.mean(batch_nsdr_0))
        print '\n[Epoch-%s: %d] - GSDR:%f, GSIR:%f, GSAR:%f, GNSDR:%f' % \
              (valid_test, epoch_num, sdr_0, sir_0, sar_0, nsdr_0)
        log_file.write('[Epoch-%s: %d] - GSDR:%f, GSIR:%f, GSAR:%f, GNSDR:%f\n' %
                       (valid_test, epoch_num, sdr_0, sir_0, sar_0, nsdr_0))
        log_file.flush()
    # else:
    #     print '\n'
    sdr = np.float(np.mean(batch_sdr))
    sir = np.float(np.mean(batch_sir))
    sar = np.float(np.mean(batch_sar))
    nsdr = np.float(np.mean(batch_nsdr))
    if epoch_num == 0:
        print '[Epoch-%s: %d] - GSDR:%f, GSIR:%f, GSAR:%f, GNSDR:%f' % \
              (valid_test, epoch_num+1, sdr, sir, sar, nsdr)
    else:
        print '\n[Epoch-%s: %d] - GSDR:%f, GSIR:%f, GSAR:%f, GNSDR:%f' % \
              (valid_test, epoch_num+1, sdr, sir, sar, nsdr)
    log_file.write('[Epoch-%s: %d] - GSDR:%f, GSIR:%f, GSAR:%f, GNSDR:%f\n' %
                   (valid_test, epoch_num+1, sdr, sir, sar, nsdr))
    log_file.flush()
    file_list.close()
Exemplo n.º 47
0
 elif filename == 'mRNA':
     # 众数
     # gamma_list = [0.25,1,2,32,1,0.03125,4]
     # 平均数
     gamma_list = [
         2.979166667, 1, 3.5625, 16.89930556, 2.045277778, 12.89583333,
         2.670138889
     ]
 feature_id = [
     1, 96, 96, 352, 352, 692, 692, 696, 696, 832, 832, 848, 848, 912
 ]
 average_pre_score, coverage_error_1, label_ranking_loss_1, ham_loss, zero_one_loss_1, \
  acc_score, predict_result, predict_result_scores, mean_weight, ap_std,acc_std,cov_std,rl_std,hl_std,zo_std, average_precision_score_list,\
         acc_score_list, coverage_error_list, ranking_loss, hamming_loss_list, zero_one_loss_list = kfold_cross_validation(feature, label, c, gamma_list)
 all_average_precision_score = eng.Average_precision(
     matlab.double(np.array(predict_result_scores).T.tolist()),
     matlab.double(label.T.tolist()))
 all_accuracy_score = eng.Accuracy(
     matlab.double(np.array(predict_result).T.tolist()),
     matlab.double(label.T.tolist()))
 all_result = []
 every_fold_result = []
 every_fold_result.append(average_precision_score_list)
 every_fold_result.append(acc_score_list)
 every_fold_result.append(coverage_error_list)
 every_fold_result.append(ranking_loss)
 every_fold_result.append(hamming_loss_list)
 every_fold_result.append(zero_one_loss_list)
 # every_fold_result = np.array(every_fold_result)
 df = pd.DataFrame(every_fold_result).T
 df.to_csv('Every_fold_' + filename + '.csv', header=None, index=None)
Exemplo n.º 48
0
def evlauate(pre_y, Y_test, pre_score_2):
    average_pre_score = eng.Average_precision(matlab.double(pre_score_2.T.tolist()), matlab.double(Y_test.T.tolist()))
    zero_one_loss_1 = eng.One_error(matlab.double(pre_score_2.T.tolist()), matlab.double(Y_test.T.tolist()))
    coverage_error_1 = eng.coverage(matlab.double(pre_score_2.T.tolist()), matlab.double(Y_test.T.tolist()))
    label_ranking_loss_1 = eng.Ranking_loss(matlab.double(pre_score_2.T.tolist()), matlab.double(Y_test.T.tolist()))
    ham_loss = eng.Hamming_loss(matlab.double(pre_y.T.tolist()), matlab.double(Y_test.T.tolist()))
    acc_score = eng.Accuracy(matlab.double(pre_y.T.tolist()), matlab.double(Y_test.T.tolist()))

    return average_pre_score,  coverage_error_1, label_ranking_loss_1, ham_loss, zero_one_loss_1, acc_score
Exemplo n.º 49
0
    def _convert_dict_dtypes(cls, dictionary):
        """
        takes in dictionary of numpy/python dtypes, and returns 
        dictionary of nearly equivalent MATLAB dtypes.

        type 'list' is currently unsupported if they are mixed dtype.
        (though this could be easily implemented using MATLAB cell,
         it would not work well in the context of FEM_VGPy)
        """
        dict_out = {}
        
        for key in dictionary.keys():
            # walk through all keys
            value = dictionary[key]

            # first if-elif ladder (initial checks)
            if '__' in (key[0:2], key[-2:]):
                # this is a python construct,
                # don't add it to the output.
                continue
            elif value is None:
                # this is a python construct,
                # don't add it to the output.
                continue
            elif type(value) is str:
                # matlab can automatically convert str to char
                dict_out[key] = value
                continue
            elif type(value) is bool:
                # matlab can automatically handle bool
                dict_out[key] = value
                continue
            elif type(value) is list:
                # attempt to convert this to a tuple. element
                # dtypes will be taken care of in 2nd ladder
                value = tuple(value)
            
            # second if-elif ladder (convert dtypes)
            if type(value) is tuple:
                # iterable type; convert to array
                if (type(value[0]) is int) or (type(value[0]) is numpy.int_):
                    dict_out[key] = matlab.int32(value)
                elif (type(value[0]) is float) or (type(value[0]) is numpy.float_):
                    dict_out[key] = matlab.double(value)

            elif type(value) is numpy.ndarray:
                # we want to convert to an equivalent matlab matrix...

                # convert data to equivalent list
                data_list = value.tolist()
               
                # change to matlab equivalent and save
                if (value.dtype == 'int32') or (value.dtype == 'int64') or (value.dtype == 'int'):
                    # for python 2.7, int64 is always treated as int32 in matlab.
                    dict_out[key] = matlab.int32(data_list)
                else:
                    dict_out[key] = matlab.double(data_list)
                
            elif type(value) is float:
                # matlab considers floats to be matrices as well.
                dict_out[key] = matlab.double([value])

            elif type(value) is int:
                # matlab considers ints to be matrices as well.
                dict_out[key] = matlab.int32([value])

            elif type(value) is dict:
                # use recursion to take care of this
                dict_out[key] = cls._convert_dict_dtypes(value)
                
            elif type(value) is unicode:
                # convert this to a string representation
                dict_out[key] = str(value)
                
            else:
                # undefined. alert user, then save as-is.
                # if there is a problem, matlab engine will
                # throw the proper exceptions
                print "\n!!! undefined type ",
                print type(value),
                print "... saving anyway\n"
                dict_out[key] = value
        
        return dict_out