a-hurst / klibs

A simple Python framework for writing cognitive psychology experiments
7 stars 1 forks source link

Saving data to multiple tables #34

Closed madi011 closed 7 months ago

madi011 commented 8 months ago

I have managed to create multiple tables in the schema.sql which works well and those tables exist within the database and can be exported without issue. However, only the trials table actually populates with data. The additional tables have the correct headers but no data.

I've added the secondary table to the params.py with the title corresponding to the method I created for it. Within the trials method, I call the secondary method within which the appropriate dictionary should be returned.

I can't figure out how to return the secondary dictionary of variables/data to have it linked to a secondary table. Or perhaps, it is an issue with the collection of the data and not writing it to the table.

a-hurst commented 8 months ago

Hi @madi011,

Thanks for opening the issue! This is definitely a part of klibs that should be better documented.

I'm a little unclear what you're asking, though: is the problem with writing data to secondary tables in the database, or with exporting data from secondary tables into text files? If it's the former, take a look at this code from a recent project:

https://github.com/a-hurst/MotorMapping/blob/318ea9f871a9a386ce95d4fafc945c331844c8a7/experiment.py#L48-L54

        if P.collect_kviq:
            kviq = KVIQ(handedness == "l")
            responses = kviq.run()
            for movement, dat in responses.items():
                dat['participant_id'] = P.participant_id
                dat['movement'] = movement
                self.db.insert(dat, table='kviq')

For that task, there's a series of rating scales that participants complete at the start of the task and it's saved to a separate table in the database ('kviq'). The main thing you might be missing is the call to self.db.insert() on the last line: that's the code that takes a dictionary of data with a key/value for each column (dat, in this case) and inserts it into a specific table. The dictionary returned at the end of klibs' trial method only ever gets inserted into the main trials database unless you change the primary table in the params file.

Afterwards, once the data's in the database, you can export it separately using klibs export -t kviq (replacing kviq with the name of the secondary table you want to export).

Hope that helps!

madi011 commented 8 months ago

Thank you!

My issue was with writing the data to the database and is now resolved using self.db.insert().

Thank you for the quick reply!