text:
let serviceName = 'user';
module.exports.get = function *() {
  let service = yield this.getModuleService(serviceName);

  this.bag.setMultipleRessourceResponse();

  let options = this.utils.getFromQueryM(['page', 'limit', 'orderby', 'order'], null, true);
  let page = options.page && options.page.match(/^\d+$/) && options.page > 0 ? options.page : 1;
  let limit = options.limite && options.limit.match(/^\d+$/) && options.limit > 0 && options.limit < 1000 ? options.limit : 25;
  let orderby = options.orderby && ['username', 'firstname', 'lastname', 'email'].indexOf(options.orderby) > -1 ? options.orderby : 'username';
  let order = options.order && options.order === 'desc' ? 'desc' : 'asc';

  let items = yield service.getByPage({}, page, limit, orderby, order);
  this.bag.setDataFromVos(items);
  return this.renderBag();
};

module.exports.getOneById = function *() {
  let service = yield this.getModuleService(serviceName);

  this.bag.setSingleRessourceResponse();

  let id = this.params.id || '';

  let item = yield service.getOneById(id);
  this.assert(item, 404, 'item not found');
  this.bag.setDataFromVo(item);
  return this.renderBag();
};

module.exports.getOneByUsername = function *() {
  let service = yield this.getModuleService(serviceName);

  this.bag.setSingleRessourceResponse();

  let username = this.params.username || '';

  let item = yield service.getOneByUsername(username);
  this.assert(item, 404, 'item not found');
  this.bag.setDataFromVo(item);
  return this.renderBag();
};

module.exports.createOne = function *(next) {
  let service = yield this.getModuleService(serviceName);

  this.bag.setSingleRessourceResponse();

  let itemData = this.utils.getFromPostM(['username', 'firstname', 'lastname', 'email']);
  try {
    let newVo = yield service.createNewOne(itemData, 'api');
    this.bag.setDataFromVo(newVo);
    this.renderBag();
  } catch(errors) {
    if (errors instanceof Error) {
      throw errors;
    }
    this.bag.addError(errors);
    return this.renderBag();
  }
};

module.exports.updateOneById = function *() {
  let service = yield this.getModuleService(serviceName);

  this.bag.setSingleRessourceResponse();

  let id = this.params.id || '';
  let item = yield service.getOneById(id);

  this.assert(item, 404, 'item not found');

  let itemData = this.utils.getFromPostM(['firstname', 'lastname', 'email'], null, true);
  try {
    let updatedVo = yield service.updateOneFromData(itemData, item.id);
    this.bag.setDataFromVo(updatedVo);
    return this.renderBag();
  } catch(errors) {
    if (errors instanceof Error) {
      throw errors;
    }
    this.bag.addError(errors);
    return this.renderBag();
  }
};

module.exports.deleteOneById = function *() {
  let service = yield this.getModuleService(serviceName);

  this.bag.setRawResponse();

  let id = this.params.id || '';
  let item = yield service.getOneById(id);

  this.assert(item, 404, 'item not found');

  let successDeleted = yield service.deleteOneById(item.id);
  this.bag.setData({'deleted': successDeleted === true ? 1 : 0});
  return this.renderBag();
};


text:
#include <map>
#include <string>

#include <ace/Recursive_Thread_Mutex.h>
#include <ace/Guard_T.h>

#include <geode/CacheFactory.hpp>
#include <geode/RegionFactory.hpp>
#include <geode/Cache.hpp>
#include <geode/SystemProperties.hpp>
#include <geode/PoolManager.hpp>

#include "CppCacheLibrary.hpp"
#include "CacheImpl.hpp"
#include "CacheConfig.hpp"
#include "CacheRegionHelper.hpp"

namespace apache {
namespace geode {
namespace client {

RegionFactory::RegionFactory(RegionShortcut preDefinedRegion,
                             CacheImpl* cacheImpl)
    : m_preDefinedRegion(preDefinedRegion),
      m_regionAttributesFactory(std::make_shared<RegionAttributesFactory>()),
      m_cacheImpl(cacheImpl) {
  setRegionShortcut();
}
std::shared_ptr<Region> RegionFactory::create(std::string name) {
  std::shared_ptr<Region> retRegionPtr = nullptr;
  auto regionAttributes = m_regionAttributesFactory->create();
  if (m_preDefinedRegion != RegionShortcut::LOCAL &&
      regionAttributes.getPoolName().empty()) {
    auto pool = m_cacheImpl->getPoolManager().getDefaultPool();
    if (!pool) {
      throw IllegalStateException("No pool for non-local region.");
    }
    m_regionAttributesFactory->setPoolName(pool->getName());
    regionAttributes = m_regionAttributesFactory->create();
  }
  m_cacheImpl->createRegion(name, regionAttributes, retRegionPtr);

  return retRegionPtr;
}

void RegionFactory::setRegionShortcut() {
  switch (m_preDefinedRegion) {
    case RegionShortcut::PROXY: {
      m_regionAttributesFactory->setCachingEnabled(false);
    } break;
    case RegionShortcut::CACHING_PROXY: {
      m_regionAttributesFactory->setCachingEnabled(true);
    } break;
    case RegionShortcut::CACHING_PROXY_ENTRY_LRU: {
      m_regionAttributesFactory->setCachingEnabled(true);
      m_regionAttributesFactory->setLruEntriesLimit(DEFAULT_LRU_MAXIMUM_ENTRIES);
    } break;
    case RegionShortcut::LOCAL: {
    } break;
    case RegionShortcut::LOCAL_ENTRY_LRU: {
      m_regionAttributesFactory->setLruEntriesLimit(DEFAULT_LRU_MAXIMUM_ENTRIES);
    } break;
  }
}

RegionFactory& RegionFactory::setCacheLoader(
    const std::shared_ptr<CacheLoader>& cacheLoader) {
  m_regionAttributesFactory->setCacheLoader(cacheLoader);
  return *this;
}

RegionFactory& RegionFactory::setCacheWriter(
    const std::shared_ptr<CacheWriter>& cacheWriter) {
  m_regionAttributesFactory->setCacheWriter(cacheWriter);
  return *this;
}
RegionFactory& RegionFactory::setCacheListener(
    const std::shared_ptr<CacheListener>& aListener) {
  m_regionAttributesFactory->setCacheListener(aListener);
  return *this;
}
RegionFactory& RegionFactory::setPartitionResolver(
    const std::shared_ptr<PartitionResolver>& aResolver) {
  m_regionAttributesFactory->setPartitionResolver(aResolver);
  return *this;
}

RegionFactory& RegionFactory::setCacheLoader(const std::string& lib,
                                             const std::string& func) {
  m_regionAttributesFactory->setCacheLoader(lib, func);
  return *this;
}

RegionFactory& RegionFactory::setCacheWriter(const std::string& lib,
                                             const std::string& func) {
  m_regionAttributesFactory->setCacheWriter(lib, func);
  return *this;
}

RegionFactory& RegionFactory::setCacheListener(const std::string& lib,
                                               const std::string& func) {
  m_regionAttributesFactory->setCacheListener(lib, func);
  return *this;
}

RegionFactory& RegionFactory::setPartitionResolver(const std::string& lib,
                                                   const std::string& func) {
  m_regionAttributesFactory->setPartitionResolver(lib, func);
  return *this;
}

RegionFactory& RegionFactory::setEntryIdleTimeout(
    ExpirationAction action, std::chrono::seconds idleTimeout) {
  m_regionAttributesFactory->setEntryIdleTimeout(action, idleTimeout);
  return *this;
}

RegionFactory& RegionFactory::setEntryTimeToLive(
    ExpirationAction action, std::chrono::seconds timeToLive) {
  m_regionAttributesFactory->setEntryTimeToLive(action, timeToLive);
  return *this;
}

RegionFactory& RegionFactory::setRegionIdleTimeout(
    ExpirationAction action, std::chrono::seconds idleTimeout) {
  m_regionAttributesFactory->setRegionIdleTimeout(action, idleTimeout);
  return *this;
}

RegionFactory& RegionFactory::setRegionTimeToLive(
    ExpirationAction action, std::chrono::seconds timeToLive) {
  m_regionAttributesFactory->setRegionTimeToLive(action, timeToLive);
  return *this;
}

RegionFactory& RegionFactory::setInitialCapacity(int initialCapacity) {
  char excpStr[256] = {0};
  if (initialCapacity < 0) {
    ACE_OS::snprintf(excpStr, 256, "initialCapacity must be >= 0 ");
    throw IllegalArgumentException(excpStr);
  }
  m_regionAttributesFactory->setInitialCapacity(initialCapacity);
  return *this;
}

RegionFactory& RegionFactory::setLoadFactor(float loadFactor) {
  m_regionAttributesFactory->setLoadFactor(loadFactor);
  return *this;
}

RegionFactory& RegionFactory::setConcurrencyLevel(uint8_t concurrencyLevel) {
  m_regionAttributesFactory->setConcurrencyLevel(concurrencyLevel);
  return *this;
}
RegionFactory& RegionFactory::setConcurrencyChecksEnabled(bool enable) {
  m_regionAttributesFactory->setConcurrencyChecksEnabled(enable);
  return *this;
}
RegionFactory& RegionFactory::setLruEntriesLimit(const uint32_t entriesLimit) {
  m_regionAttributesFactory->setLruEntriesLimit(entriesLimit);
  return *this;
}

RegionFactory& RegionFactory::setDiskPolicy(
    const DiskPolicyType diskPolicy) {
  m_regionAttributesFactory->setDiskPolicy(diskPolicy);
  return *this;
}

RegionFactory& RegionFactory::setCachingEnabled(bool cachingEnabled) {
  m_regionAttributesFactory->setCachingEnabled(cachingEnabled);
  return *this;
}

RegionFactory& RegionFactory::setPersistenceManager(
    const std::shared_ptr<PersistenceManager>& persistenceManager,
    const std::shared_ptr<Properties>& config) {
  m_regionAttributesFactory->setPersistenceManager(persistenceManager, config);
  return *this;
}

RegionFactory& RegionFactory::setPersistenceManager(
    const std::string& lib, const std::string& func,
    const std::shared_ptr<Properties>& config) {
  m_regionAttributesFactory->setPersistenceManager(lib, func, config);
  return *this;
}

RegionFactory& RegionFactory::setPoolName(const std::string& name) {
  m_regionAttributesFactory->setPoolName(name);
  return *this;
}

RegionFactory& RegionFactory::setCloningEnabled(bool isClonable) {
  m_regionAttributesFactory->setCloningEnabled(isClonable);
  return *this;
}
}  // namespace client
}  // namespace geode
}  // namespace apache

redpajama_set_name: RedPajamaGithub


text:
package com.facebook.buck.testutil.endtoend;

import com.facebook.buck.testutil.AbstractWorkspace;
import com.facebook.buck.testutil.ProcessResult;
import com.facebook.buck.testutil.TemporaryPaths;
import com.facebook.buck.testutil.TestConsole;
import com.facebook.buck.util.DefaultProcessExecutor;
import com.facebook.buck.util.ExitCode;
import com.facebook.buck.util.ProcessExecutor;
import com.facebook.buck.util.ProcessExecutorParams;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

