Braviz Qt Models

This module provides several classes that can be used in the Model / View Qt pattern, so they inherit from QAbstractItemModel. Only methods that are not part of the Qt interface are shown here.

Generic models

This models are designed to be generic, and they don’t make any assumptions about the data.

class SimpleSetModel[source]

Transforms a python set into a Qt List Model

get_elements()[source]

Returns a set of the current data

set_elements(data_set)[source]

Sets the current elements in the model

Parameters:data_set (set) – Items to be stored in the model
class SimpleCheckModel(choices)[source]

Provides a model for selecting items from a set of choices (represented with checkboxes in Qt Views)

get_selected()[source]

Gets a tuple of the checked items

set_selection(selection)[source]

Sets the currently selected items to those (and only those) in selection

Parameters:selection (set) – New set of checked items
class DataFrameModel(data_frame, columns=None, string_columns=(), index_as_column=True, checks=False)[source]

This model is used for displaying data frames in a QT table view

Parameters:
  • data_frame (pandas.DataFrame) – Data Frame
  • columns (list) – Optional, names for columns of the data frame
  • string_columns (list) – List of column names that should be displayed as strings
  • index_as_column (bool) – Display the data frame index as the first column
  • checks (bool) – Display checkboxes next to the first column

Tabular Data Models

These models are designed to work with the braviz database and to allow users to interact with the data stored there through a graphical interface.

Variables

class VarListModel(parent=None, checkeable=False)[source]

List of available variables, optionally with checkboxes

The list can be filtered to include only variables whose name include a certain pattern

Parameters:
  • parent (QObject) – Qt parent
  • checkeable (bool) – If True the list will be displayed with checkboxes
clear_selection()[source]

Removes checks from all variables

select_items(items_list)[source]

Add checks to variables by id

This will cause checks to appear in the respective boxes when the view is updated

Parameters:items_list (list) – List of variable ids to check
select_items_by_name(items_list)[source]

Add variables to the set of checked items

This will cause checks to appear in the respective boxes when the view is updated

Parameters:items_list (list) – List of variable to check
set_highlighted(highlighted_name=None)[source]

The highlighted item will appear in bold

Parameters:highlighted_name (str) – Name of a variable to show in bold, use None to don’t highlight any variable
update_list(mask=None)[source]

Update the variable list to show variables whose name match a mask

Parameters:mask (str) – Mask in sql format, the database is configured to be case insensitive. To search for variables that include the word "age" use "%age%"
class SubjectDetails(initial_vars=None, initial_subject=None)[source]

A table showing variable values for a single subject

The first column contains variable names, and the second column its values, together with the range of values for the reference population

The user may drag table rows to change the order

Parameters:
  • initial_vars (list) – List of variables codes to include in the table from the start
  • initial_subject – Code of the initial subject
change_subject(new_subject)[source]

Change the current subject to whih the variable values correspond

Parameters:new_subject – New subject code
get_current_variables()[source]

Get a list of current variable codes

set_variables(variable_ids)[source]

Set the list of variables

Parameters:variable_ids – list of new variable ids to show in the table
class ContextVariablesModel(context_vars_list=None, parent=None, editable_dict=None)[source]

A table with three columns: Variable name, variable type, and a checkbox called “editable”

This table is used to select a list of variables, and decide which of those should be made writable, and which readonly

Parameters:
  • context_vars_list (list) – List of variables ids to include at the start
  • parent (QObject) – Qt parent
  • editable_dict (dict) – Dictionary where keys are variable ids and values are booleans indicating if the variable should be writable by the user. This object will be modified.
add_variable(var_idx)[source]

Add a variable to the table

The removeRows method can be used to remove a variable :param var_idx: Variable index

get_variables()[source]

Gets a list of variable indices currently in the table

class NewVariableValues[source]

A table with on column for subject, and a writable second column for variable values

Values are restricted to numbers

save_into_db(var_idx)[source]

Save values from the model into the braviz database

Parameters:var_idx (int) – Index of the variable to which the values should be saved
set_values_dict(values_dict)[source]

Set values for variables

Parameters:values_dict (dict) – A dictionary with subject codes as keys, and variable values as values
class NominalVariablesMeta(var_name, parent=None)[source]

A table with numerical labels in the first column and corresponding text labels in the second column

These labels correspond to the possible values a nominal variable can take Textual labels can be edited

Parameters:var_name (str) – Name of a nominal variable
add_label()[source]

Add another row with the next numerical label to the table

get_checked()[source]

Get a set of labels whose checkbox is checked

