 Hello everyone, I'm Emre. I live in Istanbul, Turkey. I'm a data scientist in Jon Snow Labs' healthcare team. We will talk about how to create a clinical knowledge graph by Spark NLP and Neo4j. Here is the agenda. I'll start explaining Spark NLP at first and then we will continue with Spark NLP in healthcare, how it works and the pillars of it. Next step will be our other main component to create a knowledge graph. Its name is Neo4j. It's a graph database. And then I will jump into clinical knowledge graph side. I mean, what should be the properties and features of the clinical knowledge graph and the points to be considered by the practitioners. I will share some of my experiences about knowledge graphs. And then here is the most important part of this talk, live demo, how we can create clinical knowledge graph by spark NLP and NEL4J. This will be hands-on part. So spark NLP is an open source library released in 2017 and right now it has around 40k daily downloads. Our monthly download is about 1.2 million and totally we hit the 10 million already. We support four languages, Python, R, Java and Scala. The goal was to be able to create a single unified NLP library that would require no other dependencies other than Spark itself and it should also work on clusters. By the way, there was no other library that could support to run on Spark clusters. We also wanted to take advantage of the transfer learning and implementing the latest and greatest state-of-the-art algorithms. So what are the modules of Spark NLP? We have public and enterprise versions. Public is totally free and the healthcare models are licensed one. We also have another library, which is called Spark OCR. As you know, mostly data entry starts from PDFs or images when it comes to clinical text. So EHRs, electronic healthcare records, or medical records usually go through Spark OCR for that and I'm not for that but I'm not cover that today. On the left hand side you see public modules. We are supporting more than 200 languages by using the same NER architecture which is another nice point to underline, because we can switch one language to another easily, as long as we have the relevant word embeddings or word coverage to accomplish certain tasks in LP. We have some deep learning based emotion detection, text classification algorithms or pre-trained models that you can just plug and play. So instead of trying to build your own pipeline you can just download and use the pre-trained pipelines and then just feed your data frame so you will end up with the latest like state-of-the-art resource using which were various word embeddings including glow and the latest cookies in a happy school kids in NLP Town, Bert, Elmo, Albert, so on and so forth. So we all support them and you can just use any model like a building block and like a plug and play. On the right side you see clinical versions. Like public models, we have four main clinical models. We have more than that, but let's just cover four of them. Clinical NER, extracting clinical entities, and we have around more than 50 different NER models pertained. Clinical NER models, which means like the pertained on certain clinical NLP data sets that are being used in clinical NLP challenges like I2B2 or N2C2 data sets. And we named them like anatomy NER, PHA NER, Pathology NER, GSL NER, so on and so forth. To extract the meaningful chunk given the task, we use these NER models. And on the other hand, clinical entity linking is assigning some SNOMED, ARICSTORM, ICD-10 and the LOINC codes to these entities detect through NER. So the assertion a very important model in the healthcare domain. It's like if the clinical note is talking about the patient's father, not the patient itself, we need to know that information so that we can assign different features. I mean, maybe we will create different features accordingly, or if it is not that, we can just filter out those symptoms maybe. For the identification, we remove or mask the insensitive information according to HIPAA rules. We need to hide or conceal some of the information and these are called the sensitive information and we need to de-identify or obfuscate them. Obfuscate means like replacing with the fake names or entities. So, we will also have some, we also have some pre-trained models that you can plug and play and combine with the public models. Okay. Next slide. We keep Spark NLP up to date and upgrade in every two weeks with releases. We have released more than 75 times. The main focus is here to have a single unified library for all NLP or NLU tasks, NLU needs. We are using Slack actively and also on the right side you see some comparisons between the NLP libraries with ours like spaCy, NLTK, Core NLP and Hugging Face. So let me show you a sample output of the Spark NLP model, Spark NLP pipeline. As you see, we can extract NERs, age, entity, gender entity, clinical finding entity, relative date entity, and dosage of the drug, and the frequency of the drug. the date entity and dosage of the drug and the frequency of the drug. And as you see, we can not just extracting these NERs, we can also map the codes of this some entities like clinical finding and the assertion status here, past SNOMED, ICD-10 and UMLS codes here. And also you see some drug related codes, ARIXNORM, NDC and we can calculate ACC risk adjustment score. These are the out-of-the-box results of structure of the healthcare NLP pipelines. First of all, everything starts with NER because it is the minimum meaningful chunk of any clinical text. The importance of NER might not be that much in other domain, but in healthcare, that's very important. Everything builds upon clinical NER models. NER means, as you know, the name entity recognition. And we have many others that, I mean, we have many others that have nothing to do with NER, but when it comes to extract some knowledge to get a sense of what's going on inside the document, inside the clinical document, we start with clinical NER model and then use the other models like I mean did the other models use the output of NERs as an input. What are these downstream models? These are clinical entity linking, assertion status, the identification and relation extraction. For example, assertion status model assigns some status if the favor and sort route is a problem entity that coming from NER. And association starts to try to find if it's absent or present. I mean, given the text and given the sentence that chunk lives in, NER chunk lives in. And if we can give another example, relation extraction models try to find relation between these entities okay this can be the temporal relation or the cause and effect like cause and effect relations okay so this is the why NER is very important and highly valuable in healthcare domain. Here is another example, and I just want to mention this. I mean, it seems to me annotating the data and NER are the brain and heart of the NLP body. And they manage every other tasks and subtasks. Recognition of name entities is basically the classification of tokens. NER tries to locate and classify predefined categories such as persons, locations, organizations, I mean hospitals, medical centers, measurements, so on and so forth. We use NER to downstream the related tasks to answer real-world questions like what? Which hospital and department have been admitted by the patient? Which clinical tests have been applied to this patient? And what are the results? What are the test results, which medication or procedure has been started and when. Relation extraction is another very important part of the NLP and we will mostly use this relation extraction to create knowledge graphs. And I mean, it is the task of predicting semantic relationships from the text. Relationships usually occur between NER chunks. occur between NER chunks and it is the core component to build relational knowledge graphs. I mean, the role of this relation extraction in NLP applications are question answering and summarizing and et cetera. are question answering and summarizing and etc. Clinical NER plays a key role in clinical NLP tasks to extract information from healthcare reports. You can use it for multiple purposes such as detecting temporal relationships between clinical events to drug-to-drug interactions, and the relation between medical problems to treatment or medication interactions, and so on and so forth. I will not go deeper into the importance of the relation extraction medical studies, but it is not less important than the ners so uh putting the clinical facts on a timeline this is this is the very big challenge in uh in lp world and the i will i will share some of my ideas about this task. I mean, let me talk about a little bit this issue. The automatic extraction of a patient's natural history from clinical texts or EHRs is a critical step. It helps to build intelligent systems that can reason about clinical variables and support decision-making. Any intelligence system should be able to extract medical concepts, data expressions, temporal relations and the order of temporal, I mean, the temporal ordering of medical events from the clinical text. But this task is, as I said before, is a challenge. I mean, it's very hard to tackle due to the domain-specific nature of clinical data, due to the domain-specific nature of clinical data, such as writing quality and the lack of structure and the presence of redundant information more generally. I think that we can handle this task by combining some rule-based methods, unified NLP library like Spark NLP, and the power of knowledge graph. To my mind, this combination is the most appropriate and the quickest way to put the clinical facts on a timeline. to put the clinical facts on a timeline. So let me give you some brief introduction to Neo4j. As you see, Emre likes Neo4j. As you see, I mean, it consists of the Neo4j. I mean, the graph databases consists of nodes and relationships and nodes and edges. Edges keep the relationship on it and they connect the nodes according to this relationship. Neo4j is a highly scalable native graph database and the core belief behind it, and the core belief behind it, connections between data are as important as the data itself. This is very nice, this is a very important point. The connections between data are as important as the data itself. I'm pretty sure that using the connections between data will create a competitive advantage to produce actionable insights in healthcare. So you see some features and properties of the graph database and the as you see it's nodes and mainly it's nodes and edges i just keep the relationships on it and the and connect the nodes and you you can traverse on the nodes and the edges according to your query so you see another, I mean the other kind of features of the Neo4j, it provides SSTX and it can be, and you can get license like MySQL, MongoDB, and you can run on it HeroQ, so on and so forth. I mean, you can get much more detailed information from the official website of Neo4j. And Cypher. Cypher is Neo4j's GraphQL language. It is a declarative pattern matching language. The declarative meaning you just tell it what you want, not how to get it. This is the same as SQL. As you see, it's also designed for graphs. This is not SQL, but this is cypher query languages okay sequel when we talk about the knowledge graphs or the man we talk about the graph databases it's all about patterns I mean you I mean you you just traverse according to your patterns on the on the graph database okay and as you see i mean the there is a two notes here and one relationship and this is the actor acted in this movie okay and this is the example of the uh cypher query uh the movie database. I mean match actor, the relationship kind type is acted in and the movie and you will return, you will get the name of the actor and the roles of the I mean the roles in the movie and title of the movie. roles in the movie and title of the movie. When we start talking about the knowledge graphs, here is important distinction. As you see, these are not graphs, these are charts. Okay? I mean, it's good to start using the right terminology because these are not graphs. These are just charts. Graph database is different from what these are. So, the purpose of the clinical knowledge graph, what should be? We need to create data points which accurately represents the patient history rather than creating a patient history which contains a variety of data points. This is not easy and because healthcare data are complex as I said before exist in multiple places the redundancy and structured and unstructured, and that you can find very, I mean, inconsistent definitions in the same text, so on and so forth. Okay, but the aim of the knowledge graph is, I mean, represent the patient's history, I mean, accurately represent the patient's history. And so it should be a representation and structure and a method, and it should organize the entire body of the clinical knowledge and also it should contain high relevancy in clinical data and it should be universally accessible. So this is the illustration of one of my conceptual graph models and here is the tip of the day especially for clinical ones before creating a knowledge graph you should spend some time working on conceptual graph models this is my experience otherwise it is very easy to create graphs that are irrelevant to your purpose. I mean, it will be completely a mess otherwise. Because it is very easy to connect the nodes to each other. After creating the knowledge graph, you should validate the graph by querying and cross-checking with the raw data, like an input and output analysis. And here is another example of the knowledge graph. As you see we just feed this text to Spark NLP and we create this knowledge graph. graph. It has like six echelons right here. It is not easy, but you can create a clinical knowledge graph using Spark and AlpineL4J. It's a good combination. So we end up with the presentation and let me jump into the live demo. First of all, I have created a notebook for you. This is the Spark NLP. I mean, this is Spark NLP and Neo4j notebook. How we can create, I mean, to show you how we can create a knowledge graph, clinical knowledge graph using Spark NLP and Neo4j. First of all, you can get Spark NLP free trial license from our website. And you can just try, I mean, shot for this for this task I think first of all you will just upload your license keys like this And after that, you will install some libraries, our Spark NLP library. I mean, the public version and the GSL and licensed version. PySpark 3.1.2, we are still using this version. And also, Spark NLP display library, if you want to display some visualizations uh i mean they are i mean using the output of the uh pipeline and then you will start the spark nlp gsl okay i'm using the 3.3.2 versions of the public one and the licensed one. And here is our pipeline. I mean, this is the relation extraction pipeline. This is post-logical relation extraction pipeline because we will feed the text about the drugs, okay? We have documenter. This is document assembler and sentence detector. We will apply tokenizer and we will get the word embeddings. And after that, we will get part of speech tags. And after that, we will apply medical NER converter to merge the NER tags, to create NER chunks. And we will apply dependency parser. And after that, lastly, we will use postulation relation extraction model to create the semantic relationship between the NER chunks. So let me run it. You can also reach this notebook from our website. I mean, the GitHub repo under the work I mean under the certification workshops okay and this is our helper function and this is our text While these are running, let me show you what is Neo4j and how can you use it, how can you interact with the Spark and LPN Neo4j at the same time. Okay, first of all, you can go to neo4j.com slash sandbox. You can launch a free sandbox and you will get this page and after that you will just select the blank sandbox it means an empty graph database and you can create it in a minute it's very short and as you see it's ready and you have three days and you can extend it uh for time for additional uh seven days and you will see some connection details here and we will use them okay so uh let me let me install some uh libraries to uh interact with the neo4j 4G graph database. I mean with these sandbox. This one and this is the helper function. Another helpful function and the update data is another helper function. And this is one of our main function and I will just talk about with this daily and you will see some I mean credentials here first of all you and your eye here is the URL okay you will just copy and paste all these all these stuffs from the right side to left side it will just copy and paste here. And here is the password. And the username is Neo4j. And when we run this code snippet, we will create a connection between this notebook and the Neo4j graph database. So what is this function what what this function do first of all check the uh check our relation extraction results okay here uh you will see some columns here relation and to one and to one begin and end uh chunk one and two and the others and you will see some confidence here so you can see that the chunk one is a relationship between the chunk tool in this manner okay and also chunk one is a duration chunk two is a drug I mean we will create a relationship between 5-day and Amok Selim using this function. Add NURs and relations. Okay and you will see some batch size here I mean it is for the efficiency and you can just walk on the I mean you can just I mean elaborate on this part okay later and the you can here is the here is the our main function here to create this relationship notes okay and we will create chunk two also as notes and we will create a relationship between them and this will be the edge okay we are connecting node one to not to okay so let me let me run it it's ready and we can go down first of all I highly suggest you to drop all constraints and create your own constraints at the beginning of this task because otherwise it will be a create and it will be a completely a mess. I mean, it can be a mess in a graph database site and you can just run this part and you can delete all the nodes, drop all the nodes. And we will create a constraint here. Constraint means means I mean just just to assure about the each any are is created only once I mean they are unique okay they will be unique at this part and I will feed relation DF here this this the data frame so this function and it's done in just in just in four seconds okay and we will see this not and edges if we try to visualize the graph database and let me run some queries on it these are the old nodes and the relationship relationship between them okay and here is the another another query I mean the node will be the mode one will be the add bill I mean add will related those its duration so on so forth okay okay as you see this patient used the advil I mean the 12 units sometimes 40 units for five days and one unit okay and you can just walk through the graph database according to your cipher query this is the cipher query and you can just spend some time about this visually visualizations okay and this is another one it's another one another one so I'm so sure okay let me show you the the visualize the graph you will just hit here the visualization visualization part is opening. By the way, while it's firing up, let me just show you another example. And I will not go through this example today, but you can just reach out from our GitHub repo. The first example is about the health care library. And the second example is the public one, the free one. You can also use graph extraction here, graph extraction annotator in SparkyNAPI to create graphs. Like what? Like this. The person, Jon Snow, was born in England and Jon Snow lives in New York. You can just find everything here like this. You can just try this part by yourself and you will see this graph after running this code snippets. Let me just visualize the graph database and we can just finish this webinar. Right. Here, as you see all the nodes and the relationships between nodes has been created and you can just visualize them using Neo4j. Thank you for listening. We can go through the Q&A part.
Creating a Clinical Knowledge Graph with Spark NLP and Neo4j - Webinar
The knowledge graph represents a collection of connected entities and their relations. A knowledge graph that is fueled by machine learning utilizes natural language processing to construct a comprehensive and semantic view of the entities. A complete knowledge graph allows answering and search systems to retrieve answers to given queries. In this study, we built a knowledge graph using Spark NLP models and Neo4j. The marriage of Spark NLP and Neo4j is very promising for creating clinical knowledge graphs to do a deeper analysis, Q&A tasks, and get insights.