/**
 * {@link EndToEndWorkspace} provides an interface to create temporary workspaces for the use in
 * EndToEndTests. It implements junit {@link TestRule}, and so can be used by:
 *
 *

 *
In test class, preceding the declaration of the Workspace with @Rule will automatically
 *       create and destroy the workspace before each test.
 *
Calling {@code .setup} and {@code .teardown} before and after testing manually
 *

 */
public class EndToEndWorkspace extends AbstractWorkspace implements TestRule {
  private TemporaryPaths tempPath = new TemporaryPaths();
  private final ProcessExecutor processExecutor = new DefaultProcessExecutor(new TestConsole());
  private Boolean ranWithBuckd = false;

  private static final String TESTDATA_DIRECTORY = "testdata";

  // TODO: Move to work on windows, and to use Resources
  private static final String BUCK_EXE =
      FileSystems.getDefault()
          .getPath("buck-out", "gen", "programs", "buck.pex")
          .toAbsolutePath()
          .toString();

  /**
   * Constructor for EndToEndWorkspace. Note that setup and teardown should be called in between
   * tests (easy way to do this is to use @Rule)
   */
  public EndToEndWorkspace() {
    super();
  }

  /**
   * Used for @Rule functionality so that EndToEndWorkspace can be automatically set up and torn
   * down before every test.
   *
   * @param base statement that contains the test
   * @param description a description of the test
   * @return transformed statement that sets up and tears down before and after the test
   */
  public Statement apply(Statement base, Description description) {
    return statement(base);
  }

  /** Sets up TemporaryPaths for the test, and sets the Workspace's root to that path */
  public void setup() throws Throwable {
    this.tempPath.before();
    this.destPath = tempPath.getRoot();
  }

  /**
   * Tears down TemporaryPaths, and resets Workspace's roots to avoid polluting execution
   * environment accidentally.
   */
  public void teardown() {
    if (this.ranWithBuckd) {
      try {
        this.runBuckCommand("kill");
      } catch (Exception e) {
        // Nothing particularly better to do than log the error and swallow it
        System.err.println(e.getMessage());
      }
    }
    this.tempPath.after();
    this.destPath = null;
  }

  private Statement statement(final Statement base) {
    return new Statement() {
      @Override
      public void evaluate() throws Throwable {
        setup();
        try {
          base.evaluate();
        } finally {
          teardown();
        }
      }
    };
  }

  private ImmutableMap<String, String> overrideSystemEnvironment(
      Boolean buckdEnabled, ImmutableMap<String, String> environmentOverrides) {
    ImmutableMap.Builder<String, String> environmentBuilder = ImmutableMap.builder();
    for (Map.Entry<String, String> entry : System.getenv().entrySet()) {
      if (entry.getKey() == "NO_BUCKD" && buckdEnabled) continue;
      environmentBuilder.put(entry.getKey(), entry.getValue());
    }
    if (buckdEnabled) {
      environmentBuilder.put("NO_BUCKD", "1");
    }
    environmentBuilder.putAll(environmentOverrides);
    return environmentBuilder.build();
  }

  /**
   * Runs Buck with the specified list of command-line arguments, and the current system environment
   * variables.
   *
   * @param args to pass to {@code buck}, so that could be {@code ["build", "//path/to:target"]},
   *     {@code ["project"]}, etc.
   * @return the result of running Buck, which includes the exit code, stdout, and stderr.
   */
  @Override
  public ProcessResult runBuckCommand(String... args) throws Exception {
    ImmutableMap<String, String> environment = ImmutableMap.of();
    return runBuckCommand(environment, args);
  }

  /**
   * Runs Buck with the specified list of command-line arguments, and the current system environment
   * variables.
   *
   * @param buckdEnabled determines whether the command is run with buckdEnabled or not
   * @param args to pass to {@code buck}, so that could be {@code ["build", "//path/to:target"]},
   *     {@code ["project"]}, etc.
   * @return the result of running Buck, which includes the exit code, stdout, and stderr.
   */
  public ProcessResult runBuckCommand(Boolean buckdEnabled, String... args) throws Exception {
    ImmutableMap<String, String> environment = ImmutableMap.of();
    String[] templates = new String[] {};
    return runBuckCommand(buckdEnabled, environment, templates, args);
  }

  /**
   * Runs Buck with the specified list of command-line arguments with the given map of environment
   * variables as overrides of the current system environment.
   *
   * @param environmentOverrides set of environment variables to override
   * @param args to pass to {@code buck}, so that could be {@code ["build", "//path/to:target"]},
   *     {@code ["project"]}, etc.
   * @return the result of running Buck, which includes the exit code, stdout, and stderr.
   */
  public ProcessResult runBuckCommand(
      ImmutableMap<String, String> environmentOverrides, String... args) throws Exception {
    String[] templates = new String[] {};
    return runBuckCommand(false, environmentOverrides, templates, args);
  }

  /**
   * Runs Buck with the specified list of command-line arguments with the given map of environment
   * variables as overrides of the current system environment.
   *
   * @param buckdEnabled determines whether the command is run with buckdEnabled or not
   * @param environmentOverrides set of environment variables to override
   * @param templates is an array of premade templates to add to the workspace before running the
   *     command.
   * @param args to pass to {@code buck}, so that could be {@code ["build", "//path/to:target"]},
   *     {@code ["project"]}, etc.
   * @return the result of running Buck, which includes the exit code, stdout, and stderr.
   */
  public ProcessResult runBuckCommand(
      Boolean buckdEnabled,
      ImmutableMap<String, String> environmentOverrides,
      String[] templates,
      String... args)
      throws Exception {
    for (String template : templates) {
      this.addPremadeTemplate(template);
    }
    List<String> command =
        ImmutableList.<String>builder().add(BUCK_EXE).addAll(ImmutableList.copyOf(args)).build();
    ImmutableMap<String, String> environment =
        overrideSystemEnvironment(buckdEnabled, environmentOverrides);
    ProcessExecutorParams params =
        ProcessExecutorParams.builder()
            .setCommand(command)
            .setEnvironment(environment)
            .setDirectory(destPath.toAbsolutePath())
            .build();
    ProcessExecutor.Result result = processExecutor.launchAndExecute(params);
    ranWithBuckd = ranWithBuckd || buckdEnabled;
    return new ProcessResult(
        ExitCode.map(result.getExitCode()),
        result.getStdout().orElse(""),
        result.getStderr().orElse(""));
  }

  /**
   * Copies the template directory of a premade template, contained in endtoend/testdata, stripping
   * the suffix from files with suffix .fixture, and not copying files with suffix .expected
   */
  public void addPremadeTemplate(String templateName) throws Exception {
    URL testDataResource = getClass().getResource(TESTDATA_DIRECTORY);
    // If we're running this test in IJ, then this path doesn't exist. Fall back to one that does
    if (testDataResource != null) {
      this.addTemplateToWorkspace(testDataResource, templateName);
      return;
    }
    Path templatePath =
        FileSystems.getDefault()
            .getPath(
                "test",
                "com",
                "facebook",
                "buck",
                "testutil",
                "endtoend",
                TESTDATA_DIRECTORY,
                templateName);
    this.addTemplateToWorkspace(templatePath);
  }
}


text:
package tests.api.java.lang;

import dalvik.annotation.BrokenTest;

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

public class ProcessTest extends junit.framework.TestCase {

    /**
     * java.lang.Process#getInputStream()
     */
    public void test_getInputStream() {
        try {
            // Test for:
            //Object[] execArgs = Support_Exec.execJava2(
            //        new String[] { "tests.support.Support_AvailTest" }, null,
            //        true);
            //Process proc = (Process) execArgs[0];

            String[] commands = { "sleep", "1"};
            Process proc = Runtime.getRuntime().exec(commands, null, null);

            OutputStream os = proc.getOutputStream();

            // first number indicates total stream length
            // second number indicates length of data after second space
            // this will allow us to verify length at start, middle, and end
            os.write("10 5 abcde".getBytes());
            os.close();

            InputStream is = proc.getInputStream();
            StringBuffer msg = new StringBuffer("");
            while (true) {
                int c = is.read();
                if (c == -1)
                    break;
                msg.append((char) c);
            }
            is.close();
            proc.waitFor();
            //Support_Exec.checkStderr(execArgs);
            proc.destroy();
            assertEquals("true", msg.toString(), msg.toString());
        } catch (IOException e) {
            fail("IOException executing avail test: " + e);
        } catch (InterruptedException e) {
            fail("InterruptedException executing avail test: " + e);
        }
    }

    /**
     * java.lang.Process#getOutputStream()
     */
    public void test_getOutputStream() {
        try {
            String[] commands = { "sleep", "1"};
            Process proc = Runtime.getRuntime().exec(commands, null, null);
            OutputStream os = proc.getOutputStream();
            // send data, and check if it is echoed back correctly
            String str1 = "Some data for testing communication between processes\n";
            String str2 = "More data that serves the same purpose.\n";
            String str3 = "Here is some more data.\n";
            os.write(str1.getBytes());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            os.close();
            InputStream is = proc.getInputStream();
            StringBuffer msg = new StringBuffer("");
            while (true) {
                int c = is.read();
                if (c == -1)
                    break;
                msg.append((char) c);
            }
            is.close();
            proc.waitFor();
            //Support_Exec.checkStderr(execArgs);
            proc.destroy();
            String org = str1;
            String recvd = msg.toString();
            // Doesn't pass on RI
            // assertTrue("Data returned did not match data sent. Received: '"
            //        + recvd + "' sent: '" + org + "'", recvd.equals(org));
        } catch (IOException e) {
            fail("IOException executing avail test: " + e);
        } catch (InterruptedException e) {
            fail("InterruptedException executing avail test: " + e);
        }
    }

    public void test_exitValue() {
        try {
            String[] commands = { "ls" };
            Process process = Runtime.getRuntime().exec(commands, null, null);
            try {
                Thread.sleep(5000);
            } catch(Exception e) {

            }
            assertTrue(process.exitValue() == 0);

            String[] commandsSleep = { "sleep", "3" };
            process = Runtime.getRuntime().exec(commandsSleep, null, null);
            process.destroy();
            try {
                Thread.sleep(5000);
            } catch (Exception e) {}
            assertTrue(process.exitValue() != 0);

            process = Runtime.getRuntime().exec(commandsSleep, null, null);
            try {
                process.exitValue();
                fail("IllegalThreadStateException was not thrown.");
            } catch(IllegalThreadStateException itse) {
               //expected
            }
        } catch (IOException e) {
            fail("IOException was thrown.");
        }
    }

    public void test_Constructor() {
        ProcessClass pc = new ProcessClass();
        assertTrue(pc.exitValue() == 0);
    }

    @BrokenTest("Sporadic timeouts in CTS, but not in CoreTestRunner")
    public void test_destroy() {
        String[] commands = { "ls"};
        try {
            Process process = Runtime.getRuntime().exec(commands, null, null);
            process.destroy();
        } catch (IOException e) {
            fail("IOException was thrown.");
        }
    }

    protected void setUp() {
    }

    protected void tearDown() {
    }

    protected void doneSuite() {
    }

