<jupyter_start><jupyter_text>Deprecated - Connecting Brain region through BAMS informationThis script connects brain regions through BAMS conenctivity informtation.However, at this level the connectivity information has no reference to the original, and that is not ok. Thereby do **not** use this.<jupyter_code>### DEPRECATED

import pandas as pd
import re 
import itertools
from difflib import SequenceMatcher

root = "Data/csvs/basal_ganglia/regions"
sim_csv_loc = "/region_similarity.csv"


def similar(a, b):
    return SequenceMatcher(None, a, b).ratio()


## Prepare regions and regions_other csvs
df_all_regions = pd.read_csv(root + "/all_regions.csv", dtype="object")

df = pd.DataFrame(columns = ["ID1", "Region_name_1", "ID2", "Region_name_2", "Sim"])

# Put region names and ID into tuple list 
subset = df_all_regions[["ID", "Region_name"]]
region_name_tuples = [tuple(x) for x in subset.to_numpy()]

# Find all combinations of region_names and look at similarity in name
for a, b in itertools.combinations(region_name_tuples, 2):
    id1, reg1 = a
    id2, reg2 = b
    sim_score = similar(reg1, reg2)
    
    if(sim_score > 0.7):
        a_row = pd.Series([id1, reg1, id2, reg2, sim_score], index = ["ID1", "Region_name_1", "ID2", "Region_name_2", "Sim"])
        df = df.append(a_row, ignore_index=True)


# Store similarities
df_sorted = df.sort_values('Sim')
df_sorted.to_csv(root + sim_csv_loc, encoding='utf-8')

print("Similarities stored in", sim_csv_loc)
def get_count_of_type(label, session):
    q = "MATCH (n:%s) RETURN count(n)" % label
    res = session.run(q)
    print("Added", res.value()[0], "nodes of type", label)
    
def get_count_of_relationship(label, session):
    q = "MATCH ()-[r:%s]-() RETURN count(*)" %label
    res = session.run(q)
    print("Added", res.value()[0], "relationships of type", label)

def get_csv_path(csv_file):
    path_all_csv = os.path.realpath("Data/csvs/basal_ganglia/regions")
    return os.path.join(path_all_csv, csv_file).replace("\\","/")


## Then find the regions that correspond to each other and stor that in a new CSV file

# Add relation to all areas that define positions
positioning = ["caudal", "rostral", "ventral", "dorsal"]
area_describing = ["internal", "compact", "core", "shell"]

df_sims = pd.read_csv(root + sim_csv_loc, converters = {"Sims": float})

# ALl with score above 0.95 are the same
    # Also the same: Substantia innominata, basal",103,"Substantia innominata, basal part" 0.91
    
df_equals = df_sims.loc[df_sims['Sim'] > 0.95]
df_sorted.to_csv(root + "/regions_equal.csv", encoding='utf-8')

from neo4j import GraphDatabase, basic_auth
from dotenv import load_dotenv
import os

load_dotenv()

neo4jUser = os.getenv("NEO4J_USER")
neo4jPwd = os.getenv("NEO4J_PASSWORD")

driver = GraphDatabase.driver("bolt://localhost:7687",auth=basic_auth(neo4jUser, neo4jPwd))

# Relationship EQUALS between equal BrainRegion nodes
csv_file_path = "file:///%s" % get_csv_path("regions_equal.csv")
query="""
        LOAD CSV WITH HEADERS FROM "%s" AS row
        MATCH (a:BrainRegion { id: row.ID1})
        MATCH (c:BrainRegion { id: row.ID2 })
        MERGE (a)-[:EQUALS]->(c)
    """ % csv_file_path

with driver.session() as session:
    session.run(query)
    get_count_of_relationship("EQUALS", session)

## TODO add rel for belongs-to/part of<jupyter_output>Added 6124 relationships of type EQUALS
{'fraction_non_alphanumeric': 0.13185840707964602, 'fraction_numerical': 0.01592920353982301, 'mean_word_length': 4.699159663865546, 'pattern_counts': {'":': 1, '<': 4, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '1218', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1241, 'n_tokens_neox': 1178, 'n_words': 320, 'path': '1. Extending the dataset with data from other sources/X - Deprecated - Connecting the regions through BAMS information.ipynb', 'repo_name': 'marenpg/jupyter_basal_ganglia', 'size': '3390'}