By Luling Huang
A. Split the data based on unique values in the column “ThreadID;”
B. Do the cleaning work in Part I and Part II on one subset;
C. Apply the work done in “Task B” to other subsets.
The reason to include python is that it automates the process in Tasks A and C, which is especially important with a large dataset. Whereas my previous two blog posts speak more to my own project, I hope this one can be helpful for a more general situation when you want to automate the OpenRefine workflow on many subsets.
II. pandas: a python library for processing dataframes (here is the link for installation);
III. refine-python: a client library for interacting with OpenRefine in python (here is the link for its GitHub page, and yes, it’s still working under OpenRefine 2.6).
Link for the hypothetical data used in this demonstration: Example_3Threads.csv (for simplification the time data is ready).
What I thought I could do: Export subsets based on a facet of the column “ThreadID” in OpenRefine.
Python code using pandas:
Line 2: Create a dataframe from the sample csv file.
Line 3: Create a list of unique values in the column “ThreadID.”
Line 4: Locate a subdirectory under the working directory to store all split files.
Lines 5 to 8: Loop through “ThreadIDList,” do the split, and export to csv with corresponding file names. The key is line 6: rows are selected based on values in the column “ThreadID.”
Result: Three split files based on thread IDs have been stored in the folder “SplitFiles.”
In OpenRefine, do the work covered in Part II on any one of the subsets.
OpenRefine stores every step that changes the data in an operation history. History of one project can be extracted, saved in JSON, and applied to other projects. Therefore, the logic of completing Task C is straightforward: C1. When Task B is finished, extract and save the history as JSON; C2. Use refine-python to automate the process of applying the operation history to the other two subsets.
C1: Extract operation history.
The left column lists all the steps performed in Task B. The right column shows the operation history in JSON. We can copy the JSON data to a code editor, and save it as “OperationHistory.json.”
C2: Let python streamline the remaining process.
Notes on directories for this demonstration: the working directory is “/python refine/,” where both “refine.py” and “OperationHistory.json” locate. Under “python refine,” “SplitFiles” stores split csv files from Task A, and “SplitFiles2” stores transformed csv files.
Python code with brief comments:
The tricky part (for me, may not apply to other situations):
In Part II (Step 6), there is a part of GREL’s cross() function that points to the working project’s name. Therefore, I have to update the JSON file in each loop so that the project names match. Lines 22-34 do this job (read, change, and update JSON).
More specifically, when python reads JSON, it converts JSON’s content to a list of dictionaries. Locating and changing parts of the JSON content thus follows the way such a list is structured (lines 29-30, the two places where project name needs to be changed locate in the keys “description” and “expression” under the sixth dictionary in the list).
More concretely, when “Example_3Threads-3.csv” enters the loop (the project name follows its file name by default), after line 34, the original project name in JSON, e.g., “Example_3Threads-689(whatever the index is).csv,” is changed to “Example_3Threads-3.csv.”