    class ProcessClass extends Process {

        ProcessClass() {
            super();
        }

        @Override
        public void destroy() {

        }

        @Override
        public int exitValue() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public InputStream getErrorStream() {
            return null;
        }

        @Override
        public InputStream getInputStream() {
            return null;
        }

        @Override
        public OutputStream getOutputStream() {
            return null;
        }

        @Override
        public int waitFor() throws InterruptedException {
            // TODO Auto-generated method stub
            return 0;
        }

    }
}


text:
package com.martin.ads.vrlib.utils;

import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.widget.ImageView;

import com.martin.ads.vrlib.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;

/**
 * Created by xilin on 2015/12/14.
 */
public class UIUtils
{
    public static String getShowTime(long milliseconds) {
        /*
            // 获取日历函数
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(milliseconds);
            SimpleDateFormat dateFormat = null;
            // 判断是否大于60分钟,如果大于就显示小时。设置日期格式
            if (milliseconds / 60000 > 60) {
                dateFormat = new SimpleDateFormat("hh:mm:ss");
            } else {
                dateFormat = new SimpleDateFormat("00:mm:ss");
            }
            return dateFormat.format(calendar.getTime());
        */
        //abhiank209 (pr #10)
        return String.format("%02d:%02d:%02d",
                    TimeUnit.MILLISECONDS.toHours(milliseconds),
                    TimeUnit.MILLISECONDS.toMinutes(milliseconds) -
                                    TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(milliseconds)),
                    TimeUnit.MILLISECONDS.toSeconds(milliseconds) -
                                    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliseconds)));
    }

    public static void startImageAnim(ImageView Img, int anim)
    {
        Img.setVisibility(View.VISIBLE);
        try
        {
            Img.setImageResource(anim);
            AnimationDrawable animationDrawable = (AnimationDrawable) Img.getDrawable();
            animationDrawable.start();
        }
        catch (ClassCastException e)
        {
            e.printStackTrace();
        }
    }

    public static void stopImageAnim(ImageView Img)
    {
        try
        {
            AnimationDrawable animationDrawable = (AnimationDrawable) Img.getDrawable();
            animationDrawable.stop();
        }
        catch (ClassCastException e)
        {
            e.printStackTrace();
        }
        Img.setVisibility(View.GONE);
    }

    //缓冲动画控制
    public static void setBufferVisibility(ImageView imgBuffer, boolean Visible)
    {
        if (Visible)
        {
            imgBuffer.setVisibility(View.VISIBLE);
            startImageAnim(imgBuffer, R.drawable.loading);
        }
        else
        {
            stopImageAnim(imgBuffer);
            imgBuffer.setVisibility(View.GONE);
        }
    }

}


text:
package skylin;

class SkylinException extends RuntimeException
{
      public SkylinException() {}

      public SkylinException (AM am,String message)
      {
    	  super (message);
    	  am.fatal(message);
      }

      public SkylinException (Throwable cause)
      {
    	  super (cause);
      }

      public SkylinException (String message, Throwable cause)
      {
    	  super (message, cause);
      }
 }


text:
<?php

namespace Oro\Bundle\WorkflowBundle\Model\Action;

use Oro\Bundle\ActionBundle\Action\CreateDate as BaseCreateDate;

/**
 * @deprecated since 1.10. Use {@see Oro\Bundle\ActionBundle\Action\CreateDate} instead
 */
class CreateDate extends BaseCreateDate
{
}


text:
<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <title>Lung cancer network Example</title><meta name="generator" content="MATLAB 7.12"><link rel="schema.DC" href="http://purl.org/dc/elements/1.1/"><meta name="DC.date" content="2012-03-27"><meta name="DC.source" content="lungcancerGMdemo.m">

Lung cancer network Example

Make DAG
   S
  / \
 v  v
CB  LC
  \/   \
  v     v
 SOB    X

Contents

Display joint

Conditional marginals

Unconditional marginals

Joint mode

% This file is from pmtk3.googlecode.com

nvars = 5;
toporder = false;
[dgm] = mkLungCancerDgm(toporder);
S = dgm.nodeNum.S; CB = dgm.nodeNum.CB; LC = dgm.nodeNum.LC;
SOB = dgm.nodeNum.SOB; X = dgm.nodeNum.X;

names = dgm.nodeNames;
%drawNetwork(G, '-nodeLabels', names);

joint = dgmInferQuery(dgm, [S CB LC SOB X]);

Display joint

lab=cellfun(@(x) {sprintf('%d ',x)}, num2cell(ind2subv([2 2 2 2 2],1:32)-1,2));
figure;
bar(joint.T(:))
set(gca,'xtick',1:32);
xticklabelRot(lab, 90, 8, 0.025)
title('joint distribution of lung cancer network')
ylabel('probability truncated at 0.2')
set(gca,'ylim',[0 0.2]) % zoom in
printPmtkFigure('lungcancerJointBar')

Conditional marginals

clamped = sparsevec(SOB, 2, nvars);
pLCgivenSOB = tabularFactorCondition(joint, LC, clamped);
pLCgivenSOBdgm = dgmInferQuery(dgm, LC, 'clamped', clamped);
assert(tfequal(pLCgivenSOB, pLCgivenSOBdgm));
fprintf('p(LC=1|SOB=1)=%5.3f\n', pLCgivenSOB.T(2))

clamped = sparsevec([SOB,X], [2,2], nvars);
pLCgivenSOBandX = tabularFactorCondition(joint, LC, clamped);
pLCgivenSOBandXdgm = dgmInferQuery(dgm, LC, 'clamped', clamped);
assert(tfequal(pLCgivenSOBandX, pLCgivenSOBandXdgm));
fprintf('p(LC=1|SOB=1,XR=1)=%5.3f\n', pLCgivenSOBandX.T(2))

clamped = sparsevec(SOB, 2, nvars);
pCBgivenSOB = tabularFactorCondition(joint, CB, clamped);
pCBgivenSOBdgm = dgmInferQuery(dgm, CB, 'clamped', clamped);
assert(tfequal(pCBgivenSOB, pCBgivenSOBdgm));
fprintf('p(CB=1|SOB=1)=%5.3f\n', pCBgivenSOB.T(2))
assert(approxeq(pCBgivenSOB.T, [0.5038; 0.4962]));

clamped = sparsevec([SOB, X], [2, 2], nvars);
pCBgivenSOBandX = tabularFactorCondition(joint, CB, clamped);
pCBgivenSOBandXdgm = dgmInferQuery(dgm, CB, 'clamped', clamped);
assert(tfequal(pCBgivenSOBandX, pCBgivenSOBandXdgm));
fprintf('p(CB=1|SOB=1,XR=1)=%5.3f\n', pCBgivenSOBandX.T(2))
% explaining away - prob of CB is now lower given X, since LC is more
% likely
assert(approxeq(pCBgivenSOBandX.T, [0.5220; 0.4780]));

p(LC=1|SOB=1)=0.004
p(LC=1|SOB=1,XR=1)=0.106
p(CB=1|SOB=1)=0.496
p(CB=1|SOB=1,XR=1)=0.478

Unconditional marginals

mmap = zeros(1,5);
for i=1:5
  p = tabularFactorMarginalize(joint, i);
  fprintf('p(%s=1)=%5.3f\n', names{i}, p.T(2));
  if p.T(2)>0.5, mmap(i) = 1; end
end

p(S=1)=0.091
p(LC=1)=0.001
p(CB=1)=0.020
p(SOB=1)=0.090
p(X=1)=0.200

Joint mode

mode = argmax(joint.T)-1

mode =
     0     0     0     0     0

      Published with MATLAB® 7.12


text:
SLevelEditorModeContent::~SLevelEditorModeContent()
{
	GLevelEditorModeTools().OnEditorModeChanged().RemoveAll( this );
	GetMutableDefault<UEditorPerProjectUserSettings>()->OnUserSettingChanged().RemoveAll( this );
}

void SLevelEditorModeContent::Construct( const FArguments& InArgs, const TSharedRef< class ILevelEditor >& InOwningLevelEditor, const TSharedRef< class SDockTab >& InOwningDocTab, FEdMode* InEditorMode )
{
	LevelEditor = InOwningLevelEditor;
	DocTab = InOwningDocTab;
	EditorMode = InEditorMode;

	InOwningDocTab->SetOnTabClosed( SDockTab::FOnTabClosedCallback::CreateSP(this, &SLevelEditorModeContent::HandleParentClosed ) );
	GLevelEditorModeTools().OnEditorModeChanged().AddSP( this, &SLevelEditorModeContent::HandleEditorModeChanged );
	GetMutableDefault<UEditorPerProjectUserSettings>()->OnUserSettingChanged().AddSP( this, &SLevelEditorModeContent::HandleUserSettingsChange );

	ChildSlot
	[
		SNew( SHorizontalBox )

		// The Current Creation Tool
		+ SHorizontalBox::Slot()
		.FillWidth( 1.0 )
		.Padding( 2, 0, 0, 0 )
		[
			SNew( SVerticalBox )
			+ SVerticalBox::Slot()
			[
				SAssignNew(InlineContentHolder, SBorder)
				.BorderImage( FEditorStyle::GetBrush("NoBorder") )
				.Padding(0.f)
				.Visibility( this, &SLevelEditorModeContent::GetInlineContentHolderVisibility )
			]
		]
	];

	UpdateModeToolBar();
}

void SLevelEditorModeContent::HandleEditorModeChanged( FEdMode* Mode, bool IsEnabled )
{
	if ( Mode == EditorMode && !IsEnabled )
	{
		DocTab.Pin()->SetOnTabClosed( SDockTab::FOnTabClosedCallback() );
		DocTab.Pin()->RequestCloseTab();
	}
}

void SLevelEditorModeContent::HandleUserSettingsChange( FName PropertyName )
{
	UpdateModeToolBar();
}

void SLevelEditorModeContent::UpdateModeToolBar()
{
	const TArray< TSharedPtr< IToolkit > >& HostedToolkits = LevelEditor.Pin()->GetHostedToolkits();
	for( auto HostedToolkitIt = HostedToolkits.CreateConstIterator(); HostedToolkitIt; ++HostedToolkitIt )
	{
		UpdateInlineContent( ( *HostedToolkitIt )->GetInlineContent() );
		break;
	}
}

EVisibility SLevelEditorModeContent::GetInlineContentHolderVisibility() const
{
	return InlineContentHolder->GetContent() == SNullWidget::NullWidget ? EVisibility::Collapsed : EVisibility::Visible;
}

void SLevelEditorModeContent::UpdateInlineContent(TSharedPtr<SWidget> InlineContent) const
{
	if (InlineContent.IsValid() && InlineContentHolder.IsValid())
	{
		InlineContentHolder->SetContent(InlineContent.ToSharedRef());
	}
}

void SLevelEditorModeContent::OnToolkitHostingStarted( const TSharedRef< class IToolkit >& Toolkit )
{
	if( ToolkitArea.IsValid() )
	{
		ToolkitArea->OnToolkitHostingStarted( Toolkit );
	}

	UpdateInlineContent( Toolkit->GetInlineContent() );
}

