def rodar(patterns, metodos, radical, sinais, occupancy, lock): u = Utiliters() info = u.setup_logger('information', radical + 'info.log') startedI = datetime.datetime.now() print('Started Nu Test with occupancy %d at %s' % (occupancy, startedI.strftime("%H:%M:%S %d/%m/%Y"))) info.info('Started Nu Test with occupancy %d' % occupancy) algo = Algorithms() matrizes = Matrizes() matrix = matrizes.matrix() samples = 1820 iterations = 331 for pattern in patterns: nome = radical + pattern + '_' + str(occupancy) bunch = pattern.rsplit('b', 1) empty = bunch[1].rsplit('e', 1) b = int(bunch[0]) e = int(empty[0]) halfA = e - int(math.ceil(e / 2)) fillAd = np.zeros(halfA) fillAe = np.zeros(e - halfA) try: signalT = np.genfromtxt(sinais + 'signalT_' + pattern + '_' + str(occupancy) + '.csv', delimiter=',') signalN = np.genfromtxt(sinais + 'signalN_' + pattern + '_' + str(occupancy) + '.csv', delimiter=',') except: print('Error get saved signals') signalT, signalN, signalTf, signalNf = u.sgen(pattern, samples, b, fillAd, fillAe, matrix, sinais) const = {'iterations': iterations, 'occupancy': occupancy, 'pattern': pattern, 'signalT': signalT, 'signalN': signalN} itens = np.arange(0) constantes = {} for i in range(1, 101): for j in range(1, 101): if i != j: aux = float('%.6g' % (i / j)) if not (aux in itens): itens = np.append(itens, aux) constantes.update({aux: str(i) + '#' + str(j)}) colunas = ['Nu', 'i', 'j'] for metodo in metodos: colunas.append(metodo + ':' + str(occupancy) + ':RMS') data = panda.DataFrame([], columns=colunas) for nu in itens: opt = {'nu': nu, 'mi': .25} row = [nu] row.extend(constantes.get(nu).split('#')) for metodo in metodos: const['metodo'] = metodo row.append(algo.getRMSfloat(const, opt)['rms']) l = len(data) data.loc[l] = row with lock: data.to_csv(nome + '.csv', index=False) ended = datetime.datetime.now() print('Ended Nu Test with occupancy %d at %s after %s' % ( occupancy, ended.strftime("%H:%M:%S %d/%m/%Y"), u.totalTime(startedI))) info.info('Ended Nu Test with occupancy %d after %s' % (occupancy, u.totalTime(startedI)))
def rodar(radical, algos, const, lambdas, occupancy, lock): algoritmo = Algorithms() nome = radical + str(occupancy) data = panda.DataFrame(lambdas, columns=['lambdas']) div = 10 opt = {'mi': .25, 'samples': 182} constant = const[occupancy] listN = np.split(constant['signalN'], div) listT = np.split(constant['signalT'], div) for algo in algos: started = datetime.now() print('Inicio %s ocupancia %d as %s' % (algo, occupancy, started.strftime("%H:%M:%S %d/%m/%Y"))) res = [] constant['metodo'] = algo for lamb in lambdas: rms = [] opt['lambda'] = lamb for i in range(div): signalN = np.asarray(listN[i]) singalT = np.asarray(listT[i]) constant['signalN'] = signalN constant['signalT'] = singalT rms.append(algoritmo.getRMSfloat(constant, opt)['rms']) std = np.asarray(rms, dtype=float) res.append(std.std()) tmp = panda.DataFrame(res, columns=[algo + ':' + str(occupancy) + ':std']) data = panda.concat([data, tmp], axis=1, sort=False) with lock: data.to_csv(nome + '.csv', index=False) ended = datetime.now() print('Fim %s ocupancia %d as %s' % (algo, occupancy, ended.strftime("%H:%M:%S %d/%m/%Y")))
def graphFDIP(self, occupancies, sO, eO, show=True, nome='', pattern='48b7e', path='./../tests/signals/'): fig = plt.figure(1, figsize=[6, 4.5], dpi=160, facecolor='w', edgecolor='k') colors = plt.cm.jet(np.linspace(0, 1, len(occupancies))) ax = fig.add_subplot(1, 1, 1) gerador = Signal() algo = Algorithms() minY, maxY = 10000, 0.000001 for occupancy in occupancies: try: signalT = np.genfromtxt(path + 'signalT_' + pattern + '_' + str(occupancy) + '.csv', delimiter=',') signalN = np.genfromtxt(path + 'signalN_' + pattern + '_' + str(occupancy) + '.csv', delimiter=',') signalTf = np.genfromtxt(path + 'fir/signalT_' + pattern + '_' + str(occupancy) + '.csv', delimiter=',') signalNf = np.genfromtxt(path + 'fir/signalN_' + pattern + '_' + str(occupancy) + '.csv', delimiter=',') except: print('Error get saved signals') dados = [] ordens = [] for order in range(sO, eO, 2): signalF = algo.FIR(order, signalNf, signalTf, signalN) dados.append(gerador.rms(signalF - signalT)) ordens.append(order) ax.plot(ordens, dados, label='Occupancy ' + str(occupancy), color=colors[occupancies.index(occupancy)]) minimum = min(dados) maximum = max(dados) minY = minimum if minY > minimum else minY maxY = maximum if maxY < maximum else maxY ax.plot(ordens[dados.index(minimum)], minimum, linestyle='none', marker='.', color='black') #ax.set_title('RMS error by constant value') # ax.set_xlabel('Constant value') ax.set_xlabel('Order') ax.set_ylabel('RMS Error distribution (ADC counts)') ax.tick_params(axis='both', which='major', labelsize=8) ax.tick_params(axis='both', which='minor', labelsize=0) ax.tick_params(which='both', direction='out') ax.grid(which='minor', alpha=0.3) ax.grid(which='major', alpha=0.7) minX, maxX = 2, 52 plt.subplots_adjust(left=0.15, right=0.98, top=0.99, bottom=0.1) # ax.legend(loc='upper left', ncol=1, shadow=True, fancybox=True) ax.set_xlim(minX - (maxX / 100), maxX + (maxX / 100)) ax.set_ylim(minY - (maxY / 100), maxY + (maxY / 100)) ax.set_xticks(np.arange(minX, maxX + (maxX / 100), maxX / 10)) ax.set_xticks(np.arange(minX, maxX + (maxX / 100), maxX / 50), minor=True) ax.set_yticks(np.arange(minY, maxY + (maxY / 100), maxY / 10)) ax.set_yticks(np.arange(minY, maxY + (maxY / 100), maxY / 50), minor=True) ax.xaxis.set_major_locator(MaxNLocator(nbins=15, integer=True)) if show: plt.show() plt.clf() plt.cla() plt.close() else: plt.savefig(nome + '.png') fig.clear() plt.close(fig)
def rodar(radical, algos, const, thresholdsAll, occupancy, lock): gerador = Signal() algoritmo = Algorithms() nome = radical + str(occupancy) data = panda.DataFrame([]) div = 10 opt = {'mi': .25, 'samples': 1820} constant = const[occupancy] listN = np.split(constant['signalN'], div) listT = np.split(constant['signalT'], div) for algo in algos: started = datetime.now() print('Inicio %s ocupancia %d as %s' % (algo, occupancy, started.strftime("%H:%M:%S %d/%m/%Y"))) res = [] constant['metodo'] = algo thresholds = thresholdsAll[algo + ':' + str(occupancy) + ':threshold'] for threshold in thresholds: rms = [] for i in range(div): if algo == 'FDIP': concatN = np.asarray([]) concatT = np.asarray([]) for j in range(div): if i != j: concatN = np.concatenate( (concatN, np.asarray(listN[j]))) concatT = np.concatenate( (concatT, np.asarray(listT[j]))) constant['signalNf'] = concatN constant['signalTf'] = concatT signalN = np.asarray(listN[i]) singalT = np.asarray(listT[i]) constant['signalN'] = signalN constant['signalT'] = singalT rms.append( gerador.roc(algoritmo.getRMSfloat(constant, opt)['signal'], singalT, threshold=threshold)['RMS'][0]) std = np.asarray(rms) res.append(std.std()) tmp = panda.DataFrame(res, columns=[algo + ':' + str(occupancy) + ':std']) data = panda.concat([data, tmp], axis=1, sort=False) with lock: data.to_csv(nome + '.csv', index=False) ended = datetime.now() print('Fim %s ocupancia %d as %s' % (algo, occupancy, ended.strftime("%H:%M:%S %d/%m/%Y")))
def rodarA(radical, algos, const, signalTrainT, signalTrainN, thresholdsAll, occupancy, lock): gerador = Signal() algoritmo = Algorithms() signalT = const[occupancy]['signalT'] nome = radical + '_' + str(occupancy) data = panda.DataFrame([]) for algo in algos: started = datetime.now() print('Inicio %s ocupancia %d as %s' % (algo, occupancy, started.strftime("%H:%M:%S %d/%m/%Y"))) res = [] const[occupancy]['metodo'] = algo thresholds = thresholdsAll[algo + ':' + str(occupancy) + ':threshold'] for threshold in thresholds: rms = [] for idx in range(10): if algo == 'FDIP': const[occupancy]['signalTf'] = signalTrainT[occupancy][idx] const[occupancy]['signalNf'] = signalTrainN[occupancy][idx] rms.append( gerador.roc(algoritmo.getRMSfloat( const[occupancy])['signal'], signalT, threshold=threshold)['RMS'][0]) else: const[occupancy]['signalT'] = signalTrainT[occupancy][idx] const[occupancy]['signalN'] = signalTrainN[occupancy][idx] rms.append( gerador.roc(algoritmo.getRMSfloat( const[occupancy])['signal'], signalTrainT[occupancy][idx], threshold=threshold)['RMS'][0]) std = np.asarray(rms) res.append(std.std()) tmp = panda.DataFrame(res, columns=[algo + ':' + str(occupancy) + ':std']) data = panda.concat([data, tmp], axis=1, sort=False) with lock: data.to_csv(nome + '.csv', index=False) ended = datetime.now() print('Fim %s ocupancia %d as %s' % (algo, occupancy, ended.strftime("%H:%M:%S %d/%m/%Y")))
def rodar(patterns, metodos, conf, radical, sinais, occupancy, lock): u = Utiliters() info = u.setup_logger('information', radical + 'info.log') startedI = datetime.datetime.now() print('Started Lambda Test with occupancy %d at %s' % (occupancy, startedI.strftime("%H:%M:%S %d/%m/%Y"))) info.info('Started Lambda Test with occupancy %d' % occupancy) algo = Algorithms() matrizes = Matrizes() matrix = matrizes.matrix() samples = 1820 iterations = conf['iterations'] for pattern in patterns: nome = radical + pattern + '_' + str(occupancy) bunch = pattern.rsplit('b', 1) empty = bunch[1].rsplit('e', 1) b = int(bunch[0]) e = int(empty[0]) halfA = e - int(math.ceil(e / 2)) fillAd = np.zeros(halfA) fillAe = np.zeros(e - halfA) try: signalT = np.genfromtxt(sinais + 'signalT_' + pattern + '_' + str(occupancy) + '.csv', delimiter=',') signalN = np.genfromtxt(sinais + 'signalN_' + pattern + '_' + str(occupancy) + '.csv', delimiter=',') except: print('Error get saved signals') signalT, signalN, signalTf, signalNf = u.sgen( pattern, samples, b, fillAd, fillAe, matrix, sinais) const = { 'iterations': iterations, 'occupancy': occupancy, 'pattern': pattern, 'signalT': signalT, 'signalN': signalN } colunas = ['Lambda'] for metodo in metodos: for ite in range(1, iterations): colunas.append(metodo + ':' + str(occupancy) + ':RMS:' + str(ite)) data = panda.DataFrame([], columns=colunas) for lam in range(conf['sL'], conf['eL']): if lam != 0: lamb = lam / 10 else: lamb = 0 opt = {'lambda': lamb} row = [lamb] for metodo in metodos: const['metodo'] = metodo for ite in range(1, iterations): const['iterations'] = ite row.append(algo.getRMSfloat(const, opt)['rms']) l = len(data) data.loc[l] = row with lock: data.to_csv(nome + '.csv', index=False) ended = datetime.datetime.now() print('Ended Lambda Test with occupancy %d at %s after %s' % (occupancy, ended.strftime("%H:%M:%S %d/%m/%Y"), u.totalTime(startedI))) info.info('Ended Lambda Test with occupancy %d after %s' % (occupancy, u.totalTime(startedI)))
def rodar(patterns, radical, sinais, occupancy, lock, metodos, load=None): u = Utiliters() info = u.setup_logger('information', radical + 'info.log') startedI = datetime.now() print('Started ROC generate for occupancy %d at %s' % (occupancy, startedI.strftime("%H:%M:%S %d/%m/%Y"))) info.info('Started ROC generate for occupancy %d' % (occupancy)) algo = Algorithms() gerador = Signal() matrizes = Matrizes() matrix = matrizes.matrix() samples = 1820 for pattern in patterns: if load is None: data = panda.DataFrame([]) nome = radical + pattern + '_' + str(occupancy) + '.csv' else: nome = load + pattern + '_' + str(occupancy) + '.csv' data = panda.read_csv(nome) bunch = pattern.rsplit('b', 1) empty = bunch[1].rsplit('e', 1) b = int(bunch[0]) e = int(empty[0]) bwindow = b + 6 window = b + e halfA = e - int(math.ceil(e / 2)) halfCd = int(math.ceil((bwindow - window) / 2)) halfCe = int(bwindow - window - halfCd) if halfCd > 0: fillCd = np.zeros(halfCd) else: fillCd = np.arange(0) if halfCe > 0: fillCe = np.zeros(halfCe) else: fillCe = np.arange(0) fillAd = np.zeros(halfA) fillAe = np.zeros(e - halfA) fill = [fillAd, fillAe, fillCd, fillCe] H, A, B = matrizes.generate(b) h = matrix[0:7, 5] try: signalT = np.genfromtxt(sinais + 'signalT_' + pattern + '_' + str(occupancy) + '.csv', delimiter=',') signalN = np.genfromtxt(sinais + 'signalN_' + pattern + '_' + str(occupancy) + '.csv', delimiter=',') signalTf = np.genfromtxt(sinais + 'fir/signalT_' + pattern + '_' + str(occupancy) + '.csv', delimiter=',') signalNf = np.genfromtxt(sinais + 'fir/signalN_' + pattern + '_' + str(occupancy) + '.csv', delimiter=',') except: print('Error get saved signals') signalT, signalN, signalTf, signalNf = u.sgen( pattern, samples, b, fillAd, fillAe, matrix, sinais) const = { 'iterations': 331, 'occupancy': occupancy, 'pattern': pattern, 'signalT': signalT, 'signalN': signalN } opt = {'samples': samples} nnzST = np.count_nonzero(signalT) nzST = len(signalT) - nnzST pike = int(np.max(signalN) + 1) if ('FDIP' in metodos) or ('MF' in metodos): started = datetime.now() print('Started ROC generate for old with occupancy %d at %s' % (occupancy, started.strftime("%H:%M:%S %d/%m/%Y"))) info.info('Started ROC generate for old with occupancy %d' % (occupancy)) metodo = 'FDIP' if metodo in metodos: const['metodo'] = metodo constf = const constf['signalNf'] = signalNf constf['signalTf'] = signalTf roc = gerador.roc(algo.getRMSfloat(constf)['signal'], signalT) roc = roc.rename( columns={ s: metodo + ':' + str(occupancy) + ':' + s for s in list(roc.columns.values) }) data = panda.concat([data, roc], axis=1, sort=False) with lock: data.to_csv(nome, index=False) metodo = 'MF' if metodo in metodos: signalMf, roc = algo.MatchedFw_roc(signalN, h, samples, b, e, fill, signalT) roc = roc.rename( columns={ s: 'MF:' + str(occupancy) + ':' + s for s in list(roc.columns.values) }) data = panda.concat([data, roc], axis=1, sort=False) with lock: data.to_csv(nome, index=False) ended = datetime.now() print( 'Ended ROC generate for old with occupancy %d at %s after %s' % (occupancy, ended.strftime("%H:%M:%S %d/%m/%Y"), u.totalTime(started))) info.info('Ended ROC generate for old with occupancy %d after %s' % (occupancy, u.totalTime(started))) if (' MF ' in metodos) or ('OMP ' in metodos) or ('LS-OMP' in metodos): started = datetime.now() print('Started ROC generate for Greedy with occupancy %d at %s' % (occupancy, started.strftime("%H:%M:%S %d/%m/%Y"))) info.info('Started ROC generate for Greedy with occupancy %d' % (occupancy)) metodo = ' MP ' if metodo in metodos: threshold, pdr, far = 0, 5, 5 res = [] while (float('{:.2f}'.format(pdr)) > 0.01) or (float( '{:.2f}'.format(far)) > 0.01): faA, pdA = 0, 0 signalA = np.zeros(window * samples) for i in range(samples): step = (i * window) paso = step + window if (e > 6): paso = paso - (e - 6) signalTw = np.concatenate( (fillCd, signalT[step:paso], fillCe)) signalSw = np.concatenate( (fillCd, signalN[step:paso], fillCe)) step += halfA paso = step + b x, fa, pd = algo.MP_roc(threshold, signalSw, signalTw, b, H) signalA[step:paso] = x faA += fa pdA += pd far = (faA / nzST) pdr = (pdA / nnzST) tmp = '%d,%.6f,%.6f,%.6f' % ( threshold, gerador.rms(signalA - signalT), far, pdr) if threshold < pike: threshold += 1 else: if threshold == pike: threshold = math.ceil(pike / 100) * 100 else: threshold += 100 res.append([float(s) for s in tmp.split(',')]) roc = panda.DataFrame( res, columns=[ 'MP:' + str(occupancy) + ':threshold', 'MP:' + str(occupancy) + ':RMS', 'MP:' + str(occupancy) + ':FA', 'MP:' + str(occupancy) + ':DP' ]) data = panda.concat([data, roc], axis=1, sort=False) with lock: data.to_csv(nome, index=False) metodo = 'OMP ' if metodo in metodos: threshold, pdr, far = 0, 5, 5 res = [] while (float('{:.2f}'.format(pdr)) > 0.01) or (float( '{:.2f}'.format(far)) > 0.01): faA, pdA = 0, 0 signalA = np.zeros(window * samples) for i in range(samples): step = (i * window) paso = step + window if (e > 6): paso = paso - (e - 6) signalTw = np.concatenate( (fillCd, signalT[step:paso], fillCe)) signalSw = np.concatenate( (fillCd, signalN[step:paso], fillCe)) step += halfA paso = step + b x, fa, pd = algo.OMP_roc(threshold, signalSw, signalTw, b, H) signalA[step:paso] = x faA += fa pdA += pd far = (faA / nzST) pdr = (pdA / nnzST) tmp = '%d,%.6f,%.6f,%.6f' % ( threshold, gerador.rms(signalA - signalT), far, pdr) if (threshold < pike): threshold += 1 else: if threshold == pike: threshold = math.ceil(pike / 100) * 100 else: threshold += 100 res.append([float(s) for s in tmp.split(',')]) roc = panda.DataFrame( res, columns=[ 'OMP:' + str(occupancy) + ':threshold', 'OMP:' + str(occupancy) + ':RMS', 'OMP:' + str(occupancy) + ':FA', 'OMP:' + str(occupancy) + ':DP' ]) data = panda.concat([data, roc], axis=1, sort=False) with lock: data.to_csv(nome, index=False) metodo = 'LS-OMP' if metodo in metodos: threshold, pdr, far = 0, 5, 5 res = [] while (float('{:.2f}'.format(pdr)) > 0.01) or (float( '{:.2f}'.format(far)) > 0.01): faA, pdA = 0, 0 signalA = np.zeros(window * samples) for i in range(samples): step = (i * window) paso = step + window if (e > 6): paso = paso - (e - 6) signalTw = np.concatenate( (fillCd, signalT[step:paso], fillCe)) signalSw = np.concatenate( (fillCd, signalN[step:paso], fillCe)) step += halfA paso = step + b x, fa, pd = algo.LS_OMP_roc(threshold, signalSw, signalTw, b, H) signalA[step:paso] = x faA += fa pdA += pd far = (faA / nzST) pdr = (pdA / nnzST) tmp = '%d,%.6f,%.6f,%.6f' % ( threshold, gerador.rms(signalA - signalT), far, pdr) if (threshold < pike): threshold += 1 else: if threshold == pike: threshold = math.ceil(pike / 100) * 100 else: threshold += 100 res.append([float(s) for s in tmp.split(',')]) roc = panda.DataFrame( res, columns=[ 'LS-OMP:' + str(occupancy) + ':threshold', 'LS-OMP:' + str(occupancy) + ':RMS', 'LS-OMP:' + str(occupancy) + ':FA', 'LS-OMP:' + str(occupancy) + ':DP' ]) data = panda.concat([data, roc], axis=1, sort=False) with lock: data.to_csv(nome, index=False) ended = datetime.now() print( 'Ended ROC generate for Greedy with occupancy %d at %s after %s' % (occupancy, ended.strftime("%H:%M:%S %d/%m/%Y"), u.totalTime(started))) info.info( 'Ended ROC generate for Greedy with occupancy %d after %s' % (occupancy, u.totalTime(started))) started = datetime.now() print('Started ROC generate for Shrinkage with occupancy %d at %s' % (occupancy, started.strftime("%H:%M:%S %d/%m/%Y"))) info.info('Started ROC generate for Shrinkage with occupancy %d' % (occupancy)) metodo = 'DS' if metodo in metodos: const['metodo'] = metodo roc = gerador.roc(algo.getRMSfloat(const, opt)['signal'], signalT) roc = roc.rename( columns={ s: metodo + ':' + str(occupancy) + ':' + s for s in list(roc.columns.values) }) data = panda.concat([data, roc], axis=1, sort=False) with lock: data.to_csv(nome, index=False) metodo = 'GD ' if metodo in metodos: metodo = metodo.strip() const['metodo'] = metodo roc = gerador.roc(algo.getRMSfloat(const, opt)['signal'], signalT) roc = roc.rename( columns={ s: metodo + ':' + str(occupancy) + ':' + s for s in list(roc.columns.values) }) data = panda.concat([data, roc], axis=1, sort=False) with lock: data.to_csv(nome, index=False) metodo = 'GDi' if metodo in metodos: metodo = metodo.strip() const['metodo'] = metodo roc = gerador.roc(algo.getRMSfloat(const, opt)['signal'], signalT) roc = roc.rename( columns={ s: metodo + ':' + str(occupancy) + ':' + s for s in list(roc.columns.values) }) data = panda.concat([data, roc], axis=1, sort=False) with lock: data.to_csv(nome, index=False) metodo = 'SSF ' if metodo in metodos: metodo = metodo.strip() const['metodo'] = metodo opt['mi'] = .25 roc = gerador.roc(algo.getRMSfloat(const, opt)['signal'], signalT) roc = roc.rename( columns={ s: metodo + ':' + str(occupancy) + ':' + s for s in list(roc.columns.values) }) data = panda.concat([data, roc], axis=1, sort=False) with lock: data.to_csv(nome, index=False) metodo = 'SSFi' if metodo in metodos: metodo = metodo.strip() const['metodo'] = metodo opt['mi'] = .25 roc = gerador.roc(algo.getRMSfloat(const, opt)['signal'], signalT) roc = roc.rename( columns={ s: metodo + ':' + str(occupancy) + ':' + s for s in list(roc.columns.values) }) data = panda.concat([data, roc], axis=1, sort=False) with lock: data.to_csv(nome, index=False) metodo = 'SSFls ' if metodo in metodos: metodo = metodo.strip() const['metodo'] = metodo opt['mi'] = .25 roc = gerador.roc(algo.getRMSfloat(const, opt)['signal'], signalT) roc = roc.rename( columns={ s: metodo + ':' + str(occupancy) + ':' + s for s in list(roc.columns.values) }) data = panda.concat([data, roc], axis=1, sort=False) with lock: data.to_csv(nome, index=False) metodo = 'SSFlsi' if metodo in metodos: metodo = metodo.strip() const['metodo'] = metodo opt['mi'] = .25 roc = gerador.roc(algo.getRMSfloat(const, opt)['signal'], signalT) roc = roc.rename( columns={ s: metodo + ':' + str(occupancy) + ':' + s for s in list(roc.columns.values) }) data = panda.concat([data, roc], axis=1, sort=False) with lock: data.to_csv(nome, index=False) metodo = 'SSFlsc ' if metodo in metodos: metodo = metodo.strip() const['metodo'] = metodo opt['mi'] = .25 roc = gerador.roc(algo.getRMSfloat(const, opt)['signal'], signalT) roc = roc.rename( columns={ s: metodo + ':' + str(occupancy) + ':' + s for s in list(roc.columns.values) }) data = panda.concat([data, roc], axis=1, sort=False) with lock: data.to_csv(nome, index=False) metodo = 'SSFlsci' if metodo in metodos: metodo = metodo.strip() const['metodo'] = metodo opt['mi'] = .25 roc = gerador.roc(algo.getRMSfloat(const, opt)['signal'], signalT) roc = roc.rename( columns={ s: metodo + ':' + str(occupancy) + ':' + s for s in list(roc.columns.values) }) data = panda.concat([data, roc], axis=1, sort=False) with lock: data.to_csv(nome, index=False) metodo = 'PCD ' if metodo in metodos: metodo = metodo.strip() const['metodo'] = metodo opt['mi'] = math.inf roc = gerador.roc(algo.getRMSfloat(const, opt)['signal'], signalT) roc = roc.rename( columns={ s: metodo + ':' + str(occupancy) + ':' + s for s in list(roc.columns.values) }) data = panda.concat([data, roc], axis=1, sort=False) with lock: data.to_csv(nome, index=False) metodo = 'PCDi' if metodo in metodos: metodo = metodo.strip() const['metodo'] = metodo opt['mi'] = math.inf roc = gerador.roc(algo.getRMSfloat(const, opt)['signal'], signalT) roc = roc.rename( columns={ s: metodo + ':' + str(occupancy) + ':' + s for s in list(roc.columns.values) }) data = panda.concat([data, roc], axis=1, sort=False) with lock: data.to_csv(nome, index=False) ended = datetime.now() print( 'Ended ROC generate for Shrinkage with occupancy %d at %s after %s' % (occupancy, ended.strftime("%H:%M:%S %d/%m/%Y"), u.totalTime(started))) info.info( 'Ended ROC generate for Shrinkage with occupancy %d after %s' % (occupancy, u.totalTime(started))) ended = datetime.now() print('Ended ROC generate for occupancy %d at %s after %s' % (occupancy, ended.strftime("%H:%M:%S %d/%m/%Y"), u.totalTime(startedI))) info.info('Ended ROC generate for occupancy %d after %s' % (occupancy, u.totalTime(startedI)))
def dataGenerate(self, occupancy, lock): algorithm = Algorithms() generator = Signal() const = self.const methods = const['methods'] iterations = const['iterations'] b = const['b'] e = const['e'] h = const['h'] H = const['H'] A = const['A'] B = const['B'] fill = const['fill'] fillAd, fillAe, fillCd, fillCe = fill halfA = len(fillAd) matrix = const['matrix'] window = const['window'] totalSamples = const['totalSamples'] signalT, signalN = generator.signalGenerator(totalSamples, b, fillAd, fillAe, matrix, exp_mean=occupancy) signalTf, signalNf = generator.signalGenerator(totalSamples, b, fillAd, fillAe, matrix, exp_mean=occupancy) nnzST = np.count_nonzero(signalT) nzST = len(signalT) - nnzST pike = int(np.max(signalN) + 1) for method in methods: if 'FIR' in method: signalA = algorithm.FIR(26, signalNf, signalTf, signalN) data = generator.roc(signalA, signalT) data = data.rename(columns={s: 'FIR:' + str(occupancy) + ':' + s for s in list(data.columns.values)}) with lock: self.addData(data) elif 'MF' in method: signalMf, data = algorithm.MatchedFw_roc(signalN, h, totalSamples, b, e, fill, signalT) data = data.rename(columns={s: 'MF:' + str(occupancy) + ':' + s for s in list(data.columns.values)}) with lock: self.addData(data) elif 'MP' in method: threshold, pdr, far = 0, 5, 5 res = [] while (float('{:.2f}'.format(pdr)) > 0.01) or (float('{:.2f}'.format(far)) > 0.01): faA, pdA = 0, 0 signalA = np.zeros(window * totalSamples) for i in range(totalSamples): step = (i * window) pace = step + window if (e > 6): pace = pace - (e - 6) signalTw = np.concatenate((fillCd, signalT[step:pace], fillCe)) signalSw = np.concatenate((fillCd, signalN[step:pace], fillCe)) step += halfA pace = step + b if 'LS-OMP' in method: x, fa, pd = algorithm.LS_OMP_roc(threshold, signalSw, signalTw, b, H) elif 'OMP' in method: x, fa, pd = algorithm.OMP_roc(threshold, signalSw, signalTw, b, H) elif 'MP' in method: x, fa, pd = algorithm.MP_roc(threshold, signalSw, signalTw, b, H) signalA[step:pace] = x faA += fa pdA += pd far = (faA / nzST) pdr = (pdA / nnzST) tmp = '%d,%.6f,%.6f,%.6f' % (threshold, generator.rms(signalA - signalT), far, pdr) if threshold < pike: threshold += 1 else: if threshold == pike: threshold = math.ceil(pike / 100) * 100 else: threshold += 100 res.append([float(s) for s in tmp.split(',')]) data = panda.DataFrame(res, columns=[method + ':' + str(occupancy) + ':threshold', method + ':' + str(occupancy) + ':RMS', method + ':' + str(occupancy) + ':FA', method + ':' + str(occupancy) + ':DP']) with lock: self.addData(data) else: signalA = np.zeros(window * totalSamples) mi = 0.25 lam = 0.0 for i in range(totalSamples): step = (i * window) pace = step + window if (e > 6): pace = pace - (e - 6) signalS = np.concatenate((fillCd, signalN[step:pace], fillCe)) step += halfA pace = step + b xAll = signalS[3:b + 3] Hs = H.T.dot(signalS) if 'i' in method: x = B.dot(signalS) else: x = xAll for it in range(iterations): if 'GD' in method: x = algorithm.GD(x, Hs, A, mi) elif 'SSF' in method: x = algorithm.SSF(x, Hs, A, mi, lam) elif 'PCD' in method: x = algorithm.PCD(x, Hs, A, mi, lam) elif 'TAS' in method: x = algorithm.TAS(x, Hs, A, mi, lam) signalA[step:pace] = x data = generator.roc(signalA, signalT) data = data.rename(columns={s: method + ':' + str(occupancy) + ':' + s for s in list(data.columns.values)}) with lock: self.addData(data)
iteration, muI, lamb, quantization, gain, constant, path=path) optV = { 'samples': 1820, 'mi': .25, 'gain': gain, 'bitsH': quantization } verilog.generate() algorithm = Algorithms() hard = XbYe() algorithmv = hard.getAlgo([pattern]) try: path = './../tests/signals/' signalT = np.genfromtxt(path + 'signalT_' + pattern + '_30.csv', delimiter=',') signalN = np.genfromtxt(path + 'signalN_' + pattern + '_30.csv', delimiter=',') signalTf = np.genfromtxt(path + 'fir/signalT_' + pattern + '_30.csv', delimiter=',') signalNf = np.genfromtxt(path + 'fir/signalN_' + pattern + '_30.csv', delimiter=',') except: print('Error ao carregar os sinais') const = {
def rodar(patterns, metodos, radical, sinais, occupancy, lock): u = Utiliters() info = u.setup_logger('information', radical + 'info.log') startedI = datetime.datetime.now() print('Started Mi full Test with occupancy %d at %s' % (occupancy, startedI.strftime("%H:%M:%S %d/%m/%Y"))) info.info('Started Mi full Test with occupancy %d' % occupancy) algo = Algorithms() matrizes = Matrizes() samples = 1820 iterations = 331 for pattern in patterns: nome = radical + pattern + '_' + str(occupancy) bunch = pattern.rsplit('b', 1) empty = bunch[1].rsplit('e', 1) b = int(bunch[0]) e = int(empty[0]) u = Utiliters() bwindow = b + 6 window = b + e halfA = e - int(math.ceil(e / 2)) halfCd = int(math.ceil((bwindow - window) / 2)) halfCe = int(bwindow - window - halfCd) fillAd = np.zeros(halfA) fillAe = np.zeros(e - halfA) if halfCd > 0: fillCd = np.zeros(halfCd) else: fillCd = np.arange(0) if halfCe > 0: fillCe = np.zeros(halfCe) else: fillCe = np.arange(0) H, A, B = matrizes.generate(b) matrix = matrizes.matrix() try: signalN = np.genfromtxt(sinais + 'signalN_' + pattern + '_' + str(occupancy) + '.csv', delimiter=',') except: print('Error get saved signals') signalT, signalN, signalTf, signalNf = u.sgen( pattern, samples, b, fillAd, fillAe, matrix, sinais) colunas = ['Window'] for metodo in metodos: for ite in range(1, iterations): colunas.append(metodo + ':' + str(occupancy) + ':mu:' + str(ite)) data = panda.DataFrame([], columns=colunas) for its in range(samples): step = (its * window) paso = step + window if (e > 6): paso = paso - (e - 6) signalS = np.concatenate((fillCd, signalN[step:paso], fillCe)) step += halfA Hs = H.T.dot(signalS) row = [its] for metodo in metodos: musL = [] for ite in range(1, iterations): if 'i' in metodo: x = B.dot(signalS) else: x = signalS[3:b + 3] muA, mu = 0.0, 0.0 for i in range(ite): if 'GDP' in metodo: x, mu = algo.GDP(x, Hs, A, returnMu=True) elif 'GD' in metodo: x, mu = algo.GD(x, Hs, A, returnMu=True) elif 'SSFlsc' in metodo: x, mu = algo.SSFlsc(x, Hs, A, returnMu=True) elif 'SSFls' in metodo: x, mu = algo.SSFls(x, Hs, A, returnMu=True) elif 'SSF' in metodo: x, mu = algo.SSF(x, Hs, A, returnMu=True) elif 'PCD' in metodo: x, mu = algo.PCD(x, Hs, A, returnMu=True) muA += mu musL.append('%.6f' % (muA / ite)) row.extend(musL) l = len(data) data.loc[l] = row with lock: data.to_csv(nome + '.csv', index=False) ended = datetime.datetime.now() print('Ended Mi full Test with occupancy %d at %s after %s' % (occupancy, ended.strftime("%H:%M:%S %d/%m/%Y"), u.totalTime(startedI))) info.info('Ended Mi full Test with occupancy %d after %s' % (occupancy, u.totalTime(startedI)))
ax3.hist(signalC - signalT, orientation=u'horizontal') # ax3.set_xlim(-1, 201) ax3.set_ylim(minY2 - (maxY2 / 100), maxY2 + (maxY2 / 100)) ax3.tick_params(axis='both', which='major', labelsize=8) ax3.set_yticklabels([]) ax3.set_xlabel('Histogram') # plt.show() plt.savefig('../graphics/results/' + label + '.png') fig.clear() plt.close(fig) if __name__ == '__main__': matrizes = Matrizes() algo = Algorithms() gerador = Signal() util = Utiliters() pattern = '8b4e' totalSamples = 20 bunch = pattern.rsplit('b', 1) empty = bunch[1].rsplit('e', 1) b = int(bunch[0]) e = int(empty[0]) bwindow = b + 6 window = b + e halfA = e - int(math.ceil(e / 2)) halfCd = int(math.ceil((bwindow - window) / 2)) halfCe = int(bwindow - window - halfCd) fillAd = np.zeros(halfA) fillAe = np.zeros(e - halfA)
def rodar(patterns, metodos, radical, sinais, occupancy, lock): u = Utiliters() info = u.setup_logger('information', radical + 'info.log') startedI = datetime.datetime.now() print('Started B Fator Test with occupancy %d at %s' % (occupancy, startedI.strftime("%H:%M:%S %d/%m/%Y"))) info.info('Started B Fator Test with occupancy %d' % occupancy) algo = Algorithms() matrizes = Matrizes() matrix = matrizes.matrix() samples = 1820 iterations = 331 for pattern in patterns: nome = radical + pattern + '_' + str(occupancy) bunch = pattern.rsplit('b', 1) empty = bunch[1].rsplit('e', 1) b = int(bunch[0]) e = int(empty[0]) halfA = e - int(math.ceil(e / 2)) fillAd = np.zeros(halfA) fillAe = np.zeros(e - halfA) try: signalT = np.genfromtxt(sinais + 'signalT_' + pattern + '_' + str(occupancy) + '.csv', delimiter=',') signalN = np.genfromtxt(sinais + 'signalN_' + pattern + '_' + str(occupancy) + '.csv', delimiter=',') except: print('Error get saved signals') signalT, signalN, signalTf, signalNf = u.sgen( pattern, samples, b, fillAd, fillAe, matrix, sinais) const = { 'iterations': iterations, 'occupancy': occupancy, 'pattern': pattern, 'signalT': signalT, 'signalN': signalN } # H, A, B = matrizes.generate(b, 0) # s, e = int(round(max(B.min(), B.max(), key=abs)*1000)), int(round(min(B.min(), B.max(), key=abs)*1000)-1) fatores = [ 2.38, 2.34, 2.3, 2.22, 2.06, 1.62, 1.46, 1.42, 1.4, 1.3, 1.22, 1.14, 1.02, 0.9, 0.86, 0.74, 0.7, 0.66, 0.62, 0.58, 0.54, 0.5, 0.46, 0.38, 0.34, 0.3, 0.26, 0.22, 0.18, 0.14, 0.1, 0.06, 0.02, 0.006, 0.005, 0.004, 0.003, 0.002, 0.001, 0 ] elementos = { 2.38: 0, 2.34: 38, 2.3: 40, 2.22: 42, 2.06: 44, 1.62: 46, 1.46: 119, 1.42: 125, 1.4: 127, 1.3: 130, 1.22: 132, 1.14: 134, 1.02: 136, 0.9: 140, 0.86: 180, 0.74: 187, 0.7: 221, 0.66: 227, 0.62: 230, 0.58: 233, 0.54: 235, 0.5: 275, 0.46: 278, 0.38: 310, 0.34: 320, 0.3: 363, 0.26: 369, 0.22: 409, 0.18: 452, 0.14: 494, 0.1: 551, 0.06: 638, 0.02: 816, 0.006: 986, 0.005: 1050, 0.004: 1062, 0.003: 1128, 0.002: 1148, 0.001: 1280, 0: 2592 } # te = [np.count_nonzero(B)] # for i in range(s, e, -1): # aux = i / 1000 # aaux = abs(aux) # H, A, B = matrizes.generate(b, aaux) # tmp = np.count_nonzero(B) # if not tmp in te: # fatores.append(aaux) # elementos[aaux] = tmp # te.append(tmp) fatores = sorted(np.abs(fatores)) colunas = ['Fator', 'Elementos'] for metodo in metodos: for ite in range(1, iterations): colunas.append(metodo + ':' + str(occupancy) + ':RMS:' + str(ite)) data = panda.DataFrame([], columns=colunas) for fator in fatores: opt = {'maxB': fator, 'mi': .25} row = ['%.3f' % fator] row.append(elementos[fator]) for metodo in metodos: const['metodo'] = metodo for ite in range(1, iterations): const['iterations'] = ite row.append(algo.getRMSfloat(const, opt)['rms']) l = len(data) data.loc[l] = row with lock: data.to_csv(nome + '.csv', index=False) ended = datetime.datetime.now() print('Ended B Fator Test with occupancy %d at %s after %s' % (occupancy, ended.strftime("%H:%M:%S %d/%m/%Y"), u.totalTime(startedI))) info.info('Ended B Fator Test with occupancy %d after %s' % (occupancy, u.totalTime(startedI)))
def rodar(patterns, metodos, conf, radical, sinais, occupancy, lock): u = Utiliters() info = u.setup_logger('information', radical + 'info.log') startedI = datetime.datetime.now() print('Started Mi Test with occupancy %d at %s' % (occupancy, startedI.strftime("%H:%M:%S %d/%m/%Y"))) info.info('Started Mi Test with occupancy %d' % occupancy) algo = Algorithms() matrizes = Matrizes() matrix = matrizes.matrix() samples = 1820 for pattern in patterns: nome = radical + pattern + '_' + str(occupancy) bunch = pattern.rsplit('b', 1) empty = bunch[1].rsplit('e', 1) b = int(bunch[0]) e = int(empty[0]) halfA = e - int(math.ceil(e / 2)) fillAd = np.zeros(halfA) fillAe = np.zeros(e - halfA) try: signalT = np.genfromtxt(sinais + 'signalT_' + pattern + '_' + str(occupancy) + '.csv', delimiter=',') signalN = np.genfromtxt(sinais + 'signalN_' + pattern + '_' + str(occupancy) + '.csv', delimiter=',') signalTf = np.genfromtxt(sinais + 'fir/signalT_' + pattern + '_' + str(occupancy) + '.csv', delimiter=',') signalNf = np.genfromtxt(sinais + 'fir/signalN_' + pattern + '_' + str(occupancy) + '.csv', delimiter=',') except: print('Error get saved signals') signalT, signalN, signalTf, signalNf = u.sgen( pattern, samples, b, fillAd, fillAe, matrix, sinais) const = { 'iterations': conf['iterations'], 'occupancy': occupancy, 'pattern': pattern, 'signalT': signalT, 'signalN': signalN } const['metodo'] = 'FDIP' constf = const constf['signalNf'] = signalNf constf['signalTf'] = signalTf rms = algo.getRMSfloat(constf)['rms'] colunas = ['Iterations', 'mu'] for metodo in metodos: colunas.append(metodo + ':' + str(occupancy) + ':RMS') colunas.append('FDIP:' + str(occupancy) + ':RMS') data = panda.DataFrame([[np.inf] * (len(colunas) - 1) + [rms]], columns=colunas) for it in range(1, conf['iterations']): const['iterations'] = it for muI in range(conf['sM'], conf['eM']): muF = 1 / math.pow(2, muI) if muF == 1.0: mi = math.inf else: mi = muF opt = {'mi': mi} row = [it, muF] for metodo in metodos: const['metodo'] = metodo row.append(algo.getRMSfloat(const, opt)['rms']) row.append(np.inf) l = len(data) data.loc[l] = row with lock: data.to_csv(nome + '.csv', index=False) ended = datetime.datetime.now() print('Ended Mi Test with occupancy %d at %s after %s' % (occupancy, ended.strftime("%H:%M:%S %d/%m/%Y"), u.totalTime(startedI))) info.info('Ended Mi Test with occupancy %d after %s' % (occupancy, u.totalTime(startedI)))