get_unchecked()[source]

Get a set of labels whose checkbox is empty

save_into_db(var_idx=None)[source]

Save the textual labels into the database

Parameters:var_idx (int) – Index of the variable to which the labels will be saved, only required if var_name is not set.
set_checkeable(checkeable)[source]

Choose if check boxes should be displayed next to numerical labels

Parameters:checkeable (bool) – If True checkboxes will appear
set_labels_dict(labels_dict)[source]

Sets textual labels

Parameters:labels_dict (dict) – Dictionary that maps numerical labels to textual labels
update_model(var_name)[source]

Update the table with data from the database

Parameters:var_name (str) – Read labels for this variable

Subjects and samples

class SampleTree(columns=None)[source]

Creates a tree for representing a sample

Each first level child represents a nominal variable, and its sons are the different values for that variable finally, the leafs contain the subject ids. For each node the number of subjects it contains is represented in the second column

Parameters:columns (list) – List of nominal variable names to include in the tree.
get_leafs(QModelIndex)[source]

Get subjectids under a certain node

Parameters:QModelIndex (QModelIndex) – Index of a node in the tree
set_sample(new_sample)[source]

Sets the subsample to show in the tree

Parameters:new_sample (set) – Set of subjects in the new subsample
class SubjectsTable(initial_columns=None, sample=None)[source]

A table of subjects and values for specified variables

The first column contains subject codes. There are additional columns for each requested variable. Values for nominal variables are shown as textual labels

Parameters:
  • initial_columns (list) – List of initial variables indices to include as columns in the table
  • sample (list) – List of subject ids to show in the table
get_current_column_indexes()[source]

Get a list of current variable indices shown as columns

get_current_columns()[source]

Get a list of current variable names shown as columns

get_subject_index(subj_id)[source]

Get row number for a certain subject id

set_sample(new_sample)[source]

Set the subsample of subjects in the table

Parameters:new_sample (set) – List of subject ids
set_var_columns(columns)[source]

Set the columns in the table

Parameters:columns (list) – List of variable indices
class SubjectChecklist(initial_list=(), show_checks=True)[source]

A list of subjects with checkboxes

Parameters:
  • initial_list (list) – List of subject ids
  • show_checks (bool) – If False no checkboxes will be shown
checked

set of checked subjects

set_list(lst)[source]

Set the list of subjects

Parameters:lst (list) – List of subject ids
class SubjectCheckTable(initial_list=(), data_cols=(), headers=('', ''))[source]

A table of subjects with optional checkboxes

Parameters:
  • initial_list (list) – List of subject ids
  • data_cols (list) – List of columns to shown, where each column is an iterable of the same length as the subject lists, such that the value for the subject in position i is also located at position i
  • headers (list) – List of strings used as headers for the table on top of each data column
checked

Set of checked subjects

set_data_cols(new_cols)[source]

Sets new data columns

Parameters:new_cols (list) – List of data columns, must have the same length as the headers. Each data column is itself an iterable of the same length and indices as the subjects list
class SamplesSelectionModel[source]

A table showing available subsamples

It has three columns: sample size, sample name, and sample description

get_sample(QModelIndex)[source]

Get the set of subjects in a subsample located at a certain row

Parameters:QModelIndex (QModelIndex) – Index of a cell in the table (only the row is important)
get_sample_index(QModelIndex)[source]

Get the database index of a subsample located at a certain row

Parameters:QModelIndex (QModelIndex) – Index of a cell in the table (only the row is important)
get_sample_name(QModelIndex)[source]

Get the name of a subsample located at a certain row

Parameters:QModelIndex (QModelIndex) – Index of a cell in the table (only the row is important)
reload()[source]

Reload subsamples from the database

class SamplesFilterModel[source]

A list of filters that can be applied to a sample

add_filter(filter_name, filter_func)[source]

Adds a filter to the list

Parameters:
  • filter_name (str) – Name of the filter, this will be shown to the user
  • filter_func (function) – This function should take a subject id and return a boolean.
apply_filters(input_set)[source]

Apply all filters to a given set

Parameters:input_set (set) – Set of subject ids
Returns:filtered set of subject ids
remove_filter(index)[source]

Removes a filter located at a given row

Parameters:index (QModelIndex) – Index of the row containing the filter

Statistics

class VarAndGiniModel(outcome_var=None, parent=None, sample=None)[source]

A table of variables which can include the gini index.

The gini index is associated to each variable when predicting an outcome variable