void SLevelEditorModeContent::OnToolkitHostingFinished( const TSharedRef< class IToolkit >& Toolkit )
{
	if( ToolkitArea.IsValid() )
	{
		ToolkitArea->OnToolkitHostingFinished( Toolkit );
	}

	bool FoundAnotherToolkit = false;
	const TArray< TSharedPtr< IToolkit > >& HostedToolkits = LevelEditor.Pin()->GetHostedToolkits();
	for( auto HostedToolkitIt = HostedToolkits.CreateConstIterator(); HostedToolkitIt; ++HostedToolkitIt )
	{
		if ( ( *HostedToolkitIt ) != Toolkit )
		{
			UpdateInlineContent( ( *HostedToolkitIt )->GetInlineContent() );
			FoundAnotherToolkit = true;
			break;
		}
	}

	if ( !FoundAnotherToolkit )
	{
		UpdateInlineContent( SNullWidget::NullWidget );
	}
}

void SLevelEditorModeContent::HandleParentClosed( TSharedRef<SDockTab> TabBeingClosed )
{
	if ( GLevelEditorModeTools().IsModeActive(EditorMode->GetID()) )
	{
		GLevelEditorModeTools().DeactivateMode(EditorMode->GetID());
	}
}

#undef LOCTEXT_NAMESPACE


text:
from lxml import etree
from urllib.request import urlopen

import pandas as pd

DATA_URL = "https://en.wikipedia.org/w/index.php?title=Spacecraft_propulsion&oldid=760799107"

parser = etree.HTMLParser()
with urlopen(DATA_URL) as fp:
    all_html = etree.parse(fp, parser)

tables = [t for t in all_html.xpath(r"//table[@class='wikitable sortable']")]
table = tables[0]

# Clear sort values
for span in table.xpath(r"//span[@style='display:none']"):
    span.text = ""

with open("table_stripped_final.html", "w") as fp:
    print(etree.tostring(table, pretty_print=True).decode("utf-8"), file=fp)

# TODO: We could save an intermediate file here
data = pd.read_html("table_stripped_final.html")[0]
data.columns = data.iloc[0].values

# Remove top and bottom headers
data = data.iloc[1:-1]

# Use None values
data = data.replace(["?"], [None])

# Replace non-ASCII character
data = data.replace(["(.*) – (.*)"], [r"\1 ~ \2"], regex=True)

# Remove citation marks
data = data.replace([r"(.*)\[(full )?citation needed\]"], [r"\1"], regex=True)
data = data.replace(["([^\]]*)(\[.+\])+"], [r"\1"], regex=True)

# Final cleanup of corner cases
data.at[12, "Thrust (N)"] = "9/km2 ~ 230/km2"
data.at[12, "Technology readiness level"] = "5: Light-sail validated in lit vacuum\'"

data.at[25, "Technology readiness level"] = data.at[25, "Thrust (N)"]
data.at[25, "Thrust (N)"] = None
data.at[25, "Effective exhaust velocity (km/s)"] = None

data.at[31, "Technology readiness level"] = data.at[31, "Thrust (N)"]
data.at[31, "Thrust (N)"] = None
data.at[31, "Effective exhaust velocity (km/s)"] = None

# Export
data.to_csv("table.csv", index=False)

# Simplify LaTeX output
data["TRL"] = data.pop("Technology readiness level").replace(["(\d):.*"], [r"\1"], regex=True).astype(int)

# http://tex.stackexchange.com/a/63592/2488
with open("table_long.tex", "w") as fp:
    data.fillna("?").to_latex(fp, index=False, longtable=True)


text:
exports.before = function (methodName, callback, context) {
  return weave.call(this, 'before', methodName, callback, context);
};

// 在指定方法执行后,再执行 callback
exports.after = function (methodName, callback, context) {
  return weave.call(this, 'after', methodName, callback, context);
};

// Helpers
// -------

var eventSplitter = /\s+/;

function weave(when, methodName, callback, context) {
  var names = methodName.split(eventSplitter);
  var name, method;

  while ((name = names.shift())) {
    method = getMethod(this, name);
    if (!method.__isAspected) {
      wrap.call(this, name);
    }
    this.on(when + ':' + name, callback, context);
  }

  return this;
}

function getMethod(host, methodName) {
  var method = host[methodName];
  if (!method) {
    throw new Error('Invalid method name: ' + methodName);
  }
  return method;
}

function wrap(methodName) {
  var old = this[methodName];

  this[methodName] = function () {
    var args = Array.prototype.slice.call(arguments);
    var beforeArgs = ['before:' + methodName].concat(args);

    // prevent if trigger return false
    if (this.trigger.apply(this, beforeArgs) === false) return;

    var ret = old.apply(this, arguments);
    var afterArgs = ['after:' + methodName, ret].concat(args);
    this.trigger.apply(this, afterArgs);

    return ret;
  };

  this[methodName].__isAspected = true;
}


text:
<?php
namespace Smth\AjaxAuthBundle\Security\Authentication;

use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;

use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface,
	Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;

use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\HttpFoundation\Request,
	Symfony\Component\HttpFoundation\Response,
	Symfony\Component\HttpFoundation\RedirectResponse;

class AuthenticationHandler
implements AuthenticationSuccessHandlerInterface,
		   AuthenticationFailureHandlerInterface
{
	private $router;
	private $defaultRedirectPath;
	private $alwaysUseDefaultPath;

	public function __construct(Router $router, $defaultRedirectPath, $alwaysUseDefaultPath)
	{
		$this->router 				= $router;
		$this->defaultRedirectPath  = $defaultRedirectPath;
		$this->alwaysUseDefaultPath = $alwaysUseDefaultPath;
	}

	public function onInteractiveLoginEvent(InteractiveLoginEvent $event)
	{
		return $event->response =
			$this->onAuthenticationSuccess($event->getRequest(), $event->getAuthenticationToken());
	}

	public function onAuthenticationSuccess(Request $request, TokenInterface $token)
	{
		if (
			($targetPath = $request->getSession()->get('_security.secured_area.target_path'))
			&& !$this->alwaysUseDefaultPath
		)
			$url = $targetPath;
		else // Otherwise, redirect him to wherever you want
			$url = $this->router->generate($this->defaultRedirectPath);

		if ($request->isXmlHttpRequest()) {
			$result = array('result' => 'login_succeded', 'redirect_url' => $url);
			return $this->ajaxResponse($result);
		} else {
			// If the user tried to access a protected resource and was forces to login
			// redirect him back to that resource
			return new RedirectResponse($url);
		}
	}

	public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
	{
		if ($request->isXmlHttpRequest()) {
			$result = array('result' => 'login_failed', 'message' => $exception->getMessage());
			return $this->ajaxResponse($result);
		} else {
			// Create a flash message with the authentication error message
			$request->getSession()->setFlash('error', $exception->getMessage());
			$url = $this->router->generate('user_login');

			return new RedirectResponse($url);
		}
	}

	protected function ajaxResponse($result)
	{
		$response = new Response(json_encode($result));
		$response->headers->set('Content-Type', 'application/json');
		return $response;
	}
}


text:
"""
Dynamic factor model

Author: Chad Fulton
License: Simplified-BSD
"""

import numpy as np
from .mlemodel import MLEModel, MLEResults, MLEResultsWrapper
from .tools import (
    is_invertible, prepare_exog,
    constrain_stationary_univariate, unconstrain_stationary_univariate,
    constrain_stationary_multivariate, unconstrain_stationary_multivariate
)
from statsmodels.multivariate.pca import PCA
from statsmodels.regression.linear_model import OLS
from statsmodels.tsa.vector_ar.var_model import VAR
from statsmodels.tsa.arima.model import ARIMA
from statsmodels.tools.tools import Bunch
from statsmodels.tools.data import _is_using_pandas
from statsmodels.tsa.tsatools import lagmat
from statsmodels.tools.decorators import cache_readonly
import statsmodels.base.wrapper as wrap
from statsmodels.compat.pandas import Appender

