text:
package org.apache.drill.exec.store;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.Lists;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.permission.FsPermission;

import java.io.IOException;
import java.util.List;

/** Contains list of parameters that will be used to store path / files on file system. */
public class StorageStrategy {

  /**
   * Primary is used for persistent tables.
   * For directories: drwxrwxr-x (owner and group have full access, others can read and execute).
   * For files: -rw-r--r-- (owner can read and write, group and others can read).
   * Folders and files are not deleted on file system close.
   */
  public static final StorageStrategy PERSISTENT = new StorageStrategy("775", "644", false);

  /**
   * Primary is used for temporary tables.
   * For directories: drwx------ (owner has full access, group and others have no access).
   * For files: -rw------- (owner can read and write, group and others have no access).
   * Folders and files are deleted on file system close.
   */
  public static final StorageStrategy TEMPORARY = new StorageStrategy("700", "600", true);

  private final String folderPermission;
  private final String filePermission;
  private final boolean deleteOnExit;

  @JsonCreator
  public StorageStrategy(@JsonProperty("folderPermission") String folderPermission,
                         @JsonProperty("filePermission") String filePermission,
                         @JsonProperty("deleteOnExit") boolean deleteOnExit) {
    this.folderPermission = folderPermission;
    this.filePermission = filePermission;
    this.deleteOnExit = deleteOnExit;
  }

  public String getFolderPermission() {
    return folderPermission;
  }

  public String getFilePermission() { return filePermission; }

  public boolean isDeleteOnExit() {
    return deleteOnExit;
  }

  /**
   * Creates passed path on appropriate file system.
   * Before creation checks which parent directories do not exists.
   * Applies storage strategy rules to all newly created directories.
   * Will return first created path or null already existed.
   *
   * Case 1: /a/b -> already exists, attempt to create /a/b/c/d
   * Will create path and return /a/b/c.
   * Case 2: /a/b/c -> already exists, attempt to create /a/b/c/d
   * Will create path and return /a/b/c/d.
   * Case 3: /a/b/c/d -> already exists, will return null.
   *
   * @param fs file system where file should be located
   * @param path location path
   * @return first created parent path or file
   * @throws IOException is thrown in case of problems while creating path, setting permission
   *         or adding path to delete on exit list
   */
  public Path createPathAndApply(FileSystem fs, Path path) throws IOException {
    List<Path> locations = getNonExistentLocations(fs, path);
    if (locations.isEmpty()) {
      return null;
    }
    fs.mkdirs(path);
    for (Path location : locations) {
      applyStrategy(fs, location, folderPermission, deleteOnExit);
    }
    return locations.get(locations.size() - 1);
  }

  /**
   * Creates passed file on appropriate file system.
   * Before creation checks which parent directories do not exists.
   * Applies storage strategy rules to all newly created directories and file.
   * Will return first created parent path or file if no new parent paths created.
   *
   * Case 1: /a/b -> already exists, attempt to create /a/b/c/some_file.txt
   * Will create file and return /a/b/c.
   * Case 2: /a/b/c -> already exists, attempt to create /a/b/c/some_file.txt
   * Will create file and return /a/b/c/some_file.txt.
   * Case 3: /a/b/c/some_file.txt -> already exists, will fail.
   *
   * @param fs file system where file should be located
   * @param file file path
   * @return first created parent path or file
   * @throws IOException is thrown in case of problems while creating path, setting permission
   *         or adding path to delete on exit list
   */
  public Path createFileAndApply(FileSystem fs, Path file) throws IOException {
    List<Path> locations = getNonExistentLocations(fs, file.getParent());
    if (!fs.createNewFile(file)) {
      throw new IOException(String.format("File [%s] already exists on file system [%s].",
          file.toUri().getPath(), fs.getUri()));
    }
    applyToFile(fs, file);

    if (locations.isEmpty()) {
      return file;
    }

    for (Path location : locations) {
      applyStrategy(fs, location, folderPermission, deleteOnExit);
    }
    return locations.get(locations.size() - 1);
  }

  /**
   * Applies storage strategy to file:
   * sets permission and adds to file system delete on exit list if needed.
   *
   * @param fs file system
   * @param file path to file
   * @throws IOException is thrown in case of problems while setting permission
   *         or adding file to delete on exit list
   */
  public void applyToFile(FileSystem fs, Path file) throws IOException {
    applyStrategy(fs, file, filePermission, deleteOnExit);
  }

  /**
   * Returns list of parent locations that do not exist, including initial location.
   * First in the list will be initial location,
   * last in the list will be last parent location that does not exist.
   * If all locations exist, empty list will be returned.
   *
   * Case 1: if /a/b exists and passed location is /a/b/c/d,
   * will return list with two elements: 0 -> /a/b/c/d, 1 -> /a/b/c
   * Case 2: if /a/b exists and passed location is /a/b, will return empty list.
   *
   * @param fs file system where locations should be located
   * @param path location path
   * @return list of locations that do not exist
   * @throws IOException in case of troubles accessing file system
   */
  private List<Path> getNonExistentLocations(FileSystem fs, Path path) throws IOException {
    List<Path> locations = Lists.newArrayList();
    Path starting = path;
    while (starting != null && !fs.exists(starting)) {
      locations.add(starting);
      starting = starting.getParent();
    }
    return locations;
  }

  /**
   * Applies storage strategy to passed path on passed file system.
   * Sets appropriate permission
   * and adds to file system delete on exit list if needed.
   *
   * @param fs file system where path is located
   * @param path path location
   * @param permission permission to be applied
   * @param deleteOnExit if to delete path on exit
   * @throws IOException is thrown in case of problems while setting permission
   *         or adding path to delete on exit list
   */
  private void applyStrategy(FileSystem fs, Path path, String permission, boolean deleteOnExit) throws IOException {
    fs.setPermission(path, new FsPermission(permission));
    if (deleteOnExit) {
      fs.deleteOnExit(path);
    }
  }
}

redpajama_set_name: RedPajamaGithub


text:
<resources>
	<string name="app_name">NicoliveHelper</string>
</resources>


text:
package net.java.sip.communicator.service.protocol;

/**
 * Provides operations necessary to create and handle conferencing calls through
 * a video bridge.
 *
 * @author Yana Stamcheva
 */
public interface OperationSetVideoBridge
    extends OperationSet
{
    /**
     * The name of the property under which the user may specify if
     * video bridge should be disabled.
     */
    public static final String IS_VIDEO_BRIDGE_DISABLED
        = "net.java.sip.communicator.service.protocol.VIDEO_BRIDGE_DISABLED";

    /**
     * Creates a conference call with the specified callees as call peers via a
     * video bridge provided by the parent Jabber provider.
     *
     * @param callees the list of addresses that we should call
     * @return the newly created conference call containing all CallPeers
     * @throws OperationFailedException if establishing the conference call
     * fails
     * @throws OperationNotSupportedException if the provider does not have any
     * conferencing features.
     */
    public Call createConfCall(String[] callees)
        throws OperationFailedException,
               OperationNotSupportedException;

    /**
     * Invites the callee represented by the specified uri to an already
     * existing call. The difference between this method and createConfCall is
     * that inviteCalleeToCall allows a user to transform an existing 1-to-1
     * call into a conference call, or add new peers to an already established
     * conference.
     *
     * @param uri the callee to invite to an existing conf call.
     * @param call the call that we should invite the callee to.
     * @return the CallPeer object corresponding to the callee represented by
     * the specified uri.
     * @throws OperationFailedException if inviting the specified callee to the
     * specified call fails
     * @throws OperationNotSupportedException if allowing additional callees to
     * a pre-established call is not supported.
     */
    public CallPeer inviteCalleeToCall(String uri, Call call)
        throws OperationFailedException,
               OperationNotSupportedException;

    /**
     * Indicates if there's an active video bridge available at this moment. The
     * Jabber provider may announce support for video bridge, but it should not
     * be used for calling until it becomes actually active.
     *
     * @return <tt>true</tt> to indicate that there's currently an active
     * available video bridge, <tt>false</tt> - otherwise
     */
    public boolean isActive();
}


text:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="is_light_status_bar_on_login">false</bool>
</resources>


text:
#include "tensorflow/compiler/xla/service/conditional_simplifier.h"

#include <string>
#include <utility>

#include "tensorflow/compiler/xla/literal_util.h"
#include "tensorflow/compiler/xla/service/hlo_computation.h"
#include "tensorflow/compiler/xla/service/hlo_instruction.h"
#include "tensorflow/compiler/xla/service/hlo_matchers.h"
#include "tensorflow/compiler/xla/service/hlo_opcode.h"
#include "tensorflow/compiler/xla/shape_util.h"
#include "tensorflow/compiler/xla/test.h"
#include "tensorflow/compiler/xla/tests/hlo_test_base.h"
#include "tensorflow/compiler/xla/types.h"
#include "tensorflow/compiler/xla/xla_data.pb.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/lib/core/status_test_util.h"
#include "tensorflow/core/platform/types.h"

namespace xla {
namespace {

namespace op = xla::testing::opcode_matchers;

class ConditionalSimplifierTest : public HloTestBase {
 public:
  // Makes a computation that contains a conditional with constant predicate.
  HloComputation* MakeConditional(HloModule* module);
};

HloComputation* ConditionalSimplifierTest::MakeConditional(HloModule* module) {
  HloComputation::Builder builder(TestName());

  // true_computation returns param+1.
  HloComputation* true_computation;
  {
    HloComputation::Builder true_computation_builder(TestName() +
                                                     ".true_computation");
    auto param =
        true_computation_builder.AddInstruction(HloInstruction::CreateParameter(
            0, ShapeUtil::MakeShape(S32, {}), "param"));
    auto one = true_computation_builder.AddInstruction(
        HloInstruction::CreateConstant(LiteralUtil::CreateR0<int32>(1)));

    true_computation_builder.AddInstruction(HloInstruction::CreateBinary(
        ShapeUtil::MakeShape(S32, {}), HloOpcode::kAdd, param, one));

    true_computation =
        module->AddEmbeddedComputation(true_computation_builder.Build());
  }

  // false_computation returns param+42.
  HloComputation* false_computation;
  {
    HloComputation::Builder false_computation_builder(TestName() +
                                                      ".false_computation");
    auto param = false_computation_builder.AddInstruction(
        HloInstruction::CreateParameter(0, ShapeUtil::MakeShape(S32, {}),
                                        "param"));
    auto forty_two = false_computation_builder.AddInstruction(
        HloInstruction::CreateConstant(LiteralUtil::CreateR0<int32>(42)));

    false_computation_builder.AddInstruction(HloInstruction::CreateBinary(
        ShapeUtil::MakeShape(S32, {}), HloOpcode::kAdd, param, forty_two));
    false_computation =
        module->AddEmbeddedComputation(false_computation_builder.Build());
  }

  auto false_instrn = builder.AddInstruction(
      HloInstruction::CreateConstant(LiteralUtil::CreateR0<bool>(false)));
  auto false_param = builder.AddInstruction(HloInstruction::CreateParameter(
      0, ShapeUtil::MakeShape(S32, {}), "false_param"));
  auto one = builder.AddInstruction(
      HloInstruction::CreateConstant(LiteralUtil::CreateR0<int32>(1)));

  builder.AddInstruction(HloInstruction::CreateConditional(
      ShapeUtil::MakeShape(S32, {}), false_instrn, one, true_computation,
      false_param, false_computation));

  return module->AddEntryComputation(builder.Build());
}

TEST_F(ConditionalSimplifierTest, ConditionalGetsInlined) {
  auto m = CreateNewVerifiedModule();
  HloComputation* computation = MakeConditional(m.get());
  ASSERT_TRUE(ConditionalSimplifier().Run(m.get()).ValueOrDie());
  EXPECT_THAT(computation->root_instruction(),
              op::Add(op::Parameter(), op::Constant()));
}

TEST_F(ConditionalSimplifierTest, ConditionalWithControlDependency) {
  auto m = CreateNewVerifiedModule();
  HloComputation* computation = MakeConditional(m.get());

  auto* true_op = computation->AddInstruction(
      HloInstruction::CreateConstant(LiteralUtil::CreateR0<bool>(true)));
  TF_ASSERT_OK(
      true_op->AddControlDependencyTo(computation->root_instruction()));

  EXPECT_FALSE(ConditionalSimplifier().Run(m.get()).ValueOrDie());
}

TEST_F(ConditionalSimplifierTest, NotRemovedIfContainsSend) {
  auto m = CreateNewVerifiedModule();
  HloComputation* computation = MakeConditional(m.get());
  auto* conditional = computation->root_instruction();
  ASSERT_EQ(conditional->opcode(), HloOpcode::kConditional);

  auto* true_computation = conditional->true_computation();
  auto* token = true_computation->AddInstruction(HloInstruction::CreateToken());
  auto* send = true_computation->AddInstruction(HloInstruction::CreateSend(
      true_computation->AddInstruction(
          HloInstruction::CreateConstant(LiteralUtil::CreateR0<bool>(true))),
      token, /*channel_id=*/0));
  true_computation->AddInstruction(HloInstruction::CreateSendDone(send));
  EXPECT_FALSE(ConditionalSimplifier().Run(m.get()).ValueOrDie());
}

TEST_F(ConditionalSimplifierTest, NotRemovedIfContainsRecv) {
  auto m = CreateNewVerifiedModule();
  HloComputation* computation = MakeConditional(m.get());
  auto* conditional = computation->root_instruction();
  ASSERT_EQ(conditional->opcode(), HloOpcode::kConditional);

  auto* true_computation = conditional->true_computation();
  auto* token = true_computation->AddInstruction(HloInstruction::CreateToken());
  auto* recv = true_computation->AddInstruction(HloInstruction::CreateRecv(
      ShapeUtil::MakeShape(F32, {1}), token, /*channel_id=*/0));
  true_computation->AddInstruction(HloInstruction::CreateRecvDone(recv));
  EXPECT_FALSE(ConditionalSimplifier().Run(m.get()).ValueOrDie());
}

TEST_F(ConditionalSimplifierTest, NotRemovedIfContainsNonRemovableInstruction) {
  auto m = CreateNewVerifiedModule();
  HloComputation* computation = MakeConditional(m.get());
  auto* conditional = computation->root_instruction();
  ASSERT_EQ(conditional->opcode(), HloOpcode::kConditional);
  auto* false_computation = conditional->false_computation();
  auto token = false_computation->AddInstruction(HloInstruction::CreateToken());
  false_computation->AddInstruction(HloInstruction::CreateInfeed(
      ShapeUtil::MakeShape(F32, {1}), token, "config"));
  EXPECT_FALSE(ConditionalSimplifier().Run(m.get()).ValueOrDie());
}

}  // namespace
}  // namespace xla


