text:
"""
Copyright (c) 2015-2022 Red Hat, Inc
All rights reserved.

This software may be modified and distributed under the terms
of the BSD license. See the LICENSE file for details.
"""

import inspect
import json
import logging
import os
import signal
import time
from dataclasses import fields, Field
from pathlib import Path
from textwrap import dedent
from typing import Any, Dict, List

import pytest
from flexmock import flexmock

import osbs.exceptions
from dockerfile_parse import DockerfileParser
from osbs.utils import ImageName

from atomic_reactor.inner import (BuildResults, BuildResultsEncoder,
                                  BuildResultsJSONDecoder, DockerBuildWorkflow,
                                  FSWatcher, ImageBuildWorkflowData, TagConf)
from atomic_reactor.source import PathSource, DummySource
from atomic_reactor.util import (
    DockerfileImages, validate_with_schema, graceful_chain_get
)
from atomic_reactor.dirs import ContextDir
from atomic_reactor.plugin import Plugin, PluginFailedException
from tests.util import is_string_type
from tests.constants import DOCKERFILE_MULTISTAGE_CUSTOM_BAD_PATH

BUILD_RESULTS_ATTRS = ['build_logs',
                       'built_img_inspect',
                       'built_img_info',
                       'base_img_info',
                       'base_plugins_output',
                       'built_img_plugins_output']

NAMESPACE = 'test-namespace'
PIPELINE_RUN_NAME = 'test-pipeline-run'

pytestmark = pytest.mark.usefixtures('user_params')

class Watcher(object):
    def __init__(self, raise_exc=None):
        self.called = False
        self.raise_exc = raise_exc

    def call(self):
        self.called = True
        if self.raise_exc is not None:
            raise self.raise_exc    # pylint: disable=raising-bad-type

    def was_called(self):
        return self.called

class WatcherWithSignal(Watcher):
    def __init__(self, signal=None):
        super(WatcherWithSignal, self).__init__()
        self.signal = signal

    def call(self):
        super(WatcherWithSignal, self).call()
        if self.signal:
            os.kill(os.getpid(), self.signal)

class UpdateMaintainerPlugin(Plugin):
    key = "update_maintainer"
    def run(self): pass

class PushImagePlugin(Plugin):
    key = "push_image"
    def run(self): pass

class CleanupPlugin(Plugin):
    key = "cleanup"
    def run(self): pass

class WatchedMixIn(object):
    """
    Mix-in class for plugins we want to watch.
    """

    def __init__(self, workflow, watcher, *args, **kwargs):
        super(WatchedMixIn, self).__init__(workflow, *args, **kwargs)
        self.watcher = watcher

    def run(self):
        self.watcher.call()

class UpdateMaintainerPluginWatched(WatchedMixIn, Plugin):
    key = 'update_maintainer_watched'

    def run(self):
        super().run()
        return True

class PushImagePluginWatched(WatchedMixIn, Plugin):
    key = 'push_image_watched'

    def run(self):
        super().run()
        return "pushed image"

class CleanupPluginWatched(WatchedMixIn, Plugin):
    key = 'cleanup_watched'

def test_build_results_encoder():
    results = BuildResults()
    expected_data = {}
    for attr in BUILD_RESULTS_ATTRS:
        setattr(results, attr, attr)
        expected_data[attr] = attr

    data = json.loads(json.dumps(results, cls=BuildResultsEncoder))
    assert data == expected_data

def test_build_results_decoder():
    data = {}
    expected_results = BuildResults()
    for attr in BUILD_RESULTS_ATTRS:
        setattr(expected_results, attr, attr)
        data[attr] = attr

    results = json.loads(json.dumps(data), cls=BuildResultsJSONDecoder)
    for attr in set(BUILD_RESULTS_ATTRS) - {'build_logs'}:
        assert getattr(results, attr) == getattr(expected_results, attr)

@pytest.mark.parametrize('prev_task_cancelled', [False, True])
@pytest.mark.parametrize('data_build_canceled', [False, True])
@pytest.mark.parametrize('prev_task_failed', [False, True])
@pytest.mark.parametrize('plugin_errors', [{}, {"some_plugin": "some error"}])
def test_check_build_outcome(
    workflow: DockerBuildWorkflow,
    prev_task_failed: bool,
    data_build_canceled: bool,
    prev_task_cancelled: bool,
    plugin_errors: Dict[str, str],
):

    expect_canceled = prev_task_cancelled or data_build_canceled
    expect_failed = expect_canceled or prev_task_failed or bool(plugin_errors)

    workflow.conf.conf['openshift'] = {'url': 'https://something.com'}
    mock_osbs = flexmock(workflow.osbs)
    (mock_osbs
        .should_receive('build_has_any_cancelled_tasks')
        .once()
        .and_return(prev_task_cancelled))
    (mock_osbs
        .should_receive('build_has_any_failed_tasks')
        .times(0 if expect_canceled else 1)
        .and_return(prev_task_failed))

    workflow.data.build_canceled = data_build_canceled
    workflow.data.plugins_errors = plugin_errors

    outcome = workflow.check_build_outcome()
    assert outcome == (expect_failed, expect_canceled)
    assert outcome != (False, True)  # this is impossible, canceled counts as failed

@pytest.mark.parametrize("terminate_build", [True, False])
def test_workflow_build_image(terminate_build: bool, workflow: DockerBuildWorkflow, caplog):
    """
    Test workflow for base images
    """
    flexmock(DockerfileParser, content='df_content')
    this_file = inspect.getfile(UpdateMaintainerPlugin)
    watch_update_maintainer = Watcher()
    watch_push_image = Watcher()
    watch_cleanup = Watcher()

    workflow.plugins_conf = [
        {
            'name': UpdateMaintainerPluginWatched.key,
            'args': {'watcher': watch_update_maintainer},
        },
        {
            'name': PushImagePluginWatched.key,
            'args': {'watcher': watch_push_image}
        },
        {
            'name': CleanupPluginWatched.key,
            'args': {'watcher': watch_cleanup}
        },
    ]
    workflow.plugin_files = [this_file]

    # This test does not require a separate FSWatcher thread.
    fs_watcher = flexmock(workflow.fs_watcher)
    fs_watcher.should_receive('start')
    fs_watcher.should_receive('finish')

    if terminate_build:
        workflow.plugins_conf[1]['args']['watcher'] = WatcherWithSignal(signal=signal.SIGTERM)
        workflow.build_docker_image()
        assert "Build was canceled" in caplog.text
        assert workflow.data.build_canceled
    else:
        workflow.build_docker_image()

        assert watch_update_maintainer.was_called()
        assert watch_push_image.was_called()
        assert watch_cleanup.was_called()

        results = workflow.data.plugins_results
        assert results[UpdateMaintainerPluginWatched.key]
        assert "pushed image" == results[PushImagePluginWatched.key]
        assert results[CleanupPluginWatched.key] is None

@pytest.mark.parametrize('plugins_conf', [
    # No such plugin but it is required, subsequent plugin should not run.
    [
        {'name': 'no plugin', 'args': {}},
        {'name': UpdateMaintainerPluginWatched.key, 'args': {'watcher': Watcher()}}
    ],
])
def test_bad_plugins_conf(plugins_conf: List[Dict[str, Any]], workflow, caplog):
    flexmock(DockerfileParser, content='df_content')
    this_file = inspect.getfile(UpdateMaintainerPlugin)

    caplog.clear()

    workflow.plugins_conf = plugins_conf
    workflow.plugin_files = [this_file]

    # Find the 'watcher' parameter
    watchers = [conf.get('args', {}).get('watcher') for conf in plugins_conf]
    watcher = [x for x in watchers if x][0]

    with pytest.raises(PluginFailedException):
        workflow.build_docker_image()

    assert not watcher.was_called()
    assert workflow.data.plugins_errors
    assert all([is_string_type(plugin)
                for plugin in workflow.data.plugins_errors])
    assert all([is_string_type(reason)
                for reason in workflow.data.plugins_errors.values()])

    assert any(record.levelno == logging.ERROR for record in caplog.records)

@pytest.mark.parametrize('plugins_conf', [
    # No 'args' key
    [
        {'name': UpdateMaintainerPlugin.key},
        {'name': UpdateMaintainerPluginWatched.key, 'args': {'watcher': Watcher()}},
    ],
    # No such plugin, not required, subsequent plugins should run.
    [
        {'name': 'no plugin', 'args': {}, 'required': False},
        {'name': UpdateMaintainerPluginWatched.key, 'args': {'watcher': Watcher()}}
    ],
])
def test_good_plugins_conf(plugins_conf: List[Dict[str, Any]], workflow, caplog):
    flexmock(DockerfileParser, content='df_content')
    this_file = inspect.getfile(UpdateMaintainerPlugin)

    caplog.clear()

    workflow.plugins_conf = plugins_conf
    workflow.plugin_files = [this_file]

    # Find the 'watcher' parameter
    watchers = [conf.get('args', {}).get('watcher') for conf in plugins_conf]
    watcher = [x for x in watchers if x][0]

    workflow.build_docker_image()
    assert watcher.was_called()
    assert not workflow.data.plugins_errors
    assert all(record.levelno != logging.ERROR for record in caplog.records)

