Hi Chris,
I am looking to save my data to different directories for different subjects. I would like to do something like the following:
data_file ("$pwd/${subject_name}/[USERNAME?]-[EXPERIMENT-NAME?]-%Y%m%d-%H%M%S")
where subject name is defined separately.
First, would this be the right approach? And if so, how would you access the username and experiment name here, so that I can retain the default format for the filename?
Thanks for your help,
Hokyung
Hi Hokyung,
Yes, this is the right approach.
The username and experiment name would need to be assigned to variables. For example:
var root_dir = '/Users/cstawarz/Downloads/Hokyung/Data'
var subject_name = 'Bob'
var username = 'Chris'
var experiment_name = 'date_file_demo'
data_file ('$root_dir/$subject_name/$username-$experiment_name-$(date("%Y%m%d-%H%M%S"))')
Since the data file name isn’t evaluated until the experiment starts running, you could change the values of the variables via the variables window or by loading a variable set, and those changes would be reflected in the file name.
Alternatively, you could use expression interpolation (as with date
above) to generate parts of the filename. For example, you might use pyeval
to get the value of the USERNAME
environment variable.
Cheers,
Chris
Thanks Chris!
A quick follow up: I was wondering what would be the best way to access the name of the loaded .mwel file without having to manually define variables for the experiment name – it seems like by default MWorks somehow has access to which .mwel file is loaded and uses it to generate the data file.
Hi Hokyung,
MWorks does keep track of the loaded experiment file internally, but it isn’t available to the experiment itself. Well, it is available via the #loadedExperiment system variable (which you can access from Python), as one of the keys in the dictionary. But that dictionary also contains the paths to all the files that get included (directly or indirectly) by the main experiment file, and you don’t have a way to distinguish the main file from all the others.
I suppose we could add a function that returns the name and/or path of the current experiment, but at the moment, you’ll probably need to define a variable with the name manually.
Cheers,
Chris
I see, thank you for the explanation! It isn’t urgent, but I do think it would be useful to have the function for future versions.
Best,
Hokyung
Hi Chris,
So I implemented the custom paths as you suggested. It seems to work well, except I’m seeing that now a new file gets created every time I stop and run a protocol, or switch to running a different protocol. This is my data_file line, for test runs:
data_file ("test/${filename}-$(platform)-$(date('%Y%m%d-%H%M%S'))")
This seems to happen because every time I run a protocol, the hour/minute/second is different and MWorks evaluates the expression for that time and creates a new data file.
As you know, I am working with a single file that contains multiple protocols that I have to switch between throughout a session. Ideally, I would like to have the data for running different protocols be in a single file corresponding to that session (i.e. from loading a .mwel to closing it), similar to what the default data file does for an experiment. Meanwhile it would be still be useful for me to include the experimental session’s start time in the data filename, as I am planning to have multiple sessions for multiple subjects within a day and a timestamp would help for tracking these runs.
Would there be a way to 1) designate custom paths and filenames for data file, 2) ideally include H/M/S timestamps in the filename, and 3) store multiple runs of different protocols within a session in a single data file? I imagine it would be possible since the default data file naming functionality allows for 2) and 3), but I couldn’t figure out how to do it with custom filenames.
Thank you for your help!
Best,
Hokyung
Hi Hokyung,
It isn’t urgent, but I do think it would be useful to have the function for future versions.
OK, I’ve added it to my to-do list.
This seems to happen because every time I run a protocol, the hour/minute/second is different and MWorks evaluates the expression for that time and creates a new data file.
That’s correct. It works that way by design, so that you can have a new data file for each run of the experiment, if that’s what you want.
Would there be a way to 1) designate custom paths and filenames for data file, 2) ideally include H/M/S timestamps in the filename, and 3) store multiple runs of different protocols within a session in a single data file?
Yes: Store the timestamp in a variable, and reference the variable in the filename expression. For example:
var timestamp = "$(date('%Y%m%d-%H%M%S'))"
data_file ("test/${filename}-${platform}-${timestamp}")
The value of variable timestamp
will be set once, when the experiment loads, and won’t change between protocols. When you start a new protocol, MWorks will see that the requested data file name is identical to the name of the currently-open file and will keep that file open, rather than create a new one. (Again, this is by design.)
Cheers,
Chris
Ah I see. That’s a neat solution. Thank you!
Best,
Hokyung