Parameters:
  • outcome_var (str) – Name of variable to treat as outcome
  • parent (QObject) – Qt parent for this object
calculate_gini_indexes()[source]

Calculates the gini indices of the variables via a random forest

update_list(mask)[source]

Updates the list to include only variables whose names match a mask

Parameters:mask (str) – mask in sql syntax
class AnovaRegressorsModel(regressors_list=(), parent=None)[source]

Holds the regressors and interaction terms for a linear model

The model exposes two columns, regressor name and degrees of freedom. Internally it keeps track if each regressor is a single term or an interaction of multiple variables

It is possible to show only interactions, or only single term regressors

Parameters:
  • regressors_list (list) – List of variable names to be used as regressors
  • parent (QObject) – Qt parent
add_interactor(factor_rw_indexes)[source]

Add an interaction term to the model

Parameters:factor_rw_indexes (list) – Positions of single term regressors that compose the interaction. This positions are the row number of the variable in this model, without counting interaction terms (or the row number when interaction terms are hidden)
add_interactor_by_names(factor_names)[source]

Add an interaction term by giving the name of its terms

Parameters:factor_names (list) – List of variable names, already in the model, which make up the interaction
add_regressor(var_name)[source]

Add a regressor to the list

Parameters:var_name (str) – Name of a variable
get_data_frame()[source]

Get the internal data frame

Returns:pandas.DataFrame with three columns – regressor name, degrees of freedom, and interaction. The last column has zeros for single variable regressors and 1 for interaction terms.
get_interactions()[source]

Get a list of interaction terms

get_interactors_dict()[source]

Get the interactions dictionary

Returns:A dictionary that maps dataframe indices of interactions terms to the dataframe indices of its factors.
get_regressors()[source]

Get a list of single term regressors

reset_data(regressors_list)[source]

Sets a new list of regressors

Parameters:regressors_list (list) – List of variable names
show_interactions(value=True)[source]

Set if interaction terms should be shown

Parameters:value (bool) – if False interaction terms will be hidden
show_regressors(value=True)[source]

Set if single variable terms should be shown

Parameters:value (bool) – if False single variable terms will be hidden
class AnovaResultsModel(results_df=None, residuals=None, intercept=None, fitted=None)[source]

A model to represent the results of an anova regression

It has columns for factor names, sum of squares, degrees of freedom, F statistic, and p value. Internally it also holds residuals, fitted values and intercept.

Parameters:
  • results_df (pandas.DataFrame) – A Data Frame containing the columns indicated above
  • residuals (list) – A vector of the regression residuals
  • intercept (float) – Value of the intercept term of the regression
  • fitted (list) – Vector of fitted values

Bundles

class SimpleBundlesList[source]

A list of database fiber bundles

An optional special bundle, called “<From Segment>” may also be shown

add_bundle(bundle_id, name)[source]

Add a bundle to the list

Parameters:
  • bundle_id (int) – Bundle database index
  • name (str) – Bundle name to show
get_bundle_name(bid)[source]

Get the name used to show a bundle with the given id

Parameters:bid (int) – Bundle database id
get_ids()[source]

Get ids in the list, if the special bundle is present it is ignored

set_ids(id_list, names_dict=None)[source]

Set bundle ids to show in the list

Parameters:
  • id_list (list) – List of database indices of bundles
  • names_dict (dict) – Optional, if present it is used to map ids to names; otherwise the names in the database are used
set_show_special(show_special)[source]

Show or hide the special “<From segment>” bundle

Parameters:show_special (bool) – If True the special bundle will appear on the list
class BundlesSelectionList[source]

A list of bundles with checkboxes

get_selected()[source]

Get a list of selected bundle ids

refresh_model()[source]

Reloads data from the database

select_many_ids(ids_it)[source]

Adds several bundles to the selection

Parameters:ids_it (list) – List of database ids to add

Scenarios

class ScenariosTableModel(app_name)[source]

A table with available scenarios

Optionally restricts the table to scenarios for a certain application It has three columns: date, name and description

Parameters:app_name (str) – Name of application, if None scenarios for all applications are shown
get_index(row)[source]

Get the database index for the scenario in a given row

get_name(row)[source]

Get the name of the scenario at a given row

Structures model

class StructureTreeModel(reader, subj=None, dominant=False)[source]

A tree of freesurfer segmented structures

Parameters:
  • reader (braviz.readAndFilter.base_reader.BaseReader) – A reader from which to get available models
  • subj – Id of a subject to use as reference for getting available models
  • dominant (bool) – If False the tree will have left and right hemispheres, otherwise it will have dominant and non-dominant hemispheres
