import numpy as np from mesostat.metric.metric import MetricCalculator nChannel = 5 nTrial = 20 nTime = 100 data = np.random.randint(0, 4, (nChannel, nTrial, nTime)).astype(int) mc = MetricCalculator(serial=False, nCore=4) mc.set_data(data, 'prs') settings_estimator = {'pid_estimator': 'TartuPID', 'lags_pid': [0, 0]} rez = mc.metric3D('BivariatePID', '', metricSettings={ 'settings_estimator': settings_estimator, 'parallelSrc3D': [1, 2, 3], 'trg': 4 }) print(rez.shape) print(rez[:, :, 0])
print("Appended root directory", rootpath) from mesostat.metric.metric import MetricCalculator from lib.gallerosalas.data_fc_db_raw import DataFCDatabase from lib.analysis.triplet_compute.datasweep import multiprocess_mouse_trgsweep # tmp_path = root_path_data if 'root_path_data' in locals() else "./" params = {} # params['root_path_data'] = '/home/alfomi/data/yasirdata_raw' params['root_path_data'] = '/media/alyosha/Data/TE_data/yasirdata_raw' # params['root_path_data'] = gui_fpath('h5path', './') dataDB = DataFCDatabase(params) h5outname = 'pr2_gallerosalas_timesweep_all_df.h5' mc = MetricCalculator(serial=True, verbose=False) #, nCore=4) # Sweep over following parameters argSweepDict = { 'mousename': dataDB.mice, # ['mvg_4'] # 'intervName': dataDB.get_interval_names(), 'datatype': ['bn_trial', 'bn_session'], 'trialType': ['None', 'Hit', 'CR'] } # Exclude following parameter combinations exclQueryLst = [ # {'datatype': 'bn_trial', 'intervName': 'PRE'}, # Pre-trial interval not meaningful for bn_trial # {'mousename' : 'mou_6', 'intervName': 'REW'} # No reward recorded for mouse 6 ]
varSweep = [(p, d, s) for p in ["incr", "random"] for d in ["linear", "circular"] for s in ["matching", "shifted"]] nSweep = len(varSweep) ######################## # Figures ######################## psFigs = PhaseSpaceFigures(nSweep) metricFigs = MetricFigures(["values", "lags", "p-values"], nSweep, algNames) ######################## # Sweep loop ######################## mc = MetricCalculator(nCore=4) performanceTimes = [] for iSweep, var in enumerate(varSweep): predictName, dataName, shiftName = var data = generate_data(nTrial, nTime, predictName == "incr", dataName == "linear", shiftName == "shifted") dataLabel = predictName + "_" + dataName + "_" + shiftName psFigs.addrow(data, dataLabel) metricFigs.set_row_label(iSweep, dataLabel) for iAlg, (algName, metricName, estimator, cmi) in enumerate(zip(algNames, *algDict.values())): # Get label resultLabel = dataLabel + "_" + algName
def parallel(nCore): task(MetricCalculator(serial=False, nCore=nCore))
def serial(): task(MetricCalculator(serial=True))
def test_shapes(self): # Create dummy data nProcess = 12 nSample = 200 nRepetition = 100 dataShape = (nProcess, nSample, nRepetition) dataDimOrd = "psr" data = np.random.uniform(0, 1, dataShape) # Settings for some of the metrics metricSettings = { "max_lag": 3, "lag": 5, "hist": 3, "parallelTrg": True, 'min_lag_sources': 1, 'max_lag_sources': 1, 'cmi_estimator': "JidtGaussianCMI" } mc = MetricCalculator(serial=False) mc.set_data(data, dataDimOrd, timeWindow=None, zscoreDim="sr") # Test if result of the metric has expected shape, based on source and target axis def testAxis(metrics, srcDimOrder, trgDimOrder, extraAxis=None, metricSettings=None): trgShape = tuple( [dataShape[srcDimOrder.index(e)] for e in trgDimOrder]) for iMetric, metricName in enumerate(metrics): print(metricName, trgDimOrder) trgShapeEff = trgShape if extraAxis is None else trgShape + extraAxis[ iMetric] rez = mc.metric3D(metricName, trgDimOrder, metricSettings=metricSettings) self.assertEqual(rez.shape, trgShapeEff, msg="Error in " + metricName) # Test if metrics throw ValueError exception def testRaises(metrics, trgDimOrder): for iMetric, metricName in enumerate(metrics): print(metricName, trgDimOrder) with self.assertRaises(ValueError): mc.metric3D(metricName, trgDimOrder, metricSettings=metricSettings) ######################################## # TEST 1.1: Shape: Full projections ######################################## # "mean": self._nanmean, # "std": self._nanstd, # "autocorr": autocorr_3D, # "corr": corr_3D, # "crosscorr": cross_corr_3D, # "BivariateMI": lambda data, settings : self._TE("BivariateMI", data, settings), # "BivariateTE": lambda data, settings: self._TE("BivariateTE", data, settings), # "MultivariateMI": lambda data, settings: self._TE("MultivariateMI", data, settings), # "MultivariateTE": lambda data, settings: self._TE("MultivariateTE", data, settings), # "autocorr_d1": autocorr_d1_3D, # "ar1_coeff": autoregression.ar1_coeff, # "ar1_testerr": autoregression.ar1_testerr, # "ar_testerr": autoregression.ar_testerr, # "mar1_coeff": autoregression.mar1_coeff, # "mar1_testerr": autoregression.mar1_testerr, # "mar_testerr": autoregression.mar_testerr, # "mar_inp_testerr": autoregression.mar_inp_testerr, # "avgcorr": avg_corr_3D, # "avg_entropy": average_entropy_3D, # "avg_PI": average_predictive_info, # "ord_mean": sequence.temporal_mean_3D, # "ord_moments": sequence.temporal_moments_3D, # "ord_binary": sequence.bivariate_binary_orderability_3D, # "ord_binary_avg": sequence.avg_bivariate_binary_orderability_3D, # "ord_student": sequence.bivariate_student_orderability_3D, # "ord_student_avg": sequence.avg_bivariate_student_orderability_3D, # "temporal_basis": stretch_basis_projection, # "generic_metric": self._generic_metric # "avg_entropy_1D" : self._avg_entropy_1D, # "avg_PI_1D": self._avg_PI_1D, # "avg_TC_1D": self._avg_TC, # "avg_TPI_1D": self._avg_TPI, # # Scalar # metricScalarFull = [ # "mean", "std", # "autocorr_d1", # "ar1_coeff", "ar1_testerr", "ar_testerr", "mar1_testerr", "mar_testerr", # "avgcorr", # "avg_entropy", "avg_PI", "avg_entropy_1D", "avg_PI_1D", "avg_TC_1D", "avg_TPI_1D", # "ord_mean", "ord_binary_avg", # ] # for metricName in metricScalarFull: # print("Testing full projection for", metricName) # rez = mc.metric3D(metricName, "", metricSettings=metricSettings) # self.assertTrue(np.isscalar(rez)) # self.assertTrue(~np.isnan(rez)) # Vector metricVectorFull = [ "autocorr", "corr", "crosscorr", "BivariateMI", "BivariateTE", "MultivariateMI", "MultivariateTE", "ord_binary", "temporal_basis" ] extraAxisVectorFull = [(nSample, ), (nProcess, nProcess), (nProcess, nProcess), (3, nProcess, nProcess), (3, nProcess, nProcess), (3, nProcess, nProcess), (3, nProcess, nProcess), (nProcess, nProcess), (5, )] testAxis(metricVectorFull, dataDimOrd, "", extraAxis=extraAxisVectorFull, metricSettings=metricSettings)
# import standard libraries import numpy as np import matplotlib.pyplot as plt from mesostat.metric.metric import MetricCalculator from mesostat.visualization.mpl_matrix import plot_matrix mc = MetricCalculator(serial=True, verbose=True) ''' Test 1: Generate random data, and shift it by fixed steps for each channel Expected outcomes: * If shift <= max_delay, corr ~ 1, delay = shift * If shift > max_delay, corr ~ 0, delay = rand * Delay is the same for all diagonals, because we compare essentially the same data, both cycled by the same amount ''' nNode = 5 nData = 1000 data = np.zeros((nNode, nData)) data[0] = np.random.normal(0, 1, nData) for i in range(1, nNode): data[i] = np.hstack((data[i - 1][1:], data[i - 1][0])) mc.set_data(data, 'ps') sweepSettings = {"lag": [1, 2]} rezCorr = mc.metric3D("crosscorr", "", metricSettings={"estimator": "corr"}, sweepSettings=sweepSettings) rezSpr = mc.metric3D("crosscorr",