def test_parse_dockerfile_again_after_data_is_loaded(context_dir, build_dir, tmpdir):
    context_dir = ContextDir(Path(tmpdir.join("context_dir")))
    wf_data = ImageBuildWorkflowData.load_from_dir(context_dir)
    # Note that argument source is None, that causes a DummySource is created
    # and "FROM scratch" is included in the Dockerfile.
    workflow = DockerBuildWorkflow(context_dir, build_dir, NAMESPACE, PIPELINE_RUN_NAME, wf_data)
    assert ["scratch"] == workflow.data.dockerfile_images.original_parents

    # Now, save the workflow data and load it again
    wf_data.save(context_dir)

    another_source = DummySource("git", "https://git.host/")
    dfp = DockerfileParser(another_source.source_path)
    dfp.content = 'FROM fedora:35\nCMD ["bash", "--version"]'

    wf_data = ImageBuildWorkflowData.load_from_dir(context_dir)
    flexmock(DockerBuildWorkflow).should_receive("_parse_dockerfile_images").never()
    flexmock(wf_data.dockerfile_images).should_receive("set_source_registry").never()
    workflow = DockerBuildWorkflow(context_dir, build_dir, NAMESPACE, PIPELINE_RUN_NAME, wf_data,
                                   source=another_source)
    assert ["scratch"] == workflow.data.dockerfile_images.original_parents, \
        "The dockerfile_images should not be changed."

@pytest.mark.parametrize('has_version', [True, False])
def test_show_version(has_version, context_dir, build_dir, workflow: DockerBuildWorkflow, caplog):
    """
    Test atomic-reactor print version of osbs-client used to build the build json
    if available
    """
    version = "1.0"
    flexmock(DockerfileParser, content='df_content')
    this_file = inspect.getfile(UpdateMaintainerPlugin)
    plugin_watcher = Watcher()

    caplog.clear()

    kwargs = {}
    if has_version:
        kwargs['client_version'] = version

    workflow = DockerBuildWorkflow(
        context_dir,
        build_dir,
        namespace=NAMESPACE,
        pipeline_run_name=PIPELINE_RUN_NAME,
        source=None,
        plugins_conf=[
            {'name': UpdateMaintainerPlugin.key, 'args': {'watcher': plugin_watcher}}
        ],
        plugin_files=[this_file],
        **kwargs,
    )

    workflow.build_docker_image()
    expected_log_message = f"build json was built by osbs-client {version}"
    assert any(
        expected_log_message in record.message
        for record in caplog.records
        if record.levelno == logging.DEBUG
    ) == has_version

def test_parent_images_to_str(workflow, caplog):
    workflow.data.dockerfile_images = DockerfileImages(['fedora:latest', 'bacon'])
    workflow.data.dockerfile_images['fedora:latest'] = "spam"
    expected_results = {
        "fedora:latest": "spam:latest"
    }
    assert workflow.parent_images_to_str() == expected_results
    assert "None in: base bacon:latest has parent None" in caplog.text

def test_no_base_image(context_dir, build_dir):
    source = DummySource("git", "https://git.host/")
    dfp = DockerfileParser(source.source_path)
    dfp.content = "# no FROM\nADD spam /eggs"
    with pytest.raises(RuntimeError, match="no base image specified"):
        DockerBuildWorkflow(context_dir,
                            build_dir,
                            namespace=NAMESPACE,
                            pipeline_run_name=PIPELINE_RUN_NAME,
                            source=source)

def test_different_custom_base_images(context_dir, build_dir, source_dir):
    source = PathSource(
        "path", f"file://{DOCKERFILE_MULTISTAGE_CUSTOM_BAD_PATH}", workdir=str(source_dir)
    )
    with pytest.raises(NotImplementedError) as exc:
        DockerBuildWorkflow(context_dir,
                            build_dir,
                            namespace=NAMESPACE,
                            pipeline_run_name=PIPELINE_RUN_NAME,
                            source=source)
    message = "multiple different custom base images aren't allowed in Dockerfile"
    assert message in str(exc.value)

def test_copy_from_unkown_stage(context_dir, build_dir, source_dir):
    """test when user has specified COPY --from=image (instead of builder)"""
    source = PathSource("path", f"file://{source_dir}", workdir=str(source_dir))

    dfp = DockerfileParser(str(source_dir))
    dfp.content = dedent("""\
        FROM monty as vikings
        FROM python
        # using a stage name we haven't seen should break:
        COPY --from=notvikings /spam/eggs /bin/eggs
    """)
    with pytest.raises(RuntimeError) as exc_info:
        DockerBuildWorkflow(context_dir,
                            build_dir,
                            namespace=NAMESPACE,
                            pipeline_run_name=PIPELINE_RUN_NAME,
                            source=source)
    assert "FROM notvikings AS source" in str(exc_info.value)

def test_copy_from_invalid_index(context_dir, build_dir, source_dir):
    source = PathSource("path", f"file://{source_dir}", workdir=str(source_dir))

    dfp = DockerfileParser(str(source_dir))
    dfp.content = dedent("""\
        FROM monty as vikings
        # using an index we haven't seen should break:
        COPY --from=5 /spam/eggs /bin/eggs
    """)
    with pytest.raises(RuntimeError) as exc_info:
        DockerBuildWorkflow(context_dir,
                            build_dir,
                            namespace=NAMESPACE,
                            pipeline_run_name=PIPELINE_RUN_NAME,
                            source=source)
    assert "COPY --from=5" in str(exc_info.value)

def test_fs_watcher_update(monkeypatch):

    # check that using the actual os call does not choke
    assert type(FSWatcher._update({})) is dict

    # check that the data actually gets updated
    stats = flexmock(
        f_frsize=1000,  # pretend blocks are 1000 bytes to make mb come out right
        f_blocks=101 * 1000,
        f_bfree=99 * 1000,
        f_files=1, f_ffree=1,
    )
    data = dict(mb_total=101, mb_free=100)
    monkeypatch.setattr(os, "statvfs", stats)
    assert type(FSWatcher._update(data)) is dict
    assert data["mb_used"] == 2
    assert data["mb_free"] == 99

def test_fs_watcher(monkeypatch):
    w = FSWatcher()
    monkeypatch.setattr(time, "sleep", lambda x: x)  # don't waste a second of test time
    w.start()
    w.finish()
    w.join(0.1)  # timeout if thread still running
    assert not w.is_alive()
    assert "mb_used" in w.get_usage_data()

class TestTagConf:
    """Test class TagConf"""

    def test_dump_empty_object(self):
        expected = {
            'primary_images': [],
            'unique_images': [],
            'floating_images': [],
        }
        assert expected == TagConf().as_dict()

    def test_as_dict(self):
        tag_conf = TagConf()
        tag_conf.add_primary_image('r.fp.o/f:35')
        tag_conf.add_floating_image('ns/img:latest')
        tag_conf.add_floating_image('ns1/img2:devel')
        expected = {
            'primary_images': [ImageName.parse('r.fp.o/f:35')],
            'unique_images': [],
            'floating_images': [
                ImageName.parse('ns/img:latest'),
                ImageName.parse('ns1/img2:devel'),
            ],
        }
        assert expected == tag_conf.as_dict()

    @pytest.mark.parametrize(
        'input_data,expected_primary_images,expected_unique_images,expected_floating_images',
        [
            [
                {
                    'primary_images': ['registry/image:2.4'],
                    'unique_images': ['registry/image:2.4'],
                    'floating_images': ['registry/image:latest'],
                },
                [ImageName.parse('registry/image:2.4')],
                [ImageName.parse('registry/image:2.4')],
                [ImageName.parse('registry/image:latest')],
            ],
            [
                {
                    'primary_images': [],
                    'unique_images': [],
                    'floating_images': ['registry/image:latest', 'registry/image:devel'],
                },
                [],
                [],
                [
                    ImageName.parse('registry/image:latest'),
                    ImageName.parse('registry/image:devel'),
                ],
            ],
            [
                {'floating_images': ['registry/image:latest']},
                [],
                [],
                [ImageName.parse('registry/image:latest')],
            ],
        ],
    )
    def test_parse_images(
        self, input_data, expected_primary_images, expected_unique_images, expected_floating_images
    ):
        tag_conf = TagConf.load(input_data)
        assert expected_primary_images == tag_conf.primary_images
        assert expected_unique_images == tag_conf.unique_images
        assert expected_floating_images == tag_conf.floating_images

    def test_get_unique_images_with_platform(self):
        image = 'registry.com/org/hello:world-16111-20220103213046'
        platform = 'x86_64'

        tag_conf = TagConf()
        tag_conf.add_unique_image(image)

        expected = [ImageName.parse(f'{image}-{platform}')]
        actual = tag_conf.get_unique_images_with_platform(platform)

        assert actual == expected