get_selected_structures()[source]

Get a list of currently checked structures

optimistic_reload_hierarchy(dominant)[source]

Rebuilds the tree from the models of the first subject with models

Parameters:dominant (bool) – If False the tree will have left and right hemispheres, otherwise it will have dominant and non-dominant hemispheres
reload_hierarchy(subj=None, dominant=False)[source]

Reload tree based on a specific subject

Parameters:
  • subj – Subject to load available models from
  • dominant (bool) – If False the tree will have left and right hemispheres, otherwise it will have dominant and non-dominant hemispheres
set_selected_structures(selected_list)[source]

Selects the structures given in a list

Parameters:selected_list (list) – New set of selected structures’ names

Helper

class StructureTreeNode(parent=None, name='', son_number=0)[source]

Node for the freesurfer structures tree

Parameters:
add_child(name)[source]

Add a child to this node

Parameters:name (str) – Name for the child node
check_and_update_tree(check=True)[source]

Add a checkmark to this node

If it has childs, they are also checked (recursively) Predecessor boxes are also updated

delete_children()[source]

Delete all children from this node

get_leaf_names()[source]

Get names of leafs that are under the current node

is_leaf()[source]

Returns True if this node is a leaf

Logic bundle model

class LogicBundleQtTree(root=None)[source]

A Qt representation of a logical fiber bundle

Parameters:root (braviz.interaction.logic_bundle_model.LogicBundleNode) – Root of the logic bundle tree
add_node(parent, node_type, value, extra_data=None)[source]

Add a son to an specific node in the tree

Parameters:
get_node(index)[source]

Get the node at a given index

Parameters:index (QAbstractModelIndex) – Index of a node
remove_node(index)[source]

Remove a node from the tree

Parameters:index (QAbstractModelIndex) – Index of node to remove
root

root node of the underlying tree

set_root(new_root)[source]

Sets a new tree for the model

Parameters:new_root (braviz.interaction.logic_bundle_model.LogicBundleNode) – Root of new tree

Helpers

class LogicBundleNode(parent, son_number, node_type, value, extra_data=None)[source]

A node for a bundle based on a logical hierarchy

See braviz.readAndFilter.bundles_db.get_logic_bundle_dict() for more information

Parameters:
  • parent (braviz.interaction.logic_bundle_model.LogicBundleNode) – Reference to parent node, None if root
  • son_number (int) – Number of siblings before the current node
  • node_type (int) – Type of the current node. May be 0 for logic, 1 for structure or 2 for roi
  • value – Logical operation for logic nodes, or structure name for structure nodes
  • extra_data (int) – Roi database index for roi nodes
add_son(node_type, value, extra_data=None)[source]

Add a son to the current node

Parameters:
  • node_type (int) – Node type of the new son
  • value – Value for the new son
  • extra_data – Extra data for the new son
add_son_from_dict(values)[source]

Adds a son from a dictionary

Parameters:values (dict) – Recursive dictionary with the following keys: node_type, value, extra_data, children (each of them as another dictionary with these keys)
decrease_son_number()[source]

Decrease my son number, call this when an older brother is removed

static from_dict(values)[source]

Construct a tree from a dictionary

Parameters:values (dict) – Recursive dictionary with the following keys for each node: node_type, value, extra_data, children (each of them as another dictionary with these keys)
Returns:The root node of the resulting tree
node_type

Type of this node, 0: Logical, 1: Structure or 2: Roi

parent

Parent node

remove_kid(index)[source]

Remove a child

Younger brother numbers are updated

Parameters:index (int) – number of son to remove
son_number

Number of sons added before this one

to_dict()[source]

Transform the tree based at this node into a dictionary

This allows for easy serialization (picking)

class LogicBundleNodeWithVTK(parent, son_number, node_type, value, extra_data=None, reader=None, subj=None, space='subject')[source]

Adds VTK drawing capabilities to the LogicBundleNode

Parameters:
prop

Get the vtkProp for the bundle

set_color(color)[source]

Set color for the actor

Parameters:color (tuple) – RGB components of the color
set_opacity(int_opac)[source]

Sets the opacity of the actor

Parameters:int_opac (int) – From 0 to 100 where 0 is invisible and 100 is opaque
update(subj, space)[source]

Change subject or coordinate system

Parameters:
static vtk_from_dict(values, reader, subj=None, space='subject')[source]

Create a tree from a recursive dictionary

Parameters:
Returns:

The root node of the resulting tree