Skip to content

Fixup script for anonymization #816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions utils/anon-cmd
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash
# Generic anonymization script which would anonymize sid based on what it had
# seen in the past or simply what the translation dict already has.
# TODO: make git-annex use optional

set -eu

Expand All @@ -11,7 +12,7 @@ debug() {
# Translation file location
# Store under .git by default to guarantee that it is not committed or locked by git-annex etc
# But it might not fit some usecases where there is no .git
anon_file_default=$(dirname "$0")/../.git/anon_sid_map.csv
anon_file_default="${0}_map.csv"
anon_file="${AC_ANON_FILE:-$anon_file_default}"
anon_fmt="${AC_ANON_FMT:-%03d}"

Expand All @@ -24,6 +25,12 @@ debug "Using $anon_file to map $sid"

if [ ! -e "$anon_file" ]; then
touch "$anon_file" # initiate it

if ! git annex add --force-large "$anon_file"; then
echo "WARNING: Setting metadata for annex failed, but we proceed -- may be not under git-annex"
else
git annex metadata --set distribution-restrictions=sensitive "$anon_file"
fi
fi

# apparently heudiconv passes even those we provided in `-s` CLI option
Expand All @@ -42,13 +49,16 @@ if [ -n "$res" ]; then
ann="${res##*,}"
debug "Found $ann in '$res'"
else
echo "We have all sids mapped already! Will not create a new one for $sid" >&2; exit 1
# TODO: uncomment if you think that all subjects already known.
# echo "We have all sids mapped already! Will not create a new one for $sid" >&2; exit 1
# need to take the latest one
largest=$(sed -e 's/.*,//g' "$anon_file" | sort -n | tail -n1 | sed -e 's,^0*,,g')
next=$((largest+1))
# shellcheck disable=SC2059
ann=$(printf "$anon_fmt" $next)
debug "Found $largest and $next to get $ann, storing"
git annex unlock "$anon_file"
echo "$sid,$ann" >> "$anon_file"
git annex add --force-large "$anon_file"
fi
echo "$ann"