class TestWorkflowData:
    """Test class ImageBuildWorkflowData."""

    def test_creation(self):
        data = ImageBuildWorkflowData()
        assert data.dockerfile_images.is_empty
        assert data.tag_conf.is_empty
        assert {} == data.plugins_results

    def test_load_from_empty_dump(self):
        wf_data = ImageBuildWorkflowData.load({})
        empty_data = ImageBuildWorkflowData()
        field: Field
        for field in fields(ImageBuildWorkflowData):
            name = field.name
            assert getattr(empty_data, name) == getattr(wf_data, name)

    def test_load_from_dump(self):
        input_data = {
            "dockerfile_images": {
                "original_parents": ["scratch"],
                "local_parents": [],
                "source_registry": None,
                "organization": None,
            },
            "plugins_results": {"plugin_1": "result"},
            "tag_conf": {
                "floating_images": [
                    ImageName.parse("registry/httpd:2.4").to_str(),
                ],
            },
        }
        wf_data = ImageBuildWorkflowData.load(input_data)

        expected_df_images = DockerfileImages.load(input_data["dockerfile_images"])
        assert expected_df_images == wf_data.dockerfile_images
        assert input_data["plugins_results"] == wf_data.plugins_results
        assert TagConf.load(input_data["tag_conf"]) == wf_data.tag_conf

    def test_load_from_empty_directory(self, tmpdir):
        context_dir = tmpdir.join("context_dir").mkdir()
        # Note: no data file is created here, e.g. workflow.json.
        wf_data = ImageBuildWorkflowData.load_from_dir(ContextDir(context_dir))
        assert wf_data.dockerfile_images.is_empty
        assert wf_data.tag_conf.is_empty
        assert {} == wf_data.plugins_results

    @pytest.mark.parametrize("data_path,prop_name,wrong_value", [
        # digests should map to an object rather than a string
        [["tag_conf"], "original_parents", "wrong value"],
        # tag name should map to an object rather than a string
        [["tag_conf"], "floating_images", "wrong value"],
    ])
    def test_load_invalid_data_from_directory(self, data_path, prop_name, wrong_value, tmpdir):
        """Test the workflow data is validated by JSON schema when reading from context_dir."""
        context_dir = ContextDir(Path(tmpdir.join("context_dir").mkdir()))

        data = ImageBuildWorkflowData(dockerfile_images=DockerfileImages(["scratch"]))
        data.tag_conf.add_floating_image("registry/httpd:2.4")
        data.plugins_results["plugin_1"] = "result"
        data.save(context_dir)

        saved_data = json.loads(context_dir.workflow_json.read_bytes())
        # Make data invalid
        graceful_chain_get(saved_data, *data_path, make_copy=False)[prop_name] = wrong_value
        context_dir.workflow_json.write_text(json.dumps(saved_data), encoding="utf-8")

        with pytest.raises(osbs.exceptions.OsbsValidationException):
            ImageBuildWorkflowData.load_from_dir(context_dir)

    def test_save_and_load(self, tmpdir):
        """Test save workflow data and then load them back properly."""
        tag_conf = TagConf()
        tag_conf.add_floating_image(ImageName.parse("registry/image:latest"))
        tag_conf.add_primary_image(ImageName.parse("registry/image:1.0"))

        wf_data = ImageBuildWorkflowData(
            dockerfile_images=DockerfileImages(["scratch", "registry/f:35"]),
            # Test object in dict values is serialized
            tag_conf=tag_conf,
            plugins_results={
                "plugin_a": {
                    'parent-images-koji-builds': {
                        ImageName(repo='base', tag='latest').to_str(): {
                            'id': 123456789,
                            'nvr': 'base-image-1.0-99',
                            'state': 1,
                        },
                    },
                },
                "tag_and_push": [
                    # Such object in a list should be handled properly.
                    ImageName(registry="localhost:5000", repo='image', tag='latest'),
                ],
                "image_build": {"logs": ["Build succeeds."]},
            },
            koji_upload_files=[
                {
                    "local_filename": "/path/to/build1.log",
                    "dest_filename": "x86_64-build.log",
                },
                {
                    "local_filename": "/path/to/dir1/remote-source.tar.gz",
                    "dest_filename": "remote-source.tar.gz",
                },
            ]
        )

        context_dir = ContextDir(Path(tmpdir.join("context_dir").mkdir()))
        wf_data.save(context_dir)

        assert context_dir.workflow_json.exists()

        # Verify the saved data matches the schema
        saved_data = json.loads(context_dir.workflow_json.read_bytes())
        try:
            validate_with_schema(saved_data, "schemas/workflow_data.json")
        except osbs.exceptions.OsbsValidationException as e:
            pytest.fail(f"The dumped workflow data does not match JSON schema: {e}")

        # Load and verify the loaded data
        loaded_wf_data = ImageBuildWorkflowData.load_from_dir(context_dir)

        assert wf_data.dockerfile_images == loaded_wf_data.dockerfile_images
        assert wf_data.tag_conf == loaded_wf_data.tag_conf
        assert wf_data.plugins_results == loaded_wf_data.plugins_results


text:
import unittest
from ...compatibility import StringIO
from ...worksheet import Worksheet

class TestWriteCustomFilters(unittest.TestCase):
    """
    Test the Worksheet _write_custom_filters() method.

    """

    def setUp(self):
        self.fh = StringIO()
        self.worksheet = Worksheet()
        self.worksheet._set_filehandle(self.fh)

    def test_write_custom_filters_1(self):
        """Test the _write_custom_filters() method"""

        self.worksheet._write_custom_filters([4, 4000])

        exp = """<customFilters><customFilter operator="greaterThan" val="4000"/></customFilters>"""
        got = self.fh.getvalue()

        self.assertEqual(got, exp)

    def test_write_custom_filters_2(self):
        """Test the _write_custom_filters() method"""

        self.worksheet._write_custom_filters([4, 3000, 0, 1, 8000])

        exp = """<customFilters and="1"><customFilter operator="greaterThan" val="3000"/><customFilter operator="lessThan" val="8000"/></customFilters>"""
        got = self.fh.getvalue()

        self.assertEqual(got, exp)

redpajama_set_name: RedPajamaGithub


text:
package org.springframework.boot.actuate.trace.http;

import java.util.List;

/**
 * A repository for {@link HttpTrace}s.
 *
 * @author Dave Syer
 * @author Andy Wilkinson
 * @since 2.0.0
 */
public interface HttpTraceRepository {

	/**
	 * Find all {@link HttpTrace} objects contained in the repository.
	 * @return the results
	 */
	List<HttpTrace> findAll();

	/**
	 * Adds a trace to the repository.
	 * @param trace the trace to add
	 */
	void add(HttpTrace trace);

}


text: <ValEnum>BINARY</ValEnum>


text:
<?php
namespace SPJ\GameBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity(repositoryClass="SPJ\GameBundle\Repository\CommentRepository")
 * @ORM\Table(name="comment")
 */
class Comment
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="datetime")
     */
    protected $dateCreated;

    /**
     * @ORM\Column(type="string", length=160, nullable=true)
     */
    protected $content;

    /**
     * @ORM\ManyToOne(targetEntity="Challenge", inversedBy="comments")
     * @ORM\JoinColumn(name="challenge_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $challenge;

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="challenges")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $user;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set dateCreated
     *
     * @param \DateTime $dateCreated
     * @return Comment
     */
    public function setDateCreated($dateCreated)
    {
        $this->dateCreated = $dateCreated;

        return $this;
    }

    /**
     * Get dateCreated
     *
     * @return \DateTime
     */
    public function getDateCreated()
    {
        return $this->dateCreated;
    }

    /**
     * Set content
     *
     * @param string $content
     * @return Comment
     */
    public function setContent($content)
    {
        $this->content = $content;

        return $this;
    }

    /**
     * Get content
     *
     * @return string
     */
    public function getContent()
    {
        return $this->content;
    }

    /**
     * Set challenge
     *
     * @param \SPJ\GameBundle\Entity\Challenge $challenge
     * @return Comment
     */
    public function setChallenge(\SPJ\GameBundle\Entity\Challenge $challenge = null)
    {
        $this->challenge = $challenge;

        return $this;
    }

    /**
     * Get challenge
     *
     * @return \SPJ\GameBundle\Entity\Challenge
     */
    public function getChallenge()
    {
        return $this->challenge;
    }

    /**
     * Set user
     *
     * @param \SPJ\GameBundle\Entity\User $user
     * @return Comment
     */
    public function setUser(\SPJ\GameBundle\Entity\User $user = null)
    {
        $this->user = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return \SPJ\GameBundle\Entity\User
     */
    public function getUser()
    {
        return $this->user;
    }

    public function getArrayData()
    {
        return array(
            'content' => $this->getContent(),
            'date_created' => $this->getDateCreated()->format("c"),
            'username' => $this->getUser()->getUsername()
        );
    }
}


text:
/**
 * NBII SKOS converter
 * @since 1.0
 */
package edu.unc.ils.mrc.hive.converter.nbii;


text:
<?php

namespace teamA\CrudBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class DefaultController extends Controller
{
    public function indexAction()
    {
        return $this->render('teamACrudBundle:Default:index.html.twig');

    }
}

class ParentController extends Controller
{
    public function parentAction()
    {
        return $this->render('teamACrudBundle:Parent:parents.html.twig');
    }
}


text:
#include <functional>
#include <memory>
#include <utility>
#include <vector>

#include <pruv/worker_loop.hpp>

namespace pruv {

class workers_reg {
public:
    static workers_reg & instance();

    void add(const char *name, std::function<worker_loop * ()> factory);
    std::unique_ptr<worker_loop> get(const char *name) const;

    template<typename T>
    struct registrator {
        template<typename ... TArg>
        registrator(const char *name, TArg ... args)
        {
            workers_reg::instance().add(name,
                    [args...]{ return new T(args...); });
        }
    };

private:
    workers_reg();
    workers_reg(const workers_reg &) = delete;
    workers_reg(workers_reg &&) = delete;
    void operator = (const workers_reg &) = delete;
    void operator = (workers_reg &&) = delete;

    std::vector<std::pair<const char *, std::function<worker_loop * ()>>> f;
};

} // namespace pruv


text:
#ifndef COMPLEX_H
#define COMPLEX_H

#include <math.h>

/* Complex number class
*/
class Complex {
 public:
    Complex() = default;

    Complex(double real)
        : re_(real) {}

    Complex(double real, double imag)
        : re_(real)
        , im_(imag) {}

    Complex(const Complex &) = default;

    Complex(Complex &&) = default;

    Complex & operator=(const Complex &) = default;

    Complex & operator=(Complex &&) = default;

    double real() const { return re_; }

    double imag() const { return im_; }

    double modulo() const { return sqrt(re_*re_ + im_*im_); }

