text:
package org.jenkinsci.plugins.workflow;

import com.google.common.base.Function;
import hudson.model.Executor;
import hudson.model.Queue;
import hudson.model.TaskListener;
import hudson.model.User;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import jenkins.model.Jenkins;
import jenkins.security.QueueItemAuthenticatorConfiguration;
import org.jenkinsci.plugins.scriptsecurity.scripts.ScriptApproval;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.flow.FlowExecution;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepExecutionImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractSynchronousNonBlockingStepExecution;
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.jenkinsci.plugins.workflow.support.actions.EnvironmentAction;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
import org.junit.Test;
import org.junit.runners.model.Statement;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.MockQueueItemAuthenticator;
import org.jvnet.hudson.test.TestExtension;
import org.kohsuke.stapler.DataBoundConstructor;

/**
 * Tests of workflows that involve restarting Jenkins in the middle.
 */
public class WorkflowTest extends SingleJobTestBase {

    /**
     * Restart Jenkins while workflow is executing to make sure it suspends all right
     */
    @Test public void demo() throws Exception {
        story.addStep(new Statement() {
            @Override
            public void evaluate() throws Throwable {
                p = jenkins().createProject(WorkflowJob.class, "demo");
                p.setDefinition(new CpsFlowDefinition("semaphore 'wait'"));
                startBuilding();
                SemaphoreStep.waitForStart("wait/1", b);
                assertTrue(b.isBuilding());
                liveness();
            }
        });
        story.addStep(new Statement() {
            @Override
            public void evaluate() throws Throwable {
                rebuildContext(story.j);
                assertThatWorkflowIsSuspended();
                for (int i = 0; i < 600 && !Queue.getInstance().isEmpty(); i++) {
                    Thread.sleep(100);
                }
                liveness();
                SemaphoreStep.success("wait/1", null);
                story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b));
            }
        });
    }
    private void liveness() {
        assertFalse(jenkins().toComputer().isIdle());
        Executor e = b.getOneOffExecutor();
        assertNotNull(e);
        assertEquals(e, b.getExecutor());
        assertTrue(e.isActive());
        assertFalse(e.isAlive());
    }

    /**
     * ability to invoke body needs to survive beyond Jenkins restart.
     */
    @Test public void invokeBodyLaterAfterRestart() throws Exception {
        story.addStep(new Statement() {
            @Override public void evaluate() throws Throwable {
                p = jenkins().createProject(WorkflowJob.class, "demo");
                p.setDefinition(new CpsFlowDefinition(
                    "int count=0;\n" +
                    "retry(3) {\n" +
                    "    semaphore 'wait'\n" +
                    "    if (count++ < 2) {\n" + // forcing retry
                    "        error 'died'\n" +
                    "    }\n" +
                    "}"));

                startBuilding();
                SemaphoreStep.waitForStart("wait/1", b);
                assertTrue(b.isBuilding());
            }
        });
        story.addStep(new Statement() {
            @Override public void evaluate() throws Throwable {
                rebuildContext(story.j);
                assertThatWorkflowIsSuspended();

                // resume execution and cause the retry to invoke the body again
                SemaphoreStep.success("wait/1", null);
                SemaphoreStep.success("wait/2", null);
                SemaphoreStep.success("wait/3", null);

                story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b));
                assertTrue(e.programPromise.get().closures.isEmpty());
            }
        });
    }

    @Test public void authentication() throws Exception {
        story.addStep(new Statement() {
            @Override public void evaluate() throws Throwable {
                jenkins().setSecurityRealm(story.j.createDummySecurityRealm());
                jenkins().save();
                QueueItemAuthenticatorConfiguration.get().getAuthenticators().add(new MockQueueItemAuthenticator(Collections.singletonMap("demo", User.get("someone").impersonate())));
                p = jenkins().createProject(WorkflowJob.class, "demo");
                p.setDefinition(new CpsFlowDefinition("checkAuth()"));
                ScriptApproval.get().preapproveAll();
                startBuilding();
                waitForWorkflowToSuspend();
                assertTrue(b.isBuilding());
                story.j.waitForMessage("running as someone", b);
                CheckAuth.finish(false);
                waitForWorkflowToSuspend();
                assertTrue(b.isBuilding());
                story.j.waitForMessage("still running as someone", b);
            }
        });
        story.addStep(new Statement() {
            @Override public void evaluate() throws Throwable {
                assertEquals(JenkinsRule.DummySecurityRealm.class, jenkins().getSecurityRealm().getClass());
                rebuildContext(story.j);
                assertThatWorkflowIsSuspended();
                story.j.waitForMessage("again running as someone", b);
                CheckAuth.finish(true);
                story.j.assertLogContains("finally running as someone", story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b)));
            }
        });
    }
    public static final class CheckAuth extends AbstractStepImpl {
        @DataBoundConstructor public CheckAuth() {}
        @TestExtension("authentication") public static final class DescriptorImpl extends AbstractStepDescriptorImpl {
            public DescriptorImpl() {
                super(Execution.class);
            }
            @Override public String getFunctionName() {
                return "checkAuth";
            }
            @Override
            public String getDisplayName() {
                return getFunctionName(); // TODO would be nice for this to be the default, perhaps?
            }
        }
        public static final class Execution extends AbstractStepExecutionImpl {
            @StepContextParameter transient TaskListener listener;
            @StepContextParameter transient FlowExecution flow;
            @Override public boolean start() throws Exception {
                listener.getLogger().println("running as " + Jenkins.getAuthentication().getName() + " from " + Thread.currentThread().getName());
                return false;
            }
            @Override public void stop(Throwable cause) throws Exception {}
            @Override public void onResume() {
                super.onResume();
                try {
                    listener.getLogger().println("again running as " + flow.getAuthentication().getName() + " from " + Thread.currentThread().getName());
                } catch (Exception x) {
                    getContext().onFailure(x);
                }
            }
        }
        public static void finish(final boolean terminate) {
            StepExecution.applyAll(Execution.class, new Function<Execution,Void>() {
                @Override public Void apply(Execution input) {
                    try {
                        input.listener.getLogger().println((terminate ? "finally" : "still") + " running as " + input.flow.getAuthentication().getName() + " from " + Thread.currentThread().getName());
                        if (terminate) {
                            input.getContext().onSuccess(null);
                        }
                    } catch (Exception x) {
                        input.getContext().onFailure(x);
                    }
                    return null;
                }
            });
        }
    }

    @Issue("JENKINS-30122")
    @Test public void authenticationInSynchronousStep() {
        story.addStep(new Statement() {
            @Override public void evaluate() throws Throwable {
                jenkins().setSecurityRealm(story.j.createDummySecurityRealm());
                jenkins().save();
                QueueItemAuthenticatorConfiguration.get().getAuthenticators().add(new MockQueueItemAuthenticator(Collections.singletonMap("demo", User.get("someone").impersonate())));
                p = jenkins().createProject(WorkflowJob.class, "demo");
                p.setDefinition(new CpsFlowDefinition("echo \"ran as ${auth()}\"", true));
                b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
                story.j.assertLogContains("ran as someone", b);
            }
        });
    }
    public static final class CheckAuthSync extends AbstractStepImpl {
        @DataBoundConstructor public CheckAuthSync() {}
        @TestExtension("authenticationInSynchronousStep") public static final class DescriptorImpl extends AbstractStepDescriptorImpl {
            public DescriptorImpl() {
                super(Execution.class);
            }
            @Override public String getFunctionName() {
                return "auth";
            }
            @Override public String getDisplayName() {
                return getFunctionName();
            }
        }
        public static final class Execution extends AbstractSynchronousNonBlockingStepExecution<String> {
            @Override protected String run() throws Exception {
                return Jenkins.getAuthentication().getName();
            }
        }
    }

    @Test public void env() {
        story.addStep(new Statement() {
            @Override public void evaluate() throws Throwable {
                Map<String,String> slaveEnv = new HashMap<String,String>();
                slaveEnv.put("BUILD_TAG", null);
                slaveEnv.put("PERMACHINE", "set");
                JenkinsRuleExt.createSpecialEnvSlave(story.j, "slave", null, slaveEnv);
                p = jenkins().createProject(WorkflowJob.class, "demo");
                p.setDefinition(new CpsFlowDefinition("node('slave') {\n"
                        + "  sh 'echo tag=$BUILD_TAG PERMACHINE=$PERMACHINE'\n"
                        + "  env.BUILD_TAG='custom'\n"
                        + "  sh 'echo tag2=$BUILD_TAG'\n"
                        + "  env.STUFF='more'\n"
                        + "  semaphore 'env'\n"
                        + "  env.BUILD_TAG=\"${env.BUILD_TAG}2\"\n"
                        + "  sh 'echo tag3=$BUILD_TAG stuff=$STUFF'\n"
                        + "  env.PATH=\"/opt/stuff/bin:${env.PATH}\"\n"
                        + "  sh 'echo shell PATH=$PATH'\n"
                        + "  echo \"groovy PATH=${env.PATH}\""
                        + "}", true));
                startBuilding();
                SemaphoreStep.waitForStart("env/1", b);
                assertTrue(b.isBuilding());
            }
        });
        story.addStep(new Statement() {
            @Override public void evaluate() throws Throwable {
                rebuildContext(story.j);
                assertThatWorkflowIsSuspended();
                SemaphoreStep.success("env/1", null);
                story.j.assertBuildStatusSuccess(story.j.waitForCompletion(b));
                story.j.assertLogContains("tag=jenkins-demo-1 PERMACHINE=set", b);
                story.j.assertLogContains("tag2=custom", b);
                story.j.assertLogContains("tag3=custom2 stuff=more", b);
                story.j.assertLogContains("shell PATH=/opt/stuff/bin:", b);
                story.j.assertLogContains("groovy PATH=/opt/stuff/bin:", b);
                EnvironmentAction a = b.getAction(EnvironmentAction.class);
                assertNotNull(a);
                assertEquals("custom2", a.getEnvironment().get("BUILD_TAG"));
                assertEquals("more", a.getEnvironment().get("STUFF"));
                assertNotNull(a.getEnvironment().get("PATH"));
            }
        });
    }

}


text:
//
//  SimpleObjectStore.h
//  Offline Surveys
//
//  Created by James Snee on 7/20/15.
//  Copyright (c) 2015 James Snee. All rights reserved.
//

/*
 * A super simple in-memory object store.
 * Just stores a list of OSUserResponses and can dump them to various types of files or over the network
 */

#import <Foundation/Foundation.h>

#import "OSUserResponse.h"

@interface SimpleObjectStore : NSObject

@property (strong, nonatomic) NSString *currentOutputFileURL;

+ (SimpleObjectStore *) getObjectStore;

- (void) storeObject:(OSUserResponse *)response;
- (void) outputStoreWithBlock:(void (^)(OSUserResponse *))outputBlock;
- (void) flushToDisk;
- (NSString *) getFileContents;
- (void) clearAll;

@end

redpajama_set_name: RedPajamaGithub


