def download(self, **kwargs):
from six.moves.urllib.request import urlopen<EOL>import os<EOL>try:<EOL><INDENT>images_db = self.to_pandas()<EOL><DEDENT>except ImportError as exc:<EOL><INDENT>exc.args = ('<STR_LIT>',)<EOL>raise<EOL><DEDENT>images_db = images_db.loc[images_db.url1 != '<STR_LIT>']<EOL>training_set = kwargs.pop('<STR_LIT>', <NUM_LIT:0>)<EOL>labelled_samples = kwargs.pop('<STR_LIT>', <NUM_LIT:0>)<EOL>download_location = kwargs.pop('<STR_LIT>',<EOL>os.path.join('<STR_LIT>'))<EOL>duration_values = np.array([<NUM_LIT:0.5>, <NUM_LIT:1.0>, <NUM_LIT>, <NUM_LIT>])<EOL>download_durs = kwargs.pop('<STR_LIT>', duration_values)<EOL>duration_idx = []<EOL>for idur in download_durs:<EOL><INDENT>duration_idx.append(np.argwhere(duration_values == idur)[<NUM_LIT:0>][<NUM_LIT:0>])<EOL><DEDENT>duration_values = duration_values[duration_idx]<EOL>duration_values = np.array([duration_values]).astype(str)<EOL>if labelled_samples:<EOL><INDENT>if '<STR_LIT>' not in images_db.columns:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT><DEDENT>if labelled_samples and not training_set:<EOL><INDENT>training_set = <NUM_LIT:1><EOL><DEDENT>cols_for_download = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']<EOL>cols_for_download = [cols_for_download[idx] for idx in duration_idx]<EOL>cols_for_download_ext = ['<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>']<EOL>if not training_set:<EOL><INDENT>images_db['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>if not labelled_samples:<EOL><INDENT>images_db['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>if not os.path.isdir(download_location):<EOL><INDENT>os.makedirs(download_location)<EOL><DEDENT>if training_set:<EOL><INDENT>for ilabel in images_db.ml_label.unique():<EOL><INDENT>if labelled_samples:<EOL><INDENT>for itype in images_db.sample_type.unique():<EOL><INDENT>if not os.path.isdir(os.path.join(<EOL>download_location,<EOL>ilabel, itype)):<EOL><INDENT>os.makedirs(os.path.join(download_location,<EOL>ilabel, itype))<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>if not os.path.isdir(os.path.join(download_location,<EOL>ilabel)):<EOL><INDENT>os.makedirs(os.path.join(download_location,<EOL>ilabel))<EOL><DEDENT><DEDENT><DEDENT><DEDENT>images_for_download = images_db[cols_for_download]<EOL>images = images_for_download.as_matrix().flatten()<EOL>images_for_download_ext = images_db[cols_for_download_ext]<EOL>duration = np.atleast_2d(<EOL>duration_values.repeat(<EOL>len(images_for_download_ext), <NUM_LIT:0>).flatten(<EOL>)).T<EOL>images_for_download_ext = images_for_download_ext.as_matrix(<EOL>).repeat(len(cols_for_download), <NUM_LIT:0>)<EOL>images_for_for_download_path = np.array([[download_location]]).repeat(<EOL>len(images_for_download_ext), <NUM_LIT:0>)<EOL>images = np.hstack((np.atleast_2d(images).T,<EOL>images_for_download_ext, duration,<EOL>images_for_for_download_path))<EOL>def get_image(url):<EOL><INDENT>name = url[<NUM_LIT:3>] + '<STR_LIT:_>' + url[<NUM_LIT:4>] + '<STR_LIT>' + url[<NUM_LIT:5>] + '<STR_LIT>'<EOL>outfile = os.path.join(url[<NUM_LIT:6>], url[<NUM_LIT:1>], url[<NUM_LIT:2>], name)<EOL>with open(outfile, '<STR_LIT:wb>') as fout:<EOL><INDENT>fout.write(urlopen(url[<NUM_LIT:0>]).read())<EOL><DEDENT><DEDENT>nproc = min(kwargs.pop('<STR_LIT>', <NUM_LIT:1>), len(images))<EOL>def _download_single_image(url):<EOL><INDENT>try:<EOL><INDENT>return url, get_image(url)<EOL><DEDENT>except Exception as exc:  <EOL><INDENT>if nproc == <NUM_LIT:1>:<EOL><INDENT>raise<EOL><DEDENT>else:<EOL><INDENT>return url, exc<EOL><DEDENT><DEDENT><DEDENT>output = mp_utils.multiprocess_with_queues(<EOL>nproc, _download_single_image, images)<EOL>for f, x in output:<EOL><INDENT>if isinstance(x, Exception):<EOL><INDENT>x.args = ('<STR_LIT>' % (f, str(x)),)<EOL>raise x<EOL><DEDENT><DEDENT>
If table contains Gravity Spy triggers `EventTable`

        Parameters
        ----------
        nproc : `int`, optional, default: 1
            number of CPUs to use for parallel file reading

        download_path : `str` optional, default: 'download'
            Specify where the images end up.

        download_durs : `list` optional, default: [0.5, 1.0, 2.0, 4.0]
            Specify exactly which durations you want to download
            default is to download all the avaialble GSpy durations.

        kwargs: Optional training_set and labelled_samples args
            that will download images in a special way
            ./"ml_label"/"sample_type"/"image"

        Returns
        -------
        Folder containing omega scans sorted by label