    double arg() const { return atan2(im_, re_); }

    friend Complex operator+(const Complex &lhs, const Complex &rhs) {
        return Complex(lhs.re_ + rhs.re_, lhs.im_ + rhs.im_);
    }

 private:
    double re_ = 0.0;
    double im_ = 0.0;
};

#endif


text:
jasmine.getFixtures().fixturesPath="./../../spec/javascripts/fixtures/";
jasmine.getFixtures().containerId ="jasmine_content";


text:
Amplifier
=========

Amplifier design software


text:
<!DOCTYPE html>

Project details

Competition name:

Lead partner:

PLACEHOLDER

Other partners:

PLACEHOLDER

Organisation:

PLACEHOLDER

Activities

The most recent activities are at the top.


text:
.class Lcom/android/bluetooth/opp/BluetoothOppLauncherActivity$3;
.super Ljava/lang/Object;
.source "BluetoothOppLauncherActivity.java"

# interfaces
.implements Ljava/lang/Runnable;

# annotations
.annotation system Ldalvik/annotation/EnclosingMethod;
    value = Lcom/android/bluetooth/opp/BluetoothOppLauncherActivity;->onCreate(Landroid/os/Bundle;)V
.end annotation

.annotation system Ldalvik/annotation/InnerClass;
    accessFlags = 0x0
    name = null
.end annotation

# instance fields
.field final synthetic this$0:Lcom/android/bluetooth/opp/BluetoothOppLauncherActivity;

.field final synthetic val$mimeType:Ljava/lang/String;

.field final synthetic val$uris:Ljava/util/ArrayList;

# direct methods
.method constructor <init>(Lcom/android/bluetooth/opp/BluetoothOppLauncherActivity;Ljava/lang/String;Ljava/util/ArrayList;)V
    .locals 0

    .prologue
    .line 174
    iput-object p1, p0, Lcom/android/bluetooth/opp/BluetoothOppLauncherActivity$3;->this$0:Lcom/android/bluetooth/opp/BluetoothOppLauncherActivity;

    iput-object p2, p0, Lcom/android/bluetooth/opp/BluetoothOppLauncherActivity$3;->val$mimeType:Ljava/lang/String;

    iput-object p3, p0, Lcom/android/bluetooth/opp/BluetoothOppLauncherActivity$3;->val$uris:Ljava/util/ArrayList;

    invoke-direct {p0}, Ljava/lang/Object;-><init>()V

    return-void
.end method

# virtual methods
.method public run()V
    .locals 4

    .prologue
    .line 176
    invoke-static {}, Lcom/android/bluetooth/opp/BluetoothOppUtility;->closeUnstartedSendFileInfo()V

    .line 177
    iget-object v0, p0, Lcom/android/bluetooth/opp/BluetoothOppLauncherActivity$3;->this$0:Lcom/android/bluetooth/opp/BluetoothOppLauncherActivity;

    invoke-static {v0}, Lcom/android/bluetooth/opp/BluetoothOppManager;->getInstance(Landroid/content/Context;)Lcom/android/bluetooth/opp/BluetoothOppManager;

    move-result-object v0

    iget-object v1, p0, Lcom/android/bluetooth/opp/BluetoothOppLauncherActivity$3;->val$mimeType:Ljava/lang/String;

    iget-object v2, p0, Lcom/android/bluetooth/opp/BluetoothOppLauncherActivity$3;->val$uris:Ljava/util/ArrayList;

    const/4 v3, 0x0

    invoke-virtual {v0, v1, v2, v3}, Lcom/android/bluetooth/opp/BluetoothOppManager;->saveSendingFileInfo(Ljava/lang/String;Ljava/util/ArrayList;Z)V

    .line 181
    iget-object v0, p0, Lcom/android/bluetooth/opp/BluetoothOppLauncherActivity$3;->this$0:Lcom/android/bluetooth/opp/BluetoothOppLauncherActivity;

    # invokes: Lcom/android/bluetooth/opp/BluetoothOppLauncherActivity;->launchDevicePicker()V
    invoke-static {v0}, Lcom/android/bluetooth/opp/BluetoothOppLauncherActivity;->access$000(Lcom/android/bluetooth/opp/BluetoothOppLauncherActivity;)V

    .line 182
    iget-object v0, p0, Lcom/android/bluetooth/opp/BluetoothOppLauncherActivity$3;->this$0:Lcom/android/bluetooth/opp/BluetoothOppLauncherActivity;

    invoke-virtual {v0}, Lcom/android/bluetooth/opp/BluetoothOppLauncherActivity;->finish()V

    .line 183
    return-void
.end method


text:
namespace Bi
{

//  Forward Declarations ******************************************************
class Z;

//  ***************************************************************************
/// This type is a templated parallel for the version in std:: called div_t.
///
template <typename T>
struct NT_div_t
{
  T quot;
  T rem;
};

//  Explicit Instantitation ***************************************************
using gcd_ex_Z_t  = std::pair<Z, Z>;
using div_Z_t     = NT_div_t<Z>;
using coef_Z_t    = std::pair<Z, Z>;

using value_t     = std::pair<int, int>;

//  ***************************************************************************
/// Calculates the gcd of a and b.
///
/// @param[in] lhs  The first parameter to use in the GCD calculation.
/// @param[in] rhs  The second parameter to use in the GCD calculation.
///                 This value must not be 0.
///
/// @return   The largest factor that both input parameter have in common.
///           Returning a value of 1 indicates they are coprime.
///
///           If rhs is 0, lhs is returned as the result. This may lead
///           to undefined behavior in your calculations.
///
template <typename T>
T gcd(T lhs, T rhs)
{
  while (rhs != 0)
  {
    lhs = lhs % rhs;
    lhs.swap(rhs);
  }

  return lhs;
}

//  ***************************************************************************
/// Calculates the gcd of a and b using the extended euclidean method.
///
/// @param[in] lhs  The first parameter to use in the GCD calculation.
/// @param[in] rhs  The second parameter to use in the GCD calculation.
///
/// @return   A pair that indicates results.
///           return.coef contains the solution to 'x' in the expression:
///
///              ax + by = gcd(a,b)
///
///           return.denom contains the gcd(a,b).
///           A value of 1 indicates they are coprime.
///
template <typename T>
std::pair<T, T> gcd_ex(T lhs, T rhs)
{
  T x0 = 1;
  T x1 = 0;

  while (rhs != 0)
  {
    // TODO: Need to add a function that captures the remainder when a number is divided.
    NT_div_t<T> result = {0};

    result.quot = lhs / rhs;
    result.rem  = lhs % rhs;

    T x2 = x0 - (result.quot * x1);

    x0.swap(x1);
    x1.swap(x2);

    lhs.swap(rhs);
    rhs = result.rem;
  }

  return {x0, lhs};
}

//  ***************************************************************************
/// Calculates the gcd of a and b using the extended euclidean method.
/// This implementation specifically handles types: int and int64_t
///
/// @param[in] lhs  The first parameter to use in the GCD calculation.
/// @param[in] rhs  The second parameter to use in the GCD calculation.
///
/// @return   A pair that indicates results.
///           return.coef contains the solution to 'x' in the expression:
///
///              ax + by = gcd(a,b)
///
///           return.denom contains the gcd(a,b).
///           A value of 1 indicates they are coprime.
///
std::pair<int, int> gcd_ex(int lhs, int rhs);
std::pair<int64_t, int64_t> gcd_ex(int64_t lhs, int64_t rhs);

} // namespace Bi

#endif


text:
package com.example.sendmessageoptions;

import io.rong.imkit.RongIM;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{

	private Button button1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button1 = (Button)findViewById(R.id.button1);

		button1.setOnClickListener(this);

	}

	 @Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
	    	switch(v.getId())
	    	{
	    	case R.id.button1:
    		{
	            RongIM.getInstance().startPrivateChat(this, "9SendmessageCusomization", "新媒5");
    		}
	    	default:
	    	{

	    	}break;
	    	}

		}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


text:
package com.mcleodmoores.integration.serialization.jodabeans;

import java.util.Map;

import org.joda.beans.BeanBuilder;
import org.joda.beans.BeanDefinition;
import org.joda.beans.JodaBeanUtils;
import org.joda.beans.MetaProperty;
import org.joda.beans.impl.direct.DirectBeanBuilder;
import org.joda.beans.impl.direct.DirectMetaPropertyMap;

/**
 * Bean for Finmath {@link net.finmath.marketdata.model.curves.ForwardCurve}.
 */
@BeanDefinition
public class ForwardCurveBean extends AbstractForwardCurveBean {

  /** The serialization version. */
  private static final long serialVersionUID = 1L;

  //------------------------- AUTOGENERATED START -------------------------
  ///CLOVER:OFF
  /**
   * The meta-bean for {@code ForwardCurveBean}.
   * @return the meta-bean, not null
   */
  public static ForwardCurveBean.Meta meta() {
    return ForwardCurveBean.Meta.INSTANCE;
  }

  static {
    JodaBeanUtils.registerMetaBean(ForwardCurveBean.Meta.INSTANCE);
  }

  @Override
  public ForwardCurveBean.Meta metaBean() {
    return ForwardCurveBean.Meta.INSTANCE;
  }