text:
package api.web.gw2.mapping.v2.pvp.amulets;

import api.web.gw2.mapping.core.ImplementationSpecific;
import api.web.gw2.mapping.v2.APIv2;

/**
 * Defines an attribute.
 * @author Fabrice Bouyé
 */
@APIv2(endpoint = "v2/pvp/amulets")
public enum PvpAmuletAttribute {

    /**
     * Defines the agony resistance attribute.
     */
    AGONY_RESISTANCE("AgonyResistance"), // NOI18N.
    /**
     * Defines the boon duration attribute.
     */
    BOON_DURATION("BoonDuration"), // NOI18N.
    /**
     * Defines the condition damage attribute.
     */
    CONDITION_DAMAGE("ConditionDamage"), // NOI18N.
    /**
     * Defines the condition duration attribute.
     */
    CONDITION_DURATION("ConditionDuration"), // NOI18N.
    /**
     * Defines the ferocity attribute.
     */
    FEROCITY("CritDamage"), // NOI18N.
    /**
     * Defines the healing attribute.
     */
    HEALING("Healing"), // NOI18N.
    /**
     * Defines the power attribute.
     */
    POWER("Power"), // NOI18N.
    /**
     * Defines the precision attribute.
     */
    PRECISION("Precision"), // NOI18N.
    /**
     * Defines the toughness attribute.
     */
    TOUGHNESS("Toughness"), // NOI18N.
    /**
     * Defines the vitality attribute.
     */
    VITALITY("Vitality"), // NOI18N.
    /**
     * Fail safe value.
     */
    @ImplementationSpecific
    UNKNOWN(null);

    final String value;

    private PvpAmuletAttribute(String value) {
        this.value = value;
    }
}


text:
package com.sksamuel.elastic4s.searches.queries

import com.sksamuel.elastic4s.analyzers.Analyzer
import com.sksamuel.exts.OptionImplicits._

sealed trait SimpleQueryStringFlag
object SimpleQueryStringFlag {
  case object ALL        extends SimpleQueryStringFlag
  case object NONE       extends SimpleQueryStringFlag
  case object AND        extends SimpleQueryStringFlag
  case object NOT        extends SimpleQueryStringFlag
  case object OR         extends SimpleQueryStringFlag
  case object PREFIX     extends SimpleQueryStringFlag
  case object PHRASE     extends SimpleQueryStringFlag
  case object PRECEDENCE extends SimpleQueryStringFlag
  case object ESCAPE     extends SimpleQueryStringFlag
  case object WHITESPACE extends SimpleQueryStringFlag
  case object FUZZY      extends SimpleQueryStringFlag
  case object NEAR       extends SimpleQueryStringFlag
  case object SLOP       extends SimpleQueryStringFlag
}

case class SimpleStringQuery(query: String,
                             analyzer: Option[String] = None,
                             analyzeWildcard: Option[Boolean] = None,
                             operator: Option[String] = None,
                             queryName: Option[String] = None,
                             quote_field_suffix: Option[String] = None,
                             lenient: Option[Boolean] = None,
                             fields: Seq[(String, Double)] = Nil,
                             flags: Seq[SimpleQueryStringFlag] = Nil,
                             minimumShouldMatch: Option[Int] = None)
    extends Query {

  def quoteFieldSuffix(suffix: String): SimpleStringQuery     = copy(quote_field_suffix = suffix.some)
  def flags(flags: SimpleQueryStringFlag*): SimpleStringQuery = copy(flags = flags)
  def analyzer(analyzer: String): SimpleStringQuery           = copy(analyzer = analyzer.some)
  def analyzer(analyzer: Analyzer): SimpleStringQuery         = copy(analyzer = analyzer.name.some)
  def queryName(queryName: String): SimpleStringQuery         = copy(queryName = queryName.some)
  def defaultOperator(op: String): SimpleStringQuery          = copy(operator = op.some)

  def lenient(lenient: Boolean): SimpleStringQuery = copy(lenient = lenient.some)

  def minimumShouldMatch(minimumShouldMatch: Int): SimpleStringQuery =
    copy(minimumShouldMatch = minimumShouldMatch.some)

  def analyzeWildcard(analyzeWildcard: Boolean): SimpleStringQuery =
    copy(analyzeWildcard = analyzeWildcard.some)

  def asfields(fields: String*): SimpleStringQuery          = copy(fields = this.fields ++ fields.map(f => (f, -1D)))
  def field(name: String): SimpleStringQuery                = copy(fields = fields :+ (name, -1D))
  def field(name: String, boost: Double): SimpleStringQuery = copy(fields = fields :+ (name, boost))
}


text:
import {Main} from '../../main/main';
import {Project, SelectedProject, autoinject} from '../../shared/index';

@autoinject()
export class Screen {
  tabs: Element;
  activeTab = 'Global';

  constructor(private main: Main,
              private selectedProject: SelectedProject) {}

  attached() {
    $(this.tabs).on('show.bs.tab', (e) => {
      this.activeTab = $(e.target).text();
    });
  }

  goBack() {
    this.main.returnToPluginList();
  }
}


text:
<?php namespace TeamWorkPm\Category;

class Message extends Model
{
}


text:
<?php

namespace OpenOrchestra\FrontBundle\SubQuery\Strategies;

use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Class DeviceSubQueryStrategy
 */
class DeviceSubQueryStrategy extends AbstractRequestSubQueryStrategy
{
    /** @var  string */
    private $deviceTypeField;

    /**
     * @param RequestStack $requestStack
     * @param string       $deviceTypeField
     */
    public function __construct(RequestStack $requestStack, $deviceTypeField = 'x-ua-device')
    {
        parent::__construct($requestStack);

        $this->deviceTypeField = $deviceTypeField;
    }

    /**
     * @param string $blockParameter
     *
     * @return bool
     */
    public function support($blockParameter)
    {
        return 'device' === $blockParameter;
    }

    /**
     * @param string $blockParameter
     *
     * @return array
     */
    public function generate($blockParameter)
    {
        $request = $this->requestStack->getMasterRequest();

        if ($device = $request->headers->get($this->deviceTypeField)) {
            return array($this->deviceTypeField => $device);
        }

        return array();
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'device';
    }
}


text:
    区域:
    <select class="form-control" id="location" name="location" data-options="locations" data-collection-id="business">

    </select>

    职位:
    <select class="form-control" id="type" name="type" data-options="types" data-collection-id="business">

    </select>

    区域
    职位
    姓名
    从属
    运营核对
    关联

加载中,请稍候


text:
package org.waveprotocol.wave.client;

import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.user.client.Command;
import org.waveprotocol.box.stat.Timer;
import org.waveprotocol.box.stat.Timing;

import org.waveprotocol.wave.client.common.util.AsyncHolder;
import org.waveprotocol.wave.client.common.util.AsyncHolder.Accessor;
import org.waveprotocol.wave.client.scheduler.Scheduler;
import org.waveprotocol.wave.client.scheduler.SchedulerInstance;

/**
 * Loads Undercurrent's stages in order.
 *
 */
public abstract class Stages {

  /** Continuation for when the last stage is loaded. */
  private Command whenFinished;

  /**
   * Creates a new stage loading sequence.
   */
  public Stages() {
  }

  /** @return the provider that loads stage zero. */
  protected AsyncHolder<StageZero> createStageZeroLoader() {
    return new StageZero.DefaultProvider();
  }

  /** @return the provider that loads stage one. */
  protected AsyncHolder<StageOne> createStageOneLoader(StageZero zero) {
    return new StageOne.DefaultProvider(zero);
  }

  /** @return the provider that loads stage two. */
  protected abstract AsyncHolder<StageTwo> createStageTwoLoader(StageOne one);

  /** @return the provider that loads stage three. */
  protected AsyncHolder<StageThree> createStageThreeLoader(StageTwo two) {
    return new StageThree.DefaultProvider(two);
  }

  /**
   * Initiates the load sequence.
   *
   * @param whenFinished optional command to run once the last stage is loaded
   */
  public final void load(Command whenFinished) {
    this.whenFinished = whenFinished;
    loadStageZero();
  }

  private void loadStageZero() {
    final Timer timer = Timing.start("Stage Zero");
    createStageZeroLoader().call(new Accessor<StageZero>() {
      @Override
      public void use(StageZero x) {
        Timing.stop(timer);
        loadStageOne(x);
      }
    });
  }

  private void loadStageOne(final StageZero zero) {
    final Timer timer = Timing.start("Stage One");
    createStageOneLoader(zero).call(new Accessor<StageOne>() {
      @Override
      public void use(StageOne x) {
        Timing.stop(timer);
        loadStageTwo(x);
      }
    });
  }

  private void loadStageTwo(final StageOne one) {
    final Timer timer = Timing.start("Stage Two");
    SchedulerInstance.getHighPriorityTimer().schedule(new Scheduler.Task() {

      @Override
      public void execute() {
        createStageTwoLoader(one).call(new Accessor<StageTwo>() {
          @Override
          public void use(StageTwo x) {
            Timing.stop(timer);
            loadStageThree(x);
          }
        });
      }
    });
  }

