del inputs  <EOL>latent_code = ed.MultivariateNormalDiag(loc=tf.zeros(self.latent_size),<EOL>sample_shape=<NUM_LIT:1>,<EOL>name="<STR_LIT>")<EOL>state = self.lstm.zero_state(<NUM_LIT:1>, dtype=tf.float32)<EOL>t = <NUM_LIT:0><EOL>productions = []<EOL>stack = [self.grammar.start_symbol]<EOL>while stack:<EOL><INDENT>symbol = stack.pop()<EOL>net, state = self.lstm(latent_code, state)<EOL>logits = (self.output_layer(net) +<EOL>self.grammar.mask(symbol, on_value=<NUM_LIT:0.>, off_value=-<NUM_LIT>))<EOL>production = ed.OneHotCategorical(logits=logits,<EOL>name="<STR_LIT>" + str(t))<EOL>_, rhs = self.grammar.production_rules[tf.argmax(<EOL>input=production, axis=-<NUM_LIT:1>)]<EOL>for symbol in rhs:<EOL><INDENT>if symbol in self.grammar.nonterminal_symbols:<EOL><INDENT>stack.append(symbol)<EOL><DEDENT><DEDENT>productions.append(production)<EOL>t += <NUM_LIT:1><EOL><DEDENT>return tf.stack(productions, axis=<NUM_LIT:1>)<EOL>

Runs the model forward to generate a sequence of productions.

        Args:
          inputs: Unused.

        Returns:
          productions: Tensor of shape [1, num_productions, num_production_rules].
            Slices along the `num_productions` dimension represent one-hot vectors.


super(ProbabilisticGrammarVariational, self).__init__()<EOL>self.latent_size = latent_size<EOL>self.encoder_net = tf.keras.Sequential([<EOL>tf.keras.layers.Conv1D(<NUM_LIT:64>, <NUM_LIT:3>, padding="<STR_LIT>"),<EOL>tf.keras.layers.BatchNormalization(),<EOL>tf.keras.layers.Activation(tf.nn.elu),<EOL>tf.keras.layers.Conv1D(<NUM_LIT>, <NUM_LIT:3>, padding="<STR_LIT>"),<EOL>tf.keras.layers.BatchNormalization(),<EOL>tf.keras.layers.Activation(tf.nn.elu),<EOL>tf.keras.layers.Dropout(<NUM_LIT:0.1>),<EOL>tf.keras.layers.GlobalAveragePooling1D(),<EOL>tf.keras.layers.Dense(latent_size * <NUM_LIT:2>, activation=None),<EOL>])<EOL>

Constructs a variational posterior for a probabilistic grammar.

        Args:
          latent_size: Number of dimensions in the latent code.


net = self.encoder_net(tf.cast(inputs, tf.float32))<EOL>return ed.MultivariateNormalDiag(<EOL>loc=net[..., :self.latent_size],<EOL>scale_diag=tf.nn.softplus(net[..., self.latent_size:]),<EOL>name="<STR_LIT>")<EOL>

Runs the model forward to return a stochastic encoding.

        Args:
          inputs: Tensor of shape [1, num_productions, num_production_rules]. It is
            a sequence of productions of length `num_productions`. Each production
            is a one-hot vector of length `num_production_rules`: it determines
            which production rule the production corresponds to.

        Returns:
          latent_code_posterior: A random variable capturing a sample from the
            variational distribution, of shape [1, self.latent_size].


return tf.math.log(tf.math.expm1(x))<EOL>

Helper which computes the function inverse of `tf.nn.softplus`.


def make_encoder(activation, latent_size, base_depth):

conv = functools.partial(<EOL>tf.keras.layers.Conv2D, padding="<STR_LIT>", activation=activation)<EOL>encoder_net = tf.keras.Sequential([<EOL>conv(base_depth, <NUM_LIT:5>, <NUM_LIT:1>),<EOL>conv(base_depth, <NUM_LIT:5>, <NUM_LIT:2>),<EOL>conv(<NUM_LIT:2> * base_depth, <NUM_LIT:5>, <NUM_LIT:1>),<EOL>conv(<NUM_LIT:2> * base_depth, <NUM_LIT:5>, <NUM_LIT:2>),<EOL>conv(<NUM_LIT:4> * latent_size, <NUM_LIT:7>, padding="<STR_LIT>"),<EOL>tf.keras.layers.Flatten(),<EOL>tf.keras.layers.Dense(<NUM_LIT:2> * latent_size, activation=None),<EOL>])<EOL>def encoder(images):<EOL><INDENT>images = <NUM_LIT:2> * tf.cast(images, dtype=tf.float32) - <NUM_LIT:1><EOL>net = encoder_net(images)<EOL>return tfd.MultivariateNormalDiag(<EOL>loc=net[..., :latent_size],<EOL>scale_diag=tf.nn.softplus(net[..., latent_size:] +<EOL>_softplus_inverse(<NUM_LIT:1.0>)),<EOL>name="<STR_LIT:code>")<EOL><DEDENT>return encoder<EOL>

Creates the encoder function.

    Args:
      activation: Activation function in hidden layers.
      latent_size: The dimensionality of the encoding.
      base_depth: The lowest depth for a layer.

    Returns:
      encoder: A `callable` mapping a `Tensor` of images to a
        `tfd.Distribution` instance over encodings.


def make_decoder(activation, latent_size, output_shape, base_depth):

deconv = functools.partial(<EOL>tf.keras.layers.Conv2DTranspose, padding="<STR_LIT>", activation=activation)<EOL>conv = functools.partial(<EOL>tf.keras.layers.Conv2D, padding="<STR_LIT>", activation=activation)<EOL>decoder_net = tf.keras.Sequential([<EOL>deconv(<NUM_LIT:2> * base_depth, <NUM_LIT:7>, padding="<STR_LIT>"),<EOL>deconv(<NUM_LIT:2> * base_depth, <NUM_LIT:5>),<EOL>deconv(<NUM_LIT:2> * base_depth, <NUM_LIT:5>, <NUM_LIT:2>),<EOL>deconv(base_depth, <NUM_LIT:5>),<EOL>deconv(base_depth, <NUM_LIT:5>, <NUM_LIT:2>),<EOL>deconv(base_depth, <NUM_LIT:5>),<EOL>conv(output_shape[-<NUM_LIT:1>], <NUM_LIT:5>, activation=None),<EOL>])<EOL>def decoder(codes):<EOL><INDENT>original_shape = tf.shape(input=codes)<EOL>codes = tf.reshape(codes, (-<NUM_LIT:1>, <NUM_LIT:1>, <NUM_LIT:1>, latent_size))<EOL>logits = decoder_net(codes)<EOL>logits = tf.reshape(<EOL>logits, shape=tf.concat([original_shape[:-<NUM_LIT:1>], output_shape], axis=<NUM_LIT:0>))<EOL>return tfd.Independent(tfd.Bernoulli(logits=logits),<EOL>reinterpreted_batch_ndims=len(output_shape),<EOL>name="<STR_LIT:image>")<EOL><DEDENT>return decoder<EOL>

Creates the decoder function.

    Args:
      activation: Activation function in hidden layers.
      latent_size: Dimensionality of the encoding.
      output_shape: The output image shape.
      base_depth: Smallest depth for a layer.

    Returns:
      decoder: A `callable` mapping a `Tensor` of encodings to a
        `tfd.Distribution` instance over images.


if mixture_components == <NUM_LIT:1>:<EOL><INDENT>return tfd.MultivariateNormalDiag(<EOL>loc=tf.zeros([latent_size]),<EOL>scale_identity_multiplier=<NUM_LIT:1.0>)<EOL><DEDENT>loc = tf.compat.v1.get_variable(<EOL>name="<STR_LIT>", shape=[mixture_components, latent_size])<EOL>raw_scale_diag = tf.compat.v1.get_variable(<EOL>name="<STR_LIT>", shape=[mixture_components, latent_size])<EOL>mixture_logits = tf.compat.v1.get_variable(<EOL>name="<STR_LIT>", shape=[mixture_components])<EOL>return tfd.MixtureSameFamily(<EOL>components_distribution=tfd.MultivariateNormalDiag(<EOL>loc=loc,<EOL>scale_diag=tf.nn.softplus(raw_scale_diag)),<EOL>mixture_distribution=tfd.Categorical(logits=mixture_logits),<EOL>name="<STR_LIT>")<EOL>

Creates the mixture of Gaussians prior distribution.

    Args:
      latent_size: The dimensionality of the latent representation.
      mixture_components: Number of elements of the mixture.

    Returns:
      random_prior: A `tfd.Distribution` instance representing the distribution
        over encodings in the absence of any evidence.


shape = tf.shape(input=images)<EOL>width = shape[-<NUM_LIT:3>]<EOL>height = shape[-<NUM_LIT:2>]<EOL>depth = shape[-<NUM_LIT:1>]<EOL>images = tf.reshape(images, (-<NUM_LIT:1>, width, height, depth))<EOL>batch = tf.shape(input=images)[<NUM_LIT:0>]<EOL>rows = tf.minimum(rows, batch)<EOL>cols = tf.minimum(batch // rows, cols)<EOL>images = images[:rows * cols]<EOL>images = tf.reshape(images, (rows, cols, width, height, depth))<EOL>images = tf.transpose(a=images, perm=[<NUM_LIT:0>, <NUM_LIT:2>, <NUM_LIT:1>, <NUM_LIT:3>, <NUM_LIT:4>])<EOL>images = tf.reshape(images, [<NUM_LIT:1>, rows * width, cols * height, depth])<EOL>return images<EOL>

Helper utility to make a field of images.


def model_fn(features, labels, mode, params, config):

del labels, config<EOL>if params["<STR_LIT>"] and params["<STR_LIT>"] != <NUM_LIT:1>:<EOL><INDENT>raise NotImplementedError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT>encoder = make_encoder(params["<STR_LIT>"],<EOL>params["<STR_LIT>"],<EOL>params["<STR_LIT>"])<EOL>decoder = make_decoder(params["<STR_LIT>"],<EOL>params["<STR_LIT>"],<EOL>IMAGE_SHAPE,<EOL>params["<STR_LIT>"])<EOL>latent_prior = make_mixture_prior(params["<STR_LIT>"],<EOL>params["<STR_LIT>"])<EOL>image_tile_summary(<EOL>"<STR_LIT:input>", tf.cast(features, dtype=tf.float32), rows=<NUM_LIT:1>, cols=<NUM_LIT:16>)<EOL>approx_posterior = encoder(features)<EOL>approx_posterior_sample = approx_posterior.sample(params["<STR_LIT>"])<EOL>decoder_likelihood = decoder(approx_posterior_sample)<EOL>image_tile_summary(<EOL>"<STR_LIT>",<EOL>tf.cast(decoder_likelihood.sample()[:<NUM_LIT:3>, :<NUM_LIT:16>], dtype=tf.float32),<EOL>rows=<NUM_LIT:3>,<EOL>cols=<NUM_LIT:16>)<EOL>image_tile_summary(<EOL>"<STR_LIT>",<EOL>decoder_likelihood.mean()[:<NUM_LIT:3>, :<NUM_LIT:16>],<EOL>rows=<NUM_LIT:3>,<EOL>cols=<NUM_LIT:16>)<EOL>distortion = -decoder_likelihood.log_prob(features)<EOL>avg_distortion = tf.reduce_mean(input_tensor=distortion)<EOL>tf.compat.v1.summary.scalar("<STR_LIT>", avg_distortion)<EOL>if params["<STR_LIT>"]:<EOL><INDENT>rate = tfd.kl_divergence(approx_posterior, latent_prior)<EOL><DEDENT>else:<EOL><INDENT>rate = (approx_posterior.log_prob(approx_posterior_sample)<EOL>- latent_prior.log_prob(approx_posterior_sample))<EOL><DEDENT>avg_rate = tf.reduce_mean(input_tensor=rate)<EOL>tf.compat.v1.summary.scalar("<STR_LIT>", avg_rate)<EOL>elbo_local = -(rate + distortion)<EOL>elbo = tf.reduce_mean(input_tensor=elbo_local)<EOL>loss = -elbo<EOL>tf.compat.v1.summary.scalar("<STR_LIT>", elbo)<EOL>importance_weighted_elbo = tf.reduce_mean(<EOL>input_tensor=tf.reduce_logsumexp(input_tensor=elbo_local, axis=<NUM_LIT:0>) -<EOL>tf.math.log(tf.cast(params["<STR_LIT>"], dtype=tf.float32)))<EOL>tf.compat.v1.summary.scalar("<STR_LIT>",<EOL>importance_weighted_elbo)<EOL>random_image = decoder(latent_prior.sample(<NUM_LIT:16>))<EOL>image_tile_summary(<EOL>"<STR_LIT>",<EOL>tf.cast(random_image.sample(), dtype=tf.float32),<EOL>rows=<NUM_LIT:4>,<EOL>cols=<NUM_LIT:4>)<EOL>image_tile_summary("<STR_LIT>", random_image.mean(), rows=<NUM_LIT:4>, cols=<NUM_LIT:4>)<EOL>global_step = tf.compat.v1.train.get_or_create_global_step()<EOL>learning_rate = tf.compat.v1.train.cosine_decay(<EOL>params["<STR_LIT>"], global_step, params["<STR_LIT>"])<EOL>tf.compat.v1.summary.scalar("<STR_LIT>", learning_rate)<EOL>optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate)<EOL>train_op = optimizer.minimize(loss, global_step=global_step)<EOL>return tf.estimator.EstimatorSpec(<EOL>mode=mode,<EOL>loss=loss,<EOL>train_op=train_op,<EOL>eval_metric_ops={<EOL>"<STR_LIT>":<EOL>tf.compat.v1.metrics.mean(elbo),<EOL>"<STR_LIT>":<EOL>tf.compat.v1.metrics.mean(importance_weighted_elbo),<EOL>"<STR_LIT>":<EOL>tf.compat.v1.metrics.mean(avg_rate),<EOL>"<STR_LIT>":<EOL>tf.compat.v1.metrics.mean(avg_distortion),<EOL>},<EOL>)<EOL>

Builds the model function for use in an estimator.

    Arguments:
      features: The input features for the estimator.
      labels: The labels, unused here.
      mode: Signifies whether it is train or test or predict.
      params: Some hyperparameters as a dictionary.
      config: The RunConfig, unused here.

    Returns:
      EstimatorSpec: A tf.estimator.EstimatorSpec instance.


filepath = os.path.join(directory, filename)<EOL>if tf.io.gfile.exists(filepath):<EOL><INDENT>return filepath<EOL><DEDENT>if not tf.io.gfile.exists(directory):<EOL><INDENT>tf.io.gfile.makedirs(directory)<EOL><DEDENT>url = os.path.join(ROOT_PATH, filename)<EOL>print("<STR_LIT>" % (url, filepath))<EOL>urllib.request.urlretrieve(url, filepath)<EOL>return filepath<EOL>