  //-----------------------------------------------------------------------
  @Override
  public ForwardCurveBean clone() {
    return JodaBeanUtils.cloneAlways(this);
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    }
    if (obj != null && obj.getClass() == this.getClass()) {
      return super.equals(obj);
    }
    return false;
  }

  @Override
  public int hashCode() {
    int hash = 7;
    return hash ^ super.hashCode();
  }

  @Override
  public String toString() {
    StringBuilder buf = new StringBuilder(32);
    buf.append("ForwardCurveBean{");
    int len = buf.length();
    toString(buf);
    if (buf.length() > len) {
      buf.setLength(buf.length() - 2);
    }
    buf.append('}');
    return buf.toString();
  }

  @Override
  protected void toString(StringBuilder buf) {
    super.toString(buf);
  }

  //-----------------------------------------------------------------------
  /**
   * The meta-bean for {@code ForwardCurveBean}.
   */
  public static class Meta extends AbstractForwardCurveBean.Meta {
    /**
     * The singleton instance of the meta-bean.
     */
    static final Meta INSTANCE = new Meta();

    /**
     * The meta-properties.
     */
    private final Map<String, MetaProperty<?>> _metaPropertyMap$ = new DirectMetaPropertyMap(
        this, (DirectMetaPropertyMap) super.metaPropertyMap());

    /**
     * Restricted constructor.
     */
    protected Meta() {
    }

    @Override
    public BeanBuilder<? extends ForwardCurveBean> builder() {
      return new DirectBeanBuilder<ForwardCurveBean>(new ForwardCurveBean());
    }

    @Override
    public Class<? extends ForwardCurveBean> beanType() {
      return ForwardCurveBean.class;
    }

    @Override
    public Map<String, MetaProperty<?>> metaPropertyMap() {
      return _metaPropertyMap$;
    }

    //-----------------------------------------------------------------------
  }

  ///CLOVER:ON
  //-------------------------- AUTOGENERATED END --------------------------
}


text:
# spring-relational-repository
Light and straight to the point implementation of Spring Repositories using JDBC without JPA or any high-level entity manager

Warning: this is still in WIP!

## Concepts
Some insights about why this framework exists. JPA's implementations (like hibernate) takes care of a lot of things,
sometimes things that should be handled by the developer. The problem arise when the developer has no idea what is the
physical design of the database, or if he his inside a transaction or not.

The idea behind this framework is to have an implementation of the Spring Data repositories which is as close to the JDBC layer
 as possible and also to be as explicit as possible.

Here is what you will NOT have:
* An Entity Manager
* Query caching (on any level)
* Schema generation
* Implicit entity resolution in foreign key management
* JPQL implementation

Here is what you WILL have:
* Spring Data repositories
* Entity mapping
* Query specific object mappings
* Custom mappers on each fields
* Foreign key resolution tools
* Dialect handling
* Query execution helpers

Here is what you can use with it:
* The spring transaction manager
* Connection and resource pooling (like DBCP or C3P0)

Additionally, this implementation is only available for Java 8. Which makes it more powerful as it can use the Stream
implementation and lambdas awesomeness which makes the data management way easier now.

## Simple stuff

### Configuration
Spring Relation Repository uses the straight forward programmatic configuration of spring.
```java
@Configuration
@EnableRelationalRepositories(basePackages = "foo.bar.repositories")
public class Configuration {
    @Bean
    public DataSource dataSource() {
        return /* your configured source */ new DataSourceImpl();
    }

    @Bean
    public RelationalTemplate relationalTemplate(DataSource dataSource) {
        BeanDialect dialect = GenericDialectProvider.mysql();
        return new RelationalTemplate(dataSource, dialect);
    }
}
```

The annotation EnableRelationalRepositories is a standard Spring Data repository scan. Check out their documentation for more information on this matter.

The template creation however is specific. It takes two arguments, firstly the JDBC data source, then the dialect. This object
specifies how to generate the necessary SQL for your implementation and the default bean mappers.

### Domain
Your domain must have the following shape

```java
@Table("Pojo")
public class Pojo {
    @PK
    private String id;

    private String name;

    // This is mapped to the column `created_date`
    private Date createdDate;

    public Pojo() {
    }

    /* ... getters and setters ... */
}
```

The annotation ```@Table``` specifies which table it must map the domain type to. ```@PK``` specifies the primary key.
 The rest of the fields are assumed to be mapped automatically. If necessary a ```@Column``` annotation can be used to
 specify mappings and names.

### Repositories
Repositories are handled like any spring data repositories. Use the RelationalRepository interface to have all features.

```java
public interface PojoRepository extends RelationalRepository<Pojo, String> {
    @Query("SELECT * FROM Pojo WHERE name = :name")
    Pojo findByName(@Param("name") String name);
}
```

The annotation ```@Query``` specifies the query in the native SQL supported by the database. Positional and named parameters are
supported as in usual JPQL queries.

Note: The annotation is mandatory! No implementation has been made to generate methods based on its name (not explicit enough and can be confusing as few SQL generation is done in this framework).

### Java 8 Stream specifics
Repositories are technically able to produce a Java 8 Stream in a ```findAll``` method for instance. However, it has been chosen
 to not provide such feature.

Let's take this simple example:
```java
// Get the stream of Pojos
Stream<Pojo> s = repository.streamAll();
// Create a index name -> our objects
Map<String, Pojo> pojoIndex = s.collect(Collectors.toMap(Pojo::getName, p -> p));
```

This example is wrong as the resources are either closed during streamAll or are still opened
 after the stream consumption. In Java 8, no resource handling can be done on streams without explicit declaration.

A good way of handling this is:
```java
Map<String, Pojo> pojoIndex;
try (Stream<Pojo> s = repository.streamAll()) {
    // Create a index name -> our objects
    pojoIndex = s.collect(Collectors.toMap(Pojo::getName, p -> p));
}
```

The Java 7 syntax of try-with-resource will effectively closes its resources after the consumption even if their is an exception.
However, this is the developer responsibility to correctly implement this.

To avoid errors and to make everything more explicit, here's what has been chosen:
```java
Map<String, Pojo> pojoIndex = repository.streamAll(stream -> stream.collect(Collectors.toMap(Pojo::getName, p -> p)));
```

The stream implementations takes as argument a function that maps a Stream of objects to a templated result.
 If the function returns a unconsumed stream, its resources are freed so you will not be able to make anything of it.

You can use this kind of syntax in your generated repository function using the ```@CollectorFunction``` annotation.
```java
@Query("SELECT * FROM Pojo WHERE name = ?0")
<E> E findByNameStream(String name, @CollectorFunction(Pojo.class) Function<Stream<Pojo>, E> apply);
```

TODO: GroupBy helper in queries

### Object Mappings
By default, association between fields and column is done via their names. *It is assumed that Java beans uses
 the camel case convention and Database tables uses the snake case convention*.

The default type mapping is completely done using Spring JDBC Utils: StatementCreatorUtils.javaTypeToSqlParameterType for writing
 and JdbcUtils.getResultSetValue. Those method calls can be found and changed in the dialect.

Repositories are able to map objects to their tables. But queries can map their row to data transfer objects like:
```java
@Query("SELECT name, count(*) as `count` FROM Pojo GROUP BY name")
List<PojoCount> countByName();
```

```PojoCount``` being a class with two fields, String name and int count. ```@Column``` annotations are used for its mapping.

Note that it can be more interesting to have something that resembles this:
```java
@Query("SELECT name, count(*) as `count` FROM Pojo GROUP BY name")
<E> E countByName(@CollectorFunction(PojoCount.class) Function<Stream<PojoCount>, E> apply);
```

Therefore, you can call this method to directly create a Map using something like this:
```java
Map<String, Integer> result = repository.countByName(s -> s.collect(Collectors.toMap(PojoCount::getName, PojoCount::getCount));
```

You are therefore assured that no intermediate structure have been created before your final result.

#### Advanced: custom mappers
Mapping can be customized using the ```@Column``` annotation. Obviously, you can change the name, but you can also change read-write operations.
 For instance if you want to map a complex object to a ```jsonb``` column in Postgres 9.4, here's how to do it.

Declare your custom mapper (called here JsonBMapper) which is able to read and write an attributes.
```java
public class JsonBMapper implements BeanAttributeMapper {
    // Jackson Mapper
    private static ObjectMapper om = new ObjectMapper();

    @Override
    public Object readAttribute(ResultSet rs, int column, Field sourceField) throws SQLException {
        // You have to extract the object in the right type from the JDBC result set
        // In this case, convert the JSON string to the desired type
        JavaType targetType = om.getTypeFactory().constructType(targetField.getGenericType());
        try {
            return om.readValue(rs.getString(column), targetType);
        } catch (IOException e) {
            // standard exception handling
            throw new SQLException(e);
        }
    }

    @Override
    public void writeAttribute(PreparedStatement ps, int column, Object o, Field sourceField) throws SQLException {
        // You have to put the correct object in the prepared statement
        // In this case, convert the object into a JSON string that you encapsulate in a PGobject
        PGobject pg = new PGobject();
        pg.setType("jsonb");
        try {
            // Pure JSON serialization
            pg.setValue(om.writeValueAsString(o));
            ps.setObject(column, pg);
        } catch (JsonProcessingException e) {
            // standard exception handling
            throw new SQLException(e);
        }
    }
}
```

Now you can use your mapper in your domain objects or in your data transfer objects.
```java
@Column(mapper = JsonBMapper.class)
Map<String, Integer> keywords;
```


text:
package vertCalcOffset

import (
	"github.com/go-gl/gl"
	"github.com/go-gl/glh"
)

