text:
r"""Deepspeech.

This model uses a deepspeech2 network to convert speech to text.
paper : https://arxiv.org/abs/1512.02595

# BiLSTM code contributed by bastings@
# github : https://github.com/bastings
# webpage : https://bastings.github.io/
"""

import functools
from typing import Any, List, Mapping, Optional, Sequence, Tuple, Type, Union

import flax
from flax import linen as nn
from flax import struct
from init2winit.model_lib import base_model
from init2winit.model_lib import librispeech_preprocessor as preprocessor
from init2winit.model_lib import spectrum_augmenter
import jax
import jax.numpy as jnp
from ml_collections.config_dict import config_dict

Array = jnp.ndarray
StateType = Union[Array, Tuple[Array, ...]]
PRNGKey = Any
Shape = Tuple[int]
Dtype = Any

DEFAULT_HPARAMS = config_dict.ConfigDict(
    dict(
        activation_function='relu',
        optimizer='adam',
        opt_hparams={
            'beta1': .9,
            'beta2': .98,
            'epsilon': 1e-9,
            'weight_decay': 0.0
        },
        batch_size=256,
        eval_batch_size=128,
        l2_decay_factor=1e-6,
        l2_decay_rank_threshold=0,
        use_shallue_label_smoothing=False,
        rng_seed=-1,
        model_dtype='float32',
        grad_clip=10.0,
        num_lstm_layers=4,
        num_ffn_layers=3,
        encoder_dim=512,
        freq_mask_count=2,
        freq_mask_max_bins=27,
        time_mask_count=10,
        time_mask_max_frames=40,
        time_mask_max_ratio=0.05,
        time_masks_per_frame=0.0,
        use_dynamic_time_mask_max_frames=True,
        use_specaug=True,
        input_dropout_rate=0.1,
        feed_forward_dropout_rate=0.1,
        enable_residual_connections=False,
        enable_decoder_layer_norm=False,
        bidirectional=True,
        total_accumulated_batch_size=None,
        enable_subsampling_batchnorm=False,
        enable_synced_batchnorm=False,
        enable_ffn_relu=False))

@struct.dataclass
class DeepspeechConfig:
  """Global hyperparameters used to minimize obnoxious kwarg plumbing."""
  vocab_size: int = 0
  dtype: Any = jnp.float32
  encoder_dim: int = 0
  num_lstm_layers: int = 3
  num_ffn_layers: int = 2
  conv_subsampling_factor: int = 2
  conv_subsampling_layers: int = 2
  train: bool = False
  use_specaug: bool = False
  freq_mask_count: int = 1
  freq_mask_max_bins: int = 15
  time_mask_count: int = 1
  time_mask_max_frames: int = 50
  time_mask_max_ratio: float = 1.0
  time_masks_per_frame: float = 0.0
  use_dynamic_time_mask_max_frames: bool = False
  batch_norm_momentum: float = 0.999
  batch_norm_epsilon: float = 0.001
  input_dropout_rate: float = 0.1
  feed_forward_dropout_rate: float = 0.1
  enable_residual_connections: bool = False
  enable_decoder_layer_norm: bool = False
  bidirectional: bool = False
  enable_subsampling_batchnorm: bool = False
  enable_synced_batchnorm: bool = False
  enable_ffn_relu: bool = False

class Subsample(nn.Module):
  """Module to perform strided convolution in order to subsample inputs.

  Attributes:
    encoder_dim: model dimension of conformer.
    input_dropout_rate: dropout rate for inputs.
  """
  config: DeepspeechConfig

  @nn.compact
  def __call__(self, inputs, output_paddings, train):
    config = self.config
    outputs = jnp.expand_dims(inputs, axis=-1)

    outputs, output_paddings = Conv2dSubsampling(
        encoder_dim=config.encoder_dim,
        dtype=config.dtype,
        batch_norm_momentum=config.batch_norm_momentum,
        batch_norm_epsilon=config.batch_norm_epsilon,
        input_channels=1,
        output_channels=config.encoder_dim,
        enable_batchnorm=config.enable_subsampling_batchnorm,
        enable_synced_batchnorm=config.enable_synced_batchnorm)(outputs,
                                                                output_paddings,
                                                                train)

    outputs, output_paddings = Conv2dSubsampling(
        encoder_dim=config.encoder_dim,
        dtype=config.dtype,
        batch_norm_momentum=config.batch_norm_momentum,
        batch_norm_epsilon=config.batch_norm_epsilon,
        input_channels=config.encoder_dim,
        output_channels=config.encoder_dim,
        enable_batchnorm=config.enable_subsampling_batchnorm,
        enable_synced_batchnorm=config.enable_synced_batchnorm)(outputs,
                                                                output_paddings,
                                                                train)

    batch_size, subsampled_lengths, subsampled_dims, channels = outputs.shape

    outputs = jnp.reshape(
        outputs, (batch_size, subsampled_lengths, subsampled_dims * channels))

    outputs = nn.Dense(
        config.encoder_dim,
        use_bias=True,
        kernel_init=nn.initializers.xavier_uniform())(outputs)

    outputs = nn.Dropout(
        rate=config.input_dropout_rate, deterministic=not train)(outputs)

    return outputs, output_paddings

class Conv2dSubsampling(nn.Module):
  """Helper module used in Subsample layer.

  1) Performs strided convolution over inputs and then applies non-linearity.
  2) Also performs strided convolution over input_paddings to return the correct
  paddings for downstream layers.
  """
  input_channels: int = 0
  output_channels: int = 0
  filter_stride: List[int] = (2, 2)
  padding: str = 'SAME'
  encoder_dim: int = 0
  dtype: Any = jnp.float32
  batch_norm_momentum: float = 0.999
  batch_norm_epsilon: float = 0.001
  enable_batchnorm: bool = False
  enable_synced_batchnorm: bool = False

  def setup(self):
    self.filter_shape = (3, 3, self.input_channels, self.output_channels)
    self.kernel = self.param('kernel', nn.initializers.xavier_uniform(),
                             self.filter_shape)
    self.bias = self.param('bias', lambda rng, s: jnp.zeros(s, jnp.float32),
                           self.output_channels)

  @nn.compact
  def __call__(self, inputs, paddings, train):
    # Computing strided convolution to subsample inputs.
    feature_group_count = inputs.shape[3] // self.filter_shape[2]
    outputs = jax.lax.conv_general_dilated(
        lhs=inputs,
        rhs=self.kernel,
        window_strides=self.filter_stride,
        padding=self.padding,
        rhs_dilation=(1, 1),
        dimension_numbers=('NHWC', 'HWIO', 'NHWC'),
        feature_group_count=feature_group_count)

    outputs += jnp.reshape(self.bias, (1,) * (outputs.ndim - 1) + (-1,))

    if self.enable_batchnorm:
      outputs = BatchNorm(self.encoder_dim, self.dtype,
                          self.batch_norm_momentum, self.batch_norm_epsilon,
                          self.enable_synced_batchnorm)(
                              outputs, input_paddings=None, train=train)

    outputs = nn.relu(outputs)

    # Computing correct paddings post input convolution.
    input_length = paddings.shape[1]
    stride = self.filter_stride[0]

    pad_len = (input_length + stride - 1) // stride * stride - input_length
    out_padding = jax.lax.conv_general_dilated(
        lhs=paddings[:, :, None],
        rhs=jnp.ones([1, 1, 1]),
        window_strides=self.filter_stride[:1],
        padding=[(0, pad_len)],
        dimension_numbers=('NHC', 'HIO', 'NHC'))
    out_padding = jnp.squeeze(out_padding, axis=-1)

    # Mask outputs by correct paddings to ensure padded elements in inputs map
    # to padded value in outputs.
    outputs = outputs * (1.0 -
                         jnp.expand_dims(jnp.expand_dims(out_padding, -1), -1))

    return outputs, out_padding

class FeedForwardModule(nn.Module):
  """Feedforward block of conformer layer."""
  config: DeepspeechConfig

  @nn.compact
  def __call__(self, inputs, input_paddings=None, train=False):
    padding_mask = jnp.expand_dims(1 - input_paddings, -1)
    config = self.config

    inputs = BatchNorm(config.encoder_dim, config.dtype,
                       config.batch_norm_momentum, config.batch_norm_epsilon,
                       config.enable_synced_batchnorm)(inputs, input_paddings,
                                                       train)
    inputs = nn.Dense(
        config.encoder_dim,
        use_bias=True,
        kernel_init=nn.initializers.xavier_uniform())(inputs)

    if config.enable_ffn_relu:
      inputs = nn.relu(inputs)
    inputs *= padding_mask

    inputs = nn.Dropout(rate=config.feed_forward_dropout_rate)(
        inputs, deterministic=not train)

    return inputs

class LayerNorm(nn.Module):
  """Module implementing layer normalization.

  This implementation is same as in this paper:
  https://arxiv.org/pdf/1607.06450.pdf.

  note: we multiply normalized inputs by (1 + scale) and initialize scale to
  zeros, this differs from default flax implementation of multiplying by scale
  and initializing to ones.
  """
  dim: int = 0
  epsilon: float = 1e-6

  def setup(self):
    self.scale = self.param('scale', nn.initializers.zeros, [self.dim])
    self.bias = self.param('bias', nn.initializers.zeros, [self.dim])

  @nn.compact
  def __call__(self, inputs):
    mean = jnp.mean(inputs, axis=-1, keepdims=True)
    var = jnp.mean(jnp.square(inputs - mean), axis=-1, keepdims=True)

    normed_inputs = (inputs - mean) * jax.lax.rsqrt(var + self.epsilon)
    normed_inputs *= (1 + self.scale)
    normed_inputs += self.bias

    return normed_inputs

class BatchNorm(nn.Module):
  """Implements batch norm respecting input paddings.

  This implementation takes into account input padding by masking inputs before
  computing mean and variance.

  This is inspired by lingvo jax implementation of BatchNorm:
  https://github.com/tensorflow/lingvo/blob/84b85514d7ad3652bc9720cb45acfab08604519b/lingvo/jax/layers/normalizations.py#L92

  and the corresponding defaults for momentum and epsilon have been copied over
  from lingvo.
  """
  encoder_dim: int = 0
  dtype: Any = jnp.float32
  batch_norm_momentum: float = 0.999
  batch_norm_epsilon: float = 0.001
  enable_synced_batchnorm: bool = False

  def setup(self):
    dim = self.encoder_dim
    dtype = self.dtype

    self.ra_mean = self.variable('batch_stats', 'mean',
                                 lambda s: jnp.zeros(s, dtype), dim)
    self.ra_var = self.variable('batch_stats', 'var',
                                lambda s: jnp.ones(s, dtype), dim)

    self.gamma = self.param('scale', nn.initializers.zeros, dim, dtype)
    self.beta = self.param('bias', nn.initializers.zeros, dim, dtype)

  def _get_default_paddings(self, inputs):
    """Gets the default paddings for an input."""
    in_shape = list(inputs.shape)
    in_shape[-1] = 1

    return jnp.zeros(in_shape, dtype=inputs.dtype)

  @nn.compact
  def __call__(self, inputs, input_paddings=None, train=False):
    rank = inputs.ndim
    reduce_over_dims = list(range(0, rank - 1))

    if input_paddings is None:
      padding = self._get_default_paddings(inputs)
    else:
      padding = jnp.expand_dims(input_paddings, -1)

    momentum = self.batch_norm_momentum
    epsilon = self.batch_norm_epsilon

    if train:
      mask = 1.0 - padding
      sum_v = jnp.sum(inputs * mask, axis=reduce_over_dims, keepdims=True)
      count_v = jnp.sum(
          jnp.ones_like(inputs) * mask, axis=reduce_over_dims, keepdims=True)

      if self.enable_synced_batchnorm:
        sum_v = jax.lax.psum(sum_v, axis_name='batch')
        count_v = jax.lax.psum(count_v, axis_name='batch')

      count_v = jnp.maximum(count_v, 1.0)
      mean = sum_v / count_v

      sum_vv = jnp.sum(
          (inputs - mean) * (inputs - mean) * mask,
          axis=reduce_over_dims,
          keepdims=True)

      if self.enable_synced_batchnorm:
        sum_vv = jax.lax.psum(sum_vv, axis_name='batch')

      var = sum_vv / count_v

      self.ra_mean.value = momentum * self.ra_mean.value + (1 - momentum) * mean
      self.ra_var.value = momentum * self.ra_var.value + (1 - momentum) * var
    else:
      mean = self.ra_mean.value
      var = self.ra_var.value

    inv = (1 + self.gamma) / jnp.sqrt(var + epsilon)

    bn_output = (inputs - mean) * inv + self.beta
    bn_output *= 1.0 - padding

    return bn_output

