Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Problems accesing BatchSessionData

edited March 21 in JATOS

Dear JATOS community,

I am encountering issues with accessing JATOS Batch Session Data. For context, I am using an HTML script with jsPsych to run an online task, and my university uses JATOS Server (Version 3.5.11).

I need to assign a unique participant ID to each person entering the experiment and track this information efficiently to avoid repeating IDs. I thought I could solve this by storing the available participant IDs, temporarily taken participant IDs, and fully taken participant IDs in the JATOS Batch Session data.My plan was to check if a list called “assignedIDs” exists in Batch Session. If it’s not defined (i.e., the first participant), I would initialize the list from a file called “sub_list”. If it’s already defined, I would use the list in Batch Session.Then, I would randomly select an ID from the “free” list, move it to the “temporary taken” list, and update the Batch Session data. This ID would then be used to load the study’s design matrices (via “loadDesignMatrices”).

The problem is that the code is not saving the information in the Batch Session data, so it cannot be accessed when a second participant runs the task. I understand that I might be mixing synchronous and asynchronous functions, but I need to obtain the design matrices at that exact moment to ensure the rest of the experiment runs smoothly (all the experiment depends on having the design_matrices of that partcipant loaded).

Below, I’ve included the relevant functions. If anyone has any suggestions on how to resolve this issue, I would be very grateful.


Thanks,

Paula


// load design matrices
function loadDesignMatrices(subject_id) {
  var key = "sub_" + subject_id;
  console.log("Loading design matrices for key:", key);
  design_matrix = DM[0][key]; // Store in global variable
  pr_design_matrix = pr_DM[0][key]; // Store in global variable
  console.log("Design matrix loaded:", design_matrix);
  console.log("Practice design matrix loaded:", pr_design_matrix);
}

// Assign random subject id from the available ids
function assign_subject() {
  console.log("Starting assign_subject_id...");
  let assignedIDs;
  if (jatos.batchSession.get("assignedIDs") === undefined) {
    assignedIDs = {
      free: [...sub_list[0].free],
      temp: Array.isArray(sub_list[0].temp) ? [...sub_list[0].temp] : [],
      taken: Array.isArray(sub_list[0].taken) ? [...sub_list[0].taken] : []
    };
  } else {
    console.log(jatos.batchSession)
    // Retrieve assignedIDs from batch session using .then()
    jatos.batchSession.get("assignedIDs")
      .then(fetchedAssignedIDs => {
        assignedIDs = fetchedAssignedIDs;
      })
  }
  console.log("trying", assignedIDs)

  if (assignedIDs.free.length > 0) {
    var randomIndex = Math.floor(Math.random() * assignedIDs.free.length);
    var subject_id = assignedIDs.free.splice(randomIndex, 1)[0]; // Remove from free list

    assignedIDs.temp.push(subject_id); // Move to temp
    console.log("Randomly moved subject ID to temp:", subject_id);

    // Load design matrices according to ID
    loadDesignMatrices(subject_id);

    // Return necessary data
    return { design_matrix, pr_design_matrix, subject_id };

    // Save updated assignedIDs to batch session using .then() and .catch()
    jatos.batchSession.set("assignedIDs", assignedIDs)
      .then(() => {
        console.log("Updated assignedIDs in batch session:", assignedIDs);
      })
      .catch(err => {
        console.error("Error updating batch session data:", err);
      });
  } else {
    console.log("No available subject IDs in free.");
  }
}

// Call function
assign_subject(); 


Comments

  • Hi Paula!

    I formatted your code to make it better readable. I'd like to encourage everyone to format their code too.

    First, but maybe you do this already, you can access `jatos.batchSession` only after the jatos.js got initialized. So you have to use `jatos.onLoad` to do that.

    Then, when I look at your code, the last part where you set something in the batch session is never executed because you return before. I'd move the return statement inside the `jatos.batchSession.set`, the part where you handle the successful handing with then . Additional I'd do a recursive call to assign_subject in case it fails. All together it could look like this (but I never tested the code):

    // load design matrices
    function loadDesignMatrices(subject_id) {
      var key = "sub_" + subject_id;
      console.log("Loading design matrices for key:", key);
      design_matrix = DM[0][key]; // Store in global variable
      pr_design_matrix = pr_DM[0][key]; // Store in global variable
      console.log("Design matrix loaded:", design_matrix);
      console.log("Practice design matrix loaded:", pr_design_matrix);
    }
    
    // Assign random subject id from the available ids
    function assign_subject() {
      console.log("Starting assign_subject_id...");
      let assignedIDs;
      if (jatos.batchSession.get("assignedIDs") === undefined) {
        assignedIDs = {
          free: [...sub_list[0].free],
          temp: Array.isArray(sub_list[0].temp) ? [...sub_list[0].temp] : [],
          taken: Array.isArray(sub_list[0].taken) ? [...sub_list[0].taken] : []
        };
      } else {
        console.log(jatos.batchSession)
        // Retrieve assignedIDs from batch session using .then()
        jatos.batchSession.get("assignedIDs")
          .then(fetchedAssignedIDs => {
            assignedIDs = fetchedAssignedIDs;
          })
      }
      console.log("trying", assignedIDs)
    
      if (assignedIDs.free.length > 0) {
        var randomIndex = Math.floor(Math.random() * assignedIDs.free.length);
        var subject_id = assignedIDs.free.splice(randomIndex, 1)[0]; // Remove from free list
    
        assignedIDs.temp.push(subject_id); // Move to temp
        console.log("Randomly moved subject ID to temp:", subject_id);
    
        // Load design matrices according to ID
        loadDesignMatrices(subject_id);
    
        // Save updated assignedIDs to batch session using .then() and .catch()
        jatos.batchSession.set("assignedIDs", assignedIDs)
        .then(() => {
          console.log("Updated assignedIDs in batch session:", assignedIDs);
          // Return necessary data
          return { design_matrix, pr_design_matrix, subject_id };
          })
          .catch(err => {
            console.error("Error updating batch session data:", err);
            // Recursively call this function until we get the necessary data
            return assign_subject();
          });
      } else {
        console.log("No available subject IDs in free.");
      }
    }
    
    // Call function
    assign_subject(); 
    

    I hope this helps.

    Best,

    Kristian

Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu http://45.77.173.118/ Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games