class DynamicFactor(MLEModel):
    r"""
    Dynamic factor model

    Parameters
    ----------
    endog : array_like
        The observed time-series process :math:`y`
    exog : array_like, optional
        Array of exogenous regressors for the observation equation, shaped
        nobs x k_exog.
    k_factors : int
        The number of unobserved factors.
    factor_order : int
        The order of the vector autoregression followed by the factors.
    error_cov_type : {'scalar', 'diagonal', 'unstructured'}, optional
        The structure of the covariance matrix of the observation error term,
        where "unstructured" puts no restrictions on the matrix, "diagonal"
        requires it to be any diagonal matrix (uncorrelated errors), and
        "scalar" requires it to be a scalar times the identity matrix. Default
        is "diagonal".
    error_order : int, optional
        The order of the vector autoregression followed by the observation
        error component. Default is None, corresponding to white noise errors.
    error_var : bool, optional
        Whether or not to model the errors jointly via a vector autoregression,
        rather than as individual autoregressions. Has no effect unless
        `error_order` is set. Default is False.
    enforce_stationarity : bool, optional
        Whether or not to transform the AR parameters to enforce stationarity
        in the autoregressive component of the model. Default is True.
    **kwargs
        Keyword arguments may be used to provide default values for state space
        matrices or for Kalman filtering options. See `Representation`, and
        `KalmanFilter` for more details.

    Attributes
    ----------
    exog : array_like, optional
        Array of exogenous regressors for the observation equation, shaped
        nobs x k_exog.
    k_factors : int
        The number of unobserved factors.
    factor_order : int
        The order of the vector autoregression followed by the factors.
    error_cov_type : {'diagonal', 'unstructured'}
        The structure of the covariance matrix of the error term, where
        "unstructured" puts no restrictions on the matrix and "diagonal"
        requires it to be a diagonal matrix (uncorrelated errors).
    error_order : int
        The order of the vector autoregression followed by the observation
        error component.
    error_var : bool
        Whether or not to model the errors jointly via a vector autoregression,
        rather than as individual autoregressions. Has no effect unless
        `error_order` is set.
    enforce_stationarity : bool, optional
        Whether or not to transform the AR parameters to enforce stationarity
        in the autoregressive component of the model. Default is True.

    Notes
    -----
    The dynamic factor model considered here is in the so-called static form,
    and is specified:

    .. math::

        y_t & = \Lambda f_t + B x_t + u_t \\
        f_t & = A_1 f_{t-1} + \dots + A_p f_{t-p} + \eta_t \\
        u_t & = C_1 u_{t-1} + \dots + C_q u_{t-q} + \varepsilon_t

    where there are `k_endog` observed series and `k_factors` unobserved
    factors. Thus :math:`y_t` is a `k_endog` x 1 vector and :math:`f_t` is a
    `k_factors` x 1 vector.

    :math:`x_t` are optional exogenous vectors, shaped `k_exog` x 1.

    :math:`\eta_t` and :math:`\varepsilon_t` are white noise error terms. In
    order to identify the factors, :math:`Var(\eta_t) = I`. Denote
    :math:`Var(\varepsilon_t) \equiv \Sigma`.

    Options related to the unobserved factors:

    - `k_factors`: this is the dimension of the vector :math:`f_t`, above.
      To exclude factors completely, set `k_factors = 0`.
    - `factor_order`: this is the number of lags to include in the factor
      evolution equation, and corresponds to :math:`p`, above. To have static
      factors, set `factor_order = 0`.

    Options related to the observation error term :math:`u_t`:

    - `error_order`: the number of lags to include in the error evolution
      equation; corresponds to :math:`q`, above. To have white noise errors,
      set `error_order = 0` (this is the default).
    - `error_cov_type`: this controls the form of the covariance matrix
      :math:`\Sigma`. If it is "dscalar", then :math:`\Sigma = \sigma^2 I`. If
      it is "diagonal", then
      :math:`\Sigma = \text{diag}(\sigma_1^2, \dots, \sigma_n^2)`. If it is
      "unstructured", then :math:`\Sigma` is any valid variance / covariance
      matrix (i.e. symmetric and positive definite).
    - `error_var`: this controls whether or not the errors evolve jointly
      according to a VAR(q), or individually according to separate AR(q)
      processes. In terms of the formulation above, if `error_var = False`,
      then the matrices :math:C_i` are diagonal, otherwise they are general
      VAR matrices.

    References
    ----------
    .. [*] Lütkepohl, Helmut. 2007.
       New Introduction to Multiple Time Series Analysis.
       Berlin: Springer.
    """

    def __init__(self, endog, k_factors, factor_order, exog=None,
                 error_order=0, error_var=False, error_cov_type='diagonal',
                 enforce_stationarity=True, **kwargs):

        # Model properties
        self.enforce_stationarity = enforce_stationarity

        # Factor-related properties
        self.k_factors = k_factors
        self.factor_order = factor_order

        # Error-related properties
        self.error_order = error_order
        self.error_var = error_var and error_order > 0
        self.error_cov_type = error_cov_type

        # Exogenous data
        (self.k_exog, exog) = prepare_exog(exog)

        # Note: at some point in the future might add state regression, as in
        # SARIMAX.
        self.mle_regression = self.k_exog > 0

        # We need to have an array or pandas at this point
        if not _is_using_pandas(endog, None):
            endog = np.asanyarray(endog, order='C')

        # Save some useful model orders, internally used
        k_endog = endog.shape[1] if endog.ndim > 1 else 1
        self._factor_order = max(1, self.factor_order) * self.k_factors
        self._error_order = self.error_order * k_endog

        # Calculate the number of states
        k_states = self._factor_order
        k_posdef = self.k_factors
        if self.error_order > 0:
            k_states += self._error_order
            k_posdef += k_endog

        # We can still estimate the model with no dynamic state (e.g. SUR), we
        # just need to have one state that does nothing.
        self._unused_state = False
        if k_states == 0:
            k_states = 1
            k_posdef = 1
            self._unused_state = True

        # Test for non-multivariate endog
        if k_endog < 2:
            raise ValueError('The dynamic factors model is only valid for'
                             ' multivariate time series.')

        # Test for too many factors
        if self.k_factors >= k_endog:
            raise ValueError('Number of factors must be less than the number'
                             ' of endogenous variables.')

        # Test for invalid error_cov_type
        if self.error_cov_type not in ['scalar', 'diagonal', 'unstructured']:
            raise ValueError('Invalid error covariance matrix type'
                             ' specification.')

        # By default, initialize as stationary
        kwargs.setdefault('initialization', 'stationary')

        # Initialize the state space model
        super(DynamicFactor, self).__init__(
            endog, exog=exog, k_states=k_states, k_posdef=k_posdef, **kwargs
        )

        # Set as time-varying model if we have exog
        if self.k_exog > 0:
            self.ssm._time_invariant = False

        # Initialize the components
        self.parameters = {}
        self._initialize_loadings()
        self._initialize_exog()
        self._initialize_error_cov()
        self._initialize_factor_transition()
        self._initialize_error_transition()
        self.k_params = sum(self.parameters.values())

        # Cache parameter vector slices
        def _slice(key, offset):
            length = self.parameters[key]
            param_slice = np.s_[offset:offset + length]
            offset += length
            return param_slice, offset

        offset = 0
        self._params_loadings, offset = _slice('factor_loadings', offset)
        self._params_exog, offset = _slice('exog', offset)
        self._params_error_cov, offset = _slice('error_cov', offset)
        self._params_factor_transition, offset = (
            _slice('factor_transition', offset))
        self._params_error_transition, offset = (
            _slice('error_transition', offset))

        # Update _init_keys attached by super
        self._init_keys += ['k_factors', 'factor_order', 'error_order',
                            'error_var', 'error_cov_type',
                            'enforce_stationarity'] + list(kwargs.keys())

    def _initialize_loadings(self):
        # Initialize the parameters
        self.parameters['factor_loadings'] = self.k_endog * self.k_factors

        # Setup fixed components of state space matrices
        if self.error_order > 0:
            start = self._factor_order
            end = self._factor_order + self.k_endog
            self.ssm['design', :, start:end] = np.eye(self.k_endog)

        # Setup indices of state space matrices
        self._idx_loadings = np.s_['design', :, :self.k_factors]

    def _initialize_exog(self):
        # Initialize the parameters
        self.parameters['exog'] = self.k_exog * self.k_endog

        # If we have exog effects, then the obs intercept needs to be
        # time-varying
        if self.k_exog > 0:
            self.ssm['obs_intercept'] = np.zeros((self.k_endog, self.nobs))

        # Setup indices of state space matrices
        self._idx_exog = np.s_['obs_intercept', :self.k_endog, :]

    def _initialize_error_cov(self):
        if self.error_cov_type == 'scalar':
            self._initialize_error_cov_diagonal(scalar=True)
        elif self.error_cov_type == 'diagonal':
            self._initialize_error_cov_diagonal(scalar=False)
        elif self.error_cov_type == 'unstructured':
            self._initialize_error_cov_unstructured()

    def _initialize_error_cov_diagonal(self, scalar=False):
        # Initialize the parameters
        self.parameters['error_cov'] = 1 if scalar else self.k_endog

        # Setup fixed components of state space matrices

        # Setup indices of state space matrices
        k_endog = self.k_endog
        k_factors = self.k_factors
        idx = np.diag_indices(k_endog)
        if self.error_order > 0:
            matrix = 'state_cov'
            idx = (idx[0] + k_factors, idx[1] + k_factors)
        else:
            matrix = 'obs_cov'
        self._idx_error_cov = (matrix,) + idx

    def _initialize_error_cov_unstructured(self):
        # Initialize the parameters
        k_endog = self.k_endog
        self.parameters['error_cov'] = int(k_endog * (k_endog + 1) / 2)

        # Setup fixed components of state space matrices

        # Setup indices of state space matrices
        self._idx_lower_error_cov = np.tril_indices(self.k_endog)
        if self.error_order > 0:
            start = self.k_factors
            end = self.k_factors + self.k_endog
            self._idx_error_cov = (
                np.s_['state_cov', start:end, start:end])
        else:
            self._idx_error_cov = np.s_['obs_cov', :, :]

    def _initialize_factor_transition(self):
        order = self.factor_order * self.k_factors
        k_factors = self.k_factors

        # Initialize the parameters
        self.parameters['factor_transition'] = (
            self.factor_order * self.k_factors**2)

        # Setup fixed components of state space matrices
        # VAR(p) for factor transition
        if self.k_factors > 0:
            if self.factor_order > 0:
                self.ssm['transition', k_factors:order, :order - k_factors] = (
                    np.eye(order - k_factors))

            self.ssm['selection', :k_factors, :k_factors] = np.eye(k_factors)
            # Identification requires constraining the state covariance to an
            # identity matrix
            self.ssm['state_cov', :k_factors, :k_factors] = np.eye(k_factors)

        # Setup indices of state space matrices
        self._idx_factor_transition = np.s_['transition', :k_factors, :order]

    def _initialize_error_transition(self):
        # Initialize the appropriate situation
        if self.error_order == 0:
            self._initialize_error_transition_white_noise()
        else:
            # Generic setup fixed components of state space matrices
            # VAR(q) for error transition
            # (in the individual AR case, we still have the VAR(q) companion
            # matrix structure, but force the coefficient matrices to be
            # diagonal)
            k_endog = self.k_endog
            k_factors = self.k_factors
            _factor_order = self._factor_order
            _error_order = self._error_order
            _slice = np.s_['selection',
                           _factor_order:_factor_order + k_endog,
                           k_factors:k_factors + k_endog]
            self.ssm[_slice] = np.eye(k_endog)
            _slice = np.s_[
                'transition',
                _factor_order + k_endog:_factor_order + _error_order,
                _factor_order:_factor_order + _error_order - k_endog]
            self.ssm[_slice] = np.eye(_error_order - k_endog)

            # Now specialized setups
            if self.error_var:
                self._initialize_error_transition_var()
            else:
                self._initialize_error_transition_individual()

    def _initialize_error_transition_white_noise(self):
        # Initialize the parameters
        self.parameters['error_transition'] = 0

        # No fixed components of state space matrices

        # Setup indices of state space matrices (just an empty slice)
        self._idx_error_transition = np.s_['transition', 0:0, 0:0]

    def _initialize_error_transition_var(self):
        k_endog = self.k_endog
        _factor_order = self._factor_order
        _error_order = self._error_order

        # Initialize the parameters
        self.parameters['error_transition'] = _error_order * k_endog

        # Fixed components already setup above

        # Setup indices of state space matrices
        # Here we want to set all of the elements of the coefficient matrices,
        # the same as in a VAR specification
        self._idx_error_transition = np.s_[
            'transition',
            _factor_order:_factor_order + k_endog,
            _factor_order:_factor_order + _error_order]

    def _initialize_error_transition_individual(self):
        k_endog = self.k_endog
        _error_order = self._error_order

        # Initialize the parameters
        self.parameters['error_transition'] = _error_order

        # Fixed components already setup above

        # Setup indices of state space matrices
        # Here we want to set only the diagonal elements of the coefficient
        # matrices, and we want to set them in order by equation, not by
        # matrix (i.e. set the first element of the first matrix's diagonal,
        # then set the first element of the second matrix's diagonal, then...)

        # The basic setup is a tiled list of diagonal indices, one for each
        # coefficient matrix
        idx = np.tile(np.diag_indices(k_endog), self.error_order)
        # Now we need to shift the rows down to the correct location
        row_shift = self._factor_order
        # And we need to shift the columns in an increasing way
        col_inc = self._factor_order + np.repeat(
            [i * k_endog for i in range(self.error_order)], k_endog)
        idx[0] += row_shift
        idx[1] += col_inc

        # Make a copy (without the row shift) so that we can easily get the
        # diagonal parameters back out of a generic coefficients matrix array
        idx_diag = idx.copy()
        idx_diag[0] -= row_shift
        idx_diag[1] -= self._factor_order
        idx_diag = idx_diag[:, np.lexsort((idx_diag[1], idx_diag[0]))]
        self._idx_error_diag = (idx_diag[0], idx_diag[1])

        # Finally, we want to fill the entries in in the correct order, which
        # is to say we want to fill in lexicographically, first by row then by
        # column
        idx = idx[:, np.lexsort((idx[1], idx[0]))]
        self._idx_error_transition = np.s_['transition', idx[0], idx[1]]

    def clone(self, endog, exog=None, **kwargs):
        return self._clone_from_init_kwds(endog, exog=exog, **kwargs)

    @property
    def _res_classes(self):
        return {'fit': (DynamicFactorResults, DynamicFactorResultsWrapper)}

    @property
    def start_params(self):
        params = np.zeros(self.k_params, dtype=np.float64)

        endog = self.endog.copy()
        mask = ~np.any(np.isnan(endog), axis=1)
        endog = endog[mask]
        if self.k_exog > 0:
            exog = self.exog[mask]

        # 1. Factor loadings (estimated via PCA)
        if self.k_factors > 0:
            # Use principal components + OLS as starting values
            res_pca = PCA(endog, ncomp=self.k_factors)
            mod_ols = OLS(endog, res_pca.factors)
            res_ols = mod_ols.fit()

            # Using OLS params for the loadings tends to gives higher starting
            # log-likelihood.
            params[self._params_loadings] = res_ols.params.T.ravel()
            # params[self._params_loadings] = res_pca.loadings.ravel()

            # However, using res_ols.resid tends to causes non-invertible
            # starting VAR coefficients for error VARs
            # endog = res_ols.resid
            endog = endog - np.dot(res_pca.factors, res_pca.loadings.T)

        # 2. Exog (OLS on residuals)
        if self.k_exog > 0:
            mod_ols = OLS(endog, exog=exog)
            res_ols = mod_ols.fit()
            # In the form: beta.x1.y1, beta.x2.y1, beta.x1.y2, ...
            params[self._params_exog] = res_ols.params.T.ravel()
            endog = res_ols.resid

        # 3. Factors (VAR on res_pca.factors)
        stationary = True
        if self.k_factors > 1 and self.factor_order > 0:
            # 3a. VAR transition (OLS on factors estimated via PCA)
            mod_factors = VAR(res_pca.factors)
            res_factors = mod_factors.fit(maxlags=self.factor_order, ic=None,
                                          trend='n')
            # Save the parameters
            params[self._params_factor_transition] = (
                res_factors.params.T.ravel())

            # Test for stationarity
            coefficient_matrices = (
                params[self._params_factor_transition].reshape(
                    self.k_factors * self.factor_order, self.k_factors
                ).T
            ).reshape(self.k_factors, self.k_factors, self.factor_order).T

            stationary = is_invertible([1] + list(-coefficient_matrices))
        elif self.k_factors > 0 and self.factor_order > 0:
            # 3b. AR transition
            Y = res_pca.factors[self.factor_order:]
            X = lagmat(res_pca.factors, self.factor_order, trim='both')
            params_ar = np.linalg.pinv(X).dot(Y)
            stationary = is_invertible(np.r_[1, -params_ar.squeeze()])
            params[self._params_factor_transition] = params_ar[:, 0]

        # Check for stationarity
        if not stationary and self.enforce_stationarity:
            raise ValueError('Non-stationary starting autoregressive'
                             ' parameters found with `enforce_stationarity`'
                             ' set to True.')

        # 4. Errors
        if self.error_order == 0:
            if self.error_cov_type == 'scalar':
                params[self._params_error_cov] = endog.var(axis=0).mean()
            elif self.error_cov_type == 'diagonal':
                params[self._params_error_cov] = endog.var(axis=0)
            elif self.error_cov_type == 'unstructured':
                cov_factor = np.diag(endog.std(axis=0))
                params[self._params_error_cov] = (
                    cov_factor[self._idx_lower_error_cov].ravel())
        elif self.error_var:
            mod_errors = VAR(endog)
            res_errors = mod_errors.fit(maxlags=self.error_order, ic=None,
                                        trend='n')

            # Test for stationarity
            coefficient_matrices = (
                np.array(res_errors.params.T).ravel().reshape(
                    self.k_endog * self.error_order, self.k_endog
                ).T
            ).reshape(self.k_endog, self.k_endog, self.error_order).T

            stationary = is_invertible([1] + list(-coefficient_matrices))
            if not stationary and self.enforce_stationarity:
                raise ValueError('Non-stationary starting error autoregressive'
                                 ' parameters found with'
                                 ' `enforce_stationarity` set to True.')

            # Get the error autoregressive parameters
            params[self._params_error_transition] = (
                    np.array(res_errors.params.T).ravel())

            # Get the error covariance parameters
            if self.error_cov_type == 'scalar':
                params[self._params_error_cov] = (
                    res_errors.sigma_u.diagonal().mean())
            elif self.error_cov_type == 'diagonal':
                params[self._params_error_cov] = res_errors.sigma_u.diagonal()
            elif self.error_cov_type == 'unstructured':
                try:
                    cov_factor = np.linalg.cholesky(res_errors.sigma_u)
                except np.linalg.LinAlgError:
                    cov_factor = np.eye(res_errors.sigma_u.shape[0]) * (
                        res_errors.sigma_u.diagonal().mean()**0.5)
                cov_factor = np.eye(res_errors.sigma_u.shape[0]) * (
                    res_errors.sigma_u.diagonal().mean()**0.5)
                params[self._params_error_cov] = (
                    cov_factor[self._idx_lower_error_cov].ravel())
        else:
            error_ar_params = []
            error_cov_params = []
            for i in range(self.k_endog):
                mod_error = ARIMA(endog[:, i], order=(self.error_order, 0, 0),
                                  trend='n', enforce_stationarity=True)
                res_error = mod_error.fit(method='burg')
                error_ar_params += res_error.params[:self.error_order].tolist()
                error_cov_params += res_error.params[-1:].tolist()

            params[self._params_error_transition] = np.r_[error_ar_params]
            params[self._params_error_cov] = np.r_[error_cov_params]

        return params

    @property
    def param_names(self):
        param_names = []
        endog_names = self.endog_names

        # 1. Factor loadings
        param_names += [
            'loading.f%d.%s' % (j+1, endog_names[i])
            for i in range(self.k_endog)
            for j in range(self.k_factors)
        ]

        # 2. Exog
        # Recall these are in the form: beta.x1.y1, beta.x2.y1, beta.x1.y2, ...
        param_names += [
            'beta.%s.%s' % (self.exog_names[j], endog_names[i])
            for i in range(self.k_endog)
            for j in range(self.k_exog)
        ]

        # 3. Error covariances
        if self.error_cov_type == 'scalar':
            param_names += ['sigma2']
        elif self.error_cov_type == 'diagonal':
            param_names += [
                'sigma2.%s' % endog_names[i]
                for i in range(self.k_endog)
            ]
        elif self.error_cov_type == 'unstructured':
            param_names += [
                'cov.chol[%d,%d]' % (i + 1, j + 1)
                for i in range(self.k_endog)
                for j in range(i+1)
            ]

        # 4. Factor transition VAR
        param_names += [
            'L%d.f%d.f%d' % (i+1, k+1, j+1)
            for j in range(self.k_factors)
            for i in range(self.factor_order)
            for k in range(self.k_factors)
        ]

        # 5. Error transition VAR
        if self.error_var:
            param_names += [
                'L%d.e(%s).e(%s)' % (i+1, endog_names[k], endog_names[j])
                for j in range(self.k_endog)
                for i in range(self.error_order)
                for k in range(self.k_endog)
            ]
        else:
            param_names += [
                'L%d.e(%s).e(%s)' % (i+1, endog_names[j], endog_names[j])
                for j in range(self.k_endog)
                for i in range(self.error_order)
            ]

        return param_names

    @property
    def state_names(self):
        names = []
        endog_names = self.endog_names

        # Factors and lags
        names += [
            (('f%d' % (j + 1)) if i == 0 else ('f%d.L%d' % (j + 1, i)))
            for i in range(max(1, self.factor_order))
            for j in range(self.k_factors)]

        if self.error_order > 0:
            names += [
                (('e(%s)' % endog_names[j]) if i == 0
                 else ('e(%s).L%d' % (endog_names[j], i)))
                for i in range(self.error_order)
                for j in range(self.k_endog)]

        if self._unused_state:
            names += ['dummy']

        return names

    def transform_params(self, unconstrained):
        """
        Transform unconstrained parameters used by the optimizer to constrained
        parameters used in likelihood evaluation

        Parameters
        ----------
        unconstrained : array_like
            Array of unconstrained parameters used by the optimizer, to be
            transformed.

        Returns
        -------
        constrained : array_like
            Array of constrained parameters which may be used in likelihood
            evaluation.

        Notes
        -----
        Constrains the factor transition to be stationary and variances to be
        positive.
        """
        unconstrained = np.array(unconstrained, ndmin=1)
        dtype = unconstrained.dtype
        constrained = np.zeros(unconstrained.shape, dtype=dtype)

        # 1. Factor loadings
        # The factor loadings do not need to be adjusted
        constrained[self._params_loadings] = (
            unconstrained[self._params_loadings])

        # 2. Exog
        # The regression coefficients do not need to be adjusted
        constrained[self._params_exog] = (
            unconstrained[self._params_exog])

        # 3. Error covariances
        # If we have variances, force them to be positive
        if self.error_cov_type in ['scalar', 'diagonal']:
            constrained[self._params_error_cov] = (
                unconstrained[self._params_error_cov]**2)
        # Otherwise, nothing needs to be done
        elif self.error_cov_type == 'unstructured':
            constrained[self._params_error_cov] = (
                unconstrained[self._params_error_cov])

        # 4. Factor transition VAR
        # VAR transition: optionally force to be stationary
        if self.enforce_stationarity and self.factor_order > 0:
            # Transform the parameters
            unconstrained_matrices = (
                unconstrained[self._params_factor_transition].reshape(
                    self.k_factors, self._factor_order))
            # This is always an identity matrix, but because the transform
            # done prior to update (where the ssm representation matrices
            # change), it may be complex
            cov = self.ssm['state_cov', :self.k_factors, :self.k_factors].real
            coefficient_matrices, variance = (
                constrain_stationary_multivariate(unconstrained_matrices, cov))
            constrained[self._params_factor_transition] = (
                coefficient_matrices.ravel())
        else:
            constrained[self._params_factor_transition] = (
                unconstrained[self._params_factor_transition])

        # 5. Error transition VAR
        # VAR transition: optionally force to be stationary
        if self.enforce_stationarity and self.error_order > 0:

            # Joint VAR specification
            if self.error_var:
                unconstrained_matrices = (
                    unconstrained[self._params_error_transition].reshape(
                        self.k_endog, self._error_order))
                start = self.k_factors
                end = self.k_factors + self.k_endog
                cov = self.ssm['state_cov', start:end, start:end].real
                coefficient_matrices, variance = (
                    constrain_stationary_multivariate(
                        unconstrained_matrices, cov))
                constrained[self._params_error_transition] = (
                    coefficient_matrices.ravel())
            # Separate AR specifications
            else:
                coefficients = (
                    unconstrained[self._params_error_transition].copy())
                for i in range(self.k_endog):
                    start = i * self.error_order
                    end = (i + 1) * self.error_order
                    coefficients[start:end] = constrain_stationary_univariate(
                        coefficients[start:end])
                constrained[self._params_error_transition] = coefficients

        else:
            constrained[self._params_error_transition] = (
                unconstrained[self._params_error_transition])

        return constrained

    def untransform_params(self, constrained):
        """
        Transform constrained parameters used in likelihood evaluation
        to unconstrained parameters used by the optimizer.

        Parameters
        ----------
        constrained : array_like
            Array of constrained parameters used in likelihood evaluation, to
            be transformed.

        Returns
        -------
        unconstrained : array_like
            Array of unconstrained parameters used by the optimizer.
        """
        constrained = np.array(constrained, ndmin=1)
        dtype = constrained.dtype
        unconstrained = np.zeros(constrained.shape, dtype=dtype)

        # 1. Factor loadings
        # The factor loadings do not need to be adjusted
        unconstrained[self._params_loadings] = (
            constrained[self._params_loadings])

        # 2. Exog
        # The regression coefficients do not need to be adjusted
        unconstrained[self._params_exog] = (
            constrained[self._params_exog])

        # 3. Error covariances
        # If we have variances, force them to be positive
        if self.error_cov_type in ['scalar', 'diagonal']:
            unconstrained[self._params_error_cov] = (
                constrained[self._params_error_cov]**0.5)
        # Otherwise, nothing needs to be done
        elif self.error_cov_type == 'unstructured':
            unconstrained[self._params_error_cov] = (
                constrained[self._params_error_cov])

        # 3. Factor transition VAR
        # VAR transition: optionally force to be stationary
        if self.enforce_stationarity and self.factor_order > 0:
            # Transform the parameters
            constrained_matrices = (
                constrained[self._params_factor_transition].reshape(
                    self.k_factors, self._factor_order))
            cov = self.ssm['state_cov', :self.k_factors, :self.k_factors].real
            coefficient_matrices, variance = (
                unconstrain_stationary_multivariate(
                    constrained_matrices, cov))
            unconstrained[self._params_factor_transition] = (
                coefficient_matrices.ravel())
        else:
            unconstrained[self._params_factor_transition] = (
                constrained[self._params_factor_transition])

        # 5. Error transition VAR
        # VAR transition: optionally force to be stationary
        if self.enforce_stationarity and self.error_order > 0:

            # Joint VAR specification
            if self.error_var:
                constrained_matrices = (
                    constrained[self._params_error_transition].reshape(
                        self.k_endog, self._error_order))
                start = self.k_factors
                end = self.k_factors + self.k_endog
                cov = self.ssm['state_cov', start:end, start:end].real
                coefficient_matrices, variance = (
                    unconstrain_stationary_multivariate(
                        constrained_matrices, cov))
                unconstrained[self._params_error_transition] = (
                    coefficient_matrices.ravel())
            # Separate AR specifications
            else:
                coefficients = (
                    constrained[self._params_error_transition].copy())
                for i in range(self.k_endog):
                    start = i * self.error_order
                    end = (i + 1) * self.error_order
                    coefficients[start:end] = (
                        unconstrain_stationary_univariate(
                            coefficients[start:end]))
                unconstrained[self._params_error_transition] = coefficients

        else:
            unconstrained[self._params_error_transition] = (
                constrained[self._params_error_transition])

        return unconstrained

    def _validate_can_fix_params(self, param_names):
        super(DynamicFactor, self)._validate_can_fix_params(param_names)

        ix = np.cumsum(list(self.parameters.values()))[:-1]
        (_, _, _, factor_transition_names, error_transition_names) = [
            arr.tolist() for arr in np.array_split(self.param_names, ix)]

        if self.enforce_stationarity and self.factor_order > 0:
            if self.k_factors > 1 or self.factor_order > 1:
                fix_all = param_names.issuperset(factor_transition_names)
                fix_any = (
                    len(param_names.intersection(factor_transition_names)) > 0)
                if fix_any and not fix_all:
                    raise ValueError(
                        'Cannot fix individual factor transition parameters'
                        ' when `enforce_stationarity=True`. In this case,'
                        ' must either fix all factor transition parameters or'
                        ' none.')
        if self.enforce_stationarity and self.error_order > 0:
            if self.error_var or self.error_order > 1:
                fix_all = param_names.issuperset(error_transition_names)
                fix_any = (
                    len(param_names.intersection(error_transition_names)) > 0)
                if fix_any and not fix_all:
                    raise ValueError(
                        'Cannot fix individual error transition parameters'
                        ' when `enforce_stationarity=True`. In this case,'
                        ' must either fix all error transition parameters or'
                        ' none.')

    def update(self, params, transformed=True, includes_fixed=False,
               complex_step=False):
        """
        Update the parameters of the model

        Updates the representation matrices to fill in the new parameter
        values.

        Parameters
        ----------
        params : array_like
            Array of new parameters.
        transformed : bool, optional
            Whether or not `params` is already transformed. If set to False,
            `transform_params` is called. Default is True..

        Returns
        -------
        params : array_like
            Array of parameters.

        Notes
        -----
        Let `n = k_endog`, `m = k_factors`, and `p = factor_order`. Then the
        `params` vector has length
        :math:`[n \times m] + [n] + [m^2 \times p]`.
        It is expanded in the following way:

        - The first :math:`n \times m` parameters fill out the factor loading
          matrix, starting from the [0,0] entry and then proceeding along rows.
          These parameters are not modified in `transform_params`.
        - The next :math:`n` parameters provide variances for the error_cov
          errors in the observation equation. They fill in the diagonal of the
          observation covariance matrix, and are constrained to be positive by
          `transofrm_params`.
        - The next :math:`m^2 \times p` parameters are used to create the `p`
          coefficient matrices for the vector autoregression describing the
          factor transition. They are transformed in `transform_params` to
          enforce stationarity of the VAR(p). They are placed so as to make
          the transition matrix a companion matrix for the VAR. In particular,
          we assume that the first :math:`m^2` parameters fill the first
          coefficient matrix (starting at [0,0] and filling along rows), the
          second :math:`m^2` parameters fill the second matrix, etc.
        """
        params = self.handle_params(params, transformed=transformed,
                                    includes_fixed=includes_fixed)

        # 1. Factor loadings
        # Update the design / factor loading matrix
        self.ssm[self._idx_loadings] = (
            params[self._params_loadings].reshape(self.k_endog, self.k_factors)
        )

        # 2. Exog
        if self.k_exog > 0:
            exog_params = params[self._params_exog].reshape(
                self.k_endog, self.k_exog).T
            self.ssm[self._idx_exog] = np.dot(self.exog, exog_params).T

        # 3. Error covariances
        if self.error_cov_type in ['scalar', 'diagonal']:
            self.ssm[self._idx_error_cov] = (
                params[self._params_error_cov])
        elif self.error_cov_type == 'unstructured':
            error_cov_lower = np.zeros((self.k_endog, self.k_endog),
                                       dtype=params.dtype)
            error_cov_lower[self._idx_lower_error_cov] = (
                params[self._params_error_cov])
            self.ssm[self._idx_error_cov] = (
                np.dot(error_cov_lower, error_cov_lower.T))

        # 4. Factor transition VAR
        self.ssm[self._idx_factor_transition] = (
            params[self._params_factor_transition].reshape(
                self.k_factors, self.factor_order * self.k_factors))

        # 5. Error transition VAR
        if self.error_var:
            self.ssm[self._idx_error_transition] = (
                params[self._params_error_transition].reshape(
                    self.k_endog, self._error_order))
        else:
            self.ssm[self._idx_error_transition] = (
                params[self._params_error_transition])

