Package: scorify
Scorify: A simple tool for scoring psychological self-report questionnaires.BackgroundMany psychology studies use one or more self-report questionnaires to understand their participants. These responses go into CSV files with one question per column, one participant per row.Scoring these files is a bunch of work. Oftentimes, many questionnaires (or sub-scales) are included in one CSV file. Often, half of the questions are "reverse-scored" to combat the tendancy people have to agree with questions. Scoring these files usually means spending a whole bunch of time in Excel, and no one likes doing that.Scorify aims to fix this.Installationscorify requires Python 3.5.pip install scorifyshould have you set up.ExamplesSeeexamples/for some test files. To run the neurohack data and
scoresheet, do something like:score_data neurohack_scoresheet.csv neurohack_April+2,+2019_11.05.csvGetting startedGiven an example CSV file, let's say you want to score 5 columns, the answers
can be 1 to 5, where the third and fifth are reversed.| ppt  | happy1 | happy2 | happy3 | happy4 | happy5 |
| 3001 | 1      | 2      | 1      | 3      | 4      |
| 3002 | 4      | 1      | 5      | 1      | 2      |
| 3003 | 1      | 3      | 2      | 3      | 1      |
| ...  |Create a scoresheet that looks like:ABCDlayoutheaderlayoutdatatransformnormalmap(1:5,1:5)transformreversemap(1:5,5:1)scorepptscorehappy1happynormalscorehappy2happynormalscorehappy3happyreversescorehappy4happynormalscorehappy5happyreversemeasurehappymean(happy)Then you callscore_datawith that scoresheet and datafile, like:score_data scoresheet.csv datafile.csvYour output just goes to STDOUT, and you should see it renaming columns.
To save the output if it looks good, just pipe it to a file:score_data scoresheet.csv datafile.csv > output.csvOther common operationsExcluding participantsIf some participant data is particularly messy, you can exclude it using your
scoresheet like this:ABCexcludeppt_id_column_name3001Keeping second row headersIf your question headers have a second row with verbose question text in them,
you can keep that in the scored data by adding alayout keepinstruction:layout header
layout keep
layout dataRepeat the layout keep instruction if you want to keep more than one row.Scoresheet referenceThe main input to scorify is a comma or tab-delimited "scoresheet" that has many rows and four columns. The first column tells what kind of command the row will be, and will be one of:layout,exclude,transform,score, ormeasure.layoutThe layout section tells scorify what your input data looks like. It must contain aheaderanddata, butskipandkeepare also valid.datatells scorify that the rest of your input file is data. So:layout header
layout skip
layout datawould tell scorify to expect a header row, skip a line, and then read the rest of the file as data.layout header
layout keep
layout datawould result in scorify expecting a header row, keeping the next line as-is,
and reading the rest of the file as data.renameThe rename section renames a header column, and looks like:rename original_name new_nameColumns can only be renamed once, and must use a new, unique name. You must use the column's new name everywhere in the scoresheet.excludeThe format of an exclude line is:exclude column valuewhich will, as you might expect, exclude rows wherecolumn==value.transformSometimes, you'll want to reverse-score a column or otherwise change its value for scoring. And you'll want to give that some kind of sane name. Transforms let you do this. They look like:transform name mapperRight now, you can apply two transformations.map()A linear mapping. Example:transform reverse map(1:5,5:1)which will map the values 1,2,3,4,5 to 5,4,3,2,1. This will happily map values outside its input domain.discrete_map()A mapping for discrete values. Useful to map a numbers to human-meaningful values.transform score_gender discrete_map("1":"f", "2":"m")Unmapped values will return a blank.This transform can be useful when combined withjoin()(below) to combine an array of checkboxes into one column.passthrough_map()Likediscrete_map(), though unmapped values will be unchanged. So, if you have:transform score_gender passthrough_map("1":"f", "2":"m")a value of "999" will still be "999".scoreThe score section is where you tell scorify which columns you want in your output, what measure (if any) they belong to, and what transform (again, if any) you want to apply. These look likescore column measure_name transformmeasure_nameandtransformare both optional. So, to reverse score (using thereversewe defined up above) a column calledhappy_1and add it to thehappymeasure, use:score happy_1 happy reverseYou can optionally pass a 5th value, which will define the output column name:score happy_1 happy reverse ReverseHappy1measureThe measure section computes aggregate measures of your scored data. These lines look like:measure final_name aggregator(measure_1, measure_2, ..., measure_n)We support the following aggregators:mean()As you might expect, this calculates the mean of the measure or measures listed. Example:measure happy mean(happy)If any values in the measures are non-numeric, returns NaN.mean_imputed()Computes the mean of the measure. However, if any of the values in the measures are non-numeric, this fills in the mean of the numeric values. For example,mean_imputed(1, '', 3, 5)is3.sum()Computes the sum fo the listed measures. Example:measure sad sum(sad)If any values in the measures are non-numeric, returns NaN.sum_imputed()Computes the sum of the measure. However, if any of the values in the measures are non-numeric, this fills in the mean of the numeric values. For example,sum_imputed(1, '', 3, 5)is12.imputed_fraction()The fraction of the data that is non-zero and would have a value imputed for it.imputed_fraction(1, '', 3, 5)is 0.25.join()join()is a little trickier. It collects all the non-blank values in the listed measures, and joins them with the|character. Useful if you have a set of values selected by checkbox. For example, if you had three measures that would either be blank or not for things participants might endorse, you could collate them into one column with:measure liked_pets join(likes_cats, likes_dogs, likes_horses)If a participant hadcatsforlikes_catsandhorsesforlikes_horses, you'd get:cats|horsesratio()ratio(a, b)will compute the ratio of two columns; in other words:a / b. Notably, this works on other measures, so you can take the ratio of sums or means. In those cases, the ratio line needs to come after the other measures' lines do.min()min(measure_1, measure_2)will output the minimum numeric value in the given measures. Non-numeric values will cause NaN.max()max(measure_1, measure_2)will output the maximum numeric value in the given measures. Non-numeric values will cause NaN.Complete exampleIf you take a scoresheet that looks like:ABCDlayoutheaderlayoutdataexcludePPT_COLbad_ppt1excludePPT_COLbad_ppt2transformnormalmap(1:5,1:5)transformreversemap(1:5,5:1)scorePPT_COLscoreHAPPY_Q1happynormalscoreSAD_Q1happynormalscoreHAPPY_Q2happyreversemeasurehappy_scoremean(happy)measuresad_scoremean(sad)measurehappiness_ratioratio(happy_score, sad_score)and run it on data that looks like:PPT_COLEXTRAHAPPY_Q1SAD_Q1HAPPY_Q2ppt1foo422ppt2bar255... you'll get output like:PPT_COLHAPPY_Q1: happySAD_Q1: sadHAPPY_Q2: happyhappy_scoresad_scorehappiness_ratioppt14233.521.75ppt22511.550.3CreditsScorify uses several excellent libraries:docoptschemaopenpyxl