amat_file = download(directory, FILE_TEMPLATE.format(split=split_name))<EOL>dataset = tf.data.TextLineDataset(amat_file)<EOL>str_to_arr = lambda string: np.array([c == b"<STR_LIT:1>" for c in string.split()])<EOL>def _parser(s):<EOL><INDENT>booltensor = tf.compat.v1.py_func(str_to_arr, [s], tf.bool)<EOL>reshaped = tf.reshape(booltensor, [<NUM_LIT>, <NUM_LIT>, <NUM_LIT:1>])<EOL>return tf.cast(reshaped, dtype=tf.float32), tf.constant(<NUM_LIT:0>, tf.int32)<EOL><DEDENT>return dataset.map(_parser)<EOL>

Returns binary static MNIST tf.data.Dataset.


random_sample = np.random.rand(batch_size, *IMAGE_SHAPE).astype("<STR_LIT>")<EOL>def train_input_fn():<EOL><INDENT>dataset = tf.data.Dataset.from_tensor_slices(<EOL>random_sample).map(lambda row: (row, <NUM_LIT:0>)).batch(batch_size).repeat()<EOL>return tf.compat.v1.data.make_one_shot_iterator(dataset).get_next()<EOL><DEDENT>def eval_input_fn():<EOL><INDENT>dataset = tf.data.Dataset.from_tensor_slices(<EOL>random_sample).map(lambda row: (row, <NUM_LIT:0>)).batch(batch_size)<EOL>return tf.compat.v1.data.make_one_shot_iterator(dataset).get_next()<EOL><DEDENT>return train_input_fn, eval_input_fn<EOL>

Builds fake MNIST-style data for unit testing.


<EOL>def train_input_fn():<EOL><INDENT>dataset = static_mnist_dataset(data_dir, "<STR_LIT:train>")<EOL>dataset = dataset.shuffle(<NUM_LIT>).repeat().batch(batch_size)<EOL>return tf.compat.v1.data.make_one_shot_iterator(dataset).get_next()<EOL><DEDENT>def eval_input_fn():<EOL><INDENT>eval_dataset = static_mnist_dataset(data_dir, "<STR_LIT>")<EOL>eval_dataset = eval_dataset.batch(batch_size)<EOL>return tf.compat.v1.data.make_one_shot_iterator(eval_dataset).get_next()<EOL><DEDENT>return train_input_fn, eval_input_fn<EOL>

Builds an Iterator switching between train and heldout data.


training_dataset = tf.data.Dataset.from_tensor_slices(train_images)<EOL>training_batches = training_dataset.shuffle(<EOL><NUM_LIT>, reshuffle_each_iteration=True).repeat().batch(batch_size)<EOL>training_iterator = tf.compat.v1.data.make_one_shot_iterator(training_batches)<EOL>images = training_iterator.get_next()<EOL>return images<EOL>

Build an iterator over training batches.


<EOL>fake_images = np.random.normal(size=size)<EOL>return fake_images<EOL>


fig = plt.figure(figsize=(<NUM_LIT:4>, <NUM_LIT:4>))<EOL>canvas = backend_agg.FigureCanvasAgg(fig)<EOL>for i, image in enumerate(images):<EOL><INDENT>ax = fig.add_subplot(<NUM_LIT:4>, <NUM_LIT:4>, i + <NUM_LIT:1>)<EOL>plt.axis('<STR_LIT>')<EOL>ax.set_xticklabels([])<EOL>ax.set_yticklabels([])<EOL>ax.imshow(image.reshape(IMAGE_SHAPE[:-<NUM_LIT:1>]), cmap='<STR_LIT>')<EOL><DEDENT>fig.tight_layout()<EOL>plt.subplots_adjust(wspace=<NUM_LIT>, hspace=<NUM_LIT>)<EOL>canvas.print_figure(fname, format='<STR_LIT>')<EOL>

Save a synthetic image as a PNG file.

    Args:
      images: samples of synthetic images generated by the generative network.
      fname: Python `str`, filename to save the plot to.


def plot_weight_posteriors(names, qm_vals, qs_vals, fname):

fig = figure.Figure(figsize=(<NUM_LIT:6>, <NUM_LIT:3>))<EOL>canvas = backend_agg.FigureCanvasAgg(fig)<EOL>ax = fig.add_subplot(<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:1>)<EOL>for n, qm in zip(names, qm_vals):<EOL><INDENT>sns.distplot(qm.flatten(), ax=ax, label=n)<EOL><DEDENT>ax.set_title("<STR_LIT>")<EOL>ax.set_xlim([-<NUM_LIT>, <NUM_LIT>])<EOL>ax.legend()<EOL>ax = fig.add_subplot(<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:2>)<EOL>for n, qs in zip(names, qs_vals):<EOL><INDENT>sns.distplot(qs.flatten(), ax=ax)<EOL><DEDENT>ax.set_title("<STR_LIT>")<EOL>ax.set_xlim([<NUM_LIT:0>, <NUM_LIT:1.>])<EOL>fig.tight_layout()<EOL>canvas.print_figure(fname, format="<STR_LIT>")<EOL>print("<STR_LIT>".format(fname))<EOL>

Save a PNG plot with histograms of weight means and stddevs.

    Args:
      names: A Python `iterable` of `str` variable names.
      qm_vals: A Python `iterable`, the same length as `names`,
        whose elements are Numpy `array`s, of any shape, containing
        posterior means of weight varibles.
      qs_vals: A Python `iterable`, the same length as `names`,
        whose elements are Numpy `array`s, of any shape, containing
        posterior standard deviations of weight varibles.
      fname: Python `str` filename to save the plot to.


def plot_heldout_prediction(input_vals, probs,<EOL>fname, n=<NUM_LIT:10>, title="<STR_LIT>"):

fig = figure.Figure(figsize=(<NUM_LIT:9>, <NUM_LIT:3>*n))<EOL>canvas = backend_agg.FigureCanvasAgg(fig)<EOL>for i in range(n):<EOL><INDENT>ax = fig.add_subplot(n, <NUM_LIT:3>, <NUM_LIT:3>*i + <NUM_LIT:1>)<EOL>ax.imshow(input_vals[i, :].reshape(IMAGE_SHAPE[:-<NUM_LIT:1>]), interpolation="<STR_LIT:None>")<EOL>ax = fig.add_subplot(n, <NUM_LIT:3>, <NUM_LIT:3>*i + <NUM_LIT:2>)<EOL>for prob_sample in probs:<EOL><INDENT>sns.barplot(np.arange(<NUM_LIT:10>), prob_sample[i, :], alpha=<NUM_LIT:0.1>, ax=ax)<EOL>ax.set_ylim([<NUM_LIT:0>, <NUM_LIT:1>])<EOL><DEDENT>ax.set_title("<STR_LIT>")<EOL>ax = fig.add_subplot(n, <NUM_LIT:3>, <NUM_LIT:3>*i + <NUM_LIT:3>)<EOL>sns.barplot(np.arange(<NUM_LIT:10>), np.mean(probs[:, i, :], axis=<NUM_LIT:0>), ax=ax)<EOL>ax.set_ylim([<NUM_LIT:0>, <NUM_LIT:1>])<EOL>ax.set_title("<STR_LIT>")<EOL><DEDENT>fig.suptitle(title)<EOL>fig.tight_layout()<EOL>canvas.print_figure(fname, format="<STR_LIT>")<EOL>print("<STR_LIT>".format(fname))<EOL>

Save a PNG plot visualizing posterior uncertainty on heldout data.

    Args:
      input_vals: A `float`-like Numpy `array` of shape
        `[num_heldout] + IMAGE_SHAPE`, containing heldout input images.
      probs: A `float`-like Numpy array of shape `[num_monte_carlo,
        num_heldout, num_classes]` containing Monte Carlo samples of
        class probabilities for each heldout sample.
      fname: Python `str` filename to save the plot to.
      n: Python `int` number of datapoints to vizualize.
      title: Python `str` title for the plot.


def build_input_pipeline(mnist_data, batch_size, heldout_size):

<EOL>training_dataset = tf.data.Dataset.from_tensor_slices(<EOL>(mnist_data.train.images, np.int32(mnist_data.train.labels)))<EOL>training_batches = training_dataset.shuffle(<EOL><NUM_LIT>, reshuffle_each_iteration=True).repeat().batch(batch_size)<EOL>training_iterator = tf.compat.v1.data.make_one_shot_iterator(training_batches)<EOL>heldout_dataset = tf.data.Dataset.from_tensor_slices(<EOL>(mnist_data.validation.images,<EOL>np.int32(mnist_data.validation.labels)))<EOL>heldout_frozen = (heldout_dataset.take(heldout_size).<EOL>repeat().batch(heldout_size))<EOL>heldout_iterator = tf.compat.v1.data.make_one_shot_iterator(heldout_frozen)<EOL>handle = tf.compat.v1.placeholder(tf.string, shape=[])<EOL>feedable_iterator = tf.compat.v1.data.Iterator.from_string_handle(<EOL>handle, training_batches.output_types, training_batches.output_shapes)<EOL>images, labels = feedable_iterator.get_next()<EOL>return images, labels, handle, training_iterator, heldout_iterator<EOL>

Build an Iterator switching between train and heldout data.


class Dummy(object):<EOL><INDENT>pass<EOL><DEDENT>num_examples = <NUM_LIT:10><EOL>mnist_data = Dummy()<EOL>mnist_data.train = Dummy()<EOL>mnist_data.train.images = np.float32(np.random.randn(<EOL>num_examples, *IMAGE_SHAPE))<EOL>mnist_data.train.labels = np.int32(np.random.permutation(<EOL>np.arange(num_examples)))<EOL>mnist_data.train.num_examples = num_examples<EOL>mnist_data.validation = Dummy()<EOL>mnist_data.validation.images = np.float32(np.random.randn(<EOL>num_examples, *IMAGE_SHAPE))<EOL>mnist_data.validation.labels = np.int32(np.random.permutation(<EOL>np.arange(num_examples)))<EOL>mnist_data.validation.num_examples = num_examples<EOL>return mnist_data<EOL>

Build fake MNIST-style data for unit testing.


def deep_exponential_family(data_size, feature_size, units, shape):

w2 = ed.Gamma(<NUM_LIT:0.1>, <NUM_LIT>, sample_shape=[units[<NUM_LIT:2>], units[<NUM_LIT:1>]], name="<STR_LIT>")<EOL>w1 = ed.Gamma(<NUM_LIT:0.1>, <NUM_LIT>, sample_shape=[units[<NUM_LIT:1>], units[<NUM_LIT:0>]], name="<STR_LIT>")<EOL>w0 = ed.Gamma(<NUM_LIT:0.1>, <NUM_LIT>, sample_shape=[units[<NUM_LIT:0>], feature_size], name="<STR_LIT>")<EOL>z2 = ed.Gamma(<NUM_LIT:0.1>, <NUM_LIT:0.1>, sample_shape=[data_size, units[<NUM_LIT:2>]], name="<STR_LIT>")<EOL>z1 = ed.Gamma(shape, shape / tf.matmul(z2, w2), name="<STR_LIT>")<EOL>z0 = ed.Gamma(shape, shape / tf.matmul(z1, w1), name="<STR_LIT>")<EOL>x = ed.Poisson(tf.matmul(z0, w0), name="<STR_LIT:x>")<EOL>return x<EOL>

A multi-layered topic model over a documents-by-terms matrix.


def trainable_positive_deterministic(shape, min_loc=<NUM_LIT>, name=None):

with tf.compat.v1.variable_scope(<EOL>None, default_name="<STR_LIT>"):<EOL><INDENT>unconstrained_loc = tf.compat.v1.get_variable("<STR_LIT>", shape)<EOL>loc = tf.maximum(tf.nn.softplus(unconstrained_loc), min_loc)<EOL>rv = ed.Deterministic(loc=loc, name=name)<EOL>return rv<EOL><DEDENT>

Learnable Deterministic distribution over positive reals.


def trainable_gamma(shape, min_concentration=<NUM_LIT>, min_scale=<NUM_LIT>, name=None):

with tf.compat.v1.variable_scope(None, default_name="<STR_LIT>"):<EOL><INDENT>unconstrained_concentration = tf.compat.v1.get_variable(<EOL>"<STR_LIT>",<EOL>shape,<EOL>initializer=tf.compat.v1.initializers.random_normal(<EOL>mean=<NUM_LIT:0.5>, stddev=<NUM_LIT:0.1>))<EOL>unconstrained_scale = tf.compat.v1.get_variable(<EOL>"<STR_LIT>",<EOL>shape,<EOL>initializer=tf.compat.v1.initializers.random_normal(stddev=<NUM_LIT:0.1>))<EOL>concentration = tf.maximum(tf.nn.softplus(unconstrained_concentration),<EOL>min_concentration)<EOL>rate = tf.maximum(<NUM_LIT:1.> / tf.nn.softplus(unconstrained_scale), <NUM_LIT:1.> / min_scale)<EOL>rv = ed.Gamma(concentration=concentration, rate=rate, name=name)<EOL>return rv<EOL><DEDENT>

Learnable Gamma via concentration and scale parameterization.


def deep_exponential_family_variational(data_size, feature_size, units):

qw2 = trainable_positive_deterministic([units[<NUM_LIT:2>], units[<NUM_LIT:1>]], name="<STR_LIT>")<EOL>qw1 = trainable_positive_deterministic([units[<NUM_LIT:1>], units[<NUM_LIT:0>]], name="<STR_LIT>")<EOL>qw0 = trainable_positive_deterministic([units[<NUM_LIT:0>], feature_size], name="<STR_LIT>")<EOL>qz2 = trainable_gamma([data_size, units[<NUM_LIT:2>]], name="<STR_LIT>")<EOL>qz1 = trainable_gamma([data_size, units[<NUM_LIT:1>]], name="<STR_LIT>")<EOL>qz0 = trainable_gamma([data_size, units[<NUM_LIT:0>]], name="<STR_LIT>")<EOL>return qw2, qw1, qw0, qz2, qz1, qz0<EOL>

Posterior approx. for deep exponential family p(w{0,1,2}, z{1,2,3} | x).


def set_values(f, *args, **kwargs):<EOL><INDENT>"""<STR_LIT>"""<EOL>name = kwargs.get("<STR_LIT:name>")<EOL>if name in model_kwargs:<EOL><INDENT>kwargs["<STR_LIT:value>"] = model_kwargs[name]<EOL><DEDENT>return ed.interceptable(f)(*args, **kwargs)<EOL><DEDENT>return set_values<EOL>

Creates a value-setting interceptor.

    Args:
      **model_kwargs: dict of str to Tensor. Keys are the names of random variable
        in the model to which this interceptor is being applied. Values are
        Tensors to set their value to.

    Returns:
      set_values: Function which sets the value of intercepted ops.