text:
package org.mwg.experiments.mwgrelated;

import org.mwg.*;
import org.mwg.core.scheduler.NoopScheduler;
import org.mwg.ml.MLPlugin;
import org.mwg.struct.Relation;

import java.io.File;
import java.io.PrintWriter;
import java.util.Random;

/**
 * Created by assaad on 19/05/16.
 */
public class TestWorldForkPerf {
    final static String csvdir = "/Users/assaad/work/github/data/consumption/londonpower/";

    public static void main(String[] arg) {

        final Graph graph = new GraphBuilder()
                .withPlugin(new MLPlugin())
                .withScheduler(new NoopScheduler())
//                .withOffHeapMemory()
                .withMemorySize(10_000_000)
//                .saveEvery(1_000)
                .withStorage(new RocksDBStorage(csvdir + "rocksdb2/"))
                .build();
        graph.connect(new Callback<Boolean>() {
            public void on(Boolean result) {
                Node node = graph.newNode(0, 0);
                Relation rel= (Relation) node.getOrCreate("rel",Type.RELATION);
                rel.clear();
                rel.addAll(new long[]{1, 2, 3});

                long calcTime = 0;
                long forkTime = 0;
                long calcTimeStart;
                long forkTimeStart;

                long calccumul=0;
                long forkcumul=0;
                try {
                    PrintWriter outTime = new PrintWriter(new File(csvdir + "worldtimePerf.csv"));
                    outTime.println("world,calcTime,calcCumul,forkTime,forcCumul");
                    long newworld = 0;
                    final Random random = new Random();
                    long[] ress=new long[3];

                    for (int world = 0; world < 100000; world++) {
                        calcTimeStart = System.nanoTime();
                        graph.lookup(newworld, 0, node.id(), new Callback<Node>() {
                            @Override
                            public void on(Node result) {
                                long[] res = (long[]) result.get("rel");
                                result.free();
                            }
                        });
                        calcTime = System.nanoTime() - calcTimeStart;
                        graph.save(null);

                        ress[0]=random.nextLong();
                        ress[0]=random.nextLong();
                        ress[0]=random.nextLong();

                        forkTimeStart = System.nanoTime();
                        newworld = graph.fork(world);
                        graph.lookup(newworld, 0, node.id(), new Callback<Node>() {
                            @Override
                            public void on(Node result) {
                                Relation rel= (Relation) result.getOrCreate("rel",Type.RELATION);
                                rel.clear();
                                rel.addAll(ress);
                                result.free();
                            }
                        });
                        forkTime = System.nanoTime() - forkTimeStart;

                        calccumul+=calcTime;
                        forkcumul+=forkTime;

                        outTime.println(world+","+calcTime+","+calccumul+","+forkTime+","+forkcumul);
                        outTime.flush();
                        if(world%1000==0){
                            System.out.println(world+","+calcTime+","+calccumul+","+forkTime+","+forkcumul);
                        }
                        //System.out.println("Available after world " + world + ": " + graph.space().available());
                    }

                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });

    }
}


text:
class AddMergeRequestPartialIndexToEvents < ActiveRecord::Migration[6.0]
  include Gitlab::Database::MigrationHelpers

  DOWNTIME = false

  disable_ddl_transaction!

  INDEX_NAME = 'index_events_on_author_id_and_created_at_merge_requests'

  def up
    add_concurrent_index(
      :events,
      [:author_id, :created_at],
      name: INDEX_NAME,
      where: "(target_type = 'MergeRequest')"
    )
  end

  def down
    remove_concurrent_index :events, INDEX_NAME
  end
end


text:
using namespace tests;

UNIT_TEST(FBuilder_ManyTypes)
{
  classificator::Load();

  FeatureBuilder1 fb1;
  FeatureParams params;

  char const * arr1[][1] = {
    { "building" },
  };
  AddTypes(params, arr1);

  char const * arr2[][2] = {
    { "place", "country" },
    { "place", "state" },
    /// @todo Can't realize is it deprecated or we forgot to add clear styles for it.
    //{ "place", "county" },
    { "place", "region" },
    { "place", "city" },
    { "place", "town" },
  };
  AddTypes(params, arr2);

  params.FinishAddingTypes();
  params.AddHouseNumber("75");
  params.AddHouseName("Best House");
  params.AddName("default", "Name");

  fb1.SetParams(params);
  fb1.SetCenter(m2::PointD(0, 0));

  TEST(fb1.RemoveInvalidTypes(), ());
  TEST(fb1.CheckValid(), ());

  FeatureBuilder1::Buffer buffer;
  TEST(fb1.PreSerialize(), ());
  fb1.Serialize(buffer);

  FeatureBuilder1 fb2;
  fb2.Deserialize(buffer);

  TEST(fb2.CheckValid(), ());
  TEST_EQUAL(fb1, fb2, ());
  TEST_EQUAL(fb2.GetTypesCount(), 6, ());
}

UNIT_TEST(FBuilder_LineTypes)
{
  FeatureBuilder1 fb1;
  FeatureParams params;

  char const * arr2[][2] = {
    { "railway", "rail" },
    { "highway", "motorway" },
    { "hwtag", "oneway" },
    { "psurface", "paved_good" },
    { "junction", "roundabout" },
  };

  AddTypes(params, arr2);
  params.FinishAddingTypes();
  fb1.SetParams(params);

  fb1.AddPoint(m2::PointD(0, 0));
  fb1.AddPoint(m2::PointD(1, 1));
  fb1.SetLinear();

  TEST(fb1.RemoveInvalidTypes(), ());
  TEST(fb1.CheckValid(), ());

  FeatureBuilder1::Buffer buffer;
  TEST(fb1.PreSerialize(), ());
  fb1.Serialize(buffer);

  FeatureBuilder1 fb2;
  fb2.Deserialize(buffer);

  TEST(fb2.CheckValid(), ());
  TEST_EQUAL(fb1, fb2, ());
  TEST_EQUAL(fb2.GetTypesCount(), 5, ());
}

UNIT_TEST(FBuilder_Waterfall)
{
  classificator::Load();

  FeatureBuilder1 fb1;
  FeatureParams params;

  char const * arr[][2] = {{"waterway", "waterfall"}};
  AddTypes(params, arr);
  TEST(params.FinishAddingTypes(), ());

  fb1.SetParams(params);
  fb1.SetCenter(m2::PointD(1, 1));

  TEST(fb1.RemoveInvalidTypes(), ());
  TEST(fb1.CheckValid(), ());

  FeatureBuilder1::Buffer buffer;
  TEST(fb1.PreSerialize(), ());
  fb1.Serialize(buffer);

  FeatureBuilder1 fb2;
  fb2.Deserialize(buffer);

  TEST(fb2.CheckValid(), ());
  TEST_EQUAL(fb1, fb2, ());
  TEST_EQUAL(fb2.GetTypesCount(), 1, ());
}

UNIT_TEST(FBbuilder_GetMostGeneralOsmId)
{
  FeatureBuilder1 fb;

  fb.AddOsmId(base::MakeOsmNode(1));
  TEST_EQUAL(fb.GetMostGenericOsmId(), base::MakeOsmNode(1), ());

  fb.AddOsmId(base::MakeOsmNode(2));
  fb.AddOsmId(base::MakeOsmWay(1));
  TEST_EQUAL(fb.GetMostGenericOsmId(), base::MakeOsmWay(1), ());

  fb.AddOsmId(base::MakeOsmNode(3));
  fb.AddOsmId(base::MakeOsmWay(2));
  fb.AddOsmId(base::MakeOsmRelation(1));
  TEST_EQUAL(fb.GetMostGenericOsmId(), base::MakeOsmRelation(1), ());
}

UNIT_TEST(FVisibility_RemoveNoDrawableTypes)
{
  classificator::Load();
  Classificator const & c = classif();

  {
    vector<uint32_t> types;
    types.push_back(c.GetTypeByPath({ "building" }));
    types.push_back(c.GetTypeByPath({ "amenity", "theatre" }));

    TEST(feature::RemoveNoDrawableTypes(types, feature::GEOM_AREA), ());
    TEST_EQUAL(types.size(), 2, ());
  }

  {
    vector<uint32_t> types;
    types.push_back(c.GetTypeByPath({ "highway", "primary" }));
    types.push_back(c.GetTypeByPath({ "building" }));

    TEST(feature::RemoveNoDrawableTypes(types, feature::GEOM_AREA, true), ());
    TEST_EQUAL(types.size(), 1, ());
    TEST_EQUAL(types[0], c.GetTypeByPath({ "building" }), ());
  }
}

UNIT_TEST(FBuilder_RemoveUselessNames)
{
  classificator::Load();

  FeatureParams params;

  char const * arr3[][3] = { { "boundary", "administrative", "2" } };
  AddTypes(params, arr3);
  char const * arr2[][2] = { { "barrier", "fence" } };
  AddTypes(params, arr2);
  params.FinishAddingTypes();

  params.AddName("default", "Name");
  params.AddName("ru", "Имя");

  FeatureBuilder1 fb1;
  fb1.SetParams(params);

  fb1.AddPoint(m2::PointD(0, 0));
  fb1.AddPoint(m2::PointD(1, 1));
  fb1.SetLinear();

  TEST(!fb1.GetName(0).empty(), ());
  TEST(!fb1.GetName(8).empty(), ());

  fb1.RemoveUselessNames();

  TEST(fb1.GetName(0).empty(), ());
  TEST(fb1.GetName(8).empty(), ());

  TEST(fb1.CheckValid(), ());
}

UNIT_TEST(FeatureParams_Parsing)
{
  classificator::Load();

  {
    FeatureParams params;
    params.AddStreet("Embarcadero\nstreet");
    TEST_EQUAL(params.GetStreet(), "Embarcadero street", ());
  }

  {
    FeatureParams params;
    params.AddAddress("165 \t\t Dolliver Street");
    TEST_EQUAL(params.GetStreet(), "Dolliver Street", ());
  }

  {
    FeatureParams params;

    params.MakeZero();
    TEST(params.AddHouseNumber("123"), ());
    TEST_EQUAL(params.house.Get(), "123", ());

    params.MakeZero();
    TEST(params.AddHouseNumber("0000123"), ());
    TEST_EQUAL(params.house.Get(), "123", ());

    params.MakeZero();
    TEST(params.AddHouseNumber("000000"), ());
    TEST_EQUAL(params.house.Get(), "0", ());
  }
}


text:
namespace NServiceKit.DataAccess.Criteria
{
	/// <summary>
	///
	/// </summary>
	public class PagingCriteria : IPagingCriteria
	{
		/// <summary>
		///
		/// </summary>
		public uint ResultOffset { get; private set; }
		/// <summary>
		///
		/// </summary>
		public uint ResultLimit { get; private set; }

		/// <summary>
		///
		/// </summary>
		/// <param name="resultOffset"></param>
		/// <param name="resultLimit"></param>
		public PagingCriteria(uint resultOffset, uint resultLimit)
		{
			this.ResultOffset = resultOffset;
			this.ResultLimit = resultLimit;
		}
	}
}


text:
require 'context-io/error'

module ContextIO
  # Raised when Context.IO returns a 4xx HTTP status code
  #
  # @api public
  class Error::ClientError < ContextIO::Error
  end
end


text:
import {Expm1} from '../kernel_names';
import {GradConfig} from '../kernel_registry';
import {exp} from '../ops/exp';
import {mul} from '../ops/mul';
import {Tensor} from '../tensor';

export const expm1GradConfig: GradConfig = {
  kernelName: Expm1,
  inputsToSave: ['x'],
  gradFunc: (dy: Tensor, saved: Tensor[]) => {
    const [x] = saved;
    return {x: () => mul(dy, exp(x))};
  }
};


text:
namespace novus
{

VoxelVolumeRenderTarget::VoxelVolumeRenderTarget()
: mpVoxelTexture(NULL),
mpNormalTexture(NULL),
mpEmissiveTexture(NULL),
mpRasterizerState(NULL),
mResolution(0),
mBounds(10.0f)
{
	setBounds(10.0f, 10.0f, 10.0f);
}

VoxelVolumeRenderTarget::~VoxelVolumeRenderTarget()
{
	ReleaseCOM(mpRasterizerState);
	NE_DELETE(mpVoxelTexture);
	NE_DELETE(mpNormalTexture);
	NE_DELETE(mpEmissiveTexture);
}

void VoxelVolumeRenderTarget::Init(int resolution)
{
	NE_DELETE(mpVoxelTexture);
	NE_DELETE(mpNormalTexture);
	NE_DELETE(mpEmissiveTexture);

	mResolution = resolution;

	D3DRenderer* renderer = EngineStatics::getRenderer();

	mpVoxelTexture = NE_NEW Texture3D();
	mpVoxelTexture->Init(renderer,
						 resolution,
						 resolution,
						 resolution,
						 DXGI_FORMAT_R8G8B8A8_TYPELESS,
						 //Math::MipMapCount(resolution, resolution),
						 1,
						 D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET | D3D11_BIND_UNORDERED_ACCESS,
						 0,
						 DXGI_FORMAT_R8G8B8A8_UNORM,
						 DXGI_FORMAT_R8G8B8A8_UNORM,
						 DXGI_FORMAT_R32_UINT);

	mpVoxelTexture->setDebugName("Diffuse Voxel Volume");

	mpNormalTexture = NE_NEW Texture3D();
	mpNormalTexture->Init(renderer,
						  resolution,
						  resolution,
						  resolution,
						  DXGI_FORMAT_R8G8B8A8_TYPELESS,
						  //Math::MipMapCount(resolution, resolution),
						  1,
						  D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET | D3D11_BIND_UNORDERED_ACCESS,
						  0,
						  DXGI_FORMAT_R8G8B8A8_UNORM,
						  DXGI_FORMAT_R8G8B8A8_UNORM,
						  DXGI_FORMAT_R32_UINT);

	mpNormalTexture->setDebugName("Normal Voxel Volume");

	mpEmissiveTexture = NE_NEW Texture3D();
	mpEmissiveTexture->Init(renderer,
							resolution,
							resolution,
							resolution,
							DXGI_FORMAT_R8G8B8A8_TYPELESS,
							//Math::MipMapCount(resolution, resolution),
							1,
							D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET | D3D11_BIND_UNORDERED_ACCESS,
							0,
							DXGI_FORMAT_R8G8B8A8_UNORM,
							DXGI_FORMAT_R8G8B8A8_UNORM,
							DXGI_FORMAT_R32_UINT);

	mpEmissiveTexture->setDebugName("Emissive Voxel Volume");

	if (mpRasterizerState == NULL)
	{
		D3D11_RASTERIZER_DESC rasterDesc;
		ZeroMemory(&rasterDesc, sizeof(D3D11_RASTERIZER_DESC));

		rasterDesc.FillMode = D3D11_FILL_SOLID;
		rasterDesc.CullMode = D3D11_CULL_NONE;
		rasterDesc.FrontCounterClockwise = true;

		HR(EngineStatics::getRenderer()->device()->CreateRasterizerState(&rasterDesc, &mpRasterizerState));
	}
}

void VoxelVolumeRenderTarget::setBounds(float width, float height, float depth)
{
	//mProjection = Matrix4::OrthographicOffCenter(width / 2.0f, -width / 2.0f, -height / 2.0f, height / 2.0f, -depth / 2.0f, depth / 2.0f);

	mBounds = Vector3(width, height, depth);

	BuildWorldToVolumeProj();

	mProjection = Matrix4::Orthographic(2.0f, 2.0f, 1.0f, -1.0f);
	//mProjection = mWorldToVolume * Matrix4::OrthographicOffCenter(1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
}

void VoxelVolumeRenderTarget::BuildWorldToVolumeProj()
{
	mWorldToVolume[0][0] = 2.0f / mBounds.x;
	mWorldToVolume[0][1] = 0.0f;
	mWorldToVolume[0][2] = 0.0f;
	mWorldToVolume[0][3] = 0.0f;

	mWorldToVolume[1][0] = 0.0f;
	mWorldToVolume[1][1] = 2.0f / mBounds.y;
	mWorldToVolume[1][2] = 0.0f;
	mWorldToVolume[1][3] = 0.0f;

	mWorldToVolume[2][0] = 0.0f;
	mWorldToVolume[2][1] = 0.0f;
	mWorldToVolume[2][2] = 2.0f / mBounds.z;
	mWorldToVolume[2][3] = 0.0f;

	mWorldToVolume[3] = Vector4(mPosition, 1.0f);
}

void VoxelVolumeRenderTarget::setPosition(const Vector3& position)
{
	mPosition = position;
}

void VoxelVolumeRenderTarget::BindTarget(D3DRenderer* renderer)
{
	CBPerFrame perFrame;

	Vector4 clearColor = Vector4(0.0f, 0.0f, 0.0f, 0.0f);

	renderer->context()->ClearRenderTargetView(mpVoxelTexture->getRenderTargetView(), &clearColor.x);
	renderer->context()->ClearRenderTargetView(mpNormalTexture->getRenderTargetView(), &clearColor.x);
	renderer->context()->ClearRenderTargetView(mpEmissiveTexture->getRenderTargetView(), &clearColor.x);

	perFrame.ScreenResolution = Vector2_t<unsigned int>(static_cast<unsigned int>(mResolution), static_cast<unsigned int>(mResolution));
	perFrame.ClipNearFar = Vector2(0.0f, 1.0f);
	perFrame.Projection = mProjection;
	perFrame.ProjectionInv = Matrix4::Inverse(perFrame.Projection);
	perFrame.View = mWorldToVolume;
	perFrame.ViewInv = Matrix4::Inverse(perFrame.View);
	perFrame.ViewProj = perFrame.View * perFrame.Projection;
	perFrame.ViewProjInv = Matrix4::Inverse(perFrame.ViewProj);

	renderer->setPerFrameBuffer(perFrame);
	renderer->UnbindTextureResources();

	ID3D11UnorderedAccessView* voxelUAVs[] =
	{
		mpVoxelTexture->getUnorderedAccessView(),
		mpNormalTexture->getUnorderedAccessView(),
		mpEmissiveTexture->getUnorderedAccessView()
	};

	renderer->context()->OMSetRenderTargetsAndUnorderedAccessViews(0, NULL, NULL, 0, 3, voxelUAVs, 0);
	renderer->setViewport(0, 0, mResolution, mResolution);
	renderer->context()->RSSetState(mpRasterizerState);
}

void VoxelVolumeRenderTarget::UnbindTarget(D3DRenderer* renderer)
{
	ID3D11UnorderedAccessView* voxelUAVs[] =
	{
		NULL,
		NULL,
		NULL
	};

	renderer->context()->OMSetRenderTargetsAndUnorderedAccessViews(0, NULL, NULL, 0, 3, voxelUAVs, 0);//This is probably not necessary
	renderer->ResetRasterizerState();
}

RenderPass::Type VoxelVolumeRenderTarget::GetRenderPass() const
{
	return RenderPass::GraphicsPrepass;
}

bool VoxelVolumeRenderTarget::ShouldRenderActor(const Actor* actor) const
{
	return true;
}

}


text:
package io.fabric8.arquillian.kubernetes.enricher;

import io.fabric8.arquillian.kubernetes.Session;
import io.fabric8.kubernetes.api.model.ReplicationControllerList;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.jboss.arquillian.core.api.Instance;
import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.arquillian.test.spi.enricher.resource.ResourceProvider;

import java.lang.annotation.Annotation;
import java.util.Map;

import static io.fabric8.arquillian.kubernetes.enricher.EnricherUtils.getLabels;

/**
 * A {@link org.jboss.arquillian.test.spi.enricher.resource.ResourceProvider} for {@link io.fabric8.kubernetes.api.model.ReplicationControllerList}.
 * It refers to replication controllers that have been created during the current session.
 */
public class ReplicationControllerListResourceProvider implements ResourceProvider {

    @Inject
    private Instance<KubernetesClient> clientInstance;

    @Inject
    private Instance<Session> sessionInstance;

    @Override
    public boolean canProvide(Class<?> type) {
        return ReplicationControllerList.class.isAssignableFrom(type);
    }

    @Override
    public Object lookup(ArquillianResource resource, Annotation... qualifiers) {
        KubernetesClient client = this.clientInstance.get();
        Session session = sessionInstance.get();

        Map<String, String> labels = getLabels(qualifiers);
        if( labels.isEmpty() ) {
            return client.replicationControllers().inNamespace(session.getNamespace()).list();
        } else {
            return client.replicationControllers().inNamespace(session.getNamespace()).withLabels(labels).list();
        }

    }
}


text:
#include "sdk_common.h"
#if NRF_MODULE_ENABLED(COMP)
#include "nrf_drv_comp.h"

#include "nrf_assert.h"
#include "nrf_error.h"
#include "nrf_soc.h"
#include "nrf_drv_common.h"
#include "app_util_platform.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#define NRF_LOG_MODULE_NAME "COMP"

#if COMP_CONFIG_LOG_ENABLED
#define NRF_LOG_LEVEL       COMP_CONFIG_LOG_LEVEL
#define NRF_LOG_INFO_COLOR  COMP_CONFIG_INFO_COLOR
#define NRF_LOG_DEBUG_COLOR COMP_CONFIG_DEBUG_COLOR
#define EVT_TO_STR(event)   (event == NRF_COMP_EVENT_READY ? "NRF_COMP_EVENT_READY" :                   \
                            (event == NRF_COMP_EVENT_DOWN ? "NRF_COMP_EVENT_DOWN" :                     \
                            (event == NRF_COMP_EVENT_UP ? "NRF_COMP_EVENT_UP" :                         \
                            (event == NRF_COMP_EVENT_CROSS ? "NRF_COMP_EVENT_CROSS" : "UNKNOWN ERROR"))))
#else //COMP_CONFIG_LOG_ENABLED
#define EVT_TO_STR(event)   ""
#define NRF_LOG_LEVEL       0
#endif //COMP_CONFIG_LOG_ENABLED
#include "nrf_log.h"
#include "nrf_log_ctrl.h"

static comp_events_handler_t     m_comp_events_handler = NULL;
static nrf_drv_state_t           m_state = NRF_DRV_STATE_UNINITIALIZED;

static const nrf_drv_comp_config_t m_default_config = NRF_DRV_COMP_DEFAULT_CONFIG(NRF_COMP_INPUT_0);

static void comp_execute_handler(nrf_comp_event_t event, uint32_t event_mask)
{
    if ( nrf_comp_event_check(event) && nrf_comp_int_enable_check(event_mask) )
    {
        nrf_comp_event_clear(event);
        NRF_LOG_DEBUG("Event: %s.\r\n", (uint32_t)EVT_TO_STR(event));

        m_comp_events_handler(event);
    }
}

#if NRF_MODULE_ENABLED(PERIPHERAL_RESOURCE_SHARING)
    #define IRQ_HANDLER_NAME   irq_handler_for_comp
    #define IRQ_HANDLER        static void IRQ_HANDLER_NAME(void)