var (
	vertShader = glh.Shader{
		gl.VERTEX_SHADER,
		`
		#version 100

		precision mediump float;

		attribute vec4 position;
		attribute vec4 color;

		uniform float loopDuration;
		uniform float time;

		varying vec4 theColor;

		void main()
		{
			float loopPos = mod(time, loopDuration) / loopDuration;
			float fullCircle = 2.0f * 3.14159f;

			vec4 totalOffset = vec4(
				0.5f * cos(loopPos * fullCircle),
				0.5f * sin(loopPos * fullCircle),
				0.0f,
				0.0f);

			gl_Position = position + totalOffset;
			theColor = color;
		}
		`,
	}

	fragShader = glh.Shader{
		gl.FRAGMENT_SHADER,
		`
		#version 100

		precision mediump float;

		varying vec4 theColor;

		void main()
		{
			gl_FragColor = theColor;
		}
		`,
	}
)


text:
package compare

import (
	"testing"

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"

	"github.com/cloudfoundry-incubator/spiff/yaml"
)

func Test(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Diffing")
}

func parseYAML(source string) yaml.Node {
	return parseYAMLFrom(source, "compare test")
}

func parseYAMLFrom(source string, name string) yaml.Node {
	parsed, err := yaml.Parse(name, []byte(source))
	if err != nil {
		panic(err)
	}

	return parsed
}


text:
from .. import db

from sqlalchemy import desc

class CsvStorage(db.Model):
    """ General CSV Storage container encompassing one CSV """
    __tablename__ = 'csv_storages'
    id = db.Column(db.Integer, primary_key=True)
    date_uploaded = db.Column(db.DateTime)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    action = db.Column(db.String, default='reset') # or 'update'
    csv_rows = db.relationship('CsvRow', backref='csv_storage',
                               uselist=True, cascade='delete, delete-orphan')
    csv_descriptors = db.relationship('CsvDescriptor', backref='csv_storage',
                                      uselist=True, cascade='delete, delete-orphan')
    csv_descriptors_remove = db.relationship('CsvDescriptorRemove', backref='csv_storage',
                                             uselist=True, cascade='delete, delete-orphan')

    """ Find option values of option descriptors in the CSV """
    def set_desc_values(self):
        all_descs = self.csv_descriptors
        opt_descs = dict([(d.name, d) for d in all_descs if d.descriptor_type == 'option'])
        for row in self.csv_rows:
            for key in row.data:
                if key and key != 'Name' and key != 'Address' and key in opt_descs:
                    val = row.data[key]
                    if val:
                        desc = opt_descs[key]
                        values = set()
                        if desc.values:
                            values = desc.values
                        val = val.split(';')
                        for v in val:
                            if v.strip():
                                values.add(v.strip())
                        desc.values = values
                        db.session.add(desc)
        db.session.commit()

    @staticmethod
    def most_recent(user):
        return CsvStorage.query.filter_by(user=user).order_by(
            desc(CsvStorage.date_uploaded)
        ).limit(1).first()

class CsvRow(db.Model):
    """ Representation of a row in a CSV
    - data field is a dictionary of header name to row value
    - can be linked to an existing resource in the app for updates
    """
    __tablename__= 'csv_rows'
    id = db.Column(db.Integer, primary_key=True)
    csv_storage_id = db.Column(db.Integer, db.ForeignKey('csv_storages.id', ondelete='CASCADE'))
    data = db.Column(db.PickleType) # json
    resource_id = db.Column(db.Integer) # no foreign key because could be null

class CsvDescriptor(db.Model):
    """ Representation of a descriptor (header) in a CSV
    - can be linked to an existing descriptor in the app for updates
    - values field will only contain values found from the CSV so if it links to
    an existing descriptor in the app, the existing values won't show up here unless
    also present in the CSV
    """
    __tablename__= 'csv_descriptors'
    id = db.Column(db.Integer, primary_key=True)
    csv_storage_id = db.Column(db.Integer, db.ForeignKey('csv_storages.id', ondelete='CASCADE'))
    name = db.Column(db.String(500))
    descriptor_type = db.Column(db.String, default='text') # or 'option'
    values = db.Column(db.PickleType) # list of string options from CSV ONLY
    descriptor_id = db.Column(db.Integer) # no foreign key because could be null

    def value_string(self):
        if not self.values:
            return ''
        l = list(self.values)
        l.sort()
        return ', '.join(map(str, l))

class CsvDescriptorRemove(db.Model):
    """
    Representation of a descriptor (header) not in the CSV but in the app
    - In updates, if a descriptor is in the app but not in the new CSV then we
    assume deletion of this descriptor
    """
    __tablename__= 'csv_descriptors_remove'
    id = db.Column(db.Integer, primary_key=True)
    csv_storage_id = db.Column(db.Integer, db.ForeignKey('csv_storages.id', ondelete='CASCADE'))
    descriptor_id = db.Column(db.Integer, db.ForeignKey('descriptors.id', ondelete='CASCADE'))
    name = db.Column(db.String(500))

class RequiredOptionDescriptorConstructor(db.Model):
    __tablename__ = 'required_option_descriptor_constructor'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(500), index=True)
    values = db.Column(db.PickleType)
    missing_dict = db.Column(db.PickleType)


text:
using System;
using System.Web.Mvc;
using FluentValidation.Attributes;
using Nop.Admin.Validators.Forums;
using Nop.Web.Framework;
using Nop.Web.Framework.Mvc;

namespace Nop.Admin.Models.Forums
{
    [Validator(typeof(ForumGroupValidator))]
    public partial class ForumGroupModel : BaseNopEntityModel
    {
        [NopResourceDisplayName("Admin.ContentManagement.Forums.ForumGroup.Fields.Name")]
        [AllowHtml]
        public string Name { get; set; }

        [NopResourceDisplayName("Admin.ContentManagement.Forums.ForumGroup.Fields.DisplayOrder")]
        public int DisplayOrder { get; set; }

        [NopResourceDisplayName("Admin.ContentManagement.Forums.ForumGroup.Fields.CreatedOn")]
        public DateTime CreatedOn { get; set; }
    }
}


text:
    ((( |||_| ///\ [[[_ (((
     ))) || |  \\/  [[_  )))
    an artsy any-platform app kit <http://github.com/shoes/shoes>

