def evaluate_metadata_statement(self, metadata, keyjar=None):

<EOL>res = dict([(k, v) for k, v in metadata.items() if k not in IgnoreKeys])<EOL>les = []<EOL>if '<STR_LIT>' in metadata:<EOL><INDENT>for fo, ms in metadata['<STR_LIT>'].items():<EOL><INDENT>if isinstance(ms, str):<EOL><INDENT>ms = json.loads(ms)<EOL><DEDENT>for _le in self.evaluate_metadata_statement(ms):<EOL><INDENT>if isinstance(ms, Message):<EOL><INDENT>le = LessOrEqual(sup=_le, **ms.to_dict())<EOL><DEDENT>else:  <EOL><INDENT>le = LessOrEqual(sup=_le, **ms)<EOL><DEDENT>if le.is_expired():<EOL><INDENT>logger.error(<EOL>'<STR_LIT>'.format(ms)<EOL>)<EOL>logger.info('<STR_LIT>'.format(utc_time_sans_frac()))<EOL>continue<EOL><DEDENT>le.eval(res)<EOL>les.append(le)<EOL><DEDENT><DEDENT>return les<EOL><DEDENT>else:  <EOL><INDENT>try:<EOL><INDENT>_iss = metadata['<STR_LIT>']<EOL><DEDENT>except:<EOL><INDENT>le = LessOrEqual()<EOL>le.eval(res)<EOL><DEDENT>else:<EOL><INDENT>le = LessOrEqual(iss=_iss, exp=metadata['<STR_LIT>'])<EOL>le.eval(res)<EOL><DEDENT>les.append(le)<EOL>return les<EOL><DEDENT>

Computes the resulting metadata statement from a compounded metadata
statement.
If something goes wrong during the evaluation an exception is raised

:param metadata: The compounded metadata statement as a dictionary
:return: A list of :py:class:`fedoidc.operator.LessOrEqual`
    instances, one per FO.


def correct_usage(self, metadata, federation_usage):

if '<STR_LIT>' in metadata:<EOL><INDENT>_msl = {}<EOL>for fo, ms in metadata['<STR_LIT>'].items():<EOL><INDENT>if not isinstance(ms, Message):<EOL><INDENT>ms = json.loads(ms)<EOL><DEDENT>if self.correct_usage(ms, federation_usage=federation_usage):<EOL><INDENT>_msl[fo] = ms<EOL><DEDENT><DEDENT>if _msl:<EOL><INDENT>metadata['<STR_LIT>'] = Message(**_msl)<EOL>return metadata<EOL><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT><DEDENT>else:  <EOL><INDENT>try:<EOL><INDENT>assert federation_usage == metadata['<STR_LIT>']<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT>except AssertionError:<EOL><INDENT>return None<EOL><DEDENT>return metadata<EOL><DEDENT>

Remove MS paths that are marked to be used for another usage

:param metadata: Metadata statement as dictionary
:param federation_usage: In which context this is expected to used.
:return: Filtered Metadata statement.


def extend_with_ms(self, req, sms_dict):

_ms_uri = {}<EOL>_ms = {}<EOL>for fo, sms in sms_dict.items():<EOL><INDENT>if sms.startswith('<STR_LIT>') or sms.startswith('<STR_LIT>'):<EOL><INDENT>_ms_uri[fo] = sms<EOL><DEDENT>else:<EOL><INDENT>_ms[fo] = sms<EOL><DEDENT><DEDENT>if _ms:<EOL><INDENT>req['<STR_LIT>'] = Message(**_ms)<EOL><DEDENT>if _ms_uri:<EOL><INDENT>req['<STR_LIT>'] = Message(**_ms_uri)<EOL><DEDENT>return req<EOL>

Add signed metadata statements to a request

:param req: The request
:param sms_dict: A dictionary with FO IDs as keys and signed metadata
    statements (sms) or uris pointing to sms as values.
:return: The updated request


def conv3x3(in_planes, out_planes, stride=<NUM_LIT:1>, dilation=<NUM_LIT:1>):

return nn.Conv2d(<EOL>in_planes,<EOL>out_planes,<EOL>kernel_size=<NUM_LIT:3>,<EOL>stride=stride,<EOL>padding=dilation,<EOL>dilation=dilation,<EOL>bias=False)<EOL>


super(Bottleneck, self).__init__()<EOL>assert style in ['<STR_LIT>', '<STR_LIT>']<EOL>if style == '<STR_LIT>':<EOL><INDENT>conv1_stride = <NUM_LIT:1><EOL>conv2_stride = stride<EOL><DEDENT>else:<EOL><INDENT>conv1_stride = stride<EOL>conv2_stride = <NUM_LIT:1><EOL><DEDENT>self.conv1 = nn.Conv2d(<EOL>inplanes, planes, kernel_size=<NUM_LIT:1>, stride=conv1_stride, bias=False)<EOL>self.conv2 = nn.Conv2d(<EOL>planes,<EOL>planes,<EOL>kernel_size=<NUM_LIT:3>,<EOL>stride=conv2_stride,<EOL>padding=dilation,<EOL>dilation=dilation,<EOL>bias=False)<EOL>self.bn1 = nn.BatchNorm2d(planes)<EOL>self.bn2 = nn.BatchNorm2d(planes)<EOL>self.conv3 = nn.Conv2d(<EOL>planes, planes * self.expansion, kernel_size=<NUM_LIT:1>, bias=False)<EOL>self.bn3 = nn.BatchNorm2d(planes * self.expansion)<EOL>self.relu = nn.ReLU(inplace=True)<EOL>self.downsample = downsample<EOL>self.stride = stride<EOL>self.dilation = dilation<EOL>self.with_cp = with_cp<EOL>

Bottleneck block.

        If style is "pytorch", the stride-two layer is the 3x3 conv layer,
        if it is "caffe", the stride-two layer is the first 1x1 conv layer.


def conv3x3(in_planes, out_planes, dilation=<NUM_LIT:1>):

return nn.Conv2d(<EOL>in_planes,<EOL>out_planes,<EOL>kernel_size=<NUM_LIT:3>,<EOL>padding=dilation,<EOL>dilation=dilation)<EOL>


def flowread(flow_or_path, quantize=False, concat_axis=<NUM_LIT:0>, *args, **kwargs):

if isinstance(flow_or_path, np.ndarray):<EOL><INDENT>if (flow_or_path.ndim != <NUM_LIT:3>) or (flow_or_path.shape[-<NUM_LIT:1>] != <NUM_LIT:2>):<EOL><INDENT>raise ValueError('<STR_LIT>'.format(<EOL>flow_or_path.shape))<EOL><DEDENT>return flow_or_path<EOL><DEDENT>elif not is_str(flow_or_path):<EOL><INDENT>raise TypeError(<EOL>'<STR_LIT>'.format(<EOL>type(flow_or_path)))<EOL><DEDENT>if not quantize:<EOL><INDENT>with open(flow_or_path, '<STR_LIT:rb>') as f:<EOL><INDENT>try:<EOL><INDENT>header = f.read(<NUM_LIT:4>).decode('<STR_LIT:utf-8>')<EOL><DEDENT>except Exception:<EOL><INDENT>raise IOError('<STR_LIT>'.format(flow_or_path))<EOL><DEDENT>else:<EOL><INDENT>if header != '<STR_LIT>':<EOL><INDENT>raise IOError(<EOL>'<STR_LIT>'.<EOL>format(flow_or_path))<EOL><DEDENT><DEDENT>w = np.fromfile(f, np.int32, <NUM_LIT:1>).squeeze()<EOL>h = np.fromfile(f, np.int32, <NUM_LIT:1>).squeeze()<EOL>flow = np.fromfile(f, np.float32, w * h * <NUM_LIT:2>).reshape((h, w, <NUM_LIT:2>))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>assert concat_axis in [<NUM_LIT:0>, <NUM_LIT:1>]<EOL>cat_flow = imread(flow_or_path, flag='<STR_LIT>')<EOL>if cat_flow.ndim != <NUM_LIT:2>:<EOL><INDENT>raise IOError(<EOL>'<STR_LIT>'.<EOL>format(flow_or_path, cat_flow.ndim))<EOL><DEDENT>assert cat_flow.shape[concat_axis] % <NUM_LIT:2> == <NUM_LIT:0><EOL>dx, dy = np.split(cat_flow, <NUM_LIT:2>, axis=concat_axis)<EOL>flow = dequantize_flow(dx, dy, *args, **kwargs)<EOL><DEDENT>return flow.astype(np.float32)<EOL>

Read an optical flow map.

    Args:
        flow_or_path (ndarray or str): A flow map or filepath.
        quantize (bool): whether to read quantized pair, if set to True,
            remaining args will be passed to :func:`dequantize_flow`.
        concat_axis (int): The axis that dx and dy are concatenated,
            can be either 0 or 1. Ignored if quantize is False.

    Returns:
        ndarray: Optical flow represented as a (h, w, 2) numpy array


def flowwrite(flow, filename, quantize=False, concat_axis=<NUM_LIT:0>, *args, **kwargs):

if not quantize:<EOL><INDENT>with open(filename, '<STR_LIT:wb>') as f:<EOL><INDENT>f.write('<STR_LIT>'.encode('<STR_LIT:utf-8>'))<EOL>np.array([flow.shape[<NUM_LIT:1>], flow.shape[<NUM_LIT:0>]], dtype=np.int32).tofile(f)<EOL>flow = flow.astype(np.float32)<EOL>flow.tofile(f)<EOL>f.flush()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>assert concat_axis in [<NUM_LIT:0>, <NUM_LIT:1>]<EOL>dx, dy = quantize_flow(flow, *args, **kwargs)<EOL>dxdy = np.concatenate((dx, dy), axis=concat_axis)<EOL>imwrite(dxdy, filename)<EOL><DEDENT>

Write optical flow to file.

    If the flow is not quantized, it will be saved as a .flo file losslessly,
    otherwise a jpeg image which is lossy but of much smaller size. (dx and dy
    will be concatenated horizontally into a single image if quantize is True.)

    Args:
        flow (ndarray): (h, w, 2) array of optical flow.
        filename (str): Output filepath.
        quantize (bool): Whether to quantize the flow and save it to 2 jpeg
            images. If set to True, remaining args will be passed to
            :func:`quantize_flow`.
        concat_axis (int): The axis that dx and dy are concatenated,
            can be either 0 or 1. Ignored if quantize is False.


def quantize_flow(flow, max_val=<NUM_LIT>, norm=True):

h, w, _ = flow.shape<EOL>dx = flow[..., <NUM_LIT:0>]<EOL>dy = flow[..., <NUM_LIT:1>]<EOL>if norm:<EOL><INDENT>dx = dx / w  <EOL>dy = dy / h<EOL><DEDENT>flow_comps = [<EOL>quantize(d, -max_val, max_val, <NUM_LIT:255>, np.uint8) for d in [dx, dy]<EOL>]<EOL>return tuple(flow_comps)<EOL>

Quantize flow to [0, 255].

    After this step, the size of flow will be much smaller, and can be
    dumped as jpeg images.

    Args:
        flow (ndarray): (h, w, 2) array of optical flow.
        max_val (float): Maximum value of flow, values beyond
                        [-max_val, max_val] will be truncated.
        norm (bool): Whether to divide flow values by image width/height.

    Returns:
        tuple[ndarray]: Quantized dx and dy.


def dequantize_flow(dx, dy, max_val=<NUM_LIT>, denorm=True):

assert dx.shape == dy.shape<EOL>assert dx.ndim == <NUM_LIT:2> or (dx.ndim == <NUM_LIT:3> and dx.shape[-<NUM_LIT:1>] == <NUM_LIT:1>)<EOL>dx, dy = [dequantize(d, -max_val, max_val, <NUM_LIT:255>) for d in [dx, dy]]<EOL>if denorm:<EOL><INDENT>dx *= dx.shape[<NUM_LIT:1>]<EOL>dy *= dx.shape[<NUM_LIT:0>]<EOL><DEDENT>flow = np.dstack((dx, dy))<EOL>return flow<EOL>

Recover from quantized flow.

    Args:
        dx (ndarray): Quantized dx.
        dy (ndarray): Quantized dy.
        max_val (float): Maximum value used when quantizing.
        denorm (bool): Whether to multiply flow values with width/height.

    Returns:
        ndarray: Dequantized flow.


@requires_executable('<STR_LIT>')<EOL>def convert_video(in_file, out_file, print_cmd=False, pre_options='<STR_LIT>',<EOL>**kwargs):