    IRQ_HANDLER;
#else
    #define IRQ_HANDLER void COMP_LPCOMP_IRQHandler(void)
#endif // NRF_MODULE_ENABLED(PERIPHERAL_RESOURCE_SHARING)

IRQ_HANDLER
{
    comp_execute_handler(NRF_COMP_EVENT_READY, COMP_INTENSET_READY_Msk);
    comp_execute_handler(NRF_COMP_EVENT_DOWN, COMP_INTENSET_DOWN_Msk);
    comp_execute_handler(NRF_COMP_EVENT_UP, COMP_INTENSET_UP_Msk);
    comp_execute_handler(NRF_COMP_EVENT_CROSS, COMP_INTENSET_CROSS_Msk);
}

ret_code_t nrf_drv_comp_init(const nrf_drv_comp_config_t * p_config,
                             comp_events_handler_t   event_handler)
{
    ret_code_t err_code;

    if (m_state != NRF_DRV_STATE_UNINITIALIZED)
    { // COMP driver is already initialized
        err_code = NRF_ERROR_INVALID_STATE;
        NRF_LOG_WARNING("Function: %s, error code: %s.\r\n", (uint32_t)__func__, (uint32_t)NRF_LOG_ERROR_STRING_GET(err_code));
        return err_code;
    }

    if (p_config == NULL)
    {
        p_config = &m_default_config;
    }

#if NRF_MODULE_ENABLED(PERIPHERAL_RESOURCE_SHARING)
    if (nrf_drv_common_per_res_acquire(NRF_COMP, IRQ_HANDLER_NAME) != NRF_SUCCESS)
    {
        err_code = NRF_ERROR_BUSY;
        NRF_LOG_WARNING("Function: %s, error code: %s.\r\n", (uint32_t)__func__, (uint32_t)NRF_LOG_ERROR_STRING_GET(err_code));
        return err_code;
    }
#endif

    nrf_comp_task_trigger(NRF_COMP_TASK_STOP);
    nrf_comp_enable();

    // Clear events to be sure there are no leftovers.
    nrf_comp_event_clear(NRF_COMP_EVENT_READY);
    nrf_comp_event_clear(NRF_COMP_EVENT_DOWN);
    nrf_comp_event_clear(NRF_COMP_EVENT_UP);
    nrf_comp_event_clear(NRF_COMP_EVENT_CROSS);

    nrf_comp_ref_set(p_config->reference);

    //If external source is chosen, write to appropriate register.
    if (p_config->reference == COMP_REFSEL_REFSEL_ARef)
    {
        nrf_comp_ext_ref_set(p_config->ext_ref);
    }

    nrf_comp_th_set(p_config->threshold);
    nrf_comp_main_mode_set(p_config->main_mode);
    nrf_comp_speed_mode_set(p_config->speed_mode);
    nrf_comp_hysteresis_set(p_config->hyst);
    nrf_comp_isource_set(p_config->isource);
    nrf_comp_shorts_disable(NRF_DRV_COMP_SHORT_STOP_AFTER_CROSS_EVT | NRF_DRV_COMP_SHORT_STOP_AFTER_UP_EVT |
                            NRF_DRV_COMP_SHORT_STOP_AFTER_DOWN_EVT);
    nrf_comp_int_disable(COMP_INTENCLR_CROSS_Msk | COMP_INTENCLR_UP_Msk |
                         COMP_INTENCLR_DOWN_Msk | COMP_INTENCLR_READY_Msk);

    if (event_handler)
    {
        m_comp_events_handler = event_handler;
    }
    else
    {
        err_code = NRF_ERROR_INVALID_PARAM;
        NRF_LOG_WARNING("Function: %s, error code: %s.\r\n", (uint32_t)__func__, (uint32_t)NRF_LOG_ERROR_STRING_GET(err_code));
        return err_code;
    }

    nrf_comp_input_select(p_config->input);

    nrf_drv_common_irq_enable(COMP_LPCOMP_IRQn, p_config->interrupt_priority);

    m_state = NRF_DRV_STATE_INITIALIZED;

    err_code = NRF_SUCCESS;
    NRF_LOG_INFO("Function: %s, error code: %s.\r\n", (uint32_t)__func__, (uint32_t)NRF_LOG_ERROR_STRING_GET(err_code));
    return err_code;
}

void nrf_drv_comp_uninit(void)
{
    ASSERT(m_state != NRF_DRV_STATE_UNINITIALIZED);
    nrf_drv_common_irq_disable(COMP_LPCOMP_IRQn);
    nrf_comp_disable();
#if NRF_MODULE_ENABLED(PERIPHERAL_RESOURCE_SHARING)
    nrf_drv_common_per_res_release(NRF_COMP);
#endif
    m_state = NRF_DRV_STATE_UNINITIALIZED;
    m_comp_events_handler = NULL;
    NRF_LOG_INFO("Uninitialized.\r\n");
}

void nrf_drv_comp_pin_select(nrf_comp_input_t psel)
{
    bool comp_enable_state = nrf_comp_enable_check();
    nrf_comp_task_trigger(NRF_COMP_TASK_STOP);
    if (m_state == NRF_DRV_STATE_POWERED_ON)
    {
        m_state = NRF_DRV_STATE_INITIALIZED;
    }
    nrf_comp_disable();
    nrf_comp_input_select(psel);
    if (comp_enable_state == true)
    {
        nrf_comp_enable();
    }
}

void nrf_drv_comp_start(uint32_t comp_int_mask, uint32_t comp_shorts_mask)
{
    ASSERT(m_state == NRF_DRV_STATE_INITIALIZED);
    nrf_comp_int_enable(comp_int_mask);
    nrf_comp_shorts_enable(comp_shorts_mask);
    nrf_comp_task_trigger(NRF_COMP_TASK_START);
    m_state = NRF_DRV_STATE_POWERED_ON;
    NRF_LOG_INFO("Enabled.\r\n");
}

void nrf_drv_comp_stop(void)
{
    ASSERT(m_state == NRF_DRV_STATE_POWERED_ON);
    nrf_comp_shorts_disable(UINT32_MAX);
    nrf_comp_int_disable(UINT32_MAX);
    nrf_comp_task_trigger(NRF_COMP_TASK_STOP);
    m_state = NRF_DRV_STATE_INITIALIZED;
    NRF_LOG_INFO("Disabled.\r\n");
}

uint32_t nrf_drv_comp_sample()
{
    ASSERT(m_state == NRF_DRV_STATE_POWERED_ON);
    nrf_comp_task_trigger(NRF_COMP_TASK_SAMPLE);
    return nrf_comp_result_get();
}
#endif //NRF_MODULE_ENABLED(COMP)


text:
require "cteng/cursor"
require "cteng/buffer"
require "cteng/window"

class State
  attr_accessor :mode, :cursor, :windows, :buffers

