mindlogger_backend_dev (module)

Submodules

girder_connections

mindlogger_backend_dev.girder_connections.configuration(config_file=None, context_file=None, which_girder='dev')

Function to set configuration variables.

Parameters:
config_file: string, optional

path to configuration file default = “config.json”

context_file: string, optional

path to context file default = “context.json”

which_girder: string, optional

“dev” or “production” default = “dev”

Returns:
config: dictionary
context: dictionary
api_url: string
mindlogger_backend_dev.girder_connections.connect_to_girder(api_url='https://data.kitware.com/api/v1/', authentication=None)

Function to connect to a Girder DB.

Parameters:
api_url: string, optional

path to running Girder DB API endpoint. Default is Kitware Data API

authentication: tuple or string, optional

(username, password) or APIkey (

username: string password: string

) default=None

APIkey: string

default=None

Returns:
girder_connection: GirderClient

Examples

>>> import girder_client as gc
>>> g = connect_to_girder()
Connected to the Girder database 🏗🍃 but could not authenticate without username and password.
>>> g.getItem(
...     "58cb124c8d777f0aef5d79ff"
... )["name"]
'LARGE_PtCu_NanoParticles-stride-5.html'
>>> g = connect_to_girder(authentication=("a", "b"))
Connected to the Girder database 🏗🍃 but could not authenticate.
>>> g = connect_to_girder(authentication="ab")
Connected to the Girder database 🏗🍃 but could not authenticate.

object_manipulation

mindlogger_backend_dev.object_manipulation.drop_empty_keys(d)

Function to iteratively drop empty keys from a given dictionary

Parameters:
d: dictionary
Returns:
d: dictionary

Examples

>>> drop_empty_keys(
...     {
...         "a": None,
...         "b": {
...             "a": None,
...             "b": {}
...         },
...         "c": [
...             "a",
...             None,
...             {"b": None},
...             (
...                 "a",
...                 {"b": None},
...                 "c"
...              ),
...              ("a", None, "c")
...          ]
...      }
... )
{'b': {'b': {}}, 'c': ['a', None, {}, ('a', {}, 'c'), ('a', None, 'c')]}
mindlogger_backend_dev.object_manipulation.isiterable(item)

Function to test if an item is an Iterable, ie a non-string object that can be iterated over

Parameters:
item: anything
Returns:
isterable: boolean

Is item iterable?

Examples

>>> isiterable(set())
True
>>> isiterable(bool())
False

postgres_to_girder

mindlogger_backend_dev.postgres_to_girder.postgres_to_girder.assingments_from_postgres(girder_connection, postgres_tables, context={}, timings={'1': 'Once', '12h': '2×Daily', '1d': 'Daily', '8h': '3×Daily'})

Function to build user activity schedules.

Parameters:
girder_connection: GirderClient
postgres_tables: DataFrame
context: dictionary, optional
timings: dictionary, optional
Returns:
schedules: set

set of Girder Item _ids

mindlogger_backend_dev.postgres_to_girder.postgres_to_girder.connect_to_postgres(postgres_config)

Function to connect to a Girder DB.

Parameters:
postgres_config: dictionary
“dbname”: string

Postgres DB name

“user”: string

Postgres username

“host”: string

