Creating pre-screening tasks
****************************

Put custom pre-screening tasks in your experiment, not in the PsyNet
package.


A single question
=================

A one-question pre-screening task is a "Module" that asks the question
and uses "conditional()" to send failing participants to
"UnsuccessfulEndPage":

   from psynet.modular_page import ModularPage, PushButtonControl
   from psynet.page import UnsuccessfulEndPage
   from psynet.timeline import Module, conditional


   def hearing_impairment_check():
       return Module(
           "hearing_impairment_check",
           ModularPage(
               "hearing_impairment",
               "Do you have any kind of hearing impairment?",
               PushButtonControl(["Yes", "No"]),
               time_estimate=3,
           ),
           conditional(
               "fail_if_impaired",
               lambda participant: participant.answer == "Yes",
               UnsuccessfulEndPage(failure_tags=["hearing_impairment_check"]),
           ),
       )

Place "hearing_impairment_check()" in the timeline before the main
task. With a "TextControl", evaluate the typed answer in the
"conditional" in the same way.


Several scored trials
=====================

A test with several trials is a "StaticTrialMaker" with
"check_performance_at_end=True". Each trial implements
"score_answer()". With "performance_check_type = "score"", the
participant passes if the sum of the trial scores reaches
"performance_threshold", and a participant who fails is sent to an
"UnsuccessfulEndPage".

"ColorBlindnessTest" is built this way. Its trial shows an image and
scores the typed number:

   class ColorBlindnessTrial(StaticTrial):
       def show_trial(self, experiment, participant):
           return ModularPage(
               "color_blindness_trial",
               ImagePrompt(
                   self.assets["image"].url,
                   _p("color_blindness_test", "Write down the number in the image."),
                   width="350px",
                   height="344px",
                   hide_after=self.trial_maker.hide_after,
                   margin_bottom="15px",
                   text_align="center",
               ),
               TextControl(width="100px"),
               bot_response=lambda: self.definition["correct_answer"],
           )

       def score_answer(self, answer, definition):
           if answer == definition["correct_answer"]:
               return 1
           else:
               return 0

The trial maker defines one "StaticNode" per image, and passes the
threshold and trial count on to "StaticTrialMaker":

       def get_nodes(self, media_url: str):
           return [
               StaticNode(
                   definition={
                       "label": label,
                       "correct_answer": answer,
                   },
                   assets={
                       "image": _opaque_media_asset(f"{media_url}/ishihara-{label}.jpg")
                   },
               )
               for label, answer in [
                   ("1", "12"),
                   ("2", "8"),
                   ("3", "29"),
                   ("4", "5"),
                   ("5", "3"),
                   ("6", "15"),
               ]
           ]

The images are hosted on S3 under file names that reveal the correct
answer, either directly or when looked up in PsyNet's public source
code (here, the plate numbers), and participants can read stimulus
URLs in the page source. The built-in test therefore wraps each URL
with the internal helper "_opaque_media_asset", which serves the image
through PsyNet under an opaque "/asset/<access_token>" URL. This
helper is internal for now, so don't import it in your own
experiments. In your own pre-screen, give stimulus files neutral names
that don't reveal the answer, or add local files with "asset()" or
"FileAsset", whose URLs don't contain the file name. Passing an
http(s) URL to "asset()" creates an "ExternalAsset", which keeps the
original URL.

       def __init__(
           self,
           label="color_blindness_test",
           media_url: str = "https://s3.amazonaws.com/ishihara-eye-test/jpg",
           time_estimate_per_trial: float = 5.0,
           performance_threshold: int = 4,
           hide_after: Optional[float] = 3.0,
           trial_class=ColorBlindnessTrial,
       ):
           self.hide_after = hide_after
           self.time_estimate_per_trial = time_estimate_per_trial
           self.performance_threshold = performance_threshold

           nodes = self.get_nodes(media_url)

           super().__init__(
               id_=label,
               trial_class=trial_class,
               nodes=nodes,
               expected_trials_per_participant=len(nodes),
               check_performance_at_end=True,
           )

The class also sets "performance_check_type = "score"" and defines an
"introduction" property that returns the instructions page.

If the test only decides whether the participant may continue, set
"fail_trials_on_participant_performance_check=False" so that the
trials of participants who fail stay valid. The default for static
trial makers is "True".
