def create_tree_from_clinical(clinical_object, concept_tree=None):
"""
:param clinical_object:
:param concept_tree:
:return:
"""
if not concept_tree:
concept_tree = ConceptTree()
column_map_ids = clinical_object.ColumnMapping.ids
no_bar = True if len(column_map_ids) < 200 else False
bar_format = '{l_bar}{bar} | {n_fmt}/{total_fmt} nodes ready, {rate_fmt}'
for var_id, variable in tqdm.tqdm_notebook(clinical_object.all_variables.items(),
bar_format=bar_format,
unit=' nodes',
leave=False,
dynamic_ncols=True,
disable=no_bar):
data_args = variable.column_map_data
# Don't need these, they're in the tree.
for k in [Mappings.cat_cd_s, Mappings.data_label_s]:
data_args.pop(k)
concept_path = path_converter(variable.concept_path, to_internal=True)
categories = {} if variable.is_numeric else variable.word_map_dict
if categories:
node_type = 'categorical'
else:
node_type = 'empty' if variable.is_empty else 'numeric'
# Store node type in `data` so it can be changed back after renaming OMIT
data_args.update({'ctype': node_type})
# Store column header of variable.
data_args.update({'dfh': variable.header})
# Add filename to SUBJ_ID and OMIT, this is a work around for unique path constraint.
if variable.data_label in {"SUBJ_ID", "OMIT"}:
concept_path = concept_path.replace("SUBJ ID", "SUBJ_ID")
node_type = 'codeleaf'
# Add categorical values to concept tree (if any)
for i, datafile_value in enumerate(categories):
oid = var_id.create_category(i + 1)
mapped = categories[datafile_value]
mapped = mapped if not pd.isnull(mapped) else ''
categorical_path = path_join(concept_path, mapped)
concept_tree.add_node(categorical_path, oid,
node_type='alpha',
data_args={Mappings.df_value_s: datafile_value})
concept_tree.add_node(concept_path, var_id,
node_type=node_type, data_args=data_args)
return concept_tree
评论列表
文章目录