options = []<EOL>for k, v in kwargs.items():<EOL><INDENT>if isinstance(v, bool):<EOL><INDENT>if v:<EOL><INDENT>options.append('<STR_LIT>'.format(k))<EOL><DEDENT><DEDENT>elif k == '<STR_LIT>':<EOL><INDENT>assert v in [<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT:error>', '<STR_LIT>', '<STR_LIT:info>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>'<EOL>]<EOL>options.append('<STR_LIT>'.format(v))<EOL><DEDENT>else:<EOL><INDENT>options.append('<STR_LIT>'.format(k, v))<EOL><DEDENT><DEDENT>cmd = '<STR_LIT>'.format(pre_options, in_file,<EOL>'<STR_LIT:U+0020>'.join(options), out_file)<EOL>if print_cmd:<EOL><INDENT>print(cmd)<EOL><DEDENT>subprocess.call(cmd, shell=True)<EOL>

Convert a video with ffmpeg.

    This provides a general api to ffmpeg, the executed command is::

        `ffmpeg -y <pre_options> -i <in_file> <options> <out_file>`

    Options(kwargs) are mapped to ffmpeg commands with the following rules:

    - key=val: "-key val"
    - key=True: "-key"
    - key=False: ""

    Args:
        in_file (str): Input video filename.
        out_file (str): Output video filename.
        pre_options (str): Options appears before "-i <in_file>".
        print_cmd (bool): Whether to print the final ffmpeg command.


if size is None and ratio is None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>elif size is not None and ratio is not None:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>options = {'<STR_LIT>': log_level}<EOL>if size:<EOL><INDENT>if not keep_ar:<EOL><INDENT>options['<STR_LIT>'] = '<STR_LIT>'.format(size[<NUM_LIT:0>], size[<NUM_LIT:1>])<EOL><DEDENT>else:<EOL><INDENT>options['<STR_LIT>'] = ('<STR_LIT>'<EOL>'<STR_LIT>'.format(size[<NUM_LIT:0>], size[<NUM_LIT:1>]))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if not isinstance(ratio, tuple):<EOL><INDENT>ratio = (ratio, ratio)<EOL><DEDENT>options['<STR_LIT>'] = '<STR_LIT>'.format(<EOL>ratio[<NUM_LIT:0>], ratio[<NUM_LIT:1>])<EOL><DEDENT>convert_video(in_file, out_file, print_cmd, **options)<EOL>

Resize a video.

    Args:
        in_file (str): Input video filename.
        out_file (str): Output video filename.
        size (tuple): Expected size (w, h), eg, (320, 240) or (320, -1).
        ratio (tuple or float): Expected resize ratio, (2, 0.5) means
            (w*2, h*0.5).
        keep_ar (bool): Whether to keep original aspect ratio.
        log_level (str): Logging level of ffmpeg.
        print_cmd (bool): Whether to print the final ffmpeg command.


options = {'<STR_LIT>': log_level}<EOL>if vcodec is None:<EOL><INDENT>options['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>if acodec is None:<EOL><INDENT>options['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>if start:<EOL><INDENT>options['<STR_LIT>'] = start<EOL><DEDENT>else:<EOL><INDENT>start = <NUM_LIT:0><EOL><DEDENT>if end:<EOL><INDENT>options['<STR_LIT:t>'] = end - start<EOL><DEDENT>convert_video(in_file, out_file, print_cmd, **options)<EOL>

Cut a clip from a video.

    Args:
        in_file (str): Input video filename.
        out_file (str): Output video filename.
        start (None or float): Start time (in seconds).
        end (None or float): End time (in seconds).
        vcodec (None or str): Output video codec, None for unchanged.
        acodec (None or str): Output audio codec, None for unchanged.
        log_level (str): Logging level of ffmpeg.
        print_cmd (bool): Whether to print the final ffmpeg command.


_, tmp_filename = tempfile.mkstemp(suffix='<STR_LIT>', text=True)<EOL>with open(tmp_filename, '<STR_LIT:w>') as f:<EOL><INDENT>for filename in video_list:<EOL><INDENT>f.write('<STR_LIT>'.format(osp.abspath(filename)))<EOL><DEDENT><DEDENT>options = {'<STR_LIT>': log_level}<EOL>if vcodec is None:<EOL><INDENT>options['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>if acodec is None:<EOL><INDENT>options['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT>convert_video(<EOL>tmp_filename,<EOL>out_file,<EOL>print_cmd,<EOL>pre_options='<STR_LIT>',<EOL>**options)<EOL>os.remove(tmp_filename)<EOL>

Concatenate multiple videos into a single one.

    Args:
        video_list (list): A list of video filenames
        out_file (str): Output video filename
        vcodec (None or str): Output video codec, None for unchanged
        acodec (None or str): Output audio codec, None for unchanged
        log_level (str): Logging level of ffmpeg.
        print_cmd (bool): Whether to print the final ffmpeg command.


if end == <NUM_LIT:0>:<EOL><INDENT>ext = filename_tmpl.split('<STR_LIT:.>')[-<NUM_LIT:1>]<EOL>end = len([name for name in scandir(frame_dir, ext)])<EOL><DEDENT>first_file = osp.join(frame_dir, filename_tmpl.format(start))<EOL>check_file_exist(first_file, '<STR_LIT>' + first_file)<EOL>img = cv2.imread(first_file)<EOL>height, width = img.shape[:<NUM_LIT:2>]<EOL>resolution = (width, height)<EOL>vwriter = cv2.VideoWriter(video_file, VideoWriter_fourcc(*fourcc), fps,<EOL>resolution)<EOL>def write_frame(file_idx):<EOL><INDENT>filename = osp.join(frame_dir, filename_tmpl.format(file_idx))<EOL>img = cv2.imread(filename)<EOL>vwriter.write(img)<EOL><DEDENT>if show_progress:<EOL><INDENT>track_progress(write_frame, range(start, end))<EOL><DEDENT>else:<EOL><INDENT>for i in range(start, end):<EOL><INDENT>filename = osp.join(frame_dir, filename_tmpl.format(i))<EOL>img = cv2.imread(filename)<EOL>vwriter.write(img)<EOL><DEDENT><DEDENT>vwriter.release()<EOL>

Read the frame images from a directory and join them as a video

    Args:
        frame_dir (str): The directory containing video frames.
        video_file (str): Output filename.
        fps (float): FPS of the output video.
        fourcc (str): Fourcc of the output video, this should be compatible
            with the output file type.
        filename_tmpl (str): Filename template with the index as the variable.
        start (int): Starting frame index.
        end (int): Ending frame index.
        show_progress (bool): Whether to show a progress bar.


return self._vcap<EOL>

:obj:`cv2.VideoCapture`: The raw VideoCapture object.


return self._vcap.isOpened()<EOL>

bool: Indicate whether the video is opened.


return self._width<EOL>


return self._height<EOL>


return (self._width, self._height)<EOL>

tuple: Video resolution (width, height).


return self._fps<EOL>


return self._frame_cnt<EOL>


return self._fourcc<EOL>

str: "Four character code" of the video.


return self._position<EOL>

int: Current cursor position, indicating frame decoded.


<EOL>if self._cache:<EOL><INDENT>img = self._cache.get(self._position)<EOL>if img is not None:<EOL><INDENT>ret = True<EOL><DEDENT>else:<EOL><INDENT>if self._position != self._get_real_position():<EOL><INDENT>self._set_real_position(self._position)<EOL><DEDENT>ret, img = self._vcap.read()<EOL>if ret:<EOL><INDENT>self._cache.put(self._position, img)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>ret, img = self._vcap.read()<EOL><DEDENT>if ret:<EOL><INDENT>self._position += <NUM_LIT:1><EOL><DEDENT>return img<EOL>

Read the next frame.

        If the next frame have been decoded before and in the cache, then
        return it directly, otherwise decode, cache and return it.

        Returns:
            ndarray or None: Return the frame if successful, otherwise None.


if frame_id < <NUM_LIT:0> or frame_id >= self._frame_cnt:<EOL><INDENT>raise IndexError(<EOL>'<STR_LIT>'.format(self._frame_cnt -<EOL><NUM_LIT:1>))<EOL><DEDENT>if frame_id == self._position:<EOL><INDENT>return self.read()<EOL><DEDENT>if self._cache:<EOL><INDENT>img = self._cache.get(frame_id)<EOL>if img is not None:<EOL><INDENT>self._position = frame_id + <NUM_LIT:1><EOL>return img<EOL><DEDENT><DEDENT>self._set_real_position(frame_id)<EOL>ret, img = self._vcap.read()<EOL>if ret:<EOL><INDENT>if self._cache:<EOL><INDENT>self._cache.put(self._position, img)<EOL><DEDENT>self._position += <NUM_LIT:1><EOL><DEDENT>return img<EOL>

Get frame by index.

        Args:
            frame_id (int): Index of the expected frame, 0-based.

        Returns:
            ndarray or None: Return the frame if successful, otherwise None.


if self._position == <NUM_LIT:0>:<EOL><INDENT>return None<EOL><DEDENT>return self._cache.get(self._position - <NUM_LIT:1>)<EOL>

Get the current frame (frame that is just visited).

        Returns:
            ndarray or None: If the video is fresh, return None, otherwise
                return the frame.


mkdir_or_exist(frame_dir)<EOL>if max_num == <NUM_LIT:0>:<EOL><INDENT>task_num = self.frame_cnt - start<EOL><DEDENT>else:<EOL><INDENT>task_num = min(self.frame_cnt - start, max_num)<EOL><DEDENT>if task_num <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>if start > <NUM_LIT:0>:<EOL><INDENT>self._set_real_position(start)<EOL><DEDENT>def write_frame(file_idx):<EOL><INDENT>img = self.read()<EOL>filename = osp.join(frame_dir, filename_tmpl.format(file_idx))<EOL>cv2.imwrite(filename, img)<EOL><DEDENT>if show_progress:<EOL><INDENT>track_progress(write_frame, range(file_start,<EOL>file_start + task_num))<EOL><DEDENT>else:<EOL><INDENT>for i in range(task_num):<EOL><INDENT>img = self.read()<EOL>if img is None:<EOL><INDENT>break<EOL><DEDENT>filename = osp.join(frame_dir,<EOL>filename_tmpl.format(i + file_start))<EOL>cv2.imwrite(filename, img)<EOL><DEDENT><DEDENT>

Convert a video to frame images

        Args:
            frame_dir (str): Output directory to store all the frame images.
            file_start (int): Filenames will start from the specified number.
            filename_tmpl (str): Filename template with the index as the
                placeholder.
            start (int): The starting frame index.
            max_num (int): Maximum number of frames to be written.
            show_progress (bool): Whether to show a progress bar.


assert n >= <NUM_LIT:0><EOL>for key in self.val_history:<EOL><INDENT>values = np.array(self.val_history[key][-n:])<EOL>nums = np.array(self.n_history[key][-n:])<EOL>avg = np.sum(values * nums) / np.sum(nums)<EOL>self.output[key] = avg<EOL><DEDENT>self.ready = True<EOL>


def load_state_dict(module, state_dict, strict=False, logger=None):

unexpected_keys = []<EOL>own_state = module.state_dict()<EOL>for name, param in state_dict.items():<EOL><INDENT>if name not in own_state:<EOL><INDENT>unexpected_keys.append(name)<EOL>continue<EOL><DEDENT>if isinstance(param, torch.nn.Parameter):<EOL><INDENT>param = param.data<EOL><DEDENT>try:<EOL><INDENT>own_state[name].copy_(param)<EOL><DEDENT>except Exception:<EOL><INDENT>raise RuntimeError('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>.format(name, own_state[name].size(),<EOL>param.size()))<EOL><DEDENT><DEDENT>missing_keys = set(own_state.keys()) - set(state_dict.keys())<EOL>err_msg = []<EOL>if unexpected_keys:<EOL><INDENT>err_msg.append('<STR_LIT>'.format(<EOL>'<STR_LIT:U+002CU+0020>'.join(unexpected_keys)))<EOL><DEDENT>if missing_keys:<EOL><INDENT>err_msg.append('<STR_LIT>'.format(<EOL>'<STR_LIT:U+002CU+0020>'.join(missing_keys)))<EOL><DEDENT>err_msg = '<STR_LIT:\n>'.join(err_msg)<EOL>if err_msg:<EOL><INDENT>if strict:<EOL><INDENT>raise RuntimeError(err_msg)<EOL><DEDENT>elif logger is not None:<EOL><INDENT>logger.warn(err_msg)<EOL><DEDENT>else:<EOL><INDENT>print(err_msg)<EOL><DEDENT><DEDENT>

Load state_dict to a module.

    This method is modified from :meth:`torch.nn.Module.load_state_dict`.
    Default value for ``strict`` is set to ``False`` and the message for
    param mismatch will be shown even if strict is False.

    Args:
        module (Module): Module that receives the state_dict.
        state_dict (OrderedDict): Weights.
        strict (bool): whether to strictly enforce that the keys
            in :attr:`state_dict` match the keys returned by this module's
            :meth:`~torch.nn.Module.state_dict` function. Default: ``False``.
        logger (:obj:`logging.Logger`, optional): Logger to log the error
            message. If not specified, print function will be used.


<EOL>if filename.startswith('<STR_LIT>'):<EOL><INDENT>import torchvision<EOL>model_urls = dict()<EOL>for _, name, ispkg in pkgutil.walk_packages(<EOL>torchvision.models.__path__):<EOL><INDENT>if not ispkg:<EOL><INDENT>_zoo = import_module('<STR_LIT>'.format(name))<EOL>_urls = getattr(_zoo, '<STR_LIT>')<EOL>model_urls.update(_urls)<EOL><DEDENT><DEDENT>model_name = filename[<NUM_LIT:11>:]<EOL>checkpoint = model_zoo.load_url(model_urls[model_name])<EOL><DEDENT>elif filename.startswith('<STR_LIT>'):<EOL><INDENT>model_name = filename[<NUM_LIT>:]<EOL>checkpoint = model_zoo.load_url(open_mmlab_model_urls[model_name])<EOL><DEDENT>elif filename.startswith(('<STR_LIT>', '<STR_LIT>')):<EOL><INDENT>checkpoint = model_zoo.load_url(filename)<EOL><DEDENT>else:<EOL><INDENT>if not osp.isfile(filename):<EOL><INDENT>raise IOError('<STR_LIT>'.format(filename))<EOL><DEDENT>checkpoint = torch.load(filename, map_location=map_location)<EOL><DEDENT>if isinstance(checkpoint, OrderedDict):<EOL><INDENT>state_dict = checkpoint<EOL><DEDENT>elif isinstance(checkpoint, dict) and '<STR_LIT>' in checkpoint:<EOL><INDENT>state_dict = checkpoint['<STR_LIT>']<EOL><DEDENT>else:<EOL><INDENT>raise RuntimeError(<EOL>'<STR_LIT>'.format(filename))<EOL><DEDENT>if list(state_dict.keys())[<NUM_LIT:0>].startswith('<STR_LIT>'):<EOL><INDENT>state_dict = {k[<NUM_LIT:7>:]: v for k, v in checkpoint['<STR_LIT>'].items()}<EOL><DEDENT>if hasattr(model, '<STR_LIT>'):<EOL><INDENT>load_state_dict(model.module, state_dict, strict, logger)<EOL><DEDENT>else:<EOL><INDENT>load_state_dict(model, state_dict, strict, logger)<EOL><DEDENT>return checkpoint<EOL>

Load checkpoint from a file or URI.

    Args:
        model (Module): Module to load checkpoint.
        filename (str): Either a filepath or URL or modelzoo://xxxxxxx.
        map_location (str): Same as :func:`torch.load`.
        strict (bool): Whether to allow different params for the model and
            checkpoint.
        logger (:mod:`logging.Logger` or None): The logger for error message.

    Returns:
        dict or OrderedDict: The loaded checkpoint.


state_dict_cpu = OrderedDict()<EOL>for key, val in state_dict.items():<EOL><INDENT>state_dict_cpu[key] = val.cpu()<EOL><DEDENT>return state_dict_cpu<EOL>

Copy a model state_dict to cpu.

    Args:
        state_dict (OrderedDict): Model weights on GPU.

    Returns:
        OrderedDict: Model weights on GPU.


def save_checkpoint(model, filename, optimizer=None, meta=None):

if meta is None:<EOL><INDENT>meta = {}<EOL><DEDENT>elif not isinstance(meta, dict):<EOL><INDENT>raise TypeError('<STR_LIT>'.format(<EOL>type(meta)))<EOL><DEDENT>meta.update(mmcv_version=mmcv.__version__, time=time.asctime())<EOL>mmcv.mkdir_or_exist(osp.dirname(filename))<EOL>if hasattr(model, '<STR_LIT>'):<EOL><INDENT>model = model.module<EOL><DEDENT>checkpoint = {<EOL>'<STR_LIT>': meta,<EOL>'<STR_LIT>': weights_to_cpu(model.state_dict())<EOL>}<EOL>if optimizer is not None:<EOL><INDENT>checkpoint['<STR_LIT>'] = optimizer.state_dict()<EOL><DEDENT>torch.save(checkpoint, filename)<EOL>

Save checkpoint to file.

    The checkpoint will have 3 fields: ``meta``, ``state_dict`` and
    ``optimizer``. By default ``meta`` will contain version and time info.

    Args:
        model (Module): Module whose params are to be saved.
        filename (str): Checkpoint filename.
        optimizer (:obj:`Optimizer`, optional): Optimizer to be saved.
        meta (dict, optional): Metadata to be saved in checkpoint.


def obj_from_dict(info, parent=None, default_args=None):

assert isinstance(info, dict) and '<STR_LIT:type>' in info<EOL>assert isinstance(default_args, dict) or default_args is None<EOL>args = info.copy()<EOL>obj_type = args.pop('<STR_LIT:type>')<EOL>if mmcv.is_str(obj_type):<EOL><INDENT>if parent is not None:<EOL><INDENT>obj_type = getattr(parent, obj_type)<EOL><DEDENT>else:<EOL><INDENT>obj_type = sys.modules[obj_type]<EOL><DEDENT><DEDENT>elif not isinstance(obj_type, type):<EOL><INDENT>raise TypeError('<STR_LIT>'.format(<EOL>type(obj_type)))<EOL><DEDENT>if default_args is not None:<EOL><INDENT>for name, value in default_args.items():<EOL><INDENT>args.setdefault(name, value)<EOL><DEDENT><DEDENT>return obj_type(**args)<EOL>

Initialize an object from dict.

    The dict must contain the key "type", which indicates the object type, it
    can be either a string or type, such as "list" or ``list``. Remaining
    fields are treated as the arguments for constructing the object.

    Args:
        info (dict): Object types and arguments.
        parent (:class:`module`): Module which may containing expected object
            classes.
        default_args (dict, optional): Default arguments for initializing the
            object.

    Returns:
        any type: Object built from the dict.


if isinstance(priority, int):<EOL><INDENT>if priority < <NUM_LIT:0> or priority > <NUM_LIT:100>:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>return priority<EOL><DEDENT>elif isinstance(priority, Priority):<EOL><INDENT>return priority.value<EOL><DEDENT>elif isinstance(priority, str):<EOL><INDENT>return Priority[priority.upper()].value<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>

Get priority value.

    Args:
        priority (int or str or :obj:`Priority`): Priority.

    Returns:
        int: The priority value.


return self._model_name<EOL>

str: Name of the model, usually the module class name.


return self._rank<EOL>

int: Rank of current process. (distributed training)


return self._world_size<EOL>

int: Number of processes participating in the job.
        (distributed training)


return self._hooks<EOL>

list[:obj:`Hook`]: A list of registered hooks.


return self._epoch<EOL>


return self._iter<EOL>


return self._inner_iter<EOL>


return self._max_epochs<EOL>


return self._max_iters<EOL>


if isinstance(optimizer, dict):<EOL><INDENT>optimizer = obj_from_dict(<EOL>optimizer, torch.optim, dict(params=self.model.parameters()))<EOL><DEDENT>elif not isinstance(optimizer, torch.optim.Optimizer):<EOL><INDENT>raise TypeError(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(type(optimizer)))<EOL><DEDENT>return optimizer<EOL>

Init the optimizer.

        Args:
            optimizer (dict or :obj:`~torch.optim.Optimizer`): Either an
                optimizer object or a dict used for constructing the optimizer.

        Returns:
            :obj:`~torch.optim.Optimizer`: An optimizer object.

        Examples:
            >>> optimizer = dict(type='SGD', lr=0.01, momentum=0.9)
            >>> type(runner.init_optimizer(optimizer))
            <class 'torch.optim.sgd.SGD'>


def init_logger(self, log_dir=None, level=logging.INFO):

logging.basicConfig(<EOL>format='<STR_LIT>', level=level)<EOL>logger = logging.getLogger(__name__)<EOL>if log_dir and self.rank == <NUM_LIT:0>:<EOL><INDENT>filename = '<STR_LIT>'.format(self.timestamp)<EOL>log_file = osp.join(log_dir, filename)<EOL>self._add_file_handler(logger, log_file, level=level)<EOL><DEDENT>return logger<EOL>

Init the logger.

        Args:
            log_dir(str, optional): Log file directory. If not specified, no
                log file will be used.
            level (int or str): See the built-in python logging module.

        Returns:
            :obj:`~logging.Logger`: Python logger.


if self.optimizer is None:<EOL><INDENT>raise RuntimeError(<EOL>'<STR_LIT>')<EOL><DEDENT>return [group['<STR_LIT>'] for group in self.optimizer.param_groups]<EOL>

Get current learning rates.

        Returns:
            list: Current learning rate of all param groups.


def register_hook(self, hook, priority='<STR_LIT>'):

assert isinstance(hook, Hook)<EOL>if hasattr(hook, '<STR_LIT>'):<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>priority = get_priority(priority)<EOL>hook.priority = priority<EOL>inserted = False<EOL>for i in range(len(self._hooks) - <NUM_LIT:1>, -<NUM_LIT:1>, -<NUM_LIT:1>):<EOL><INDENT>if priority >= self._hooks[i].priority:<EOL><INDENT>self._hooks.insert(i + <NUM_LIT:1>, hook)<EOL>inserted = True<EOL>break<EOL><DEDENT><DEDENT>if not inserted:<EOL><INDENT>self._hooks.insert(<NUM_LIT:0>, hook)<EOL><DEDENT>

Register a hook into the hook list.

        Args:
            hook (:obj:`Hook`): The hook to be registered.
            priority (int or str or :obj:`Priority`): Hook priority.
                Lower value means higher priority.


def run(self, data_loaders, workflow, max_epochs, **kwargs):

assert isinstance(data_loaders, list)<EOL>assert mmcv.is_list_of(workflow, tuple)<EOL>assert len(data_loaders) == len(workflow)<EOL>self._max_epochs = max_epochs<EOL>work_dir = self.work_dir if self.work_dir is not None else '<STR_LIT>'<EOL>self.logger.info('<STR_LIT>',<EOL>get_host_info(), work_dir)<EOL>self.logger.info('<STR_LIT>', workflow, max_epochs)<EOL>self.call_hook('<STR_LIT>')<EOL>while self.epoch < max_epochs:<EOL><INDENT>for i, flow in enumerate(workflow):<EOL><INDENT>mode, epochs = flow<EOL>if isinstance(mode, str):  <EOL><INDENT>if not hasattr(self, mode):<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>'.<EOL>format(mode))<EOL><DEDENT>epoch_runner = getattr(self, mode)<EOL><DEDENT>elif callable(mode):  <EOL><INDENT>epoch_runner = mode<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>'<EOL>'<STR_LIT>'.format(<EOL>type(mode)))<EOL><DEDENT>for _ in range(epochs):<EOL><INDENT>if mode == '<STR_LIT:train>' and self.epoch >= max_epochs:<EOL><INDENT>return<EOL><DEDENT>epoch_runner(data_loaders[i], **kwargs)<EOL><DEDENT><DEDENT><DEDENT>time.sleep(<NUM_LIT:1>)  <EOL>self.call_hook('<STR_LIT>')<EOL>

Start running.

        Args:
            data_loaders (list[:obj:`DataLoader`]): Dataloaders for training
                and validation.
            workflow (list[tuple]): A list of (phase, epochs) to specify the
                running order and epochs. E.g, [('train', 2), ('val', 1)] means
                running 2 epochs for training and 1 epoch for validation,
                iteratively.
            max_epochs (int): Total training epochs.


if optimizer_config is None:<EOL><INDENT>optimizer_config = {}<EOL><DEDENT>if checkpoint_config is None:<EOL><INDENT>checkpoint_config = {}<EOL><DEDENT>self.register_lr_hooks(lr_config)<EOL>self.register_hook(self.build_hook(optimizer_config, OptimizerHook))<EOL>self.register_hook(self.build_hook(checkpoint_config, CheckpointHook))<EOL>self.register_hook(IterTimerHook())<EOL>if log_config is not None:<EOL><INDENT>self.register_logger_hooks(log_config)<EOL><DEDENT>

Register default hooks for training.

        Default hooks include:

        - LrUpdaterHook
        - OptimizerStepperHook
        - CheckpointSaverHook
        - IterTimerHook
        - LoggerHook(s)


partial_parser = ArgumentParser(description=description)<EOL>partial_parser.add_argument('<STR_LIT>', help='<STR_LIT>')<EOL>cfg_file = partial_parser.parse_known_args()[<NUM_LIT:0>].config<EOL>cfg = Config.from_file(cfg_file)<EOL>parser = ArgumentParser(description=description)<EOL>parser.add_argument('<STR_LIT>', help='<STR_LIT>')<EOL>add_args(parser, cfg)<EOL>return parser, cfg<EOL>

Generate argparser from config file automatically (experimental)


return isinstance(x, six.string_types)<EOL>

Whether the input is an string instance.


def iter_cast(inputs, dst_type, return_type=None):

if not isinstance(inputs, collections_abc.Iterable):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>if not isinstance(dst_type, type):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>out_iterable = six.moves.map(dst_type, inputs)<EOL>if return_type is None:<EOL><INDENT>return out_iterable<EOL><DEDENT>else:<EOL><INDENT>return return_type(out_iterable)<EOL><DEDENT>

Cast elements of an iterable object into some type.

    Args:
        inputs (Iterable): The input object.
        dst_type (type): Destination type.
        return_type (type, optional): If specified, the output object will be
            converted to this type, otherwise an iterator.

    Returns:
        iterator or specified type: The converted object.


return iter_cast(inputs, dst_type, return_type=list)<EOL>

Cast elements of an iterable object into a list of some type.

    A partial method of :func:`iter_cast`.


return iter_cast(inputs, dst_type, return_type=tuple)<EOL>

Cast elements of an iterable object into a tuple of some type.

    A partial method of :func:`iter_cast`.


def is_seq_of(seq, expected_type, seq_type=None):

if seq_type is None:<EOL><INDENT>exp_seq_type = collections_abc.Sequence<EOL><DEDENT>else:<EOL><INDENT>assert isinstance(seq_type, type)<EOL>exp_seq_type = seq_type<EOL><DEDENT>if not isinstance(seq, exp_seq_type):<EOL><INDENT>return False<EOL><DEDENT>for item in seq:<EOL><INDENT>if not isinstance(item, expected_type):<EOL><INDENT>return False<EOL><DEDENT><DEDENT>return True<EOL>

Check whether it is a sequence of some type.

    Args:
        seq (Sequence): The sequence to be checked.
        expected_type (type): Expected type of sequence items.
        seq_type (type, optional): Expected sequence type.

    Returns:
        bool: Whether the sequence is valid.


return is_seq_of(seq, expected_type, seq_type=list)<EOL>

Check whether it is a list of some type.

    A partial method of :func:`is_seq_of`.


return is_seq_of(seq, expected_type, seq_type=tuple)<EOL>

Check whether it is a tuple of some type.

    A partial method of :func:`is_seq_of`.


if not isinstance(lens, list):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>elif sum(lens) != len(in_list):<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>'.format(<EOL>sum(lens), len(in_list)))<EOL><DEDENT>out_list = []<EOL>idx = <NUM_LIT:0><EOL>for i in range(len(lens)):<EOL><INDENT>out_list.append(in_list[idx:idx + lens[i]])<EOL>idx += lens[i]<EOL><DEDENT>return out_list<EOL>

Slice a list into several sub lists by a list of given length.

    Args:
        in_list (list): The list to be sliced.
        lens(int or list): The expected length of each out list.

    Returns:
        list: A list of sliced list.


return list(itertools.chain(*in_list))<EOL>

Concatenate a list of list into a single list.

    Args:
        in_list (list): The list of list to be merged.

    Returns:
        list: The concatenated flat list.


def wrap(func):<EOL><INDENT>@functools.wraps(func)<EOL>def wrapped_func(*args, **kwargs):<EOL><INDENT>requirements = [prerequisites] if isinstance(<EOL>prerequisites, str) else prerequisites<EOL>missing = []<EOL>for item in requirements:<EOL><INDENT>if not checker(item):<EOL><INDENT>missing.append(item)<EOL><DEDENT><DEDENT>if missing:<EOL><INDENT>print(msg_tmpl.format('<STR_LIT:U+002CU+0020>'.join(missing), func.__name__))<EOL>raise RuntimeError('<STR_LIT>')<EOL><DEDENT>else:<EOL><INDENT>return func(*args, **kwargs)<EOL><DEDENT><DEDENT>return wrapped_func<EOL><DEDENT>return wrap<EOL>

A decorator factory to check if prerequisites are satisfied.

    Args:
        prerequisites (str of list[str]): Prerequisites to be checked.
        checker (callable): The checker method that returns True if a
            prerequisite is meet, False otherwise.
        msg_tmpl (str): The message template with two variables.

    Returns:
        decorator: A specific decorator.


return check_prerequisites(prerequisites, checker=_check_py_package)<EOL>

A decorator to check if some python packages are installed.

    Example:
        >>> @requires_package('numpy')
        >>> func(arg1, args):
        >>>     return numpy.zeros(1)
        array([0.])
        >>> @requires_package(['numpy', 'non_package'])
        >>> func(arg1, args):
        >>>     return numpy.zeros(1)
        ImportError


return check_prerequisites(prerequisites, checker=_check_executable)<EOL>

A decorator to check if some executable files are installed.

    Example:
        >>> @requires_executable('ffmpeg')
        >>> func(arg1, args):
        >>>     print(1)
        1


if timer_id not in _g_timers:<EOL><INDENT>_g_timers[timer_id] = Timer()<EOL>return <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>return _g_timers[timer_id].since_last_check()<EOL><DEDENT>

Add check points in a single line.

    This method is suitable for running a task on a list of items. A timer will
    be registered when the method is called for the first time.

    :Example:

    >>> import time
    >>> import mmcv
    >>> for i in range(1, 6):
    >>>     # simulate a code block
    >>>     time.sleep(i)
    >>>     mmcv.check_time('task1')
    2.000
    3.000
    4.000
    5.000

    Args:
        timer_id (str): Timer identifier.


return self._is_running<EOL>

bool: indicate whether the timer is running


if not self._is_running:<EOL><INDENT>self._t_start = time()<EOL>self._is_running = True<EOL><DEDENT>self._t_last = time()<EOL>


if not self._is_running:<EOL><INDENT>raise TimerError('<STR_LIT>')<EOL><DEDENT>self._t_last = time()<EOL>return self._t_last - self._t_start<EOL>

Total time since the timer is started.

        Returns (float): Time in seconds.


if not self._is_running:<EOL><INDENT>raise TimerError('<STR_LIT>')<EOL><DEDENT>dur = time() - self._t_last<EOL>self._t_last = time()<EOL>return dur<EOL>

Time since the last checking.

        Either :func:`since_start` or :func:`since_last_check` is a checking
        operation.

        Returns (float): Time in seconds.


def track_progress(func, tasks, bar_width=<NUM_LIT:50>, **kwargs):

if isinstance(tasks, tuple):<EOL><INDENT>assert len(tasks) == <NUM_LIT:2><EOL>assert isinstance(tasks[<NUM_LIT:0>], collections_abc.Iterable)<EOL>assert isinstance(tasks[<NUM_LIT:1>], int)<EOL>task_num = tasks[<NUM_LIT:1>]<EOL>tasks = tasks[<NUM_LIT:0>]<EOL><DEDENT>elif isinstance(tasks, collections_abc.Iterable):<EOL><INDENT>task_num = len(tasks)<EOL><DEDENT>else:<EOL><INDENT>raise TypeError(<EOL>'<STR_LIT>')<EOL><DEDENT>prog_bar = ProgressBar(task_num, bar_width)<EOL>results = []<EOL>for task in tasks:<EOL><INDENT>results.append(func(task, **kwargs))<EOL>prog_bar.update()<EOL><DEDENT>sys.stdout.write('<STR_LIT:\n>')<EOL>return results<EOL>

Track the progress of tasks execution with a progress bar.

    Tasks are done with a simple for-loop.

    Args:
        func (callable): The function to be applied to each task.
        tasks (list or tuple[Iterable, int]): A list of tasks or
            (tasks, total num).
        bar_width (int): Width of progress bar.

    Returns:
        list: The task results.


if isinstance(tasks, tuple):<EOL><INDENT>assert len(tasks) == <NUM_LIT:2><EOL>assert isinstance(tasks[<NUM_LIT:0>], collections_abc.Iterable)<EOL>assert isinstance(tasks[<NUM_LIT:1>], int)<EOL>task_num = tasks[<NUM_LIT:1>]<EOL>tasks = tasks[<NUM_LIT:0>]<EOL><DEDENT>elif isinstance(tasks, collections_abc.Iterable):<EOL><INDENT>task_num = len(tasks)<EOL><DEDENT>else:<EOL><INDENT>raise TypeError(<EOL>'<STR_LIT>')<EOL><DEDENT>pool = init_pool(nproc, initializer, initargs)<EOL>start = not skip_first<EOL>task_num -= nproc * chunksize * int(skip_first)<EOL>prog_bar = ProgressBar(task_num, bar_width, start)<EOL>results = []<EOL>if keep_order:<EOL><INDENT>gen = pool.imap(func, tasks, chunksize)<EOL><DEDENT>else:<EOL><INDENT>gen = pool.imap_unordered(func, tasks, chunksize)<EOL><DEDENT>for result in gen:<EOL><INDENT>results.append(result)<EOL>if skip_first:<EOL><INDENT>if len(results) < nproc * chunksize:<EOL><INDENT>continue<EOL><DEDENT>elif len(results) == nproc * chunksize:<EOL><INDENT>prog_bar.start()<EOL>continue<EOL><DEDENT><DEDENT>prog_bar.update()<EOL><DEDENT>sys.stdout.write('<STR_LIT:\n>')<EOL>pool.close()<EOL>pool.join()<EOL>return results<EOL>

Track the progress of parallel task execution with a progress bar.

    The built-in :mod:`multiprocessing` module is used for process pools and
    tasks are done with :func:`Pool.map` or :func:`Pool.imap_unordered`.

    Args:
        func (callable): The function to be applied to each task.
        tasks (list or tuple[Iterable, int]): A list of tasks or
            (tasks, total num).
        nproc (int): Process (worker) number.
        initializer (None or callable): Refer to :class:`multiprocessing.Pool`
            for details.
        initargs (None or tuple): Refer to :class:`multiprocessing.Pool` for
            details.
        chunksize (int): Refer to :class:`multiprocessing.Pool` for details.
        bar_width (int): Width of progress bar.
        skip_first (bool): Whether to skip the first sample for each worker
            when estimating fps, since the initialization step may takes
            longer.
        keep_order (bool): If True, :func:`Pool.imap` is used, otherwise
            :func:`Pool.imap_unordered` is used.

    Returns:
        list: The task results.


def imshow(img, win_name='<STR_LIT>', wait_time=<NUM_LIT:0>):

cv2.imshow(win_name, imread(img))<EOL>cv2.waitKey(wait_time)<EOL>

Show an image.

    Args:
        img (str or ndarray): The image to be displayed.
        win_name (str): The window name.
        wait_time (int): Value of waitKey param.


img = imread(img)<EOL>if isinstance(bboxes, np.ndarray):<EOL><INDENT>bboxes = [bboxes]<EOL><DEDENT>if not isinstance(colors, list):<EOL><INDENT>colors = [colors for _ in range(len(bboxes))]<EOL><DEDENT>colors = [color_val(c) for c in colors]<EOL>assert len(bboxes) == len(colors)<EOL>for i, _bboxes in enumerate(bboxes):<EOL><INDENT>_bboxes = _bboxes.astype(np.int32)<EOL>if top_k <= <NUM_LIT:0>:<EOL><INDENT>_top_k = _bboxes.shape[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>_top_k = min(top_k, _bboxes.shape[<NUM_LIT:0>])<EOL><DEDENT>for j in range(_top_k):<EOL><INDENT>left_top = (_bboxes[j, <NUM_LIT:0>], _bboxes[j, <NUM_LIT:1>])<EOL>right_bottom = (_bboxes[j, <NUM_LIT:2>], _bboxes[j, <NUM_LIT:3>])<EOL>cv2.rectangle(<EOL>img, left_top, right_bottom, colors[i], thickness=thickness)<EOL><DEDENT><DEDENT>if show:<EOL><INDENT>imshow(img, win_name, wait_time)<EOL><DEDENT>if out_file is not None:<EOL><INDENT>imwrite(img, out_file)<EOL><DEDENT>

Draw bboxes on an image.

    Args:
        img (str or ndarray): The image to be displayed.
        bboxes (list or ndarray): A list of ndarray of shape (k, 4).
        colors (list[str or tuple or Color]): A list of colors.
        top_k (int): Plot the first k bboxes only if set positive.
        thickness (int): Thickness of lines.
        show (bool): Whether to show the image.
        win_name (str): The window name.
        wait_time (int): Value of waitKey param.
        out_file (str, optional): The filename to write the image.


assert bboxes.ndim == <NUM_LIT:2><EOL>assert labels.ndim == <NUM_LIT:1><EOL>assert bboxes.shape[<NUM_LIT:0>] == labels.shape[<NUM_LIT:0>]<EOL>assert bboxes.shape[<NUM_LIT:1>] == <NUM_LIT:4> or bboxes.shape[<NUM_LIT:1>] == <NUM_LIT:5><EOL>img = imread(img)<EOL>if score_thr > <NUM_LIT:0>:<EOL><INDENT>assert bboxes.shape[<NUM_LIT:1>] == <NUM_LIT:5><EOL>scores = bboxes[:, -<NUM_LIT:1>]<EOL>inds = scores > score_thr<EOL>bboxes = bboxes[inds, :]<EOL>labels = labels[inds]<EOL><DEDENT>bbox_color = color_val(bbox_color)<EOL>text_color = color_val(text_color)<EOL>for bbox, label in zip(bboxes, labels):<EOL><INDENT>bbox_int = bbox.astype(np.int32)<EOL>left_top = (bbox_int[<NUM_LIT:0>], bbox_int[<NUM_LIT:1>])<EOL>right_bottom = (bbox_int[<NUM_LIT:2>], bbox_int[<NUM_LIT:3>])<EOL>cv2.rectangle(<EOL>img, left_top, right_bottom, bbox_color, thickness=thickness)<EOL>label_text = class_names[<EOL>label] if class_names is not None else '<STR_LIT>'.format(label)<EOL>if len(bbox) > <NUM_LIT:4>:<EOL><INDENT>label_text += '<STR_LIT>'.format(bbox[-<NUM_LIT:1>])<EOL><DEDENT>cv2.putText(img, label_text, (bbox_int[<NUM_LIT:0>], bbox_int[<NUM_LIT:1>] - <NUM_LIT:2>),<EOL>cv2.FONT_HERSHEY_COMPLEX, font_scale, text_color)<EOL><DEDENT>if show:<EOL><INDENT>imshow(img, win_name, wait_time)<EOL><DEDENT>if out_file is not None:<EOL><INDENT>imwrite(img, out_file)<EOL><DEDENT>

Draw bboxes and class labels (with scores) on an image.

    Args:
        img (str or ndarray): The image to be displayed.
        bboxes (ndarray): Bounding boxes (with scores), shaped (n, 4) or
            (n, 5).
        labels (ndarray): Labels of bboxes.
        class_names (list[str]): Names of each classes.
        score_thr (float): Minimum score of bboxes to be shown.
        bbox_color (str or tuple or :obj:`Color`): Color of bbox lines.
        text_color (str or tuple or :obj:`Color`): Color of texts.
        thickness (int): Thickness of lines.
        font_scale (float): Font scales of texts.
        show (bool): Whether to show the image.
        win_name (str): The window name.
        wait_time (int): Value of waitKey param.
        out_file (str or None): The filename to write the image.


def flowshow(flow, win_name='<STR_LIT>', wait_time=<NUM_LIT:0>):

flow = flowread(flow)<EOL>flow_img = flow2rgb(flow)<EOL>imshow(rgb2bgr(flow_img), win_name, wait_time)<EOL>

Show optical flow.

    Args:
        flow (ndarray or str): The optical flow to be displayed.
        win_name (str): The window name.
        wait_time (int): Value of waitKey param.


def flow2rgb(flow, color_wheel=None, unknown_thr=<NUM_LIT>):

assert flow.ndim == <NUM_LIT:3> and flow.shape[-<NUM_LIT:1>] == <NUM_LIT:2><EOL>if color_wheel is None:<EOL><INDENT>color_wheel = make_color_wheel()<EOL><DEDENT>assert color_wheel.ndim == <NUM_LIT:2> and color_wheel.shape[<NUM_LIT:1>] == <NUM_LIT:3><EOL>num_bins = color_wheel.shape[<NUM_LIT:0>]<EOL>dx = flow[:, :, <NUM_LIT:0>].copy()<EOL>dy = flow[:, :, <NUM_LIT:1>].copy()<EOL>ignore_inds = (np.isnan(dx) | np.isnan(dy) | (np.abs(dx) > unknown_thr) |<EOL>(np.abs(dy) > unknown_thr))<EOL>dx[ignore_inds] = <NUM_LIT:0><EOL>dy[ignore_inds] = <NUM_LIT:0><EOL>rad = np.sqrt(dx**<NUM_LIT:2> + dy**<NUM_LIT:2>)<EOL>if np.any(rad > np.finfo(float).eps):<EOL><INDENT>max_rad = np.max(rad)<EOL>dx /= max_rad<EOL>dy /= max_rad<EOL><DEDENT>[h, w] = dx.shape<EOL>rad = np.sqrt(dx**<NUM_LIT:2> + dy**<NUM_LIT:2>)<EOL>angle = np.arctan2(-dy, -dx) / np.pi<EOL>bin_real = (angle + <NUM_LIT:1>) / <NUM_LIT:2> * (num_bins - <NUM_LIT:1>)<EOL>bin_left = np.floor(bin_real).astype(int)<EOL>bin_right = (bin_left + <NUM_LIT:1>) % num_bins<EOL>w = (bin_real - bin_left.astype(np.float32))[..., None]<EOL>flow_img = (<EOL><NUM_LIT:1> - w) * color_wheel[bin_left, :] + w * color_wheel[bin_right, :]<EOL>small_ind = rad <= <NUM_LIT:1><EOL>flow_img[small_ind] = <NUM_LIT:1> - rad[small_ind, None] * (<NUM_LIT:1> - flow_img[small_ind])<EOL>flow_img[np.logical_not(small_ind)] *= <NUM_LIT><EOL>flow_img[ignore_inds, :] = <NUM_LIT:0><EOL>return flow_img<EOL>

Convert flow map to RGB image.

    Args:
        flow (ndarray): Array of optical flow.
        color_wheel (ndarray or None): Color wheel used to map flow field to
            RGB colorspace. Default color wheel will be used if not specified.
        unknown_thr (str): Values above this threshold will be marked as
            unknown and thus ignored.

    Returns:
        ndarray: RGB image that can be visualized.


if bins is None:<EOL><INDENT>bins = [<NUM_LIT:15>, <NUM_LIT:6>, <NUM_LIT:4>, <NUM_LIT:11>, <NUM_LIT>, <NUM_LIT:6>]<EOL><DEDENT>assert len(bins) == <NUM_LIT:6><EOL>RY, YG, GC, CB, BM, MR = tuple(bins)<EOL>ry = [<NUM_LIT:1>, np.arange(RY) / RY, <NUM_LIT:0>]<EOL>yg = [<NUM_LIT:1> - np.arange(YG) / YG, <NUM_LIT:1>, <NUM_LIT:0>]<EOL>gc = [<NUM_LIT:0>, <NUM_LIT:1>, np.arange(GC) / GC]<EOL>cb = [<NUM_LIT:0>, <NUM_LIT:1> - np.arange(CB) / CB, <NUM_LIT:1>]<EOL>bm = [np.arange(BM) / BM, <NUM_LIT:0>, <NUM_LIT:1>]<EOL>mr = [<NUM_LIT:1>, <NUM_LIT:0>, <NUM_LIT:1> - np.arange(MR) / MR]<EOL>num_bins = RY + YG + GC + CB + BM + MR<EOL>color_wheel = np.zeros((<NUM_LIT:3>, num_bins), dtype=np.float32)<EOL>col = <NUM_LIT:0><EOL>for i, color in enumerate([ry, yg, gc, cb, bm, mr]):<EOL><INDENT>for j in range(<NUM_LIT:3>):<EOL><INDENT>color_wheel[j, col:col + bins[i]] = color[j]<EOL><DEDENT>col += bins[i]<EOL><DEDENT>return color_wheel.T<EOL>

Build a color wheel.

    Args:
        bins(list or tuple, optional): Specify the number of bins for each
            color range, corresponding to six ranges: red -> yellow,
            yellow -> green, green -> cyan, cyan -> blue, blue -> magenta,
            magenta -> red. [15, 6, 4, 11, 13, 6] is used for default
            (see Middlebury).

    Returns:
        ndarray: Color wheel of shape (total_bins, 3).


if is_str(color):<EOL><INDENT>return Color[color].value<EOL><DEDENT>elif isinstance(color, Color):<EOL><INDENT>return color.value<EOL><DEDENT>elif isinstance(color, tuple):<EOL><INDENT>assert len(color) == <NUM_LIT:3><EOL>for channel in color:<EOL><INDENT>assert channel >= <NUM_LIT:0> and channel <= <NUM_LIT:255><EOL><DEDENT>return color<EOL><DEDENT>elif isinstance(color, int):<EOL><INDENT>assert color >= <NUM_LIT:0> and color <= <NUM_LIT:255><EOL>return color, color, color<EOL><DEDENT>elif isinstance(color, np.ndarray):<EOL><INDENT>assert color.ndim == <NUM_LIT:1> and color.size == <NUM_LIT:3><EOL>assert np.all((color >= <NUM_LIT:0>) & (color <= <NUM_LIT:255>))<EOL>color = color.astype(np.uint8)<EOL>return tuple(color)<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>'.format(type(color)))<EOL><DEDENT>

Convert various input to color tuples.

    Args:
        color (:obj:`Color`/str/tuple/int/ndarray): Color inputs

    Returns:
        tuple[int]: A tuple of 3 integers indicating BGR channels.


def list_from_file(filename, prefix='<STR_LIT>', offset=<NUM_LIT:0>, max_num=<NUM_LIT:0>):

cnt = <NUM_LIT:0><EOL>item_list = []<EOL>with open(filename, '<STR_LIT:r>') as f:<EOL><INDENT>for _ in range(offset):<EOL><INDENT>f.readline()<EOL><DEDENT>for line in f:<EOL><INDENT>if max_num > <NUM_LIT:0> and cnt >= max_num:<EOL><INDENT>break<EOL><DEDENT>item_list.append(prefix + line.rstrip('<STR_LIT:\n>'))<EOL>cnt += <NUM_LIT:1><EOL><DEDENT><DEDENT>return item_list<EOL>

Load a text file and parse the content as a list of strings.

    Args:
        filename (str): Filename.
        prefix (str): The prefix to be inserted to the begining of each item.
        offset (int): The offset of lines.
        max_num (int): The maximum number of lines to be read,
            zeros and negatives mean no limitation.

    Returns:
        list[str]: A list of strings.


mapping = {}<EOL>with open(filename, '<STR_LIT:r>') as f:<EOL><INDENT>for line in f:<EOL><INDENT>items = line.rstrip('<STR_LIT:\n>').split()<EOL>assert len(items) >= <NUM_LIT:2><EOL>key = key_type(items[<NUM_LIT:0>])<EOL>val = items[<NUM_LIT:1>:] if len(items) > <NUM_LIT:2> else items[<NUM_LIT:1>]<EOL>mapping[key] = val<EOL><DEDENT><DEDENT>return mapping<EOL>

Load a text file and parse the content as a dict.

    Each line of the text file will be two or more columns splited by
    whitespaces or tabs. The first column will be parsed as dict keys, and
    the following columns will be parsed as dict values.

    Args:
        filename(str): Filename.
        key_type(type): Type of the dict's keys. str is user by default and
            type conversion will be performed if specified.

    Returns:
        dict: The parsed contents.


def load(file, file_format=None, **kwargs):

if file_format is None and is_str(file):<EOL><INDENT>file_format = file.split('<STR_LIT:.>')[-<NUM_LIT:1>]<EOL><DEDENT>if file_format not in file_handlers:<EOL><INDENT>raise TypeError('<STR_LIT>'.format(file_format))<EOL><DEDENT>handler = file_handlers[file_format]<EOL>if is_str(file):<EOL><INDENT>obj = handler.load_from_path(file, **kwargs)<EOL><DEDENT>elif hasattr(file, '<STR_LIT>'):<EOL><INDENT>obj = handler.load_from_fileobj(file, **kwargs)<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>return obj<EOL>

Load data from json/yaml/pickle files.

    This method provides a unified api for loading data from serialized files.

    Args:
        file (str or file-like object): Filename or a file-like object.
        file_format (str, optional): If not specified, the file format will be
            inferred from the file extension, otherwise use the specified one.
            Currently supported formats include "json", "yaml/yml" and
            "pickle/pkl".

    Returns:
        The content from the file.


def dump(obj, file=None, file_format=None, **kwargs):

if file_format is None:<EOL><INDENT>if is_str(file):<EOL><INDENT>file_format = file.split('<STR_LIT:.>')[-<NUM_LIT:1>]<EOL><DEDENT>elif file is None:<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>')<EOL><DEDENT><DEDENT>if file_format not in file_handlers:<EOL><INDENT>raise TypeError('<STR_LIT>'.format(file_format))<EOL><DEDENT>handler = file_handlers[file_format]<EOL>if file is None:<EOL><INDENT>return handler.dump_to_str(obj, **kwargs)<EOL><DEDENT>elif is_str(file):<EOL><INDENT>handler.dump_to_path(obj, file, **kwargs)<EOL><DEDENT>elif hasattr(file, '<STR_LIT>'):<EOL><INDENT>handler.dump_to_fileobj(obj, file, **kwargs)<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>

Dump data to json/yaml/pickle strings or files.

    This method provides a unified api for dumping data as strings or to files,
    and also supports custom arguments for each file format.

    Args:
        obj (any): The python object to be dumped.
        file (str or file-like object, optional): If not specified, then the
            object is dump to a str, otherwise to a file specified by the
            filename or file-like object.
        file_format (str, optional): Same as :func:`load`.

    Returns:
        bool: True for success, False otherwise.


if not isinstance(handler, BaseFileHandler):<EOL><INDENT>raise TypeError(<EOL>'<STR_LIT>'.format(<EOL>type(handler)))<EOL><DEDENT>if isinstance(file_formats, str):<EOL><INDENT>file_formats = [file_formats]<EOL><DEDENT>if not is_list_of(file_formats, str):<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>for ext in file_formats:<EOL><INDENT>file_handlers[ext] = handler<EOL><DEDENT>

Register a handler for some file extensions.

    Args:
        handler (:obj:`BaseFileHandler`): Handler to be registered.
        file_formats (str or list[str]): File formats to be handled by this
            handler.


w, h = size<EOL>return int(w * float(scale) + <NUM_LIT:0.5>), int(h * float(scale) + <NUM_LIT:0.5>)<EOL>

Rescale a size by a ratio.

    Args:
        size (tuple): w, h.
        scale (float): Scaling factor.

    Returns:
        tuple[int]: scaled size.


def imresize(img, size, return_scale=False, interpolation='<STR_LIT>'):

h, w = img.shape[:<NUM_LIT:2>]<EOL>resized_img = cv2.resize(<EOL>img, size, interpolation=interp_codes[interpolation])<EOL>if not return_scale:<EOL><INDENT>return resized_img<EOL><DEDENT>else:<EOL><INDENT>w_scale = size[<NUM_LIT:0>] / w<EOL>h_scale = size[<NUM_LIT:1>] / h<EOL>return resized_img, w_scale, h_scale<EOL><DEDENT>

Resize image to a given size.

    Args:
        img (ndarray): The input image.
        size (tuple): Target (w, h).
        return_scale (bool): Whether to return `w_scale` and `h_scale`.
        interpolation (str): Interpolation method, accepted values are
            "nearest", "bilinear", "bicubic", "area", "lanczos".

    Returns:
        tuple or ndarray: (`resized_img`, `w_scale`, `h_scale`) or
            `resized_img`.


def imresize_like(img, dst_img, return_scale=False, interpolation='<STR_LIT>'):

h, w = dst_img.shape[:<NUM_LIT:2>]<EOL>return imresize(img, (w, h), return_scale, interpolation)<EOL>

Resize image to the same size of a given image.

    Args:
        img (ndarray): The input image.
        dst_img (ndarray): The target image.
        return_scale (bool): Whether to return `w_scale` and `h_scale`.
        interpolation (str): Same as :func:`resize`.

    Returns:
        tuple or ndarray: (`resized_img`, `w_scale`, `h_scale`) or
            `resized_img`.


def imrescale(img, scale, return_scale=False, interpolation='<STR_LIT>'):

h, w = img.shape[:<NUM_LIT:2>]<EOL>if isinstance(scale, (float, int)):<EOL><INDENT>if scale <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>'.format(scale))<EOL><DEDENT>scale_factor = scale<EOL><DEDENT>elif isinstance(scale, tuple):<EOL><INDENT>max_long_edge = max(scale)<EOL>max_short_edge = min(scale)<EOL>scale_factor = min(max_long_edge / max(h, w),<EOL>max_short_edge / min(h, w))<EOL><DEDENT>else:<EOL><INDENT>raise TypeError(<EOL>'<STR_LIT>'.format(<EOL>type(scale)))<EOL><DEDENT>new_size = _scale_size((w, h), scale_factor)<EOL>rescaled_img = imresize(img, new_size, interpolation=interpolation)<EOL>if return_scale:<EOL><INDENT>return rescaled_img, scale_factor<EOL><DEDENT>else:<EOL><INDENT>return rescaled_img<EOL><DEDENT>

Resize image while keeping the aspect ratio.

    Args:
        img (ndarray): The input image.
        scale (float or tuple[int]): The scaling factor or maximum size.
            If it is a float number, then the image will be rescaled by this
            factor, else if it is a tuple of 2 integers, then the image will
            be rescaled as large as possible within the scale.
        return_scale (bool): Whether to return the scaling factor besides the
            rescaled image.
        interpolation (str): Same as :func:`resize`.

    Returns:
        ndarray: The rescaled image.


assert direction in ['<STR_LIT>', '<STR_LIT>']<EOL>if direction == '<STR_LIT>':<EOL><INDENT>return np.flip(img, axis=<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>return np.flip(img, axis=<NUM_LIT:0>)<EOL><DEDENT>

Flip an image horizontally or vertically.

    Args:
        img (ndarray): Image to be flipped.
        direction (str): The flip direction, either "horizontal" or "vertical".

    Returns:
        ndarray: The flipped image.


if center is not None and auto_bound:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>h, w = img.shape[:<NUM_LIT:2>]<EOL>if center is None:<EOL><INDENT>center = ((w - <NUM_LIT:1>) * <NUM_LIT:0.5>, (h - <NUM_LIT:1>) * <NUM_LIT:0.5>)<EOL><DEDENT>assert isinstance(center, tuple)<EOL>matrix = cv2.getRotationMatrix2D(center, -angle, scale)<EOL>if auto_bound:<EOL><INDENT>cos = np.abs(matrix[<NUM_LIT:0>, <NUM_LIT:0>])<EOL>sin = np.abs(matrix[<NUM_LIT:0>, <NUM_LIT:1>])<EOL>new_w = h * sin + w * cos<EOL>new_h = h * cos + w * sin<EOL>matrix[<NUM_LIT:0>, <NUM_LIT:2>] += (new_w - w) * <NUM_LIT:0.5><EOL>matrix[<NUM_LIT:1>, <NUM_LIT:2>] += (new_h - h) * <NUM_LIT:0.5><EOL>w = int(np.round(new_w))<EOL>h = int(np.round(new_h))<EOL><DEDENT>rotated = cv2.warpAffine(img, matrix, (w, h), borderValue=border_value)<EOL>return rotated<EOL>

Rotate an image.

    Args:
        img (ndarray): Image to be rotated.
        angle (float): Rotation angle in degrees, positive values mean
            clockwise rotation.
        center (tuple): Center of the rotation in the source image, by default
            it is the center of the image.
        scale (float): Isotropic scale factor.
        border_value (int): Border value.
        auto_bound (bool): Whether to adjust the image size to cover the whole
            rotated image.

    Returns:
        ndarray: The rotated image.


assert bboxes.shape[-<NUM_LIT:1>] % <NUM_LIT:4> == <NUM_LIT:0><EOL>clipped_bboxes = np.empty_like(bboxes, dtype=bboxes.dtype)<EOL>clipped_bboxes[..., <NUM_LIT:0>::<NUM_LIT:2>] = np.maximum(<EOL>np.minimum(bboxes[..., <NUM_LIT:0>::<NUM_LIT:2>], img_shape[<NUM_LIT:1>] - <NUM_LIT:1>), <NUM_LIT:0>)<EOL>clipped_bboxes[..., <NUM_LIT:1>::<NUM_LIT:2>] = np.maximum(<EOL>np.minimum(bboxes[..., <NUM_LIT:1>::<NUM_LIT:2>], img_shape[<NUM_LIT:0>] - <NUM_LIT:1>), <NUM_LIT:0>)<EOL>return clipped_bboxes<EOL>

Clip bboxes to fit the image shape.

    Args:
        bboxes (ndarray): Shape (..., 4*k)
        img_shape (tuple): (height, width) of the image.

    Returns:
        ndarray: Clipped bboxes.


def bbox_scaling(bboxes, scale, clip_shape=None):

if float(scale) == <NUM_LIT:1.0>:<EOL><INDENT>scaled_bboxes = bboxes.copy()<EOL><DEDENT>else:<EOL><INDENT>w = bboxes[..., <NUM_LIT:2>] - bboxes[..., <NUM_LIT:0>] + <NUM_LIT:1><EOL>h = bboxes[..., <NUM_LIT:3>] - bboxes[..., <NUM_LIT:1>] + <NUM_LIT:1><EOL>dw = (w * (scale - <NUM_LIT:1>)) * <NUM_LIT:0.5><EOL>dh = (h * (scale - <NUM_LIT:1>)) * <NUM_LIT:0.5><EOL>scaled_bboxes = bboxes + np.stack((-dw, -dh, dw, dh), axis=-<NUM_LIT:1>)<EOL><DEDENT>if clip_shape is not None:<EOL><INDENT>return bbox_clip(scaled_bboxes, clip_shape)<EOL><DEDENT>else:<EOL><INDENT>return scaled_bboxes<EOL><DEDENT>

Scaling bboxes w.r.t the box center.

    Args:
        bboxes (ndarray): Shape(..., 4).
        scale (float): Scaling factor.
        clip_shape (tuple, optional): If specified, bboxes that exceed the
            boundary will be clipped according to the given shape (h, w).

    Returns:
        ndarray: Scaled bboxes.


def imcrop(img, bboxes, scale=<NUM_LIT:1.0>, pad_fill=None):

chn = <NUM_LIT:1> if img.ndim == <NUM_LIT:2> else img.shape[<NUM_LIT:2>]<EOL>if pad_fill is not None:<EOL><INDENT>if isinstance(pad_fill, (int, float)):<EOL><INDENT>pad_fill = [pad_fill for _ in range(chn)]<EOL><DEDENT>assert len(pad_fill) == chn<EOL><DEDENT>_bboxes = bboxes[None, ...] if bboxes.ndim == <NUM_LIT:1> else bboxes<EOL>scaled_bboxes = bbox_scaling(_bboxes, scale).astype(np.int32)<EOL>clipped_bbox = bbox_clip(scaled_bboxes, img.shape)<EOL>patches = []<EOL>for i in range(clipped_bbox.shape[<NUM_LIT:0>]):<EOL><INDENT>x1, y1, x2, y2 = tuple(clipped_bbox[i, :])<EOL>if pad_fill is None:<EOL><INDENT>patch = img[y1:y2 + <NUM_LIT:1>, x1:x2 + <NUM_LIT:1>, ...]<EOL><DEDENT>else:<EOL><INDENT>_x1, _y1, _x2, _y2 = tuple(scaled_bboxes[i, :])<EOL>if chn == <NUM_LIT:2>:<EOL><INDENT>patch_shape = (_y2 - _y1 + <NUM_LIT:1>, _x2 - _x1 + <NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>patch_shape = (_y2 - _y1 + <NUM_LIT:1>, _x2 - _x1 + <NUM_LIT:1>, chn)<EOL><DEDENT>patch = np.array(<EOL>pad_fill, dtype=img.dtype) * np.ones(<EOL>patch_shape, dtype=img.dtype)<EOL>x_start = <NUM_LIT:0> if _x1 >= <NUM_LIT:0> else -_x1<EOL>y_start = <NUM_LIT:0> if _y1 >= <NUM_LIT:0> else -_y1<EOL>w = x2 - x1 + <NUM_LIT:1><EOL>h = y2 - y1 + <NUM_LIT:1><EOL>patch[y_start:y_start + h, x_start:x_start +<EOL>w, ...] = img[y1:y1 + h, x1:x1 + w, ...]<EOL><DEDENT>patches.append(patch)<EOL><DEDENT>if bboxes.ndim == <NUM_LIT:1>:<EOL><INDENT>return patches[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>return patches<EOL><DEDENT>

Crop image patches.

    3 steps: scale the bboxes -> clip bboxes -> crop and pad.

    Args:
        img (ndarray): Image to be cropped.
        bboxes (ndarray): Shape (k, 4) or (4, ), location of cropped bboxes.
        scale (float, optional): Scale ratio of bboxes, the default value
            1.0 means no padding.
        pad_fill (number or list): Value to be filled for padding, None for
            no padding.

    Returns:
        list or ndarray: The cropped image patches.


def impad(img, shape, pad_val=<NUM_LIT:0>):

if not isinstance(pad_val, (int, float)):<EOL><INDENT>assert len(pad_val) == img.shape[-<NUM_LIT:1>]<EOL><DEDENT>if len(shape) < len(img.shape):<EOL><INDENT>shape = shape + (img.shape[-<NUM_LIT:1>], )<EOL><DEDENT>assert len(shape) == len(img.shape)<EOL>for i in range(len(shape) - <NUM_LIT:1>):<EOL><INDENT>assert shape[i] >= img.shape[i]<EOL><DEDENT>pad = np.empty(shape, dtype=img.dtype)<EOL>pad[...] = pad_val<EOL>pad[:img.shape[<NUM_LIT:0>], :img.shape[<NUM_LIT:1>], ...] = img<EOL>return pad<EOL>

Pad an image to a certain shape.

    Args:
        img (ndarray): Image to be padded.
        shape (tuple): Expected padding shape.
        pad_val (number or sequence): Values to be filled in padding areas.

    Returns:
        ndarray: The padded image.


def impad_to_multiple(img, divisor, pad_val=<NUM_LIT:0>):

pad_h = int(np.ceil(img.shape[<NUM_LIT:0>] / divisor)) * divisor<EOL>pad_w = int(np.ceil(img.shape[<NUM_LIT:1>] / divisor)) * divisor<EOL>return impad(img, (pad_h, pad_w), pad_val)<EOL>

Pad an image to ensure each edge to be multiple to some number.

    Args:
        img (ndarray): Image to be padded.
        divisor (int): Padded image edges will be multiple to divisor.
        pad_val (number or sequence): Same as :func:`impad`.

    Returns:
        ndarray: The padded image.


return np.full_like(img, <NUM_LIT:255>) - img<EOL>

Invert (negate) an image
    Args:
        img (ndarray): Image to be inverted.

    Returns:
        ndarray: The inverted image.


out_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)<EOL>if keepdim:<EOL><INDENT>out_img = out_img[..., None]<EOL><DEDENT>return out_img<EOL>

Convert a BGR image to grayscale image.

    Args:
        img (ndarray): The input image.
        keepdim (bool): If False (by default), then return the grayscale image
            with 2 dims, otherwise 3 dims.

    Returns:
        ndarray: The converted grayscale image.


img = img[..., None] if img.ndim == <NUM_LIT:2> else img<EOL>out_img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)<EOL>return out_img<EOL>

Convert a grayscale image to BGR image.

    Args:
        img (ndarray or str): The input image.

    Returns:
        ndarray: The converted BGR image.


if isinstance(img_or_path, np.ndarray):<EOL><INDENT>return img_or_path<EOL><DEDENT>elif is_str(img_or_path):<EOL><INDENT>flag = imread_flags[flag] if is_str(flag) else flag<EOL>check_file_exist(img_or_path,<EOL>'<STR_LIT>'.format(img_or_path))<EOL>return cv2.imread(img_or_path, flag)<EOL><DEDENT>else:<EOL><INDENT>raise TypeError('<STR_LIT>')<EOL><DEDENT>

Read an image.

    Args:
        img_or_path (ndarray or str): Either a numpy array or image path.
            If it is a numpy array (loaded image), then it will be returned
            as is.
        flag (str): Flags specifying the color type of a loaded image,
            candidates are `color`, `grayscale` and `unchanged`.

    Returns:
        ndarray: Loaded image array.


img_np = np.frombuffer(content, np.uint8)<EOL>flag = imread_flags[flag] if is_str(flag) else flag<EOL>img = cv2.imdecode(img_np, flag)<EOL>return img<EOL>

Read an image from bytes.

    Args:
        content (bytes): Image bytes got from files or other streams.
        flag (str): Same as :func:`imread`.

    Returns:
        ndarray: Loaded image array.


def imwrite(img, file_path, params=None, auto_mkdir=True):

if auto_mkdir:<EOL><INDENT>dir_name = osp.abspath(osp.dirname(file_path))<EOL>mkdir_or_exist(dir_name)<EOL><DEDENT>return cv2.imwrite(file_path, img, params)<EOL>

Write image to file

    Args:
        img (ndarray): Image array to be written.
        file_path (str): Image file path.
        params (None or list): Same as opencv's :func:`imwrite` interface.
        auto_mkdir (bool): If the parent folder of `file_path` does not exist,
            whether to create it automatically.

    Returns:
        bool: Successful or not.


def quantize(arr, min_val, max_val, levels, dtype=np.int64):

if not (isinstance(levels, int) and levels > <NUM_LIT:1>):<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>'.format(levels))<EOL><DEDENT>if min_val >= max_val:<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>'.format(<EOL>min_val, max_val))<EOL><DEDENT>arr = np.clip(arr, min_val, max_val) - min_val<EOL>quantized_arr = np.minimum(<EOL>np.floor(levels * arr / (max_val - min_val)).astype(dtype), levels - <NUM_LIT:1>)<EOL>return quantized_arr<EOL>

Quantize an array of (-inf, inf) to [0, levels-1].

    Args:
        arr (ndarray): Input array.
        min_val (scalar): Minimum value to be clipped.
        max_val (scalar): Maximum value to be clipped.
        levels (int): Quantization levels.
        dtype (np.type): The type of the quantized array.

    Returns:
        tuple: Quantized array.


def dequantize(arr, min_val, max_val, levels, dtype=np.float64):

if not (isinstance(levels, int) and levels > <NUM_LIT:1>):<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>'.format(levels))<EOL><DEDENT>if min_val >= max_val:<EOL><INDENT>raise ValueError(<EOL>'<STR_LIT>'.format(<EOL>min_val, max_val))<EOL><DEDENT>dequantized_arr = (arr + <NUM_LIT:0.5>).astype(dtype) * (<EOL>max_val - min_val) / levels + min_val<EOL>return dequantized_arr<EOL>

Dequantize an array.

    Args:
        arr (ndarray): Input array.
        min_val (scalar): Minimum value to be clipped.
        max_val (scalar): Maximum value to be clipped.
        levels (int): Quantization levels.
        dtype (np.type): The type of the dequantized array.

    Returns:
        tuple: Dequantized array.


def scatter(inputs, target_gpus, dim=<NUM_LIT:0>):

def scatter_map(obj):<EOL><INDENT>if isinstance(obj, torch.Tensor):<EOL><INDENT>return OrigScatter.apply(target_gpus, None, dim, obj)<EOL><DEDENT>if isinstance(obj, DataContainer):<EOL><INDENT>if obj.cpu_only:<EOL><INDENT>return obj.data<EOL><DEDENT>else:<EOL><INDENT>return Scatter.forward(target_gpus, obj.data)<EOL><DEDENT><DEDENT>if isinstance(obj, tuple) and len(obj) > <NUM_LIT:0>:<EOL><INDENT>return list(zip(*map(scatter_map, obj)))<EOL><DEDENT>if isinstance(obj, list) and len(obj) > <NUM_LIT:0>:<EOL><INDENT>out = list(map(list, zip(*map(scatter_map, obj))))<EOL>return out<EOL><DEDENT>if isinstance(obj, dict) and len(obj) > <NUM_LIT:0>:<EOL><INDENT>out = list(map(type(obj), zip(*map(scatter_map, obj.items()))))<EOL>return out<EOL><DEDENT>return [obj for targets in target_gpus]<EOL><DEDENT>try:<EOL><INDENT>return scatter_map(inputs)<EOL><DEDENT>finally:<EOL><INDENT>scatter_map = None<EOL><DEDENT>

Scatter inputs to target gpus.

    The only difference from original :func:`scatter` is to add support for
    :type:`~mmcv.parallel.DataContainer`.


def scatter_kwargs(inputs, kwargs, target_gpus, dim=<NUM_LIT:0>):

inputs = scatter(inputs, target_gpus, dim) if inputs else []<EOL>kwargs = scatter(kwargs, target_gpus, dim) if kwargs else []<EOL>if len(inputs) < len(kwargs):<EOL><INDENT>inputs.extend([() for _ in range(len(kwargs) - len(inputs))])<EOL><DEDENT>elif len(kwargs) < len(inputs):<EOL><INDENT>kwargs.extend([{} for _ in range(len(inputs) - len(kwargs))])<EOL><DEDENT>inputs = tuple(inputs)<EOL>kwargs = tuple(kwargs)<EOL>return inputs, kwargs<EOL>

Scatter with support for kwargs dictionary


def scatter(input, devices, streams=None):

if streams is None:<EOL><INDENT>streams = [None] * len(devices)<EOL><DEDENT>if isinstance(input, list):<EOL><INDENT>chunk_size = (len(input) - <NUM_LIT:1>) // len(devices) + <NUM_LIT:1><EOL>outputs = [<EOL>scatter(input[i], [devices[i // chunk_size]],<EOL>[streams[i // chunk_size]]) for i in range(len(input))<EOL>]<EOL>return outputs<EOL><DEDENT>elif isinstance(input, torch.Tensor):<EOL><INDENT>output = input.contiguous()<EOL>stream = streams[<NUM_LIT:0>] if output.numel() > <NUM_LIT:0> else None<EOL>with torch.cuda.device(devices[<NUM_LIT:0>]), torch.cuda.stream(stream):<EOL><INDENT>output = output.cuda(devices[<NUM_LIT:0>], non_blocking=True)<EOL><DEDENT>return output<EOL><DEDENT>else:<EOL><INDENT>raise Exception('<STR_LIT>'.format(type(input)))<EOL><DEDENT>


if not isinstance(batch, collections.Sequence):<EOL><INDENT>raise TypeError("<STR_LIT>".format(batch.dtype))<EOL><DEDENT>if isinstance(batch[<NUM_LIT:0>], DataContainer):<EOL><INDENT>assert len(batch) % samples_per_gpu == <NUM_LIT:0><EOL>stacked = []<EOL>if batch[<NUM_LIT:0>].cpu_only:<EOL><INDENT>for i in range(<NUM_LIT:0>, len(batch), samples_per_gpu):<EOL><INDENT>stacked.append(<EOL>[sample.data for sample in batch[i:i + samples_per_gpu]])<EOL><DEDENT>return DataContainer(<EOL>stacked, batch[<NUM_LIT:0>].stack, batch[<NUM_LIT:0>].padding_value, cpu_only=True)<EOL><DEDENT>elif batch[<NUM_LIT:0>].stack:<EOL><INDENT>for i in range(<NUM_LIT:0>, len(batch), samples_per_gpu):<EOL><INDENT>assert isinstance(batch[i].data, torch.Tensor)<EOL>assert batch[i].dim() == <NUM_LIT:3><EOL>c, h, w = batch[i].size()<EOL>for sample in batch[i:i + samples_per_gpu]:<EOL><INDENT>assert c == sample.size(<NUM_LIT:0>)<EOL>h = max(h, sample.size(<NUM_LIT:1>))<EOL>w = max(w, sample.size(<NUM_LIT:2>))<EOL><DEDENT>padded_samples = [<EOL>F.pad(<EOL>sample.data,<EOL>(<NUM_LIT:0>, w - sample.size(<NUM_LIT:2>), <NUM_LIT:0>, h - sample.size(<NUM_LIT:1>)),<EOL>value=sample.padding_value)<EOL>for sample in batch[i:i + samples_per_gpu]<EOL>]<EOL>stacked.append(default_collate(padded_samples))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for i in range(<NUM_LIT:0>, len(batch), samples_per_gpu):<EOL><INDENT>stacked.append(<EOL>[sample.data for sample in batch[i:i + samples_per_gpu]])<EOL><DEDENT><DEDENT>return DataContainer(stacked, batch[<NUM_LIT:0>].stack, batch[<NUM_LIT:0>].padding_value)<EOL><DEDENT>elif isinstance(batch[<NUM_LIT:0>], collections.Sequence):<EOL><INDENT>transposed = zip(*batch)<EOL>return [collate(samples, samples_per_gpu) for samples in transposed]<EOL><DEDENT>elif isinstance(batch[<NUM_LIT:0>], collections.Mapping):<EOL><INDENT>return {<EOL>key: collate([d[key] for d in batch], samples_per_gpu)<EOL>for key in batch[<NUM_LIT:0>]<EOL>}<EOL><DEDENT>else:<EOL><INDENT>return default_collate(batch)<EOL><DEDENT>

Puts each data field into a tensor/DataContainer with outer dimension
    batch size.

    Extend default_collate to add support for
    :type:`~mmcv.parallel.DataContainer`. There are 3 cases.

    1. cpu_only = True, e.g., meta data
    2. cpu_only = False, stack = True, e.g., images tensors
    3. cpu_only = False, stack = False, e.g., gt bboxes


def accuracy(output, target, topk=(<NUM_LIT:1>, )):

with torch.no_grad():<EOL><INDENT>maxk = max(topk)<EOL>batch_size = target.size(<NUM_LIT:0>)<EOL>_, pred = output.topk(maxk, <NUM_LIT:1>, True, True)<EOL>pred = pred.t()<EOL>correct = pred.eq(target.view(<NUM_LIT:1>, -<NUM_LIT:1>).expand_as(pred))<EOL>res = []<EOL>for k in topk:<EOL><INDENT>correct_k = correct[:k].view(-<NUM_LIT:1>).float().sum(<NUM_LIT:0>, keepdim=True)<EOL>res.append(correct_k.mul_(<NUM_LIT> / batch_size))<EOL><DEDENT>return res<EOL><DEDENT>

Computes the precision@k for the specified values of k


f = open(file_name, '<STR_LIT:r>')<EOL>text = f.read()<EOL>if re.search(real_api_regex, text) is not None andre.search(zero_api_regex, text) is None:<EOL><INDENT>return True<EOL><DEDENT>return False<EOL>

Detect whether the file contains an api key in the Token object that is not 40*'0'.
See issue #86.
:param file: path-to-file to check
:return: boolean


with open(file_name, '<STR_LIT:r>') as fp:<EOL><INDENT>text = fp.read()<EOL><DEDENT>text = re.sub(real_api_regex, zero_token_string, text)<EOL>with open(file_name, '<STR_LIT:w>') as fp:<EOL><INDENT>fp.write(text)<EOL><DEDENT>return<EOL>

Change the api key in the Token object to 40*'0'.  See issue #86.
:param file: path-to-file to change


self._config = config<EOL>self._headers = {}<EOL>self._base_url = "<STR_LIT>"<EOL>if config.get('<STR_LIT>'):<EOL><INDENT>self._session = requests.Session()<EOL><DEDENT>else:<EOL><INDENT>self._session = requests<EOL><DEDENT>

Base class for interacting with RESTful APIs
           Child class MUST have a ._base_url property!

            Args:
                config (dict): Arbitrary options that child classes can access


def _request(self, method, url, **kwargs):

resp = self._session.request(method,<EOL>'<STR_LIT>'.format(self._base_url, url),<EOL>headers=self._headers,<EOL>**kwargs)<EOL>try:<EOL><INDENT>resp.raise_for_status()<EOL><DEDENT>except HTTPError as e:<EOL><INDENT>logging.error(resp.content)<EOL>raise RestClientError(e)<EOL><DEDENT>return resp<EOL>

Make HTTP request and return response object

            Args:
                method (str): GET, POST, PUT, DELETE
                url (str): path appended to the base_url to create request
                **kwargs: passed directly to a requests.request object


fields = item.keys()<EOL>values = item.values()<EOL>return json.loads(json.dumps(item),<EOL>object_hook=lambda d:<EOL>namedtuple(object_name, fields)(*values))<EOL>

Converts a python dict to a namedtuple, saving memory.


listing_file_url = "<STR_LIT>"<EOL>response = requests.get(listing_file_url)<EOL>zipdata = get_zipfile_from_response(response)<EOL>raw_csv = get_buffer_from_zipfile(zipdata, '<STR_LIT>')<EOL>reader = csv.DictReader(raw_csv)<EOL>return [row for row in reader<EOL>if row.get('<STR_LIT>') == assetType]<EOL>

Return a list of dicts of metadata tickers for all supported tickers
            of the specified asset type, as well as metadata about each ticker.
            This includes supported date range, the exchange the ticker is traded
            on, and the currency the stock is traded on.
           Tickers for unrelated products are omitted.
           https://apimedia.tiingo.com/docs/tiingo/daily/supported_tickers.zip


def get_ticker_metadata(self, ticker, fmt='<STR_LIT>'):

url = "<STR_LIT>".format(ticker)<EOL>response = self._request('<STR_LIT:GET>', url)<EOL>data = response.json()<EOL>if fmt == '<STR_LIT>':<EOL><INDENT>return data<EOL><DEDENT>elif fmt == '<STR_LIT:object>':<EOL><INDENT>return dict_to_object(data, "<STR_LIT>")<EOL><DEDENT>

Return metadata for 1 ticker
           Use TiingoClient.list_tickers() to get available options

            Args:
                ticker (str) : Unique identifier for stock


is_valid = self._is_eod_frequency(frequency) or re.match(self._frequency_pattern, frequency)<EOL>return not is_valid<EOL>

Check to see that frequency was specified correctly
:param frequency (string): frequency string
:return (boolean):


if self._invalid_frequency(frequency):<EOL><INDENT>etext = ("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>raise InvalidFrequencyError(etext.format(frequency))<EOL><DEDENT>else:<EOL><INDENT>if self._is_eod_frequency(frequency):<EOL><INDENT>return "<STR_LIT>".format(ticker)<EOL><DEDENT>else:<EOL><INDENT>return "<STR_LIT>".format(ticker)<EOL><DEDENT><DEDENT>

Return url based on frequency.  Daily, weekly, or yearly use Tiingo
EOD api; anything less than daily uses the iex intraday api.
:param ticker (string): ticker to be embedded in the url
:param frequency (string): valid frequency per Tiingo api
:return (string): url


def get_ticker_price(self, ticker,<EOL>startDate=None, endDate=None,<EOL>fmt='<STR_LIT>', frequency='<STR_LIT>'):

url = self._get_url(ticker, frequency)<EOL>params = {<EOL>'<STR_LIT>': fmt if fmt != "<STR_LIT:object>" else '<STR_LIT>',  <EOL>'<STR_LIT>': frequency<EOL>}<EOL>if startDate:<EOL><INDENT>params['<STR_LIT>'] = startDate<EOL><DEDENT>if endDate:<EOL><INDENT>params['<STR_LIT>'] = endDate<EOL><DEDENT>response = self._request('<STR_LIT:GET>', url, params=params)<EOL>if fmt == "<STR_LIT>":<EOL><INDENT>return response.json()<EOL><DEDENT>elif fmt == "<STR_LIT:object>":<EOL><INDENT>data = response.json()<EOL>return [dict_to_object(item, "<STR_LIT>") for item in data]<EOL><DEDENT>else:<EOL><INDENT>return response.content.decode("<STR_LIT:utf-8>")<EOL><DEDENT>

By default, return latest EOD Composite Price for a stock ticker.
           On average, each feed contains 3 data sources.

            Supported tickers + Available Day Ranges are here:
            https://apimedia.tiingo.com/docs/tiingo/daily/supported_tickers.zip

            Args:
                ticker (string): Unique identifier for stock ticker
                startDate (string): Start of ticker range in YYYY-MM-DD format
                endDate (string): End of ticker range in YYYY-MM-DD format
                fmt (string): 'csv' or 'json'
                frequency (string): Resample frequency


def get_dataframe(self, tickers,<EOL>startDate=None, endDate=None, metric_name=None, frequency='<STR_LIT>'):

valid_columns = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']<EOL>if metric_name is not None and metric_name not in valid_columns:<EOL><INDENT>raise APIColumnNameError('<STR_LIT>' + str(valid_columns))<EOL><DEDENT>params = {<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>'<STR_LIT>': frequency<EOL>}<EOL>if startDate:<EOL><INDENT>params['<STR_LIT>'] = startDate<EOL><DEDENT>if endDate:<EOL><INDENT>params['<STR_LIT>'] = endDate<EOL><DEDENT>if pandas_is_installed:<EOL><INDENT>if type(tickers) is str:<EOL><INDENT>stock = tickers<EOL>url = self._get_url(stock, frequency)<EOL>response = self._request('<STR_LIT:GET>', url, params=params)<EOL>df = pd.DataFrame(response.json())<EOL>if metric_name is not None:<EOL><INDENT>prices = df[metric_name]<EOL>prices.index = df['<STR_LIT:date>']<EOL><DEDENT>else:<EOL><INDENT>prices = df<EOL>prices.index = df['<STR_LIT:date>']<EOL>del (prices['<STR_LIT:date>'])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>prices = pd.DataFrame()<EOL>for stock in tickers:<EOL><INDENT>url = self._get_url(stock, frequency)<EOL>response = self._request('<STR_LIT:GET>', url, params=params)<EOL>df = pd.DataFrame(response.json())<EOL>df.index = df['<STR_LIT:date>']<EOL>df.rename(index=str, columns={metric_name: stock}, inplace=True)<EOL>prices = pd.concat([prices, df[stock]], axis=<NUM_LIT:1>)<EOL><DEDENT><DEDENT>prices.index = pd.to_datetime(prices.index)<EOL>return prices<EOL><DEDENT>else:<EOL><INDENT>error_message = ("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL>raise InstallPandasException(error_message)<EOL><DEDENT>

Return a pandas.DataFrame of historical prices for one or more ticker symbols.

            By default, return latest EOD Composite Price for a list of stock tickers.
            On average, each feed contains 3 data sources.

            Supported tickers + Available Day Ranges are here:
            https://apimedia.tiingo.com/docs/tiingo/daily/supported_tickers.zip
            or from the TiingoClient.list_tickers() method.

            Args:
                tickers (string/list): One or more unique identifiers for a stock ticker.
                startDate (string): Start of ticker range in YYYY-MM-DD format.
                endDate (string): End of ticker range in YYYY-MM-DD format.
                metric_name (string): Optional parameter specifying metric to be returned for each
                    ticker.  In the event of a single ticker, this is optional and if not specified
                    all of the available data will be returned.  In the event of a list of tickers,
                    this parameter is required.
                frequency (string): Resample frequency (defaults to daily).


def get_news(self, tickers=[], tags=[], sources=[], startDate=None,<EOL>endDate=None, limit=<NUM_LIT:100>, offset=<NUM_LIT:0>, sortBy="<STR_LIT>",<EOL>fmt='<STR_LIT>'):

url = "<STR_LIT>"<EOL>params = {<EOL>'<STR_LIT>': limit,<EOL>'<STR_LIT>': offset,<EOL>'<STR_LIT>': sortBy,<EOL>'<STR_LIT>': tickers,<EOL>'<STR_LIT>': sources,<EOL>'<STR_LIT>': tags,<EOL>'<STR_LIT>': startDate,<EOL>'<STR_LIT>': endDate<EOL>}<EOL>response = self._request('<STR_LIT:GET>', url, params=params)<EOL>data = response.json()<EOL>if fmt == '<STR_LIT>':<EOL><INDENT>return data<EOL><DEDENT>elif fmt == '<STR_LIT:object>':<EOL><INDENT>return [dict_to_object(item, "<STR_LIT>") for item in data]<EOL><DEDENT>

Return list of news articles matching given search terms
            https://api.tiingo.com/docs/tiingo/news

            # Dates are in YYYY-MM-DD Format.

            Args:
                tickers [string] : List of unique Stock Tickers to search
                tags [string] : List of topics tagged by Tiingo Algorithms
                sources [string]: List of base urls to include as news sources
                startDate, endDate [date]: Boundaries of news search window
                limit (int): Max results returned. Default 100, max 1000
                offset (int): Search results offset, used for paginating
                sortBy (string): "publishedDate" OR (#TODO: UPDATE THIS)


def get_bulk_news(self, file_id=None, fmt='<STR_LIT>'):

if file_id:<EOL><INDENT>url = "<STR_LIT>".format(file_id)<EOL><DEDENT>else:<EOL><INDENT>url = "<STR_LIT>"<EOL><DEDENT>response = self._request('<STR_LIT:GET>', url)<EOL>data = response.json()<EOL>if fmt == '<STR_LIT>':<EOL><INDENT>return data<EOL><DEDENT>elif fmt == '<STR_LIT:object>':<EOL><INDENT>return dict_to_object(data, "<STR_LIT>")<EOL><DEDENT>

Only available to institutional clients.
            If ID is NOT provided, return array of available file_ids.
            If ID is provided, provides URL which you can use to download your
            file, as well as some metadata about that file.


tokens = token.split_contents()<EOL>tokens.pop(<NUM_LIT:0>)<EOL>graph = tokens.pop(<NUM_LIT:0>)<EOL>attrs = dict([token.split("<STR_LIT:=>") for token in tokens])<EOL>if '<STR_LIT:id>' not in attrs.keys():<EOL><INDENT>attrs['<STR_LIT:id>'] = '<STR_LIT>'.join([chr(choice(range(<NUM_LIT>, <NUM_LIT>))) for i in range(<NUM_LIT:0>, <NUM_LIT:5>)])<EOL><DEDENT>else:<EOL><INDENT>attrs['<STR_LIT:id>'] = attrs['<STR_LIT:id>'][<NUM_LIT:1>:len(attrs['<STR_LIT:id>'])-<NUM_LIT:1>]<EOL><DEDENT>attr_string = '<STR_LIT>'.join(["<STR_LIT>" % (k, v) for k, v in attrs.iteritems()])<EOL>return GraphRenderer(graph, attr_string, attrs['<STR_LIT:id>'])<EOL>


if not key.startswith('<STR_LIT>'):<EOL><INDENT>if key not in self.allowed_options.keys():<EOL><INDENT>raise TypeError("<STR_LIT>"<EOL>"<STR_LIT>" % (key, self.allowed_options.keys()))<EOL><DEDENT>if not isinstance(value, self.allowed_options[key]):<EOL><INDENT>raise TypeError("<STR_LIT>" % key)<EOL><DEDENT><DEDENT>super(BaseOptions, self).__setitem__(key, value)<EOL>

__setitem__ should only allow keys within allowed option keys
and they have to batch the type


if getattr(self, '<STR_LIT:data>', False) and not getattr(self, '<STR_LIT>', False) and not getattr(self, '<STR_LIT>', False):<EOL><INDENT>_x = XVariable()<EOL>_y = YVariable()<EOL>_x.contribute_to_class(self, '<STR_LIT:X>', self.data)<EOL>_y.contribute_to_class(self, '<STR_LIT:Y>', self.data)<EOL>self['<STR_LIT:data>'] = zip(self._x.points, self._y.points)<EOL><DEDENT>else: <EOL><INDENT>for axis in ('<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>axis_obj = getattr(self, axis, False)<EOL>if not axis_obj:<EOL><INDENT>raise exception.MissingAxisException("<STR_LIT>" % axis)<EOL><DEDENT>if not getattr(axis_obj, '<STR_LIT>', False):<EOL><INDENT>raise exception.MissingDataException()<EOL><DEDENT><DEDENT>self['<STR_LIT:data>'] = zip(self._x.points, self._y.points)<EOL><DEDENT>

This method will be called to set Series data


def __init__(self, data=None, xpoints=None, ypoints=None, options=None, *args, **kwargs):

<EOL>self._options = SeriesOptions()<EOL>super(Series, self).__init__()<EOL>if '<STR_LIT:data>' not in dir(self) and data is not None:<EOL><INDENT>self.data = data<EOL><DEDENT>if data is None and xpoints is not None and ypoints is not None:<EOL><INDENT>self.data = zip(xpoints, ypoints)<EOL><DEDENT>for attr_name in dir(self):<EOL><INDENT>attr = getattr(self, attr_name)<EOL>if isinstance(attr, Variable):<EOL><INDENT>attr.contribute_to_class(self, attr_name, self.data)<EOL><DEDENT><DEDENT>for name, attr in kwargs.iteritems():<EOL><INDENT>if isinstance(attr, Variable):<EOL><INDENT>attr.contribute_to_class(self, name)<EOL><DEDENT><DEDENT>self._set_data()<EOL>for option in dir(self.Meta):<EOL><INDENT>self._options[option] = getattr(self.Meta, option)<EOL><DEDENT>self.update(self._options)<EOL>if options is not None and isinstance(options, SeriesOptions):<EOL><INDENT>self.update(options)<EOL><DEDENT>

Series should be able to get contructed and completed through
class definition or through kwargs


return json.dumps(self)<EOL>


def __init__(self, series=None, options=None, **kwargs):

self._series = []<EOL>for attr_name in dir(self):<EOL><INDENT>attr = getattr(self, attr_name)<EOL>if isinstance(attr, Series):<EOL><INDENT>self._series.append(attr)<EOL><DEDENT><DEDENT>for arg in kwargs.values():<EOL><INDENT>if isinstance(arg, Series):<EOL><INDENT>self._series.append(arg)<EOL><DEDENT><DEDENT>if series and all([isinstance(s, Series) for s in series]):<EOL><INDENT>self._series.extend(series)<EOL><DEDENT>for option in dir(self.Meta):<EOL><INDENT>self._options[option] = getattr(self.Meta, option)<EOL><DEDENT>if options and isinstance(options, GraphOptions):<EOL><INDENT>self._options.update(options)<EOL><DEDENT>self._set_options()<EOL>


if all([isinstance(getattr(s, axis), TimeVariable) for s in self._series]):<EOL><INDENT>return '<STR_LIT:time>'<EOL><DEDENT>return None<EOL>

will get the axis mode for the current series


<EOL>if '<STR_LIT>' in self._options.keys():<EOL><INDENT>self._options['<STR_LIT>'].update(<EOL>{'<STR_LIT>' : self._get_axis_mode(XAxis._var_name)})<EOL><DEDENT>if '<STR_LIT>' in self._options.keys():<EOL><INDENT>self._options['<STR_LIT>'].update(<EOL>{'<STR_LIT>' : self._get_axis_mode(YAxis._var_name)})<EOL><DEDENT>


return json.dumps(self._series)<EOL>


return json.dumps(self._options)<EOL>


for _, name, ispkg in iter_modules(path):<EOL><INDENT>module = import_module(f'<STR_LIT>', package)<EOL>if ispkg:<EOL><INDENT>build(cli.group(name)(module.group),<EOL>module.__path__,<EOL>module.__package__)<EOL><DEDENT>else:<EOL><INDENT>cli.command(name)(module.command)<EOL><DEDENT><DEDENT>

Build CLI dynamically based on the package structure.


kwargs.setdefault('<STR_LIT>', '<STR_LIT>' in sys.builtin_module_names)<EOL>kwargs.setdefault('<STR_LIT>', <NUM_LIT:1>)<EOL>proc = subprocess.Popen(*args, **kwargs)<EOL>if proc.stdout:<EOL><INDENT>q = queue.Queue()<EOL>t = threading.Thread(<EOL>target=enqueue_lines,<EOL>args=(proc.stdout, q))<EOL>proc.stdout = q<EOL>t.daemon = True<EOL>t.start()<EOL><DEDENT>if proc.stderr:<EOL><INDENT>q = queue.Queue()<EOL>t = threading.Thread(<EOL>target=enqueue_lines,<EOL>args=(proc.stderr, q))<EOL>proc.stderr = q<EOL>t.daemon = True<EOL>t.start()<EOL><DEDENT>return proc<EOL>

Open a subprocess without blocking. Return a process handle with any
output streams replaced by queues of lines from that stream.

Usage::

        proc = Popen_nonblocking(..., stdout=subprocess.PIPE)
        try:
                out_line = proc.stdout.get_nowait()
        except queue.Empty:
                "no output available"
        else:
                handle_output(out_line)


options = get_options()<EOL>dice = Dice(options.sides)<EOL>rolls = [dice.roll() for n in range(options.number)]<EOL>for roll in rolls:<EOL><INDENT>print('<STR_LIT>', roll)<EOL><DEDENT>if options.number > <NUM_LIT:1>:<EOL><INDENT>print('<STR_LIT>', sum(rolls))<EOL><DEDENT>

Roll n-sided dice and return each result and the total


lock = lock or threading.RLock()<EOL>@functools.wraps(f)<EOL>def exec_atomic(*args, **kwargs):<EOL><INDENT>lock.acquire()<EOL>try:<EOL><INDENT>return f(*args, **kwargs)<EOL><DEDENT>finally:<EOL><INDENT>lock.release()<EOL><DEDENT><DEDENT>return exec_atomic<EOL>

Decorate a function with a reentrant lock to prevent multiple
threads from calling said thread simultaneously.


with context.ExceptionTrap():<EOL><INDENT>with context.ExceptionTrap(*exceptions) as exc:<EOL><INDENT>callable()<EOL><DEDENT><DEDENT>return bool(exc)<EOL>

Return True if the callable throws the specified exception

>>> throws_exception(lambda: int('3'))
False
>>> throws_exception(lambda: int('a'))
True
>>> throws_exception(lambda: int('a'), KeyError)
False


with contextlib2.suppress(Exception):<EOL><INDENT>loaded = json.loads(value)<EOL>assert isinstance(loaded, numbers.Number)<EOL>return loaded<EOL><DEDENT>return value<EOL>

coerce takes a value and attempts to convert it to a float,
or int.

If none of the conversions are successful, the original value is
returned.

>>> coerce('3')
3

>>> coerce('3.0')
3.0

>>> coerce('foo')
'foo'

>>> coerce({})
{}

>>> coerce('{}')
'{}'


def __init__(self,<EOL>vectorized_text1: np.ndarray,<EOL>vectorized_text2: np.ndarray,<EOL>bs: int,<EOL>neg_sample_per_pair: int,<EOL>shuffle: bool = True) -> ClassVar:

<EOL>max_neg_sample = bs - <NUM_LIT:1><EOL>self.eff_bs = (bs * neg_sample_per_pair) + bs<EOL>assert vectorized_text1.shape[<NUM_LIT:0>] == vectorized_text2.shape[<NUM_LIT:0>], '<STR_LIT>'<EOL>assert bs <= vectorized_text1.shape[<NUM_LIT:0>], f'<STR_LIT>'<EOL>assert vectorized_text1.ndim == <NUM_LIT:2>, '<STR_LIT>'<EOL>assert vectorized_text2.ndim == <NUM_LIT:2>, '<STR_LIT>'<EOL>assert neg_sample_per_pair <= max_neg_sample, f'<STR_LIT>'<EOL>logging.warning(f'<STR_LIT>')<EOL>self.nrows = vectorized_text1.shape[<NUM_LIT:0>]<EOL>self.text1 = vectorized_text1<EOL>self.text2 = vectorized_text2<EOL>self.bs = bs<EOL>self.shuffle = shuffle<EOL>self.on_epoch_end()<EOL>self.k_neg = neg_sample_per_pair * bs<EOL>

vectorized_text1 and vectorized_text2 are complimentary pieces of text that
you want to train a neural network to match.

vectorized_text1 : np.ndarray[int]
    This is the vectorized version of 1 body of text.
vectorized_text2 : np.ndarray[int]
    This is the vectorized version of a complimentary body of text that has the
    same number of rows of `vectorized_text1`
bs : int
    batch size.
neg_sample_per_pair : int
    The number of negative samples you want to generate per pair of matching texts. This
    will cause your effective batch size to increase to bs * neg_sample_per_pair
shuffle : bool
    Whether or not you want to shuffle the data after each epoch.  True by default.


return self.nrows // self.bs<EOL>


def __getitem__(self, index: int) -> Tuple:

<EOL>indexes = self.indexes[index*self.bs:(index+<NUM_LIT:1>)*self.bs]<EOL>X, y = self.__negative_sampling(indexes)<EOL>return X, y<EOL>


self.indexes = np.arange(self.nrows)<EOL>if self.shuffle:<EOL><INDENT>np.random.shuffle(self.indexes)<EOL><DEDENT>

Updates indexes after each epoch for shuffling


return int(base * round(float(x) / base))<EOL>


def count_len(tkn_doc: List[str]) -> List[int]:

return myround(len(tkn_doc))<EOL>

Build histogram of length of documents (# of tokens) in increments of 5.


Defines the default function for cleaning text.

This function operates over a list.


def apply_parallel(func: Callable,<EOL>data: List[Any],<EOL>cpu_cores: int = None) -> List[Any]:

if not cpu_cores:<EOL><INDENT>cpu_cores = cpu_count()<EOL><DEDENT>try:<EOL><INDENT>chunk_size = ceil(len(data) / cpu_cores)<EOL>pool = Pool(cpu_cores)<EOL>transformed_data = pool.map(func, chunked(data, chunk_size), chunksize=<NUM_LIT:1>)<EOL><DEDENT>finally:<EOL><INDENT>pool.close()<EOL>pool.join()<EOL>return transformed_data<EOL><DEDENT>

Apply function to list of elements.

Automatically determines the chunk size.


def process_text_constructor(cleaner: Callable,<EOL>tokenizer: Callable,<EOL>append_indicators: bool,<EOL>start_tok: str,<EOL>end_tok: str):

def process_text(text):<EOL><INDENT>if append_indicators:<EOL><INDENT>return [[start_tok] + tokenizer(cleaner(doc)) + [end_tok] for doc in text]<EOL><DEDENT>return [tokenizer(cleaner(doc)) for doc in text]<EOL><DEDENT>return process_text<EOL>

Generate a function that will clean and tokenize text.


def __init__(self,<EOL>cleaner: Callable = None,<EOL>tokenizer: Callable = None) -> None:

if cleaner is None:<EOL><INDENT>self.cleaner = textacy_cleaner<EOL><DEDENT>if tokenizer is None:<EOL><INDENT>self.tokenizer = text_to_word_sequence<EOL><DEDENT>

cleaner: function that takes as input string and outputs a string
tokenizer: function that takes as input a list of strings and outputs
           a list of lists of tokens.


def set_tokenizer(self, func: Callable) -> None:

self.tokenizer = func<EOL>

Set the tokenizer you wish to use.

This is a function that f(str) -> List(str)


def set_cleaner(self, func: Callable) -> None:

self.cleaner = func<EOL>

Set the cleaner you wish to use.

This is a function that f(str) -> str


def __init__(self,<EOL>heuristic_pct_padding: float = <NUM_LIT>,<EOL>append_indicators: bool = False,<EOL>keep_n: int = <NUM_LIT>,<EOL>padding: str = '<STR_LIT>',<EOL>padding_maxlen: Union[int, None] = None,<EOL>truncating: str = '<STR_LIT>'<EOL>):

super().__init__()<EOL>self.heuristic_pct = heuristic_pct_padding<EOL>self.append_indicators = append_indicators<EOL>self.keep_n = keep_n<EOL>self.padding = padding<EOL>self.padding_maxlen = padding_maxlen<EOL>self.truncating = truncating<EOL>self.indexer = None<EOL>self.n_tokens = None<EOL>self.id2token = None<EOL>self.token2id = None<EOL>self.document_length_histogram = Counter()<EOL>self.document_length_stats = None<EOL>self.doc_length_huerestic = None<EOL>self.num_cores = cpu_count()<EOL>self.padding_value = <NUM_LIT:0.0><EOL>self.padding_dtype = '<STR_LIT>'<EOL>self.start_tok = '<STR_LIT>'<EOL>self.end_tok = '<STR_LIT>'<EOL>

Parameters:
----------
heuristic_pct_padding: float
    This parameter is only used if `padding_maxlen` = None.  A histogram
    of documents is calculated, and the maxlen is set heuristic_pct_padding.
append_indicators: bool
    If True, will append the tokens '_start_' and '_end_' to the beginning
    and end of your tokenized documents.  This can be useful when training
    seq2seq models.
keep_n: int = 150000
    This is the maximum size of your vocabulary (unique number of words
    allowed).  Consider limiting this to a reasonable size based upon
    your corpus.
padding : str
    'pre' or 'post', pad either before or after each sequence.
padding_maxlen : int or None
    Maximum sequence length, longer sequences are truncated and shorter
    sequences are padded with zeros at the end.  Note if this is specified,
    the `heuristic_pct_padding` is ignored.
truncating : str
    'pre' or 'post', remove values from sequences larger than padding_maxlen
    either in the beginning or in the end of the sequence.

See https://keras.io/preprocessing/sequence/

Attributes:
-----------
n_tokens : int
    The total number of tokens in the corpus.  Will be less than or
    equal to keep_n
id2token : dict
    dict with { int : str} ex: {'the': 2, 'cat': 3}
    this is used for converting tokens to integers.
token2id : dict
    dict with {str: int} ex: {2: 'the', 3: 'cat'}
    this is used for decoding predictions back to tokens
document_length_stats : pandas.DataFrame
    histogram of document lengths.  Can be used to decide padding_maxlen.


self.num_cores = min(int(n), cpu_count())<EOL>

Set the number of processes for process bases threading.


def process_text(self, text: List[str]) -> List[List[str]]:


def parallel_process_text(self, data: List[str]) -> List[List[str]]:

process_text = process_text_constructor(cleaner=self.cleaner,<EOL>tokenizer=self.tokenizer,<EOL>append_indicators=self.append_indicators,<EOL>start_tok=self.start_tok,<EOL>end_tok=self.end_tok)<EOL>n_cores = self.num_cores<EOL>return flattenlist(apply_parallel(process_text, data, n_cores))<EOL>