@jax.vmap
def flip_sequences(inputs: Array, lengths: Array) -> Array:
  """Flips a sequence of inputs along the time dimension.

  This function can be used to prepare inputs for the reverse direction of a
  bidirectional LSTM. It solves the issue that, when naively flipping multiple
  padded sequences stored in a matrix, the first elements would be padding
  values for those sequences that were padded. This function keeps the padding
  at the end, while flipping the rest of the elements.

  Example:
  ```python
  inputs = [[1, 0, 0],
            [2, 3, 0]
            [4, 5, 6]]
  lengths = [1, 2, 3]
  flip_sequences(inputs, lengths) = [[1, 0, 0],
                                     [3, 2, 0],
                                     [6, 5, 4]]
  ```

  Args:
    inputs: An array of input IDs <int>[batch_size, seq_length].
    lengths: The length of each sequence <int>[batch_size].

  Returns:
    An ndarray with the flipped inputs.
  """
  # Compute the indices to put the inputs in flipped order as per above example.
  max_length = inputs.shape[0]
  idxs = (jnp.arange(max_length - 1, -1, -1) + lengths) % max_length
  return inputs[idxs]

class GenericRNNSequenceEncoder(nn.Module):
  """Encodes a single sequence using any RNN cell, for example `nn.LSTMCell`.

  The sequence can be encoded left-to-right (default) or right-to-left (by
  calling the module with reverse=True). Regardless of encoding direction,
  outputs[i, j, ...] is the representation of inputs[i, j, ...].

  Attributes:
    hidden_size: The hidden size of the RNN cell.
    cell_type: The RNN cell module to use, for example, `nn.LSTMCell`.
    cell_kwargs: Optional keyword arguments for the recurrent cell.
    recurrent_dropout_rate: The dropout to apply across time steps. If this is
      greater than zero, you must use an RNN cell that implements
      `RecurrentDropoutCell` such as RecurrentDropoutOptimizedLSTMCell.
  """
  hidden_size: int
  cell_type: Type[nn.recurrent.RNNCellBase]
  cell_kwargs: Mapping[str, Any] = flax.core.FrozenDict()
  recurrent_dropout_rate: float = 0.0

  def setup(self):
    self.cell = self.cell_type(**self.cell_kwargs)

  @functools.partial(  # Repeatedly calls the below method to encode the inputs.
      nn.transforms.scan,
      variable_broadcast='params',
      in_axes=(1, flax.core.axes_scan.broadcast, flax.core.axes_scan.broadcast),
      out_axes=1,
      split_rngs={'params': False})
  def unroll_cell(self, cell_state: StateType, inputs: Array,
                  recurrent_dropout_mask: Optional[Array], deterministic: bool):
    """Unrolls a recurrent cell over an input sequence.

    Args:
      cell_state: The initial cell state, shape: <float32>[batch_size,
        hidden_size] (or an n-tuple thereof).
      inputs: The input sequence. <float32>[batch_size, seq_len, input_dim].
      recurrent_dropout_mask: An optional recurrent dropout mask to apply in
        between time steps. <float32>[batch_size, hidden_size].
      deterministic: Disables recurrent dropout when set to True.

    Returns:
      The cell state after processing the complete sequence (including padding),
      and a tuple with all intermediate cell states and cell outputs.
    """
    # We do not directly scan the cell itself, since it only returns the output.
    # This returns both the state and the output, so we can slice out the
    # correct final states later.
    new_cell_state, output = self.cell(cell_state, inputs)
    return new_cell_state, (new_cell_state, output)

  def __call__(self,
               inputs: Array,
               lengths: Array,
               initial_state: StateType,
               reverse: bool = False,
               deterministic: bool = False):
    """Unrolls the RNN cell over the inputs.

    Arguments:
      inputs: A batch of sequences. Shape: <float32>[batch_size, seq_len,
        input_dim].
      lengths: The lengths of the input sequences.
      initial_state: The initial state for the RNN cell. Shape: [batch_size,
        hidden_size].
      reverse: Process the inputs in reverse order, and reverse the outputs.
        This means that the outputs still correspond to the order of the inputs,
        but their contexts come from the right, and not from the left.
      deterministic: Disables recurrent dropout if set to True.

    Returns:
      The encoded sequence of inputs, shaped <float32>[batch_size, seq_len,
        hidden_size], as well as the final hidden states of the RNN cell. For an
        LSTM cell the final states are a tuple (c, h), each shaped <float32>[
          batch_size, hidden_size].
    """
    if reverse:
      inputs = flip_sequences(inputs, lengths)

    recurrent_dropout_mask = None
    _, (cell_states, outputs) = self.unroll_cell(initial_state, inputs,
                                                 recurrent_dropout_mask,
                                                 deterministic)
    final_state = jax.tree_map(
        lambda x: x[jnp.arange(inputs.shape[0]), lengths - 1], cell_states)

    if reverse:
      outputs = flip_sequences(outputs, lengths)

    return outputs, final_state

class GenericRNN(nn.Module):
  """Generic RNN class.

  This provides generic RNN functionality to encode sequences with any RNN cell.
  The class provides unidirectional and bidirectional layers, and these are
  stacked when asking for multiple layers.

  This class be used to create a specific RNN class such as LSTM or GRU.

  Attributes:
    cell_type: An RNN cell class to use, e.g., `flax.linen.LSTMCell`.
    hidden_size: The size of each recurrent cell.
    num_layers: The number of stacked recurrent layers. The output of the first
      layer, with optional dropout applied, feeds into the next layer.
    dropout_rate: Dropout rate to be applied between LSTM layers. Only applies
      when num_layers > 1.
    recurrent_dropout_rate: Dropout rate to be applied on the hidden state at
      each time step repeating the same dropout mask.
    bidirectional: Process the sequence left-to-right and right-to-left and
      concatenate the outputs from the two directions.
    cell_kwargs: Optional keyword arguments to instantiate the cell with.
  """
  cell_type: Type[nn.recurrent.RNNCellBase]
  hidden_size: int
  num_layers: int = 1
  dropout_rate: float = 0.
  recurrent_dropout_rate: float = 0.
  bidirectional: bool = False
  cell_kwargs: Mapping[str, Any] = flax.core.FrozenDict()

  @nn.compact
  def __call__(
      self,
      inputs: Array,
      lengths: Array,
      initial_states: Optional[Sequence[StateType]] = None,
      deterministic: bool = False
  ) -> Tuple[Array, Sequence[StateType]]:
    """Processes the input sequence using the recurrent cell.

    Args:
      inputs: The input sequence <float32>[batch_size, sequence_length, ...]
      lengths: The lengths of each sequence in the batch. <int64>[batch_size]
      initial_states: The initial states for the cells. You must provide
        `num_layers` initial states (when using bidirectional, `num_layers *
        2`).
        These must be ordered in the following way: (layer_0_forward,
          layer_0_backward, layer_1_forward, layer_1_backward, ...). If None,
          all initial states will be initialized with zeros.
      deterministic: Disables dropout between layers when set to True.
    Returns:
      The sequence of all outputs for the final layer, and a list of final
      states for each cell and direction. Directions are alternated (first
      forward, then backward, if bidirectional). For example for a bidirectional
      cell this would be: layer 1 forward, layer 1 backward, layer 2 forward,
      layer 2 backward, etc..
      For some cells like LSTMCell a state consists of an (c, h) tuple, while
      for others cells it only contains a single vector (h,).
    """
    batch_size = inputs.shape[0]
    final_states = []
    num_directions = 2 if self.bidirectional else 1
    num_cells = self.num_layers * num_directions

    # Construct initial states.
    if initial_states is None:  # Initialize with zeros.
      rng = jax.random.PRNGKey(0)
      initial_states = [
          self.cell_type.initialize_carry(rng, (batch_size,), self.hidden_size)
          for _ in range(num_cells)
      ]
    if len(initial_states) != num_cells:
      raise ValueError(
          f'Please provide {self.num_cells} (`num_layers`, *2 if bidirectional) '
          f'initial states.')

    # For each layer, apply the forward and optionally the backward RNN cell.
    cell_idx = 0
    for _ in range(self.num_layers):
      # Unroll an RNN cell (forward direction) for this layer.
      outputs, final_state = GenericRNNSequenceEncoder(
          cell_type=self.cell_type,
          cell_kwargs=self.cell_kwargs,
          hidden_size=self.hidden_size,
          recurrent_dropout_rate=self.recurrent_dropout_rate,
          name=f'{self.name}SequenceEncoder_{cell_idx}')(
              inputs,
              lengths,
              initial_state=initial_states[cell_idx],
              deterministic=deterministic)
      final_states.append(final_state)
      cell_idx += 1

      # Unroll an RNN cell (backward direction) for this layer.
      if self.bidirectional:
        backward_outputs, backward_final_state = GenericRNNSequenceEncoder(
            cell_type=self.cell_type,
            cell_kwargs=self.cell_kwargs,
            hidden_size=self.hidden_size,
            recurrent_dropout_rate=self.recurrent_dropout_rate,
            name=f'{self.name}SequenceEncoder_{cell_idx}')(
                inputs,
                lengths,
                initial_state=initial_states[cell_idx],
                reverse=True,
                deterministic=deterministic)
        outputs = jnp.concatenate([outputs, backward_outputs], axis=-1)
        final_states.append(backward_final_state)
        cell_idx += 1

      inputs = outputs

    return outputs, final_states

