def partition_timeseries(image, roi_img, mask_img=None, zeroe=True, roi_values=None, outdict=False):
img  = read_img(image)<EOL>rois = read_img(roi_img)<EOL>check_img_compatibility(img, rois, only_check_3d=True)<EOL>roi_data = rois.get_data()<EOL>if roi_values is not None:<EOL><INDENT>for rv in roi_values:<EOL><INDENT>if not np.any(roi_data == rv):<EOL><INDENT>raise ValueError('<STR_LIT>'.format(rv, repr_imgs(roi_img)))<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>roi_values = get_unique_nonzeros(roi_data)<EOL><DEDENT>if mask_img is None:<EOL><INDENT>mask_data = None<EOL><DEDENT>else:<EOL><INDENT>mask = load_mask(mask_img)<EOL>check_img_compatibility(img, mask, only_check_3d=True)<EOL>mask_data = mask.get_data()<EOL><DEDENT>if outdict:<EOL><INDENT>extract_data = _extract_timeseries_dict<EOL><DEDENT>else:<EOL><INDENT>extract_data = _extract_timeseries_list<EOL><DEDENT>try:<EOL><INDENT>return extract_data(img.get_data(), rois.get_data(), mask_data,<EOL>roi_values=roi_values, zeroe=zeroe)<EOL><DEDENT>except:<EOL><INDENT>raise<EOL><DEDENT>
Partition the timeseries in tsvol according to the ROIs in roivol.
    If a mask is given, will use it to exclude any voxel outside of it.

    The outdict indicates whether you want a dictionary for each set of timeseries keyed by the ROI value
    or a list of timeseries sets. If True and roi_img is not None will return an OrderedDict, if False
    or roi_img or roi_list is None will return a list.

    Background value is assumed to be 0 and won't be used here.

    Parameters
    ----------
    image: img-like object or str
        4D timeseries volume

    roi_img: img-like object or str
        3D volume defining different ROIs.

    mask_img: img-like object or str
        3D mask volume

    zeroe: bool
        If true will remove the null timeseries voxels.

    roi_values: list of ROI values (int?)
        List of the values of the ROIs to indicate the
        order and which ROIs will be processed.

    outdict: bool
        If True will return an OrderedDict of timeseries sets, otherwise a list.

    Returns
    -------
    timeseries: list or OrderedDict
        A dict with the timeseries as items and keys as the ROIs voxel values or
        a list where each element is the timeseries set ordered by the sorted values in roi_img or by the roi_values
        argument.