Code stringlengths 103 85.9k | Summary listlengths 0 94 |
|---|---|
Please provide a description of the function:def anno_parser(func):
"Look at params (annotated with `Param`) in func and return an `ArgumentParser`"
p = ArgumentParser(description=func.__doc__)
for k,v in inspect.signature(func).parameters.items():
param = func.__annotations__.get(k, Param())
... | [] |
Please provide a description of the function:def call_parse(func):
"Decorator to create a simple CLI from `func` using `anno_parser`"
name = inspect.currentframe().f_back.f_globals['__name__']
if name == "__main__":
args = anno_parser(func).parse_args()
func(**args.__dict__)
else: return... | [] |
Please provide a description of the function:def call_plac(f):
"Decorator to create a simple CLI from `func` using `plac`"
name = inspect.currentframe().f_back.f_globals['__name__']
if name == '__main__':
import plac
res = plac.call(f)
if callable(res): res()
else: return f | [] |
Please provide a description of the function:def numericalize_tok(tokens, max_vocab=50000, min_freq=0, unk_tok="_unk_", pad_tok="_pad_", bos_tok="_bos_", eos_tok="_eos_"):
if isinstance(tokens, str):
raise ValueError("Expected to receive a list of tokens. Received a string instead")
if isinstance(t... | [
"Takes in text tokens and returns int2tok and tok2int converters\n\n Arguments:\n tokens(list): List of tokens. Can be a list of strings, or a list of lists of strings.\n max_vocab(int): Number of tokens to return in the vocab (sorted by frequency)\n min_freq(int): Minimum number of inst... |
Please provide a description of the function:def reset(self):
"If your convolutional window is greater than 1 and you save previous xs, you must reset at the beginning of each new sequence."
for layer in self.layers: layer.reset()
if self.bidirectional:
for layer in self.layers_b... | [] |
Please provide a description of the function:def start_new_kernel(startup_timeout=60, kernel_name='python', **kwargs):
logger.debug('Starting new kernel: "%s"' % kernel_name)
km = KernelManager(kernel_name=kernel_name,
kernel_spec_manager=NbvalKernelspecManager())
km.start_kernel... | [
"Start a new kernel, and return its Manager and Client"
] |
Please provide a description of the function:def get_kernel_spec(self, kernel_name):
if kernel_name == CURRENT_ENV_KERNEL_NAME:
return self.kernel_spec_class(
resource_dir=ipykernel.kernelspec.RESOURCES,
**ipykernel.kernelspec.get_kernel_dict())
else:... | [
"Returns a :class:`KernelSpec` instance for the given kernel_name.\n\n Raises :exc:`NoSuchKernel` if the given kernel name is not found.\n "
] |
Please provide a description of the function:def get_message(self, stream, timeout=None):
try:
if stream == 'iopub':
msg = self.kc.get_iopub_msg(timeout=timeout)
elif stream == 'shell':
msg = self.kc.get_shell_msg(timeout=timeout)
else... | [
"\n Function is used to get a message from the iopub channel.\n Timeout is None by default\n When timeout is reached\n "
] |
Please provide a description of the function:def execute_cell_input(self, cell_input, allow_stdin=None):
if cell_input:
logger.debug('Executing cell: "%s"...', cell_input.splitlines()[0][:40])
else:
logger.debug('Executing empty cell')
return self.kc.execute(cell... | [
"\n Executes a string of python code in cell input.\n We do not allow the kernel to make requests to the stdin\n this is the norm for notebooks\n\n Function returns a unique message id of the reply from\n the kernel.\n "
] |
Please provide a description of the function:def await_reply(self, msg_id, timeout=None):
while True:
msg = self.get_message(stream='shell', timeout=timeout)
# Is this the message we are waiting for?
if msg['parent_header'].get('msg_id') == msg_id:
i... | [
"\n Continuously poll the kernel 'shell' stream for messages until:\n - It receives an 'execute_reply' status for the given message id\n - The timeout is reached awaiting a message, in which case\n a `Queue.Empty` exception will be raised.\n "
] |
Please provide a description of the function:def await_idle(self, parent_id, timeout):
while True:
# Get a message from the kernel iopub channel
msg = self.get_message(timeout=timeout, stream='iopub') # raises Empty on timeout!
if msg['parent_header'].get('msg_id') ... | [
"Poll the iopub stream until an idle message is received for the given parent ID"
] |
Please provide a description of the function:def stop(self):
logger.debug('Stopping kernel')
self.kc.stop_channels()
self.km.shutdown_kernel(now=True)
del self.km | [
"\n Instructs the kernel process to stop channels\n and the kernel manager to then shutdown the process.\n "
] |
Please provide a description of the function:def get_cv_idxs(n, cv_idx=0, val_pct=0.2, seed=42):
np.random.seed(seed)
n_val = int(val_pct*n)
idx_start = cv_idx*n_val
idxs = np.random.permutation(n)
return idxs[idx_start:idx_start+n_val] | [
" Get a list of index values for Validation set from a dataset\n \n Arguments:\n n : int, Total number of elements in the data set.\n cv_idx : int, starting index [idx_start = cv_idx*int(val_pct*n)] \n val_pct : (int, float), validation set percentage \n seed : seed value for Rando... |
Please provide a description of the function:def resize_img(fname, targ, path, new_path, fn=None):
if fn is None:
fn = resize_fn(targ)
dest = os.path.join(path_for(path, new_path, targ), fname)
if os.path.exists(dest): return
im = Image.open(os.path.join(path, fname)).convert('RGB')
os.... | [
"\n Enlarge or shrink a single image to scale, such that the smaller of the height or width dimension is equal to targ.\n "
] |
Please provide a description of the function:def resize_imgs(fnames, targ, path, new_path, resume=True, fn=None):
target_path = path_for(path, new_path, targ)
if resume:
subdirs = {os.path.dirname(p) for p in fnames}
subdirs = {s for s in subdirs if os.path.exists(os.path.join(target_path, ... | [
"\n Enlarge or shrink a set of images in the same directory to scale, such that the smaller of the height or width dimension is equal to targ.\n Note: \n -- This function is multithreaded for efficiency. \n -- When destination file or folder already exist, function exists without raising an error. \n ... |
Please provide a description of the function:def read_dir(path, folder):
full_path = os.path.join(path, folder)
fnames = glob(f"{full_path}/*.*")
directories = glob(f"{full_path}/*/")
if any(fnames):
return [os.path.relpath(f,path) for f in fnames]
elif any(directories):
raise F... | [
" Returns a list of relative file paths to `path` for all files within `folder` "
] |
Please provide a description of the function:def n_hot(ids, c):
'''
one hot encoding by index. Returns array of length c, where all entries are 0, except for the indecies in ids
'''
res = np.zeros((c,), dtype=np.float32)
res[ids] = 1
return res | [] |
Please provide a description of the function:def folder_source(path, folder):
fnames, lbls, all_lbls = read_dirs(path, folder)
lbl2idx = {lbl:idx for idx,lbl in enumerate(all_lbls)}
idxs = [lbl2idx[lbl] for lbl in lbls]
lbl_arr = np.array(idxs, dtype=int)
return fnames, lbl_arr, all_lbls | [
"\n Returns the filenames and labels for a folder within a path\n \n Returns:\n -------\n fnames: a list of the filenames within `folder`\n all_lbls: a list of all of the labels in `folder`, where the # of labels is determined by the # of directories within `folder`\n lbl_arr: a numpy array of ... |
Please provide a description of the function:def parse_csv_labels(fn, skip_header=True, cat_separator = ' '):
df = pd.read_csv(fn, index_col=0, header=0 if skip_header else None, dtype=str)
fnames = df.index.values
df.iloc[:,0] = df.iloc[:,0].str.split(cat_separator)
return fnames, list(df.to_dict(... | [
"Parse filenames and label sets from a CSV file.\n\n This method expects that the csv file at path :fn: has two columns. If it\n has a header, :skip_header: should be set to True. The labels in the\n label set are expected to be space separated.\n\n Arguments:\n fn: Path to a CSV file.\n s... |
Please provide a description of the function:def isdicom(fn):
'''True if the fn points to a DICOM image'''
fn = str(fn)
if fn.endswith('.dcm'):
return True
# Dicom signature from the dicom spec.
with open(fn,'rb') as fh:
fh.seek(0x80)
return fh.read(4)==b'DICM' | [] |
Please provide a description of the function:def open_image(fn):
flags = cv2.IMREAD_UNCHANGED+cv2.IMREAD_ANYDEPTH+cv2.IMREAD_ANYCOLOR
if not os.path.exists(fn) and not str(fn).startswith("http"):
raise OSError('No such file or directory: {}'.format(fn))
elif os.path.isdir(fn) and not str(fn).st... | [
" Opens an image using OpenCV given the file path.\n\n Arguments:\n fn: the file path of the image\n\n Returns:\n The image in RGB format as numpy array of floats normalized to range between 0.0 - 1.0\n "
] |
Please provide a description of the function:def split_by_idx(idxs, *a):
mask = np.zeros(len(a[0]),dtype=bool)
mask[np.array(idxs)] = True
return [(o[mask],o[~mask]) for o in a] | [
"\n Split each array passed as *a, to a pair of arrays like this (elements selected by idxs, the remaining elements)\n This can be used to split multiple arrays containing training data to validation and training set.\n\n :param idxs [int]: list of indexes selected\n :param a list: list of np.array, ea... |
Please provide a description of the function:def resize_imgs(self, targ, new_path, resume=True, fn=None):
dest = resize_imgs(self.fnames, targ, self.path, new_path, resume, fn)
return self.__class__(self.fnames, self.y, self.transform, dest) | [
"\n resize all images in the dataset and save them to `new_path`\n \n Arguments:\n targ (int): the target size\n new_path (string): the new folder to save the images\n resume (bool): if true (default), allow resuming a partial resize operation by checking for the existence\... |
Please provide a description of the function:def denorm(self,arr):
if type(arr) is not np.ndarray: arr = to_np(arr)
if len(arr.shape)==3: arr = arr[None]
return self.transform.denorm(np.rollaxis(arr,1,4)) | [
"Reverse the normalization done to a batch of images.\n\n Arguments:\n arr: of shape/size (N,3,sz,sz)\n "
] |
Please provide a description of the function:def resized(self, dl, targ, new_path, resume = True, fn=None):
return dl.dataset.resize_imgs(targ, new_path, resume=resume, fn=fn) if dl else None | [
"\n Return a copy of this dataset resized\n "
] |
Please provide a description of the function:def resize(self, targ_sz, new_path='tmp', resume=True, fn=None):
new_ds = []
dls = [self.trn_dl,self.val_dl,self.fix_dl,self.aug_dl]
if self.test_dl: dls += [self.test_dl, self.test_aug_dl]
else: dls += [None,None]
t = tqdm_no... | [
"\n Resizes all the images in the train, valid, test folders to a given size.\n\n Arguments:\n targ_sz (int): the target size\n new_path (str): the path to save the resized images (default tmp)\n resume (bool): if True, check for images in the DataSet that haven't been resized yet... |
Please provide a description of the function:def from_arrays(cls, path, trn, val, bs=64, tfms=(None,None), classes=None, num_workers=4, test=None, continuous=False):
f = ArraysIndexRegressionDataset if continuous else ArraysIndexDataset
datasets = cls.get_ds(f, trn, val, tfms, test=test)
... | [
" Read in images and their labels given as numpy arrays\n\n Arguments:\n path: a root path of the data (used for storing trained models, precomputed values, etc)\n trn: a tuple of training data matrix and target label/classification array (e.g. `trn=(x,y)` where `x` has the\n ... |
Please provide a description of the function:def from_paths(cls, path, bs=64, tfms=(None,None), trn_name='train', val_name='valid', test_name=None, test_with_labels=False, num_workers=8):
assert not(tfms[0] is None or tfms[1] is None), "please provide transformations for your train and validation sets"... | [
" Read in images and their labels given as sub-folder names\n\n Arguments:\n path: a root path of the data (used for storing trained models, precomputed values, etc)\n bs: batch size\n tfms: transformations (for data augmentations). e.g. output of `tfms_from_model`\n ... |
Please provide a description of the function:def from_csv(cls, path, folder, csv_fname, bs=64, tfms=(None,None),
val_idxs=None, suffix='', test_name=None, continuous=False, skip_header=True, num_workers=8, cat_separator=' '):
assert not (tfms[0] is None or tfms[1] is None), "please provi... | [
" Read in images and their labels given as a CSV file.\n\n This method should be used when training image labels are given in an CSV file as opposed to\n sub-directories with label names.\n\n Arguments:\n path: a root path of the data (used for storing trained models, precomputed val... |
Please provide a description of the function:def from_path_and_array(cls, path, folder, y, classes=None, val_idxs=None, test_name=None,
num_workers=8, tfms=(None,None), bs=64):
assert not (tfms[0] is None or tfms[1] is None), "please provide transformations for your train and validation set... | [
" Read in images given a sub-folder and their labels given a numpy array\n\n Arguments:\n path: a root path of the data (used for storing trained models, precomputed values, etc)\n folder: a name of the folder in which training images are contained.\n y: numpy array which con... |
Please provide a description of the function:def is_in_ipython():
"Is the code running in the ipython environment (jupyter including)"
program_name = os.path.basename(os.getenv('_', ''))
if ('jupyter-notebook' in program_name or # jupyter-notebook
'ipython' in program_name or # ipython
... | [] |
Please provide a description of the function:def get_ref_free_exc_info():
"Free traceback from references to locals() in each frame to avoid circular reference leading to gc.collect() unable to reclaim memory"
type, val, tb = sys.exc_info()
traceback.clear_frames(tb)
return (type, val, tb) | [] |
Please provide a description of the function:def gpu_mem_restore(func):
"Reclaim GPU RAM if CUDA out of memory happened, or execution was interrupted"
@functools.wraps(func)
def wrapper(*args, **kwargs):
tb_clear_frames = os.environ.get('FASTAI_TB_CLEAR_FRAMES', None)
if not IS_IN_IPYTHON or... | [
"When 'device-side assert triggered' error happens, it's not possible to recover and you must restart the kernel to continue. Use os.environ['CUDA_LAUNCH_BLOCKING']=\"1\" before restarting to debug"
] |
Please provide a description of the function:def fit(model, data, n_epochs, opt, crit, metrics=None, callbacks=None, stepper=Stepper,
swa_model=None, swa_start=None, swa_eval_freq=None, visualize=False, **kwargs):
seq_first = kwargs.pop('seq_first', False)
all_val = kwargs.pop('all_val', False)
... | [
" Fits a model\n\n Arguments:\n model (model): any pytorch module\n net = to_gpu(net)\n data (ModelData): see ModelData class and subclasses (can be a list)\n opts: an optimizer. Example: optim.Adam. \n If n_epochs is a list, it needs to be the layer_optimizer to get the optimiz... |
Please provide a description of the function:def validate_next(stepper, metrics, val_iter):
stepper.reset(False)
with no_grad_context():
(*x,y) = val_iter.next()
preds,l = stepper.evaluate(VV(x), VV(y))
res = [delistify(to_np(l))]
res += [f(datafy(preds), datafy(y)) for f in... | [
"Computes the loss on the next minibatch of the validation set."
] |
Please provide a description of the function:def link_type(arg_type, arg_name=None, include_bt:bool=True):
"Create link to documentation."
arg_name = arg_name or fn_name(arg_type)
if include_bt: arg_name = code_esc(arg_name)
if belongs_to_module(arg_type, 'torch') and ('Tensor' not in arg_name): return ... | [] |
Please provide a description of the function:def belongs_to_module(t, module_name):
"Check if `t` belongs to `module_name`."
if hasattr(t, '__func__'): return belongs_to_module(t.__func__, module_name)
if not inspect.getmodule(t): return False
return inspect.getmodule(t).__name__.startswith(module_name) | [] |
Please provide a description of the function:def format_param(p):
"Formats function param to `param1:Type=val`. Font weights: param1=bold, val=bold+italic"
arg_prefix = arg_prefixes.get(p.kind, '') # asterisk prefix for *args and **kwargs
res = f"**{arg_prefix}{code_esc(p.name)}**"
if hasattr(p, 'annota... | [] |
Please provide a description of the function:def format_ft_def(func, full_name:str=None)->str:
"Format and link `func` definition to show in documentation"
sig = inspect.signature(func)
name = f'<code>{full_name or func.__name__}</code>'
fmt_params = [format_param(param) for name,param
... | [] |
Please provide a description of the function:def get_enum_doc(elt, full_name:str)->str:
"Formatted enum documentation."
vals = ', '.join(elt.__members__.keys())
return f'{code_esc(full_name)}',f'<code>Enum</code> = [{vals}]' | [] |
Please provide a description of the function:def get_cls_doc(elt, full_name:str)->str:
"Class definition."
parent_class = inspect.getclasstree([elt])[-1][0][1][0]
name,args = format_ft_def(elt, full_name)
if parent_class != object: args += f' :: {link_type(parent_class, include_bt=True)}'
return nam... | [] |
Please provide a description of the function:def show_doc(elt, doc_string:bool=True, full_name:str=None, arg_comments:dict=None, title_level=None, alt_doc_string:str='',
ignore_warn:bool=False, markdown=True, show_tests=True):
"Show documentation for element `elt`. Supported types: class, Callable, and... | [] |
Please provide a description of the function:def doc(elt):
"Show `show_doc` info in preview window along with link to full docs."
global use_relative_links
use_relative_links = False
elt = getattr(elt, '__func__', elt)
md = show_doc(elt, markdown=False)
if is_fastai_class(elt):
md += f'\... | [] |
Please provide a description of the function:def format_docstring(elt, arg_comments:dict={}, alt_doc_string:str='', ignore_warn:bool=False)->str:
"Merge and format the docstring definition with `arg_comments` and `alt_doc_string`."
parsed = ""
doc = parse_docstring(inspect.getdoc(elt))
description = alt... | [] |
Please provide a description of the function:def link_docstring(modules, docstring:str, overwrite:bool=False)->str:
"Search `docstring` for backticks and attempt to link those functions to respective documentation."
mods = listify(modules)
for mod in mods: _modvars.update(mod.__dict__) # concat all module d... | [] |
Please provide a description of the function:def find_elt(modvars, keyword, match_last=False):
"Attempt to resolve keywords such as Learner.lr_find. `match_last` starts matching from last component."
keyword = strip_fastai(keyword)
if keyword in modvars: return modvars[keyword]
comps = keyword.split('.'... | [] |
Please provide a description of the function:def import_mod(mod_name:str, ignore_errors=False):
"Return module from `mod_name`."
splits = str.split(mod_name, '.')
try:
if len(splits) > 1 : mod = importlib.import_module('.' + '.'.join(splits[1:]), splits[0])
else: mod = importlib.import_modul... | [] |
Please provide a description of the function:def show_doc_from_name(mod_name, ft_name:str, doc_string:bool=True, arg_comments:dict={}, alt_doc_string:str=''):
"Show documentation for `ft_name`, see `show_doc`."
mod = import_mod(mod_name)
splits = str.split(ft_name, '.')
assert hasattr(mod, splits[0]), p... | [] |
Please provide a description of the function:def get_ft_names(mod, include_inner=False)->List[str]:
"Return all the functions of module `mod`."
# If the module has an attribute __all__, it picks those.
# Otherwise, it returns all the functions defined inside a module.
fn_names = []
for elt_name in g... | [] |
Please provide a description of the function:def get_inner_fts(elt)->List[str]:
"List the inner functions of a class."
fts = []
for ft_name in elt.__dict__.keys():
if ft_name.startswith('_'): continue
ft = getattr(elt, ft_name)
if inspect.isfunction(ft): fts.append(f'{elt.__name__}.{... | [] |
Please provide a description of the function:def get_module_toc(mod_name):
"Display table of contents for given `mod_name`."
mod = import_mod(mod_name)
ft_names = mod.__all__ if hasattr(mod,'__all__') else get_ft_names(mod)
ft_names.sort(key = str.lower)
tabmat = ''
for ft_name in ft_names:
... | [] |
Please provide a description of the function:def get_fn_link(ft)->str:
"Return function link to notebook documentation of `ft`. Private functions link to source code"
ft = getattr(ft, '__func__', ft)
anchor = strip_fastai(get_anchor(ft))
module_name = strip_fastai(get_module_name(ft))
base = '' if u... | [] |
Please provide a description of the function:def get_pytorch_link(ft)->str:
"Returns link to pytorch docs of `ft`."
name = ft.__name__
ext = '.html'
if name == 'device': return f'{PYTORCH_DOCS}tensor_attributes{ext}#torch-device'
if name == 'Tensor': return f'{PYTORCH_DOCS}tensors{ext}#torch-tensor'... | [] |
Please provide a description of the function:def get_source_link(file, line, display_text="[source]", **kwargs)->str:
"Returns github link for given file"
link = f"{SOURCE_URL}{file}#L{line}"
if display_text is None: return link
return f'<a href="{link}" class="source_link" style="float:right">{display_... | [] |
Please provide a description of the function:def get_function_source(ft, **kwargs)->str:
"Returns link to `ft` in source code."
try: line = inspect.getsourcelines(ft)[1]
except Exception: return ''
mod_path = get_module_name(ft).replace('.', '/') + '.py'
return get_source_link(mod_path, line, **kwar... | [] |
Please provide a description of the function:def find_comment_markers(cellsource):
found = {}
for line in cellsource.splitlines():
line = line.strip()
if line.startswith('#'):
# print("Found comment in '{}'".format(line))
comment = line.lstrip('#').strip()
... | [
"Look through the cell source for comments which affect nbval's behaviour\n\n Yield an iterable of ``(MARKER_TYPE, True)``.\n "
] |
Please provide a description of the function:def coalesce_streams(outputs):
if not outputs:
return outputs
new_outputs = []
streams = {}
for output in outputs:
if (output.output_type == 'stream'):
if output.name in streams:
streams[output.name].text += o... | [
"\n Merge all stream outputs with shared names into single streams\n to ensure deterministic outputs.\n\n Parameters\n ----------\n outputs : iterable of NotebookNodes\n Outputs being processed\n "
] |
Please provide a description of the function:def transform_streams_for_comparison(outputs):
new_outputs = []
for output in outputs:
if (output.output_type == 'stream'):
# Transform output
new_outputs.append({
'output_type': 'stream',
output.na... | [
"Makes failure output for streams better by having key be the stream name"
] |
Please provide a description of the function:def _trim_base64(s):
if len(s) > 64 and _base64.match(s.replace('\n', '')):
h = hash_string(s)
s = '%s...<snip base64, md5=%s...>' % (s[:8], h[:16])
return s | [
"Trim and hash base64 strings"
] |
Please provide a description of the function:def _indent(s, indent=' '):
if isinstance(s, six.string_types):
return '\n'.join(('%s%s' % (indent, line) for line in s.splitlines()))
return s | [
"Intent each line with indent"
] |
Please provide a description of the function:def setup(self):
if self.parent.config.option.current_env:
kernel_name = CURRENT_ENV_KERNEL_NAME
else:
kernel_name = self.nb.metadata.get(
'kernelspec', {}).get('name', 'python')
self.kernel = RunningK... | [
"\n Called by pytest to setup the collector cells in .\n Here we start a kernel and setup the sanitize patterns.\n "
] |
Please provide a description of the function:def setup_sanitize_files(self):
for fname in self.get_sanitize_files():
with open(fname, 'r') as f:
self.sanitize_patterns.update(get_sanitize_patterns(f.read())) | [
"\n For each of the sanitize files that were specified as command line options\n load the contents of the file into the sanitise patterns dictionary.\n "
] |
Please provide a description of the function:def get_sanitize_files(self):
if self.parent.config.option.sanitize_with is not None:
return [self.parent.config.option.sanitize_with]
else:
return [] | [
"\n Return list of all sanitize files provided by the user on the command line.\n\n N.B.: We only support one sanitize file at the moment, but\n this is likely to change in the future\n\n "
] |
Please provide a description of the function:def get_kernel_message(self, timeout=None, stream='iopub'):
return self.kernel.get_message(stream, timeout=timeout) | [
"\n Gets a message from the iopub channel of the notebook kernel.\n "
] |
Please provide a description of the function:def collect(self):
self.nb = nbformat.read(str(self.fspath), as_version=4)
# Start the cell count
cell_num = 0
# Iterate over the cells in the notebook
for cell in self.nb.cells:
# Skip the cells that have text,... | [
"\n The collect function is required by pytest and is used to yield pytest\n Item objects. We specify an Item for each code cell in the notebook.\n "
] |
Please provide a description of the function:def repr_failure(self, excinfo):
exc = excinfo.value
cc = self.colors
if isinstance(exc, NbCellError):
msg_items = [
cc.FAIL + "Notebook cell execution failed" + cc.ENDC]
formatstring = (
... | [
" called when self.runtest() raises an exception. "
] |
Please provide a description of the function:def format_output_compare(self, key, left, right):
if isinstance(left, six.string_types):
left = _trim_base64(left)
if isinstance(right, six.string_types):
right = _trim_base64(right)
cc = self.colors
self.co... | [
"Format an output for printing"
] |
Please provide a description of the function:def sanitize(self, s):
if not isinstance(s, six.string_types):
return s
for regex, replace in six.iteritems(self.parent.sanitize_patterns):
s = re.sub(regex, replace, s)
return s | [
"sanitize a string for comparison.\n ",
"\n re.sub matches a regex and replaces it with another.\n The regex replacements are taken from a file if the option\n is passed when py.test is called. Otherwise, the strings\n are not processed\n "
] |
Please provide a description of the function:def _tta_only(learn:Learner, ds_type:DatasetType=DatasetType.Valid, scale:float=1.35) -> Iterator[List[Tensor]]:
"Computes the outputs for several augmented inputs for TTA"
dl = learn.dl(ds_type)
ds = dl.dataset
old = ds.tfms
augm_tfm = [o for o in learn.... | [] |
Please provide a description of the function:def _TTA(learn:Learner, beta:float=0.4, scale:float=1.35, ds_type:DatasetType=DatasetType.Valid, with_loss:bool=False) -> Tensors:
"Applies TTA to predict on `ds_type` dataset."
preds,y = learn.get_preds(ds_type)
all_preds = list(learn.tta_only(scale=scale, ds_ty... | [] |
Please provide a description of the function:def fbeta(y_pred:Tensor, y_true:Tensor, thresh:float=0.2, beta:float=2, eps:float=1e-9, sigmoid:bool=True)->Rank0Tensor:
"Computes the f_beta between `preds` and `targets`"
beta2 = beta ** 2
if sigmoid: y_pred = y_pred.sigmoid()
y_pred = (y_pred>thresh).float... | [] |
Please provide a description of the function:def accuracy(input:Tensor, targs:Tensor)->Rank0Tensor:
"Compute accuracy with `targs` when `input` is bs * n_classes."
n = targs.shape[0]
input = input.argmax(dim=-1).view(n,-1)
targs = targs.view(n,-1)
return (input==targs).float().mean() | [] |
Please provide a description of the function:def accuracy_thresh(y_pred:Tensor, y_true:Tensor, thresh:float=0.5, sigmoid:bool=True)->Rank0Tensor:
"Compute accuracy when `y_pred` and `y_true` are the same size."
if sigmoid: y_pred = y_pred.sigmoid()
return ((y_pred>thresh)==y_true.byte()).float().mean() | [] |
Please provide a description of the function:def top_k_accuracy(input:Tensor, targs:Tensor, k:int=5)->Rank0Tensor:
"Computes the Top-k accuracy (target is in the top k predictions)."
input = input.topk(k=k, dim=-1)[1]
targs = targs.unsqueeze(dim=-1).expand_as(input)
return (input == targs).max(dim=-1)[0... | [] |
Please provide a description of the function:def dice(input:Tensor, targs:Tensor, iou:bool=False)->Rank0Tensor:
"Dice coefficient metric for binary target. If iou=True, returns iou metric, classic for segmentation problems."
n = targs.shape[0]
input = input.argmax(dim=1).view(n,-1)
targs = targs.view(n,... | [] |
Please provide a description of the function:def exp_rmspe(pred:Tensor, targ:Tensor)->Rank0Tensor:
"Exp RMSE between `pred` and `targ`."
pred,targ = flatten_check(pred,targ)
pred, targ = torch.exp(pred), torch.exp(targ)
pct_var = (targ - pred)/targ
return torch.sqrt((pct_var**2).mean()) | [] |
Please provide a description of the function:def mean_absolute_error(pred:Tensor, targ:Tensor)->Rank0Tensor:
"Mean absolute error between `pred` and `targ`."
pred,targ = flatten_check(pred,targ)
return torch.abs(targ - pred).mean() | [] |
Please provide a description of the function:def mean_squared_error(pred:Tensor, targ:Tensor)->Rank0Tensor:
"Mean squared error between `pred` and `targ`."
pred,targ = flatten_check(pred,targ)
return F.mse_loss(pred, targ) | [] |
Please provide a description of the function:def root_mean_squared_error(pred:Tensor, targ:Tensor)->Rank0Tensor:
"Root mean squared error between `pred` and `targ`."
pred,targ = flatten_check(pred,targ)
return torch.sqrt(F.mse_loss(pred, targ)) | [] |
Please provide a description of the function:def mean_squared_logarithmic_error(pred:Tensor, targ:Tensor)->Rank0Tensor:
"Mean squared logarithmic error between `pred` and `targ`."
pred,targ = flatten_check(pred,targ)
return F.mse_loss(torch.log(1 + pred), torch.log(1 + targ)) | [] |
Please provide a description of the function:def explained_variance(pred:Tensor, targ:Tensor)->Rank0Tensor:
"Explained variance between `pred` and `targ`."
pred,targ = flatten_check(pred,targ)
var_pct = torch.var(targ - pred) / torch.var(targ)
return 1 - var_pct | [] |
Please provide a description of the function:def r2_score(pred:Tensor, targ:Tensor)->Rank0Tensor:
"R2 score (coefficient of determination) between `pred` and `targ`."
pred,targ = flatten_check(pred,targ)
u = torch.sum((targ - pred) ** 2)
d = torch.sum((targ - targ.mean()) ** 2)
return 1 - u / d | [] |
Please provide a description of the function:def auc_roc_score(input:Tensor, targ:Tensor):
"Using trapezoid method to calculate the area under roc curve"
fpr, tpr = roc_curve(input, targ)
d = fpr[1:] - fpr[:-1]
sl1, sl2 = [slice(None)], [slice(None)]
sl1[-1], sl2[-1] = slice(1, None), slice(None, -1... | [] |
Please provide a description of the function:def roc_curve(input:Tensor, targ:Tensor):
"Returns the false positive and true positive rates"
targ = (targ == 1)
desc_score_indices = torch.flip(input.argsort(-1), [-1])
input = input[desc_score_indices]
targ = targ[desc_score_indices]
d = input[1:] ... | [] |
Please provide a description of the function:def A(*a):
return np.array(a[0]) if len(a)==1 else [np.array(o) for o in a] | [
"convert iterable object into numpy array"
] |
Please provide a description of the function:def T(a, half=False, cuda=True):
if not torch.is_tensor(a):
a = np.array(np.ascontiguousarray(a))
if a.dtype in (np.int8, np.int16, np.int32, np.int64):
a = torch.LongTensor(a.astype(np.int64))
elif a.dtype in (np.float32, np.floa... | [
"\n Convert numpy array into a pytorch tensor. \n if Cuda is available and USE_GPU=True, store resulting tensor in GPU.\n "
] |
Please provide a description of the function:def V_(x, requires_grad=False, volatile=False):
'''equivalent to create_variable, which creates a pytorch tensor'''
return create_variable(x, volatile=volatile, requires_grad=requires_grad) | [] |
Please provide a description of the function:def V(x, requires_grad=False, volatile=False):
'''creates a single or a list of pytorch tensors, depending on input x. '''
return map_over(x, lambda o: V_(o, requires_grad, volatile)) | [] |
Please provide a description of the function:def to_np(v):
'''returns an np.array object given an input of np.array, list, tuple, torch variable or tensor.'''
if isinstance(v, float): return np.array(v)
if isinstance(v, (np.ndarray, np.generic)): return v
if isinstance(v, (list,tuple)): return [to_np(o)... | [] |
Please provide a description of the function:def to_gpu(x, *args, **kwargs):
'''puts pytorch variable to gpu, if cuda is available and USE_GPU is set to true. '''
return x.cuda(*args, **kwargs) if USE_GPU else x | [] |
Please provide a description of the function:def split_by_idxs(seq, idxs):
'''A generator that returns sequence pieces, seperated by indexes specified in idxs. '''
last = 0
for idx in idxs:
if not (-len(seq) <= idx < len(seq)):
raise KeyError(f'Idx {idx} is out-of-bounds')
yield se... | [] |
Please provide a description of the function:def partition(a, sz):
return [a[i:i+sz] for i in range(0, len(a), sz)] | [
"splits iterables a in equal parts of size sz"
] |
Please provide a description of the function:def chunk_iter(iterable, chunk_size):
'''A generator that yields chunks of iterable, chunk_size at a time. '''
while True:
chunk = []
try:
for _ in range(chunk_size): chunk.append(next(iterable))
yield chunk
except Stop... | [] |
Please provide a description of the function:def _brightness(x, change:uniform):
"Apply `change` in brightness of image `x`."
return x.add_(scipy.special.logit(change)) | [] |
Please provide a description of the function:def _rotate(degrees:uniform):
"Rotate image by `degrees`."
angle = degrees * math.pi / 180
return [[cos(angle), -sin(angle), 0.],
[sin(angle), cos(angle), 0.],
[0. , 0. , 1.]] | [] |
Please provide a description of the function:def _get_zoom_mat(sw:float, sh:float, c:float, r:float)->AffineMatrix:
"`sw`,`sh` scale width,height - `c`,`r` focus col,row."
return [[sw, 0, c],
[0, sh, r],
[0, 0, 1.]] | [] |
Please provide a description of the function:def _zoom(scale:uniform=1.0, row_pct:uniform=0.5, col_pct:uniform=0.5):
"Zoom image by `scale`. `row_pct`,`col_pct` select focal point of zoom."
s = 1-1/scale
col_c = s * (2*col_pct - 1)
row_c = s * (2*row_pct - 1)
return _get_zoom_mat(1/scale, 1/scale, c... | [] |
Please provide a description of the function:def _squish(scale:uniform=1.0, row_pct:uniform=0.5, col_pct:uniform=0.5):
"Squish image by `scale`. `row_pct`,`col_pct` select focal point of zoom."
if scale <= 1:
col_c = (1-scale) * (2*col_pct - 1)
return _get_zoom_mat(scale, 1, col_c, 0.)
else:... | [] |
Please provide a description of the function:def _jitter(c, magnitude:uniform):
"Replace pixels by random neighbors at `magnitude`."
c.flow.add_((torch.rand_like(c.flow)-0.5)*magnitude*2)
return c | [] |
Please provide a description of the function:def _flip_lr(x):
"Flip `x` horizontally."
#return x.flip(2)
if isinstance(x, ImagePoints):
x.flow.flow[...,0] *= -1
return x
return tensor(np.ascontiguousarray(np.array(x)[...,::-1])) | [] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.