active Postgres IP (no protocol, ie, without “https://”)

“password”: string

password for Postgres user

Returns:
postgres_connection: connection

http://initd.org/psycopg/docs/connection.html#connection

Examples

>>> config_file = os.path.join(
...    os.path.dirname(__file__),
...    os.pardir,
...    "config.json.template"
... )
>>> connect_to_postgres(
...     configuration(
...         config_file=config_file
...     )[0]["postgres"]
... )
I am unable to connect to the Postgres database 🐘
mindlogger_backend_dev.postgres_to_girder.postgres_to_girder.get_abbreviation(activity)

Function to extract abbreviation from activity name if one is present

Parameters:
activity: string
Returns:
activity_name: string
abbreviation: string

Examples

>>> get_abbreviation(
...     "Corresponding parts of congruent "
...     "triangles are congruent (CPCTC)"
... )[1]
'CPCTC'
mindlogger_backend_dev.postgres_to_girder.postgres_to_girder.get_postgres_item_version(activity_name, abbreviation=None, activity_source=None, respondent=None, version=None)

Function to create an item version in Mindlogger Item format: [Source] — [Activity] — [Respondent] Report ([Version]).

Parameters:
activity_name: string
abbreviation: string
activity_source: string
respondent: string
version: string
Returns:
item_version: string

Examples

>>> activity_name, abbreviation = get_abbreviation(
...     "EHQ (Edinburgh Handedness Questionnaire)"
... )
>>> get_postgres_item_version(
...     activity_name=activity_name,
...     abbreviation=abbreviation,
...     activity_source="MATTER Lab",
...     respondent="Coworker",
...     version="v0.1"
... )
'MATTER Lab ― Edinburgh Handedness Questionnaire (EHQ) ― Coworker Report (v0.1)'
mindlogger_backend_dev.postgres_to_girder.postgres_to_girder.postgres_activities_to_girder_activities(acts, gc, users, users_by_email, context)

Function to transfer users from Postgres table to Girder collection.

Parameters:
acts: DataFrame

activities table from Postgres DB

gc: GirderClient

active GirderClient in which to add the users

users: DataFrame

users table from Postgres DB

users_by_email: dictionary
key: string

email address

value: string

Girder User_id

context: dictionary

JSON-LD context

Returns:
activities: DataFrame
mindlogger_backend_dev.postgres_to_girder.postgres_to_girder.postgres_answers_to_girder_answers(girder_connection, postgres_tables, context)

Function to port User responses from Postgres to Girder

Parameters:
girder_connection: GirderClient
postgres_tables: DataFrame
context: dictionary
Returns:
response_folder_ids: set
mindlogger_backend_dev.postgres_to_girder.postgres_to_girder.postgres_options_to_JSONLD_options(gc, q, item_id, language='en-US')

Function to convert Postgres question options to JSON-LD options

Parameters:
gc: GirderClient
q: dictionary
item_id: string
language: string, default en-US
Returns:
j_options: list of dictionaries
mindlogger_backend_dev.postgres_to_girder.postgres_to_girder.postgres_question_to_girder_question(q, question_text, variable_name, context, language='en-US')
mindlogger_backend_dev.postgres_to_girder.postgres_to_girder.postgres_questions_to_girder_screens(girder_connection, questions, short_name, screen_type, activity_version, context, language='en-US')

Function to convert Postgres questions to Girder screens

Parameters:
girder_connection: GirderClient
questions: list of dictionaries
short_name: string
screen_type: string
activity_version: string
Returns:
screens: list

list of Girder screen _ids

mindlogger_backend_dev.postgres_to_girder.postgres_to_girder.postgres_user_assign_to_girder_groups(postgres_user, girder_user, girder_connection)

Function to assign User to appropriate Girder Groups per permissions in PostgresDB.

Parameters:
postgres_user: Series

row from users DataFrame

girder_user: string

Girder User “_id”

girder_connection: GirderClient

active GirderClient

Returns:
groups: dictionary
key: string

Group_id

value: string

permissions level

groups and permissions levels assigned

mindlogger_backend_dev.postgres_to_girder.postgres_to_girder.postgres_users_to_girder_users(users, girder_connection, unknown_person={'first_name': '[Notname]', 'last_name': '[Anonymous]'})

Function to transfer users from Postgres table to Girder collection.

Parameters:
users: DataFrame

users table from Postgres DB

girder_connection: GirderClient

active GirderClient in which to add the users

unknown_person: dictionary

unknown_person[“first_name”]: string unknown_person[“last_name”]: string

Returns:
users_by_email: dictionary
key: string

email address

value: string

Girder User_id

Examples

>>> import girder_client as gc
>>> import pandas as pd
>>> postgres_users_to_girder_users(
...     pd.DataFrame(
...         {
...             'first_name': ['Packages,'],
...             'last_name': ['TravisCI'],
...             'email': ['travis-packages'],
...             'password': [
...                 '$2b$12$jchNQFK2jj7UZ/papXfWsu1'
...                 '6enEeCUjk2gUxWJ/n6iYPYmejcmNnq'
...             ],
...             'role': ['user']
...         }
...     ),
...     girder_connection=gc.GirderClient(
...         apiUrl="https://data.kitware.com/api/v1/"
...     )
... )
{'travis-packages': '55535d828d777f082b592f54'}
mindlogger_backend_dev.postgres_to_girder.postgres_to_girder.table_cells_from_postgres(rows, columns, response_type)

Function to convert Postgres table options encoded as rows and columns to dictionaries with (row, column) keys where 0 represents a header in either dimension and internal cells are 1-indexed.

Parameters:
rows: list
rows[]: dictionary
key: string

“text” or “img_url”

value: string

row header

columns: list
columns[] dictionary
key: string

“text” or “img_url”

value: string

internal cell option or column header

response_type: string
Returns:
table: dictionary
key: 2-tuple

(row_number, column_number) row_number: int

0 == header

column_number: int

0 == header

value: dictionary
key: string

“text” or “img_url”

value: string

internal cell option or column header

Examples

>>> [
...     key for key in table_cells_from_postgres(
...         rows=[
...             {'text': 'Good question'},
...             {'text': 'Bad'}
...         ],
...         columns=[
...             {'text': '1'},
...             {'text': '2'}
...         ],
...         response_type="image_sel"
...     )
... ]
[(1, 0), (2, 0), (1, 1), (1, 2), (2, 1), (2, 2)]
>>> [
...     key for key in table_cells_from_postgres(
...         rows=[
...             {'text': 'Good question'},
...             {'text': 'Bad'}
...         ],
...         columns=[
...             {'text': '1'},
...             {'text': '2'}
...         ],
...         response_type="single_sel"
...     )
... ]
[(1, 0), (2, 0), (0, 1), (0, 2)]
mindlogger_backend_dev.postgres_to_girder.postgres_to_girder.upload_applicable_files(gc, act_data, item_id, item_name)

Function to find a File in a Girder Item if such File exists, otherwise to upload said File from Postgres pointer.

Parameters:
gc: GirderClient

active girder client

act_data: dictionary

from JSON in Postgres

item_id: string

Girder _id for Item

item_name: string

name of Item

Returns:
file_ids: dictionary
key: string

filename

value: dictionary
“@id”: string

“file/[Girder _id of File]”

Examples

>>> import girder_client
>>> upload_applicable_files(
...     gc=girder_client.GirderClient(
...         apiUrl="https://data.kitware.com/api/v1/"
...     ),
...     act_data = {
...         "image_url": "https://data.kitware.com/api/"
...         "v1/file/596f64838d777f16d01e9c28/download/"
...         "ensembl_vega_mart_88_drerio_gene_vega__"
...         "gene__main.sql.gz"
...     },
...     item_id="596f64838d777f16d01e9c27",
...     item_name="ensembl_vega_mart_88_drerio_gene_"
...     "vega__gene__main.sql"
... )[
...     "ensembl_vega_mart_88_drerio_gene_"
...     "vega__gene__main.sql.gz"
... ]["@id"]
'file/596f64838d777f16d01e9c28'

update_schema

mindlogger_backend_dev.update_schema.add_to_schedule(girder_connection, frequency, schedules_id, activity_item_id, context={}, timings={'1': 'Once', '12h': '2×Daily', '1d': 'Daily', '8h': '3×Daily'}, schedule_folder_id=None, schedule_item_id=None)

Function to add Activities to a Schedule

Parameters:
girder_connection: GirderClient
frequency: string
schedules_id: string
activity_item_id: string
context: dictionary, optional

default: {}

timings: dictionary, optional
key: string

frequency

value: string

schedule name

default: {

“1d”: “Daily”, “1”: “Once”, “8h”: “3×Daily”, “12h”: “2×Daily”

}

schedule_folder_id: string, optional

default: _id for public schedules

schedule_item_id: string, optional

default: _id for “Version 0 [Frequency]”

Returns:
schedule_item_id: string
mindlogger_backend_dev.update_schema.find_or_create(x, parent, girder_connection)

Function to find or create a Girder Folder or Item under a specific parent.

Parameters:
x: 2-tuple
x[0]: string

“Folder”, “Item”, etc.

x[1]: string

entity name

parent: 2-tuple
parent[0]: string

“Collection”, “Folder”, etc.

parent[1]: string

Girder_id

girder_connection: GirderClient

active GirderClient

Returns:
_id: string

Girder_id of found or created entity

mindlogger_backend_dev.update_schema.get_files_in_item(girder_connection, item_id, sort='created', sortdir=-1)

Function to get a dictionary of Files in an Item in a Girder database.

Parameters:
girder_connection: GirderClient

an active GirderClient

item_id: string

Girder _id of Item.

sort: string, optional

Field to sort the result set by. default = “created”

sortdir: int, optional

Sort order: 1 for ascending, -1 for descending. default = -1

Returns:
files: dictionary or None

metadata of files in Girder Item

Examples

>>> import girder_client as gc
>>> get_files_in_item(
...     girder_connection=gc.GirderClient(
...         apiUrl="https://data.kitware.com/api/v1/"
...     ),
...     item_id="58a372f38d777f0721a64df3"
... )[0]["name"]
'Normal001-T1-Flash.mha'
mindlogger_backend_dev.update_schema.get_girder_id_by_name(girder_connection, entity, name, parent=None, limit=1, sortdir=-1, index=0)

Function to get the _id of a single entity in a Girder database.

Parameters:
girder_connection: GirderClient

an active GirderClient

entity: string

“collection”, “folder”, “item”, “file”, “user”

name: string

name of entity

parent: 2-tuple, optional, default=None

(parentType, parent_id) parentType: string

“Collection”, “Folder”, or “User”

parendId: string

Girder _id for parent

limit: int, optional, default=1

maximum number of query results

sortdir: int, optional, default=-1

Sort order: 1 for ascending, -1 for descending.

index: int, default=0

0-indexed index of named entity in given sort order.

Returns:
_id: string

Girder _id of requested entity

Examples

>>> import girder_client as gc
>>> get_girder_id_by_name(
...     girder_connection=gc.GirderClient(
...         apiUrl="https://data.kitware.com/api/v1/"
...     ),
...     entity="collection",
...     name="Cinema",
...     parent=None,
...     sortdir=1
... )
'55706aa58d777f649a9ba164'
mindlogger_backend_dev.update_schema.get_group_ids(girder_connection, groups={'Viewers', 'Editors', 'Users', 'Managers'}, create_missing=False)

Function to collect Girder _ids, optionally creating any missing groups.

Parameters:
girder_connection: GirderClient

active Girder Client

groups: set

set of Group names for which to get Girder _ids item: string

Group name

default: {

“Editors”, “Managers”, “Users”, “Viewers”

}

create_missing: boolean

create Group if none with that name exists? default: False

Returns:
groups: dictionary
key: string

name from original set

value: string

Girder Group _id

Examples

>>> import girder_client as gc
>>> get_group_ids(
...     girder_connection=gc.GirderClient(
...         apiUrl="https://data.kitware.com/api/v1/"
...     ),
...     groups={"VIGILANT"}
... )
{'VIGILANT': '58a354fe8d777f0721a6106a'}
mindlogger_backend_dev.update_schema.get_user_id_by_email(girder_connection, email)

Function to get the _id of a single User in a Girder database.

Parameters:
girder_connection: GirderClient

an active GirderClient

email: string

email address

Returns:
_id: string or None

Girder _id of requested User, or None if not found

Examples

>>> import girder_client as gc
>>> get_user_id_by_email(
...     girder_connection=gc.GirderClient(
...         apiUrl="https://data.kitware.com/api/v1/"
...     ),
...     email="test@example.com"
... )
mindlogger_backend_dev.update_schema.ls_x_in_y(x_type, y, girder_connection)

Function to list x in y.

Parameters:
x_type: string

“Folder”, “Item”, etc.

y: 2-tuple
y[0]: string

“Collection”, “Folder”, etc.

y[1]: string

Girder_id

girder_connection: GirderClient

active GirderClient

Returns:
x: list of dictionaries

≅ JSON array of objects

Examples

>>> import girder_client as gc
>>> ls_x_in_y(
...     "Folder",
...     ("Collection", "58b5d21a8d777f0aef5d04b1"),
...     girder_connection=gc.GirderClient(
...         apiUrl="https://data.kitware.com/api/v1/"
...     )
... )
[{'_accessLevel': 0, '_id': '58b5d86c8d777f0aef5d04b4', '_modelType': 'folder', 'baseParentId': '58b5d21a8d777f0aef5d04b1', 'baseParentType': 'collection', 'created': '2017-02-28T20:07:08.074000+00:00', 'creatorId': '55a413168d777f649a9ba343', 'description': '', 'name': 'TestData', 'parentCollection': 'collection', 'parentId': '58b5d21a8d777f0aef5d04b1', 'public': True, 'publicFlags': [], 'size': 41319999, 'updated': '2017-02-28T20:07:08.074000+00:00'}, {'_accessLevel': 0, '_id': '58cb12048d777f0aef5d79fc', '_modelType': 'folder', 'baseParentId': '58b5d21a8d777f0aef5d04b1', 'baseParentType': 'collection', 'created': '2017-03-16T22:30:28.994000+00:00', 'creatorId': '55a413168d777f649a9ba343', 'description': '', 'name': 'TomvizData', 'parentCollection': 'collection', 'parentId': '58b5d21a8d777f0aef5d04b1', 'public': True, 'size': 0, 'updated': '2017-03-24T18:30:21.117000+00:00'}]
mindlogger_backend_dev.update_schema.mv(x, new_parent, girder_connection)

Function to move a Girder entity to a new_parent.

Parameters:
x: 2-tuple
x[0]: string

“Folder”, “Item”, etc.

x[1]: string

Girder_id

new_parent: 2-tuple
new_parent[0]: string

“Collection”, “Folder”, etc.

new_parent[1]: string

Girder_id

girder_connection: GirderClient

active GirderClient

Returns:
x_object: dictionary

≅JSON representation of entity in new location

mindlogger_backend_dev.update_schema.rename(x, new_name, girder_connection)

Function to rename a Girder entity.

Parameters:
x: 2-tuple
x[0]: string

“Folder”, “Item”, etc.

x[1]: string

Girder_id

new_name: string

new name for entity

girder_connection: GirderClient

active GirderClient

Returns:
x_object: dictionary

≅JSON representation of entity in new location