class LSTM(nn.Module):
  """LSTM.

  Attributes:
    hidden_size: The size of each recurrent cell.
    num_layers: The number of stacked recurrent layers. The output of the first
      layer, with optional dropout applied, feeds into the next layer.
    dropout_rate: Dropout rate to be applied between LSTM layers. Only applies
      when num_layers > 1.
    recurrent_dropout_rate: Dropout rate to be applied on the hidden state at
      each time step repeating the same dropout mask.
    bidirectional: Process the sequence left-to-right and right-to-left and
      concatenate the outputs from the two directions.
    cell_type: The LSTM cell class to use. Default:
      `flax.linen.OptimizedLSTMCell`. If you use hidden_size of >2048, consider
      using `flax.linen.LSTMCell` instead, since the optimized LSTM cell works
      best for hidden sizes up to 2048.
    cell_kwargs: Optional keyword arguments to instantiate the cell with.
  """
  hidden_size: int
  num_layers: int = 1
  dropout_rate: float = 0.
  recurrent_dropout_rate: float = 0.
  bidirectional: bool = False
  cell_type: Any = nn.OptimizedLSTMCell
  cell_kwargs: Mapping[str, Any] = flax.core.FrozenDict()

  @nn.compact
  def __call__(
      self,
      inputs: Array,
      lengths: Array,
      initial_states: Optional[Sequence[StateType]] = None,
      deterministic: bool = False) -> Tuple[Array, Sequence[StateType]]:
    """Processes an input sequence with an LSTM cell.

    Example usage:
    ```
      inputs = np.random.normal(size=(2, 3, 4))
      lengths = np.array([1, 3])
      outputs, final_states = LSTM(hidden_size=10).apply(rngs, inputs, lengths)
    ```

    Args:
      inputs: The input sequence <float32>[batch_size, sequence_length, ...]
      lengths: The lengths of each sequence in the batch. <int64>[batch_size]
      initial_states: The initial states for the cells. You must provide
        `num_layers` initial states (when using bidirectional, `num_layers *
        2`). These must be ordered in the following way: (layer_0_forward,
          layer_0_backward, layer_1_forward, layer_1_backward, ...). If None,
          all initial states will be initialized with zeros.
      deterministic: Disables dropout between layers when set to True.

    Returns:
      The sequence of all outputs for the final layer, and a list of final
      states (h, c) for each cell and direction, ordered first by layer number
      and then by direction (first forward, then backward, if bidirectional).
    """
    return GenericRNN(
        cell_type=self.cell_type,
        hidden_size=self.hidden_size,
        num_layers=self.num_layers,
        dropout_rate=self.dropout_rate,
        recurrent_dropout_rate=self.recurrent_dropout_rate,
        bidirectional=self.bidirectional,
        cell_kwargs=self.cell_kwargs,
        name='LSTM')(
            inputs,
            lengths,
            initial_states=initial_states,
            deterministic=deterministic)

class BatchRNN(nn.Module):
  """Implements a single conformer encoder layer.

  High level overview:
  """
  config: DeepspeechConfig

  @nn.compact
  def __call__(self, inputs, input_paddings, train):
    config = self.config

    inputs = BatchNorm(config.encoder_dim, config.dtype,
                       config.batch_norm_momentum, config.batch_norm_epsilon,
                       config.enable_synced_batchnorm)(inputs, input_paddings,
                                                       train)
    lengths = jnp.sum(1 - input_paddings, axis=-1, dtype=jnp.int32)

    output, _ = LSTM(
        hidden_size=config.encoder_dim // 2, bidirectional=config.bidirectional,
        num_layers=1)(inputs, lengths)

    return output

class DeepSpeechEncoderDecoder(nn.Module):
  """Conformer (encoder + decoder) block.

  Takes audio input signals and outputs probability distribution over vocab size
  for each time step. The output is then fed into a CTC loss which eliminates
  the need for alignment with targets.
  """
  config: DeepspeechConfig

  def setup(self):
    config = self.config
    self.specaug = spectrum_augmenter.SpecAug(
        freq_mask_count=config.freq_mask_count,
        freq_mask_max_bins=config.freq_mask_max_bins,
        time_mask_count=config.time_mask_count,
        time_mask_max_frames=config.time_mask_max_frames,
        time_mask_max_ratio=config.time_mask_max_ratio,
        time_masks_per_frame=config.time_masks_per_frame,
        use_dynamic_time_mask_max_frames=config.use_dynamic_time_mask_max_frames
    )

  @nn.compact
  def __call__(self, inputs, input_paddings, train):
    config = self.config

    outputs = inputs
    output_paddings = input_paddings

    # Compute normalized log mel spectrograms from input audio signal.
    preprocessing_config = preprocessor.LibrispeechPreprocessingConfig()
    outputs, output_paddings = preprocessor.MelFilterbankFrontend(
        preprocessing_config,
        per_bin_mean=preprocessor.LIBRISPEECH_MEAN_VECTOR,
        per_bin_stddev=preprocessor.LIBRISPEECH_STD_VECTOR)(outputs,
                                                            output_paddings)

    # Ablate random parts of input along temporal and frequency dimension
    # following the specaug procedure in https://arxiv.org/abs/1904.08779.
    if config.use_specaug and train:
      outputs, output_paddings = self.specaug(outputs, output_paddings)

    # Subsample input by a factor of 4 by performing strided convolutions.
    outputs, output_paddings = Subsample(
        config=config)(outputs, output_paddings, train)

    # Run the lstm layers.
    for _ in range(config.num_lstm_layers):
      if config.enable_residual_connections:
        outputs = outputs + BatchRNN(config)(outputs, output_paddings, train)
      else:
        outputs = BatchRNN(config)(outputs, output_paddings, train)

    for _ in range(config.num_ffn_layers):
      if config.enable_residual_connections:
        outputs = outputs + FeedForwardModule(config=self.config)(
            outputs, output_paddings, train)
      else:
        outputs = FeedForwardModule(config=self.config)(outputs,
                                                        output_paddings, train)

    # Run the decoder which in this case is a trivial projection layer.
    if config.enable_decoder_layer_norm:
      outputs = LayerNorm(config.encoder_dim)(outputs)

    outputs = nn.Dense(
        config.vocab_size,
        use_bias=True,
        kernel_init=nn.initializers.xavier_uniform())(outputs)

    return outputs, output_paddings

class DeepSpeechModel(base_model.BaseModel):
  """Conformer model that takes in log mel spectrograms as inputs.

  outputs probability distribution over vocab size for each time step.
  """

  # Adapted form lingvo's greedy decoding logic here:
  # https://github.com/tensorflow/lingvo/blob/2ee26814c57b7dcead3f0382170f2f3da006f810/lingvo/jax/layers/ctc_objectives.py#L138
  def sequence_mask(self, lengths, maxlen):
    batch_size = lengths.shape[0]
    a = jnp.ones([batch_size, maxlen])
    b = jnp.cumsum(a, axis=-1)
    c = jnp.less_equal(b, lengths[:, jnp.newaxis]).astype(lengths.dtype)
    return c

  def compute_loss(self, logits, logit_paddings, labels, label_paddings):
    logprobs = nn.log_softmax(logits)
    per_seq_loss = self.loss_fn(logprobs, logit_paddings, labels,
                                label_paddings)
    normalizer = jnp.sum(1 - label_paddings)

    normalized_loss = jnp.sum(per_seq_loss) / jnp.maximum(normalizer, 1)
    return normalized_loss

  def collapse_and_remove_blanks(self, labels, seq_length, blank_id: int = 0):
    b, t = labels.shape
    # Zap out blank.
    blank_mask = 1 - jnp.equal(labels, blank_id)
    labels = (labels * blank_mask).astype(labels.dtype)

    # Mask labels that don't equal previous label.
    label_mask = jnp.concatenate([
        jnp.ones_like(labels[:, :1], dtype=jnp.int32),
        jnp.not_equal(labels[:, 1:], labels[:, :-1])
    ],
                                 axis=1)

    # Filter labels that aren't in the original sequence.
    maxlen = labels.shape[1]
    seq_mask = self.sequence_mask(seq_length, maxlen=maxlen)
    label_mask = label_mask * seq_mask

    # Remove repetitions from the labels.
    ulabels = label_mask * labels

    # Count masks for new sequence lengths.
    label_mask = jnp.not_equal(ulabels, 0).astype(labels.dtype)
    new_seq_len = jnp.sum(label_mask, axis=1)

    # Mask indexes based on sequence length mask.
    new_maxlen = maxlen
    idx_mask = self.sequence_mask(new_seq_len, maxlen=new_maxlen)

    # Flatten everything and mask out labels to keep and sparse indices.
    flat_labels = jnp.reshape(ulabels, [-1])
    flat_idx_mask = jnp.reshape(idx_mask, [-1])

    indices = jnp.nonzero(flat_idx_mask, size=b * t)[0]
    values = jnp.nonzero(flat_labels, size=b * t)[0]
    updates = jnp.take_along_axis(flat_labels, values, axis=-1)

    # Scatter to flat shape.
    flat = jnp.zeros(flat_idx_mask.shape).astype(labels.dtype)
    flat = flat.at[indices].set(updates)
    # 0'th position in the flat array gets clobbered by later padded updates,
    # so reset it here to its original value
    flat = flat.at[0].set(updates[0])

    # Reshape back to square batch.
    batch_size = labels.shape[0]
    new_shape = [batch_size, new_maxlen]
    return (jnp.reshape(flat, new_shape).astype(labels.dtype),
            new_seq_len.astype(seq_length.dtype))

  def greedy_decode(self, logits, logit_paddings):
    per_frame_max = jnp.argmax(logits, axis=-1)
    seqlen = jnp.sum(1.0 - logit_paddings, axis=-1)
    hyp, _ = self.collapse_and_remove_blanks(per_frame_max, seqlen, blank_id=0)
    hyp_paddings = jnp.equal(hyp, 0).astype(jnp.int32)
    return hyp, hyp_paddings

  def evaluate_batch(self, params, batch_stats, batch):
    """Evaluates cross_entopy on the given batch."""

    logits, logit_paddings = self.flax_module.apply(
        {
            'params': params,
            'batch_stats': batch_stats
        },
        batch['inputs'],
        batch['input_paddings'],
        train=False,
        mutable=False)

    labels = batch['targets']
    label_paddings = batch['target_paddings']

    normalized_loss = self.compute_loss(logits, logit_paddings, labels,
                                        label_paddings)
    hyps, hyp_paddings = self.greedy_decode(logits, logit_paddings)

    return self.metrics_bundle.gather_from_model_output(
        normalized_loss=normalized_loss,
        hyps=hyps,
        hyp_paddings=hyp_paddings,
        targets=labels,
        target_paddings=label_paddings,
        axis_name='batch')

  def training_cost(self, params, batch, batch_stats=None, dropout_rng=None):
    """Return CTC loss."""

    # For more information on flax.linen.Module.apply, see the docs at
    # https://flax.readthedocs.io/en/latest/flax.linen.html#flax.linen.Module.apply.
    (outputs, output_paddings), new_batch_stats = self.flax_module.apply(
        {
            'params': params,
            'batch_stats': batch_stats
        },
        batch['inputs'],
        batch['input_paddings'],
        rngs={'dropout': dropout_rng},
        mutable=['batch_stats'],
        train=True)

    labels = batch['targets']
    label_paddings = batch['target_paddings']

    normalized_loss = self.compute_loss(outputs, output_paddings, labels,
                                        label_paddings)
    return normalized_loss, new_batch_stats

  def build_flax_module(self):
    config = DeepspeechConfig(
        vocab_size=self.hps.output_shape[1],
        encoder_dim=self.hps.encoder_dim,
        num_lstm_layers=self.hps.num_lstm_layers,
        num_ffn_layers=self.hps.num_ffn_layers,
        freq_mask_count=self.hps.freq_mask_count,
        freq_mask_max_bins=self.hps.freq_mask_max_bins,
        time_mask_count=self.hps.time_mask_count,
        time_mask_max_frames=self.hps.time_mask_max_frames,
        time_mask_max_ratio=self.hps.time_mask_max_ratio,
        time_masks_per_frame=self.hps.time_masks_per_frame,
        use_dynamic_time_mask_max_frames=self.hps
        .use_dynamic_time_mask_max_frames,
        use_specaug=self.hps.use_specaug,
        input_dropout_rate=self.hps.input_dropout_rate,
        feed_forward_dropout_rate=self.hps.feed_forward_dropout_rate,
        enable_residual_connections=self.hps.enable_residual_connections,
        enable_decoder_layer_norm=self.hps.enable_decoder_layer_norm,
        bidirectional=self.hps.bidirectional,
        enable_subsampling_batchnorm=self.hps.enable_subsampling_batchnorm,
        enable_synced_batchnorm=self.hps.enable_synced_batchnorm,
        enable_ffn_relu=self.hps.enable_ffn_relu)
    module = DeepSpeechEncoderDecoder(config)

    return module

  def get_fake_inputs(self, hps):
    """Helper method solely for the purpose of initializing the model."""
    dummy_inputs = [
        jnp.zeros((hps.batch_size, *x), dtype=hps.model_dtype)
        for x in hps.input_shape
    ]
    return dummy_inputs