  def initialize
    @mode = :default
    @cursor = Cursor.new

    buffer = Buffer.new

    @buffers = [buffer]
    @windows = []
  end

  def window
    windows[cursor.window_index]
  end

  def buffer
    window.buffer
  end

  def create_window(width, height, x = 0, y = 0)
    buffer = Buffer.new
    buffers << buffer
    windows << Window.new(buffer, width, height, x, y)
  end
end


text:
/*!
 * Angular Material Design
 * https://github.com/angular/material
 * @license MIT
 * v0.9.0-master-9110d1e
 */
/* mixin definition ; sets LTR and RTL within the same style call */
md-divider.md-THEME_NAME-theme {
  border-top-color: '{{foreground-4}}'; }


text:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.pieShare.pieShareApp.model.entities;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import org.pieShare.pieShareApp.model.entities.api.IBaseEntity;

/**
 *
 * @author Richard
 */
@MappedSuperclass
public class BaseEntity implements IBaseEntity {

	@Id
	@GeneratedValue
	private long id;

	public long getId() {
		return id;
	}
}


text:
#include "storage/format/orc/orc-format.h"

#include <memory>
#include <utility>

#include "dbcommon/log/logger.h"

namespace storage {

void ORCFormat::beginInsert(const std::string &targetName,
                            const dbcommon::TupleDesc &tupleDesc) {
  assert(!targetName.empty());
  assert(params != nullptr);

  writer.reset(new ORCFormatWriter(
      this->fsManager, const_cast<dbcommon::TupleDesc *>(&tupleDesc),
      targetName.c_str(), this->blockAlignSize, params));
  writer->beginWrite();
}

void ORCFormat::doInsert(std::unique_ptr<dbcommon::TupleBatch> tb) {
  writer->write(tb.get());
}

void ORCFormat::endInsert() { writer->endWrite(); }

void ORCFormat::beginUpdate(const std::string &targetName,
                            const dbcommon::TupleDesc &td) {
  LOG_ERROR(ERRCODE_FEATURE_NOT_SUPPORTED, "UPDATE is not implemented yet");
}

void ORCFormat::doUpdate(std::unique_ptr<dbcommon::TupleBatch> tb) {
  LOG_ERROR(ERRCODE_FEATURE_NOT_SUPPORTED, "UPDATE is not implemented yet");
}

void ORCFormat::endUpdate() {
  LOG_ERROR(ERRCODE_FEATURE_NOT_SUPPORTED, "UPDATE is not implemented yet");
}

void ORCFormat::beginDelete(const std::string &targetName,
                            const dbcommon::TupleDesc &td) {
  LOG_ERROR(ERRCODE_FEATURE_NOT_SUPPORTED, "DELETE is not implemented yet");
}

void ORCFormat::doDelete(std::unique_ptr<dbcommon::TupleBatch> tb) {
  LOG_ERROR(ERRCODE_FEATURE_NOT_SUPPORTED, "DELETE is not implemented yet");
}

void ORCFormat::endDelete() {
  LOG_ERROR(ERRCODE_FEATURE_NOT_SUPPORTED, "DELETE is not implemented yet");
}

void ORCFormat::beginScan(const univplan::UnivPlanScanFileSplitListList *splits,
                          const dbcommon::TupleDesc *tupleDesc,
                          const std::vector<bool> *projectionCols,
                          const univplan::UnivPlanExprPolyList *filterExprs,
                          const FormatContext *formatContext,
                          bool readStatsOnly) {
  this->splits = splits;
  if (this->splits != nullptr) {
    assert(tupleDesc != nullptr);
    assert(params != nullptr);
    std::string tableOptionStr = params->get("table.options", "");
    assert(!tableOptionStr.empty());
    Json::Reader jreader;
    Json::Value root;
    if (!jreader.parse(tableOptionStr, root))
      LOG_ERROR(ERRCODE_INTERNAL_ERROR, "jsoncpp failed to parse \'%s\'",
                tableOptionStr.c_str());

    reader.reset(new ORCFormatReader());

    assert(fsManager != nullptr);

    reader->beginRead(fsManager, splits,
                      const_cast<std::vector<bool> *>(projectionCols),
                      this->nTuplesPerBatch,
                      const_cast<univplan::UnivPlanExprPolyList *>(filterExprs),
                      tupleDesc, readStatsOnly);
  }
}

dbcommon::TupleBatch::uptr ORCFormat::next() {
  if (splits != nullptr) {
    dbcommon::TupleBatch::uptr result = reader->read();
    assert(!result || result->isValid());
    return std::move(result);
  }

  return dbcommon::TupleBatch::uptr();
}

void ORCFormat::endScan() {
  if (splits != nullptr) {
    reader->endRead();
  }
}

void ORCFormat::reScan() {
  if (this->splits != nullptr) {
    assert(reader != nullptr);
    reader->reset();
  }
}

void ORCFormat::stopScan() {
  LOG_ERROR(ERRCODE_FEATURE_NOT_SUPPORTED, "stopScan is not implemented yet");
}

}  // namespace storage


text:
namespace Microsoft.Azure.Management.MySQL.FlexibleServers
{
    using Microsoft.Rest;
    using Microsoft.Rest.Azure;
    using Models;
    using System.Threading;
    using System.Threading.Tasks;