path = os.path.expanduser(path)<EOL>filename = "<STR_LIT>"<EOL>filepath = os.path.join(path, filename)<EOL>if not os.path.exists(filepath):<EOL><INDENT>url = ("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>if not tf.io.gfile.exists(path):<EOL><INDENT>tf.io.gfile.makedirs(path)<EOL><DEDENT>print("<STR_LIT>" % (url, filepath))<EOL>urllib.request.urlretrieve(url, filepath)<EOL><DEDENT>with open(filepath) as f:<EOL><INDENT>iterator = csv.reader(f)<EOL>documents = next(iterator)[<NUM_LIT:1>:]<EOL>words = []<EOL>x_train = []<EOL>for row in iterator:<EOL><INDENT>words.append(row[<NUM_LIT:0>])<EOL>x_train.append(row[<NUM_LIT:1>:])<EOL><DEDENT><DEDENT>x_train = np.array(x_train, dtype=np.int)<EOL>doc_idx = [i for i, document in enumerate(documents)<EOL>if document.startswith("<STR_LIT>")]<EOL>documents = [documents[doc] for doc in doc_idx]<EOL>x_train = x_train[:, doc_idx]<EOL>word_idx = np.logical_and(np.sum(x_train != <NUM_LIT:0>, <NUM_LIT:1>) >= <NUM_LIT:2>,<EOL>np.sum(x_train, <NUM_LIT:1>) >= <NUM_LIT:10>)<EOL>words = [word for word, idx in zip(words, word_idx) if idx]<EOL>bag_of_words = x_train[word_idx, :].T<EOL>return bag_of_words, words<EOL>

Loads NIPS 2011 conference papers.

    The NIPS 1987-2015 data set is in the form of a 11,463 x 5,812 matrix of
    per-paper word counts, containing 11,463 words and 5,811 NIPS conference
    papers (Perrone et al., 2016). We subset to papers in 2011 and words appearing
    in at least two documents and having a total word count of at least 10.

    Built from the Observations Python package.

    Args:
      path: str.
        Path to directory which either stores file or otherwise file will
        be downloaded and extracted there. Filename is `NIPS_1987-2015.csv`.

    Returns:
      bag_of_words: np.ndarray of shape [num_documents, num_words]. Each element
        denotes the number of occurrences of a specific word in a specific
        document.
      words: List of strings, denoting the words for `bag_of_words`'s columns.


try:<EOL><INDENT>import tensorflow as tf<EOL><DEDENT>except ImportError:<EOL><INDENT>print("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL>raise<EOL><DEDENT>import distutils.version<EOL>required_tensorflow_version = "<STR_LIT>"<EOL>if (distutils.version.LooseVersion(tf.__version__) <<EOL>distutils.version.LooseVersion(required_tensorflow_version)):<EOL><INDENT>raise ImportError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(<EOL>required=required_tensorflow_version,<EOL>present=tf.__version__))<EOL><DEDENT>

Attempt to import tensorflow, and ensure its version is sufficient.

    Raises:
      ImportError: if either tensorflow is not importable or its version is
      inadequate.


R = <NUM_LIT>  <EOL>lat1, lon1 = math.radians(a[<NUM_LIT:0>]), math.radians(a[<NUM_LIT:1>])<EOL>lat2, lon2 = math.radians(b[<NUM_LIT:0>]), math.radians(b[<NUM_LIT:1>])<EOL>return math.acos(math.sin(lat1) * math.sin(lat2) +<EOL>math.cos(lat1) * math.cos(lat2) * math.cos(lon1 - lon2)) * R<EOL>

Calculates distance between two latitude-longitude coordinates.


for i in range(<NUM_LIT:0>, len(l), n):<EOL><INDENT>yield l[i:i+n]<EOL><DEDENT>


state = self.state if state is None else state<EOL>route = state<EOL>a = random.randint(self.locked_range, len(route) - <NUM_LIT:1>)<EOL>b = random.randint(self.locked_range, len(route) - <NUM_LIT:1>)<EOL>route[a], route[b] = route[b], route[a]<EOL>

Swaps two cities in the route.

        :type state: TSPState


state = self.state if state is None else state<EOL>route = state<EOL>e = <NUM_LIT:0><EOL>if self.distance_matrix:<EOL><INDENT>for i in range(len(route)):<EOL><INDENT>e += self.distance_matrix["<STR_LIT>".format(route[i-<NUM_LIT:1>], route[i])]<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for i in range(len(route)):<EOL><INDENT>e += distance(self.cities[route[i-<NUM_LIT:1>]], self.cities[route[i]])<EOL><DEDENT><DEDENT>return e<EOL>


@classmethod<EOL><INDENT>def divide(cls, divisions, problem_data):<DEDENT>

tspp = TSPProblem(**problem_data)<EOL>def routes_for_subgroup(cs):<EOL><INDENT>for city in cs:<EOL><INDENT>if city == tspp.start_city:<EOL><INDENT>continue<EOL><DEDENT>cities = tspp.cities.keys()<EOL>cities.remove(tspp.start_city)<EOL>cities.remove(city)<EOL>random.shuffle(cities)<EOL>route = [tspp.start_city, city] + cities<EOL>assert len(set(route)) == len(route)<EOL>assert len(route) == len(tspp.cities)<EOL>yield json.dumps(route)<EOL><DEDENT><DEDENT>if divisions:<EOL><INDENT>chunk_size = int(math.ceil(len(tspp.cities) / divisions))<EOL><DEDENT>else:<EOL><INDENT>chunk_size = <NUM_LIT:1><EOL><DEDENT>for subgroup in chunks(tspp.cities.keys(), chunk_size):<EOL><INDENT>routes = list(routes_for_subgroup(subgroup))<EOL>if routes:<EOL><INDENT>yield routes<EOL><DEDENT><DEDENT>

divide

        :type problem_data: dict


raise NotImplementedError<EOL>

Divide the Problem defined by the problem_data into the
        given number of divisions

        Divide should be deterministic in the sense that it returns the
        same divisions in the same order on multiple calls. This is done
        because the scheduler does not store division values, but enumeration
        of the divisions as indices.

        :param int divisions: If divisions given is 0, use a default
            number of divisions determined programatically
        :type problem_data: dict


here = os.path.abspath(os.path.dirname(__file__))<EOL>return codecs.open(os.path.join(here, filename), '<STR_LIT:r>').read()<EOL>

Read and return `filename` in root dir of project and return string


url = COURSES_API.format(course_id) + "<STR_LIT>"<EOL>standards = []<EOL>for data in self._get_resource(url):<EOL><INDENT>standards.append(GradingStandard(data=data))<EOL><DEDENT>return standards<EOL>

List the grading standards available to a course
https://canvas.instructure.com/doc/api/grading_standards.html#method.grading_standards_api.context_index


def create_grading_standard_for_course(self, course_id, name,<EOL>grading_scheme, creator):

url = COURSES_API.format(course_id) + "<STR_LIT>"<EOL>body = {<EOL>"<STR_LIT:title>": name,<EOL>"<STR_LIT>": grading_scheme,<EOL>"<STR_LIT>": creator<EOL>}<EOL>return GradingStandard(data=self._post_resource(url, body))<EOL>

Create a new grading standard for the passed course.

https://canvas.instructure.com/doc/api/grading_standards.html#method.grading_standards_api.create


def get_section(self, section_id, params={}):

url = SECTIONS_API.format(section_id)<EOL>return CanvasSection(data=self._get_resource(url, params=params))<EOL>

Return section resource for given canvas section id.

https://canvas.instructure.com/doc/api/sections.html#method.sections.show


def get_section_by_sis_id(self, sis_section_id, params={}):

return self.get_section(<EOL>self._sis_id(sis_section_id, sis_field="<STR_LIT>"), params)<EOL>

Return section resource for given sis id.


def get_sections_in_course(self, course_id, params={}):

url = COURSES_API.format(course_id) + "<STR_LIT>"<EOL>sections = []<EOL>for data in self._get_paged_resource(url, params=params):<EOL><INDENT>sections.append(CanvasSection(data=data))<EOL><DEDENT>return sections<EOL>

Return list of sections for the passed course ID.

https://canvas.instructure.com/doc/api/sections.html#method.sections.index


def get_sections_in_course_by_sis_id(self, sis_course_id, params={}):

return self.get_sections_in_course(<EOL>self._sis_id(sis_course_id, sis_field="<STR_LIT>"), params)<EOL>

Return list of sections for the passed course SIS ID.


def get_sections_with_students_in_course(self, course_id, params={}):

include = params.get("<STR_LIT>", [])<EOL>if "<STR_LIT>" not in include:<EOL><INDENT>include.append("<STR_LIT>")<EOL><DEDENT>params["<STR_LIT>"] = include<EOL>return self.get_sections_in_course(course_id, params)<EOL>

Return list of sections including students for the passed course ID.


return self.get_sections_with_students_in_course(<EOL>self._sis_id(sis_course_id, sis_field="<STR_LIT>"), params)<EOL>

Return list of sections including students for the passed sis ID.


def create_section(self, course_id, name, sis_section_id):

url = COURSES_API.format(course_id) + "<STR_LIT>"<EOL>body = {"<STR_LIT>": {"<STR_LIT:name>": name,<EOL>"<STR_LIT>": sis_section_id}}<EOL>return CanvasSection(data=self._post_resource(url, body))<EOL>

Create a canvas section in the given course id.

https://canvas.instructure.com/doc/api/sections.html#method.sections.create


def update_section(self, section_id, name, sis_section_id):

url = SECTIONS_API.format(section_id)<EOL>body = {"<STR_LIT>": {}}<EOL>if name:<EOL><INDENT>body["<STR_LIT>"]["<STR_LIT:name>"] = name<EOL><DEDENT>if sis_section_id:<EOL><INDENT>body["<STR_LIT>"]["<STR_LIT>"] = sis_section_id<EOL><DEDENT>return CanvasSection(data=self._put_resource(url, body))<EOL>

Update a canvas section with the given section id.

https://canvas.instructure.com/doc/api/sections.html#method.sections.update


url = ACCOUNTS_API.format(account_id) + "<STR_LIT>"<EOL>report_types = []<EOL>for datum in self._get_resource(url):<EOL><INDENT>report_types.append(ReportType(data=datum, account_id=account_id))<EOL><DEDENT>return report_types<EOL>

Returns the list of reports for the canvas account id.

https://canvas.instructure.com/doc/api/account_reports.html#method.account_reports.available_reports


def get_reports_by_type(self, account_id, report_type):

url = ACCOUNTS_API.format(account_id) + "<STR_LIT>".format(<EOL>report_type)<EOL>reports = []<EOL>for datum in self._get_resource(url):<EOL><INDENT>datum["<STR_LIT>"] = account_id<EOL>reports.append(Report(data=datum))<EOL><DEDENT>return reports<EOL>

Shows all reports of the passed report_type that have been run
for the canvas account id.

https://canvas.instructure.com/doc/api/account_reports.html#method.account_reports.index


def create_report(self, report_type, account_id, term_id=None, params={}):

if term_id is not None:<EOL><INDENT>params["<STR_LIT>"] = term_id<EOL><DEDENT>url = ACCOUNTS_API.format(account_id) + "<STR_LIT>".format(<EOL>report_type)<EOL>body = {"<STR_LIT>": params}<EOL>data = self._post_resource(url, body)<EOL>data["<STR_LIT>"] = account_id<EOL>return Report(data=data)<EOL>

Generates a report instance for the canvas account id.

https://canvas.instructure.com/doc/api/account_reports.html#method.account_reports.create


def create_course_provisioning_report(self, account_id, term_id=None,<EOL>params={}):

params["<STR_LIT>"] = True<EOL>return self.create_report(ReportType.PROVISIONING, account_id, term_id,<EOL>params)<EOL>

Convenience method for create_report, for creating a course
provisioning report.


def create_enrollments_provisioning_report(self, account_id, term_id=None,<EOL>params={}):

Convenience method for create_report, for creating an enrollment
provisioning report.


def create_user_provisioning_report(self, account_id, term_id=None,<EOL>params={}):

Convenience method for create_report, for creating a user
provisioning report.


def create_xlist_provisioning_report(self, account_id, term_id=None,<EOL>params={}):

Convenience method for create_report, for creating a crosslist
provisioning report.


def create_course_sis_export_report(self, account_id, term_id=None,<EOL>params={}):

params["<STR_LIT>"] = True<EOL>return self.create_report(ReportType.SIS_EXPORT, account_id, term_id,<EOL>params)<EOL>

Convenience method for create_report, for creating a course sis export
report.


def create_unused_courses_report(self, account_id, term_id=None):

return self.create_report(ReportType.UNUSED_COURSES, account_id,<EOL>term_id)<EOL>

Convenience method for create_report, for creating an unused courses
report.


if report.report_id is None or report.status is None:<EOL><INDENT>raise ReportFailureException(report)<EOL><DEDENT>interval = getattr(settings, '<STR_LIT>', <NUM_LIT:5>)<EOL>while report.status != "<STR_LIT>":<EOL><INDENT>if report.status == "<STR_LIT:error>":<EOL><INDENT>raise ReportFailureException(report)<EOL><DEDENT>sleep(interval)<EOL>report = self.get_report_status(report)<EOL><DEDENT>if report.attachment is None or report.attachment.url is None:<EOL><INDENT>return<EOL><DEDENT>data = self._get_report_file(report.attachment.url)<EOL>return data.split("<STR_LIT:\n>")<EOL>

Returns a completed report as a list of csv strings.


if (report.account_id is None or report.type is None or<EOL>report.report_id is None):<EOL><INDENT>raise ReportFailureException(report)<EOL><DEDENT>url = ACCOUNTS_API.format(report.account_id) + "<STR_LIT>".format(<EOL>report.type, report.report_id)<EOL>data = self._get_resource(url)<EOL>data["<STR_LIT>"] = report.account_id<EOL>return Report(data=data)<EOL>

Returns the status of a report.

https://canvas.instructure.com/doc/api/account_reports.html#method.account_reports.show


url = ACCOUNTS_API.format(report.account_id) + "<STR_LIT>".format(<EOL>report.type, report.report_id)<EOL>response = self._delete_resource(url)<EOL>return True<EOL>

Deletes a generated report instance.

https://canvas.instructure.com/doc/api/account_reports.html#method.account_reports.destroy


def get_activity_by_account(self, account_id, term_id):

url = ("<STR_LIT>"<EOL>"<STR_LIT>") % (account_id, term_id)<EOL>return self._get_resource(url)<EOL>

Returns participation data for the given account_id and term_id.

https://canvas.instructure.com/doc/api/analytics.html#method.analytics_api.department_participation


def get_grades_by_account(self, account_id, term_id):

Returns grade data for the given account_id and term_id.

https://canvas.instructure.com/doc/api/analytics.html#method.analytics_api.department_grades


def get_statistics_by_account(self, account_id, term_id):

Returns statistics for the given account_id and term_id.

https://canvas.instructure.com/doc/api/analytics.html#method.analytics_api.department_statistics


url = "<STR_LIT>" % (<EOL>self._sis_id(sis_course_id, sis_field="<STR_LIT>"))<EOL>return self._get_resource(url)<EOL>

Returns participation data for the given sis_course_id.

https://canvas.instructure.com/doc/api/analytics.html#method.analytics_api.course_participation


Returns assignment data for the given course_id.

https://canvas.instructure.com/doc/api/analytics.html#method.analytics_api.course_assignments


Returns per-student data for the given course_id.

https://canvas.instructure.com/doc/api/analytics.html#method.analytics_api.course_student_summaries


def get_student_activity_for_sis_course_id_and_sis_user_id(<EOL>self, sis_user_id, sis_course_id):

url = ("<STR_LIT>"<EOL>"<STR_LIT>") % (<EOL>self._sis_id(sis_course_id, sis_field="<STR_LIT>"), sis_user_id)<EOL>return self._get_resource(url)<EOL>

Returns student activity data for the given user_id and course_id.

https://canvas.instructure.com/doc/api/analytics.html#method.analytics_api.student_in_course_participation


def get_student_assignments_for_sis_course_id_and_sis_user_id(<EOL>self, sis_user_id, sis_course_id):

Returns student assignment data for the given user_id and course_id.

https://canvas.instructure.com/doc/api/analytics.html#method.analytics_api.student_in_course_assignments


def get_student_assignments_for_sis_course_id_and_canvas_user_id(<EOL>self, sis_course_id, user_id):

url = "<STR_LIT>" % (<EOL>self._sis_id(sis_course_id, sis_field="<STR_LIT>"), user_id)<EOL>return self._get_resource(url)<EOL>


def get_student_messaging_for_sis_course_id_and_sis_user_id(<EOL>self, sis_user_id, sis_course_id):

Returns student messaging data for the given user_id and course_id.

https://canvas.instructure.com/doc/api/analytics.html#method.analytics_api.student_in_course_messaging


def get_roles_in_account(self, account_id, params={}):

url = ACCOUNTS_API.format(account_id) + "<STR_LIT>"<EOL>roles = []<EOL>for datum in self._get_resource(url, params=params):<EOL><INDENT>roles.append(CanvasRole(data=datum))<EOL><DEDENT>return roles<EOL>

List the roles for an account, for the passed Canvas account ID.

https://canvas.instructure.com/doc/api/roles.html#method.role_overrides.api_index


def get_roles_by_account_sis_id(self, account_sis_id, params={}):

return self.get_roles_in_account(self._sis_id(account_sis_id,<EOL>sis_field="<STR_LIT>"),<EOL>params)<EOL>

List the roles for an account, for the passed account SIS ID.


course_roles = []<EOL>params = {"<STR_LIT>": "<STR_LIT:1>"}<EOL>for role in self.get_roles_in_account(account_id, params):<EOL><INDENT>if role.base_role_type != "<STR_LIT>":<EOL><INDENT>course_roles.append(role)<EOL><DEDENT><DEDENT>return course_roles<EOL>

List all course roles available to an account, for the passed Canvas
account ID, including course roles inherited from parent accounts.


def get_role(self, account_id, role_id):

url = ACCOUNTS_API.format(account_id) + "<STR_LIT>".format(role_id)<EOL>return CanvasRole(data=self._get_resource(url))<EOL>

Get information about a single role, for the passed Canvas account ID.

https://canvas.instructure.com/doc/api/roles.html#method.role_overrides.show


def get_role_by_account_sis_id(self, account_sis_id, role_id):

return self.get_role(self._sis_id(account_sis_id, sis_field="<STR_LIT>"),<EOL>role_id)<EOL>

Get information about a single role, for the passed account SIS ID.


url = ACCOUNTS_API.format(account_id)<EOL>return CanvasAccount(data=self._get_resource(url))<EOL>

Return account resource for given canvas account id.

https://canvas.instructure.com/doc/api/accounts.html#method.accounts.show


return self.get_account(self._sis_id(sis_id))<EOL>

Return account resource for given sis id.


def get_sub_accounts(self, account_id, params={}):

url = ACCOUNTS_API.format(account_id) + "<STR_LIT>"<EOL>accounts = []<EOL>for datum in self._get_paged_resource(url, params=params):<EOL><INDENT>accounts.append(CanvasAccount(data=datum))<EOL><DEDENT>return accounts<EOL>

Return list of subaccounts within the account with the passed
canvas id.

https://canvas.instructure.com/doc/api/accounts.html#method.accounts.sub_accounts


return self.get_sub_accounts(self._sis_id(sis_id), params={})<EOL>

Return list of subaccounts within the account with the passed sis id.


return self.get_sub_accounts(account_id,<EOL>params={"<STR_LIT>": "<STR_LIT:true>"})<EOL>

Return a recursive list of subaccounts within the account with
the passed canvas id.


return self.get_sub_accounts(self._sis_id(sis_id),<EOL>params={"<STR_LIT>": "<STR_LIT:true>"})<EOL>

Return a recursive list of subaccounts within the account with
the passed sis id.


url = ACCOUNTS_API.format(account.account_id)<EOL>body = {"<STR_LIT>": {"<STR_LIT:name>": account.name}}<EOL>return CanvasAccount(data=self._put_resource(url, body))<EOL>

Update the passed account. Returns the updated account.

https://canvas.instructure.com/doc/api/accounts.html#method.accounts.update


def update_sis_id(self, account_id, sis_account_id):

if account_id == self._canvas_account_id:<EOL><INDENT>raise Exception("<STR_LIT>")<EOL><DEDENT>url = ACCOUNTS_API.format(account_id)<EOL>body = {"<STR_LIT>": {"<STR_LIT>": sis_account_id}}<EOL>return CanvasAccount(data=self._put_resource(url, body))<EOL>

Updates the SIS ID for the account identified by the passed account ID.

https://canvas.instructure.com/doc/api/accounts.html#method.accounts.update


url = ACCOUNTS_API.format(account_id) + "<STR_LIT>"<EOL>return CanvasSSOSettings(data=self._get_resource(url))<EOL>

Return the authentication settings for the passed account_id.

https://canvas.instructure.com/doc/api/authentication_providers.html#method.account_authorization_configs.show_sso_settings


def update_auth_settings(self, account_id, auth_settings):

url = ACCOUNTS_API.format(account_id) + "<STR_LIT>"<EOL>data = self._put_resource(url, auth_settings.json_data())<EOL>return CanvasSSOSettings(data=data)<EOL>

Update the authentication settings for the passed account_id.

https://canvas.instructure.com/doc/api/authentication_providers.html#method.account_authorization_configs.update_sso_settings


if not self._canvas_account_id:<EOL><INDENT>raise MissingAccountID()<EOL><DEDENT>params["<STR_LIT>"] = SISImportModel.CSV_IMPORT_TYPE<EOL>url = SIS_IMPORTS_API.format(<EOL>self._canvas_account_id) + "<STR_LIT>".format(self._params(params))<EOL>headers = {"<STR_LIT:Content-Type>": "<STR_LIT>"}<EOL>return SISImportModel(data=self._post_resource(url, headers, csv))<EOL>

Imports a CSV string.

https://canvas.instructure.com/doc/api/sis_imports.html#method.sis_imports_api.create


def import_dir(self, dir_path, params={}):

if not self._canvas_account_id:<EOL><INDENT>raise MissingAccountID()<EOL><DEDENT>body = self._build_archive(dir_path)<EOL>params["<STR_LIT>"] = SISImportModel.CSV_IMPORT_TYPE<EOL>url = SIS_IMPORTS_API.format(<EOL>self._canvas_account_id) + "<STR_LIT>".format(self._params(params))<EOL>headers = {"<STR_LIT:Content-Type>": "<STR_LIT>"}<EOL>return SISImportModel(data=self._post_resource(url, headers, body))<EOL>

Imports a directory of CSV files.

https://canvas.instructure.com/doc/api/sis_imports.html#method.sis_imports_api.create


if not self._canvas_account_id:<EOL><INDENT>raise MissingAccountID()<EOL><DEDENT>url = SIS_IMPORTS_API.format(<EOL>self._canvas_account_id) + "<STR_LIT>".format(sis_import.import_id)<EOL>return SISImportModel(data=self._get_resource(url))<EOL>

Get the status of an already created SIS import.

https://canvas.instructure.com/doc/api/sis_imports.html#method.sis_imports_api.show


zip_path = os.path.join(dir_path, "<STR_LIT>")<EOL>archive = zipfile.ZipFile(zip_path, "<STR_LIT:w>")<EOL>for filename in CSV_FILES:<EOL><INDENT>filepath = os.path.join(dir_path, filename)<EOL>if os.path.exists(filepath):<EOL><INDENT>archive.write(filepath, filename, zipfile.ZIP_DEFLATED)<EOL><DEDENT><DEDENT>archive.close()<EOL>with open(zip_path, "<STR_LIT:rb>") as f:<EOL><INDENT>body = f.read()<EOL><DEDENT>return body<EOL>

Creates a zip archive from files in path.


url = ASSIGNMENTS_API.format(course_id)<EOL>data = self._get_resource(url)<EOL>assignments = []<EOL>for datum in data:<EOL><INDENT>assignments.append(Assignment(data=datum))<EOL><DEDENT>return assignments<EOL>

List assignments for a given course

https://canvas.instructure.com/doc/api/assignments.html#method.assignments_api.index


return self.get_assignments(self._sis_id(sis_id, "<STR_LIT>"))<EOL>

List assignments for a given course by sid_id

https://canvas.instructure.com/doc/api/assignments.html#method.assignments_api.index


url = ASSIGNMENTS_API.format(assignment.course_id) + "<STR_LIT>".format(<EOL>assignment.assignment_id)<EOL>data = self._put_resource(url, assignment.json_data())<EOL>return Assignment(data=data)<EOL>

Modify an existing assignment.

https://canvas.instructure.com/doc/api/assignments.html#method.assignments_api.update


def get_external_tools_in_account(self, account_id, params={}):

url = ACCOUNTS_API.format(account_id) + "<STR_LIT>"<EOL>external_tools = []<EOL>for data in self._get_paged_resource(url, params=params):<EOL><INDENT>external_tools.append(data)<EOL><DEDENT>return external_tools<EOL>

Return external tools for the passed canvas account id.

https://canvas.instructure.com/doc/api/external_tools.html#method.external_tools.index


return self.get_external_tools_in_account(self._sis_id(sis_id,<EOL>"<STR_LIT>"))<EOL>

Return external tools for given account sis id.


def get_external_tools_in_course(self, course_id, params={}):

url = COURSES_API.format(course_id) + "<STR_LIT>"<EOL>external_tools = []<EOL>for data in self._get_paged_resource(url, params=params):<EOL><INDENT>external_tools.append(data)<EOL><DEDENT>return external_tools<EOL>

Return external tools for the passed canvas course id.

https://canvas.instructure.com/doc/api/external_tools.html#method.external_tools.index


return self.get_external_tools_in_course(self._sis_id(sis_id,<EOL>"<STR_LIT>"))<EOL>

Return external tools for given course sis id.


def _create_external_tool(self, context, context_id, json_data):

url = context.format(context_id) + "<STR_LIT>"<EOL>return self._post_resource(url, body=json_data)<EOL>

Create an external tool using the passed json_data.

context is either COURSES_API or ACCOUNTS_API.
context_id is the Canvas course_id or account_id, depending on context.

https://canvas.instructure.com/doc/api/external_tools.html#method.external_tools.create


def _update_external_tool(self, context, context_id, external_tool_id,<EOL>json_data):

url = context.format(context_id) + "<STR_LIT>".format(<EOL>external_tool_id)<EOL>return self._put_resource(url, body=json_data)<EOL>

Update the external tool identified by external_tool_id with the passed
json data.

context is either COURSES_API or ACCOUNTS_API.
context_id is the course_id or account_id, depending on context

https://canvas.instructure.com/doc/api/external_tools.html#method.external_tools.update


def _delete_external_tool(self, context, context_id, external_tool_id):

url = context.format(context_id) + "<STR_LIT>".format(<EOL>external_tool_id)<EOL>response = self._delete_resource(url)<EOL>return True<EOL>

Delete the external tool identified by external_tool_id.

context is either COURSES_API or ACCOUNTS_API.
context_id is the course_id or account_id, depending on context

https://canvas.instructure.com/doc/api/external_tools.html#method.external_tools.destroy


def _get_sessionless_launch_url(self, context, context_id, tool_id):

url = context.format(context_id) + "<STR_LIT>"<EOL>params = {"<STR_LIT:id>": tool_id}<EOL>return self._get_resource(url, params)<EOL>

Get a sessionless launch url for an external tool.

https://canvas.instructure.com/doc/api/external_tools.html#method.external_tools.generate_sessionless_launch


def get_sessionless_launch_url_from_account(self, tool_id, account_id):

return self._get_sessionless_launch_url(<EOL>ACCOUNTS_API, account_id, tool_id)<EOL>


def get_sessionless_launch_url_from_account_sis_id(<EOL>self, tool_id, account_sis_id):

return self.get_sessionless_launch_url_from_account(<EOL>tool_id, self._sis_id(account_sis_id, "<STR_LIT>"))<EOL>


def get_sessionless_launch_url_from_course(self, tool_id, course_id):

return self._get_sessionless_launch_url(<EOL>COURSES_API, course_id, tool_id)<EOL>


def get_sessionless_launch_url_from_course_sis_id(<EOL>self, tool_id, course_sis_id):

return self.get_sessionless_launch_url_from_course(<EOL>tool_id, self._sis_id(course_sis_id, "<STR_LIT>"))<EOL>


if not self._canvas_account_id:<EOL><INDENT>raise MissingAccountID()<EOL><DEDENT>params = {"<STR_LIT>": '<STR_LIT:all>', '<STR_LIT>': <NUM_LIT>}<EOL>url = ACCOUNTS_API.format(self._canvas_account_id) + "<STR_LIT>"<EOL>data_key = '<STR_LIT>'<EOL>terms = []<EOL>response = self._get_paged_resource(url, params, data_key)<EOL>for data in response[data_key]:<EOL><INDENT>terms.append(CanvasTerm(data=data))<EOL><DEDENT>return terms<EOL>

Return all of the terms in the account.
https://canvas.instructure.com/doc/api/enrollment_terms.html#method.terms_api.index


for term in self.get_all_terms():<EOL><INDENT>if term.sis_term_id == sis_term_id:<EOL><INDENT>return term<EOL><DEDENT><DEDENT>

Return a term resource for the passed SIS ID.


def update_term_overrides(self, sis_term_id, overrides={}):

if not self._canvas_account_id:<EOL><INDENT>raise MissingAccountID()<EOL><DEDENT>url = ACCOUNTS_API.format(<EOL>self._canvas_account_id) + "<STR_LIT>".format(<EOL>self._sis_id(sis_term_id, sis_field='<STR_LIT>'))<EOL>body = {'<STR_LIT>': {'<STR_LIT>': overrides}}<EOL>return CanvasTerm(data=self._put_resource(url, body))<EOL>

Update an existing enrollment term for the passed SIS ID.
https://canvas.instructure.com/doc/api/enrollment_terms.html#method.terms.update


return self.get_quizzes(self._sis_id(sis_id, "<STR_LIT>"))<EOL>

List quizzes for a given course sis id

https://canvas.instructure.com/doc/api/quizzes.html#method.quizzes_api.index


url = QUIZZES_API.format(course_id)<EOL>data = self._get_resource(url)<EOL>quizzes = []<EOL>for datum in data:<EOL><INDENT>quizzes.append(Quiz(data=datum))<EOL><DEDENT>return quizzes<EOL>

List quizzes for a given course

https://canvas.instructure.com/doc/api/quizzes.html#method.quizzes_api.index


def __init__(self, per_page=DEFAULT_PAGINATION, as_user=MASQUERADING_USER):

self._per_page = per_page<EOL>self._as_user = as_user<EOL>self._re_canvas_id = re.compile(r'<STR_LIT>')<EOL>self._canvas_account_id = getattr(<EOL>settings, '<STR_LIT>', None)<EOL>


def _sis_id(self, sis_id, sis_field='<STR_LIT>'):

return quote('<STR_LIT>'.format(sis_field, sis_id))<EOL>


for link in response.getheader("<STR_LIT>", "<STR_LIT>").split("<STR_LIT:U+002C>"):<EOL><INDENT>try:<EOL><INDENT>(url, rel) = link.split("<STR_LIT:;>")<EOL>if "<STR_LIT>" in rel:<EOL><INDENT>return url.lstrip("<STR_LIT:<>").rstrip("<STR_LIT:>>")<EOL><DEDENT><DEDENT>except Exception:<EOL><INDENT>return<EOL><DEDENT><DEDENT>

return url path to next page of paginated data


def _get_resource_url(self, url, auto_page, data_key):

headers = {'<STR_LIT>': '<STR_LIT:application/json>',<EOL>'<STR_LIT>': '<STR_LIT>'}<EOL>response = DAO.getURL(url, headers)<EOL>if response.status != <NUM_LIT:200>:<EOL><INDENT>raise DataFailureException(url, response.status, response.data)<EOL><DEDENT>data = json.loads(response.data)<EOL>self.next_page_url = self._next_page(response)<EOL>if auto_page and self.next_page_url:<EOL><INDENT>if isinstance(data, list):<EOL><INDENT>data.extend(self._get_resource_url(self.next_page_url, True,<EOL>data_key))<EOL><DEDENT>elif isinstance(data, dict) and data_key is not None:<EOL><INDENT>data[data_key].extend(self._get_resource_url(<EOL>self.next_page_url, True, data_key)[data_key])<EOL><DEDENT><DEDENT>return data<EOL>

Canvas GET method on a full url. Return representation of the
requested resource, chasing pagination links to coalesce resources
if indicated.


def _get_paged_resource(self, url, params=None, data_key=None):

if not params:<EOL><INDENT>params = {}<EOL><DEDENT>self._set_as_user(params)<EOL>auto_page = not ('<STR_LIT>' in params or '<STR_LIT>' in params)<EOL>if '<STR_LIT>' not in params and self._per_page != DEFAULT_PAGINATION:<EOL><INDENT>params["<STR_LIT>"] = self._per_page<EOL><DEDENT>full_url = url + self._params(params)<EOL>return self._get_resource_url(full_url, auto_page, data_key)<EOL>

Canvas GET method. Return representation of the requested paged
resource, either the requested page, or chase pagination links to
coalesce resources.


def _get_resource(self, url, params=None, data_key=None):

if not params:<EOL><INDENT>params = {}<EOL><DEDENT>self._set_as_user(params)<EOL>full_url = url + self._params(params)<EOL>return self._get_resource_url(full_url, True, data_key)<EOL>

Canvas GET method. Return representation of the requested resource.


params = {}<EOL>self._set_as_user(params)<EOL>headers = {'<STR_LIT:Content-Type>': '<STR_LIT:application/json>',<EOL>'<STR_LIT>': '<STR_LIT:application/json>',<EOL>'<STR_LIT>': '<STR_LIT>'}<EOL>url = url + self._params(params)<EOL>response = DAO.putURL(url, headers, json.dumps(body))<EOL>if not (response.status == <NUM_LIT:200> or response.status == <NUM_LIT> or<EOL>response.status == <NUM_LIT>):<EOL><INDENT>raise DataFailureException(url, response.status, response.data)<EOL><DEDENT>return json.loads(response.data)<EOL>


params = {}<EOL>self._set_as_user(params)<EOL>headers = {'<STR_LIT:Content-Type>': '<STR_LIT:application/json>',<EOL>'<STR_LIT>': '<STR_LIT:application/json>',<EOL>'<STR_LIT>': '<STR_LIT>'}<EOL>url = url + self._params(params)<EOL>response = DAO.postURL(url, headers, json.dumps(body))<EOL>if not (response.status == <NUM_LIT:200> or response.status == <NUM_LIT>):<EOL><INDENT>raise DataFailureException(url, response.status, response.data)<EOL><DEDENT>return json.loads(response.data)<EOL>


params = {}<EOL>self._set_as_user(params)<EOL>headers = {'<STR_LIT>': '<STR_LIT:application/json>',<EOL>'<STR_LIT>': '<STR_LIT>'}<EOL>url = url + self._params(params)<EOL>response = DAO.deleteURL(url, headers)<EOL>if not (response.status == <NUM_LIT:200> or response.status == <NUM_LIT>):<EOL><INDENT>raise DataFailureException(url, response.status, response.data)<EOL><DEDENT>return response<EOL>


url = USERS_API.format(user_id) + "<STR_LIT>"<EOL>return CanvasUser(data=self._get_resource(url))<EOL>

Returns user profile data.

https://canvas.instructure.com/doc/api/users.html#method.profile.settings


return self.get_user(self._sis_id(sis_user_id, sis_field="<STR_LIT:user>"))<EOL>

Returns user profile data for the passed user sis id.

https://canvas.instructure.com/doc/api/courses.html#method.courses.users


def get_users_for_course(self, course_id, params={}):

url = COURSES_API.format(course_id) + "<STR_LIT>"<EOL>data = self._get_paged_resource(url, params=params)<EOL>users = []<EOL>for datum in data:<EOL><INDENT>users.append(CanvasUser(data=datum))<EOL><DEDENT>return users<EOL>

Returns a list of users for the given course id.


def get_users_for_sis_course_id(self, sis_course_id, params={}):

return self.get_users_for_course(<EOL>self._sis_id(sis_course_id, sis_field="<STR_LIT>"), params)<EOL>

Returns a list of users for the given sis course id.


def create_user(self, user, account_id=None):

if account_id is None:<EOL><INDENT>account_id = self._canvas_account_id<EOL>if account_id is None:<EOL><INDENT>raise MissingAccountID()<EOL><DEDENT><DEDENT>url = ACCOUNTS_API.format(account_id) + "<STR_LIT>"<EOL>data = self._post_resource(url, user.post_data())<EOL>return CanvasUser(data=data)<EOL>

Create and return a new user and pseudonym for an account.

https://canvas.instructure.com/doc/api/users.html#method.users.create


def get_user_logins(self, user_id, params={}):

url = USERS_API.format(user_id) + "<STR_LIT>"<EOL>data = self._get_paged_resource(url, params=params)<EOL>logins = []<EOL>for login_data in data:<EOL><INDENT>logins.append(Login(data=login_data))<EOL><DEDENT>return logins<EOL>

Return a user's logins for the given user_id.

https://canvas.instructure.com/doc/api/logins.html#method.pseudonyms.index


return self.get_user_logins(self._sis_id(sis_user_id,<EOL>sis_field="<STR_LIT:user>"))<EOL>

Returns user login data for the passed user sis id.


def update_user_login(self, login, account_id=None):

if account_id is None:<EOL><INDENT>account_id = self._canvas_account_id<EOL>if account_id is None:<EOL><INDENT>raise MissingAccountID<EOL><DEDENT><DEDENT>login_id = login.login_id<EOL>url = ACCOUNTS_API.format(account_id) + "<STR_LIT>".format(login_id)<EOL>data = self._put_resource(url, login.put_data())<EOL>return Login(data=data)<EOL>

Update an existing login for a user in the given account.

https://canvas.instructure.com/doc/api/logins.html#method.pseudonyms.update


def get_enrollments_for_course(self, course_id, params={}):

url = COURSES_API.format(course_id) + "<STR_LIT>"<EOL>enrollments = []<EOL>for datum in self._get_paged_resource(url, params=params):<EOL><INDENT>enrollments.append(CanvasEnrollment(data=datum))<EOL><DEDENT>return enrollments<EOL>

Return a list of all enrollments for the passed course_id.

https://canvas.instructure.com/doc/api/enrollments.html#method.enrollments_api.index


def get_enrollments_for_course_by_sis_id(self, sis_course_id, params={}):

return self.get_enrollments_for_course(<EOL>self._sis_id(sis_course_id, sis_field="<STR_LIT>"), params)<EOL>

Return a list of all enrollments for the passed course sis id.


def get_enrollments_for_section(self, section_id, params={}):

url = SECTIONS_API.format(section_id) + "<STR_LIT>"<EOL>enrollments = []<EOL>for datum in self._get_paged_resource(url, params=params):<EOL><INDENT>enrollments.append(CanvasEnrollment(data=datum))<EOL><DEDENT>return enrollments<EOL>

Return a list of all enrollments for the passed section_id.

https://canvas.instructure.com/doc/api/enrollments.html#method.enrollments_api.index


def get_enrollments_for_section_by_sis_id(self, sis_section_id, params={}):

return self.get_enrollments_for_section(<EOL>self._sis_id(sis_section_id, sis_field="<STR_LIT>"), params)<EOL>

Return a list of all enrollments for the passed section sis id.


def get_enrollments_for_regid(self, regid, params={},<EOL>include_courses=True):

sis_user_id = self._sis_id(regid, sis_field="<STR_LIT:user>")<EOL>url = USERS_API.format(sis_user_id) + "<STR_LIT>"<EOL>courses = Courses() if include_courses else None<EOL>enrollments = []<EOL>for datum in self._get_paged_resource(url, params=params):<EOL><INDENT>enrollment = CanvasEnrollment(data=datum)<EOL>if include_courses:<EOL><INDENT>course_id = datum["<STR_LIT>"]<EOL>course = courses.get_course(course_id)<EOL>if course.sis_course_id is not None:<EOL><INDENT>enrollment.course = course<EOL>enrollment.course_url = course.course_url<EOL>enrollment.course_name = course.name<EOL>enrollment.sis_course_id = course.sis_course_id<EOL><DEDENT><DEDENT>else:<EOL><INDENT>enrollment.course_url = re.sub(<EOL>r'<STR_LIT>', '<STR_LIT>', enrollment.html_url)<EOL><DEDENT>enrollments.append(enrollment)<EOL><DEDENT>return enrollments<EOL>

Return a list of enrollments for the passed user regid.

https://canvas.instructure.com/doc/api/enrollments.html#method.enrollments_api.index


def enroll_user(self, course_id, user_id, enrollment_type, params=None):

url = COURSES_API.format(course_id) + "<STR_LIT>"<EOL>if not params:<EOL><INDENT>params = {}<EOL><DEDENT>params["<STR_LIT>"] = user_id<EOL>params["<STR_LIT:type>"] = enrollment_type<EOL>data = self._post_resource(url, {"<STR_LIT>": params})<EOL>return CanvasEnrollment(data=data)<EOL>

Enroll a user into a course.

https://canvas.instructure.com/doc/api/enrollments.html#method.enrollments_api.create


def get_submissions_by_course_and_assignment(<EOL>self, course_id, assignment_id, params={}):

url = COURSES_API.format(course_id)<EOL>url += "<STR_LIT>".format(assignment_id)<EOL>submissions = []<EOL>for data in self._get_paged_resource(url, params=params):<EOL><INDENT>submissions.append(Submission(data=data))<EOL><DEDENT>return submissions<EOL>


def get_submissions_multiple_assignments_by_sis_id(<EOL>self, is_section, sis_id, students=None, assignments=None,<EOL>**params):

if is_section:<EOL><INDENT>return self.get_submissions_multiple_assignments(<EOL>is_section, self._sis_id(sis_id, '<STR_LIT>'), students,<EOL>assignments, **params)<EOL><DEDENT>else:<EOL><INDENT>return self.get_submissions_multiple_assignments(<EOL>is_section, self._sis_id(sis_id, '<STR_LIT>'), students,<EOL>assignments, **params)<EOL><DEDENT>

List submissions for multiple assignments by course/section sis id and
optionally student

https://canvas.instructure.com/doc/api/submissions.html#method.submissions_api.for_students


def get_submissions_multiple_assignments(<EOL>self, is_section, course_id, students=None, assignments=None,<EOL>**params):

api = SECTIONS_API if is_section else COURSES_API<EOL>if students is not None:<EOL><INDENT>params['<STR_LIT>'] = students<EOL><DEDENT>if assignments is not None:<EOL><INDENT>params['<STR_LIT>'] = assignments<EOL><DEDENT>url = api.format(course_id) + "<STR_LIT>"<EOL>data = self._get_paged_resource(url, params=params)<EOL>submissions = []<EOL>for submission in data:<EOL><INDENT>submissions.append(Submission(data=submission))<EOL><DEDENT>return submissions<EOL>

List submissions for multiple assignments by course/section id and
optionally student

https://canvas.instructure.com/doc/api/submissions.html#method.submissions_api.for_students


def get_course(self, course_id, params={}):

include = params.get("<STR_LIT>", [])<EOL>if "<STR_LIT>" not in include:<EOL><INDENT>include.append("<STR_LIT>")<EOL><DEDENT>params["<STR_LIT>"] = include<EOL>url = COURSES_API.format(course_id)<EOL>return CanvasCourse(data=self._get_resource(url, params=params))<EOL>

Return course resource for given canvas course id.

https://canvas.instructure.com/doc/api/courses.html#method.courses.show


def get_course_by_sis_id(self, sis_course_id, params={}):

return self.get_course(self._sis_id(sis_course_id, sis_field="<STR_LIT>"),<EOL>params)<EOL>

Return course resource for given sis id.


def get_courses_in_account(self, account_id, params={}):

if "<STR_LIT>" in params:<EOL><INDENT>params["<STR_LIT>"] = "<STR_LIT:true>" if params["<STR_LIT>"] else "<STR_LIT>"<EOL><DEDENT>url = ACCOUNTS_API.format(account_id) + "<STR_LIT>"<EOL>courses = []<EOL>for data in self._get_paged_resource(url, params=params):<EOL><INDENT>courses.append(CanvasCourse(data=data))<EOL><DEDENT>return courses<EOL>

Returns a list of courses for the passed account ID.

https://canvas.instructure.com/doc/api/accounts.html#method.accounts.courses_api


def get_courses_in_account_by_sis_id(self, sis_account_id, params={}):

return self.get_courses_in_account(<EOL>self._sis_id(sis_account_id, sis_field="<STR_LIT>"), params)<EOL>

Return a list of courses for the passed account SIS ID.


def get_published_courses_in_account(self, account_id, params={}):

params["<STR_LIT>"] = True<EOL>return self.get_courses_in_account(account_id, params)<EOL>

Return a list of published courses for the passed account ID.


return self.get_published_courses_in_account(<EOL>self._sis_id(sis_account_id, sis_field="<STR_LIT>"), params)<EOL>

Return a list of published courses for the passed account SIS ID.


def get_courses_for_regid(self, regid, params={}):

self._as_user = regid<EOL>data = self._get_resource("<STR_LIT>", params=params)<EOL>self._as_user = None<EOL>courses = []<EOL>for datum in data:<EOL><INDENT>if "<STR_LIT>" in datum:<EOL><INDENT>courses.append(CanvasCourse(data=datum))<EOL><DEDENT>else:<EOL><INDENT>courses.append(self.get_course(datum["<STR_LIT:id>"], params))<EOL><DEDENT><DEDENT>return courses<EOL>

Return a list of courses for the passed regid.

https://canvas.instructure.com/doc/api/courses.html#method.courses.index


def create_course(self, account_id, course_name):

url = ACCOUNTS_API.format(account_id) + "<STR_LIT>"<EOL>body = {"<STR_LIT>": {"<STR_LIT:name>": course_name}}<EOL>return CanvasCourse(data=self._post_resource(url, body))<EOL>

Create a canvas course with the given subaccount id and course name.

https://canvas.instructure.com/doc/api/courses.html#method.courses.create


def update_sis_id(self, course_id, sis_course_id):

url = COURSES_API.format(course_id)<EOL>body = {"<STR_LIT>": {"<STR_LIT>": sis_course_id}}<EOL>return CanvasCourse(data=self._put_resource(url, body))<EOL>

Updates the SIS ID for the course identified by the passed course ID.

https://canvas.instructure.com/doc/api/courses.html#method.courses.update


def get_admins(self, account_id, params={}):

url = ADMINS_API.format(account_id)<EOL>admins = []<EOL>for data in self._get_paged_resource(url, params=params):<EOL><INDENT>admins.append(CanvasAdmin(data=data))<EOL><DEDENT>return admins<EOL>

Return a list of the admins in the account.

https://canvas.instructure.com/doc/api/admins.html#method.admins.index


return self.get_admins(self._sis_id(sis_account_id))<EOL>

Return a list of the admins in the account by sis id.


def create_admin(self, account_id, user_id, role):

url = ADMINS_API.format(account_id)<EOL>body = {"<STR_LIT>": unquote(str(user_id)),<EOL>"<STR_LIT>": role,<EOL>"<STR_LIT>": False}<EOL>return CanvasAdmin(data=self._post_resource(url, body))<EOL>

Flag an existing user as an admin within the account.

https://canvas.instructure.com/doc/api/admins.html#method.admins.create


def create_admin_by_sis_id(self, sis_account_id, user_id, role):

return self.create_admin(self._sis_id(sis_account_id), user_id, role)<EOL>

Flag an existing user as an admin within the account sis id.


def delete_admin(self, account_id, user_id, role):

url = ADMINS_API.format(account_id) + "<STR_LIT>".format(<EOL>user_id, quote(role))<EOL>response = self._delete_resource(url)<EOL>return True<EOL>

Remove an account admin role from a user.

https://canvas.instructure.com/doc/api/admins.html#method.admins.destroy


def delete_admin_by_sis_id(self, sis_account_id, user_id, role):

return self.delete_admin(self._sis_id(sis_account_id), user_id, role)<EOL>

Remove an account admin role from a user for the account sis id.


def find_attacker_slider(dest_list, occ_bb, piece_bb, target_bb, pos,<EOL>domain):

pos_map, domain_trans, pos_inv_map = domain<EOL>r = reach[pos_map(pos)][domain_trans(target_bb, pos)]<EOL>m = r & domain_trans(piece_bb, pos)<EOL>while m:<EOL><INDENT>r = m&-m<EOL>rpos = r.bit_length()-<NUM_LIT:1><EOL>if not (ray[rpos][pos_map(pos)] & domain_trans(occ_bb, pos)):<EOL><INDENT>dest_list.append(pos_inv_map(rpos, pos))<EOL><DEDENT>m ^= r<EOL><DEDENT>

Find a slider attacker

    Parameters
    ----------
    dest_list : list
        To store the results.
    occ_bb : int, bitboard
        Occupancy bitboard.
    piece_bb : int, bitboard
        Bitboard with the position of the attacker piece.
    target_bb : int, bitboard
        Occupancy bitboard without any of the sliders in question.
    pos : int
        Target position.
    pos_map : function
        Mapping between a board position and its position in a single
        rotated/translated rank produced with domain_trans.
    domain_trans : function
        Transformation from a rank/file/diagonal/anti-diagonal containing pos
        to a single rank
    pos_inv_map : function
        Inverse of pos_map


np = P()<EOL>while n >= <NUM_LIT:1>:<EOL><INDENT>if n % <NUM_LIT:2>:<EOL><INDENT>np = np + p<EOL><DEDENT>p = p + p<EOL>n = n // <NUM_LIT:2><EOL><DEDENT>return np<EOL>

Returns a Pattern that matches exactly n repetitions of Pattern p.


categories = (<EOL>(('<STR_LIT:l>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'), '<STR_LIT>'),  <EOL>(('<STR_LIT>', '<STR_LIT>', '<STR_LIT:x>', '<STR_LIT:y>', '<STR_LIT:z>'), '<STR_LIT>'),  <EOL>(('<STR_LIT:f>', ), '<STR_LIT>'),  <EOL>(('<STR_LIT:d>', '<STR_LIT>'), '<STR_LIT>'),  <EOL>(('<STR_LIT>', ), '<STR_LIT>'),  <EOL>)<EOL>template_from = "<STR_LIT>"<EOL>template_to = '<STR_LIT>'<EOL>for option, color in categories:<EOL><INDENT>arguments = re.sub(template_from.format('<STR_LIT:|>'.join(option)),<EOL>template_to.format(color),<EOL>arguments)<EOL><DEDENT>return arguments<EOL>

Color insane arguments in regression tests


result = arguments<EOL>input_dir = None<EOL>func_names = ('<STR_LIT>',<EOL>'<STR_LIT>')<EOL>func_name = None<EOL>for name in func_names:<EOL><INDENT>if arguments.startswith(name + '<STR_LIT:(>'):<EOL><INDENT>func_name = name<EOL>break<EOL><DEDENT><DEDENT>if func_name is not None:<EOL><INDENT>reduced_name = arguments[len(func_name) + <NUM_LIT:1>:-<NUM_LIT:1>]<EOL>insane_args, input_dir = shlex.split(reduced_name)<EOL>if replace_files:<EOL><INDENT>arg_list = shlex.split(insane_args)<EOL>new_list = []<EOL>while arg_list:<EOL><INDENT>arg = arg_list.pop(<NUM_LIT:0>)<EOL>new_list.append(arg)<EOL>if arg == '<STR_LIT>':<EOL><INDENT>arg = arg_list.pop(<NUM_LIT:0>)<EOL>new_list.append(os.path.join(input_dir, arg))<EOL><DEDENT><DEDENT>insane_args = '<STR_LIT:U+0020>'.join(new_list)<EOL><DEDENT>result = '<STR_LIT>' + insane_args[:-<NUM_LIT:1>]<EOL><DEDENT>return result<EOL>

Rewrites insane regression test description for easy copy-paste


super(ImprovedDisplay, self).options(parser, env)<EOL>parser.add_option(<EOL>"<STR_LIT>", action="<STR_LIT>",<EOL>default=True, dest="<STR_LIT>",<EOL>help="<STR_LIT>"<EOL>)<EOL>parser.add_option(<EOL>"<STR_LIT>", action="<STR_LIT>",<EOL>default=True, dest="<STR_LIT>",<EOL>help="<STR_LIT>"<EOL>)<EOL>parser.add_option(<EOL>"<STR_LIT>", action="<STR_LIT>",<EOL>default=True, dest="<STR_LIT>",<EOL>help=("<STR_LIT>"<EOL>"<STR_LIT>")<EOL>)<EOL>

Registers the commandline option, defaulting to enabled.


super(ImprovedDisplay, self).configure(options, conf)<EOL>self.color_verbose = options.color_verbose<EOL>self.easy_copy = options.easy_copy<EOL>self.replace_input = options.replace_input<EOL>try:<EOL><INDENT>self.enabled = options.color_verbose or options.easy_copy<EOL><DEDENT>except AttributeError:<EOL><INDENT>self.enabled = False<EOL><DEDENT>

Configure plugin. Plugin is enabled by default.


Nose hook that prints at the end of the tests.


try:<EOL><INDENT>arguments_list = shlex.split(arguments)<EOL><DEDENT>except ValueError:<EOL><INDENT>arguments_list = arguments<EOL><DEDENT>return arguments_list<EOL>

Return the arguments as a list as expected by subprocess.Popen.

The arguments can be provided as a string that will be spitted to a list.
They can also be provided as a list, then the list will be returned
untouched.


if len(case) == <NUM_LIT:3>:<EOL><INDENT>case_args, input_dir, alias = case<EOL>if input_dir is not None:<EOL><INDENT>input_dir = os.path.join(INPUT_DIR, input_dir)<EOL><DEDENT><DEDENT>elif len(case) == <NUM_LIT:2>:<EOL><INDENT>case_args, input_dir = case<EOL>input_dir = os.path.join(INPUT_DIR, input_dir)<EOL>alias = case_args<EOL><DEDENT>else:<EOL><INDENT>case_args = case<EOL>input_dir = None<EOL>alias = case_args<EOL><DEDENT>return case_args, input_dir, alias<EOL>

Get the arguments and the input directory from a test case.


arg_list = _arguments_as_list(arguments)<EOL>simple_case_ref_data = os.path.join(DATA_DIR, '<STR_LIT>')<EOL>base_name = arguments if alias is None else alias<EOL>return os.path.join(simple_case_ref_data, base_name)<EOL>

Get the path to the reference files for the simple test cases.


def run_insane(arguments, input_directory=None, runner=_run_internal):

<EOL>if input_directory is not None:<EOL><INDENT>for path in glob.glob(os.path.join(input_directory, '<STR_LIT:*>')):<EOL><INDENT>if os.path.isdir(path):<EOL><INDENT>shutil.copytree(path, '<STR_LIT:.>')<EOL><DEDENT>else:<EOL><INDENT>shutil.copy2(path, '<STR_LIT:.>')<EOL><DEDENT><DEDENT><DEDENT>out, err, returncode = runner(arguments)<EOL>print("<STR_LIT>".format(returncode))<EOL>if returncode:<EOL><INDENT>print(err)<EOL><DEDENT>return out, err, returncode<EOL>

Run insane with the given arguments

Insane is run in a copy of `input_directory`.


out_file = utils._open_if_needed(output)<EOL>ref_file = utils._open_if_needed(reference)<EOL>with out_file, ref_file:<EOL><INDENT>lines_zip = zip(out_file, ref_file)<EOL>for out_line, ref_line in lines_zip:<EOL><INDENT>assert_equal(out_line, ref_line)<EOL><DEDENT>extra_out = list(out_file)<EOL>extra_ref = list(ref_file)<EOL>assert_equal(extra_out, [])<EOL>assert_equal(extra_ref, [])<EOL><DEDENT>


def run_and_compare(arguments, input_dir, ref_dir, runner):

<EOL>arguments = _arguments_as_list(arguments)<EOL>ref_stdout = os.path.join(ref_dir, '<STR_LIT>')<EOL>ref_stderr = os.path.join(ref_dir, '<STR_LIT>')<EOL>with utils.tempdir():<EOL><INDENT>out, err, returncode = run_insane(arguments, input_dir, runner=runner)<EOL>assert not returncode<EOL>compare(utils.ContextStringIO(out), ref_stdout)<EOL>compare(utils.ContextStringIO(err), ref_stderr)<EOL>compare_directories('<STR_LIT>', ref_dir,<EOL>ignore=('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'))<EOL><DEDENT>

Run insane and compare its output against a reference


for case in SIMPLE_TEST_CASES:<EOL><INDENT>case_args, input_dir, alias = _split_case(case)<EOL>arguments = _arguments_as_list(case_args)<EOL>ref_dir = _reference_path(case_args, alias)<EOL>ref_stdout = os.path.join(ref_dir, '<STR_LIT>')<EOL>ref_stderr = os.path.join(ref_dir, '<STR_LIT>')<EOL>if os.path.exists(ref_dir):<EOL><INDENT>shutil.rmtree(ref_dir)<EOL><DEDENT>os.mkdir(ref_dir)<EOL>with utils.in_directory(ref_dir):<EOL><INDENT>print(INSANE + '<STR_LIT:U+0020>' + '<STR_LIT:U+0020>'.join(arguments))<EOL>out, err, _ = run_insane(arguments, input_dir)<EOL>with open(ref_stdout, '<STR_LIT:w>') as outfile:<EOL><INDENT>for line in out:<EOL><INDENT>print(line, file=outfile, end='<STR_LIT>')<EOL><DEDENT><DEDENT>with open(ref_stderr, '<STR_LIT:w>') as outfile:<EOL><INDENT>for line in err:<EOL><INDENT>print(line, file=outfile, end='<STR_LIT>')<EOL><DEDENT><DEDENT><DEDENT><DEDENT>

Run insane to generate reference files for the simple regression tests.

Run insane with the arguments listed in SIMPLE_TEST_CASES. The output
files, the standard output, and the standard error are stored in the
DATA_DIR/simple_case directory.


simple_test_cases = [_split_case(case)[<NUM_LIT:2>] for case in SIMPLE_TEST_CASES]<EOL>simple_case_ref_data = os.path.join(DATA_DIR, '<STR_LIT>')<EOL>for path in glob.glob(os.path.join(simple_case_ref_data, '<STR_LIT:*>')):<EOL><INDENT>base_name = os.path.basename(os.path.splitext(path)[<NUM_LIT:0>])<EOL>if base_name not in simple_test_cases:<EOL><INDENT>print(path)<EOL>os.remove(path)<EOL><DEDENT><DEDENT>

Delete reference files for the simple tests if they are not in use anymore.


help_ =


dirpath = tempfile.mkdtemp()<EOL>try:<EOL><INDENT>with in_directory(dirpath):<EOL><INDENT>yield dirpath<EOL><DEDENT><DEDENT>finally:<EOL><INDENT>shutil.rmtree(dirpath)<EOL><DEDENT>

Context manager that moves in a temporary directory.

.. code::

    # We are in the initial working directory
    with tempdir():
        # We are now in a temporary directory
        ...
    # We are back to the initial working directory.
    # The temporary does not exist anymore.


if None in args:<EOL><INDENT>return None<EOL><DEDENT>return os.path.realpath(<EOL>os.path.expanduser(os.path.expandvars(os.path.join(*args)))<EOL>)<EOL>

Join all args and return the real path, rooted at /.
    Expands '~', '~user', and environment variables such as :envvar`$HOME`.
    Returns ``None`` if any of the args is ``None``.


def is_exe(fpath):<EOL><INDENT>"""<STR_LIT>"""<EOL>return os.path.isfile(fpath) and os.access(fpath, os.X_OK)<EOL><DEDENT>fpath, _ = os.path.split(program)<EOL>if fpath:<EOL><INDENT>real_program = realpath(program)<EOL>if is_exe(real_program):<EOL><INDENT>return real_program<EOL><DEDENT><DEDENT>else:<EOL><INDENT>for path in os.environ["<STR_LIT>"].split(os.pathsep):<EOL><INDENT>exe_file = os.path.join(path, program)<EOL>if is_exe(exe_file):<EOL><INDENT>return exe_file<EOL><DEDENT><DEDENT><DEDENT>return None<EOL>

Determine full path of executable *program* on :envvar:`PATH`.

    (Jay at http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python)


<EOL>title = next(stream)<EOL>natoms = int(next(stream))<EOL>atoms = []<EOL>for atom_count, line in enumerate(stream, start=<NUM_LIT:0>):<EOL><INDENT>if atom_count == natoms:<EOL><INDENT>break<EOL><DEDENT>atoms.append({key: convert(line[begin:end].strip())<EOL>for key, ((begin, end), convert) in GRO_FIELDS.items()})<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>box = [float(x) for x in line.split()]<EOL>try:<EOL><INDENT>next(stream)<EOL><DEDENT>except StopIteration:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>raise ValueError('<STR_LIT>'<EOL>'<STR_LIT>')<EOL><DEDENT>return title, atoms, box<EOL>

Parse a gro file

Read an iterable over the lines of a GRO file. Returns the title, the
atoms, and the box. The atoms are returned as a list of dict, where each
dict represents an atom. The keys of the atom dicts are

* 'resid' for the residue number;
* 'resname' for the residue name;
* 'index' for the atom index as written in the file;
* 'name' for the atom name;
* 'x', 'y', and 'z' for the atom coordinates.

The box is returned as a list of floats.

.. note::

   The function does not read velocities. Also, it does not support
   variable precision.


def compare_gro(stream, ref_stream, tolerance=<NUM_LIT>):

differences = []<EOL>title, atoms, box = read_gro(stream)<EOL>title_ref, atoms_ref, box_ref = read_gro(ref_stream)<EOL>if title != title_ref:<EOL><INDENT>diff = GroDiff(linenum=<NUM_LIT:1>, line=title, ref_line=title_ref, fields=None)<EOL>differences.append(diff)<EOL><DEDENT>if len(atoms) != len(atoms_ref):<EOL><INDENT>diff = GroDiff(linenum=<NUM_LIT:2>,<EOL>line=str(len(atoms)),<EOL>ref_line=str(len(atoms_ref)),<EOL>fields=None)<EOL>differences.append(diff)<EOL><DEDENT>atom_iter = enumerate(<EOL>zip_longest(atoms, atoms_ref, fillvalue={}),<EOL>start=<NUM_LIT:3><EOL>)<EOL>for linenum, (atom, atom_ref) in atom_iter:<EOL><INDENT>if atom and atom_ref:<EOL><INDENT>diff_fields = []<EOL>for gro_field, (_, gro_type) in GRO_FIELDS.items():<EOL><INDENT>if gro_type == float:<EOL><INDENT>error = math.fabs(atom[gro_field] - atom_ref[gro_field])<EOL>if error > tolerance:<EOL><INDENT>diff_fields.append(gro_field)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if atom[gro_field] != atom_ref[gro_field]:<EOL><INDENT>diff_fields.append(gro_field)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>diff_fields.append(None)<EOL><DEDENT>if diff_fields:<EOL><INDENT>line = ref_line = '<STR_LIT>'<EOL>if atom:<EOL><INDENT>line = GRO_TEMPLATE.format(**atom)<EOL><DEDENT>if atom_ref:<EOL><INDENT>ref_line = GRO_TEMPLATE.format(**atom_ref)<EOL><DEDENT>diff = GroDiff(linenum=linenum,<EOL>line=line,<EOL>ref_line=ref_line,<EOL>fields=diff_fields)<EOL>differences.append(diff)<EOL><DEDENT><DEDENT>if box != box_ref:<EOL><INDENT>diff = GroDiff(linenum=linenum + <NUM_LIT:1>,<EOL>line='<STR_LIT:U+0020>'.join(map(str, box)),<EOL>ref_line='<STR_LIT:U+0020>'.join(map(str, box_ref)),<EOL>fields='<STR_LIT>')<EOL>differences.append(diff)<EOL><DEDENT>return differences<EOL>

Compare two gro files with a tolerance on the coordinates

The `stream` and `ref_stream` arguments are iterable over the lines of two
GRO files to compare. The tolerance is provided in nanometer so that

    abs(x1 - x2) <= tolerance

The function returns a list of differences. Each difference is represented
as a 'GroDiff' named tupple with the following field:

* 'linenum': the number of the line where the difference occurs, the line
  count starts at 1 to make the difference easier to finc in a text editor;
* 'line' and 'ref_line': the line that differ as it is written in 'stream'
  and in 'ref_stream', respectivelly;
* 'fields': the specific field that differ beween the lines.

The lines in the differences are re-formated from the information that have
been parsed. The exact string may differ, especially if velocities were
provided.

The fields in the difference list are named after the GRO_FIELDS dict. It
is `None` if the difference occurs in the title or the number of atoms. It
is 'box' if for the box.

If the number of atoms differ between the two files, then the atoms that
exist in only one of the files are all counted as different and the field
is set to `None`. If the box also differ, then its line number in the
repport will be wrong for one of the files.


def format_gro_diff(differences, outstream=sys.stdout, max_atoms=<NUM_LIT:10>):

if not differences:<EOL><INDENT>return<EOL><DEDENT>differences = copy.copy(differences)<EOL>if differences and differences[<NUM_LIT:0>].linenum == <NUM_LIT:0>:<EOL><INDENT>diff = differences.pop(<NUM_LIT:0>)<EOL>print('<STR_LIT>', file=outstream)<EOL>print(diff.line, file=outstream)<EOL>print(diff.ref_line, file=outstream)<EOL><DEDENT>if differences and differences[<NUM_LIT:0>].linenum == <NUM_LIT:1>:<EOL><INDENT>diff = differences.pop(<NUM_LIT:0>)<EOL>print('<STR_LIT>'<EOL>'<STR_LIT>'.format(diff.line, diff.ref_line),<EOL>file=outstream)<EOL><DEDENT>if differences and differences[-<NUM_LIT:1>].fields == '<STR_LIT>':<EOL><INDENT>diff = differences.pop(-<NUM_LIT:1>)<EOL>print('<STR_LIT>', file=outstream)<EOL>print(diff.line, file=outstream)<EOL>print(diff.ref_line, file=outstream)<EOL><DEDENT>if len(differences) > max_atoms:<EOL><INDENT>print('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>.format(len(differences), max_atoms),<EOL>file=outstream)<EOL><DEDENT>for diff in differences[:max_atoms]:<EOL><INDENT>print('<STR_LIT>'.format(diff.linenum), file=outstream)<EOL>print(diff.line, file=outstream)<EOL>print(diff.ref_line, file=outstream)<EOL><DEDENT>

Format differences between GRO files in a human readable way.


diff_out = StringIO()<EOL>with open(path) as stream, open(ref_path) as ref_stream:<EOL><INDENT>differences = compare_gro(stream, ref_stream)<EOL>format_gro_diff(differences, outstream=diff_out)<EOL>assert len(differences) == <NUM_LIT:0>, '<STR_LIT:\n>' + diff_out.getvalue()<EOL><DEDENT>

Raise an AssertionError if two GRO files are not semantically identical.


if isinstance(handle, ContextStringIO):<EOL><INDENT>return handle<EOL><DEDENT>return open(handle)<EOL>

Return handle if it is a ContextStringIO instance else try to open it.


pass<EOL>

Does nothing when entering a 'with' statement.


pass<EOL>

Does nothing when exiting a 'with' statement.


lip = x.split("<STR_LIT::>")<EOL>abn = lip[<NUM_LIT:0>].split("<STR_LIT:=>")<EOL>names = abn[<NUM_LIT:0>]<EOL>if len(abn) > <NUM_LIT:1>:<EOL><INDENT>nrel = <NUM_LIT:0><EOL>nabs = int(abn[<NUM_LIT:1>])<EOL><DEDENT>else:<EOL><INDENT>nabs = <NUM_LIT:0><EOL>if len(lip) > <NUM_LIT:1>:<EOL><INDENT>nrel = float(lip[<NUM_LIT:1>])<EOL><DEDENT>else:<EOL><INDENT>nrel = <NUM_LIT:1><EOL><DEDENT><DEDENT>return abn[<NUM_LIT:0>], nabs, nrel<EOL>

Parse a string for a lipid or a solvent as given on the command line
(MOLECULE[=NUMBER|:NUMBER]); where `=NUMBER` sets an absolute number of the
molecule, and `:NUMBER` sets a relative number of it.
If both absolute and relative number are set False, then relative count is 1.


fields = [i.split("<STR_LIT:=>") for i in string.split('<STR_LIT:U+002CU+0020>')]<EOL>for what, val in fields:<EOL><INDENT>what = what.strip()<EOL>val  = val.split()<EOL>if what.endswith("<STR_LIT>"):<EOL><INDENT>self.head = val<EOL><DEDENT>elif what.endswith("<STR_LIT>"):<EOL><INDENT>self.link = val<EOL><DEDENT>elif what.endswith("<STR_LIT>"):<EOL><INDENT>self.tail = val<EOL><DEDENT>elif what == "<STR_LIT>":<EOL><INDENT>self.charge = float(val[<NUM_LIT:0>])<EOL><DEDENT>elif what.endswith("<STR_LIT:name>") and not self.name:<EOL><INDENT>self.name = val[<NUM_LIT:0>]<EOL><DEDENT><DEDENT>if self.charge is None:<EOL><INDENT>self.charge = sum([headgroup_charges[bead] for bead in self.head])<EOL><DEDENT>

Parse lipid definition from string:

    alhead=C P, allink=A A, altail=TCC CCCC, alname=DPSM, charge=0.0


if not self.coords:<EOL><INDENT>if self.beads and self.template:<EOL><INDENT>stuff = zip(self.beads, self.template)<EOL>self.coords = [[i, x, y, z] for i, (x, y, z) in stuff if i != "<STR_LIT:->"]<EOL><DEDENT>else:<EOL><INDENT>if self.beads:<EOL><INDENT>beads = list(self.beads)<EOL><DEDENT>else:<EOL><INDENT>beads = [HEADBEADS[i] for i in self.head]<EOL>beads.extend([LINKBEADS[n] + str(i + <NUM_LIT:1>)<EOL>for i, n in enumerate(self.link)])<EOL>for i, t in enumerate(self.tail):<EOL><INDENT>beads.extend([n + chr(<NUM_LIT> + i) + str(j + <NUM_LIT:1>)<EOL>for j, n in enumerate(t)])<EOL><DEDENT><DEDENT>taillength = max([<NUM_LIT:0>]+[len(i) for i in self.tail])<EOL>length = len(self.head)+taillength<EOL>rl     = range(len(self.head))<EOL>struc  = [(<NUM_LIT:0>, <NUM_LIT:0>, length-i) for i in rl]<EOL>rl     = range(len(self.link))<EOL>struc.extend([(i%<NUM_LIT:2>, i//<NUM_LIT:2>, taillength) for i in rl ])<EOL>for j, tail in enumerate(self.tail):<EOL><INDENT>rl = range(len(tail))<EOL>struc.extend([(j%<NUM_LIT:2>, j//<NUM_LIT:2>, taillength-<NUM_LIT:1>-i) for i in rl])<EOL><DEDENT>mx, my, mz = [ (max(i)+min(i))/<NUM_LIT:2> for i in zip(*struc) ]<EOL>self.coords = [<EOL>[i, <NUM_LIT> * (x - mx), <NUM_LIT> * (y - my), z]<EOL>for i, (x, y, z) in zip(beads, struc)<EOL>]<EOL><DEDENT><DEDENT>diam   = kwargs.get("<STR_LIT>", self.diam)<EOL>radius = diam*<NUM_LIT><EOL>minmax = [ (min(i), max(i)) for i in list(zip(*self.coords))[<NUM_LIT:1>:] ]<EOL>mx, my, mz = [ sum(i)/<NUM_LIT> for i in minmax ]<EOL>scale  = radius/math.sqrt((minmax[<NUM_LIT:0>][<NUM_LIT:0>]-mx)**<NUM_LIT:2> + (minmax[<NUM_LIT:1>][<NUM_LIT:0>]-my)**<NUM_LIT:2>)<EOL>for i in self.coords:<EOL><INDENT>i[<NUM_LIT:1>] = scale*(i[<NUM_LIT:1>]-mx)<EOL>i[<NUM_LIT:2>] = scale*(i[<NUM_LIT:2>]-my)<EOL>i[<NUM_LIT:3>] -= minmax[<NUM_LIT:2>][<NUM_LIT:0>]<EOL><DEDENT>return self.coords<EOL>

Build/return a list of [(bead, x, y, z), ...]


def occupancy(grid, points, spacing=<NUM_LIT>):

distances = ((grid[:,None,:] - points[None,:,:])**<NUM_LIT:2>).sum(axis=<NUM_LIT:2>)<EOL>occupied = (distances < spacing).sum(axis=<NUM_LIT:1>)<EOL>return occupied<EOL>

Return a vector with the occupancy of each grid point for
    given array of points


def write_gro(outfile, title, atoms, box):

<EOL>print(title, file=outfile)<EOL>print("<STR_LIT>".format(len(atoms)), file=outfile)<EOL>atom_template = "<STR_LIT>"<EOL>for idx, atname, resname, resid, x, y, z in atoms:<EOL><INDENT>print(atom_template<EOL>.format(int(resid % <NUM_LIT>), resname, atname, int(idx % <NUM_LIT>),<EOL>x, y, z),<EOL>file=outfile)<EOL><DEDENT>grobox = (box[<NUM_LIT:0>][<NUM_LIT:0>], box[<NUM_LIT:1>][<NUM_LIT:1>], box[<NUM_LIT:2>][<NUM_LIT:2>],<EOL>box[<NUM_LIT:0>][<NUM_LIT:1>], box[<NUM_LIT:0>][<NUM_LIT:2>], box[<NUM_LIT:1>][<NUM_LIT:0>],<EOL>box[<NUM_LIT:1>][<NUM_LIT:2>], box[<NUM_LIT:2>][<NUM_LIT:0>], box[<NUM_LIT:2>][<NUM_LIT:1>])<EOL>box_template = '<STR_LIT>' * <NUM_LIT:9><EOL>print(box_template.format(*grobox), file=outfile)<EOL>

Write a GRO file.

Parameters
----------
outfile
    The stream to write in.
title
    The title of the GRO file. Must be a single line.
atoms
    An instance of Structure containing the atoms to write.
box
    The periodic box as a 3x3 matrix.


def write_pdb(outfile, title, atoms, box):

<EOL>print('<STR_LIT>' + title, file=outfile)<EOL>print(pdbBoxString(box), file=outfile)<EOL>for idx, atname, resname, resid, x, y, z in atoms:<EOL><INDENT>print(pdbline % (idx % <NUM_LIT>, atname[:<NUM_LIT:4>], resname[:<NUM_LIT:3>], "<STR_LIT>",<EOL>resid % <NUM_LIT>, '<STR_LIT>', <NUM_LIT:10>*x, <NUM_LIT:10>*y, <NUM_LIT:10>*z, <NUM_LIT:0>, <NUM_LIT:0>, '<STR_LIT>'),<EOL>file=outfile)<EOL><DEDENT>

Write a PDB file.

Parameters
----------
outfile
    The stream to write in.
title
    The title of the GRO file. Must be a single line.
atoms
    An instance of Structure containing the atoms to write.
box
    The periodic box as a 3x3 matrix.


def determine_molecule_numbers(total, molecules, absolute, relative):

weight = sum(relative)<EOL>if not any(absolute):<EOL><INDENT>numbers = [int(total*i/weight) for i in relative]<EOL><DEDENT>elif any(relative):<EOL><INDENT>rest = total - sum(absolute)<EOL>numbers = [int(rest*i/weight) if i else j <EOL>for i,j in zip(relative, absolute)]<EOL><DEDENT>else:<EOL><INDENT>numbers = absolute<EOL><DEDENT>return list(zip(molecules, numbers))<EOL>

Determine molecule numbers for given total,
    absolute and relative numbers


def resize_pbc_for_lipids(pbc, relL, relU, absL, absU,<EOL>uparea, area, hole, proteins):

if any(relL) and any(relU):<EOL><INDENT>if <NUM_LIT:0> in (pbc.x, pbc.y, pbc.z):<EOL><INDENT>raise PBCException('<STR_LIT>')<EOL><DEDENT><DEDENT>elif any(absL) or any(absU):<EOL><INDENT>if pbc.z == <NUM_LIT:0>:<EOL><INDENT>raise PBCException('<STR_LIT>')<EOL><DEDENT>if <NUM_LIT:0> in (pbc.x, pbc.y):<EOL><INDENT>pbc.x = pbc.y = <NUM_LIT:1><EOL><DEDENT>upsize = sum(absU) * uparea<EOL>losize = sum(absL) * area<EOL>holesize = np.pi * hole ** <NUM_LIT:2><EOL>xysize = pbc.x * pbc.y<EOL>psize_up = sum([p.areaxy(<NUM_LIT:0>, <NUM_LIT>) for p in proteins])<EOL>psize_lo = sum([p.areaxy(-<NUM_LIT>, <NUM_LIT:0>) for p in proteins])<EOL>unavail_up = holesize + psize_up<EOL>unavail_lo = holesize + psize_lo<EOL>upscale = (upsize + unavail_up)/xysize<EOL>loscale = (losize + unavail_lo)/xysize<EOL>area_scale = max(upscale, loscale)<EOL>aspect_ratio = pbc.x / pbc.y<EOL>scale_x = np.sqrt(area_scale / aspect_ratio)<EOL>scale_y = np.sqrt(area_scale / aspect_ratio)<EOL>pbc.box[:<NUM_LIT:2>,:] *= math.sqrt(area_scale)<EOL><DEDENT>

Adapt the size of the box to accomodate the lipids.

The PBC is changed **in place**.


def write_top(outpath, molecules, title):

topmolecules = []<EOL>for i in molecules:<EOL><INDENT>if i[<NUM_LIT:0>].endswith('<STR_LIT>'):<EOL><INDENT>topmolecules.append(tuple([i[<NUM_LIT:0>][:-<NUM_LIT:2>]]+list(i[<NUM_LIT:1>:])))<EOL><DEDENT>else:<EOL><INDENT>topmolecules.append(i)<EOL><DEDENT><DEDENT>if outpath:<EOL><INDENT>with open(outpath, "<STR_LIT:w>") as top:<EOL><INDENT>print('<STR_LIT>', file=top)<EOL>print('<STR_LIT>', file=top)<EOL>print('<STR_LIT>', file=top)<EOL>print(title, file=top)<EOL>print('<STR_LIT:\n>', file=top)<EOL>print('<STR_LIT>', file=top)<EOL>print('<STR_LIT>', file=top)<EOL>print("<STR_LIT:\n>".join("<STR_LIT>"%i for i in topmolecules), file=top)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>added_molecules = (molecule for molecule in topmolecules<EOL>if molecule[<NUM_LIT:0>] != '<STR_LIT>')<EOL>print("<STR_LIT:\n>".join("<STR_LIT>"%i for i in added_molecules), file=sys.stderr)<EOL><DEDENT>

Write a basic TOP file.

The topology is written in *outpath*. If *outpath* is en empty string, or
anything for which ``bool(outpath) == False``, the topology is written on
the standard error, and the header is omitted, and only what has been buit
by Insane id displayed (e.g. Proteins are excluded).

Parameters
----------
outpath
    The path to the file to write. If empty, a simplify topology is
    written on stderr.
molecules
    List of molecules with the number of them.
title
    Title of the system.


with pkg_resources.resource_stream(__name__, filename) as resource:<EOL><INDENT>for line in resource:<EOL><INDENT>yield line.decode('<STR_LIT:utf-8>')<EOL><DEDENT><DEDENT>

Return a stream for a given resource file in the module.

The resource file has to be part of the module and its filenane given
relative to the module.


return '<STR_LIT>', []<EOL>

\
        kills the child - if running - and then single-beat itself exits
        useful to terminate all single-beat instances in one go
        :return:


return '<STR_LIT>', []<EOL>

\
        it will kill the child and pause all nodes


return '<STR_LIT>', []<EOL>

\
        will resume all nodes - set them to waiting state


return '<STR_LIT>', []<EOL>

\
        it will restart the child process - in the same node
        useful for when deploying new code


return '<STR_LIT>', []<EOL>

\
        it will stop the child process, then any single-beat node will pick it up
        and restart


return '<STR_LIT:_>'.join(args)<EOL>

by looking at arguments we try to generate a proper identifier
        >>> get_process_identifier(['python', 'echo.py', '1'])
        'python_echo.py_1'


if self.REDIS_SERVER.startswith('<STR_LIT>') orself.REDIS_SERVER.startswith('<STR_LIT>') orself.REDIS_SERVER.startswith('<STR_LIT>'):<EOL><INDENT>return self.REDIS_SERVER<EOL><DEDENT>return '<STR_LIT>'.format(self.REDIS_SERVER)<EOL>

\
        if REDIS_SERVER is just an ip address, then we try to translate it to
        redis_url, redis://REDIS_SERVER so that it doesn't try to connect to
        localhost while you try to connect to another server
        :return:


if self._host_identifier:<EOL><INDENT>return self._host_identifier<EOL><DEDENT>local_ip_addr = self.get_redis().connection_pool.get_connection('<STR_LIT>')._sock.getsockname()[<NUM_LIT:0>]<EOL>self._host_identifier = '<STR_LIT>'.format(local_ip_addr, os.getpid())<EOL>return self._host_identifier<EOL>

\
        we try to return IPADDR:PID form to identify where any singlebeat instance is
        running.

        :return:


When child exits we use the same exit status code


\
        when we deliberately restart/stop the child process,
        we don't want to exit ourselves, so we replace proc_exit_cb
        with a noop one when restarting
        :param exit_status:
        :return:


\
        this is used when we restart the process,
        it re-triggers the start


if self.sprocess:<EOL><INDENT>return self.sprocess.pid<EOL><DEDENT>return -<NUM_LIT:1><EOL>

\
        when we are restarting, we want to keep sending heart beat, so any other single-beat
        node will not pick it up.
        hence we need a process-id as an identifier - even for a short period of time.
        :return:


\
        when restarting we are doing exactly the same as running - we don't want any other
        single-beat node to pick up
        :return:


def sigterm_handler(self, signum, frame):

assert(self.state in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>'))<EOL>logger.debug("<STR_LIT>", self.state)<EOL>if self.state == '<STR_LIT>':<EOL><INDENT>return self.ioloop.stop()<EOL><DEDENT>if self.state == '<STR_LIT>':<EOL><INDENT>logger.debug('<STR_LIT>',<EOL>self.sprocess.pid)<EOL>os.kill(self.sprocess.pid, signum)<EOL><DEDENT>self.ioloop.stop()<EOL>

When we get term signal
        if we are waiting and got a sigterm, we just exit.
        if we have a child running, we pass the signal first to the child
        then we exit.

        :param signum:
        :param frame:
        :return:


if self.state == State.RUNNING and self.sprocess and self.sprocess.proc:<EOL><INDENT>self.sprocess.proc.kill()<EOL><DEDENT>else:<EOL><INDENT>sys.exit(<NUM_LIT:0>)<EOL><DEDENT>


info = '<STR_LIT>'<EOL>if self.state == State.RUNNING and self.sprocess and self.sprocess.proc:<EOL><INDENT>self.sprocess.set_exit_callback(self.proc_exit_cb_noop)<EOL>self.sprocess.proc.kill()<EOL>info = '<STR_LIT>'<EOL><DEDENT>self.state = State.PAUSED<EOL>return info<EOL>

\
        if we have a running child we kill it and set our state to paused
        if we don't have a running child, we set our state to paused
        this will pause all the nodes in single-beat cluster

        its useful when you deploy some code and don't want your child to spawn
        randomly

        :param msg:
        :return:


if self.state == State.PAUSED:<EOL><INDENT>self.state = State.WAITING<EOL><DEDENT>

\
        sets state to waiting - so we resume spawning children


info = '<STR_LIT>'<EOL>if self.state == State.RUNNING and self.sprocess and self.sprocess.proc:<EOL><INDENT>self.state = State.PAUSED<EOL>self.sprocess.set_exit_callback(self.proc_exit_cb_state_set)<EOL>self.sprocess.proc.kill()<EOL>info = '<STR_LIT>'<EOL><DEDENT>return info<EOL>

\
        stops the running child process - if its running
        it will re-spawn in any single-beat node after sometime

        :param msg:
        :return:


info = '<STR_LIT>'<EOL>if self.state == State.RUNNING and self.sprocess and self.sprocess.proc:<EOL><INDENT>self.state = State.RESTARTING<EOL>self.sprocess.set_exit_callback(self.proc_exit_cb_restart)<EOL>self.sprocess.proc.kill()<EOL>info = '<STR_LIT>'<EOL><DEDENT>return info<EOL>

\
        restart the subprocess
        i. we set our state to RESTARTING - on restarting we still send heartbeat
        ii. we kill the subprocess
        iii. we start again
        iv. if its started we set our state to RUNNING, else we set it to WAITING

        :param msg:
        :return:


@functools.wraps(interface)<EOL>def connection(*args, **kwargs):<EOL><INDENT>"""<STR_LIT>"""<EOL>session = Session()<EOL>session.mount('<STR_LIT>',  HTTPAdapter(max_retries=<NUM_LIT:2>))<EOL>session.mount('<STR_LIT>', HTTPAdapter(max_retries=<NUM_LIT:2>))<EOL>request  = Request(**interface(*args, **kwargs))<EOL>prepare  = session.prepare_request(request)<EOL>response = session.send(prepare, verify=True)<EOL>if response.status_code != requests.codes.ok:<EOL><INDENT>response.raise_for_status()<EOL><DEDENT>cleanup = re.subn(r'<STR_LIT>', '<STR_LIT>', response.content.decode('<STR_LIT:utf-8>'))[<NUM_LIT:0>]<EOL>return json.loads(cleanup.replace(r'<STR_LIT>', r'<STR_LIT:U+0020>').replace('<STR_LIT>', '<STR_LIT>'), encoding='<STR_LIT>')<EOL><DEDENT>return connection<EOL>

Decorates a function returning the url of translation API.
Creates and maintains HTTP connection state

Returns a dict response object from the server containing the translated
text and metadata of the request body

:param interface: Callable Request Interface
:type interface: Function


@wraps(func)<EOL>def initialization(*args, **kwargs):<EOL><INDENT>start = func(*args, **kwargs)<EOL>next(start)<EOL>return start<EOL><DEDENT>return initialization<EOL>

Initializes coroutine essentially priming it to the yield statement.
Used as a decorator over functions that generate coroutines.

.. code-block:: python

    # Basic coroutine producer/consumer pattern
    from translate import coroutine

    @coroutine
    def coroutine_foo(bar):
        try:
            while True:
                baz = (yield)
                bar.send(baz)

        except GeneratorExit:
            bar.close()

:param func: Unprimed Generator
:type func: Function

:return: Initialized Coroutine
:rtype: Function


return (<EOL>init + len(update)<EOL>if isinstance(init, int) else<EOL>init + update<EOL>)<EOL>

Generic accumulator function.

.. code-block:: python

    # Simplest Form
    >>> a = 'this' + ' '
    >>> b = 'that'
    >>> c = functools.reduce(accumulator, a, b)
    >>> c
    'this that'

    # The type of the initial value determines output type.
    >>> a = 5
    >>> b = Hello
    >>> c = functools.reduce(accumulator, a, b)
    >>> c
    10

:param init:  Initial Value
:param update: Value to accumulate

:return: Combined Values


first    = operator.itemgetter(<NUM_LIT:0>)<EOL>sentence, _ = script<EOL>printer  = partial(print, file=sys.stdout, end='<STR_LIT>')<EOL>for line in sentence:<EOL><INDENT>if isinstance(first(line), str):<EOL><INDENT>printer(first(line))<EOL><DEDENT>else:<EOL><INDENT>printer(first(line).encode('<STR_LIT>'))<EOL><DEDENT><DEDENT>printer('<STR_LIT:\n>')<EOL>return sys.stdout.flush()<EOL>

:param script: Translated Text
:type script: Iterable

:param output: Output Type (either 'trans' or 'translit')
:type output: String