text:
#include "config.h"

#if ENABLE(SVG) && ENABLE(FILTERS)
#include "SVGFEMergeNodeElement.h"

#include "Attribute.h"
#include "RenderObject.h"
#include "RenderSVGResource.h"
#include "SVGElementInstance.h"
#include "SVGFilterElement.h"
#include "SVGFilterPrimitiveStandardAttributes.h"
#include "SVGNames.h"

namespace WebCore {

// Animated property definitions
DEFINE_ANIMATED_STRING(SVGFEMergeNodeElement, SVGNames::inAttr, In1, in1)

BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEMergeNodeElement)
    REGISTER_LOCAL_ANIMATED_PROPERTY(in1)
END_REGISTER_ANIMATED_PROPERTIES

inline SVGFEMergeNodeElement::SVGFEMergeNodeElement(const QualifiedName& tagName, Document* document)
    : SVGElement(tagName, document)
{
    ASSERT(hasTagName(SVGNames::feMergeNodeTag));
    registerAnimatedPropertiesForSVGFEMergeNodeElement();
}

PassRefPtr<SVGFEMergeNodeElement> SVGFEMergeNodeElement::create(const QualifiedName& tagName, Document* document)
{
    return adoptRef(new SVGFEMergeNodeElement(tagName, document));
}

bool SVGFEMergeNodeElement::isSupportedAttribute(const QualifiedName& attrName)
{
    DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
    if (supportedAttributes.isEmpty())
        supportedAttributes.add(SVGNames::inAttr);
    return supportedAttributes.contains<QualifiedName, SVGAttributeHashTranslator>(attrName);
}

void SVGFEMergeNodeElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
{
    if (!isSupportedAttribute(name)) {
        SVGElement::parseAttribute(name, value);
        return;
    }

    if (name == SVGNames::inAttr) {
        setIn1BaseValue(value);
        return;
    }

    ASSERT_NOT_REACHED();
}

void SVGFEMergeNodeElement::svgAttributeChanged(const QualifiedName& attrName)
{
    if (!isSupportedAttribute(attrName)) {
        SVGElement::svgAttributeChanged(attrName);
        return;
    }

    SVGElementInstance::InvalidationGuard invalidationGuard(this);

    if (attrName == SVGNames::inAttr) {
        invalidateFilterPrimitiveParent(this);
        return;
    }

    ASSERT_NOT_REACHED();
}

}

#endif // ENABLE(SVG)

redpajama_set_name: RedPajamaGithub


text:
	<a class="noUnderlineAnchor" href="#app/<%= type %>/addInstance">

			<%= name %>

        	<%= name %>

				<img  src="<%= picture === null ? 'pics/TSTV.png' : 'pics/' + picture %>" alt="" width="69" height="42"/>

 <%= name %> suitable for entry tier configs. <%= name %> description will go here;


text:
/*
 * https://html.spec.whatwg.org/multipage/embedded-content.html#texttrackcuelist
 *
 * interface TextTrackCueList {
 *   readonly attribute unsigned long length;
 *   getter TextTrackCue (unsigned long index);
 *   TextTrackCue? getCueById(DOMString id);
 * };
 */

vjs.TextTrackCueList = function(cues) {
  var list = this,
      prop;

  if (vjs.IS_IE8) {
    list = document.createElement('custom');

    for (prop in vjs.TextTrackCueList.prototype) {
      list[prop] = vjs.TextTrackCueList.prototype[prop];
    }
  }

  vjs.TextTrackCueList.prototype.setCues_.call(list, cues);

  Object.defineProperty(list, 'length', {
    get: function() {
      return this.length_;
    }
  });

  if (vjs.IS_IE8) {
    return list;
  }
};

vjs.TextTrackCueList.prototype.setCues_ = function(cues) {
  var oldLength = this.length || 0,
      i = 0,
      l = cues.length,
      defineProp;

  this.cues_ = cues;
  this.length_ = cues.length;

  defineProp = function(i) {
    if (!(''+i in this)) {
      Object.defineProperty(this, '' + i, {
        get: function() {
          return this.cues_[i];
        }
      });
    }
  };

  if (oldLength < l) {
    i = oldLength;
    for(; i < l; i++) {
      defineProp.call(this, i);
    }
  }
};

vjs.TextTrackCueList.prototype.getCueById = function(id) {
  var i = 0,
      l = this.length,
      result = null,
      cue;

  for (; i < l; i++) {
    cue = this[i];
    if (cue.id === id) {
      result = cue;
      break;
    }
  }

  return result;
};


text:
ORM Exceptions
==============

.. automodule:: sqlalchemy.orm.exc
    :show-inheritance:
    :members:


text:
package gw.internal.xml;

import java.io.IOException;
import java.io.OutputStream;

public class TeeOutputStream extends OutputStream {

  private OutputStream _out1;
  private OutputStream _out2;

  public TeeOutputStream( OutputStream out1, OutputStream out2 ) {
    _out1 = out1;
    _out2 = out2;
  }

  @Override
  public void write( int b ) throws IOException {
    _out1.write( b );
    _out2.write( b );
  }

  @Override
  public void write( byte[] b ) throws IOException {
    _out1.write( b );
    _out2.write( b );
  }

  @Override
  public void write( byte[] b, int off, int len ) throws IOException {
    _out1.write( b, off, len );
    _out2.write( b, off, len );
  }

  @Override
  public void flush() throws IOException {
    _out1.flush();
    _out2.flush();
  }

  @Override
  public void close() throws IOException {
    _out1.close();
    _out2.close();
  }

}


text:
  Chesnois-Auboncourt est
       un village
       situé dans le département des Ardennes en Champagne-Ardenne. On dénombrait 153 habitants en 2008.

À proximité de Chesnois-Auboncourt sont positionnées géographiquement les villes de
Saint-Loup-Terrier localisée à 3 km, 160 habitants,
Vaux-Montreuil localisée à 0 km, 103 habitants,
Hagnicourt localisée à 4 km, 59 habitants,
Faissault localisée à 5 km, 198 habitants,
Sorcy-Bauthémont à 4 km, 141 habitants,
Écordal située à 4 km, 299 habitants,
 entre autres. De plus, Chesnois-Auboncourt est située à seulement 25 km de Charleville-Mézières.

Le nombre d'habitations, à Chesnois-Auboncourt, se décomposait en 2011 en cinq appartements et 86 maisons soit
 un marché plutôt équilibré.

Si vous pensez emmenager à Chesnois-Auboncourt, vous pourrez facilement trouver une maison à acheter.


text:
package com.gaoyehua.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * Created by 高业华 on 2016/7/17.
 */
public class HomeTextView extends TextView {

    //在代码中调用
    public HomeTextView(Context context) {
        super(context);
    }
    //在布局文件中调用

    public HomeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    //也是布局文件,多了一个参数

    public HomeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    //设置自定义的TextView

    @Override
    public boolean isFocused() {
        //true获取焦点,false不获取焦点
        return true;
    }
}


text:
/**
 */
package CIM.IEC61970.Informative.MarketOperations;

import CIM.IEC61970.Core.Curve;

/**
 *
 * A representation of the model object 'MW Limit Schedule'.
 *
 *
 *

 * The following features are supported:
 *

 *

 *
{@link CIM.IEC61970.Informative.MarketOperations.MWLimitSchedule#getSecurityConstraintLimit Security Constraint Limit}

 *

 *
 * @see CIM.IEC61970.Informative.MarketOperations.MarketOperationsPackage#getMWLimitSchedule()
 * @model annotation="http://iec.ch/TC57/2009/CIM-schema-cim14 Documentation='Maximum MW and optionally Minimum MW (Y1 and Y2, respectively)'"
 *        annotation="http://langdale.com.au/2005/UML Profile\040documentation='Maximum MW and optionally Minimum MW (Y1 and Y2, respectively)'"
 *        annotation="http://www.eclipse.org/emf/2002/GenModel Documentation='Maximum MW and optionally Minimum MW (Y1 and Y2, respectively)' Profile\040documentation='Maximum MW and optionally Minimum MW (Y1 and Y2, respectively)'"
 * @generated
 */
public interface MWLimitSchedule extends Curve {
	/**
	 * Returns the value of the 'Security Constraint Limit' reference.
	 * It is bidirectional and its opposite is '{@link CIM.IEC61970.Informative.MarketOperations.ContingencyConstraintLimit#getMWLimitSchedules MW Limit Schedules}'.
	 *
	 *

	 * If the meaning of the 'Security Constraint Limit' reference isn't clear,
	 * there really should be more of a description here...
	 *

	 *
	 * @return the value of the 'Security Constraint Limit' reference.
	 * @see #setSecurityConstraintLimit(ContingencyConstraintLimit)
	 * @see CIM.IEC61970.Informative.MarketOperations.MarketOperationsPackage#getMWLimitSchedule_SecurityConstraintLimit()
	 * @see CIM.IEC61970.Informative.MarketOperations.ContingencyConstraintLimit#getMWLimitSchedules
	 * @model opposite="MWLimitSchedules"
	 * @generated
	 */
	ContingencyConstraintLimit getSecurityConstraintLimit();

	/**
	 * Sets the value of the '{@link CIM.IEC61970.Informative.MarketOperations.MWLimitSchedule#getSecurityConstraintLimit Security Constraint Limit}' reference.
	 *
	 *
	 * @param value the new value of the 'Security Constraint Limit' reference.
	 * @see #getSecurityConstraintLimit()
	 * @generated
	 */
	void setSecurityConstraintLimit(ContingencyConstraintLimit value);

} // MWLimitSchedule