    /// <summary>
    /// Extension methods for CheckNameAvailabilityOperations.
    /// </summary>
    public static partial class CheckNameAvailabilityOperationsExtensions
    {
            /// <summary>
            /// Check the availability of name for server
            /// </summary>
            /// <param name='operations'>
            /// The operations group for this extension method.
            /// </param>
            /// <param name='nameAvailabilityRequest'>
            /// The required parameters for checking if server name is available.
            /// </param>
            public static NameAvailability Execute(this ICheckNameAvailabilityOperations operations, NameAvailabilityRequest nameAvailabilityRequest)
            {
                return operations.ExecuteAsync(nameAvailabilityRequest).GetAwaiter().GetResult();
            }

            /// <summary>
            /// Check the availability of name for server
            /// </summary>
            /// <param name='operations'>
            /// The operations group for this extension method.
            /// </param>
            /// <param name='nameAvailabilityRequest'>
            /// The required parameters for checking if server name is available.
            /// </param>
            /// <param name='cancellationToken'>
            /// The cancellation token.
            /// </param>
            public static async Task<NameAvailability> ExecuteAsync(this ICheckNameAvailabilityOperations operations, NameAvailabilityRequest nameAvailabilityRequest, CancellationToken cancellationToken = default(CancellationToken))
            {
                using (var _result = await operations.ExecuteWithHttpMessagesAsync(nameAvailabilityRequest, null, cancellationToken).ConfigureAwait(false))
                {
                    return _result.Body;
                }
            }

    }
}


text:
"""
===============================================================================

    Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).

    This file is part of cardinal_pythonlib.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        https://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

===============================================================================
"""

import re
from typing import Any, Dict, Iterable, List
import unicodedata

# =============================================================================
# Finding
# =============================================================================

def find_nth(s: str, x: str, n: int = 0, overlap: bool = False) -> int:
    r"""
    Finds the position of *n*\ th occurrence of ``x`` in ``s``, or ``-1`` if
    there isn't one.

    - The ``n`` parameter is zero-based (i.e. 0 for the first, 1 for the
      second...).
    - If ``overlap`` is true, allows fragments to overlap. If not, they must be
      distinct.

    As per
    https://stackoverflow.com/questions/1883980/find-the-nth-occurrence-of-substring-in-a-string
    """  # noqa
    length_of_fragment = 1 if overlap else len(x)
    i = -length_of_fragment
    for _ in range(n + 1):
        i = s.find(x, i + length_of_fragment)
        if i < 0:
            break
    return i

# =============================================================================
# Splitting
# =============================================================================

def split_string(x: str, n: int) -> List[str]:
    """
    Split string into chunks of length n
    """
    # https://stackoverflow.com/questions/9475241/split-string-every-nth-character  # noqa
    return [x[i : i + n] for i in range(0, len(x), n)]

# =============================================================================
# Replacement
# =============================================================================

def multiple_replace(text: str, rep: Dict[str, str]) -> str:
    """
    Returns a version of ``text`` in which the keys of ``rep`` (a dict) have
    been replaced by their values.

    As per
    https://stackoverflow.com/questions/6116978/python-replace-multiple-strings.
    """
    rep = dict((re.escape(k), v) for k, v in rep.items())
    pattern = re.compile("|".join(rep.keys()))
    return pattern.sub(lambda m: rep[re.escape(m.group(0))], text)

def replace_in_list(
    stringlist: Iterable[str], replacedict: Dict[str, str]
) -> List[str]:
    """
    Returns a list produced by applying :func:`multiple_replace` to every
    string in ``stringlist``.

    Args:
        stringlist: list of source strings
        replacedict: dictionary mapping "original" to "replacement" strings

    Returns:
        list of final strings

    """
    newlist = []
    for fromstring in stringlist:
        newlist.append(multiple_replace(fromstring, replacedict))
    return newlist

# =============================================================================
# Mangling to ASCII
# =============================================================================

def mangle_unicode_to_ascii(s: Any) -> str:
    """
    Mangle unicode to ASCII, losing accents etc. in the process.
    """
    # https://stackoverflow.com/questions/1207457
    if s is None:
        return ""
    if not isinstance(s, str):
        s = str(s)
    return (
        unicodedata.normalize("NFKD", s)
        .encode("ascii", "ignore")  # gets rid of accents
        .decode("ascii")  # back to a string
    )

# =============================================================================
# Making strings and string lists
# =============================================================================

def strnum(prefix: str, num: int, suffix: str = "") -> str:
    """
    Makes a string of the format ``<prefix><number><suffix>``.
    """
    return f"{prefix}{num}{suffix}"

def strnumlist(prefix: str, numbers: List[int], suffix: str = "") -> List[str]:
    """
    Makes a string of the format ``<prefix><number><suffix>`` for every number
    in ``numbers``, and returns them as a list.
    """
    return [f"{prefix}{num}{suffix}" for num in numbers]

def strseq(prefix: str, first: int, last: int, suffix: str = "") -> List[str]:
    """
    Makes a string of the format ``<prefix><number><suffix>`` for every number
    from ``first`` to ``last`` inclusive, and returns them as a list.
    """
    return [strnum(prefix, n, suffix) for n in range(first, last + 1)]


text:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <errno.h>
#include <pthread.h>
#include <sys/time.h>

#include <zt.h>
#include <libzt/zt_threads.h>

zt_threads_mutex *
zt_threads_pthreads_lock_alloc(int locktype UNUSED) {
    pthread_mutex_t *lock;

    if ((lock = calloc(sizeof(pthread_mutex_t), 1)) == NULL) {
        return NULL;
    }

    if (pthread_mutex_init(lock, NULL)) {
        free(lock);
        return NULL;
    }

    return lock;
}

void
zt_threads_pthreads_lock_free(zt_threads_mutex *_lock, int locktype UNUSED) {
    pthread_mutex_t *lock;

    lock = (pthread_mutex_t *)_lock;

    pthread_mutex_destroy(lock);
    free(lock);
}

int
zt_threads_pthreads_lock(int mode, zt_threads_mutex *_lock) {
    pthread_mutex_t *lock;

    lock = (pthread_mutex_t *)_lock;

    if (mode & ZT_THREADS_LOCK_NONBLOCKING) {
        return pthread_mutex_trylock(lock);
    }

    return pthread_mutex_lock(lock);
}

int
zt_threads_pthreads_unlock(int mode UNUSED, zt_threads_mutex *_lock) {
    pthread_mutex_t *lock;

    lock = (pthread_mutex_t *)_lock;

    return pthread_mutex_unlock(lock);
}

zt_threads_cond *
zt_threads_pthreads_cond_alloc(int condtype UNUSED) {
    pthread_cond_t *cond;

    if ((cond = calloc(sizeof(pthread_cond_t), 1)) == NULL) {
        return NULL;
    }

    if (pthread_cond_init(cond, NULL)) {
        free(cond);
        return NULL;
    }

    return cond;
}

void
zt_threads_pthreads_cond_free(zt_threads_cond *_cond) {
    pthread_cond_t *cond;

    cond = (pthread_cond_t *)_cond;
    pthread_cond_destroy(cond);
    free(cond);
}

int
zt_threads_pthreads_cond_signal(zt_threads_cond *_cond, int broadcast) {
    pthread_cond_t *cond;
    int             status;

    cond = (pthread_cond_t *)_cond;

    if (broadcast) {
        status = pthread_cond_broadcast(cond);
    } else {
        status = pthread_cond_signal(cond);
    }

    return status ? -1 : 0;
}

int
zt_threads_pthreads_cond_wait(zt_threads_cond *_cond, zt_threads_mutex *_lock, struct timeval *tv) {
    int              status;
    pthread_cond_t  *cond;
    pthread_mutex_t *lock;

    cond = (pthread_cond_t *)_cond;
    lock = (pthread_mutex_t *)_lock;

    if (tv != NULL) {
        struct timeval  now;
        struct timeval  abstime;
        struct timespec ts;

        /* timeradd(tvp, uvp, vvp) timeradd((tvp), (uvp), (vvp)) */
        gettimeofday(&now, NULL);
        zt_add_time(&abstime, &now, tv);
        ts.tv_sec  = abstime.tv_sec;
        ts.tv_nsec = abstime.tv_usec * 1000;

        status     = pthread_cond_timedwait(cond, lock, &ts);

        if (status == ETIMEDOUT) {
            return 1;
        }

        if (status) {
            return -1;
        }

        return 0;
    }

    status = pthread_cond_wait(cond, lock);
    return status ? -1 : 0;
}

int
zt_threads_pthreads_start(zt_threads_thread *_thread, zt_threads_attr *_attr,
                          void * (*start_cb)(void *), void *args) {
    pthread_t      *thread;
    pthread_attr_t *attr;
    int             status;

    thread = (pthread_t *)_thread;
    attr   = (pthread_attr_t *)_attr;

    status = pthread_create(thread, attr, start_cb, args);

    return status ? -1 : 0;
}

void
zt_threads_pthreads_end(void *args) {
    pthread_exit(args);
}

zt_threads_thread *
zt_threads_pthreads_alloc_thread(void) {
    return calloc(sizeof(pthread_t), 1);
}

int
zt_threads_pthreads_kill(zt_threads_thread *_thread) {
    pthread_t *thread;

    thread = (pthread_t *)_thread;

    return pthread_cancel(*thread);
}

unsigned long int
zt_threads_pthread_id(void) {
    return (unsigned long int)pthread_self();
}

int
zt_threads_pthread_join(zt_threads_thread *_thread, void **data) {
    pthread_t *thread;

    thread = (pthread_t *)_thread;
    return pthread_join(*thread, data);
}

int zt_threads_pthread_detach(zt_threads_thread *thread) {
    pthread_t *t = (pthread_t *)thread;

    return pthread_detach(*t);
}

int
zt_threads_use_pthreads(void) {
    struct zt_threads_lock_callbacks  cbs = {
        zt_threads_pthreads_lock_alloc,
        zt_threads_pthreads_lock_free,
        zt_threads_pthreads_lock,
        zt_threads_pthreads_unlock
    };

    struct zt_threads_cond_callbacks  cond_cbs = {
        zt_threads_pthreads_cond_alloc,
        zt_threads_pthreads_cond_free,
        zt_threads_pthreads_cond_signal,
        zt_threads_pthreads_cond_wait
    };

    struct zt_threads_cntrl_callbacks cntrl_cbs = {
        zt_threads_pthreads_start,
        zt_threads_pthreads_end,
        zt_threads_pthreads_kill,
        zt_threads_pthread_id,
        zt_threads_pthread_join,
        zt_threads_pthread_detach
    };

    zt_threads_set_lock_callbacks(&cbs);
    zt_threads_set_cond_callbacks(&cond_cbs);
    zt_threads_set_cntrl_callbacks(&cntrl_cbs);
    zt_threads_alloc_thread = zt_threads_pthreads_alloc_thread;

    return 0;
}


text:
package tools

//go:generate go install github.com/golang/mock/mockgen

import (
	_ "github.com/golang/mock/mockgen"
)


text:
package pl.narfsoftware.thermometer.ui;

import pl.narfsoftware.thermometer.R;
import pl.narfsoftware.thermometer.ThermometerApp;
import pl.narfsoftware.thermometer.db.DbHelper;
import pl.narfsoftware.thermometer.db.SensorData;
import pl.narfsoftware.thermometer.preferences.Preferences;
import pl.narfsoftware.thermometer.service.SensorsDataSavingService;
import pl.narfsoftware.thermometer.utils.Constants;
import pl.narfsoftware.thermometer.utils.Label;
import pl.narfsoftware.thermometer.utils.RefresherRunnable;
import pl.narfsoftware.thermometer.utils.TimerRunnable;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.graphics.Color;
import android.hardware.Sensor;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.LineGraphView;

public class PlotFragment extends Fragment implements OnCheckedChangeListener {
	static final String TAG = "PlotFragment";

