<commit_before>from psychopy import visual, core, event
from psychopy.visual import Rect, Circle, ImageStim, TextStim
from random import sample, shuffle
from helpers import choose_pair, retrieve_subject_info, get_subject_info
import os, csv


def step(window, transparency, img, frames):
    """
    Performs a single 'step' for the staircase method.

    If a key is pressed, the subject has seen the stimulus and has pressed a
    key, meaning the stimulus must be more transparent in the next presentation
    """

    pressToContinue(window)

    img.setOpacity(transparency)
    img.setAutoDraw(True)

    for frameN in range(60):

        frame_num = frameN//6
        mask_frame = frames[frame_num]

        mask_frame.draw()
        window.flip()

    img.setAutoDraw(False)

    # Mask shows for 100ms longer
    for frameN in range(6):
        frame_num = frameN//6
        mask_frame = frames[frame_num]

        mask_frame.draw()
        window.flip()

    window.flip()

    keys = event.waitKeys(maxWait = 2)

    if keys:
        return -0.02
    else:
        return 0.02


def pressToContinue(window):
    """
    Implements "Press to continue" between trials
    """

    text_1 = TextStim(win = window,
                      text = "Press space to \ncontinue",
                      pos= (200, 150),
                      alignHoriz = 'center',
                      alignVert = 'center')

    text_2 = TextStim(win = window,
                      text = "Press space to \ncontinue",
                      pos = (-200,150),
                      alignHoriz = 'center',
                      alignVert = 'center')

    text_1.draw()
    text_2.draw()

    window.flip()
    event.waitKeys()
    window.flip()


def staircase(window, image, transparency, dominant_eye):
    """
    Performs a single staircase to find the threshold of visibility for a
    subject

    returns the threshold
    """

    if dominant_eye == "True":
        maskPos = 200
    else:
        maskPos = -200
    # Image stuff
    img = ImageStim(window,
                           image = image,
                           color=(1,1,1),
                           size = [130,130],
                           pos = (-1 * maskPos,150),
                           opacity = transparency)

    # Fusion box stuff

    box_1 = Rect(win = window,
                 width = 180,
                 height = 180,
                 lineWidth = 4,
                 lineColor = 'grey',
                 pos = (maskPos, 150),
                 autoDraw = True)

    box_2 = Rect(win = window,
                 width = 180,
                 height = 180,
                 lineWidth = 4,
                 lineColor = 'grey',
                 pos = (-1 * maskPos, 150),
                 autoDraw = True)

    box_1.setAutoDraw(True)
    box_2.setAutoDraw(True)

    # Mask stuff

    frame_paths = ["Masks/" + file for file in os.listdir("Masks")]
    frames = map(lambda file_name: ImageStim(window,
                                                    image = file_name,
                                                    color = (1,1,1),
                                                    size = [150, 150],
                                                    pos = (maskPos,150)), frame_paths)

    #Fixation dot stuff

    fixation_dot_1 = Circle(win = window,
                          radius = 2,
                          fillColor = 'red',
                          pos = (maskPos, 150),
                          lineWidth = 0,
                          autoDraw = True)

    fixation_dot_2 = Circle(win = window,
                          radius = 2,
                          fillColor = 'red',
                          pos = (-1 * maskPos, 150),
                          lineWidth = 0,
                          autoDraw = True)

    for i in range(40):
        transparency += step(window, transparency, img, frames)
        if transparency > 1:
            transparency = 1
        if transparency < 0:
            transparency = 0

    box_1.setAutoDraw = False
    box_2.setAutoDraw = False
    fixation_dot_1.setAutoDraw = False
    fixation_dot_2.setAutoDraw = False

    return transparency

def write_to_csv(new_experiment, subject_number, difficulties, individual_results, first_average, second_average):

    if new_experiment:
        if(os.path.exists('testing_results.csv')):
            os.remove('testing_results.csv')


    data = [subject_number, difficulties, individual_results, first_average, second_average]

    with open('testing_results.csv', 'ab') as f:
        wr = csv.writer(f, quoting=csv.QUOTE_ALL)

        if new_experiment:
            header = ["Subject Number", "Difficulties", "Individual results", "Image 1 Average", "Image 2 Average"]
            wr.writerow(header)

        wr.writerow(data)


def main(window, new_experiment =  True, subject_number = 1):

    subject_info = retrieve_subject_info(subject_number)
    dominant_eye = subject_info[2]
    pair_num = subject_info[3]
    difficulties = (subject_info[6], subject_info[7])

    pair_path = choose_pair(pair_num)
    img_1 = pair_path + "1.png"
    img_2 = pair_path + "2.png"

    result_10 = (window, img_1, 0, dominant_eye)
    result_11 = (window, img_1, 1, dominant_eye)
    result_20 = (window, img_2, 0, dominant_eye)
    result_21 = (window, img_2, 1, dominant_eye)

    results = [result_10, result_11, result_20, result_21]

    shuffle(results)

    for result in results:
        result = staircase(*result)

    img_1_avg = (result_10 + result_11)/2
    img_2_avg = (result_20 + result_21)/2

    individual_results = [result_10, result_11, result_20, result_21]

    write_to_csv(new_experiment, subject_number, difficulties, individual_results, img_1_avg, img_2_avg)

if __name__ == "__main__":
    main()<commit_msg>Fixed random order staircase<commit_after>from psychopy import visual, core, event