![blue_shoes.jpg](http://github.com/hacketyhack/blue_shoes/raw/master/static/blue_shoes.jpg)

# About Shoes

Shoes is the best little DSL for cross-platform GUI programming there is. It feels like real Ruby, rather than just another C++ library wrapper. If Gtk or wxWidgets is Rails, Shoes is Sinatra.

# Let me tell you a story about Shoes

  Way way back in the day, there was a guy named \_why. He created a project known as [Hackety Hack](http://hackety-hack.com) to teach programming to everyone. In order to reach all corners of the earth, \_why decided to make Hackety Hack work on Windows, Mac OS X, and Linux. This was a lot of work, and so \_why decided to share his toolkit with the world. Thus, Shoes was born.

Everybody loved Shoes. Many apps were made, and put into [The Shoebox](http://the-shoebox.org/). But, one day, \_why left. In his memory, Team Shoes assembled, and carried on making Shoes. They released Shoes version 3 in the late summer of 2010, but all was not good in Shoe-ville. You see, Shoes was barely held together by little bits of string. Three different platforms was a lot of work! And since \_why wasn't around to help, the code was confusing in places, and there were lots of little problems. Also, new versions of Windows and OSX came out after \_why's mysterious disappearance, and it was hard to test them all.

Therefore, Team Shoes decided upon a grand experiment. Shoes would become many different colored Shoes. And thus, Blue Shoes was born. Alongside it, there are also Green Shoes, built with GTK, and Brown Shoes, on JRuby. The original Shoes became Red Shoes.

But we're not here to talk about all those other Shoes... you care about Blue Shoes! It was started by the new champion of Hackety Hack, Steve Klabnik. Blue Shoes signifies the return of the union from which Shoes was originally born, and is made with Hackety Hack in mind. It's built on top of the QT library, which runs natively on all three platforms.

# Helping out with Blue Shoes

So you're intrigued, eh? Great! Blue Shoes is currently in the very nanescent stages of development, so there's lots of stuff to help with! The first thing you'll need to do is set up an environment.

## Setting up an environment

First of all, you're going to need git. You can get it [here](http://git-scm.com/).

Blue Shoes works with Ruby 1.9.2. I'm using rvm, so to get 1.9.2, just `rvm install 1.9.2`, and wait!

If you don't have rvm, you can get it [here](http://rvm.beginrescueend.com/). If you don't want to use rvm... you should.

## Grab the source, luke

First, make a fork here on GitHub! Just click the fork button.

Next, clone your version:

    $ git clone git@github.com:$YOUR_GITHUB_NAME/blue_shoes.git

This'll make a `blue_shoes` directory, with all the code inside.

Then, grab git-flow: http://github.com/nvie/gitflow

`cd` into the `blue_shoes` directory, and type: '`git flow init`'. Give the default for all of the answers, except for when it asks you about `devel`. Use `development` instead.

## Dependencies

To get qtbindings, first you need qt4 for your platform. Instructions shamelessly stolen from [here](http://github.com/ryanmelt/qtbindings):

Debian Linux
------------
1. The following should get you the packages you need:

    sudo apt-get install build-essential bison autoconf g++ zlib1g-dev libreadline-dev libsqlite3-dev libxslt-dev libxml2-dev libssl-dev libsqlite3-dev curl git-core subversion xorg-dev libgl1-mesa-dev libglu1-mesa-dev

Mac OSX Snow Leopard
-----------------------
1. XCode
2. qt4-mac installed from macports - NOTE: For some reason smokegen does
   not work with the SDK or Cocoa libraries directly from qt.nokia.com -
   Uninstall these if necessary using:
   sudo python /Developer/Tools/uninstall_qt.py

Windows - Note: Only necessary for debugging (binary gem available)
--------
1. mingw from the Qt SDK in your path: ie C:\Qt\2010.04\mingw\bin

## Install the gem

Then you just `gem install qtbindings`, and off you go!

## Use Bundler

Bundler is gaining a lot of steam as a dependency management tool. So we're using it. To build your bundle, just type

    $ bundle install

and everything should get built.

# What works

Right now, very simple apps work. Check out the 'samples' directory for some examples.

# Stubs

All methods and classes should be stubbed out. I went through the manual, and I think I got everything. Some classes have nothing in them, though, so I'm sure I'm missing some things! This was done in like 10 hours, with 6 of them being sleep...


text:
<?php

	class User extends DataMapper
	{
		/* DataMapper specific members below*/
		var $table = 'users';
		var $has_one = array();
		var $has_many = array(
            "alert",
            "group"	=> array(
				'join_table' => 'link_groups_users',
				'join_self_as' => 'user',
				'join_other_as' => 'group',
			),
        );

		/* User specific members below*/

		private $session;
		private static $s_session;
        private $user_model;
        public $groups     = array();
        private $user_data;
        private $alerts;
        private $in_edit_mode;
        public $verified = '';

        public function __construct($id = NULL)
        {
           parent::__construct($id);
           EventManager::fire("builderengine_user_construct", $this);

        }
        function _init(&$session)
        {
            $in_edit_mode = false;
            $this->session = self::$s_session = &$session;

            if($this->is_logged_in()){
                $this->initialize($this->get_id());
            }
            else
            {
                $this->ci_load->model('group');
            	$group = new Group();
                // $group =  $this->group;
            	$group->where('name', 'Guests')->get();
                array_push($this->groups, $group->id);
            }

        }

        function initialize($user_id)
        {
            $this->get_by_id($user_id);
        	foreach($this->group->get() as $group)
        	{
        		array_push($this->groups, $group->id);
        	}
            $this->session->set_userdata('user_id', (int)$user_id);
            $this->register_activity();
            $this->load_alerts();
        }
        public function get_group_ids()
        {
        	return $this->groups;
        }
        function load_alerts()
        {
        }
        function logout($redirect = "")
        {
            $this->session->sess_destroy();
            if($redirect != "")
                redirect($redirect, "location");
            else
                redirect(base_url("/admin/main/login"), "location");

        }
        function get_id()
        {
            return $this->session->userdata('user_id');
        }
        function get_username()
        {
            return $this->username;
        }
        function is_logged_in()
        {
            return $this->session->userdata('user_id') > 0;
        }

        function is_guest()
        {
            return $this->session->userdata('user_id') == 0;
        }
        function register_activity()
        {
        	$this->last_activity = time();
        	$this->save();
        }
        function is_member_of_any($groups)
        {

            foreach($groups as $entry)
            {
                if($this->is_member_of($entry))
                    return true;
            }

            return false;
        }

        function is_member_of($group_id)
        {
            // TODO: check if other than current user is member of some group
            $int_val = intval($group_id);
            if(is_string($group_id)){
                $group = new Group();
                $group->where('name', $group_id)->get();
                $group_id = $group->id;
            }

            foreach($this->groups as $entry)
            {
                if($entry == $group_id)
                    return true;
            }

            return false;
        }

        function require_group($group_name, $login_url = "/admin/main/login")
        {
             if($this->is_guest())
                redirect($login_url, 'location');

            $group = new Group();

            $group_id = $group->get_by_name($group_name)->id;

             if(!$this->is_member_of((int)$group_id)){ //PHP, why aren't you strong typed.......
                redirect('/', 'location');
                die();
            }
        }
        function is_verified()
        {
            if($this->get_id)
                if($this->verified == ''){
                    $user = $this->where('id',$this->get_id())->get();
                    $this->verified = $user->all->verified;
                }
            else
                redirect("/", 'location');
            if($this->verified != 'yes')
                redirect('/', 'location');
        }
        function require_login()
        {
            if($this->is_guest())
                redirect('/admin/main/login', 'location');
        }

        function notify($type, $message)
        {
            $notification = array(
                'type'      => $type,
                'message'   => $message
            );
            $notifications = array();
            array_push($notifications,$notification);
            $this->session->set_userdata('notifications', $notifications);
            return $this;
        }

        function editor_mode($bool)
        {
            $this->session->set_userdata('edit_mode', $bool);
            return $this;
        }
        function is_editor_mode()
        {
            return $this->session->userdata('edit_mode') == true;
        }
        function alert($text, $url, $icon, $tag)
        {
            // $alert = new Alert();
            $ci =& get_instance();
            $alert = $ci->load->model('alert');
            $alert->where("user_id", $this->id)->where("text", $text)->where("url", $url)->get();
            $alert->text = $text;
            $alert->url = $url;
            $alert->icon = $icon;
            $alert->tag = $tag;
            $alert->save();
            $this->save_alert($alert);
            //$this->user_model->add_alert($this->get_id(), $text, $url, $icon, $tag);
            $this->load_alerts();
            return $this;
        }
        function get_notifications()
        {
            $notifications = $this->session->userdata('notifications');
            $this->session->unset_userdata('notifications');
            if(is_array($notifications))
                return $notifications;
            else
                return array();
        }

        function get_alerts()
        {
            return $this->alerts;
        }

        function set_session_data($key, $value)
        {
            $this->session->set_userdata($key, $value);
            return $this;
        }

        function get_session_data($key)
        {
            return $this->session->userdata($key);
        }

        function can_access_module($module, $access_type)
        {

            foreach($this->groups as $user_group){
                foreach($module->group_permission->get() as $permission)
                {
                    if($user_group == $permission->group->get()->id && $permission->access == $access_type)
                        return true;
                }
            }
            return false;
        }

        function login($login, $password)
        {
            $this->group_start()->where('username', $login)->or_where('email', $login)->group_end()->where('password', md5($password))->get();

            if(!empty($this->id)){
                $this->initialize($this->id);
                $this->notify('success', "Login successful!");
                return true;
            }else
                return false;
        }

        function register()
        {
            $this->date_registered = time();
            $this->save();
            $this->groups = array();
            $this->set_member_of("Members");

            return $this;
        }

        function set_member_of($group_id)
        {
            $int_val = intval($group_id);
            if(is_string($group_id)){
                $group = new Group();
                $group->where('name', $group_id)->get();
                $this->save_group($group);
            }else
            {
                $group = new Group($group_id);
                $this->user->save_group($group);
            }

            return $this;
        }

        function get_avatar()
        {
            if($this->avatar == "" || !file_exists(APPPATH."../".$this->avatar))
                return base_url("/themes/dashboard/images/avatars/no_avatar.jpg");
            else
                return base_url($this->avatar);
        }
        function upload_file($input_name, $url, $image_name)
        {
            $config['upload_path']          = $url;
            $config['file_type']            = 'image/jpeg';
            $config['allowed_types']        = 'gif|jpg|png';
            $config['file_name']            = $image_name;
            $config['max_size']             = 1000000000;
            $config['max_width']            = 1024000000;
            $config['max_height']           = 76800000;

            $ci = & get_instance();
            $ci->load->library('upload', $config);

            $ci->upload->do_upload($input_name);
        }
	}
?>


text:
<!DOCTYPE html>

    <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Hugo 0.30.2" />

<title>Drawing A Line in the Randomness: General Linear Models and Simulations - Let's See What Happens When We Take Away The Puppy</title>
<meta property="og:title" content="Drawing A Line in the Randomness: General Linear Models and Simulations - Let's See What Happens When We Take Away The Puppy">

<link href='//cdn.bootcss.com/highlight.js/9.11.0/styles/github.min.css' rel='stylesheet' type='text/css' />

<link rel="stylesheet" href="/oliverclark.github.io/css/fonts.css" media="all">
<link rel="stylesheet" href="/oliverclark.github.io/css/main.css" media="all">

About

GitHub

OSF

Twitter

    11 min read

Drawing A Line in the Randomness: General Linear Models and Simulations

    2018/06/20

Introduction

Chosing parameters

General Linear Model

Reverse Engineering

Running Analyses (Welch's t Test)

Analysis (ANOVA)

Power Analysis

Brute Force Power Analysis

Introduction

I am currently planning a replication and extension of an experimental study that had a count-based dependent variable. The aim is to submit it as a registered report before collecting the data and so I need to have an analysis plan and estimated sample size/stopping rule.

One of the ways that this can be achieved is by simulating the data that you expect to see if your hypothesis is correct. This was suggested by Stan Lazic at the Cumberland Lodge Reproducibility Retreat this year and simulation was covered also by Dorothy Bishop and Daniel Lakens.

In essence, the point of simulation-based power analysis is to bash out a huge number of 'replications' by drawing random numbers that represent the participants in your study. You then count the number of instances where p < 0.051.

There are a number of ways in which this can be achieved, but the one I found most intuitive was reverse engineering the general linear model equation.

The following text and code will cover the following:

Deciding on baseline values.

Thinking about how manipulations will affect these values.

Using these two ideals and the equation for a straight line to represent sets of idealised participants.

Run analyses on these imaginary participants.

Repeat the above thousands of times and calculate the power of your study.

Show how sample size affects power.

Chosing parameters

Think of task. It can be anything. If you have choice paralysis let's say reaction time on an attentional task. If you were to take 1000 participants and test them on this task without changing anything, what would you expect the mean reaction time to be? Let us for now say 900ms \(\pm\) 100ms.

How imagine that we take 500 of these participants and load them with some sort of depressant. Let's say 1000ml of finest Diamond White Cider2.

How do you think these participants will respond? Do you think they'll get slower? Perhaps their reaction time will increase by 200ms, so the mean in this group is 900ms 100ms.

You have just made assumptions that can be represented by the general linear model.

General Linear Model

The form of a general linear model is \(y = \alpha + \beta_{1}x_{1} + \beta_{2}x_{2}...+\beta_{n}x_{n} + \epsilon\)3

Beta is the amount of change in y for any value of x. In our above example that is the difference between the two conditions - in this case 200ms. Alpha is the value of y when all beta's are 0. So 900ms in the group who are not drinking horrible cider. Epsilon accounts for the fact that we are not dealing with machines but random data and is just noise added to the model representing unexplained variation.

You can have as many betas as you like in your equation (or as many as is meaningful) but they are meaningless without something (x) to multiply them by.

X can refer to conditions in an experiment (0 = control (no nasty booze), and 1 = experimental (too much nasty booze)) or some scale that you predict will alter y by a certain amount (average alcohol consumption?).

So applying this to our chosen values above, this can be nicely represented by the model above such that \(y_{nothing} = 900 + 200*0 + \epsilon\) or just the base score with error. Making x = 1 means that 200 is added to y.

Thom Bagueley4 describes the lefthand side of the equation as the random element and the righthand side the systematic element. That is, y depends on both the random attributes of the participant and the random assignment within in the study whereas the righthand side of the equation entirely describes y.

One of the general goals of regression analysis is to estimate each \(\beta_i\) given \(X_i\) and \(y_i\)5.

Reverse Engineering

When reading around how to simulate data I stumbled across a StackOverflow post that suggests reverse engineering this equation to simulate the data. That is, rather than estimating \(\beta_{i}\) given y and x, you calculate y given a set of \(\beta\)'s and x and adding some random error to it.

This approach helped a lot, since before that I was relying on example of full factorial designs using matrix multiplication^[I had no idea what this was or how to do it - this link was really valuable if you are interested.

Lets do an example in R:

#set parameters

a = 900 # Value at intercept when X = 0
b1 = 200 # The about that X is multiplied by

#create a vector of conditions

x <- c(1) #X is one here - if it was 0 then it would be an intercept only model/control condition

#calculate a single observation of y

y <- a + b1*x[1]

This creates a value of y that is 900 + 200 = 1100ms. Of course, because we're dealing with randomness, the parameters will not be perfectly reflected when collecting data from participants, so we add some random noise to y. This is taken from a normal distribution with a mean of 0 and a standard deviations of 100ms:

y <- a + b1*x[1] + rnorm(1,0,100)
y

## [1] 1045.848

In this observation our simulated participant was only exposed to one condition.

We can expand this to an entire set of participants easily by just changing a few of the lines that we already have:

a = 900
b1 = 200

# create a vector of conditions for 100 participants

x <- rep(c(1,0),50)

y <- a + b1 * x + rnorm (100,0,100) #compute y for 100 people with normal error

paste("Mean of Control Group =", round(mean(y[x==0]), digits = 2))

## [1] "Mean of Control Group = 894.76"

paste("Mean of Cider Group =", round(mean(y[x==1]), digits = 2))

## [1] "Mean of Cider Group = 1102.37"

You can explore what is going on here by typing the individual variable names into the R console.

Running Analyses (Welch's t Test)

We now have 100 participants drawn from the control condition (intercept only) and an experimental condition (mean difference = 2).

To keep things neat, we can create a dataframe to store all of the variables in one place:

dat <- as.data.frame(cbind(y,x,pp = 1:100))

We can then run a t-test on this equation to see if there is a significant difference:

t.test(y ~ x, data = dat)

##
##  Welch Two Sample t-test
##
## data:  y by x
## t = -10.578, df = 97.955, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -246.5668 -168.6684
## sample estimates:
## mean in group 0 mean in group 1
##        894.7558       1102.3734

As you can see the mean in group 0 is close to 900 and the mean in group1 is close to 200 which is what we coded our betas as.

Analysis (ANOVA)

This can easily be extended to multiple variables and then analysed using Analysis of Variance. Perhaps we give a second group a similar dose of Buckfast6:

a = 900
b1 = 200
b2 = 500

#three groups, 33 a piece

grp1 <- c(rep(0,33),rep(1,33),rep(0,33))
grp2 <- c(rep(0,66),rep(1,33))

y <- a + b1*grp1 + b2 * grp2 + rnorm(99,0,100)

dat <- as.data.frame(cbind(y,grp1,grp2,pp = 1:99))

summary(aov(y ~ grp1 * grp2, data = dat))

##             Df  Sum Sq Mean Sq F value  Pr(>F)
## grp1         1   92388   92388   10.54 0.00161 **
## grp2         1 3654546 3654546  417.02 < 2e-16 ***
## Residuals   96  841300    8764
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

If you run a regression analysis on the same data you will find the values that we set are retrieved:

summary(lm(y~grp1 * grp2, data = dat))

##
## Call:
## lm(formula = y ~ grp1 * grp2, data = dat)
##
## Residuals:
##      Min       1Q   Median       3Q      Max
## -205.538  -74.133    3.824   65.680  170.708
##
## Coefficients: (1 not defined because of singularities)
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)   924.07      16.30  56.705  < 2e-16 ***
## grp1          170.51      23.05   7.399  5.2e-11 ***
## grp2          470.62      23.05  20.421  < 2e-16 ***
## grp1:grp2         NA         NA      NA       NA
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 93.61 on 96 degrees of freedom
## Multiple R-squared:  0.8166, Adjusted R-squared:  0.8128
## F-statistic: 213.8 on 2 and 96 DF,  p-value: < 2.2e-16

Power Analysis

I wont go too far into what Power Analysis is, other than that it tells you the probability of you finding an effect, if there is one, given three parameters:

Sample size.

Effect Size.

Type 1 error rate (how willing you are to find an effect when there isn't one).

Typically it is used for sample size estimation using software like G*Power and you would input the smallest effect size that you would deem to be meaningful, your type one error rate (usually 0.05, but see the link above to find out how to justify your this value) and how willing you are to miss an effect if there is one (type two error rate).

Another way to run a power analysis that doesn't necessarily require you to know how to compute Cohen's \(F^2\) is to run simulations as we have above. This way we have to think about what change from the intercept value we think is meaningful. It also means that we have to know how to analyse our data before we collect it because as we have seen, the same model (GLM) is used to both generate fake samples and analyse the real data.

Brute Force Power Analysis

Using the methods above, you can run the simulation thousands of times and find out what power your model has:

#Create an empty dataframe
P <- data.frame()
#Set a loop to repeat the code in Curley brackets ten thousand times

for(i in 1:1e4){

a = 900
b1 = 200
b2 = 500

#three groups, 33 a piece

grp1 <- c(rep(0,33),rep(1,33),rep(0,33))
grp2 <- c(rep(0,66),rep(1,33))

y <- a + b1*grp1 + b2 * grp2 + rnorm(99,0,100)

dat <- as.data.frame(cbind(y,grp1,grp2,pp = 1:99))

#The right side of this looks horrible but it is just an index to the p values in the summary object

P <- rbind(P,P<-summary(aov(y ~ grp1 * grp2, data = dat))[[1]][["Pr(>F)"]][[1]][[1]])

}
#Calculate the power
Power <- length(P[P<0.05])/1e4*100

So given the provided mean differences, sample size and number of groups, this simulated study had a power of 63.85.

To explore this further you can create a function and pass different sample sizes into the model. So, below we start with 9 participants and then gradually work up to over 300:

Power_Calc <- function(n){
a = 900
b1 = 200
b2 = 500

#three groups, n/3 a piece

grp1 <- c(rep(0,n/3),rep(1,n/3),rep(0,n/3))
grp2 <- c(rep(0,(n/3*2)),rep(1,n/3))

y <- a + b1*grp1 + b2 * grp2 + rnorm(n,0,100)

dat <- as.data.frame(cbind(y,grp1,grp2,pp = 1:n))

P <- summary(aov(y ~ grp1 * grp2, data = dat))[[1]][["Pr(>F)"]][[1]][[1]]

return(P)
}

Power <- data.frame() # Empty dataframe
n = 9 # starting value of 9 participants)

for(i in 1:100){
Power <- rbind(Power,cbind(Power_Calc(n),n))
n = n+3 #Keeping equal groups
}

colnames(Power) <- c("P_Value", "Participants")
plot(P_Value~Participants, data = Power)

We can then plot the data, as above, to show what p-values you can expect for different sample sizes. It is also a good demonstration of how even though we know there is a mean difference of between 200-500, you can still get values way above 0.05 even with a lot of participants. Try running the code a few times and playing with the different beta values.

This is by no means the best or most efficient method for running power analysis using simulation (I would suggest working through the documents on the OSF page referenced above to see some more elegant methods), but hopefully it seems intuitive enough to get started.

In the following weeks I intend to expand on this method, demonstrating how you can use it to run generalised linear models such as count and logistic regression studies. Since I am quite in to Bayesian statistics and the purpose of me doing this analysis was to plan a Bayesian study, I may throw in some of that too.

Thanks for reading!

or whichever alpha you choose↩

This is what many UK teenagers first discover hangovers with^ [My first draft of this mentioned White Lightening which was notorious when I was at school - but I have since found that it was discontinued in 2009]↩

I am using Greek letters below because this is the terminology used in regression equations.↩

I recently bought his book Serious Stats and it is really accessible given the content - definitely one to buy if you're ready to get serious with your statistics!↩

oughta!↩

this one is more for the students and those north of Hadrian's wall↩

  Please enable JavaScript to view the comments powered by Disqus.

            RSS feed

            Made with