	Activity activity;
	ThermometerApp app;
	Preferences preferences;
	final static String ARG_KEY = "sensor_key";
	/**
	 * Key of ambient condition as in HashMaps, not index on the list in
	 * SensorsFragment
	 */
	int currentKey = Sensor.TYPE_AMBIENT_TEMPERATURE;

	boolean saveData;

	LinearLayout backgroundLayout;
	TextView tvUnit;
	GraphView graphView;
	Switch saveDataSwitch;

	SensorData sensorData;
	GraphViewSeries dataSeries;
	String tableName = "null";

	float textSize;
	int verticalLabelsWidth;
	Label label;

	private final Handler handler = new Handler();
	private TimerRunnable timer;
	private RefresherRunnable refresher;

	OnSensorDataSaveStateChangedListener callback;

	public interface OnSensorDataSaveStateChangedListener {
		/**
		 * Called by PlotFragment when a dataSave switch checked state changes
		 *
		 * @param key
		 *            Sensors' type - key in dictionary
		 */
		public void onSensorDataSaveStateChanged(int key);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.activity = activity;
		// This makes sure that the container activity has implemented
		// the callback interface. If not, it throws an exception.
		try {
			callback = (OnSensorDataSaveStateChangedListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement OnSensorDataSaveStateChangedListener");
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		app = ((ThermometerApp) activity.getApplication());
		preferences = app.getPrefs();
		textSize = getResources().getInteger(R.integer.plot_label_text_size);
		verticalLabelsWidth = getResources().getInteger(
				R.integer.plot_vertical_labels_widht);
		sensorData = app.getSensorData();

		initGraphView();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View inflatedView = inflater.inflate(R.layout.plot_view, container,
				false);
		backgroundLayout = (LinearLayout) inflatedView.findViewById(R.id.graph);
		saveDataSwitch = (Switch) inflatedView.findViewById(R.id.saveData);
		tvUnit = (TextView) inflatedView.findViewById(R.id.tvUnit);

		saveDataSwitch.setOnCheckedChangeListener(this);

		// If activity recreated (such as from screen rotate), restore
		// the previous sensor selection set by onSaveInstanceState().
		// This is primarily necessary when in the two-pane layout.
		if (savedInstanceState != null) {
			currentKey = savedInstanceState.getInt(ARG_KEY);
		}

		// Inflate the layout for this fragment
		return inflatedView;
	}

	@Override
	public void onStart() {
		super.onStart();

		// During startup, check if there are arguments passed to the fragment.
		// onStart is a good place to do this because the layout has already
		// been applied to the fragment at this point so we can safely call the
		// method below that sets the article text.
		Bundle args = getArguments();
		if (args != null) {
			// Set plot based on argument passed in
			// updatePlotFragment(args.getInt(ARG_KEY));
			currentKey = args.getInt(ARG_KEY);
		} else if (currentKey < 0) {
			// Set plot based on saved instance state defined during
			// onCreateView
			// updatePlotFragment(currentKey);
			currentKey = Sensor.TYPE_AMBIENT_TEMPERATURE;
		}
	}

	@Override
	public void onResume() {
		super.onResume();

		initDataForGraphView();

		if (dataSeries.getValues().length <= 1) {
			// hide vertical labels
			graphView.getGraphViewStyle().setVerticalLabelsWidth(1);
			if (!preferences.dataHintToastShowed())
				showDataHindToast();
		} else
			showData();

		runGraphView();
	}

	private void initDataForGraphView() {
		tvUnit.setText("");
		tvUnit.setTypeface(preferences.getTypeface());
		saveDataSwitch.setChecked(app.saveData(currentKey));
		saveDataSwitch.setTypeface(preferences.getTypeface());
		tableName = DbHelper.TABLE_NAMES.get(currentKey);
		saveData = app.saveData(currentKey);
		dataSeries = new GraphViewSeries(sensorData.query(tableName,
				preferences.getTempUnitCode()));
		Log.d(TAG, "data rows count: " + dataSeries.getValues().length);
	}

	private void showDataHindToast() {
		// this notification is showed only once
		preferences.setDataHintToastShowed();
		String toastText = getResources()
				.getString(R.string.no_data_info_toast);
		toastText += "\n"
				+ getResources().getString(R.string.no_data_hint_toast);
		Toast.makeText(activity, toastText, Toast.LENGTH_LONG).show();
	}

	private void showData() {
		if (tvUnit != null)
			tvUnit.setText(Constants.UNITS[preferences.getTempUnitCode()]
					.get(currentKey));

		graphView.setCustomLabelFormatter(new Label(dataSeries));

		graphView.getGraphViewStyle().setVerticalLabelsWidth(
				verticalLabelsWidth);

		graphView.addSeries(dataSeries);
		graphView.setViewPort(
				dataSeries.getValues()[0].getX(),
				dataSeries.getValues()[dataSeries.getValues().length - 1]
						.getX() - dataSeries.getValues()[0].getX());
		graphView.setScalable(true);
	}

	private void runGraphView() {
		graphView.setScrollable(true);
		// add graph view
		if (backgroundLayout != null && graphView.getParent() == null)
			backgroundLayout.addView(graphView);

		timer = new TimerRunnable(activity, saveData, dataSeries, sensorData,
				tableName, graphView, handler);
		refresher = new RefresherRunnable(saveData, dataSeries, tvUnit,
				Constants.UNITS[preferences.getTempUnitCode()].get(currentKey),
				verticalLabelsWidth, graphView, handler);

		handler.postDelayed(timer, Constants.ONE_SECOND);
		handler.postDelayed(refresher, Constants.ONE_SECOND);
	}

	@Override
	public void onPause() {
		super.onPause();
		handler.removeCallbacks(timer);
		handler.removeCallbacks(refresher);
	}

	@Override
	public void onStop() {
		super.onStop();

		graphView.removeAllSeries();

		if (graphView.getParent() != null)
			backgroundLayout.removeView(graphView);

		if (!saveData)
			sensorData.close();
	}

	public void updatePlotFragment(int key) {
		currentKey = key;

		handler.removeCallbacks(timer);
		handler.removeCallbacks(refresher);

		graphView.removeAllSeries();

		initDataForGraphView();

		if (dataSeries.getValues().length <= 1) {
			// hide vertical labels
			graphView.getGraphViewStyle().setVerticalLabelsWidth(1);
			if (!preferences.dataHintToastShowed())
				showDataHindToast();
		} else
			showData();

		runGraphView();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);

		// Save the current ambient condition selection in case we need to
		// recreate the fragment
		outState.putInt(ARG_KEY, currentKey);
	}