class DynamicFactorResults(MLEResults):
    """
    Class to hold results from fitting an DynamicFactor model.

    Parameters
    ----------
    model : DynamicFactor instance
        The fitted model instance

    Attributes
    ----------
    specification : dictionary
        Dictionary including all attributes from the DynamicFactor model
        instance.
    coefficient_matrices_var : ndarray
        Array containing autoregressive lag polynomial coefficient matrices,
        ordered from lowest degree to highest.

    See Also
    --------
    statsmodels.tsa.statespace.kalman_filter.FilterResults
    statsmodels.tsa.statespace.mlemodel.MLEResults
    """
    def __init__(self, model, params, filter_results, cov_type=None,
                 **kwargs):
        super(DynamicFactorResults, self).__init__(model, params,
                                                   filter_results, cov_type,
                                                   **kwargs)

        self.df_resid = np.inf  # attribute required for wald tests

        self.specification = Bunch(**{
            # Model properties
            'k_endog': self.model.k_endog,
            'enforce_stationarity': self.model.enforce_stationarity,

            # Factor-related properties
            'k_factors': self.model.k_factors,
            'factor_order': self.model.factor_order,

            # Error-related properties
            'error_order': self.model.error_order,
            'error_var': self.model.error_var,
            'error_cov_type': self.model.error_cov_type,

            # Other properties
            'k_exog': self.model.k_exog
        })

        # Polynomials / coefficient matrices
        self.coefficient_matrices_var = None
        if self.model.factor_order > 0:
            ar_params = (
                np.array(self.params[self.model._params_factor_transition]))
            k_factors = self.model.k_factors
            factor_order = self.model.factor_order
            self.coefficient_matrices_var = (
                ar_params.reshape(k_factors * factor_order, k_factors).T
            ).reshape(k_factors, k_factors, factor_order).T

        self.coefficient_matrices_error = None
        if self.model.error_order > 0:
            ar_params = (
                np.array(self.params[self.model._params_error_transition]))
            k_endog = self.model.k_endog
            error_order = self.model.error_order
            if self.model.error_var:
                self.coefficient_matrices_error = (
                    ar_params.reshape(k_endog * error_order, k_endog).T
                ).reshape(k_endog, k_endog, error_order).T
            else:
                mat = np.zeros((k_endog, k_endog * error_order))
                mat[self.model._idx_error_diag] = ar_params
                self.coefficient_matrices_error = (
                    mat.T.reshape(error_order, k_endog, k_endog))

    @property
    def factors(self):
        """
        Estimates of unobserved factors

        Returns
        -------
        out : Bunch
            Has the following attributes shown in Notes.

        Notes
        -----
        The output is a bunch of the following format:

        - `filtered`: a time series array with the filtered estimate of
          the component
        - `filtered_cov`: a time series array with the filtered estimate of
          the variance/covariance of the component
        - `smoothed`: a time series array with the smoothed estimate of
          the component
        - `smoothed_cov`: a time series array with the smoothed estimate of
          the variance/covariance of the component
        - `offset`: an integer giving the offset in the state vector where
          this component begins
        """
        # If present, level is always the first component of the state vector
        out = None
        spec = self.specification
        if spec.k_factors > 0:
            offset = 0
            end = spec.k_factors
            res = self.filter_results
            out = Bunch(
                filtered=res.filtered_state[offset:end],
                filtered_cov=res.filtered_state_cov[offset:end, offset:end],
                smoothed=None, smoothed_cov=None,
                offset=offset)
            if self.smoothed_state is not None:
                out.smoothed = self.smoothed_state[offset:end]
            if self.smoothed_state_cov is not None:
                out.smoothed_cov = (
                    self.smoothed_state_cov[offset:end, offset:end])
        return out

    @cache_readonly
    def coefficients_of_determination(self):
        """
        Coefficients of determination (:math:`R^2`) from regressions of
        individual estimated factors on endogenous variables.

        Returns
        -------
        coefficients_of_determination : ndarray
            A `k_endog` x `k_factors` array, where
            `coefficients_of_determination[i, j]` represents the :math:`R^2`
            value from a regression of factor `j` and a constant on endogenous
            variable `i`.

        Notes
        -----
        Although it can be difficult to interpret the estimated factor loadings
        and factors, it is often helpful to use the coefficients of
        determination from univariate regressions to assess the importance of
        each factor in explaining the variation in each endogenous variable.

        In models with many variables and factors, this can sometimes lend
        interpretation to the factors (for example sometimes one factor will
        load primarily on real variables and another on nominal variables).

        See Also
        --------
        plot_coefficients_of_determination
        """
        from statsmodels.tools import add_constant
        spec = self.specification
        coefficients = np.zeros((spec.k_endog, spec.k_factors))
        which = 'filtered' if self.smoothed_state is None else 'smoothed'

        for i in range(spec.k_factors):
            exog = add_constant(self.factors[which][i])
            for j in range(spec.k_endog):
                endog = self.filter_results.endog[j]
                coefficients[j, i] = OLS(endog, exog).fit().rsquared

        return coefficients

    def plot_coefficients_of_determination(self, endog_labels=None,
                                           fig=None, figsize=None):
        """
        Plot the coefficients of determination

        Parameters
        ----------
        endog_labels : bool, optional
            Whether or not to label the endogenous variables along the x-axis
            of the plots. Default is to include labels if there are 5 or fewer
            endogenous variables.
        fig : Figure, optional
            If given, subplots are created in this figure instead of in a new
            figure. Note that the grid will be created in the provided
            figure using `fig.add_subplot()`.
        figsize : tuple, optional
            If a figure is created, this argument allows specifying a size.
            The tuple is (width, height).

        Notes
        -----

        Produces a `k_factors` x 1 plot grid. The `i`th plot shows a bar plot
        of the coefficients of determination associated with factor `i`. The
        endogenous variables are arranged along the x-axis according to their
        position in the `endog` array.

        See Also
        --------
        coefficients_of_determination
        """
        from statsmodels.graphics.utils import _import_mpl, create_mpl_fig
        _import_mpl()
        fig = create_mpl_fig(fig, figsize)

        spec = self.specification

        # Should we label endogenous variables?
        if endog_labels is None:
            endog_labels = spec.k_endog <= 5

        # Plot the coefficients of determination
        coefficients_of_determination = self.coefficients_of_determination
        plot_idx = 1
        locations = np.arange(spec.k_endog)
        for coeffs in coefficients_of_determination.T:
            # Create the new axis
            ax = fig.add_subplot(spec.k_factors, 1, plot_idx)
            ax.set_ylim((0, 1))
            ax.set(title='Factor %i' % plot_idx, ylabel=r'$R^2$')
            bars = ax.bar(locations, coeffs)

            if endog_labels:
                width = bars[0].get_width()
                ax.xaxis.set_ticks(locations + width / 2)
                ax.xaxis.set_ticklabels(self.model.endog_names)
            else:
                ax.set(xlabel='Endogenous variables')
                ax.xaxis.set_ticks([])

            plot_idx += 1

        return fig

    @Appender(MLEResults.summary.__doc__)
    def summary(self, alpha=.05, start=None, separate_params=True):
        from statsmodels.iolib.summary import summary_params
        spec = self.specification

        # Create the model name
        model_name = []
        if spec.k_factors > 0:
            if spec.factor_order > 0:
                model_type = ('DynamicFactor(factors=%d, order=%d)' %
                              (spec.k_factors, spec.factor_order))
            else:
                model_type = 'StaticFactor(factors=%d)' % spec.k_factors

            model_name.append(model_type)
            if spec.k_exog > 0:
                model_name.append('%d regressors' % spec.k_exog)
        else:
            model_name.append('SUR(%d regressors)' % spec.k_exog)

        if spec.error_order > 0:
            error_type = 'VAR' if spec.error_var else 'AR'
            model_name.append('%s(%d) errors' % (error_type, spec.error_order))

        summary = super(DynamicFactorResults, self).summary(
            alpha=alpha, start=start, model_name=model_name,
            display_params=not separate_params
        )

        if separate_params:
            indices = np.arange(len(self.params))

            def make_table(self, mask, title, strip_end=True):
                res = (self, self.params[mask], self.bse[mask],
                       self.zvalues[mask], self.pvalues[mask],
                       self.conf_int(alpha)[mask])

                param_names = [
                    '.'.join(name.split('.')[:-1]) if strip_end else name
                    for name in
                    np.array(self.data.param_names)[mask].tolist()
                ]

                return summary_params(res, yname=None, xname=param_names,
                                      alpha=alpha, use_t=False, title=title)

            k_endog = self.model.k_endog
            k_exog = self.model.k_exog
            k_factors = self.model.k_factors
            factor_order = self.model.factor_order
            _factor_order = self.model._factor_order
            _error_order = self.model._error_order

            # Add parameter tables for each endogenous variable
            loading_indices = indices[self.model._params_loadings]
            loading_masks = []
            exog_indices = indices[self.model._params_exog]
            exog_masks = []
            for i in range(k_endog):
                # 1. Factor loadings
                # Recall these are in the form:
                # 'loading.f1.y1', 'loading.f2.y1', 'loading.f1.y2', ...

                loading_mask = (
                    loading_indices[i * k_factors:(i + 1) * k_factors])
                loading_masks.append(loading_mask)

                # 2. Exog
                # Recall these are in the form:
                # beta.x1.y1, beta.x2.y1, beta.x1.y2, ...
                exog_mask = exog_indices[i * k_exog:(i + 1) * k_exog]
                exog_masks.append(exog_mask)

                # Create the table
                mask = np.concatenate([loading_mask, exog_mask])
                title = "Results for equation %s" % self.model.endog_names[i]
                table = make_table(self, mask, title)
                summary.tables.append(table)

            # Add parameter tables for each factor
            factor_indices = indices[self.model._params_factor_transition]
            factor_masks = []
            if factor_order > 0:
                for i in range(k_factors):
                    start = i * _factor_order
                    factor_mask = factor_indices[start: start + _factor_order]
                    factor_masks.append(factor_mask)

                    # Create the table
                    title = "Results for factor equation f%d" % (i+1)
                    table = make_table(self, factor_mask, title)
                    summary.tables.append(table)

            # Add parameter tables for error transitions
            error_masks = []
            if spec.error_order > 0:
                error_indices = indices[self.model._params_error_transition]
                for i in range(k_endog):
                    if spec.error_var:
                        start = i * _error_order
                        end = (i + 1) * _error_order
                    else:
                        start = i * spec.error_order
                        end = (i + 1) * spec.error_order

                    error_mask = error_indices[start:end]
                    error_masks.append(error_mask)

                    # Create the table
                    title = ("Results for error equation e(%s)" %
                             self.model.endog_names[i])
                    table = make_table(self, error_mask, title)
                    summary.tables.append(table)

            # Error covariance terms
            error_cov_mask = indices[self.model._params_error_cov]
            table = make_table(self, error_cov_mask,
                               "Error covariance matrix", strip_end=False)
            summary.tables.append(table)

            # Add a table for all other parameters
            masks = []
            for m in (loading_masks, exog_masks, factor_masks,
                      error_masks, [error_cov_mask]):
                m = np.array(m).flatten()
                if len(m) > 0:
                    masks.append(m)
            masks = np.concatenate(masks)
            inverse_mask = np.array(list(set(indices).difference(set(masks))))
            if len(inverse_mask) > 0:
                table = make_table(self, inverse_mask, "Other parameters",
                                   strip_end=False)
                summary.tables.append(table)

        return summary