text:
CQChartsTest \
 -tsv data/digits.tsv -first_line_header \
 -type boxplot -columns "set=0,value=2,group=1" \
 -properties "outlier.visible=0,options.connected=1" \
 -plot_title "boxplot"


text:
----------------------------------------------------------------------
Copyright (c) 2014, SRI International

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
----------------------------------------------------------------------

# PythonCyc Tutorial

 PythonCyc is a Python interface package to [Pathway Tools](http://brg.ai.sri.com/ptools/), version 18.5
 or above. PythonCyc has been tested with Python 2.6 and IPython on Mac
 OS X, Linux and Microsoft Windows.  Since PythonCyc is based on the programming
 language Python, you must use a Python interpreter to use
 PythonCyc. In the following we assume that you have installed Python
 (we recommend version 2.6 or above, but it most likely work with any
 2.x version).

 For the complete API documentation of PythonCyc, please consult [PythonCyc API](http://pythoncyc.readthedocs.org).

 For the latest news about PythonCyc, please consult the [PythonCyc web page](http://brg.ai.sri.com/ptools/pythoncyc.html).

## Installation

We assume that you have downloaded PythonCyc from
[PythonCyc at GitHub](https://github.com/latendre/PythonCyc).
This may be done by using git cloning or by downloading the zip
file of PythonCyc from GitHub.
The next step is to make PythonCyc accessible from your running Python
interpreter. To do so, install PythonCyc according to one of the
following platforms.

### Mac OS X and Linux

Open a terminal window and change your directory to the location where
you unpacked the PythonCyc package. You must have the file setup.py, and the subdirectory pythoncyc,
in that directory. Assuming that 'shell>'
is the prompt of your current Unix shell, execute the following
command:

shell> sudo python setup.py install

 This may prompt you for your login password because sudo requires
authorization to modify some system directories. This command copies
several files from the pythoncyc subdirectory to other locations on
your computer where Python is installed, byte-compiles these files
and may do other operations depending on the Python installation you
have. No error messages should be reported. In case of errors, make
sure you have installed Python and that it is working.

To test your
PythonCyc installation, please consult the Section [Getting Started](#gettingStarted) in this document.

### Microsoft Windows

On a Microsoft Windows platform, starts a command prompt window using
the start/Accessories menu, then change the directory to the location
where you unpacked the PythonCyc package. You must have the file
<tt>setup.py</tt>, and the subdirectory <tt>pythoncyc</tt>, in that directory .
Then, at the command prompt, execute the following command:

 python setup.py install

This command copies several files from the pythoncyc subdirectory
to other locations on your computer where Python is installed,
byte-compiles these files and may do other operations depending on the
Python installation you have. No error messages should be reported. In case
of errors, make sure you have installed Python and that it is working. To
test your PythonCyc installation, please consult the
Section [Getting Started](#gettingStarted) in this
document.

## Getting Started

 Pathway Tools (version 18.5 and up) must be running on some
computer started with at least the command line option '-python' or
'-python-local-only', which starts the Python server in Pathway
Tools. If '-python' is specified, connections made to Pathway Tools
could come from a remote computer, whereas for '-python-local-only',
no remote computer can connect to the Python server. The option
'-python-local-only' is for added cybersecurity because with that
option no remote computer can access your locally running Pathway Tools
via the Python server. PythonCyc communicates to this running Pathway
Tools server via a socket on port 5008 (default setup).  It is also
recommended to start Pathway Tools with the command line option
'-lisp', so that the connection can be monitored and debugged, if need
be. In summary, we recommend to start Pathway Tools using the following options

./pathway-tools -lisp -python

or for added cybersecurity with the options

./pathway-tools -lisp -python-local-only

To run PythonCyc remotely from Pathway Tools, please
read the Section Remotely Accessing Pathway
Tools to setup PythonCyc to remotely access Pathway Tools.  In the
following, we assume that Pathway Tools is running on the same computer
as Python and that it has the MetaCyc database.

Start a Python interpreter on the same computer as Pathway Tools.
You should get a prompt such as >>>, then enter the following

>>> import pythoncyc

This command imports the PythonCyc module to access its classes,
functions and methods. If any error messages is given when this command
make sure PythonCyc has been installed according to the Installation instructions.
Then enter the command

>>> meta = pythoncyc.select_organism('meta')

This command sends a request to Pathway Tools to create a
**PGDB** object associated with the database MetaCyc, which is
specified using the 'meta' organism identifier (i.e., orgid). That PGDB
object is assigned to the variable <tt>meta</tt>. (If you get
an error message, it could be that Pathway Tools was not started
as given above.)

Print out the Python <tt>meta</tt> variable

>>> print meta
<PGDB meta, currently has 0 PFrames>

This means that <tt>meta</tt> is bound to a PGDB object, its orgid is meta and
it has currently no PFrames attached to it. Indeed, currently we only created
a PGDB object that has no _data frames_ in it. The following sections will show
how to retrieve data frames from Pathway Tools to PythonCyc via this variable <tt>meta</tt>.

If you want to have a list of all PGDBs, and their orgids, accessible from your
running Pathway Tools, evaluate <tt>pythoncyc.all_orgids()</tt>.

Please, consult the source file <tt>__init__.py</tt> for other
fundamental methods available from the pythoncyc module.

Also, more
advanced technical documentation is provided for each method and
functions by consulting the PythonCyc modules config.py, PGDB.py,
PToolsFrame.py and PTools.py. As usual, the Python <tt>help</tt>
command can provide documentation about the modules, classes and
methods: for example, <tt>help(pythoncyc)</tt> prints the
documentation for the module <tt>pythoncyc</tt>; or the
command <tt>help(meta)</tt>, once variable <tt>meta</tt> is bound to a
PGDB object, prints all the methods available, and their documentation,
for a PGDB object.

## Retrieving Frames From Pathway Tools using Frame Ids

 Using that variable <tt>meta</tt>, it is now possible to request data from the
MetaCyc database.  For example, the following statement retrieves compound
TRP (i.e., L-tryptophan). 'TRP' is the **frame id** of compound L-tryptophan, which we
can also specify in lower case letters:

>>> meta.trp

Evaluating <tt>meta.trp</tt>, if done for the first time, triggers
a call to Pathway Tools to retrieve the _frame data_ (or simply
frame) of compound TRP: the slots (also called attributes) and their
data of the Pathway Tools frame representing compound TRP are
sent to PythonCyc to create a **PFrame** object to represent
the compound TRP. PythonCyc also prints that frame content which is
represented as a Python dictionary: the slots of the frame are
the keys of the dictionary and the value of the slots are the
values of the dictionary.

 Note: in Pathway Tools, the _slots_ are used to access the
data of a frame (i.e., object).  In Python, an object has
_attributes_. The meaning of these two terms, that is, slots and
attributes, are very similar. A slot of a frame in Pathway Tools
becomes an attribute of a PFrame object in PythonCyc. We will use the term
slots when referring to a frame in Pathway Tools and attributes when referring
to the same slots in PythonCyc.

Printing out meta now shows that we have one PFrame attached to
meta:

>>> print meta
<PGDB meta, currently has 1 PFrames>

Indeed, this PFrame for TRP was also bound to the PGDB meta such that it
became an attribute of meta. That is, executing <tt>meta.trp</tt> again would
not retrieve the data from Pathway Tools, but directly use the PFrame
already created for it and now stored as an attribute for meta. All
created PFrames based on a PGDB object are attached to that object and
no two PFrames can have the same frame id for that PGDB object.

In Pathway Tools, the frame id is in upper case, that is,
'TRP'. The conversion from lower case to upper case is handled
automatically by PythonCyc.  PFrame, in PythonCyc, is the class of
objects to represent frame objects from Pathway Tools. More details
about PFrames is given below and in the Section [PFrame Objects](#pframes). In particular, note that PFrames
are read only, that is, their attributes cannot be modified.

From a PGDB object, a PFrame can be accessed using the frame id
either by using the attribute or indexing syntax of Python. For example, the
following would retrieve reaction with frame id RXN-9000

>>> rxn9000 = meta['RXN-9000']

or by using the attribute syntax

>>> rxn9000 = meta.rxn_9000

Both forms access the same attribute. Note that the frame id
'RXN-9000' has a dash in its name but an attribute in Python cannot
have a dash. To provide access to such frame ids, using the attribute
syntax of Python, dashes are converted to underscores. Mixed cases
(i.e., upper or lower case letters) can be used for both syntax
(attributes and indexing) because an automatic conversion is done by
PythonCyc. There are cases where only the second form, the indexing
syntax, can be used. For example, for a slot name starting with a
digit, or a character that is not a letter or an underscore, the
indexing syntax with a string must be used. This is due to the syntax
of attribute names in Python which can only have letters, digits and
underscores, and cannot start with a digit. For example, the slot
name 'N+1-NAME' can only be accessed using the index syntax because
it has the character '+' which cannot be used in a Python identifier.

In PythonCyc, frame ids are stored as strings prefixed and suffixed
by '|'. In general, these vertical bars identify symbols in
PythonCyc which exists as Lisp symbols in Pathway Tools. When
symbols are return from Pathway Tools, the vertical bars are inserted.
For example, we can see the frame id of meta.trp by printing it:

>>> print meta.trp.frameid
|TRP|

The syntax '|...|' is used to indicate that this string represents a symbol and can be
interpreted by Pathway Tools as a frame id. In Lisp, the programming language used
to implement Pathway Tools, the double vertical bars signifies that this is a symbol that
must be read exactly as given without any transformation (e.g., no case
conversion on letters).

Advanced technical note: all PFrames created using a PGDB object are
included in a Python dictionary bound to an attribute, of that PGDB
object, called "frames". The keys of that dictionary are the frame ids
of the PFrames converted to valid Python identifiers. The __getattr__
and __getitem__ methods of the PGDB class were written in such a way
that such an expression as 'meta.trp' searches in the dictionary for a
PFrame with frame id 'TRP'.

Once you have access to a frame, the frame ids stored in that
frame can be used to create more PFrames. For example, the
<tt>trp</tt> object has an attribute called <tt>appears_in_right_side_of</tt>
which has a list of reaction frame ids as a value. The reaction
frame ids refer to all reactions that has TRP on its right-hand side.
The reactions can be retrieved in the following way:

>>> rxns_trp_right = [meta[fid] for fid in meta.trp.appears_in_right_side_of]

The variable <tt>rxns_trp_right</tt> is bound to a list of PFrames
representing the reactions. Each PFrame becomes also attributes to
<tt>meta</tt> based on the frame ids.

The basic mechanism of attribute access and indexing on a PGDB
object just shown is enough to retrieve all frames from a PGDB
assuming that frame ids are known and PFrames are implicitly
(i.e., automatically) created. The next section shows
how to retrieve large number of frames based on classes of objects,
which indirectly provides the frame ids of large number of frames.

## Retrieving Classes of Frames from Pathway Tools

Another implicit operations done by PythonCyc is the retrieval of
classes of objects from Pathway Tools. There are many classes of
objects, such as Reactions, Proteins, Compounds, Genes, Pathways, and
more. For example, retrieving the class of all reactions of PGDB meta,
from Pathway Tools, can be done by

>>> reactions = meta.reactions

 which assign to variable <tt>reactions</tt> a PFrame representing the class
of reactions from PGDB meta. Printing out this variable gives

>>> reactions
<PFrame class |Reactions| currently with 13081 instances (meta)>

As can be seen, <tt>reactions</tt> is a PFrame and it is a class which
has the name |Reactions| in Pathway Tools and it has 13,081 instances,
that is, 13,081 reactions, all from the PGDB meta (i.e., MetaCyc).
The number of instances may differ for you because MetaCyc is
periodically modified. Remember that the vertical bars in a name means
that it is a symbol, and here it is more specifically a frame id.

This PFrame <tt>reactions</tt> has an attribute <tt>instances</tt>
assigned with the list of all reactions of the MetaCyc PGDB. Each such
reaction is also a PFrame, although these PFrames have currently **no
other data than the frame id of each reaction**. It is a _lazy
transfer_ of the frames where only the frame ids were requested
from Pathway Tools. This approach is useful because in some cases not
all data from all reactions are needed. We can access each reaction
via the attribute <tt>instances</tt>, for example

>>> reactions.instances[0]

but we can also use indexing directly on the class reactions such as

>>> reactions[0]
{'_gotframe': False, '_isclass': False, 'pgdb': meta, 'frameid': u'|3.1.22.4-RXN|'}

but the frameid value is likely different (MetaCyc is periodically
modified). As mentioned, each object in a PGDB has a unique identifier
called the frame id, which in PythonCyc is stored in the field frameid
of a PFrame. When the reactions were retrieved from Pathway Tools,
the frame id values also became attributes of the Python PGDB object meta,
that is, we can also indexed object <tt>meta</tt> with the frame
ids. For example,

>>> meta['|3.1.22.4-RXN|']
{'_gotframe': False, '_isclass': False, 'pgdb': meta, 'frameid': u'|3.1.22.4-RXN|'}

This Python dictionary is a very short representation of the frame
with almost no data. The attributes shown are only created by
PythonCyc to maintain that frame in Python. If you access one
slot of that reaction frame, which is not listed in that
output, the value of that slot is retrieved from Pathway Tools. For
example,

>>> reactions[0].left
[u'|Double-Stranded-DNAs|', u'|WATER|']

retrieves the data for slot <tt>left</tt> **and that value is kept in the PFrame**.
Therefore, accessing several times the same slot of a frame, only triggers one communication
to Pathway Tools because after it was accessed once, it is no longer accessed again
from Pathway tools. (Note: the methods <tt>get_slot_values</tt> and <tt>get_slot_value</tt>
presented later in this document always trigger a transfer from Pathway Tools.)
We can verify that the the PFrame has the left attribute in the PFrame itself:

>>> reactions[0]
{u'left': [u'|Double-Stranded-DNAs|', u'|WATER|'], '_gotframe': False,
'_isclass': False, 'pgdb': meta, 'frameid': u'|3.1.11.3-RXN|'}

### Explicit Transfer of Frames

Another very different approach to retrieve all the data
of a list of frames is by using the Python method
<tt>get_frame_objects</tt> defined for a PGDB object.
That method takes a list of frame ids as input and send a request to
Pathway Tools to transfer all the slots and their data for all the
frames identified by these frame ids. The function creates a PFrame,
for each frames retrieved, for the PGDB, if none exist.

For example, assuming that we have the variable <tt>reactions</tt>
bound to the class of reactions as in the previous section,
to retrieve all the frame data for the first 10 reactions,
the following can be used: (We retrieve only the first 10 reactions
to reduce execution time)

>>> r = meta.get_frame_objects([f.frameid for f in reactions.instances[0:10]])

The list comprehension gathers the frame ids in one list and a call to
the <tt>get_frame_objects</tt> method is done, using the PGDB object meta, to
retrieve from Pathay Tools all the slots and their data for all the
frame ids. This approach always transfer the frames from Pathway Tools
even if we already transferred them already. This can be needed if the
frames were modified and there is a need to transfer them again.

## Explicit Access to Slot Data Without PFrames

Another very different way to access the frame data is to use
methods <tt>get_slot_value</tt> and <tt>get_slot_values</tt>.  These
methods are among the more than 150 methods of the PGDB class which is the
subject of the next section.  The
first method retrieves a scalar value on a slot that can only have one
value whereas the second method retrieves a list of values from a slot
that can have multiple values. In both cases, only the data from one
slot is retrieved, not the whole frame. That is, there are **no
creation of PFrames** when using these functions.

For example, the following retrieve the Gibbs free
energy of reaction RXN-9000 from MetaCyc

>>> meta.get_slot_value('RXN-9000', 'GIBBS-0')
7.5217285

We did not specify the vertical bars for the frame id 'RXN-9000'
and the name of the slot 'GIBBS-0' although both are symbols in
Pathway Tools.  The translation to symbols is handled automatically by
the function <tt>get_slot_value</tt>, and many other functions that
require symbols as arguments.

The following retrieves the chemical formula of compound TRP

>>> meta.get_slot_values('TRP', 'CHEMICAL-FORMULA')
{u'|N|': [2], u'|C|': [11], u'|H|': [12], u'|O|': [2]}

The method <tt>get_slot_values</tt> is needed (instead of
<tt>get_slot_value</tt>, the singular version) because the slot
'CHEMICAL-FORMULA' keeps the chemical formula as a list of pairs
(atom-species coefficient) where atom-species is the species of the
atom (e.g., 'C' for carbon) and coefficient is an integer. In that
particular case, that is, a list of pairs, the result will be returned
as a Python dictionary where the keys are the atom species and the
values are the coefficients.  In the next section there is more
explanation on data conversion between Lisp and Python.

## Calling Pathway Tools Functions Using a PGDB Object

Using a variable bound to a PGDB object, which is the case for
variable <tt>meta</tt> from the previous sections, any of the more than 150
methods from the PGDB class can be called, each corresponding to a
specific function in Pathway Tools. The list of these methods (or
functions), and their documentation, can be obtained by consulting the
source code of file <tt>PGDB.py</tt> or by using the standard help mechanism
of Python (or IPython). For example,

>>> help(pythoncyc.PGDB)

will list the source documentation of the class PGDB with the list of
all methods. For a particular function, you can request its documentation
by naming the function. For example,

>>> help(pythoncyc.PGDB.get_slot_value)

Almost all these methods do not create PFrame objects but returns
basic Python object, that is, boolean, numbers, strings, lists,
dictionaries, and so on. When a Pathway Tools object (e.g., gene,
pathway, reaction) needs to be returned, the frame id (as a string) is
returned. For example, the following call retrieves all the pathways
from meta by returning a list of frame ids (as strings):

>>> pwys = meta.all_pathways()

As presented in the previous section, to create PFrames, and the
data about pathways, using frame ids, you can use the method
<tt>get_frame_objects</tt>.

Many other Lisp functions, defined in Pathway Tools, can be called using
Python's syntax. These functions often need a frame as one of the parameter.
A frame can be specified as a frame id (a string) or as a PFrame object. For example,

>>> meta.reactions_of_compound('TRP')

where 'TRP' is the frame id of compound L-tryptophan. The
<tt>reactions_of_compound</tt> method (which is a function in Pathway Tools)
retrieves all reaction frame ids that
use TRP as a substrate. Note that, if the given frame id was not
existing in the PGDB, it would raise a PToolsError in PythonCyc because
Pathway Tools itself will report a 'non coercible frame'.

A PFrame can also be used instead of the frame id. For example,
<tt>meta.trp</tt> refers to a PFrame for compound TRP and
can be used to do the same operation we just did, that is,

>>> meta.reactions_of_compound(meta.trp)

Some methods modify the PGDB in Pathway Tools, such as

>>> meta.put_slot_value('RXN-9000','GIBBS-0',2.7)

which modifies the slot 'GIBBS-0' for frame 'RXN-9000' to the value
2.7 for the PGDB associated with object meta, which in our case is MetaCyc.

Some functions have keywords arguments, which are always optional. But notice
that the default value is often the Python value <tt>None</tt>.
The value <tt>None</tt> is not translated to <tt>False</tt>, but
indicates to use the default value of the Lisp function called. These defaults
are given in the documentation of each PythonCyc method.

The transfer of all data from Pathway Tools to PythonCyc is done
using the JSON (JavaScript Object Notation) syntax. On the other hand,
PythonCyc **does not use** JSON for the transfer of data from
Python to Pathway Tools because Pathway Tools uses Lisp and it is
simpler to use a Lisp syntax for that transfer.  Some conversion rules
are applied on data types when transferring data to and from Pathway
Tools and we discuss these in the following. Note that all the special
rules of conversion from Lisp to Python is based on what the JSON Lisp
package does whereas the special rules of conversion from Python to
Lisp is what the method <tt>convertArgToLisp</tt>, of class PGDB,
does.

During conversion, the numbers are kept relatively unchanged:
integers and floating-point numbers are translated using the same
types but floating-point numbers are not guaranteed to have the same
precision. In Lisp and Python, infinite precision of integers are
implemented. The rational numbers of Lisp are translated to
floating-point numbers in Python.

The Python value <tt>True</tt> is translated into <tt>t</tt> in Lisp whereas
<tt>False</tt> and <tt>None</tt> are translated into <tt>nil</tt> in Lisp.
The value <tt>t</tt> in Lisp is translated into <tt>True</tt> in Python.
On the other hand, the value <tt>nil</tt> in Lisp is translated into
<tt>False</tt>, <tt>None</tt> or the empty list <tt>[]</tt> in Python depending
on the context. It is translated to <tt>False</tt> when a PythonCyc method
is called that expects a boolean value as a result: for example, the
PythonCyc method <tt>pathway_hole_p</tt> does return <tt>False</tt> when
the result of the corresponding function in Lisp is <tt>nil</tt>. It is translated
to the empty list <tt>[]</tt> when a PythonCyc
method is called that expects a list as a result: for example, the
PythonCyc method <tt>get_slot_values</tt> does return <tt>[]</tt>
when the slot value is <tt>nil</tt>. Finally, the <tt>nil</tt>
values is translated to <tt>None</tt> when the result is not
known to be a boolean or a list value: for example, the
PythonCyc method <tt>get_slot_value</tt> would return <tt>None</tt>
if the slot value is <tt>nil</tt>.

Any Lisp string is translated into a Python string. A Python string
that starts and ends with a '|', and contains no space, is assumed to
represent a symbol and is translated successfully into one if indeed
the Python string follows the correct syntax of a Lisp symbol. For example,
the Python string <tt>"|RXN-9000|"</tt> is translated into the Lisp
symbol <tt>RXN-9000</tt>.

Lisp lists are translated to Python lists, but with one important
exception: a Lisp list where all elements are non empty sublists, and
every first element of each sublist is a symbol or string, are
translated into a Python dictionary. For example, the
slot <tt>chemical-formula</tt> has such lists: the chemical formula H2O is
represented as the list of sublists <tt>((H 2) (O 1))</tt>, where H and O
are symbols, in that slot. Each sublist is translated, in JSON, to a
key/value pair in a dictionary, where the key is the first element of
the sublist and the value is the rest of the list. Therefore, the Lisp
value <tt>((H 2) (O 1))</tt> is translated into the Python dictionary
<tt>{'|H|' : [2], '|O|' : [1]}</tt>. Notice that H and O are translated into Python strings
with the vertical bars to indicate that they are symbols
and the coefficients 2 and 1 are translated into lists of one element
because the rest of each sublist is a list.

A dictionary in Python is translated into a list of dotted pairs or
list of lists when transferred to Pathway Tools. For example,
<tt>{'|H|' : [2], '|O|' : [1]}</tt> is translated into <tt>((H 2) (O
1))</tt>, which is the desired Lisp representation in Pathway Tools
for the a chemical-formula slot value. On the other hand the Python
dictionary <tt>{'|H|' : 2, '|O|' : 1}</tt> would be translated into
<tt>((H . 2) (O . 1))</tt>, that is, a list of dotted pairs, which is
not the desired Lisp representation for that slot but could be used
correctly in some other context although Pathway Tools rarely use
dotted pairs.

A vector from Pathway Tools is translated into a Python Lisp.
Note: there is no basic vector type in Python.

Finally, a Python tuple is translated into an improper Lisp list.
For example, the Python tuple <tt>(1, 2, 3)</tt> is translated
into the improper Lisp list <tt>(1 2 . 3)</tt>. The last element
of the list is preceded by a dot, which makes it an improper list.
Note that the particular case of a tuple with only one element is translated
into an improper list of <tt>nil</tt> followed by the element. For example,
the Python tuple <tt>(2,)</tt> is translated into <tt>(nil . 2)</tt>.
Improper lists are uncommon in Pathway Tools such that you may never need
to worry about them.

If you modify the slots of data frames in Pathway Tools, via the
PythonCyc interface, care must be taken to store the appropriate data
structures and use the appropriate corresponding Python data
structures.  As we just saw, this should not be a problem for numbers,
strings, booleans and list of these basic types as the translation is
one to one. For example, the value of the slot <tt>synonyms</tt> of compound
frames is a list of strings, the synonyms of the compounds:

>>> meta.get_slot_values('atp', 'synonyms')
[u'adenylpyrophosphate', u'adenosine-triphosphate', u"adenosine-5'-triphosphate"]

You could modify that list, say to only include the first two synonyms, in the following
way:

>>> meta.put_slot_values('atp', 'synonyms', [u'adenylpyrophosphate', u'adenosine-triphosphate'])

 Note: Typically, modifying a slot should be done on your own
created PGDB, not on MetaCyc.  In any case, you will not be able to
save MetaCyc and any slots modified in MetaCyc will be restored to its
original value after you restart the Pathway Tools application.

As for a slot such as <tt>chemical-formula</tt>, you can represent the
value to store in the slot as a list of sublists, such as:

>>> meta.put_slot_values('water', 'chemical-formula', [['|H|',2],['|O|',1]])
{u'|H|': [2], u'|O|': [1]}

The returned value is a dictionary that contains frames ids (i.e., the vertical bars
surrounding H and O indicate that they are symbols). Or you can use a dictionary
as in

>>> meta.put_slot_values('water', 'chemical-formula', {u'|H|': [2], u'|O|': [1]})

In all cases, care must be taken to have the right representation when
modifying a frame slot of a PGDB in Pathway Tools because, at the moment of
modifiying the slot, there is no verification of the validity of
the data being stored.

 When a slot is supposed to contain frames, storing the frame ids
in the slot automatically convert these frame ids into frame
references.  For example, the slot <tt>structure-atoms</tt> is a list
of atom species frames. To modify it would simply require to list the
atom species frame ids. For example, for compound water:

>>> meta.put_slot_values('water', 'structure-atoms', ['|O|', '|H|', '|H|'])

Notice that we passed the frame ids O and H using the vertical bars.
This is needed because otherwise these would be taken as strings not symbols that
refer to frame object in Pathway Tools.

As already mentioned, there are many more methods that can be
called to access functions in Pathway Tools and some of them can run
complex analysis. For example, there is a method to run flux balance
analysis (FBA) called <tt>run_fba</tt>. Please, consult the file <tt>PGDB.py</tt> for the
complete list of available methods and their documentation.

## More on PFrame Objects

PFrame is a Python class to represent Pathway Tools' frames in
PythonCyc. A PFrame can represent a Pathway Tools class frame (e.g.,
Reactions) as well as an instance frame (e.g., RXN-9000). PFrames are
useful to retrieve many frames and all their data from Pathway to
PythonCyc and then operate on that data locally in Python. On the
other hand, if the data needs to be modified in the PGDB of Pathway
Tools, PFrames are not useful because they are read only.

As discussed in the previous sections, PFrames are automatically
created when retrieving classes, or instances using the attribute or
indexing syntax applied to a PGDB object. You could also directly create PFrames.
To do so, you need to import the class PFrame:

>>> from pythoncyc.PToolsFrame import PFrame

The required parameters to create a PFrame are the frame id (a string) and a PGDB
object. For example, assuming that variable <tt>meta</tt> is bound to a PGDB object, the following
create a PFrame to represent the reaction RXN-9000,

>>> PFrame('RXN-9000', meta)
{'_gotframe': False, '_isclass': False, 'pgdb': <PGDB meta, currently has 1 PFrames>,
 'frameid': '|RXN-9000|'}

By default, an instance PFrame (not a class PFrame) is created and
the data of the frame is not requested from the server, that is,
a PFrame object is created containing only the frame id, the PGDB and a fews other
attributes to maintain the PFrame. This is what the print out of that frame shows
above. That PFrame is also attached, as an attribute, to the PGDB meta. That
can be seen by evaluating

>>> meta._frames.keys()
['rxn_9000']

By specifying the keyword argument <tt>getFrameData=True</tt>, all slots and data of the frame are retrieved
from Pathway Tools. For example, the following
create a PFrame for reaction RXN-9000 and retrieve all its slots and data,

>>> PFrame('RXN-9000', meta, getFrameData=True)
{u'enzymatic_reaction': [u'|ENZRXN-14558|'], u'right': [u'|PROTON|', u'|CPD-9460|', u'|UDP|'],
u'schema_p': True, '_isclass': False, u'creator': u'|Kate|', u'ec_number': [u'|EC-2.4.1.17|'],
u'creation_date': 3408307573, u'reaction_direction': u'|LEFT-TO-RIGHT|', 'frameid': '|RXN-9000|',
u'in_pathway': [u'|PWY-5756|'], u'left': [u'|CPD-9459|', u'|UDP-GLUCURONATE|'],
u'citations': [u'WOJCIECHOWSKI75'], u'key_slots': u'|COMMON-NAME|', u'physiologically_relevant_p': [True],
u'synonym_slots': [u'|ABBREV-NAME|', u'|SYNONYMS|'], u'atom_mappings': {u'|NO-HYDROGEN-ENCODING|':
[[38, 37, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 6,
7, 9, 10, 12, 13, 60, 61, 62, 63, 64, 65, 66, 19, 20, 22, 24, 23, 68, 69, 67, 32, 0, 1, 2, 3, 4,
5, 8, 11, 14, 15, 16, 17, 18, 21, 25, 29, 33, 28, 26, 27, 30, 31, 34, 36, 35],
[{u'|CPD-9459|': [37, 69], u'|UDP-GLUCURONATE|': [0, 36]}, {u'|CPD-9460|': [0, 44], u'|UDP|': [45, 69]}]]},
'_gotframe': True, u'gibbs_0': 2.7000000000000002, u'substrates': [u'|UDP-GLUCURONATE|',
u'|CPD-9459|', u'|PROTON|', u'|CPD-9460|', u'|UDP|'], 'pgdb': <PGDB meta, currently has 1 PFrames>}

For creating a class, the <tt>isClass</tt> keyword parameter must say so,

>>> PFrame('Reactions', meta, isClass=True)

When creating a class, if <tt>getFrameData=True</tt> is specified, the class slots and its
data are fetched and all the instances of the class are also created
as, mostly empty, PFrames. They are mostly empty in the sense that the
slots and data of the instances are not transferred, but only the
frame id of each frame initialized each PFrame.

The attribute values of the PFrames cannot be modified, that is, attributes are read only.
If you try to modify a PFrame attribute, a PythonCycError is raised.
On the other hand, slots of Pathway Tools' objects can be modified using methods
<tt>put_slot_value</tt> and <tt>put_slot_values</tt>. In that case, the PGDB
itself in Pathway Tools is modified. See class PGDB for these methods.

## Remotely Accessing Pathway Tools

It is possible to use PythonCyc to access a Pathway Tools
application running on a remote computer. First, Pathway Tools must be
started with the command line option '-python' (not
'-python-local-only'). That is,

./pathway-tools -python

or with also the option -lisp to get a Lisp console to monitor the Python server

./pathway-tools -lisp -python

Note: If you were using the option '-python-local-only' instead of '-python'
the Python server would not accept connection from
remote computers increasing cybersecurity.

Second, you need to
use the config module of PythonCyc to set the appropriate host name
of that remote computer. For example, assuming that the remote computer
is at address 'ptools.mydomain.com' (this is a fictive address for
this example). The following would configure
PythonCyc to communicate with it:

>>> import pythoncyc.config as config
>>> config.set_host_name('ptools.mydomain.com')

If for some reason the Pathway Tools Python server is not using the default port
(i.e., 5008),  but some other port such as 5000,
it can also be configured on the PythonCyc side by using method <tt>set_host_port</tt>

>>> config.set_host_port(5000)

The preceding Python configuration can be done at any time and it affects
all future operations of PythonCyc. It could be done several times to
access different Pathway Tools running on different ports or host names.

## Complete Examples

### Function to Gather the Gibbs Free Energies of Substrates of Reactions

 This example is a function that gathers the Gibbs free energy of
the compounds involved in each reaction of a PGDB with a given
orgid. The result is a Python dictionary with keys as the frame ids of
the reactions and the values as lists of the substrates' Gibbs free
energy of each reaction.

This function does not create any PFrame but uses the basic <tt>get_slot_value</tt>
and <tt>get_slot_values</tt> to retrieve data from Pathway Tools. The function
<tt>all_rxns</tt> retrieves the frame ids of all reactions from the PGDB,
then the substrates slot is accessed to get the compound
frame ids involved in each reaction. Finally, for each compound frame id, the slot
'GIBBS-0' is accessed to create pairs of compound frame id and Gibbs free energies values
transformed into a dictionary using the Python <tt>dict</tt> function.

def gather_gibbs_substrates_of_reactions(orgid):
     """
     Return a dictionary of all reactions of PGDB orgid with the Gibbs free energies of formation
     of their substrates. The keys are the frame ids of the reactions and the values are dictionaries
     of compound frame ids to Gibbs free energies of formation.
     """
     pgdb = pythoncyc.select_organism(orgid)
     rxn_frameids = pgdb.all_rxns(type='all')
     gibbs_dict = {}
     for rxn_fid in rxn_frameids:
         cpds_fids = pgdb.get_slot_values(rxn_fid,'SUBSTRATES')
         gibbs_dict[rxn_fid] = dict([(cpd_fid, pgdb.get_slot_value(cpd_fid, 'GIBBS-0')) for cpd_fid in cpds_fids])
     return gibbs_dict

This function could be called in the following way assuming that
the PGDB with orgid 'ecoli' is available from the Python server of Pathway Tools.

>>> gibbs_dict = gather_gibbs_substrates_of_reactions('ecoli')

This function may take more than 30 seconds to execute because it is retrieving
a large amount of data from Pathway Tools.

### Function to Create a PGDB with all its Compounds and Reactions

The following is a simple function to create a PGDB object based on
the organism name (i.e., orgid) and retrieve all its basic PFrames for
compounds and reactions. Note that these PFrames has no other data
than their frame ids.

def create_pgdb_with_compounds_and_reactions(orgid):
     """
     Create a PythonCyc PGDB object with all its compounds and reactions
     PFrames created. Return the PGDB object.
     """
     pgdb = pythoncyc.select_organism(orgid)
     pgdb.compounds
     pgdb.reactions
     return pgdb

 Assuming that the PGDB ecoli is available on your running Pathway
Tools server, the following call would bound variable <tt>pgdb</tt> to a PGDB
object containing the PFrames for the compounds and reactions of
ecoli:

pgdb = create_pgdb_with_compounds_and_reactions('ecoli')

## Support

For comments, questions and bug reports about PythonCyc,
send an email to ptools-support@ai.sri.com

## Acknowledgments

Eli Bogart inspired some implementation details of PythonCyc from
its PyCyc package, Tomer Altman wrote the original Pathway Tools Lisp
API documentation at http://brg.ai.sri.com/ptools/api/ and Daniel
Weaver suggested to implement some specific functions to access the
functionality of FBA in Pathway Tools.

* * *


text:
import * as structure from './structure'
import * as types from './types'
import * as util from './util'

class DataError extends Error {
  constructor (code) {
    super(DataError.messages.error[code])
    Error.captureStackTrace(this, DataError)
    this.code = code
  }
  static get types () {
    return {
      INVALID_TYPE: 0,
      BAD_PARAMS: 1
    }
  }
  static get messages () {
    return {
      error: {
        0: 'Invalid Fragment type',
        1: 'Bad parameters'
      }
    }
  }
}

export {
  structure,
  types,
  util,
  DataError
}


text:
/*
 * Iodine, (C) 2015 Minio, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package iodine

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"reflect"
	"runtime"
	"strconv"
	"strings"
	"sync"

	"github.com/dustin/go-humanize"
)

// Error is the iodine error which contains a pointer to the original error
// and stack traces.
type Error struct {
	EmbeddedError error `json:"-"`
	ErrorMessage  string
	ErrorType     string

	Stack []StackEntry
}

// StackEntry contains the entry in the stack trace
type StackEntry struct {
	Host string
	File string
	Func string
	Line int
	Data map[string]string
}

var gopathSource string

var globalState = struct {
	sync.RWMutex
	m map[string]string
}{m: make(map[string]string)}

// SetGlobalState - set global state
func SetGlobalState(key, value string) {
	globalState.Lock()
	globalState.m[key] = value
	globalState.Unlock()
}

// ClearGlobalState - clear info in globalState struct
func ClearGlobalState() {
	globalState.Lock()
	for k := range globalState.m {
		delete(globalState.m, k)
	}
	globalState.Unlock()
}

// GetGlobalState - get map from globalState struct
func GetGlobalState() map[string]string {
	result := make(map[string]string)
	globalState.RLock()
	for k, v := range globalState.m {
		result[k] = v
	}
	globalState.RUnlock()
	return result
}

// GetGlobalStateKey - get value for key from globalState struct
func GetGlobalStateKey(k string) string {
	globalState.RLock()
	result, _ := globalState.m[k]
	globalState.RUnlock()
	return result
}

// ToError returns the input if it is not an iodine error. It returns the embedded error if it is an iodine error. If nil, returns nil.
func ToError(err error) error {
	switch err := err.(type) {
	case nil:
		{
			return nil
		}
	case Error:
		{
			if err.EmbeddedError != nil {
				return err.EmbeddedError
			}
			return errors.New(err.ErrorMessage)
		}
	default:
		{
			return err
		}
	}
}

// New - instantiate an error, turning it into an iodine error.
// Adds an initial stack trace.
func New(err error, data map[string]string) error {
	if err != nil {
		entry := createStackEntry()
		var newErr Error

		// check if error is wrapped
		switch typedError := err.(type) {
		case Error:
			{
				newErr = typedError
			}
		default:
			{
				newErr = Error{
					EmbeddedError: err,
					ErrorMessage:  err.Error(),
					ErrorType:     reflect.TypeOf(err).String(),
					Stack:         []StackEntry{},
				}
			}
		}
		for k, v := range data {
			entry.Data[k] = v
		}
		newErr.Stack = append(newErr.Stack, entry)
		return newErr
	}
	return nil
}

// createStackEntry - create stack entries
func createStackEntry() StackEntry {
	host, _ := os.Hostname()
	pc, file, line, _ := runtime.Caller(2)
	function := runtime.FuncForPC(pc).Name()
	_, function = filepath.Split(function)
	file = strings.TrimPrefix(file, gopathSource) // trim gopathSource from file

	data := GetGlobalState()
	for k, v := range getSystemData() {
		data[k] = v
	}

	entry := StackEntry{
		Host: host,
		File: file,
		Func: function,
		Line: line,
		Data: data,
	}
	return entry
}

func getSystemData() map[string]string {
	host, err := os.Hostname()
	if err != nil {
		host = ""
	}
	memstats := &runtime.MemStats{}
	runtime.ReadMemStats(memstats)
	return map[string]string{
		"sys.host":               host,
		"sys.os":                 runtime.GOOS,
		"sys.arch":               runtime.GOARCH,
		"sys.go":                 runtime.Version(),
		"sys.cpus":               strconv.Itoa(runtime.NumCPU()),
		"sys.mem.used":           humanize.Bytes(memstats.Alloc),
		"sys.mem.allocated":      humanize.Bytes(memstats.TotalAlloc),
		"sys.mem.heap.used":      humanize.Bytes(memstats.HeapAlloc),
		"sys.mem.heap.allocated": humanize.Bytes(memstats.HeapSys),
	}
}

// Annotate an error with a stack entry and returns itself
//
//  func (err *WrappedError) Annotate(info map[string]string) *WrappedError {
//       entry := createStackEntry()
//       for k, v := range info {
// 		 entry.Data[k] = v
//       }
//       err.Stack = append(err.Stack, entry)
//       return err
//  }

// EmitJSON writes JSON output for the error
func (err Error) EmitJSON() ([]byte, error) {
	return json.Marshal(err)
}

// EmitHumanReadable returns a human readable error message
func (err Error) EmitHumanReadable() string {
	var errorBuffer bytes.Buffer
	fmt.Fprintln(&errorBuffer, err.ErrorMessage)
	for i, entry := range err.Stack {
		prettyData, _ := json.Marshal(entry.Data)
		fmt.Fprintln(&errorBuffer, "-", i, entry.Host+":"+entry.File+":"+strconv.Itoa(entry.Line)+" "+entry.Func+"():", string(prettyData))
	}
	return string(errorBuffer.Bytes())
}

// Emits the original error message
func (err Error) Error() string {
	return err.EmitHumanReadable()
}

func init() {
	_, iodineFile, _, _ := runtime.Caller(0)
	iodineFile = filepath.Dir(iodineFile)         // trim iodine.go
	iodineFile = filepath.Dir(iodineFile)         // trim iodine
	iodineFile = filepath.Dir(iodineFile)         // trim pkg
	iodineFile = filepath.Dir(iodineFile)         // trim minio
	iodineFile = filepath.Dir(iodineFile)         // trim minio
	gopathSource = filepath.Dir(iodineFile) + "/" // trim github.com
}


text:
Debugging in FreeMarker made easy! Manually or dynamically traverse through local and namespaced FreeMarker variables or objects from the data model sent to the FreeMarker view.

## Usage

```ftl
<#import "debugger.ftl" as debugger />

<#-- Basic usage. Creates a table of the top-level data model objects -->
<@debugger.debug />

<#-- Adds links to traversable elements -->
<@debugger.debugDynamic />
```

## Settings

Basic settings are included to allow for configuration flexibility:
```ftl
<#assign settings = {
  "styleClassPrefix": "freemarker-debug",
  "queryParamKey": "debugQuery",
  "includeStyles": true,
  "ignoredKeys": ["class"],
  "ignoredPatterns": ["org.springframework."]
} />
```

`styleClassPrefix`
* Controls what all the CSS classes are prefixed with.
* While it is unlikely you will have existing styles that conflict with `freemarker-debug`, this option is included in case customization is preferred.

`queryParamKey`
* Controls what query string dynamic links are built with.
* It is recommended that this value be customized in order to obscure what parameter your project will use. (Projects should be configured to prevent the debugger to run in production.)

`includeStyles`
* Flag to determine whether or not the default CSS styles should be included.
* The styles only affect the debug output and are added so that it is readable no matter what the design of the page is.

`ignoredKeys`
* Keys exactly matching any of these values will not be output.
* Case-sensitive

`ignoredPatterns`
* Keys **starting** with any of these values will not be output.
* Case sensitive.

### Example setting customization

```ftl
<#import "debugger.ftl" as debugger />

<#-- This will change all css classes to be prefixed
     with "custom-prefix" and ignore any keys equal to "class" or "equals" -->
<#assign customSettings = debugger.settings + {
    "styleClassPrefix": "custom-prefix",
    "ignoredKeys": ["class", "equals"]
  } />
<#assign settings = customSettings in debugger />

<@debugger.debug />
```
## License

[MIT](http://opensource.org/licenses/MIT)


text:
import React from "react";
import Actions from "../lib/actions";

const freeze = Object.freeze;

const wrapperStyle = freeze({
	borderTop: "1px solid #ccc",
	marginTop: "1em",
	paddingTop: "1em"
});
const inputStyle = freeze({
	fontFamily: "monospace",
	margin: "0 0.5em",
	width: "18em"
});
const addButtonStyle = freeze({
	backgroundColor: "#00b300",
	border: "1px solid #178017",
	color: "#fff",
	cursor: "pointer",
	fontFamily: "sans-serif",
	margin: 0,
	padding: "0.25em 0.75em"
});

const DomainAdder = React.createClass({
	addDomain() {
		const node = React.findDOMNode( this.refs.newDomain );
		Actions.addDomain( node.value );
		node.value = "";
	},

	handleInput( event ) {
		if ( event.key !== "Enter" ) {
			return;
		}
		this.addDomain();
	},

	render() {
		return (
			<div style={wrapperStyle}>
				<label htmlFor="new-domain">
					New Domain
				</label>
				<input type="text"
					id="new-domain"
					ref="newDomain"
					placeholder="Example: www.example.com"
					onKeyDown={this.handleInput}
					style={inputStyle} />
				<button type="button"
						style={addButtonStyle}
						onClick={this.addDomain}>
					Add
				</button>
			</div>
		);
	}
});

export default DomainAdder;


text:
<template name="splash">
  {{#if route}}

Welcome to High Treason!

High Treason is an Avalon clone with some names changed for reasons

Create an account or login to begin

    {{> loginButtons}}

  {{/if}}
</template>


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Recorder.Controls
{
    /// <summary>
    /// Interaction logic for ActionPanel.xaml
    /// </summary>
    public partial class ActionPanel : UserControl
    {
        public ActionPanel()
        {
            InitializeComponent();
        }
    }
}