	private void initGraphView() {
		graphView = new LineGraphView(activity, "");
		graphView.getGraphViewStyle().setHorizontalLabelsColor(Color.BLACK);
		graphView.getGraphViewStyle().setVerticalLabelsColor(Color.BLACK);
		graphView.getGraphViewStyle().setGridColor(Color.GRAY);
		graphView.getGraphViewStyle().setTextSize(textSize);
		graphView.getGraphViewStyle().setNumHorizontalLabels(
				Constants.HORIZONTAL_LABELS_COUNT);
		graphView.getGraphViewStyle().setNumVerticalLabels(
				Constants.VERTICAL_LABELS_COUNT);
		graphView.getGraphViewStyle().setVerticalLabelsWidth(
				verticalLabelsWidth);
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		app.setSaveData(currentKey, saveData = isChecked);
		if (refresher != null)
			refresher.setSaveData(isChecked);
		if (timer != null)
			timer.setSaveData(isChecked);
		if (((SensorsActivity) activity).sensorsFragment != null) {
			((SensorsActivity) activity).sensorsFragment.initIcons();
			((SensorsActivity) activity).sensorsFragment.adapter
					.notifyDataSetChanged();
		}
		callback.onSensorDataSaveStateChanged(currentKey);

		if (app.saveAnyData())
			activity.startService(new Intent(activity,
					SensorsDataSavingService.class));
		else
			activity.stopService(new Intent(activity,
					SensorsDataSavingService.class));
	}
}


text:
try:
    from setuptools import setup
except ImportError:
    from distutils.core import setup

from landsat import __version__

def readme():
    with open('README.rst') as f:
        return f.read()

test_requirements = [
  'nose==1.3.3'
]

setup(
    name='landsat-util',
    version=__version__,
    description='A utility to search, download and process Landsat 8' +
    ' satellite imagery',
    long_description=readme(),
    author='Development Seed',
    author_email='info@developmentseed.org',
    scripts=['bin/landsat'],
    url='https://github.com/developmentseed/landsat-util',
    packages=['landsat'],
    include_package_data=True,
    license='CCO',
    platforms='Posix; MacOS X; Windows',
    install_requires=[
      'requests==2.5.3',
      'python-dateutil==2.2',
      'numpy==1.9.1',
      'termcolor==1.1.0',
      'rasterio==0.18',
      'six==1.9.0',
      'scipy==0.15.1',
      'scikit-image==0.10.1',
      'homura==0.1.0'
    ],
    test_suite='nose.collector',
    test_require=test_requirements
)


text:
<!doctype html>

    <meta charset="utf-8">
    <title>CAP Regression</title>
    <meta name="description" content="Analytics for Big and Complex Data Group">
    <meta name="author" content="Xi (Rossi) LUO">
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link rel="stylesheet" href="./reveal.js.3.5/css/reveal.css">
    <link rel="stylesheet" href="CSS/rossisimple.css" id="theme">
    <link rel="stylesheet" href="CSS/brightRoom.css" id="theme">

    <link rel="stylesheet" href="./reveal.js.3.5/lib/css/zenburn.css">

    <link rel="stylesheet" type="text/css" href="JSLibrary/coverflowjs/coverflow.css" />

       <credit></credit>