class DynamicFactorResultsWrapper(MLEResultsWrapper):
    _attrs = {}
    _wrap_attrs = wrap.union_dicts(MLEResultsWrapper._wrap_attrs,
                                   _attrs)
    _methods = {}
    _wrap_methods = wrap.union_dicts(MLEResultsWrapper._wrap_methods,
                                     _methods)
wrap.populate_wrapper(DynamicFactorResultsWrapper,  # noqa:E305
                      DynamicFactorResults)


text:
package org.cesiumjs.cs.datasources.properties;

import jsinterop.annotations.JsProperty;
import org.cesiumjs.cs.core.Cartesian3;
import org.cesiumjs.cs.core.Quaternion;
import org.cesiumjs.cs.core.TranslationRotationScale;
import org.cesiumjs.cs.datasources.properties.options.NodeTransformationPropertyOptions;

/**
 * A {@link Property} that produces {@link TranslationRotationScale} data.
 *
 * @author Serge Silaev aka iSergio
 */
public class NodeTransformationProperty<T extends TranslationRotationScale> extends Property<T> {
    /**
     * Gets or sets the Quaternion Property specifying the (x, y, z, w) rotation to
     * apply to the node. Default: {@link Quaternion#IDENTITY()}.
     */
    @JsProperty
    public Property<Quaternion> rotation;
    /**
     * Gets or sets the Cartesian3 Property specifying the (x, y, z) scaling to
     * apply to the node. Default: new Cartesian3(1.0, 1.0, 1.0)
     */
    @JsProperty
    public Property<Cartesian3> scale;
    /**
     * Gets or sets the Cartesian3 Property specifying the (x, y, z) translation to
     * apply to the node. Default: {@link Cartesian3#ZERO()}
     */
    @JsProperty
    public Property<Cartesian3> translation;

