Exemple #1
0
    def value(self) -> 'simulation.GenMenus':
        assert self.alt_count is not None

        generator: simulation.MenuGenerator
        defaults: bool

        if self.rbExhaustive.isChecked():
            generator = simulation.Exhaustive()

        elif self.rbSampleWithReplacement.isChecked():
            generator = simulation.SampleWithReplacement(
                menu_count=self.sbSampleWithReplacementCount.value(), )

        elif self.rbBinary.isChecked():
            generator = simulation.Binary()

        else:
            raise gui.ValidationError('please select a choice distribution')

        if self.cbDefault.currentIndex() == 0:
            defaults = False
        elif self.cbDefault.currentIndex() == 1:
            defaults = True
        else:
            raise gui.ValidationError('unrecognised choice of defaults')

        return simulation.GenMenus(
            generator=generator,
            defaults=defaults,
        )
def _simulation_gen():
    f_in = io.BytesIO()
    f_out = io.BytesIO()
    with Core(f_tee=(f_in, f_out)) as core:
        response = simulation.run(
            core,
            simulation.Request(
                name='random',
                alternatives=['A', 'B', 'C', 'D', 'E'],
                gen_menus=simulation.GenMenus(
                    generator=simulation.Exhaustive(),
                    defaults=False,
                ),
                gen_choices=simulation.Uniform(
                    forced_choice=True,
                    multiple_choice=False,
                ),
                preserve_deferrals=False,
            ))

    with open('in.bin', 'wb') as f:
        f.write(f_in.getbuffer())

    with open('out.bin', 'wb') as f:
        f.write(f_out.getbuffer())
Exemple #3
0
def test_simulation(nsubjects=256, f_mock=None):
    with Core(f_mock=f_mock) as core:
        response = simulation.run(
            core,
            simulation.Request(
                name='random',
                alternatives=('A', 'B', 'C', 'D', 'E'),
                gen_menus=simulation.GenMenus(
                    generator=simulation.Exhaustive(),
                    defaults=False,
                ),
                gen_choices=simulation.Uniform(
                    forced_choice=True,
                    multiple_choice=False,
                ),
            ))

    assert len(response.subject_packed) == 223
    def analysis_simulation(
            self, worker: Worker,
            options: 'gui.copycat_simulation.Options') -> 'ExperimentalData':
        subjects: List[PackedSubject] = []

        with Core() as core:
            worker.interrupt = lambda: core.shutdown(
            )  # register interrupt hook

            worker.set_work_size(len(self.subjects) * options.multiplicity)
            position = 0
            for subject_packed in self.subjects:
                for j in range(options.multiplicity):
                    response = simulation.run(
                        core,
                        simulation.Request(
                            name='random%d' % (j + 1),
                            alternatives=self.
                            alternatives,  # we don't use subject.alternatives here
                            gen_menus=simulation.GenMenus(
                                generator=simulation.Copycat(subject_packed),
                                defaults=False,  # this will be ignored, anyway
                            ),
                            gen_choices=options.gen_choices,
                            preserve_deferrals=options.preserve_deferrals,
                        ))

                    subjects.append(response.subject_packed)

                    position += 1
                    if position % 1024 == 0:
                        worker.set_progress(position)

        ds = ExperimentalData(name=options.name,
                              alternatives=self.alternatives)
        ds.subjects = subjects
        ds.observ_count = options.multiplicity * self.observ_count
        return ds