            \(\def\loading{......LOADING......Please Wait......} \def\RR{\bf R} \def\real{\mathbb{R}} \def\bold#1{\bf #1} \def\d{\mbox{Cord}} \def\hd{\widehat \mbox{Cord}} \DeclareMathOperator{\cov}{cov} \DeclareMathOperator{\var}{var} \DeclareMathOperator{\cor}{cor} \newcommand{\ac}[1]{\left\{#1\right\}} \DeclareMathOperator{\Ex}{\mathbb{E}} \DeclareMathOperator{\diag}{diag}  \newcommand{\bm}[1]{\boldsymbol{#1}} \def\wait{......LOADING......Please Wait......}\)

Covariate Assisted Principal (<highlight>CAP</highlight>) Regression for   Matrix Outcomes

Xi (Rossi) LUO

                        <medium>The University of Texas

Health Science Center

School of Public Health

Dept of Biostatistics

and Data Science

ABCD Research Group </medium>

                    <medium>CMStat, London, UK

December 14, 2019</medium>

                     Funding: NIH R01EB022911;   NSF/DMS (BD2K) 1557467

Co-Authors

Yi Zhao

                            Indiana Univ

Bingkai Wang

                            Johns Hopkins Biostat

<medium>Stewart Mostofsky</medium>

                            Johns Hopkins Medicine

Brian Caffo

                            Johns Hopkins Biostat

Slides viewable on web:

<emph>bit.ly</emph>/<highlight>cmstat19</highlight>

Statistics/Data Science Focuses

Motivating Example

Brain network connections vary by covariates (e.g. age/sex)

<emph>Goal</emph>:  model  how covariates change  network connections

                $$\textrm{function}(\textbf{graph}) = \textbf{age}\times \beta_1 +  \textbf{sex}\times \beta_2 + \cdots $$

Resting-state fMRI Networks

fMRI measures brain activities over time

Resting-state: "do nothing" during scanning

Brain networks constructed using <emph>cov/cor</emph> matrices of time series

Mathematical Problem

Given $n$ (semi-)positive matrix outcomes, $\Sigma_i\in \real^{p\times p}$

Given $n$  corresponding vector covariates, $x_i \in \real^{q}$

Find function $g(\Sigma_i) = x_i \beta$, $i=1,\dotsc, n$

In essense, <emph>regress positive matrices on vectors</emph>

Some Related Problems

Heterogeneous regression or weighted LS:

Usually for scalar variance $\sigma_i$, find $g(\sigma_i) = f(x_i)$

Goal: to improve efficiency, not to interpret $x_i \beta$

Covariance models Anderson, 73; Pourahmadi, 99; Hoff, Niu, 12; Fox, Dunson, 15; Zou, 17

Model $\Sigma_i = g(x_i)$, sometimes $n=i=1$

Goal: better models for $\Sigma_i$

Multi-group PCA Flury, 84, 88; Boik 02; Hoff 09; Franks, Hoff, 16

No regression model, cannot handle vector $x_i$

Goal: find common/uncommon parts of multiple $\Sigma_i$

Tensor-on-scalar regression Li, Zhang, 17; Sun, Li, 17

No guarantees for positive matrix outcomes

Massive Edgewise Regressions

Intuitive method by mostly neuroscientists

Try $g_{j,k}(\Sigma_i) = \Sigma_{i}[j,k] = x_i \beta$

Repeat for all   $(j,k) \in \{1,\dotsc, p\}^2$  pairs

Essentially $O(p^2)$ regressions for each connection

Limitations: multiple testing $O(p^2)$, failure to accout for dependencies between regressions

Our CAP in a Nutshell

                    $\mbox{PCA}(\Sigma_i) = x_i \beta$

Essentially, we aim to turn unsupervised PCA to a supervised PCA

Ours differs from existing PCA methods:

Supervised PCA Bair et al, 06 models <emph>scalar-on-vector</emph>

Model and Method

Model

Find principal direction (PD) $\gamma \in \real^p$, such that:
$$ \log({\gamma}^\top\Sigma_{i}{\gamma})=\beta_{0}+x_{i}^\top{\beta}_{1}, \quad i =1,\dotsc, n$$

Example (p=2): PD1 largest variation but not related to $x$

PCA  selects PD1, Ours selects <highlight>PD2</highlight>

Advantages

Scalability: potentially for $p \sim 10^6$ or larger

Interpretation: covariate assisted PCA

Turn <highlight>unsupervised</highlight> PCA into <highlight>supervised</highlight>

Sensitivity: target those covariate-related variations

<highlight>Covariate assisted</highlight> SVD?

Applicability: other big data problems besides fMRI

Method

MLE with constraints:
                      $$\scriptsize \begin{eqnarray}\label{eq:obj_func}
                             \underset{\boldsymbol{\beta},\boldsymbol{\gamma}}{\text{minimize}} &&  \ell(\boldsymbol{\beta},\boldsymbol{\gamma}) :=  \frac{1}{2}\sum_{i=1}^{n}(x_{i}^\top\boldsymbol{\beta}) \cdot T_{i}  +\frac{1}{2}\sum_{i=1}^{n}\boldsymbol{\gamma}^\top \Sigma_{i}\boldsymbol{\gamma} \cdot \exp(-x_{i}^\top\boldsymbol{\beta}) , \nonumber \\
                             \text{such that} && \boldsymbol{\gamma}^\top H \boldsymbol{\gamma}=1
                         \end{eqnarray}$$

Two obvious constriants:

C1: $H = I$

C2: $H = n^{-1}  (\Sigma_1 + \cdots + \Sigma_n) $

Choice of $H$

Proposition:  When (C1) $H=\boldsymbol{\mathrm{I}}$ in  the  optimization problem, for any  fixed $\boldsymbol{\beta}$, the solution of $\boldsymbol{\gamma}$ is the eigenvector corresponding to the minimum eigenvalue of matrix
$$        \sum_{i=1}^{n}\frac{\Sigma_{i}}{\exp(x_{i}^\top\boldsymbol{\beta})} $$

Will focus on the constraint (C2)

Algoirthm

Iteratively update $\beta$ and then $\gamma$

Prove explicit updates

Extension to multiple $\gamma$:

After finding $\gamma^{(1)}$, we will update $\Sigma_i$ by removing its effect

Search for the next PD $\gamma^{(k)}$, $k=2, \dotsc$

Impose the orthogonal constraints such that $\gamma^{k}$ is orthogonal to all $\gamma^{(t)}$ for $t\lt k$

Theory for $\beta$

Theorem:
Assume $\sum_{i=1}^{n}x_{i}x_{i}^\top/n\rightarrow Q$ as $n\rightarrow\infty$. Let $T=\min_{i}T_{i}$, $M_{n}=\sum_{i=1}^{n}T_{i}$, under the true $\boldsymbol{\gamma}$, we have
    \begin{equation}
        \sqrt{M_{n}}\left(\hat{\boldsymbol{\beta}}-\boldsymbol{\beta}\right)\overset{\mathcal{D}}{\longrightarrow}\mathcal{N}\left(\boldsymbol{\mathrm{0}},2 Q^{-1}\right),\quad \text{as } n,T\rightarrow\infty,
    \end{equation}
    where $\hat{\boldsymbol{\beta}}$ is the maximum likelihood estimator when the true $\boldsymbol{\gamma}$ is known.

Theory for $\gamma$

Theorem:
Assume $\Sigma_{i}=\Gamma\Lambda_{i}\Gamma^\top$, where $\Gamma=(\boldsymbol{\gamma}_{1},\dots,\boldsymbol{\gamma}_{p})$ is an orthogonal matrix and $\Lambda_{i}=\mathrm{diag}\{\lambda_{i1},\dots,\lambda_{ip}\}$ with $\lambda_{ik}\neq\lambda_{il}$ ($k\neq l$), for at least one $i\in\{1,\dots,n\}$. There exists $k\in\{1,\dots,p\}$ such that for $\forall~i\in\{1,\dots,n\}$, $\boldsymbol{\gamma}_{k}^\top\Sigma_{i}\boldsymbol{\gamma}_{k}=\exp(x_{i}^\top\boldsymbol{\beta})$. Let $\hat{\boldsymbol{\gamma}}$ be the maximum likelihood estimator of $\boldsymbol{\gamma}_{k}$ in Flury, 84. Then assuming that the assumptions  are satisfied,   $\hat{ \boldsymbol{\beta}}$  from our algorithm   is $\sqrt{M_{n}}$-consistent estimator of $\boldsymbol{\beta}$.

Simulations

PCA and common PCA do not find the first principal direction, because they don't model covariates

Resting-state fMRI

Regression Coefficients

Age

Sex

Age*Sex

No statistical significant changes were found by massive edgewise regression

Brain Map of $\gamma$

Discussion

Regress matrices on vectors

Method to identify covariate-related directions

Theorectical justification

Paper: Biostatistics (to appear, preprint at DOI: 10.1101/425033)

R pkg: <highlight>cap</highlight>

Thank you!

Comments? Questions?

                     BigComplexData.com

 or
                     BrainDataScience.com


text:
/* $OpenBSD: dsaparam.c,v 1.6 2015/10/10 22:28:51 doug Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
 * All rights reserved.
 *
 * This package is an SSL implementation written
 * by Eric Young (eay@cryptsoft.com).
 * The implementation was written so as to conform with Netscapes SSL.
 *
 * This library is free for commercial and non-commercial use as long as
 * the following conditions are aheared to.  The following conditions
 * apply to all code found in this distribution, be it the RC4, RSA,
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
 * included with this distribution is covered by the same copyright terms
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
 *
 * Copyright remains Eric Young's, and as such any Copyright notices in
 * the code are not to be removed.
 * If this package is used in a product, Eric Young should be given attribution
 * as the author of the parts of the library used.
 * This can be in the form of a textual message at program startup or
 * in documentation (online or textual) provided with the package.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    "This product includes cryptographic software written by
 *     Eric Young (eay@cryptsoft.com)"
 *    The word 'cryptographic' can be left out if the rouines from the library
 *    being used are not cryptographic related :-).
 * 4. If you include any Windows specific code (or a derivative thereof) from
 *    the apps directory (application code) you must include an acknowledgement:
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
 *
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * The licence and distribution terms for any publically available version or
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
 * copied and put under another distribution licence
 * [including the GNU Public Licence.]
 */

#include <openssl/opensslconf.h>	/* for OPENSSL_NO_DSA */

/* Until the key-gen callbacks are modified to use newer prototypes, we allow
 * deprecated functions for openssl-internal code */
#ifdef OPENSSL_NO_DEPRECATED
#undef OPENSSL_NO_DEPRECATED
#endif

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "apps.h"

#include <openssl/bio.h>
#include <openssl/bn.h>
#include <openssl/err.h>
#include <openssl/dsa.h>
#include <openssl/pem.h>
#include <openssl/x509.h>

static struct {
	int C;
	int genkey;
	char *infile;
	int informat;
	int noout;
	char *outfile;
	int outformat;
	int text;
} dsaparam_config;

static struct option dsaparam_options[] = {
	{
		.name = "C",
		.desc = "Convert DSA parameters into C code",
		.type = OPTION_FLAG,
		.opt.flag = &dsaparam_config.C,
	},
	{
		.name = "genkey",
		.desc = "Generate a DSA key",
		.type = OPTION_FLAG,
		.opt.flag = &dsaparam_config.genkey,
	},
	{
		.name = "in",
		.argname = "file",
		.desc = "Input file (default stdin)",
		.type = OPTION_ARG,
		.opt.arg = &dsaparam_config.infile,
	},
	{
		.name = "inform",
		.argname = "format",
		.desc = "Input format (DER or PEM (default))",
		.type = OPTION_ARG_FORMAT,
		.opt.value = &dsaparam_config.informat,
	},
	{
		.name = "noout",
		.desc = "No output",
		.type = OPTION_FLAG,
		.opt.flag = &dsaparam_config.noout,
	},
	{
		.name = "out",
		.argname = "file",
		.desc = "Output file (default stdout)",
		.type = OPTION_ARG,
		.opt.arg = &dsaparam_config.outfile,
	},
	{
		.name = "outform",
		.argname = "format",
		.desc = "Output format (DER or PEM (default))",
		.type = OPTION_ARG_FORMAT,
		.opt.value = &dsaparam_config.outformat,
	},
	{
		.name = "text",
		.desc = "Print as text",
		.type = OPTION_FLAG,
		.opt.flag = &dsaparam_config.text,
	},
	{ NULL },
};

static void
dsaparam_usage(void)
{
	fprintf(stderr,
	    "usage: dsaparam [-C] [-genkey] [-in file]\n"
	    "    [-inform format] [-noout] [-out file] [-outform format]\n"
	    "    [-text] [numbits]\n\n");
	options_usage(dsaparam_options);
}

static int dsa_cb(int p, int n, BN_GENCB * cb);

int
dsaparam_main(int argc, char **argv)
{
	DSA *dsa = NULL;
	int i;
	BIO *in = NULL, *out = NULL;
	int ret = 1;
	int numbits = -1;
	char *strbits = NULL;

	if (single_execution) {
		if (pledge("stdio rpath wpath cpath", NULL) == -1) {
			perror("pledge");
			exit(1);
		}
	}

	memset(&dsaparam_config, 0, sizeof(dsaparam_config));

	dsaparam_config.informat = FORMAT_PEM;
	dsaparam_config.outformat = FORMAT_PEM;

	if (options_parse(argc, argv, dsaparam_options, &strbits, NULL) != 0) {
		dsaparam_usage();
		goto end;
	}

	if (strbits != NULL) {
		const char *errstr;
		numbits = strtonum(strbits, 0, INT_MAX, &errstr);
		if (errstr) {
			fprintf(stderr, "Invalid number of bits: %s", errstr);
			goto end;
		}
	}

	in = BIO_new(BIO_s_file());
	out = BIO_new(BIO_s_file());
	if (in == NULL || out == NULL) {
		ERR_print_errors(bio_err);
		goto end;
	}
	if (dsaparam_config.infile == NULL)
		BIO_set_fp(in, stdin, BIO_NOCLOSE);
	else {
		if (BIO_read_filename(in, dsaparam_config.infile) <= 0) {
			perror(dsaparam_config.infile);
			goto end;
		}
	}
	if (dsaparam_config.outfile == NULL) {
		BIO_set_fp(out, stdout, BIO_NOCLOSE);
	} else {
		if (BIO_write_filename(out, dsaparam_config.outfile) <= 0) {
			perror(dsaparam_config.outfile);
			goto end;
		}
	}

	if (numbits > 0) {
		BN_GENCB cb;
		BN_GENCB_set(&cb, dsa_cb, bio_err);
		dsa = DSA_new();
		if (!dsa) {
			BIO_printf(bio_err, "Error allocating DSA object\n");
			goto end;
		}
		BIO_printf(bio_err, "Generating DSA parameters, %d bit long prime\n", numbits);
		BIO_printf(bio_err, "This could take some time\n");
		if (!DSA_generate_parameters_ex(dsa, numbits, NULL, 0, NULL, NULL, &cb)) {
			ERR_print_errors(bio_err);
			BIO_printf(bio_err, "Error, DSA key generation failed\n");
			goto end;
		}
	} else if (dsaparam_config.informat == FORMAT_ASN1)
		dsa = d2i_DSAparams_bio(in, NULL);
	else if (dsaparam_config.informat == FORMAT_PEM)
		dsa = PEM_read_bio_DSAparams(in, NULL, NULL, NULL);
	else {
		BIO_printf(bio_err, "bad input format specified\n");
		goto end;
	}
	if (dsa == NULL) {
		BIO_printf(bio_err, "unable to load DSA parameters\n");
		ERR_print_errors(bio_err);
		goto end;
	}
	if (dsaparam_config.text) {
		DSAparams_print(out, dsa);
	}
	if (dsaparam_config.C) {
		unsigned char *data;
		int l, len, bits_p;

		len = BN_num_bytes(dsa->p);
		bits_p = BN_num_bits(dsa->p);
		data = malloc(len + 20);
		if (data == NULL) {
			perror("malloc");
			goto end;
		}
		l = BN_bn2bin(dsa->p, data);
		printf("static unsigned char dsa%d_p[] = {", bits_p);
		for (i = 0; i < l; i++) {
			if ((i % 12) == 0)
				printf("\n\t");
			printf("0x%02X, ", data[i]);
		}
		printf("\n\t};\n");

		l = BN_bn2bin(dsa->q, data);
		printf("static unsigned char dsa%d_q[] = {", bits_p);
		for (i = 0; i < l; i++) {
			if ((i % 12) == 0)
				printf("\n\t");
			printf("0x%02X, ", data[i]);
		}
		printf("\n\t};\n");

		l = BN_bn2bin(dsa->g, data);
		printf("static unsigned char dsa%d_g[] = {", bits_p);
		for (i = 0; i < l; i++) {
			if ((i % 12) == 0)
				printf("\n\t");
			printf("0x%02X, ", data[i]);
		}
		free(data);
		printf("\n\t};\n\n");

		printf("DSA *get_dsa%d()\n\t{\n", bits_p);
		printf("\tDSA *dsa;\n\n");
		printf("\tif ((dsa = DSA_new()) == NULL) return(NULL);\n");
		printf("\tdsa->p = BN_bin2bn(dsa%d_p, sizeof(dsa%d_p), NULL);\n",
		    bits_p, bits_p);
		printf("\tdsa->q = BN_bin2bn(dsa%d_q, sizeof(dsa%d_q), NULL);\n",
		    bits_p, bits_p);
		printf("\tdsa->g = BN_bin2bn(dsa%d_g, sizeof(dsa%d_g), NULL);\n",
		    bits_p, bits_p);
		printf("\tif ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL))\n");
		printf("\t\t{ DSA_free(dsa); return(NULL); }\n");
		printf("\treturn(dsa);\n\t}\n");
	}
	if (!dsaparam_config.noout) {
		if (dsaparam_config.outformat == FORMAT_ASN1)
			i = i2d_DSAparams_bio(out, dsa);
		else if (dsaparam_config.outformat == FORMAT_PEM)
			i = PEM_write_bio_DSAparams(out, dsa);
		else {
			BIO_printf(bio_err, "bad output format specified for outfile\n");
			goto end;
		}
		if (!i) {
			BIO_printf(bio_err, "unable to write DSA parameters\n");
			ERR_print_errors(bio_err);
			goto end;
		}
	}
	if (dsaparam_config.genkey) {
		DSA *dsakey;

		if ((dsakey = DSAparams_dup(dsa)) == NULL)
			goto end;
		if (!DSA_generate_key(dsakey)) {
			ERR_print_errors(bio_err);
			DSA_free(dsakey);
			goto end;
		}
		if (dsaparam_config.outformat == FORMAT_ASN1)
			i = i2d_DSAPrivateKey_bio(out, dsakey);
		else if (dsaparam_config.outformat == FORMAT_PEM)
			i = PEM_write_bio_DSAPrivateKey(out, dsakey, NULL, NULL, 0, NULL, NULL);
		else {
			BIO_printf(bio_err, "bad output format specified for outfile\n");
			DSA_free(dsakey);
			goto end;
		}
		DSA_free(dsakey);
	}
	ret = 0;

end:
	BIO_free(in);
	if (out != NULL)
		BIO_free_all(out);
	if (dsa != NULL)
		DSA_free(dsa);

	return (ret);
}

static int
dsa_cb(int p, int n, BN_GENCB * cb)
{
	char c = '*';

	if (p == 0)
		c = '.';
	if (p == 1)
		c = '+';
	if (p == 2)
		c = '*';
	if (p == 3)
		c = '\n';
	BIO_write(cb->arg, &c, 1);
	(void) BIO_flush(cb->arg);
#ifdef GENCB_TEST
	if (stop_keygen_flag)
		return 0;
#endif
	return 1;
}