text_prompt
stringlengths
157
13.1k
code_prompt
stringlengths
7
19.8k
⌀
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def parse_memory(s): """Converts bytes expression to number of mebibytes. If no unit is specified, ``MiB`` is used."""
if isinstance(s, integer): out = s elif isinstance(s, float): out = math_ceil(s) elif isinstance(s, string): s = s.replace(' ', '') if not s: raise context.ValueError("Could not interpret %r as a byte unit" % s) if s[0].isdigit(): for i, c i...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def from_default(cls): """The default security configuration. Usually this loads the credentials stored in the configuration directory (``~/.skein`` by default)....
from .core import properties # Are we in a container started by skein? if properties.application_id is not None: if properties.container_dir is not None: cert_path = os.path.join(properties.container_dir, '.skein.crt') key_path = os.path.join(propert...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def from_directory(cls, directory): """Create a security object from a directory. Relies on standard names for each file (``skein.crt`` and ``skein.pem``)."""
cert_path = os.path.join(directory, 'skein.crt') key_path = os.path.join(directory, 'skein.pem') for path, name in [(cert_path, 'cert'), (key_path, 'key')]: if not os.path.exists(path): raise context.FileNotFoundError( "Security %s file not found ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def to_directory(self, directory, force=False): """Write this security object to a directory. Parameters directory : str The directory to write the configuration...
self._validate() # Create directory if it doesn't exist makedirs(directory, exist_ok=True) cert_path = os.path.join(directory, 'skein.crt') key_path = os.path.join(directory, 'skein.pem') cert_bytes = self._get_bytes('cert') key_bytes = self._get_bytes('key') ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _from_any(cls, spec): """Generic creation method for all types accepted as ``spec``"""
if isinstance(spec, str): spec = cls.from_file(spec) elif isinstance(spec, dict): spec = cls.from_dict(spec) elif not isinstance(spec, cls): raise context.TypeError("spec must be either an ApplicationSpec, " "path, or dict,...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def from_file(cls, path, format='infer'): """Create an instance from a json or yaml file. Parameters path : str The path to the file to load. format : {'infer', ...
format = _infer_format(path, format=format) origin = os.path.abspath(os.path.dirname(path)) with open(path) as f: data = f.read() if format == 'json': obj = json.loads(data) else: obj = yaml.safe_load(data) return cls.from_dict(obj, _...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def to_file(self, path, format='infer', skip_nulls=True): """Write object to a file. Parameters path : str The path to the file to load. format : {'infer', 'json...
format = _infer_format(path, format=format) data = getattr(self, 'to_' + format)(skip_nulls=skip_nulls) with open(path, mode='w') as f: f.write(data)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def lock_file(path): """File based lock on ``path``. Creates a file based lock. When acquired, other processes or threads are prevented from acquiring the same l...
with _paths_lock: lock = _paths_to_locks.get(path) if lock is None: _paths_to_locks[path] = lock = _FileLock(path) return lock
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def grpc_fork_support_disabled(): """Temporarily disable fork support in gRPC. Fork + exec has always been supported, but the recent fork handling code in gRPC (...
if LooseVersion(GRPC_VERSION) < '1.18.0': key = 'GRPC_ENABLE_FORK_SUPPORT' try: os.environ[key] = '0' yield finally: del os.environ[key] else: yield
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def humanize_timedelta(td): """Pretty-print a timedelta in a human readable format."""
secs = int(td.total_seconds()) hours, secs = divmod(secs, 60 * 60) mins, secs = divmod(secs, 60) if hours: return '%dh %dm' % (hours, mins) if mins: return '%dm' % mins return '%ds' % secs
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def datetime_to_millis(x): """Convert a `datetime.datetime` to milliseconds since the epoch"""
if x is None: return None if hasattr(x, 'timestamp'): # Python >= 3.3 secs = x.timestamp() elif x.tzinfo is None: # Timezone naive secs = (time.mktime((x.year, x.month, x.day, x.hour, x.minute, x.second, -1, -...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def format_table(columns, rows): """Formats an ascii table for given columns and rows. Parameters columns : list The column names rows : list of tuples The rows ...
rows = [tuple(str(i) for i in r) for r in rows] columns = tuple(str(i).upper() for i in columns) if rows: widths = tuple(max(max(map(len, x)), len(c)) for x, c in zip(zip(*rows), columns)) else: widths = tuple(map(len, columns)) row_template = (' '.join('%%...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def build_attrs(self, *args, **kwargs): """Add select2 data attributes."""
attrs = super(Select2Mixin, self).build_attrs(*args, **kwargs) if self.is_required: attrs.setdefault('data-allow-clear', 'false') else: attrs.setdefault('data-allow-clear', 'true') attrs.setdefault('data-placeholder', '') attrs.setdefault('data-minim...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def optgroups(self, name, value, attrs=None): """Add empty option for clearable selects."""
if not self.is_required and not self.allow_multiple_selected: self.choices = list(chain([('', '')], self.choices)) return super(Select2Mixin, self).optgroups(name, value, attrs=attrs)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _get_media(self): """ Construct Media as a dynamic property. .. Note:: For more information visit https://docs.djangoproject.com/en/stable/topics/forms/media...
lang = get_language() select2_js = (settings.SELECT2_JS,) if settings.SELECT2_JS else () select2_css = (settings.SELECT2_CSS,) if settings.SELECT2_CSS else () i18n_name = SELECT2_TRANSLATIONS.get(lang) if i18n_name not in settings.SELECT2_I18N_AVAILABLE_LANGUAGES: i...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def build_attrs(self, *args, **kwargs): """Add select2's tag attributes."""
self.attrs.setdefault('data-minimum-input-length', 1) self.attrs.setdefault('data-tags', 'true') self.attrs.setdefault('data-token-separators', '[",", " "]') return super(Select2TagMixin, self).build_attrs(*args, **kwargs)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def build_attrs(self, *args, **kwargs): """Set select2's AJAX attributes."""
attrs = super(HeavySelect2Mixin, self).build_attrs(*args, **kwargs) # encrypt instance Id self.widget_id = signing.dumps(id(self)) attrs['data-field_id'] = self.widget_id attrs.setdefault('data-ajax--url', self.get_url()) attrs.setdefault('data-ajax--cache', "true") ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def render(self, *args, **kwargs): """Render widget and register it in Django's cache."""
output = super(HeavySelect2Mixin, self).render(*args, **kwargs) self.set_to_cache() return output
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def set_to_cache(self): """ Add widget object to Django's cache. You may need to overwrite this method, to pickle all information that is required to serve your ...
try: cache.set(self._get_cache_key(), { 'widget': self, 'url': self.get_url(), }) except (PicklingError, AttributeError): msg = "You need to overwrite \"set_to_cache\" or ensure that %s is serialisable." raise NotImplemente...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def set_to_cache(self): """ Add widget's attributes to Django's cache. Split the QuerySet, to not pickle the result set. """
queryset = self.get_queryset() cache.set(self._get_cache_key(), { 'queryset': [ queryset.none(), queryset.query, ], 'cls': self.__class__, 'search_fields': tuple(self.search_fields), ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def filter_queryset(self, request, term, queryset=None, **dependent_fields): """ Return QuerySet filtered by search_fields matching the passed term. Args: reques...
if queryset is None: queryset = self.get_queryset() search_fields = self.get_search_fields() select = Q() term = term.replace('\t', ' ') term = term.replace('\n', ' ') for t in [t for t in term.split(' ') if not t == '']: select &= reduce(lambda x...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_search_fields(self): """Return list of lookup names."""
if self.search_fields: return self.search_fields raise NotImplementedError('%s, must implement "search_fields".' % self.__class__.__name__)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def optgroups(self, name, value, attrs=None): """Return only selected options and set QuerySet from `ModelChoicesIterator`."""
default = (None, [], 0) groups = [default] has_selected = False selected_choices = {str(v) for v in value} if not self.is_required and not self.allow_multiple_selected: default[1].append(self.create_option(name, '', '', False, 0)) if not isinstance(self.choic...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_queryset(self): """Get QuerySet from cached widget."""
kwargs = { model_field_name: self.request.GET.get(form_field_name) for form_field_name, model_field_name in self.widget.dependent_fields.items() if form_field_name in self.request.GET and self.request.GET.get(form_field_name, '') != '' } return self.widget.fi...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_widget_or_404(self): """ Get and return widget from cache. Raises: Http404: If if the widget can not be found or no id is provided. Returns: ModelSelect2...
field_id = self.kwargs.get('field_id', self.request.GET.get('field_id', None)) if not field_id: raise Http404('No "field_id" provided.') try: key = signing.loads(field_id) except BadSignature: raise Http404('Invalid "field_id".') else: ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def load_widget(path): """ Load custom widget for the form field """
i = path.rfind('.') module, attr = path[:i], path[i + 1:] try: mod = import_module(module) except (ImportError, ValueError) as e: error_message = 'Error importing widget for BleachField %s: "%s"' raise ImproperlyConfigured(error_message % (path, e)) try: cls = getat...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_default_widget(): """ Get the default widget or the widget defined in settings """
default_widget = forms.Textarea if hasattr(settings, 'BLEACH_DEFAULT_WIDGET'): default_widget = load_widget(settings.BLEACH_DEFAULT_WIDGET) return default_widget
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def to_python(self, value): """ Strips any dodgy HTML tags from the input """
if value in self.empty_values: try: return self.empty_value except AttributeError: # CharField.empty_value was introduced in Django 1.11; in prior # versions a unicode string was returned for empty values in # all cases. ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def build_dynamic_field(self, group, field_meta): """ Builds a field based on JIRA's meta field information """
schema = field_meta['schema'] # set up some defaults for form fields fieldtype = 'text' fkwargs = { 'label': field_meta['name'], 'required': field_meta['required'], } # override defaults based on field configuration if (schema['type'] in ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def create_issue(self, request, group, form_data, **kwargs): """ Creates the issue on the remote service and returns an issue ID. """
instance = self.get_option('instance', group.project) project = ( form_data.get('project') or self.get_option('default_project', group.project) ) client = self.get_client(request.user) title = form_data['title'] description = form_data['descript...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def _format_date_param(params, key, format="%Y-%m-%d %H:%M:%S"): """ Utility function to convert datetime values to strings. If the value is already a str, or is...
if key in params: param = params[key] if hasattr(param, "strftime"): params[key] = param.strftime(format)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def submit_sms_conversion(self, message_id, delivered=True, timestamp=None): """ Notify Nexmo that an SMS was successfully received. :param message_id: The `mess...
params = { "message-id": message_id, "delivered": delivered, "timestamp": timestamp or datetime.now(pytz.utc), } # Ensure timestamp is a string: _format_date_param(params, "timestamp") return self.post(self.api_host, "/conversions/sms", params...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_stripped_file_lines(filename): """ Return lines of a file with whitespace removed """
try: lines = open(filename).readlines() except FileNotFoundError: fatal("Could not open file: {!r}".format(filename)) return [line.strip() for line in lines]
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def create_settings(from_environment=False, locustfile=None, classes=None, host=None, num_clients=None, hatch_rate=None, reset_stats=False, run_time="3m"): ''' Returns a settings object to be used by a LocalLocustRunner. Arguments from_environment: get settings from environment var...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description:
def get_lambda_runtime_info(context): ''' Returns a dictionary of information about the AWS Lambda function invocation Arguments context: The context object from AWS Lambda. ''' runtime_info = { 'remaining_time': context.get_remaining_time_in_millis(), 'function_name': context...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def solvedbi_sm(ah, rho, b, c=None, axis=4): r""" Solve a diagonal block linear system with a scaled identity term using the Sherman-Morrison equation. The solut...
a = np.conj(ah) if c is None: c = solvedbi_sm_c(ah, a, rho, axis) if have_numexpr: cb = inner(c, b, axis=axis) return ne.evaluate('(b - (a * cb)) / rho') else: return (b - (a * inner(c, b, axis=axis))) / rho
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def solvedbd_sm(ah, d, b, c=None, axis=4): r""" Solve a diagonal block linear system with a diagonal term using the Sherman-Morrison equation. The solution is ob...
a = np.conj(ah) if c is None: c = solvedbd_sm_c(ah, a, d, axis) if have_numexpr: cb = inner(c, b, axis=axis) return ne.evaluate('(b - (a * cb)) / d') else: return (b - (a * inner(c, b, axis=axis))) / d
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def Gax(x, ax): """ Compute gradient of `x` along axis `ax`. Parameters x : array_like Input array ax : int Axis on which gradient is to be computed Returns ----...
slc = (slice(None),)*ax + (slice(-1, None),) xg = np.roll(x, -1, axis=ax) - x xg[slc] = 0.0 return xg
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def GTax(x, ax): """ Compute transpose of gradient of `x` along axis `ax`. Parameters x : array_like Input array ax : int Axis on which gradient transpose is to ...
slc0 = (slice(None),) * ax xg = np.roll(x, 1, axis=ax) - x xg[slc0 + (slice(0, 1),)] = -x[slc0 + (slice(0, 1),)] xg[slc0 + (slice(-1, None),)] = x[slc0 + (slice(-2, -1),)] return xg
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def GradientFilters(ndim, axes, axshp, dtype=None): r""" Construct a set of filters for computing gradients in the frequency domain. Parameters ndim : integer To...
if dtype is None: dtype = np.float32 g = np.zeros([2 if k in axes else 1 for k in range(ndim)] + [len(axes),], dtype) for k in axes: g[(0,) * k + (slice(None),) + (0,) * (g.ndim - 2 - k) + (k,)] = \ np.array([1, -1]) Gf = rfftn(g, axshp, axes=axes) GHGf...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def promote16(u, fn=None, *args, **kwargs): r""" Utility function for use with functions that do not support arrays of dtype ``np.float16``. This function has tw...
dtype = np.float32 if u.dtype == np.float16 else u.dtype up = np.asarray(u, dtype=dtype) if fn is None: return up else: v = fn(up, *args, **kwargs) if isinstance(v, tuple): vp = tuple([np.asarray(vk, dtype=u.dtype) for vk in v]) else: vp = np.asa...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def LaplaceCentreWeight(self): """Centre weighting matrix for TV Laplacian."""
sz = [1,] * self.S.ndim for ax in self.axes: sz[ax] = self.S.shape[ax] lcw = 2*len(self.axes)*np.ones(sz, dtype=self.dtype) for ax in self.axes: lcw[(slice(None),)*ax + ([0, -1],)] -= 1.0 return lcw
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def GaussSeidelStep(self, S, X, ATYU, rho, lcw, W2): """Gauss-Seidel step for linear system in TV problem."""
Xss = np.zeros_like(S, dtype=self.dtype) for ax in self.axes: Xss += sl.zpad(X[(slice(None),)*ax + (slice(0, -1),)], (1, 0), ax) Xss += sl.zpad(X[(slice(None),)*ax + (slice(1, None),)], (0, 1), ax) return (rho*(Xss +...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def runtime(self): """Transitional property providing access to the new timer mechanism. This will be removed in the future. """
warnings.warn("admm.ADMM.runtime attribute has been replaced by " "an upgraded timer class: please see the documentation " "for admm.ADMM.solve method and util.Timer class", PendingDeprecationWarning) return self.timer.elapsed('init') +...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def ustep(self): """Dual variable update."""
self.U += self.rsdl_r(self.AX, self.Y)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def update_rho(self, k, r, s): """Automatic rho adjustment."""
if self.opt['AutoRho', 'Enabled']: tau = self.rho_tau mu = self.rho_mu xi = self.rho_xi if k != 0 and np.mod(k + 1, self.opt['AutoRho', 'Period']) == 0: if self.opt['AutoRho', 'AutoScaling']: if s == 0.0 or r == 0.0: ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def display_status(self, fmtstr, itst): """Display current iteration status as selection of fields from iteration stats tuple. """
if self.opt['Verbose']: hdrtxt = type(self).hdrtxt() hdrval = type(self).hdrval() itdsp = tuple([getattr(itst, hdrval[col]) for col in hdrtxt]) if not self.opt['AutoRho', 'Enabled']: itdsp = itdsp[0:-1] print(fmtstr % itdsp)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def rsdl_sn(self, U): """Compute dual residual normalisation term. Overriding this method is required if methods :meth:`cnst_A`, :meth:`cnst_AT`, :meth:`cnst_B`,...
return self.rho * np.linalg.norm(self.cnst_AT(U))
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def getmin(self): """Get minimiser after optimisation."""
if self.opt['ReturnVar'] == 'X': return self.var_x() elif self.opt['ReturnVar'] == 'Y0': return self.var_y0() elif self.opt['ReturnVar'] == 'Y1': return self.var_y1() else: raise ValueError(self.opt['ReturnVar'] + ' is not a valid value' ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def cbpdnmsk_class_label_lookup(label): """Get a ConvBPDNMask class from a label string."""
clsmod = {'admm': admm_cbpdn.ConvBPDNMaskDcpl, 'fista': fista_cbpdn.ConvBPDNMask} if label in clsmod: return clsmod[label] else: raise ValueError('Unknown ConvBPDNMask solver method %s' % label)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def ConvBPDNMaskOptionsDefaults(method='admm'): """Get defaults dict for the ConvBPDNMask class specified by the ``method`` parameter. """
dflt = copy.deepcopy(cbpdnmsk_class_label_lookup(method).Options.defaults) if method == 'admm': dflt.update({'MaxMainIter': 1, 'AutoRho': {'Period': 10, 'AutoScaling': False, 'RsdlRatio': 10.0, 'Scaling': 2.0, 'RsdlTarget': 1.0}}) el...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def ccmodmsk_class_label_lookup(label): """Get a ConvCnstrMODMask class from a label string."""
clsmod = {'ism': admm_ccmod.ConvCnstrMODMaskDcpl_IterSM, 'cg': admm_ccmod.ConvCnstrMODMaskDcpl_CG, 'cns': admm_ccmod.ConvCnstrMODMaskDcpl_Consensus, 'fista': fista_ccmod.ConvCnstrMODMask} if label in clsmod: return clsmod[label] else: raise ValueEr...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def ConvCnstrMODMaskOptionsDefaults(method='fista'): """Get defaults dict for the ConvCnstrMODMask class specified by the ``method`` parameter. """
dflt = copy.deepcopy(ccmodmsk_class_label_lookup(method).Options.defaults) if method == 'fista': dflt.update({'MaxMainIter': 1, 'BackTrack': {'gamma_u': 1.2, 'MaxIter': 50}}) else: dflt.update({'MaxMainIter': 1, 'AutoRho': {'Period': 10, 'AutoScali...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def pathsplit(pth, dropext=True): """Split a path into a tuple of all of its components."""
if dropext: pth = os.path.splitext(pth)[0] parts = os.path.split(pth) if parts[0] == '': return parts[1:] elif len(parts[0]) == 1: return parts else: return pathsplit(parts[0], dropext=False) + parts[1:]
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def update_required(srcpth, dstpth): """ If the file at `dstpth` is generated from the file at `srcpth`, determine whether an update is required. Returns True if...
return not os.path.exists(dstpth) or \ os.stat(srcpth).st_mtime > os.stat(dstpth).st_mtime
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def read_sphinx_environment(pth): """Read the sphinx environment.pickle file at path `pth`."""
with open(pth, 'rb') as fo: env = pickle.load(fo) return env
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def parse_rst_index(rstpth): """ Parse the top-level RST index file, at `rstpth`, for the example python scripts. Returns a list of subdirectories in order of ap...
pthidx = {} pthlst = [] with open(rstpth) as fd: lines = fd.readlines() for i, l in enumerate(lines): if i > 0: if re.match(r'^ \w+', l) is not None and \ re.match(r'^\w+', lines[i - 1]) is not None: # List of subdirectories in order of appea...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def preprocess_script_string(str): """ Process python script represented as string `str` in preparation for conversion to a notebook. This processing includes re...
# Remove header comment str = re.sub(r'^(#[^#\n]+\n){5}\n*', r'', str) # Insert notebook plotting configuration function str = re.sub(r'from sporco import plot', r'from sporco import plot' '\nplot.config_notebook_plotting()', str, flags=re.MULTILINE) # Remove fina...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def script_string_to_notebook(str, pth): """ Convert a python script represented as string `str` to a notebook with filename `pth`. """
nb = py2jn.py_string_to_notebook(str) py2jn.write_notebook(nb, pth)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def script_to_notebook(spth, npth, cr): """ Convert the script at `spth` to a notebook at `npth`. Parameter `cr` is a CrossReferenceLookup object. """
# Read entire text of example script with open(spth) as f: stxt = f.read() # Process script text stxt = preprocess_script_string(stxt) # If the notebook file exists and has been executed, try to # update markdown cells without deleting output cells if os.path.exists(npth) and note...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def script_string_to_notebook_with_links(str, pth, cr=None): """ Convert a python script represented as string `str` to a notebook with filename `pth` and replac...
if cr is None: script_string_to_notebook(str, pth) else: ntbk = script_string_to_notebook_object(str) notebook_substitute_ref_with_url(ntbk, cr) with open(pth, 'wt') as f: nbformat.write(ntbk, f)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def rst_to_notebook(infile, outfile): """Convert an rst file to a notebook file."""
# Read infile into a string with open(infile, 'r') as fin: rststr = fin.read() # Convert string from rst to markdown mdfmt = 'markdown_github+tex_math_dollars+fenced_code_attributes' mdstr = pypandoc.convert_text(rststr, mdfmt, format='rst', extra_args=['-...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def markdown_to_notebook(infile, outfile): """Convert a markdown file to a notebook file."""
# Read infile into a string with open(infile, 'r') as fin: str = fin.read() # Enclose the markdown within triple quotes and convert from # python to notebook str = '"""' + str + '"""' nb = py2jn.py_string_to_notebook(str) py2jn.tools.write_notebook(nb, outfile, nbver=4)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def rst_to_docs_rst(infile, outfile): """Convert an rst file to a sphinx docs rst file."""
# Read infile into a list of lines with open(infile, 'r') as fin: rst = fin.readlines() # Inspect outfile path components to determine whether outfile # is in the root of the examples directory or in a subdirectory # thererof ps = pathsplit(outfile)[-3:] if ps[-2] == 'examples': ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def parse_notebook_index(ntbkpth): """ Parse the top-level notebook index file at `ntbkpth`. Returns a list of subdirectories in order of appearance in the index...
# Convert notebook to RST text in string rex = RSTExporter() rsttxt = rex.from_filename(ntbkpth)[0] # Clean up trailing whitespace rsttxt = re.sub(r'\n ', r'', rsttxt, re.M | re.S) pthidx = {} pthlst = [] lines = rsttxt.split('\n') for l in lines: m = re.match(r'^-\s+`([^<...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def construct_notebook_index(title, pthlst, pthidx): """ Construct a string containing a markdown format index for the list of paths in `pthlst`. The title for t...
# Insert title text txt = '"""\n## %s\n"""\n\n"""' % title # Insert entry for each item in pthlst for pth in pthlst: # If pth refers to a .py file, replace .py with .ipynb, otherwise # assume it's a directory name and append '/index.ipynb' if pth[-3:] == '.py': link...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def notebook_executed(pth): """Determine whether the notebook at `pth` has been executed."""
nb = nbformat.read(pth, as_version=4) for n in range(len(nb['cells'])): if nb['cells'][n].cell_type == 'code' and \ nb['cells'][n].execution_count is None: return False return True
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def same_notebook_code(nb1, nb2): """ Return true of the code cells of notebook objects `nb1` and `nb2` are the same. """
# Notebooks do not match of the number of cells differ if len(nb1['cells']) != len(nb2['cells']): return False # Iterate over cells in nb1 for n in range(len(nb1['cells'])): # Notebooks do not match if corresponding cells have different # types if nb1['cells'][n]['cell...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def execute_notebook(npth, dpth, timeout=1200, kernel='python3'): """ Execute the notebook at `npth` using `dpth` as the execution directory. The execution timeo...
ep = ExecutePreprocessor(timeout=timeout, kernel_name=kernel) nb = nbformat.read(npth, as_version=4) t0 = timer() ep.preprocess(nb, {'metadata': {'path': dpth}}) t1 = timer() with open(npth, 'wt') as f: nbformat.write(nb, f) return t1 - t0
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def replace_markdown_cells(src, dst): """ Overwrite markdown cells in notebook object `dst` with corresponding cells in notebook object `src`. """
# It is an error to attempt markdown replacement if src and dst # have different numbers of cells if len(src['cells']) != len(dst['cells']): raise ValueError('notebooks do not have the same number of cells') # Iterate over cells in src for n in range(len(src['cells'])): # It is an...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def notebook_substitute_ref_with_url(ntbk, cr): """ In markdown cells of notebook object `ntbk`, replace sphinx cross-references with links to online docs. Param...
# Iterate over cells in notebook for n in range(len(ntbk['cells'])): # Only process cells of type 'markdown' if ntbk['cells'][n]['cell_type'] == 'markdown': # Get text of markdown cell txt = ntbk['cells'][n]['source'] # Replace links to online docs with sphi...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def preprocess_notebook(ntbk, cr): """ Process notebook object `ntbk` in preparation for conversion to an rst document. This processing replaces links to online ...
# Iterate over cells in notebook for n in range(len(ntbk['cells'])): # Only process cells of type 'markdown' if ntbk['cells'][n]['cell_type'] == 'markdown': # Get text of markdown cell txt = ntbk['cells'][n]['source'] # Replace links to online docs with sphi...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def write_notebook_rst(txt, res, fnm, pth): """ Write the converted notebook text `txt` and resources `res` to filename `fnm` in directory `pth`. """
# Extended filename used for output images extfnm = fnm + '_files' # Directory into which output images are written extpth = os.path.join(pth, extfnm) # Make output image directory if it doesn't exist mkdir(extpth) # Iterate over output images in resources dict for r in res['outputs']....
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def notebook_to_rst(npth, rpth, rdir, cr=None): """ Convert notebook at `npth` to rst document at `rpth`, in directory `rdir`. Parameter `cr` is a CrossReference...
# Read the notebook file ntbk = nbformat.read(npth, nbformat.NO_CONVERT) # Convert notebook object to rstpth notebook_object_to_rst(ntbk, rpth, rdir, cr)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def notebook_object_to_rst(ntbk, rpth, cr=None): """ Convert notebook object `ntbk` to rst document at `rpth`, in directory `rdir`. Parameter `cr` is a CrossRefe...
# Parent directory of file rpth rdir = os.path.dirname(rpth) # File basename rb = os.path.basename(os.path.splitext(rpth)[0]) # Pre-process notebook prior to conversion to rst if cr is not None: preprocess_notebook(ntbk, cr) # Convert notebook to rst rex = RSTExporter() rs...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def make_example_scripts_docs(spth, npth, rpth): """ Generate rst docs from example scripts. Arguments `spth`, `npth`, and `rpth` are the top-level scripts direc...
# Ensure that output directory exists mkdir(rpth) # Iterate over index files for fp in glob(os.path.join(spth, '*.rst')) + \ glob(os.path.join(spth, '*', '*.rst')): # Index basename b = os.path.basename(fp) # Index dirname dn = os.path.dirname(fp) ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_full_name(self, role, name): """ If ``name`` is already the full name of an object, return ``name``. Otherwise, if ``name`` is a partial object name, loo...
# An initial '.' indicates a partial name if name[0] == '.': # Find matches for the partial name in the string # containing all full names for this role ptrn = r'(?<= )[^,]*' + name + r'(?=,)' ml = re.findall(ptrn, self.rolnam[role]) # Handle...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def matching_base_url(self, url): """ Return True if the initial part of `url` matches the base url passed to the initialiser of this object, and False otherwise...
n = len(self.baseurl) return url[0:n] == self.baseurl
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def inventory_maps(inv): """ Construct dicts facilitating information lookup in an inventory dict. A reversed dict allows lookup of a tuple specifying the sphinx...
# Initialise dicts revinv = {} rolnam = {} # Iterate over domain keys in inventory dict for d in inv: # Since keys seem to be duplicated, ignore those not # starting with 'py:' if d[0:3] == 'py:' and d in IntersphinxInventory.domainrole: ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def get_docs_label(self, role, name): """Get an appropriate label to use in a link to the online docs."""
if role == 'cite': # Get the string used as the citation label in the text try: cstr = self.env.bibtex_cache.get_label_from_key(name) except Exception: raise KeyError('cite key %s not found' % name, 'cite', 0) # The link label is ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def substitute_ref_with_url(self, txt): """ In the string `txt`, replace sphinx references with corresponding links to online docs. """
# Find sphinx cross-references mi = re.finditer(r':([^:]+):`([^`]+)`', txt) if mi: # Iterate over match objects in iterator returned by re.finditer for mo in mi: # Initialize link label and url for substitution lbl = None ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def substitute_url_with_ref(self, txt): """ In the string `txt`, replace links to online docs with corresponding sphinx cross-references. """
# Find links mi = re.finditer(r'\[([^\]]+|\[[^\]]+\])\]\(([^\)]+)\)', txt) if mi: # Iterate over match objects in iterator returned by # re.finditer for mo in mi: # Get components of current match: full matching text, # the li...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def obfn_fvarf(self): """Variable to be evaluated in computing data fidelity term, depending on 'fEvalX' option value. """
return self.Xf if self.opt['fEvalX'] else \ sl.rfftn(self.Y, None, self.cri.axisN)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def rsdl(self): """Compute fixed point residual in Fourier domain."""
diff = self.Xf - self.Yfprv return sl.rfl2norm2(diff, self.X.shape, axis=self.cri.axisN)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def cbpdn_class_label_lookup(label): """Get a CBPDN class from a label string."""
clsmod = {'admm': admm_cbpdn.ConvBPDN, 'fista': fista_cbpdn.ConvBPDN} if label in clsmod: return clsmod[label] else: raise ValueError('Unknown ConvBPDN solver method %s' % label)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def ConvBPDNOptionsDefaults(method='admm'): """Get defaults dict for the ConvBPDN class specified by the ``method`` parameter. """
dflt = copy.deepcopy(cbpdn_class_label_lookup(method).Options.defaults) if method == 'admm': dflt.update({'MaxMainIter': 1, 'AutoRho': {'Period': 10, 'AutoScaling': False, 'RsdlRatio': 10.0, 'Scaling': 2.0, 'RsdlTarget': 1.0}}) else:...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def ccmod_class_label_lookup(label): """Get a CCMOD class from a label string."""
clsmod = {'ism': admm_ccmod.ConvCnstrMOD_IterSM, 'cg': admm_ccmod.ConvCnstrMOD_CG, 'cns': admm_ccmod.ConvCnstrMOD_Consensus, 'fista': fista_ccmod.ConvCnstrMOD} if label in clsmod: return clsmod[label] else: raise ValueError('Unknown ConvCnstrMOD so...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def ConvCnstrMODOptionsDefaults(method='fista'): """Get defaults dict for the ConvCnstrMOD class specified by the ``method`` parameter. """
dflt = copy.deepcopy(ccmod_class_label_lookup(method).Options.defaults) if method == 'fista': dflt.update({'MaxMainIter': 1, 'BackTrack': {'gamma_u': 1.2, 'MaxIter': 50}}) else: dflt.update({'MaxMainIter': 1, 'AutoRho': {'Period': 10, 'AutoScaling'...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def evaluate(self): """Evaluate functional value of previous iteration"""
if self.opt['AccurateDFid']: D = self.dstep.var_y() X = self.xstep.var_y() S = self.xstep.S dfd = 0.5*np.linalg.norm((D.dot(X) - S))**2 rl1 = np.sum(np.abs(X)) return dict(DFid=dfd, RegL1=rl1, ObjFun=dfd+self.xstep.lmbda*rl1) else...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def is_newer_than(pth1, pth2): """ Return true if either file pth1 or file pth2 don't exist, or if pth1 has been modified more recently than pth2 """
return not os.path.exists(pth1) or not os.path.exists(pth2) or \ os.stat(pth1).st_mtime > os.stat(pth2).st_mtime
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def mpraw_as_np(shape, dtype): """Construct a numpy array of the specified shape and dtype for which the underlying storage is a multiprocessing RawArray in shar...
sz = int(np.product(shape)) csz = sz * np.dtype(dtype).itemsize raw = mp.RawArray('c', csz) return np.frombuffer(raw, dtype=dtype, count=sz).reshape(shape)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def init_mpraw(mpv, npv): """Set a global variable as a multiprocessing RawArray in shared memory with a numpy array wrapper and initialise its value. Parameters...
globals()[mpv] = mpraw_as_np(npv.shape, npv.dtype) globals()[mpv][:] = npv
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def cbpdn_setdict(): """Set the dictionary for the cbpdn stage. There are no parameters or return values because all inputs and outputs are from and to global va...
global mp_DSf # Set working dictionary for cbpdn step and compute DFT of dictionary # D and of D^T S mp_Df[:] = sl.rfftn(mp_D_Y, mp_cri.Nv, mp_cri.axisN) if mp_cri.Cd == 1: mp_DSf[:] = np.conj(mp_Df) * mp_Sf else: mp_DSf[:] = sl.inner(np.conj(mp_Df[np.newaxis, ...]), mp_Sf, ...
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def cbpdnmd_ustep(k): """Do the U step of the cbpdn stage. The only parameter is the slice index `k` and there are no return values; all inputs and outputs are f...
mp_Z_U0[k] += mp_DX[k] - mp_Z_Y0[k] - mp_S[k] mp_Z_U1[k] += mp_Z_X[k] - mp_Z_Y1[k]
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def ccmodmd_relax(k): """Do relaxation for the ccmod stage. The only parameter is the slice index `k` and there are no return values; all inputs and outputs are ...
mp_D_X[k] = mp_drlx * mp_D_X[k] + (1 - mp_drlx) * mp_D_Y0 mp_DX[k] = mp_drlx * mp_DX[k] + (1 - mp_drlx) * (mp_D_Y1[k] + mp_S[k])
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def eval_grad(self): """Compute gradient in spatial domain for variable Y."""
# Compute D^T(D Y - S) return self.D.T.dot(self.D.dot(self.Y) - self.S)
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def rsdl(self): """Compute fixed point residual."""
return np.linalg.norm((self.X - self.Yprv).ravel())
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def eval_Rf(self, Vf): """Evaluate smooth term in Vf."""
return sl.inner(self.Df, Vf, axis=self.cri.axisM) - self.Sf
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def zpad(v, Nv): """Zero-pad initial axes of array to specified size. Padding is applied to the right, top, etc. of the array indices. Parameters v : array_like ...
vp = np.zeros(Nv + v.shape[len(Nv):], dtype=v.dtype) axnslc = tuple([slice(0, x) for x in v.shape]) vp[axnslc] = v return vp
<SYSTEM_TASK:> Solve the following problem using Python, implementing the functions described below, one line at a time <END_TASK> <USER_TASK:> Description: def Pcn(x, dsz, Nv, dimN=2, dimC=1, crp=False, zm=False): """Constraint set projection for convolutional dictionary update problem. Parameters x : array_like Inp...
if crp: def zpadfn(x): return x else: def zpadfn(x): return zpad(x, Nv) if zm: def zmeanfn(x): return zeromean(x, dsz, dimN) else: def zmeanfn(x): return x return normalise(zmeanfn(zpadfn(bcrop(x, dsz, dimN))), dimN ...