from psychopy.visual import Rect, Circle, ImageStim, TextStim
from random import sample, shuffle
from helpers import choose_pair, retrieve_subject_info, get_subject_info
import os, csv


def step(window, transparency, img, frames):
    """
    Performs a single 'step' for the staircase method.

    If a key is pressed, the subject has seen the stimulus and has pressed a
    key, meaning the stimulus must be more transparent in the next presentation
    """

    pressToContinue(window)

    img.setOpacity(transparency)
    img.setAutoDraw(True)

    for frameN in range(60):

        frame_num = frameN//6
        mask_frame = frames[frame_num]

        mask_frame.draw()
        window.flip()

    img.setAutoDraw(False)

    # Mask shows for 100ms longer
    for frameN in range(6):
        frame_num = frameN//6
        mask_frame = frames[frame_num]

        mask_frame.draw()
        window.flip()

    window.flip()

    keys = event.waitKeys(maxWait = 2)

    if keys:
        return -0.02
    else:
        return 0.02


def pressToContinue(window):
    """
    Implements "Press to continue" between trials
    """

    text_1 = TextStim(win = window,
                      text = "Press space to \ncontinue",
                      pos= (200, 150),
                      alignHoriz = 'center',
                      alignVert = 'center')

    text_2 = TextStim(win = window,
                      text = "Press space to \ncontinue",
                      pos = (-200,150),
                      alignHoriz = 'center',
                      alignVert = 'center')

    text_1.draw()
    text_2.draw()

    window.flip()
    event.waitKeys()
    window.flip()


def staircase(window, image, transparency, dominant_eye):
    """
    Performs a single staircase to find the threshold of visibility for a
    subject

    returns the threshold
    """

    if dominant_eye == "True":
        maskPos = 200
    else:
        maskPos = -200
    # Image stuff
    img = ImageStim(window,
                           image = image,
                           color=(1,1,1),
                           size = [130,130],
                           pos = (-1 * maskPos,150),
                           opacity = transparency)

    # Fusion box stuff

    box_1 = Rect(win = window,
                 width = 180,
                 height = 180,
                 lineWidth = 4,
                 lineColor = 'grey',
                 pos = (maskPos, 150),
                 autoDraw = True)

    box_2 = Rect(win = window,
                 width = 180,
                 height = 180,
                 lineWidth = 4,
                 lineColor = 'grey',
                 pos = (-1 * maskPos, 150),
                 autoDraw = True)

    box_1.setAutoDraw(True)
    box_2.setAutoDraw(True)

    # Mask stuff

    frame_paths = ["Masks/" + file for file in os.listdir("Masks")]
    frames = map(lambda file_name: ImageStim(window,
                                                    image = file_name,
                                                    color = (1,1,1),
                                                    size = [150, 150],
                                                    pos = (maskPos,150)), frame_paths)

    #Fixation dot stuff

    fixation_dot_1 = Circle(win = window,
                          radius = 2,
                          fillColor = 'red',
                          pos = (maskPos, 150),
                          lineWidth = 0,
                          autoDraw = True)

    fixation_dot_2 = Circle(win = window,
                          radius = 2,
                          fillColor = 'red',
                          pos = (-1 * maskPos, 150),
                          lineWidth = 0,
                          autoDraw = True)

    for i in range(40):
        transparency += step(window, transparency, img, frames)
        if transparency > 1:
            transparency = 1
        if transparency < 0:
            transparency = 0

    box_1.setAutoDraw = False
    box_2.setAutoDraw = False
    fixation_dot_1.setAutoDraw = False
    fixation_dot_2.setAutoDraw = False

    return transparency

def write_to_csv(new_experiment, subject_number, difficulties, individual_results, first_average, second_average):

    if new_experiment:
        if(os.path.exists('testing_results.csv')):
            os.remove('testing_results.csv')


    data = [subject_number, difficulties, individual_results, first_average, second_average]

    with open('testing_results.csv', 'ab') as f:
        wr = csv.writer(f, quoting=csv.QUOTE_ALL)

        if new_experiment:
            header = ["Subject Number", "Difficulties", "Individual results", "Image 1 Average", "Image 2 Average"]
            wr.writerow(header)

        wr.writerow(data)


def main(window, new_experiment =  True, subject_number = 1):

    subject_info = retrieve_subject_info(subject_number)
    dominant_eye = subject_info[2]
    pair_num = subject_info[3]
    difficulties = (subject_info[6], subject_info[7])

    pair_path = choose_pair(pair_num)
    img_1 = pair_path + "1.png"
    img_2 = pair_path + "2.png"

    result_10 = (window, img_1, 0, dominant_eye)
    result_11 = (window, img_1, 1, dominant_eye)
    result_20 = (window, img_2, 0, dominant_eye)
    result_21 = (window, img_2, 1, dominant_eye)

    results = [result_10, result_11, result_20, result_21]

    shuffle(results)

    for index, result in enumerate(results):
        results[index] = staircase(*result)

    img_1_avg = (result_10 + result_11)/2
    img_2_avg = (result_20 + result_21)/2

    individual_results = [result_10, result_11, result_20, result_21]

    write_to_csv(new_experiment, subject_number, difficulties, individual_results, img_1_avg, img_2_avg)

if __name__ == "__main__":
    main()
{'fraction_non_alphanumeric': 0.09160892994611239, 'fraction_numerical': 0.02856898468907707, 'mean_word_length': 1.7197022563386835, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3888950', 'n_tokens_mistral': 3599, 'n_tokens_neox': 3218, 'n_words': 865}