def _derive_temporalnetwork(self, f, i, tag, params, confounds_exist, confound_files):
        """
        Funciton called by TenetoBIDS.derive_temporalnetwork for concurrent processing.
        """
        data = load_tabular_file(f, index_col=True, header=True)

        fs, _ = drop_bids_suffix(f)
        save_name, save_dir, _ = self._save_namepaths_bids_derivatives(
            fs, tag, 'tvc', 'tvcconn')
        if 'weight-var' in params.keys():
            if params['weight-var'] == 'from-subject-fc':
                fc_files = self.get_selected_files(
                    quiet=1, pipeline='functionalconnectivity', forfile=f)
                if len(fc_files) == 1:
                    # Could change to load_data call
                    params['weight-var'] = load_tabular_file(
                        fc_files[0]).values
                else:
                    raise ValueError('Cannot correctly find FC files')

        if 'weight-mean' in params.keys():
            if params['weight-mean'] == 'from-subject-fc':
                fc_files = self.get_selected_files(
                    quiet=1, pipeline='functionalconnectivity', forfile=f)
                if len(fc_files) == 1:
                    # Could change to load_data call
                    params['weight-mean'] = load_tabular_file(
                        fc_files[0]).values
                else:
                    raise ValueError('Cannot correctly find FC files')

        params['report'] = 'yes'
        params['report_path'] = save_dir + '/report/'
        params['report_filename'] = save_name + '_derivationreport.html'

        if not os.path.exists(params['report_path']):
            os.makedirs(params['report_path'])

        if 'dimord' not in params:
            params['dimord'] = 'time,node'

        dfc = teneto.timeseries.derive_temporalnetwork(data.values, params)
        dfc_net = TemporalNetwork(from_array=dfc, nettype='wu')
        dfc_net.network.to_csv(save_dir + save_name + '.tsv', sep='\t')

        sidecar = get_sidecar(f)
        sidecar['tvc'] = params
        if 'weight-var' in sidecar['tvc']:
            sidecar['tvc']['weight-var'] = True
            sidecar['tvc']['fc source'] = fc_files
        if 'weight-mean' in sidecar['tvc']:
            sidecar['tvc']['weight-mean'] = True
            sidecar['tvc']['fc source'] = fc_files
        sidecar['tvc']['inputfile'] = f
        sidecar['tvc']['description'] = 'Time varying connectivity information.'
        with open(save_dir + save_name + '.json', 'w') as fs:
            json.dump(sidecar, fs)

        if confounds_exist:
            analysis_step = 'tvc-derive'
            df = pd.read_csv(confound_files[i], sep='\t')
            df = df.fillna(df.median())
            ind = np.triu_indices(dfc.shape[0], k=1)
            dfc_df = pd.DataFrame(dfc[ind[0], ind[1], :].transpose())
            # If windowed, prune df so that it matches with dfc_df
            if len(df) != len(dfc_df):
                df = df.iloc[int(np.round((params['windowsize']-1)/2)): int(np.round((params['windowsize']-1)/2)+len(dfc_df))]
                df.reset_index(inplace=True, drop=True)
            # NOW CORRELATE DF WITH DFC BUT ALONG INDEX NOT DF.
            dfc_df_z = (dfc_df - dfc_df.mean())
            df_z = (df - df.mean())
            R_df = dfc_df_z.T.dot(df_z).div(len(dfc_df)).div(
                df_z.std(ddof=0)).div(dfc_df_z.std(ddof=0), axis=0)
            R_df_describe = R_df.describe()
            desc_index = R_df_describe.index
            confound_report_dir = params['report_path'] + \
                '/' + save_name + '_confoundcorr/'
            confound_report_figdir = confound_report_dir + 'figures/'
            if not os.path.exists(confound_report_figdir):
                os.makedirs(confound_report_figdir)
            report = '<html><body>'
            report += '<h1> Correlation of ' + analysis_step + ' and confounds.</h1>'
            for c in R_df.columns:
                fig, ax = plt.subplots(1)
                ax = sns.distplot(
                    R_df[c], hist=False, color='m', ax=ax, kde_kws={"shade": True})
                fig.savefig(confound_report_figdir + c + '.png')
                plt.close(fig)
                report += '<h2>' + c + '</h2>'
                for ind_name, r in enumerate(R_df_describe[c]):
                    report += str(desc_index[ind_name]) + ': '
                    report += str(r) + '<br>'
                report += 'Distribution of corrlation values:'
                report += '<img src=' + \
                    os.path.abspath(confound_report_figdir) + \
                    '/' + c + '.png><br><br>'
            report += '</body></html>'

            with open(confound_report_dir + save_name + '_confoundcorr.html', 'w') as file:
                file.write(report)