  private void loadStageThree(final StageTwo two) {
    final Timer timer = Timing.start("Stage Tree");
    SchedulerInstance.getHighPriorityTimer().schedule(new Scheduler.Task() {

      @Override
      public void execute() {
        createStageThreeLoader(two).call(new Accessor<StageThree>() {
          @Override
          public void use(StageThree x) {
            Timing.stop(timer);
            finish();
          }
        });
      }
    });
  }

  private void finish() {
    if (whenFinished != null) {
      whenFinished.execute();
      whenFinished = null;
    }
  }

  private static abstract class SimpleAsyncCallback implements RunAsyncCallback {
    @Override
    public void onFailure(Throwable reason) {
      throw new RuntimeException(reason);
    }
  }
}


text:
<!DOCTYPE html>

    <meta http-equiv="refresh" content="0;URL=constant.SDL_SCANCODE_COMPUTER.html">

Redirecting to constant.SDL_SCANCODE_COMPUTER.html...


text:
function features_matrix(files_in,files_out,opt)
%FEATURES_MATRIX creates feature matrix from samples
%   FEATURES_MATRIX creates feature matrix from samples. formatted
%   for use with PSOM pipeline
%
%   Input
%   -----
%   files_in (string/cell/struct)
%       file name(s) of samples file(s) to process, see output of
%       bricks.lattice_filter_sources
%
%       struct version
%       contains fields test and train each containing a file name of a
%       list of samples to process, requires the file_in_field parameter
%   files_out (string)
%       output file name
%   opt (cell array)
%       function options specified as name value pairs. unmatched name
%       value pairs are passed onto the data2feature function
%
%   Parameters
%   ----------
%   data2feature (string)
%       function name that converts a list of data files to a feature
%       matrix
%
%       [samples,class_labels,feature_labels] = data2feature(file_list);
%
%   file_in_field (string, default = '')
%       name of the field in files_in to select as the input file for the
%       function
%
%   DEPRECATED? threshold (scalar, default = 'none')
%       threshold for lattice coefficient values, samples containing
%       coefficents above this value are removed
%
%   Output
%   ------
%   output data contains the following fields
%
%   feature_labels (cell array)
%       feature labels
%   samples (matrix)
%       feature matrix with size [samples features]
%   class_labels (vector)
%       class labels for each sample

p = inputParser;
p.KeepUnmatched = true;
p.StructExpand = false;
addRequired(p,'files_in',@(x) ischar(x) | isstruct(x) | iscell(x));
addRequired(p,'files_out',@ischar);
addParameter(p,'data2feature',@ischar);
addParameter(p,'file_in_field','',@ischar);
parse(p,files_in,files_out,opt{:});

if ~isparfor()
    % fail if parallel execution isn't set up
    return;
end

% load the file list
if isstruct(files_in)
    if isempty(p.Results.file_in_field)
        error('file_in_field parameter is required');
    end
    file_list = files_in.(p.Results.file_in_field);
else
    file_list = files_in;
end

% convert data
fh = str2func(p.Results.data2feature);
opt_fh = struct2namevalue(p.Unmatched);
[samples,class_labels,feature_labels] = fh(file_list,opt_fh{:});

% save data
data = [];
data.feature_labels = feature_labels;
data.samples = samples;
data.class_labels = class_labels;
save(files_out, 'data','-v7.3');

end


text:
<main class="profile">
  <profile-feed
    ng-repeat="profile in profileCtrl.profiles"
    profile="profile">
  </profile-feed>
</main>


text:
Simple Metrics
==============

Introduction
------------
Simple Metrics was built as a NodeJS module which enables the end user to track and manage metrics of a collection of measurements of performance over time. The module natively supports displaying time and delta time information in the format of microseconds.

Installation
------------
`npm install --save simple-metrics`

Usage
-----
```
var myMetrics = require('simple-metrics');

//start() inits a new metric measurement and returns a
//guid to refer to this particular metric measurement.
var guid = myMetrics.start();

//... Do work to be performance tested ...

//stop() records the endTime and elapsedTime
//of a particular metric measurement and returns the
//elapsed time
var elapsed = myMetrics.stop(guid);

//getAllMetrics() returns an object literal collection of
//metric measurement objects.
console.log(myMetrics.getAllMetrics());
myMetrics.clear();

```

Features
--------

API
---
Detailed API information can be found by (obviously ;) ) reading the source or by checking the README in the src dir: https://github.com/tpennetta/simple-metrics/tree/master/src


text:
*Avalanche* is mostly about making the life of a continual learning researcher easier.

Below, you can see the main Avalanche modules and how they interact with each other.