    public NodeTransformationProperty(NodeTransformationPropertyOptions options) {
    }
}


text:
package controllers

import models.{DestId, Destination, Vote}
import play.api.data.Form
import play.api.data.Forms._
import play.api.mvc.{Action, Controller}
import scalikejdbc._
import utils.WithSession

import scala.util.Try
import scala.concurrent.duration._

class API extends Controller {
  import API._
  def vote(destId: Long) = Action { implicit req =>
    DB localTx { implicit db =>
      WithSession.check { session =>
        val v = Vote.defaultAlias
        val ip = ipToLong(req.domain).getOrElse(req.domain.hashCode.toLong)
        val dayAgo = System.currentTimeMillis() - 1.day.toMillis
        val sessionCount = Vote.where(sqls.eq(v.sessionId, session.id.value).and.gt(v.created, dayAgo)).count()
        if(sessionCount > 0) Redirect(routes.View.index("Already voted today."))
        else {
          Vote.create(DestId(destId), session.id, ip)
          Redirect(routes.View.index())
        }
      }
    }
  }

  val form = Form("name" -> text.verifying(_.nonEmpty))
  def createDestination() = Action { implicit req =>
    DB localTx { implicit db =>
      WithSession { _ =>
        val name = form.bindFromRequest().get
        val count = Destination.where('name -> name).count()
        if(count != 0L) {
          Redirect(routes.View.index("Already exists"))
        } else {
          Destination.create(name)
          Redirect(routes.View.index())
        }
      }
    }
  }
}

object API {
  def ipToLong(ip: String): Option[Long] = Try {
    ip.split('.').map(_.toLong).reduceLeft { (orig, x) => orig * 256L + x }
  }.toOption
}