def _derive_temporalnetwork ( self , f , i , tag , params , confounds_exist , confound_files ) : data = load_tabular_file ( f , index_col = True , header = True ) fs , _ = drop_bids_suffix ( f ) save_name , save_dir , _ = self . _save_namepaths_bids_derivatives ( fs , tag , 'tvc' , 'tvcconn' ) if 'weight-var' in params . keys ( ) : if params [ 'weight-var' ] == 'from-subject-fc' : fc_files = self . get_selected_files ( quiet = 1 , pipeline = 'functionalconnectivity' , forfile = f ) if len ( fc_files ) == 1 : params [ 'weight-var' ] = load_tabular_file ( fc_files [ 0 ] ) . values else : raise ValueError ( 'Cannot correctly find FC files' ) if 'weight-mean' in params . keys ( ) : if params [ 'weight-mean' ] == 'from-subject-fc' : fc_files = self . get_selected_files ( quiet = 1 , pipeline = 'functionalconnectivity' , forfile = f ) if len ( fc_files ) == 1 : params [ 'weight-mean' ] = load_tabular_file ( fc_files [ 0 ] ) . values else : raise ValueError ( 'Cannot correctly find FC files' ) params [ 'report' ] = 'yes' params [ 'report_path' ] = save_dir + '/report/' params [ 'report_filename' ] = save_name + '_derivationreport.html' if not os . path . exists ( params [ 'report_path' ] ) : os . makedirs ( params [ 'report_path' ] ) if 'dimord' not in params : params [ 'dimord' ] = 'time,node' dfc = teneto . timeseries . derive_temporalnetwork ( data . values , params ) dfc_net = TemporalNetwork ( from_array = dfc , nettype = 'wu' ) dfc_net . network . to_csv ( save_dir + save_name + '.tsv' , sep = '\t' ) sidecar = get_sidecar ( f ) sidecar [ 'tvc' ] = params if 'weight-var' in sidecar [ 'tvc' ] : sidecar [ 'tvc' ] [ 'weight-var' ] = True sidecar [ 'tvc' ] [ 'fc source' ] = fc_files if 'weight-mean' in sidecar [ 'tvc' ] : sidecar [ 'tvc' ] [ 'weight-mean' ] = True sidecar [ 'tvc' ] [ 'fc source' ] = fc_files sidecar [ 'tvc' ] [ 'inputfile' ] = f sidecar [ 'tvc' ] [ 'description' ] = 'Time varying connectivity information.' with open ( save_dir + save_name + '.json' , 'w' ) as fs : json . dump ( sidecar , fs ) if confounds_exist : analysis_step = 'tvc-derive' df = pd . read_csv ( confound_files [ i ] , sep = '\t' ) df = df . fillna ( df . median ( ) ) ind = np . triu_indices ( dfc . shape [ 0 ] , k = 1 ) dfc_df = pd . DataFrame ( dfc [ ind [ 0 ] , ind [ 1 ] , : ] . transpose ( ) ) if len ( df ) != len ( dfc_df ) : df = df . iloc [ int ( np . round ( ( params [ 'windowsize' ] - 1 ) / 2 ) ) : int ( np . round ( ( params [ 'windowsize' ] - 1 ) / 2 ) + len ( dfc_df ) ) ] df . reset_index ( inplace = True , drop = True ) dfc_df_z = ( dfc_df - dfc_df . mean ( ) ) df_z = ( df - df . mean ( ) ) R_df = dfc_df_z . T . dot ( df_z ) . div ( len ( dfc_df ) ) . div ( df_z . std ( ddof = 0 ) ) . div ( dfc_df_z . std ( ddof = 0 ) , axis = 0 ) R_df_describe = R_df . describe ( ) desc_index = R_df_describe . index confound_report_dir = params [ 'report_path' ] + '/' + save_name + '_confoundcorr/' confound_report_figdir = confound_report_dir + 'figures/' if not os . path . exists ( confound_report_figdir ) : os . makedirs ( confound_report_figdir ) report = '<html><body>' report += '<h1> Correlation of ' + analysis_step + ' and confounds.</h1>' for c in R_df . columns : fig , ax = plt . subplots ( 1 ) ax = sns . distplot ( R_df [ c ] , hist = False , color = 'm' , ax = ax , kde_kws = { "shade" : True } ) fig . savefig ( confound_report_figdir + c + '.png' ) plt . close ( fig ) report += '<h2>' + c + '</h2>' for ind_name , r in enumerate ( R_df_describe [ c ] ) : report += str ( desc_index [ ind_name ] ) + ': ' report += str ( r ) + '<br>' report += 'Distribution of corrlation values:' report += '<img src=' + os . path . abspath ( confound_report_figdir ) + '/' + c + '.png><br><br>' report += '</body></html>' with open ( confound_report_dir + save_name + '_confoundcorr.html' , 'w' ) as file : file . write ( report )
Funciton called by TenetoBIDS.derive_temporalnetwork for concurrent processing.