![avalanche](https://raw.githubusercontent.com/ContinualAI/avalanche/master/docs/gitbook/.gitbook/assets/avalanche.png)

**What are the three pillars of any respectful continual learning research project?**

1. **`Benchmarks`**: Machine learning researchers need multiple benchmarks with efficient data handling utils to design and prototype new algorithms. Quantitative results on ever-changing benchmarks has been one of the driving forces of *Deep Learning*.
1. **`Training`**: Efficient implementation and training of continual learning algorithms; comparisons with other baselines and state-of-the-art methods become fundamental to asses the quality of an original algorithmic proposal.
1. **`Evaluation`**: *Training* utils and *Benchmarks* are not enough alone to push continual learning research forward. Comprehensive and sound *evaluation protocols* and *metrics* need to be employed as well.

**With Avalanche, you can find all these three fundamental pieces together and much more, in a single and coherent, well-maintained codebase.**

Let's take a quick tour on how you can use Avalanche for your research projects with a **5-minutes guide**, for *researchers on the run*!

Let's first **install Avalanche**. Please, check out our [How to Install](https://avalanche.continualai.org/getting-started/how-to-install) guide for further details.

```python
!pip install avalanche-lib==0.2.0
```

## 🏛 General Architecture

Avalanche is organized in five main modules:

1. **Benchmarks**: This module maintains a uniform API for data handling: mostly generating a stream of data from one or more datasets. It contains all the major CL benchmarks (similar to what has been done for [torchvision](https://pytorch.org/docs/stable/torchvision/index.html)).
1. **Training**: This module provides all the necessary utilities concerning model training. This includes simple and efficient ways of implement new continual learning strategies as well as a set pre-implemented CL baselines and state-of-the-art algorithms you will be able to use for comparison!
1. **Evaluation**: This modules provides all the utilities and metrics that can help in evaluating a CL algorithm with respect to all the factors we believe to be important for a continually learning system.
1. **Models**: In this module you'll be able to find several model architectures and pre-trained models that can be used for your continual learning experiment (similar to what has been done in [torchvision.models](https://pytorch.org/docs/stable/torchvision/index.html)).
1. **Logging**: It includes advanced logging and plotting features, including native stdout, file and [Tensorboard](https://www.tensorflow.org/tensorboard) support (How cool it is to have a complete, interactive dashboard, tracking your experiment metrics in real-time with a single line of code?)

In the graphic below, you can see how Avalanche sub-modules are available and organized as well:
{% code title="Avalanche Main Modules and Sub-Modules" %}
```text
Avalanche
├── Benchmarks
│   ├── Classic
│   ├── Datasets
│   ├── Generators
│   ├── Scenarios
│   └── Utils
├── Evaluation
│   ├── Metrics
|   └── Utils
├── Training
│   ├── Strategies
│   ├── Plugins
|   └── Utils
├── Models
└── Loggers

```
{% endcode %}
We will learn more about each of them during this tutorial series, but keep in mind that the [Avalanche API documentation](https://avalanche-api.continualai.org/en/latest/) is your friend as well!

All right, let's start with the benchmarks module right away 👇

## 📚 Benchmarks

The benchmark module offers three main features:
1. **Datasets**: a comprehensive list of PyTorch Datasets ready to use (It includes all the Torchvision Datasets and more!).
1. **Classic Benchmarks**: a set of classic Continual Learning Benchmarks ready to be used (there can be multiple benchmarks based on a single dataset).
1. **Generators**: a set of functions you can use to generate your own benchmark starting from any PyTorch Dataset!

### Datasets

Datasets can be imported in Avalanche as simply as:

```python
from avalanche.benchmarks.datasets import MNIST, FashionMNIST, KMNIST, EMNIST, \
    QMNIST, FakeData, CocoCaptions, CocoDetection, LSUN, ImageNet, CIFAR10, \
    CIFAR100, STL10, SVHN, PhotoTour, SBU, Flickr8k, Flickr30k, VOCDetection, \
    VOCSegmentation, Cityscapes, SBDataset, USPS, Kinetics400, HMDB51, UCF101, \
    CelebA, CORe50Dataset, TinyImagenet, CUB200, OpenLORIS, MiniImageNetDataset, Stream51, \
    CLEARDataset
```

Of course, you can use them as you would use any *PyTorch Dataset*.

### Benchmarks Basics

The *Avalanche* benchmarks (instances of the Scenario class), contains several attributes that describe the benchmark. However, the most important ones are the `train` and `test` streams.

In *Avalanche* we often suppose to have access to these **two parallel stream** of data (even though some benchmarks may not provide such feature, but contain just a unique test set).

Each of these `streams` are iterable, indexable and sliceable objects that are composed of **experiences**. Experiences are batch of data (or "tasks") that can be provided with or without a specific task label.

### Classic Benchmarks

*Avalanche* maintains a set of commonly used benchmarks built on top of one or multiple datasets.

```python
from avalanche.benchmarks.classic import CORe50, SplitTinyImageNet, SplitCIFAR10, \
    SplitCIFAR100, SplitCIFAR110, SplitMNIST, RotatedMNIST, PermutedMNIST, SplitCUB200

# creating the benchmark (scenario object)
perm_mnist = PermutedMNIST(
    n_experiences=3,
    seed=1234,
)

# recovering the train and test streams
train_stream = perm_mnist.train_stream
test_stream = perm_mnist.test_stream

# iterating over the train stream
for experience in train_stream:
    print("Start of task ", experience.task_label)
    print('Classes in this task:', experience.classes_in_this_experience)

    # The current Pytorch training set can be easily recovered through the
    # experience
    current_training_set = experience.dataset
    # ...as well as the task_label
    print('Task {}'.format(experience.task_label))
    print('This task contains', len(current_training_set), 'training examples')

    # we can recover the corresponding test experience in the test stream
    current_test_set = test_stream[experience.current_experience].dataset
    print('This task contains', len(current_test_set), 'test examples')
```

### Benchmarks Generators

What if we want to create a new benchmark that is not present in the *"Classic"* ones? Well, in that case Avalanche offers a number of utilities that you can use to create your own benchmark with maximum flexibility: the **benchmark generators**!

The *specific* scenario generators are useful when starting from one or multiple PyTorch datasets and you want to create a **"New Instances"** or **"New Classes"** benchmark: i.e. it supports the easy and flexible creation of a *Domain-Incremental*, *Class-Incremental* or *Task-Incremental* scenarios among others.

```python
from avalanche.benchmarks.generators import nc_benchmark, ni_benchmark
from torchvision.datasets import MNIST

mnist_train = MNIST('.', train=True, download=True)
mnist_test = MNIST('.', train=False)

benchmark = ni_benchmark(
    mnist_train, mnist_test, n_experiences=10, shuffle=True, seed=1234,
    balance_experiences=True
)
benchmark = nc_benchmark(
    mnist_train, mnist_test, n_experiences=10, shuffle=True, seed=1234,
    task_labels=False
)
```

Finally, if your ideal benchmark does not fit well in the aforementioned *Domain-Incremental*, *Class-Incremental* or *Task-Incremental* scenarios, you can always use our **generic generators**:
* **filelist_benchmark**
* **paths_benchmark**
* **dataset_benchmark**
* **tensors_benchmark**

```python
from avalanche.benchmarks.generators import filelist_benchmark, dataset_benchmark, \
                                            tensors_benchmark, paths_benchmark
```

You can read more about how to use them the full *Benchmarks* module tutorial!

## 💪 Training

The `training` module in *Avalanche* is build on modularity and it has two main goals:
1. provide a set of standard *continual learning baselines* that can be easily run for comparison;
1. provide the necessary utilities to **implement and run your own strategy** in the most efficient and simple way possible thanks to the building blocks we already prepared for you.

### Strategies

If you want to compare your strategy with other classic continual learning algorithms or baselines, in *Avalanche* this is as simple as creating an object:

```python
from avalanche.models import SimpleMLP
from avalanche.training import Naive, CWRStar, Replay, GDumb, \
    Cumulative, LwF, GEM, AGEM, EWC, AR1
from torch.optim import SGD
from torch.nn import CrossEntropyLoss

model = SimpleMLP(num_classes=10)
cl_strategy = Naive(
    model, SGD(model.parameters(), lr=0.001, momentum=0.9),
    CrossEntropyLoss(), train_mb_size=100, train_epochs=4, eval_mb_size=100
)
```

### Create your own Strategy

The simplest way to build your own strategy is to create a python class that implements the main `train` and `eval` methods.

Let's define our Continual Learning algorithm *"MyStrategy"* as a simple python class:

```python
from torch.utils.data import DataLoader

class MyStrategy():
    """My Basic Strategy"""

    def __init__(self, model, optimizer, criterion):
        self.model = model
        self.optimizer = optimizer
        self.criterion = criterion

    def train(self, experience):
        # here you can implement your own training loop for each experience (i.e.
        # batch or task).

        train_dataset = experience.dataset
        t = experience.task_label
        train_data_loader = DataLoader(
            train_dataset, num_workers=4, batch_size=128
        )

        for epoch in range(1):
            for mb in train_data_loader:
                # you magin here...
                pass

    def eval(self, experience):
        # here you can implement your own eval loop for each experience (i.e.
        # batch or task).

        eval_dataset = experience.dataset
        t = experience.task_label
        eval_data_loader = DataLoader(
            eval_dataset, num_workers=4, batch_size=128
        )

        # eval here
```

Then, we can use our strategy as we would do for the pre-implemented ones:

```python
from avalanche.models import SimpleMLP
from avalanche.benchmarks import SplitMNIST

# Benchmark creation
benchmark = SplitMNIST(n_experiences=5)

# Model Creation
model = SimpleMLP(num_classes=benchmark.n_classes)

# Create the Strategy Instance (MyStrategy)
cl_strategy = MyStrategy(
    model, SGD(model.parameters(), lr=0.001, momentum=0.9),
    CrossEntropyLoss())

# Training Loop
print('Starting experiment...')

for exp_id, experience in enumerate(benchmark.train_stream):
    print("Start of experience ", experience.current_experience)

    cl_strategy.train(experience)
    print('Training completed')

    print('Computing accuracy on the current test set')
    cl_strategy.eval(benchmark.test_stream[exp_id])
```

While this is the easiest possible way to add your own strategy, *Avalanche* supports more sophisticated modalities (based on *callbacks*) that lets you write **more neat**, modular and reusable code**, inheriting functionality from a parent classes and using **pre-implemented plugins**.

Check out more details about what Avalanche can offer in this module following the *"Training"* chapter of the **"From Zero to Hero"** tutorial!

## 📈 Evaluation

The `evaluation` module is quite straightforward: it offers all the basic functionalities to evaluate and keep track of a continual learning experiment.

This is mostly done through the **Metrics** and the **Loggers**. The **Metrics** provide a set of classes which implements the main continual learning metrics like Accuracy, Forgetting, Memory Usage, Running Times, etc.
Metrics should be created via the utility functions (e.g. `accuracy_metrics`, `timing_metrics` and others) specifying in the arguments when those metrics should be computed (after each minibatch, epoch, experience etc...).
The **Loggers** specify a way to report the metrics (e.g. with Tensorboard, on console or others). Loggers are created by instantiating the respective class.

Metrics and loggers interact via the **Evaluation Plugin**: this is the main object responsible of tracking the experiment progress. Metrics and loggers are directly passed to the `EvaluationPlugin` instance. You will see the output of the loggers automatically during training and evaluation! Let's see how to put this together in few lines of code:

```python
# utility functions to create plugin metrics
from avalanche.evaluation.metrics import accuracy_metrics, loss_metrics, forgetting_metrics
from avalanche.logging import InteractiveLogger, TensorboardLogger
from avalanche.training.plugins import EvaluationPlugin

eval_plugin = EvaluationPlugin(
    # accuracy after each training epoch
    # and after each evaluation experience
    accuracy_metrics(epoch=True, experience=True),
    # loss after each training minibatch and each
    # evaluation stream
    loss_metrics(minibatch=True, stream=True),
    # catastrophic forgetting after each evaluation
    # experience
    forgetting_metrics(experience=True, stream=True),
    # add as many metrics as you like
    loggers=[InteractiveLogger(), TensorboardLogger()])

# pass the evaluation plugin instance to the strategy
# strategy = EWC(..., evaluator=eval_plugin)

# THAT'S IT!!
```

For more details about the evaluation module (how to write new metrics/loggers, a deeper tutorial on metrics) check out the extended guide in the *"Evaluation"* chapter of the **"From Zero to Hero"** *Avalanche* tutorial!

## 🔗 Putting all Together

You've learned how to install *Avalanche*, how to create benchmarks that can suit your needs, how you can create your own continual learning algorithm and how you can evaluate its performance.

Here we show how you can use all these modules together to **design your experiments** as quantitative supporting evidence for your research project or paper.

```python
from avalanche.benchmarks.classic import SplitMNIST
from avalanche.evaluation.metrics import forgetting_metrics, accuracy_metrics,\
    loss_metrics, timing_metrics, cpu_usage_metrics, StreamConfusionMatrix,\
    disk_usage_metrics, gpu_usage_metrics
from avalanche.models import SimpleMLP
from avalanche.logging import InteractiveLogger, TextLogger, TensorboardLogger
from avalanche.training.plugins import EvaluationPlugin
from avalanche.training import Naive

from torch.optim import SGD
from torch.nn import CrossEntropyLoss

benchmark = SplitMNIST(n_experiences=5)

# MODEL CREATION
model = SimpleMLP(num_classes=benchmark.n_classes)

# DEFINE THE EVALUATION PLUGIN and LOGGERS
# The evaluation plugin manages the metrics computation.
# It takes as argument a list of metrics, collectes their results and returns
# them to the strategy it is attached to.

# log to Tensorboard
tb_logger = TensorboardLogger()

# log to text file
text_logger = TextLogger(open('log.txt', 'a'))

# print to stdout
interactive_logger = InteractiveLogger()

eval_plugin = EvaluationPlugin(
    accuracy_metrics(minibatch=True, epoch=True, experience=True, stream=True),
    loss_metrics(minibatch=True, epoch=True, experience=True, stream=True),
    timing_metrics(epoch=True),
    cpu_usage_metrics(experience=True),
    forgetting_metrics(experience=True, stream=True),
    StreamConfusionMatrix(num_classes=benchmark.n_classes, save_image=False),
    disk_usage_metrics(minibatch=True, epoch=True, experience=True, stream=True),
    loggers=[interactive_logger, text_logger, tb_logger],
    benchmark=benchmark
)

# CREATE THE STRATEGY INSTANCE (NAIVE)
cl_strategy = Naive(
    model, SGD(model.parameters(), lr=0.001, momentum=0.9),
    CrossEntropyLoss(), train_mb_size=500, train_epochs=1, eval_mb_size=100,
    evaluator=eval_plugin)

# TRAINING LOOP
print('Starting experiment...')
results = []
for experience in benchmark.train_stream:
    print("Start of experience: ", experience.current_experience)
    print("Current Classes: ", experience.classes_in_this_experience)

    # train returns a dictionary which contains all the metric values
    res = cl_strategy.train(experience, num_workers=4)
    print('Training completed')

    print('Computing accuracy on the whole test set')
    # eval also returns a dictionary which contains all the metric values
    results.append(cl_strategy.eval(benchmark.test_stream, num_workers=4))
```

## 🤝 Run it on Google Colab

You can run _this chapter_ and play with it on Google Colaboratory: [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ContinualAI/avalanche/blob/master/notebooks/getting-started/learn-avalanche-in-5-minutes.ipynb)


text:
## HiddenInitializationView ⇐ BaseView
**Kind**: global class
**Extends:** BaseView

* [HiddenInitializationView](#HiddenInitializationView) ⇐ BaseView
  * [new HiddenInitializationView()](#new_HiddenInitializationView_new)
  * [.autoRender](#HiddenInitializationView#autoRender)

### new HiddenInitializationView()
View allows hide part of DOM tree till all page components will be initialized

Usage sample:

> Please note that all div's attributes are required for valid work.

```html
<div class="invisible"
        data-page-component-module="oroui/js/app/components/view-component"
        data-page-component-options="{'view': 'oroui/js/app/views/hidden-initialization-view'}"
        data-layout="separate">
    <!-- write anything here -->
</div>
```

### hiddenInitializationView.autoRender
**Kind**: instance class of [HiddenInitializationView](#HiddenInitializationView)


text:
const test = require('ava');
const _ = require('lodash');
const TestUtils = require('fountain-generator').TestUtils;
const u = require('../utils');

let context;

function merge(args) {
  const result = {};
  _.mergeWith(result, ...args, (x, y) => {
    if (_.isArray(x)) {
      return _.uniq(x.concat(y));
    }
  });
  return result;
}

test.before(() => {
  context = TestUtils.mock('app');
  require('../../generators/app/index');
});

test.beforeEach(() => {
  context.mergeJson['package.json'] = {};
});

test('Configuring package.json: angular1/webpack/babel', t => {
  TestUtils.call(context, 'configuring.pkg', {framework: 'angular1', modules: 'webpack', js: 'babel'});
  const expected = merge([u.angular1Base, u.base, u.webpackBase, u.babelBase, u.babelTypescriptBase]);
  t.deepEqual(context.mergeJson['package.json'], expected);
});

test('Configuring package.json: angular1/webpack/js', t => {
  TestUtils.call(context, 'configuring.pkg', {framework: 'angular1', modules: 'webpack', js: 'js'});
  const expected = merge([u.angular1Base, u.base, u.webpackBase, u.jsBase]);
  t.deepEqual(context.mergeJson['package.json'], expected);
});

test('Configuring package.json: angular1/webpack/ts', t => {
  TestUtils.call(context, 'configuring.pkg', {framework: 'angular1', modules: 'webpack', js: 'typescript'});
  const expected = merge([u.angular1Base, u.base, u.webpackBase, u.babelTypescriptBase]);
  t.deepEqual(context.mergeJson['package.json'], expected);
});

test('Configuring package.json: angular1/systemjs/babel', t => {
  TestUtils.call(context, 'configuring.pkg', {framework: 'angular1', modules: 'systemjs', js: 'babel'});
  const expected = merge([u.angular1Base, u.base, u.systemjsBase, u.babelBase, u.babelTypescriptBase, {
    jspm: {
      dependencies: {babel: 'npm:babel-core@^6.23.1'},
      devDependencies: {'plugin-babel': 'npm:systemjs-plugin-babel@^0.0.21'}
    }
  }]);
  t.deepEqual(context.mergeJson['package.json'], expected);
});

test('Configuring package.json: angular1/systemjs/js', t => {
  TestUtils.call(context, 'configuring.pkg', {framework: 'angular1', modules: 'systemjs', js: 'js'});
  const expected = merge([u.angular1Base, u.base, u.systemjsBase, u.jsBase, {
    jspm: {
      dependencies: {babel: 'npm:babel-core@^6.23.1'},
      devDependencies: {'plugin-babel': 'npm:systemjs-plugin-babel@^0.0.21'}
    }
  }]);
  t.deepEqual(context.mergeJson['package.json'], expected);
});

test('Configuring package.json: angular1/systemjs/ts', t => {
  TestUtils.call(context, 'configuring.pkg', {framework: 'angular1', modules: 'systemjs', js: 'typescript'});
  const expected = merge([u.angular1Base, u.base, u.systemjsBase, u.babelTypescriptBase]);
  t.deepEqual(context.mergeJson['package.json'], expected);
});

test('Configuring package.json: angular1/inject/babel', t => {
  TestUtils.call(context, 'configuring.pkg', {framework: 'angular1', modules: 'inject', js: 'babel'});
  const expected = merge([u.angular1Base, u.base, u.injectBase, u.injectAngular1, u.babelBase, u.babelTypescriptBase, {
    devDependencies: {'gulp-babel': '^6.1.2'}
  }]);
  t.deepEqual(context.mergeJson['package.json'], expected);
});

test('Configuring package.json: angular1/inject/js', t => {
  TestUtils.call(context, 'configuring.pkg', {framework: 'angular1', modules: 'inject', js: 'js'});
  const expected = merge([u.angular1Base, u.base, u.injectBase, u.injectAngular1, u.jsBase]);
  t.deepEqual(context.mergeJson['package.json'], expected);
});

test('Configuring package.json: angular1/inject/ts', t => {
  TestUtils.call(context, 'configuring.pkg', {framework: 'angular1', modules: 'inject', js: 'typescript'});
  const expected = merge([u.angular1Base, u.base, u.injectBase, u.injectAngular1, u.babelTypescriptBase]);
  t.deepEqual(context.mergeJson['package.json'], expected);
});

test('Configuring package.json: angular1/inject/js/todoMVC', t => {
  TestUtils.call(context, 'configuring.pkg', {framework: 'angular1', modules: 'inject', js: 'js', sample: 'todoMVC'});
  const expected = merge([u.angular1Base, u.base, u.injectBase, u.injectAngular1, u.jsBase, {
    eslintConfig: {
      rules: {
        'no-undef': 0,
        'no-unused-vars': 0
      }
    }
  }]);
  t.deepEqual(context.mergeJson['package.json'], expected);
});


text:
/// @file
/// @copyright The code is licensed under the BSD License
///            <http://opensource.org/licenses/BSD-2-Clause>,
///            Copyright (c) 2013-2015 Alexandre Hamez.
/// @author Alexandre Hamez

#pragma once

#include <cereal/archives/json.hpp>
#include <cereal/types/deque.hpp>

#include <sdd/tools/serialization.hh>

#include "conf/configuration.hh"
#include "mc/shared/statistics.hh"

namespace pnmc { namespace mc { namespace shared {

/*------------------------------------------------------------------------------------------------*/

template <typename Archive, typename C>
void
save(Archive& archive, const statistics<C>& s)
{
  if (s.max_time)
  {
    archive(cereal::make_nvp("time limit", s.max_time->count()));
  }
  archive( cereal::make_nvp("interrupted", s.interrupted)
         , cereal::make_nvp("total time", s.total_duration.count())
         , cereal::make_nvp("relation time", s.relation_duration.count())
         , cereal::make_nvp("rewrite time", s.rewrite_duration.count())
         , cereal::make_nvp("state space time", s.state_space_duration.count()));
  if (s.force_duration)
  {
    archive(cereal::make_nvp("FORCE time", s.force_duration->count()));
  }
  if (s.dead_states_duration)
  {
    archive(cereal::make_nvp("dead states time", s.dead_states_duration->count()));
  }
  if (s.trace_duration)
  {
    archive(cereal::make_nvp("trace time", s.trace_duration->count()));
  }
  if (s.tokens_duration)
  {
    archive(cereal::make_nvp("count tokens time", s.tokens_duration->count()));
  }
  if (s.manager_statistics)
  {
    archive(cereal::make_nvp("libsdd", *s.manager_statistics));
  }
  if (s.force_spans)
  {
    archive(cereal::make_nvp("FORCE spans", *s.force_spans));
  }
  if (s.sdd_ut_size)
  {
    archive(cereal::make_nvp("sdd samples", *s.sdd_ut_size));
  }
  if (s.pn_statistics)
  {
    archive(cereal::make_nvp("pn", *s.pn_statistics));
  }
  if (s.sdd_statistics)
  {
    archive(cereal::make_nvp("state space", *s.sdd_statistics));
  }
}

/*------------------------------------------------------------------------------------------------*/

}}} // namespace pnmc::mc::shared


text:
module SessionsHelper

  def current_student
    remember_token = Student.hash(cookies[:remember_token])
    @current_student ||= Student.find_by(remember_token: remember_token)
  end

  def current_student= (student)
    @current_student = student
  end

  def current_student?(student)
    current_student == student
  end

  def signed_in?
    !current_student.nil?
  end

  def sign_in(student)
    remember_token = Student.new_remember_token
    cookies.permanent[:remember_token] = remember_token
    student.update_attribute(:remember_token, Student.hash(remember_token))
    self.current_student = student
  end

  def sign_out
    current_student.update_attribute(:remember_token, Student.hash(Student.new_remember_token))
    cookies.delete(:remember_token)
    self.current_student = nil
  end

  # Location Management methods:

  def store_location
    if request.get?
      session[:return_to] = request.url
    end
  end

  def redirect_back_or(default)
    redirect_to(session[:return_to] || default)
    session.delete(:return_to)
  end

  def require_signin
    if !signed_in?
      store_location
      flash[:notice] = "Please sign in."
      redirect_to signin_url
    end
  end
end


text:
<!DOCTYPE html>

  <meta charset="utf-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>metpy.calc.kinematics — MetPy 0.11</title>

    <link rel="shortcut icon" href="../../../_static/metpy_32x32.ico"/>

    <link rel="canonical" href="https://unidata.github.io/MetPy/latest/_modules/metpy/calc/kinematics.html"/>

    <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />

    <link rel="stylesheet" href="../../../_static/gallery.css" type="text/css" />

    <link rel="stylesheet" href="../../../_static/theme_override.css" type="text/css" />

        <link rel="index" title="Index"
              href="../../../genindex.html"/>
        <link rel="search" title="Search" href="../../../search.html"/>
    <link rel="top" title="MetPy 0.11" href="../../../index.html"/>
        <link rel="up" title="Module code" href="../../index.html"/>

             MetPy

                    <select class="version-list" id="version-list">
                      <option value=''>0.11</option>

                          <option value="../latest">latest</option>

                          <option value="../dev">dev</option>

                    </select>

  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>

Installation Guide

Getting Started with MetPy

Unit Support

Reference/API Examples

Topical Examples

Tutorials

Reference Guide

MetPy Roadmap

GEMPAK Conversion Guide

Support

Contributors Guide

Infrastructure Guide

Citing MetPy

References

          MetPy

Docs »

Module code »

metpy.calc.kinematics

Source code for metpy.calc.kinematics

# Copyright (c) 2009,2017,2018,2019 MetPy Developers.
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause
"""Contains calculation of kinematic parameters (e.g. divergence or vorticity)."""
from __future__ import division

import functools
import warnings

import numpy as np

from . import coriolis_parameter
from .tools import first_derivative, get_layer_heights, gradient
from .. import constants as mpconsts
from ..cbook import is_string_like, iterable
from ..package_tools import Exporter
from ..units import atleast_2d, check_units, concatenate, units
from ..xarray import preprocess_xarray

exporter = Exporter(globals())

def _stack(arrs):
    return concatenate([a[np.newaxis] if iterable(a) else a for a in arrs], axis=0)

def _is_x_first_dim(dim_order):
    """Determine whether x is the first dimension based on the value of dim_order."""
    if dim_order is None:
        dim_order = 'yx'
    return dim_order == 'xy'

def _check_and_flip(arr):
    """Transpose array or list of arrays if they are 2D."""
    if hasattr(arr, 'ndim'):
        if arr.ndim >= 2:
            return arr.T
        else:
            return arr
    elif not is_string_like(arr) and iterable(arr):
        return tuple(_check_and_flip(a) for a in arr)
    else:
        return arr

def ensure_yx_order(func):
    """Wrap a function to ensure all array arguments are y, x ordered, based on kwarg."""
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        # Check what order we're given
        dim_order = kwargs.pop('dim_order', None)
        x_first = _is_x_first_dim(dim_order)

        # If x is the first dimension, flip (transpose) every array within the function args.
        if x_first:
            args = tuple(_check_and_flip(arr) for arr in args)
            for k, v in kwargs:
                kwargs[k] = _check_and_flip(v)

        ret = func(*args, **kwargs)

        # If we flipped on the way in, need to flip on the way out so that output array(s)
        # match the dimension order of the original input.
        if x_first:
            return _check_and_flip(ret)
        else:
            return ret

    # Inject a docstring for the dim_order argument into the function's docstring.
    dim_order_doc = """
    dim_order : str or ``None``, optional
        The ordering of dimensions in passed in arrays. Can be one of ``None``, ``'xy'``,
        or ``'yx'``. ``'xy'`` indicates that the dimension corresponding to x is the leading
        dimension, followed by y. ``'yx'`` indicates that x is the last dimension, preceded
        by y. ``None`` indicates that the default ordering should be assumed,
        which is 'yx'. Can only be passed as a keyword argument, i.e.
        func(..., dim_order='xy')."""

    # Find the first blank line after the start of the parameters section
    params = wrapper.__doc__.find('Parameters')
    blank = wrapper.__doc__.find('\n\n', params)
    wrapper.__doc__ = wrapper.__doc__[:blank] + dim_order_doc + wrapper.__doc__[blank:]

    return wrapper

[docs]@exporter.export
@preprocess_xarray
@ensure_yx_order
@check_units('[speed]', '[speed]', '[length]', '[length]')
def vorticity(u, v, dx, dy):
    r"""Calculate the vertical vorticity of the horizontal wind.

    Parameters
    ----------
    u : (M, N) `pint.Quantity`
        x component of the wind
    v : (M, N) `pint.Quantity`
        y component of the wind
    dx : `pint.Quantity`
        The grid spacing(s) in the x-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.
    dy : `pint.Quantity`
        The grid spacing(s) in the y-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.

    Returns
    -------
    (M, N) `pint.Quantity`
        vertical vorticity

    See Also
    --------
    divergence

    Notes
    -----
    If inputs have more than two dimensions, they are assumed to have either leading dimensions
    of (x, y) or trailing dimensions of (y, x), depending on the value of ``dim_order``.

    """
    dudy = first_derivative(u, delta=dy, axis=-2)
    dvdx = first_derivative(v, delta=dx, axis=-1)
    return dvdx - dudy

[docs]@exporter.export
@preprocess_xarray
@ensure_yx_order
@check_units(dx='[length]', dy='[length]')
def divergence(u, v, dx, dy):
    r"""Calculate the horizontal divergence of a vector.

    Parameters
    ----------
    u : (M, N) `pint.Quantity`
        x component of the vector
    v : (M, N) `pint.Quantity`
        y component of the vector
    dx : `pint.Quantity`
        The grid spacing(s) in the x-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.
    dy : `pint.Quantity`
        The grid spacing(s) in the y-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.

    Returns
    -------
    (M, N) `pint.Quantity`
        The horizontal divergence

    See Also
    --------
    vorticity

    Notes
    -----
    If inputs have more than two dimensions, they are assumed to have either leading dimensions
    of (x, y) or trailing dimensions of (y, x), depending on the value of ``dim_order``.

    """
    dudx = first_derivative(u, delta=dx, axis=-1)
    dvdy = first_derivative(v, delta=dy, axis=-2)
    return dudx + dvdy

[docs]@exporter.export
@preprocess_xarray
@ensure_yx_order
@check_units('[speed]', '[speed]', '[length]', '[length]')
def shearing_deformation(u, v, dx, dy):
    r"""Calculate the shearing deformation of the horizontal wind.

    Parameters
    ----------
    u : (M, N) `pint.Quantity`
        x component of the wind
    v : (M, N) `pint.Quantity`
        y component of the wind
    dx : `pint.Quantity`
        The grid spacing(s) in the x-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.
    dy : `pint.Quantity`
        The grid spacing(s) in the y-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.

    Returns
    -------
    (M, N) `pint.Quantity`
        Shearing Deformation

    See Also
    --------
    stretching_deformation, total_deformation

    Notes
    -----
    If inputs have more than two dimensions, they are assumed to have either leading dimensions
    of (x, y) or trailing dimensions of (y, x), depending on the value of ``dim_order``.

    """
    dudy = first_derivative(u, delta=dy, axis=-2)
    dvdx = first_derivative(v, delta=dx, axis=-1)
    return dvdx + dudy

[docs]@exporter.export
@preprocess_xarray
@ensure_yx_order
@check_units('[speed]', '[speed]', '[length]', '[length]')
def stretching_deformation(u, v, dx, dy):
    r"""Calculate the stretching deformation of the horizontal wind.

    Parameters
    ----------
    u : (M, N) `pint.Quantity`
        x component of the wind
    v : (M, N) `pint.Quantity`
        y component of the wind
    dx : `pint.Quantity`
        The grid spacing(s) in the x-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.
    dy : `pint.Quantity`
        The grid spacing(s) in the y-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.

    Returns
    -------
    (M, N) `pint.Quantity`
        Stretching Deformation

    See Also
    --------
    shearing_deformation, total_deformation

    Notes
    -----
    If inputs have more than two dimensions, they are assumed to have either leading dimensions
    of (x, y) or trailing dimensions of (y, x), depending on the value of ``dim_order``.

    """
    dudx = first_derivative(u, delta=dx, axis=-1)
    dvdy = first_derivative(v, delta=dy, axis=-2)
    return dudx - dvdy

[docs]@exporter.export
@preprocess_xarray
@ensure_yx_order
@check_units('[speed]', '[speed]', '[length]', '[length]')
def total_deformation(u, v, dx, dy):
    r"""Calculate the horizontal total deformation of the horizontal wind.

    Parameters
    ----------
    u : (M, N) `pint.Quantity`
        x component of the wind
    v : (M, N) `pint.Quantity`
        y component of the wind
    dx : `pint.Quantity`
        The grid spacing(s) in the x-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.
    dy : `pint.Quantity`
        The grid spacing(s) in the y-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.

    Returns
    -------
    (M, N) `pint.Quantity`
        Total Deformation

    See Also
    --------
    shearing_deformation, stretching_deformation

    Notes
    -----
    If inputs have more than two dimensions, they are assumed to have either leading dimensions
    of (x, y) or trailing dimensions of (y, x), depending on the value of ``dim_order``.

    """
    dudy, dudx = gradient(u, deltas=(dy, dx), axes=(-2, -1))
    dvdy, dvdx = gradient(v, deltas=(dy, dx), axes=(-2, -1))
    return np.sqrt((dvdx + dudy)**2 + (dudx - dvdy)**2)

[docs]@exporter.export
@preprocess_xarray
@ensure_yx_order
def advection(scalar, wind, deltas):
    r"""Calculate the advection of a scalar field by the wind.

    The order of the dimensions of the arrays must match the order in which
    the wind components are given.  For example, if the winds are given [u, v],
    then the scalar and wind arrays must be indexed as x,y (which puts x as the
    rows, not columns).

    Parameters
    ----------
    scalar : N-dimensional array
        Array (with N-dimensions) with the quantity to be advected.
    wind : sequence of arrays
        Length M sequence of N-dimensional arrays.  Represents the flow,
        with a component of the wind in each dimension.  For example, for
        horizontal advection, this could be a list: [u, v], where u and v
        are each a 2-dimensional array.
    deltas : sequence of float or ndarray
        A (length M) sequence containing the grid spacing(s) in each dimension. If using
        arrays, in each array there should be one item less than the size of `scalar` along the
        applicable axis.

    Returns
    -------
    N-dimensional array
        An N-dimensional array containing the advection at all grid points.

    """
    # This allows passing in a list of wind components or an array.
    wind = _stack(wind)

    # If we have more than one component, we need to reverse the order along the first
    # dimension so that the wind components line up with the
    # order of the gradients from the ..., y, x ordered array.
    if wind.ndim > scalar.ndim:
        wind = wind[::-1]

    # Gradient returns a list of derivatives along each dimension. We convert
    # this to an array with dimension as the first index. Reverse the deltas to line up
    # with the order of the dimensions.
    grad = _stack(gradient(scalar, deltas=deltas[::-1]))

    # Make them be at least 2D (handling the 1D case) so that we can do the
    # multiply and sum below
    grad, wind = atleast_2d(grad, wind)

    return (-grad * wind).sum(axis=0)

[docs]@exporter.export
@preprocess_xarray
@ensure_yx_order
@check_units('[temperature]', '[speed]', '[speed]', '[length]', '[length]')
def frontogenesis(thta, u, v, dx, dy, dim_order='yx'):
    r"""Calculate the 2D kinematic frontogenesis of a temperature field.

    The implementation is a form of the Petterssen Frontogenesis and uses the formula
    outlined in [Bluestein1993]_ pg.248-253.

    .. math:: F=\frac{1}{2}\left|\nabla \theta\right|[D cos(2\beta)-\delta]

    * :math:`F` is 2D kinematic frontogenesis
    * :math:`\theta` is potential temperature
    * :math:`D` is the total deformation
    * :math:`\beta` is the angle between the axis of dilitation and the isentropes
    * :math:`\delta` is the divergence

    Parameters
    ----------
    thta : (M, N) `pint.Quantity`
        Potential temperature
    u : (M, N) `pint.Quantity`
        x component of the wind
    v : (M, N) `pint.Quantity`
        y component of the wind
    dx : `pint.Quantity`
        The grid spacing(s) in the x-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.
    dy : `pint.Quantity`
        The grid spacing(s) in the y-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.

    Returns
    -------
    (M, N) `pint.Quantity`
        2D Frontogenesis in [temperature units]/m/s

    Notes
    -----
    If inputs have more than two dimensions, they are assumed to have either leading dimensions
    of (x, y) or trailing dimensions of (y, x), depending on the value of ``dim_order``.

    Conversion factor to go from [temperature units]/m/s to [temperature units/100km/3h]
    :math:`1.08e4*1.e5`

    """
    # Get gradients of potential temperature in both x and y
    ddy_thta = first_derivative(thta, delta=dy, axis=-2)
    ddx_thta = first_derivative(thta, delta=dx, axis=-1)

    # Compute the magnitude of the potential temperature gradient
    mag_thta = np.sqrt(ddx_thta**2 + ddy_thta**2)

    # Get the shearing, stretching, and total deformation of the wind field
    shrd = shearing_deformation(u, v, dx, dy, dim_order=dim_order)
    strd = stretching_deformation(u, v, dx, dy, dim_order=dim_order)
    tdef = total_deformation(u, v, dx, dy, dim_order=dim_order)

    # Get the divergence of the wind field
    div = divergence(u, v, dx, dy, dim_order=dim_order)

    # Compute the angle (beta) between the wind field and the gradient of potential temperature
    psi = 0.5 * np.arctan2(shrd, strd)
    beta = np.arcsin((-ddx_thta * np.cos(psi) - ddy_thta * np.sin(psi)) / mag_thta)

    return 0.5 * mag_thta * (tdef * np.cos(2 * beta) - div)

[docs]@exporter.export
@preprocess_xarray
@ensure_yx_order
@check_units(f='[frequency]', dx='[length]', dy='[length]')
def geostrophic_wind(heights, f, dx, dy):
    r"""Calculate the geostrophic wind given from the heights or geopotential.

    Parameters
    ----------
    heights : (M, N) `pint.Quantity`
        The height field, with either leading dimensions of (x, y) or trailing dimensions
        of (y, x), depending on the value of ``dim_order``.
    f : array_like
        The coriolis parameter.  This can be a scalar to be applied
        everywhere or an array of values.
    dx : `pint.Quantity`
        The grid spacing(s) in the x-direction. If an array, there should be one item less than
        the size of `heights` along the applicable axis.
    dy : `pint.Quantity`
        The grid spacing(s) in the y-direction. If an array, there should be one item less than
        the size of `heights` along the applicable axis.

    Returns
    -------
    A 2-item tuple of arrays, `pint.Quantity`
        A tuple of the u-component and v-component of the geostrophic wind.

    Notes
    -----
    If inputs have more than two dimensions, they are assumed to have either leading dimensions
    of (x, y) or trailing dimensions of (y, x), depending on the value of ``dim_order``.

    """
    if heights.dimensionality['[length]'] == 2.0:
        norm_factor = 1. / f
    else:
        norm_factor = mpconsts.g / f

    dhdy = first_derivative(heights, delta=dy, axis=-2)
    dhdx = first_derivative(heights, delta=dx, axis=-1)
    return -norm_factor * dhdy, norm_factor * dhdx

[docs]@exporter.export
@check_units(f='[frequency]', dx='[length]', dy='[length]', u='[speed]', v='[speed]')
def ageostrophic_wind(heights, f, dx, dy, u, v, dim_order='yx'):
    r"""Calculate the ageostrophic wind given from the heights or geopotential.

    Parameters
    ----------
    heights : (M, N) ndarray
        The height or geopotential field.
    f : array_like
        The coriolis parameter.  This can be a scalar to be applied
        everywhere or an array of values.
    dx : `pint.Quantity`
        The grid spacing(s) in the x-direction. If an array, there should be one item less than
        the size of `heights` along the applicable axis.
    dy : `pint.Quantity`
        The grid spacing(s) in the y-direction. If an array, there should be one item less than
        the size of `heights` along the applicable axis.
    u : (M, N) `pint.Quantity`
        The u wind field.
    v : (M, N) `pint.Quantity`
        The u wind field.

    Returns
    -------
    A 2-item tuple of arrays, `pint.Quantity`
        A tuple of the u-component and v-component of the ageostrophic wind.

    Notes
    -----
    If inputs have more than two dimensions, they are assumed to have either leading dimensions
    of (x, y) or trailing dimensions of (y, x), depending on the value of ``dim_order``.

    The order of the inputs will be changed in 1.0 to be (heights, u, v, f, dx, dy).
    To updated to the new format, use `from metpy.future import ageostrophic_wind`.

    """
    warnings.warn('Input variables will be reordered in 1.0 to be (heights, u, v, f, dx, dy).'
                  'To update to new input format before 1.0 is released, use'
                  '`from metpy.future import ageostrophic_wind`.', FutureWarning)
    from ..future import ageostrophic_wind as _ageostrophic_wind
    return _ageostrophic_wind(heights, u, v, f, dx, dy, dim_order=dim_order)

[docs]@exporter.export
@preprocess_xarray
@check_units('[length]', '[temperature]')
def montgomery_streamfunction(height, temperature):
    r"""Compute the Montgomery Streamfunction on isentropic surfaces.

    The Montgomery Streamfunction is the streamfunction of the geostrophic wind on an
    isentropic surface. This quantity is proportional to the geostrophic wind in isentropic
    coordinates, and its gradient can be interpreted similarly to the pressure gradient in
    isobaric coordinates.

    Parameters
    ----------
    height : `pint.Quantity`
        Array of geopotential height of isentropic surfaces
    temperature : `pint.Quantity`
        Array of temperature on isentropic surfaces

    Returns
    -------
    stream_func : `pint.Quantity`

    Notes
    -----
    The formula used is that from [Lackmann2011]_ p. 69.

    .. math:: \Psi = gZ + C_pT

    * :math:`\Psi` is Montgomery Streamfunction
    * :math:`g` is avg. gravitational acceleration on Earth
    * :math:`Z` is geopotential height of the isentropic surface
    * :math:`C_p` is specific heat at constant pressure for dry air
    * :math:`T` is temperature of the isentropic surface

    See Also
    --------
    get_isentropic_pressure

    """
    return (mpconsts.g * height) + (mpconsts.Cp_d * temperature)

[docs]@exporter.export
@preprocess_xarray
@check_units('[speed]', '[speed]', '[length]', '[length]', '[length]',
             '[speed]', '[speed]')
def storm_relative_helicity(u, v, heights, depth, bottom=0 * units.m,
                            storm_u=0 * units('m/s'), storm_v=0 * units('m/s')):
    # Partially adapted from similar SharpPy code
    r"""Calculate storm relative helicity.

    Calculates storm relatively helicity following [Markowski2010]_ 230-231.

    .. math:: \int\limits_0^d (\bar v - c) \cdot \bar\omega_{h} \,dz

    This is applied to the data from a hodograph with the following summation:

    .. math:: \sum_{n = 1}^{N-1} [(u_{n+1} - c_{x})(v_{n} - c_{y}) -
                                  (u_{n} - c_{x})(v_{n+1} - c_{y})]

    Parameters
    ----------
    u : array-like
        u component winds
    v : array-like
        v component winds
    heights : array-like
        atmospheric heights, will be converted to AGL
    depth : number
        depth of the layer
    bottom : number
        height of layer bottom AGL (default is surface)
    storm_u : number
        u component of storm motion (default is 0 m/s)
    storm_v : number
        v component of storm motion (default is 0 m/s)

    Returns
    -------
    `pint.Quantity`
        positive storm-relative helicity
    `pint.Quantity`
        negative storm-relative helicity
    `pint.Quantity`
        total storm-relative helicity

    """
    _, u, v = get_layer_heights(heights, depth, u, v, with_agl=True, bottom=bottom)

    storm_relative_u = u - storm_u
    storm_relative_v = v - storm_v

    int_layers = (storm_relative_u[1:] * storm_relative_v[:-1]
                  - storm_relative_u[:-1] * storm_relative_v[1:])

    # Need to manually check for masked value because sum() on masked array with non-default
    # mask will return a masked value rather than 0. See numpy/numpy#11736
    positive_srh = int_layers[int_layers.magnitude > 0.].sum()
    if np.ma.is_masked(positive_srh):
        positive_srh = 0.0 * units('meter**2 / second**2')
    negative_srh = int_layers[int_layers.magnitude < 0.].sum()
    if np.ma.is_masked(negative_srh):
        negative_srh = 0.0 * units('meter**2 / second**2')

    return (positive_srh.to('meter ** 2 / second ** 2'),
            negative_srh.to('meter ** 2 / second ** 2'),
            (positive_srh + negative_srh).to('meter ** 2 / second ** 2'))

[docs]@exporter.export
@preprocess_xarray
@check_units('[speed]', '[speed]', '[length]', '[length]')
def absolute_vorticity(u, v, dx, dy, lats, dim_order='yx'):
    """Calculate the absolute vorticity of the horizontal wind.

    Parameters
    ----------
    u : (M, N) `pint.Quantity`
        x component of the wind
    v : (M, N) `pint.Quantity`
        y component of the wind
    dx : `pint.Quantity`
        The grid spacing(s) in the x-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.
    dy : `pint.Quantity`
        The grid spacing(s) in the y-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.
    lats : (M, N) ndarray
        latitudes of the wind data in radians or with appropriate unit information attached

    Returns
    -------
    (M, N) `pint.Quantity`
        absolute vorticity

    Notes
    -----
    If inputs have more than two dimensions, they are assumed to have either leading dimensions
    of (x, y) or trailing dimensions of (y, x), depending on the value of ``dim_order``.

    """
    f = coriolis_parameter(lats)
    relative_vorticity = vorticity(u, v, dx, dy, dim_order=dim_order)
    return relative_vorticity + f

[docs]@exporter.export
@preprocess_xarray
@check_units('[temperature]', '[pressure]', '[speed]', '[speed]',
             '[length]', '[length]', '[dimensionless]')
def potential_vorticity_baroclinic(potential_temperature, pressure, u, v, dx, dy, lats):
    r"""Calculate the baroclinic potential vorticity.

    .. math:: PV = -g \left(\frac{\partial u}{\partial p}\frac{\partial \theta}{\partial y}
              - \frac{\partial v}{\partial p}\frac{\partial \theta}{\partial x}
              + \frac{\partial \theta}{\partial p}(\zeta + f) \right)

    This formula is based on equation 4.5.93 [Bluestein1993]_.

    Parameters
    ----------
    potential_temperature : (P, M, N) `pint.Quantity`
        potential temperature
    pressure : (P, M, N) `pint.Quantity`
        vertical pressures
    u : (P, M, N) `pint.Quantity`
        x component of the wind
    v : (P, M, N) `pint.Quantity`
        y component of the wind
    dx : `pint.Quantity`
        The grid spacing(s) in the x-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.
    dy : `pint.Quantity`
        The grid spacing(s) in the y-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.
    lats : (M, N) ndarray
        latitudes of the wind data in radians or with appropriate unit information attached

    Returns
    -------
    (P, M, N) `pint.Quantity`
        baroclinic potential vorticity

    Notes
    -----
    This function will only work with data that is in (P, Y, X) format. If your data
    is in a different order you will need to re-order your data in order to get correct
    results from this function.

    The same function can be used for isobaric and isentropic PV analysis. Provide winds
    for vorticity calculations on the desired isobaric or isentropic surface. At least three
    layers of pressure/potential temperature are required in order to calculate the vertical
    derivative (one above and below the desired surface). The first two terms will be zero if
    isentropic level data is used due to the gradient of theta in both the x and y-directions
    will be zero since you are on an isentropic surface.

    This function expects pressure/isentropic level to increase with increasing array element
    (e.g., from higher in the atmosphere to closer to the surface. If the pressure array is
    one-dimensional p[:, None, None] can be used to make it appear multi-dimensional.)

    """
    if ((np.shape(potential_temperature)[-3] < 3) or (np.shape(pressure)[-3] < 3)
       or (np.shape(potential_temperature)[-3] != (np.shape(pressure)[-3]))):
        raise ValueError('Length of potential temperature along the pressure axis '
                         '{} must be at least 3.'.format(-3))

    avor = absolute_vorticity(u, v, dx, dy, lats, dim_order='yx')
    dthtadp = first_derivative(potential_temperature, x=pressure, axis=-3)

    if ((np.shape(potential_temperature)[-2] == 1)
       and (np.shape(potential_temperature)[-1] == 1)):
        dthtady = 0 * units.K / units.m  # axis=-2 only has one dimension
        dthtadx = 0 * units.K / units.m  # axis=-1 only has one dimension
    else:
        dthtady = first_derivative(potential_temperature, delta=dy, axis=-2)
        dthtadx = first_derivative(potential_temperature, delta=dx, axis=-1)
    dudp = first_derivative(u, x=pressure, axis=-3)
    dvdp = first_derivative(v, x=pressure, axis=-3)

    return (-mpconsts.g * (dudp * dthtady - dvdp * dthtadx
                           + avor * dthtadp)).to(units.kelvin * units.meter**2
                                                 / (units.second * units.kilogram))

[docs]@exporter.export
@preprocess_xarray
@check_units('[length]', '[speed]', '[speed]', '[length]', '[length]', '[dimensionless]')
def potential_vorticity_barotropic(heights, u, v, dx, dy, lats, dim_order='yx'):
    r"""Calculate the barotropic (Rossby) potential vorticity.

    .. math:: PV = \frac{f + \zeta}{H}

    This formula is based on equation 7.27 [Hobbs2006]_.

    Parameters
    ----------
    heights : (M, N) `pint.Quantity`
        atmospheric heights
    u : (M, N) `pint.Quantity`
        x component of the wind
    v : (M, N) `pint.Quantity`
        y component of the wind
    dx : `pint.Quantity`
        The grid spacing(s) in the x-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.
    dy : `pint.Quantity`
        The grid spacing(s) in the y-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.
    lats : (M, N) ndarray
        latitudes of the wind data in radians or with appropriate unit information attached

    Returns
    -------
    (M, N) `pint.Quantity`
        barotropic potential vorticity

    Notes
    -----
    If inputs have more than two dimensions, they are assumed to have either leading dimensions
    of (x, y) or trailing dimensions of (y, x), depending on the value of ``dim_order``.

    """
    avor = absolute_vorticity(u, v, dx, dy, lats, dim_order=dim_order)
    return (avor / heights).to('meter**-1 * second**-1')

[docs]@exporter.export
@preprocess_xarray
@check_units('[speed]', '[speed]', '[speed]', '[speed]', '[length]', '[length]',
             '[dimensionless]')
def inertial_advective_wind(u, v, u_geostrophic, v_geostrophic, dx, dy, lats):
    r"""Calculate the inertial advective wind.

    .. math:: \frac{\hat k}{f} \times (\vec V \cdot \nabla)\hat V_g

    .. math:: \frac{\hat k}{f} \times \left[ \left( u \frac{\partial u_g}{\partial x} + v
              \frac{\partial u_g}{\partial y} \right) \hat i + \left( u \frac{\partial v_g}
              {\partial x} + v \frac{\partial v_g}{\partial y} \right) \hat j \right]

    .. math:: \left[ -\frac{1}{f}\left(u \frac{\partial v_g}{\partial x} + v
              \frac{\partial v_g}{\partial y} \right) \right] \hat i + \left[ \frac{1}{f}
              \left( u \frac{\partial u_g}{\partial x} + v \frac{\partial u_g}{\partial y}
              \right) \right] \hat j

    This formula is based on equation 27 of [Rochette2006]_.

    Parameters
    ----------
    u : (M, N) `pint.Quantity`
        x component of the advecting wind
    v : (M, N) `pint.Quantity`
        y component of the advecting wind
    u_geostrophic : (M, N) `pint.Quantity`
        x component of the geostrophic (advected) wind
    v_geostrophic : (M, N) `pint.Quantity`
        y component of the geostrophic (advected) wind
    dx : `pint.Quantity`
        The grid spacing(s) in the x-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.
    dy : `pint.Quantity`
        The grid spacing(s) in the y-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.
    lats : (M, N) ndarray
        latitudes of the wind data in radians or with appropriate unit information attached

    Returns
    -------
    (M, N) `pint.Quantity`
        x component of inertial advective wind
    (M, N) `pint.Quantity`
        y component of inertial advective wind

    Notes
    -----
    Many forms of the inertial advective wind assume the advecting and advected
    wind to both be the geostrophic wind. To do so, pass the x and y components
    of the geostrophic with for u and u_geostrophic/v and v_geostrophic.

    If inputs have more than two dimensions, they are assumed to have either leading dimensions
    of (x, y) or trailing dimensions of (y, x), depending on the value of ``dim_order``.

    """
    f = coriolis_parameter(lats)

    dugdy, dugdx = gradient(u_geostrophic, deltas=(dy, dx), axes=(-2, -1))
    dvgdy, dvgdx = gradient(v_geostrophic, deltas=(dy, dx), axes=(-2, -1))

    u_component = -(u * dvgdx + v * dvgdy) / f
    v_component = (u * dugdx + v * dugdy) / f

    return u_component, v_component

[docs]@exporter.export
@preprocess_xarray
@check_units('[speed]', '[speed]', '[temperature]', '[pressure]', '[length]', '[length]')
def q_vector(u, v, temperature, pressure, dx, dy, static_stability=1):
    r"""Calculate Q-vector at a given pressure level using the u, v winds and temperature.

    .. math:: \vec{Q} = (Q_1, Q_2)
                      =  - \frac{R}{\sigma p}\left(
                               \frac{\partial \vec{v}_g}{\partial x} \cdot \nabla_p T,
                               \frac{\partial \vec{v}_g}{\partial y} \cdot \nabla_p T
                           \right)

    This formula follows equation 5.7.55 from [Bluestein1992]_, and can be used with the
    the below form of the quasigeostrophic omega equation to assess vertical motion
    ([Bluestein1992]_ equation 5.7.54):

    .. math:: \left( \nabla_p^2 + \frac{f_0^2}{\sigma} \frac{\partial^2}{\partial p^2}
                  \right) \omega =
              - 2 \nabla_p \cdot \vec{Q} -
                  \frac{R}{\sigma p} \beta \frac{\partial T}{\partial x}.

    Parameters
    ----------
    u : (M, N) `pint.Quantity`
        x component of the wind (geostrophic in QG-theory)
    v : (M, N) `pint.Quantity`
        y component of the wind (geostrophic in QG-theory)
    temperature : (M, N) `pint.Quantity`
        Array of temperature at pressure level
    pressure : `pint.Quantity`
        Pressure at level
    dx : `pint.Quantity`
        The grid spacing(s) in the x-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.
    dy : `pint.Quantity`
        The grid spacing(s) in the y-direction. If an array, there should be one item less than
        the size of `u` along the applicable axis.
    static_stability : `pint.Quantity`, optional
        The static stability at the pressure level. Defaults to 1 if not given to calculate
        the Q-vector without factoring in static stability.

    Returns
    -------
    tuple of (M, N) `pint.Quantity`
        The components of the Q-vector in the u- and v-directions respectively

    See Also
    --------
    static_stability

    Notes
    -----
    If inputs have more than two dimensions, they are assumed to have either leading dimensions
    of (x, y) or trailing dimensions of (y, x), depending on the value of ``dim_order``.

    """
    dudy, dudx = gradient(u, deltas=(dy, dx), axes=(-2, -1))
    dvdy, dvdx = gradient(v, deltas=(dy, dx), axes=(-2, -1))
    dtempdy, dtempdx = gradient(temperature, deltas=(dy, dx), axes=(-2, -1))

    q1 = -mpconsts.Rd / (pressure * static_stability) * (dudx * dtempdx + dvdx * dtempdy)
    q2 = -mpconsts.Rd / (pressure * static_stability) * (dudy * dtempdx + dvdy * dtempdy)

    return q1.to_base_units(), q2.to_base_units()

        © Copyright 2019, MetPy Developers. Development supported by National Science Foundation grants AGS-1344155, OAC-1740315, and AGS-1901712..
      Last updated on Oct 18, 2019 at 17:16:45.

  Built with Sphinx using a theme provided by Read the Docs.

Do you enjoy using MetPy?
  Say Thanks!


text:
typedef struct node{
  int val;
  int degree;
  struct node *next;
} mono;

int charToDigit(char);
mono* parseInput(mono *, char *);
mono* createNode(int);
void  freeNodes(mono *);
mono* multiply(mono *, mono *, mono *);
mono* degreeExists(mono *, int);
void printList(mono *);

void main(){
  char input[100];
  printf("Problem 5: Multiplying polynomials.\n");
  mono *head1 = NULL, *head2 = NULL, *head = NULL; //input 1, 2, and answer respectively
  printf("Polynomial 1: ");
  scanf("%s", input);
  head1 = parseInput(head1, input);
  printf("Polynomial 2: ");
  scanf("%s", input);
  head2 = parseInput(head2, input);
  head = multiply(head1, head2, head);
  printf("Head 1: ");
  printList(head1);
  printf("\n");
  printf("Head 2: ");
  printList(head2);
  printf("\n");
  printf("made it to end.\n");
  printList(head);
  freeNodes(head);
  freeNodes(head1);
  freeNodes(head2);
}

mono* parseInput(mono *head, char *input){
  if (head == NULL){ //create list if empty
    head = createNode(0);
  }
  mono *traverser = head; //we need head later
  char *end = input + strlen(input) - 1; //we want to read from the end
  int degree = 0;
  int tenpow = 0;
  while (end >= input){
    if (charToDigit(*end) > -1){ //just add the digit to the monomial node
      int i, adder = charToDigit(*end);
      for (i = 0; i < tenpow; i++){ //since we started at the end we have to account for this
        adder *= 10;
      }
      traverser -> val += adder;
      tenpow++;
    } else {
      printf("ay lamo");
      if (*end == ','){ //prepare for the next monomial value
        degree++;
        tenpow = 0;
        traverser -> next = createNode(degree);
        traverser = traverser -> next;
      } else if (*end == '-'){
        traverser -> val *= -1;
      }
    }
    end--;
  }
  return head;
}

mono* createNode(int degree){
  mono *new = malloc(sizeof(mono));
  new -> degree = degree;
  new -> val = 0;
  new -> next = NULL;
  return new;
}

void freeNodes(mono *head){
  while (head -> next != NULL){ //free memory because i'm nice
    mono *tofree = head;
    head = head -> next;
    free(tofree);
  }
}
mono* multiply(mono *head1, mono *head2t, mono *head){
  mono *tail, *head2;
  while (head1 != NULL){
    head2 = head2t;
    while (head2 != NULL){
      int degree = (head1 -> degree) + (head2 -> degree);
        printf("%d", degree);
      if (head == NULL){
        head = createNode(degree);
        tail = head;
        head -> val += head1 -> val * (head2 -> val);
      } else if (degreeExists(head, degree) != NULL){
        degreeExists(head, degree) -> val += head1 -> val * (head2 -> val);
      } else { //crashes here
        tail -> next = createNode(degree);
        tail = tail -> next;
        tail -> val += head1 -> val * (head2 -> val);
      }
      head2 = head2 -> next;

    }
    head1 = head1 -> next;
  }
  return head;
}

//returns pointer if found, otherwise NULL
mono* degreeExists(mono *head, int degree){
  while (head != NULL){
    if (head -> degree == degree){
      return head;
    } else {
      head = head -> next;
    }
  }
  return NULL;
}

int charToDigit(char intwannabe){
  if (intwannabe >= 48 && intwannabe <= 57){
    return intwannabe - 48;
  }
  return -1;
}

void printList(mono *head){
  int i = 0;
  while (head != NULL){
    printf("Elem %d: ", i);
    printf("(%d, %d) ", head -> val, head -> degree);
    if (head -> next != NULL) printf(", ");
    head = head -> next;
    i++;
  }
}


text:
@interface NLPathMaker ()
@property (nonatomic, strong) NSString *basePath;
@property (nonatomic, strong) NSMutableArray *pathComponents;
@property (nonatomic, strong) NSMutableDictionary *queryParams;

+ (instancetype)pathMakerWithBasePath:(NSString *)basePath;
@end

NLPathMaker * createPath(NSString *basePath) {
    return [NLPathMaker pathMakerWithBasePath:basePath];
}

@implementation NLPathMaker

+ (instancetype)pathMakerWithBasePath:(NSString *)basePath {
    return [[[self class] alloc] initWithBasePath:basePath];
}

- (instancetype)initWithBasePath:(NSString *)basePath {
    self = [super init];
    if (self) {
        self.basePath = basePath;
        self.pathComponents = [NSMutableArray new];
        self.queryParams = [NSMutableDictionary new];
    }
    return self;
}

- (void)setBasePath:(NSString *)basePath {
    NSString *finalBasePath = [basePath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (finalBasePath.length > 0) {
        NSString *lastChar = [finalBasePath substringFromIndex:finalBasePath.length - 1];
        if ([lastChar isEqualToString:@"/"]) {
            finalBasePath = [finalBasePath substringToIndex:finalBasePath.length - 1];
        }
    }
    _basePath = finalBasePath;
}

- (AppendPathComponentMethod)appendPathComponent {
    return ^(NSString *pathComponent) {
        return [self appendPathComponent:pathComponent];
    };
}

- (AddQueryParamMethod)addQueryParam {
    return ^(NSString *key, id value) {
        return [self addQueryParam:key value:value];
    };
}

- (AddQueryParamMethodConditional)addQueryParamConditional {
    return ^(NSString *key, id value, BOOL condition) {
        if (condition) {
            return [self addQueryParam:key value:value];
        }
        else {
            return self;
        }
    };
}

- (instancetype)appendPathComponent:(NSString *)pathComponent {
    if (pathComponent != nil) {
        [self.pathComponents addObject:pathComponent];
    }
    return self;
}

- (instancetype)addQueryParam:(NSString *)param value:(id)value {
    if (value != nil && param != nil) {
        [self.queryParams setValue:value forKey:param];
    }
    return self;
}

- (NSString *)paramStringValue:(id)value {
    if ([value isKindOfClass:[NSString class]]) {
        return value;
    }
    else if ([value respondsToSelector:@selector(stringValue)]) {
        return [value stringValue];
    }
    else {
        return nil;
    }
}

- (NSString *)stringParamForKey:(NSString *)key value:(id)value {
    NSString *paramStringValue = [self paramStringValue:value];
    if (paramStringValue) {
        return [NSString stringWithFormat:@"%@=%@", key, paramStringValue];
    }
    else if ([value isKindOfClass:[NSArray class]]) {
        NSMutableString *arrayStringParam = [NSMutableString new];
        [value enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            [arrayStringParam appendString:[self stringParamForKey:key value:obj]];
            if (idx < [value count] -1) {
                [arrayStringParam appendString:@"&"];
            }
        }];
        return arrayStringParam;
    }
    else {
        NSAssert(YES, @"Error creating the path");
        return nil;
    }
}

- (NSString *)path {
    NSMutableString *path = [NSMutableString stringWithString:self.basePath];
    if ([self.pathComponents count] > 0) {
        NSString *pathComponents = [self.pathComponents componentsJoinedByString:@"/"];
        [path appendString:@"/"];
        [path appendString:pathComponents];
    }

    if ([self.queryParams count] > 0) {
        [path appendString:@"?"];
        NSArray *allKeys = [[self.queryParams allKeys] sortedArrayUsingSelector:@selector(compare:)];
        NSString *lastKey = [allKeys lastObject];
        for (NSString *key in allKeys) {
            [path appendString:[self stringParamForKey:key value:self.queryParams[key]]];
            if (key != lastKey) {
                [path appendString:@"&"];
            }
        }
    }

    return path;
}

@end


text:
package com.composum.sling.core.util;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ResourceResolver;

import javax.servlet.http.HttpServletRequest;

public interface LinkMapper {

    String LINK_MAPPER_REQUEST_ATTRIBUTE = LinkMapper.class.getName() + "_linkMapper";

    /**
     * If uri is an URL, it is returned unmodified; if it's a path, it's mapped to an URL without scheme, but validly URL-encoded.
     * we have to say that it returns it URL-encoded since {@link ResourceResolver#map(HttpServletRequest, String)} encodes
     * characters not valid in an URL.
     */
    String mapUri(SlingHttpServletRequest request, String uri);

    ContextMapper CONTEXT = new ContextMapper();
    ResolverMapper RESOLVER = new ResolverMapper();

    class ContextMapper implements LinkMapper {

        @Override
        public String mapUri(SlingHttpServletRequest request, String uri) {
            return LinkUtil.isExternalUrl(uri) ? uri : request.getContextPath() + LinkUtil.encodePath(uri);
        }
    }

    class ResolverMapper implements LinkMapper {

        @Override
        public String mapUri(SlingHttpServletRequest request, String uri) {
            ResourceResolver resolver = request.getResourceResolver();
            // caution: resolver.map doesn't encode many other special characters like #,? like LinkUtil.encodePath does.
            // it should work, though, except for # and ? who are legal in JCR resource names, but a very bad idea, anyway.
            // As of 1.6.12 it treats ? and # as parameter and comment separator, and doesn't percent-encode the rest.
            return LinkUtil.isExternalUrl(uri) ? uri : resolver.map(request, uri);
        }
    }
}