Vertically concatenating stc files

If you have a question or issue with MNE-Python, please include the following info:

  • MNE-Python version: 0.23.0
  • operating system: OS

I want to vertically concatenate my stc files. I have obtained stc’s of my data corresponding to each HCP parcellation and I want to create a single stc file that contains data of a particular brain region (i.e., broca).

I already have tried this function, using np.vstack to concatenate them:

def return_the_roi_data_stcFormat(brain_region, s_date, s_name, n_stim, stim_index):

    roi_lbl_list = []

    if(brain_region == "soma_motor"):
        roi_lbl_list = soma_motor
        
    if(brain_region == "v4"):
        roi_lbl_list = v4_area_lbls
    
    if(brain_region == "v3"):
        roi_lbl_list = v3_area_lbls
    
    if(brain_region == "v2"):
        roi_lbl_list = v2_area_lbls
    
    if(brain_region == "v1"):
        roi_lbl_list = v1_area_lbls
    
    if(brain_region == "Wernicke"):
        roi_lbl_list = Wernicke_area_lbls
    
    if(brain_region == "broca"):
        roi_lbl_list = broca_area_lbls
    
    
    
    stc_sm = []
    firstTime_stim = True




    i_stim = stim_index
    firstTime_lbl = True

    stc_lbl = []
    for lbl in roi_lbl_list:

        fname_dir = os.path.join(hcplabeled_dir_parent, s_date, s_name, lbl)

        suffix = lbl[-3:]
        stc_tmp = mne.read_source_estimate(os.path.join(fname_dir, "stc_" + str(i_stim) + "_" + lbl + suffix +".stc"))
        
        if(firstTime_lbl):
            stc_lbl = stc_tmp
            firstTime_lbl = False
        else:
            stc_lbl = np.vstack((stc_lbl, stc_tmp))
                
                
    return stc_lbl

but the results will be like this:

array([[<SourceEstimate | 77 vertices, tmin : -100.0 (ms), tmax : 450.00000000000006 (ms), tstep : 1.0 (ms), data shape : (77, 551), ~167 kB>],
[<SourceEstimate | 72 vertices, tmin : -100.0 (ms), tmax : 450.00000000000006 (ms), tstep : 1.0 (ms), data shape : (72, 551), ~156 kB>],
[<SourceEstimate | 103 vertices, tmin : -100.0 (ms), tmax : 450.00000000000006 (ms), tstep : 1.0 (ms), data shape : (103, 551), ~223 kB>],
[<SourceEstimate | 61 vertices, tmin : -100.0 (ms), tmax : 450.00000000000006 (ms), tstep : 1.0 (ms), data shape : (61, 551), ~132 kB>],
[<SourceEstimate | 61 vertices, tmin : -100.0 (ms), tmax : 450.00000000000006 (ms), tstep : 1.0 (ms), data shape : (61, 551), ~132 kB>],
[<SourceEstimate | 59 vertices, tmin : -100.0 (ms), tmax : 450.00000000000006 (ms), tstep : 1.0 (ms), data shape : (59, 551), ~128 kB>],
[<SourceEstimate | 49 vertices, tmin : -100.0 (ms), tmax : 450.00000000000006 (ms), tstep : 1.0 (ms), data shape : (49, 551), ~106 kB>],
[<SourceEstimate | 74 vertices, tmin : -100.0 (ms), tmax : 450.00000000000006 (ms), tstep : 1.0 (ms), data shape : (74, 551), ~160 kB>],
[<SourceEstimate | 51 vertices, tmin : -100.0 (ms), tmax : 450.00000000000006 (ms), tstep : 1.0 (ms), data shape : (51, 551), ~110 kB>],
[<SourceEstimate | 34 vertices, tmin : -100.0 (ms), tmax : 450.00000000000006 (ms), tstep : 1.0 (ms), data shape : (34, 551), ~74 kB>]],
dtype=object)

I want it to be a single stc file. How should I do that?

Hello,

what you’re trying to do is not possible.

What is the goal here? Why do you even want to do that?

Best wishes,
Richard

I want to visualize the brain activity on specific brain areas (i.e., broca)!

By following this tutorial:
https://mne.tools/stable/auto_tutorials/inverse/60_visualize_stc.html#sphx-glr-auto-tutorials-inverse-60-visualize-stc-py

Thanks! My understanding is that you already have the activity in different brain areas, only in separate STCs. Do you now want to merge those STCs to view the activation in all those areas simultaneously? I’m trying to understand why you want to concat the data in the first place.

yes exactly that is my goal

Hello @shirinV, since apparently all those STCs are derived from the same brain and span the same time period, why did you even create so many of them, and one per label / area? You could create a single source estimate and then restrict your analyses to whichever area is of interest.

First, I’ll reiterate what @richard already said: it is much easier to keep the original data in one STC, and (for example) use stc.in_label() to make a copy of just the data in a particular label when you need it (for plotting, for example) or use things like label.center_of_mass(), label.compute_area(), stc.extract_label_time_course() etc, as needed. In MNE-Python, labels can be combined with a simple + operator, so it’s easy to combine multiple parcellation labels into a single Label object if needed before applying one of the methods above.

If you somehow are stuck with a bunch of single-label STC files and don’t have access to the original file, and you want to reconstruct the original, an approach that might work is to extract just the data array from each STC, and add (not vstack) those arrays. Something like:

# assuming you have a list of STCs called all_stcs:
aggregator = np.zeros_like(all_stcs[0].data)
for this_stc in all_stcs:
    aggregator += this_stc.data

then you can put the aggregated data into a new STC:

aggregated_stc = all_stcs[0].copy()
aggregated_stc.data = aggregator

As @richard said above, this will only work if all STCs are from the same brain, and furthermore this assumes that the various STCs are fully disjoint (meaning their data is all zeros except for in a single label, and none of the STCs have data from the same / overlapping labels).

1 Like