Fetch example dataset.

    If the requested dataset is not found in the location specified by
    `datadir`, the function attempts to download it.

    Parameters
    ----------
    dataset : str
        Which dataset to load. Currently only 'mi' is supported.
    datadir : str
        Path to the storage location of example datasets. Datasets are
        downloaded to this location if they cannot be found. If the directory
        does not exist it is created.

    Returns
    -------
        data : list of dicts
            The data set is stored in a list, where each list element
            corresponds to data from one subject. Each list element is a
            dictionary with the following keys:
              "eeg" ... EEG signals
              "triggers" ... Trigger latencies
              "labels" ... Class labels
              "fs" ... Sample rate
              "locations" ... Channel locations

def fetch(dataset="mi", datadir=datadir):
    """Fetch example dataset.

    If the requested dataset is not found in the location specified by
    `datadir`, the function attempts to download it.

    Parameters
    ----------
    dataset : str
        Which dataset to load. Currently only 'mi' is supported.
    datadir : str
        Path to the storage location of example datasets. Datasets are
        downloaded to this location if they cannot be found. If the directory
        does not exist it is created.

    Returns
    -------
        data : list of dicts
            The data set is stored in a list, where each list element
            corresponds to data from one subject. Each list element is a
            dictionary with the following keys:
              "eeg" ... EEG signals
              "triggers" ... Trigger latencies
              "labels" ... Class labels
              "fs" ... Sample rate
              "locations" ... Channel locations
    """
    if dataset not in datasets:
        raise ValueError("Example data '{}' not available.".format(dataset))
    else:
        files = datasets[dataset]["files"]
        url = datasets[dataset]["url"]
        md5 = datasets[dataset]["md5"]
    if not isdir(datadir):
        makedirs(datadir)

    data = []

    for n, filename in enumerate(files):
        fullfile = join(datadir, filename)
        if not isfile(fullfile):
            with open(fullfile, "wb") as f:
                response = get(join(url, filename))
                f.write(response.content)
        with open(fullfile, "rb") as f:  # check if MD5 of downloaded file matches original hash
            hash = hashlib.md5(f.read()).hexdigest()
        if hash != md5[n]:
            raise MD5MismatchError("MD5 hash of {} does not match {}.".format(fullfile, md5[n]))
        data.append(convert(dataset, loadmat(fullfile)))

    return data