In this article
You can use the transform command to quickly access and modify your survey data with Python code.
See Managing Data with the Tab Import Script for information on modifying data using external data files.
Note: The transform command is meant to edit real data. Do not modify virtual data using the transform command.
1: Preparing a Python File
To modify your survey data using transform, you first need to create a Python file (e.g. "somefile.py") containing your transformation code. This code can include a variable, marker, and status modifications, and instructions as well for removing or replacing an entire record.
1.1: Modifying Variables
When modifying variables, include the variable label names, original data values (i.e., what will appear in the data file), and the values you would like assigned to them.
For example, assuming you have the variables q1, q2r1, and q2r2, you can add the following code to your file to assign a value of "q2r2" to q2r1 if q1 has a value of "1":
if q1 == 1: q2r1 = q2r2.val
Keep in mind that a single question may have multiple variables assigned to it. Those variables are grouped in "variable groups", and you can get all the variables for a question by accessing its q.label.
For example, if there is a one-dimensional check-box question called "q1", with q1r1, r2, and r3 variables, accessing q.q1 would give you a list of these three (which you could then check for certain values or modify).
1.2: Modifying Markers
To modify survey markers using Python, run one of the following commands, specifying the marker you would like to modify:
-
hasMarker("..."): To check for records with the specified marker. -
setMarker: To set a marker for the specified record. -
removeMarker: To remove the specified marker.
1.3: Deleting a Record
To delete a record, call the delete() function.
if q3r3 == 1: delete()
1.4: Replacing a Record
The replace function allows you to specify the default values for variables using the variable labels and new values.
For example, if you have variables q1r1 and q1r2 and you want to replace their values with "0" and "1", respectively, use the following command:
replace([q1r1, q1r2], 0, 1)
2: Running the transform Command
Next, add the Python file to your project directory. Run the transform command, specifying your Python file. (The period . means the current directory.)
transform . somefile.py
If desired, you can include one of the following attributes to further define your command:
-
-v: To Trace changes as they happen. -
-t: To run your transformation on test data. -
-a: To run your transformation while allowing modification of hidden variables (where="notdp").
Note: If your code transformation is simple, you can alternatively pass it through on the command line by adding a space to the command (the extra space will designate a command code rather than a file). For example, transform.'setMarker(“qualified”)' would set the qualified marker on every participant in the current survey.
Once run, you will be given a summary of the changes and prompted to continue before those changes are saved. The code in your Python file is run on each row of the survey data and works similarly to variables in a tab-delimited file (i.e., each variable in the data corresponds to one Python variable).
3: Additional Considerations
- Similar to the Tab Import script,
transformmakes a backup to the data/old-results/ directory. - If using the
-tswitch, you can also use a script to create your own customized output file. For example:
transform -t . 'print "uuid = %s, status = %s" % (uuid, status)'