/
Examples

Examples

 

Introduction

In this part is presented a detailed description of the available CogStack examples, which currently are:

  • Example 1 – Processing a simple, structured dataset from a single DB source.

  • Example 2 – Processing a combined structured and free-text dataset from a single DB source (as in Quickstart).

  • Example 3 – Processing a combined dataset from multiple DB sources, multiple jobs.

  • Example 4 – Processing a combined dataset with embedded documents from a single DB source.

  • Example 5 – 2-step processing of a combined dataset with embedded documents from a single DB source.

  • Example 6 – Basic security use-case: Example 2 extended reverse proxy enabling a secure access.

  • Example 7 – Logging: Example 6 extended with logging mechanisms.

  • Example 8 – Simple NLP use-case: drug annotation using GATE and based on Example 2.

  • Example 9 – Defining multi-component pipelines: Example 4 and Example 8 combined.

  • Example 10 –  Ingesting free-text data from DB source to ES, annotating with MedCAT and re-ingesting back to ES. 

  • Sample production deployment - structured project setup of files to be used for production deployments

The main directory with resources used in this tutorial is available in the the CogStack bundle under examples directory.

Some parts of this document are also used in CogStack Quickstart tutorial.



Note

This tutorial is based on the newest version of CogStack 1.3.0.

The previous versions of CogStack and documentation can be found in the official github repository.





Index :



How are the examples organized

Each of the examples is organized in a way that it can be deployed and run independently. The directory structure of examples/ tree is as follows:

. ├── docker-common │   ├── elasticsearch │   │   └── config │   │   └── elasticsearch.yml │   ├── fluentd │   │   └── conf │   │   └── fluent.conf │   ├── kibana │   │   └── config │   │   └── kibana.yml │   ├── nginx │   │   ├── auth │   │   └── config │   │   └── nginx.conf │   ├── pgsamples │   │   └── init_db.sh │   ├── pgjobrepo │   │ └── create_repo.sh │   └── docker.compose.yml ├── example1 │   ├── cogstack │   │   └── observations.properties │   ├── db_dump │   │   └── db_samples.sql.gz │   ├── docker │   │   └── docker-compose.override.yml │   ├── extra │   │   ├── db_create_schema.sql │   │   └── prepare_db.sh │   └── setup.sh ├── example10 │   ├── cogstack-pipeline │   │   ├── annotation_ingester │   │   │   └── config │   │   │   └── config.yml │   │   ├── cogstack │   │   │   ├── conf │   │   │   │   └── step-1 │   │   │   │   └── es_ingestion.properties │   │   │   └── run_pipeline.sh │   │   ├── docker-compose.yml │   │   ├── elasticsearch │   │   │   ├── config │   │   │   │   └── elasticsearch.yml │   │   │   └── scripts │   │   │   └── es_index_initializer.py │   │   ├── kibana │   │   │   └── config │   │   │   └── kibana.yml │   │   ├── medcat_service │   │   │   └── envs │   │   │   ├── env_app │   │   │   ├── env_medcat │   │   │   ├── env_medcat_snomed │   │   │   └── env_medcat_umls │   │   └── scripts │   │   ├── create_pgsql_job_repo.sh │   │   └── create_source_and_sink_db.sh │   ├── data │   │   ├── db_samples.sql.gz │   │   └── models │   │   └── download_medmen.sh │   ├── README ├── example2 │   ├── cogstack │   │   └── observations.properties │   ├── db_dump │   │   └── db_samples.sql.gz │   ├── docker │   │   └── docker-compose.override.yml │   ├── extra │   │   ├── db_create_schema.sql │   │   └── prepare_db.sh │   └── setup.sh ├── example3 │   ├── cogstack │   │   ├── gen_config.sh │   │   ├── mt.properties │   │   └── template.properties │   ├── db_dump │   │   ├── db_samples-mt.sql.gz │   │   └── db_samples-syn.sql.gz │   ├── docker │   │   └── docker-compose.override.yml │   ├── extra │   │   ├── db_create_mt_schema.sql │   │   ├── db_create_syn_schema.sql │   │   ├── prepare_mtsamples_db.sh │   │   └── prepare_synsamples_db.sh │   └── setup.sh ├── example4 │   ├── cogstack │   │   ├── observations.properties │   │   └── test2.sh │   ├── db_dump │   │   ├── db_samples-docx-small.sql.gz │   │   ├── db_samples-jpg-small.sql.gz │   │   ├── db_samples-pdf-img-small.sql.gz │   │   └── db_samples-pdf-text-small.sql.gz │   ├── docker │   │   └── docker-compose.override.yml │   ├── extra │   │   ├── db_create_schema.sql │   │   ├── prepare_db.sh │   │   └── prepare_single_db.sh │   └── setup.sh ├── example5 │   ├── cogstack │   │   ├── conf │   │   │   ├── step-1 │   │   │   │   └── reports.properties │   │   │   └── step-2 │   │   │   └── observations.properties │   │   └── test2.sh │   ├── db_dump │   │   ├── db_samples-docx-small.sql.gz │   │   ├── db_samples-jpg-small.sql.gz │   │   ├── db_samples-pdf-img-small.sql.gz │   │   └── db_samples-pdf-text-small.sql.gz │   ├── docker │   │   └── docker-compose.override.yml │   ├── extra │   │   ├── db_create_schema.sql │   │   ├── prepare_db.sh │   │   └── prepare_single_db.sh │   └── setup.sh ├── example6 │   ├── cogstack │   │   └── observations.properties │   ├── db_dump │   │   └── db_samples.sql.gz │   ├── docker │   │   └── docker-compose.override.yml │   └── setup.sh ├── example7 │   ├── cogstack │   │   └─── observations.properties │   ├── db_dump │   │   └── db_samples.sql.gz │   ├── docker │   │   └── docker-compose.override.yml │   └── setup.sh ├── example8 │   ├── cogstack │   │   └── observations.properties │   ├── db_dump │   │   └── db_samples.sql.gz │   ├── docker │   │   └── docker-compose.override.yml │   ├── gate │   │   └── app │   │   ├── active.lst │   │   ├── drug.gapp │   │   ├── drug.lst │   │   └── lists.def │   ├── extra │   │   └── clean_list.py │   └── setup.sh ├── example9 │   ├── cogstack │   │   └── observations.properties │   ├── db_dump │   │   └── db_samples.sql.gz │   ├── docker │   │   └── docker-compose.override.yml │   ├── gate │   │   └── app │   │   ├── active.lst │   │   ├── drug.gapp │   │   ├── drug.lst │   │   └── lists.def │   └── setup.sh ├── rawdata │   ├── mtsamples-txt-full.tgz │   ├── mtsamples-txt-small.tgz │   └── synsamples.tgz ├── download_db_dumps.sh ├── prepare_db_dumps.sh └── prepare_docs.sh



Info

Note: the contents of db_dump subdirectories for each example will be created after running download_db_dumps.sh script (please see below).







Common and reusable components

The directory docker-common contains some common components and microservice configuration files that are used within all the examples. These components include:

  • PostgreSQL databases: pgsamples and pgjobrepo directories,

  • ElasticSearch node: elasticsearch directory,

  • Kibana webservice dashboard: kibana directory,

  • nginx reverse proxy service: nginx directory,

  • Fluentd logging driver: fluentd directory,

  • Common microservices Docker Compose base configuration file used across examples: docker-compose.yml.

Examples

The directories example* stores the content of the examples, each containing such subdirectories:

  • cogstack directory containing CogStack configuration files and/or custom pipeline scripts,

  • db_dump directory containing database dumps used to initialize the samples input database,

  • docker directory containing configuration files for docker-based deployment,

  • extra directory containing scripts to generate database dumps locally (optional) or additional, useful materials,

  • setup.sh script to initialize the example before running it for the first time.

For a detailed description of each example please refer to its appropriate section.

Raw data

The directory rawdata contains the raw EHRs data which will be used to prepare the initial database dumps for running the examples. The datasets used in all the examples are the ones used in the Quickstart guide – please refer to it for a more detailed description of the available datasets.

Data preparation scripts

The script prepare_docs.sh is used to prepare the document data for Example 4 and Example 5 in PDFs, DOCX and image formats.

The script prepare_db_dumps.sh is used to prepare locally all the database dumps to initialize the examples

However, the script download_db_dumps.sh is used to automatically download all the database dumps.





General information about examples

Database schema

In the current implementation, CogStack can only ingest EHR data from a specified input database and relation. This is why, in order to process the sample patient data covered in this tutorial, one needs to create an appropriate database schema and load the EHR data into a preferred data sink. The data can be stored either as records in a database, documents in an ElasticSearch cluster or as files in a filesystem.

The most commonly used data sink is going to be ElasticSearch cluster (in our examples – just a single node). However, as relational join statements have a high performance burden for ElasticSearch, the EHR data is best to be stored denormalized in ElasticSearch. This is why, for the moment, we rely on ingesting the data from additional view(s) created in the sample database.

Properties file

Database source

When using PostgreSQL database as a data source, please note that the source.poolSize property defines the maximum size of the connection pool available for performing queries by CogStack engine. A PostgreSQL database, by default, has a maximum connection limit set to 100, hence exceeding the limit (either by a single job or multiple parallel ones) may lead to termination of the data pipeline.

One of the solutions to overcome this issue can be to run the PostgreSQL container with additional options specified in Docker-compose file:

command: "-c 'shared_buffers=256MB' -c 'max_connections=1000'"

extending the connection limit with the available RAM for connection buffers.

ElasticSearch sink

As an additional feature, security and ssl encryption can be enabled for communication with ElasticSearch. However, it uses the ElasticSearch X-Pack bundle and requires license for commercial deployments, hence it is disabled by default.

Moreover, the ElasticSearch and Kibana images that are used in the tutorial are not bundled with X-Pack, using the -oss variant of the image as defined in examples/docker-common/docker-compose.yml .

Partitioner

In the current implementation, CogStack engine can only partition the data using the records’ primary key (partitioner.pkColumnName property containing unique values) and records’ update time (partitioner.timeStampColumnName property) as defined in created views. Also, the table used to partition (partitioner.tableToPartition property) needs to be specified. This is specified by PKTimeStamp partitioning method as shown below, e.g.:

partitioner.partitionType = PKTimeStamp
partitioner.tableToPartition = <table-name> partitioner.pkColumnName = <primary-key-column-name>
partitioner.timeStampColumnName = <timestamp-column-name>





Running CogStack

Setup

For the ease of use CogStack is being deployed and run using Docker. However, before starting the CogStack ecosystem for the first time, a setup scripts needs to be run locally to prepare the Docker images and configuration files for CogStack data processing engine. For each of the examples, a script is available in its directory examples/example*/ path and can be run as:

bash setup.sh

As a result, a temporary directory __deploy/ will be created containing all the necessary artifacts to deploy CogStack.

Docker-based deployment

Next, we can proceed to deploy CogStack ecosystem using Docker Compose. It will configure and start microservices based on the provided Compose files:

  • common base configuration, copied from examples/docker-common/docker-compose.yml ,

  • example-specific configuration copied from examples/example*/docker/docker-compose.override.yml.

Moreover, the PostgreSQL database container comes with pre-initialized database dump ready to be loaded directly into.



In order to run CogStack, type in the examples/example*/__deploy/ directory:

docker-compose up

In the console there will be printed status logs of the currently running microservices. For the moment, however, they may be not very informative (we’re working on that).





Connecting to the microservices

CogStack ecosystem

The picture below sketches a general idea on how the microservices are running and communicating within a sample CogStack ecosystem.

Assuming that everything is working fine, we should be able to connect to the running microservices.

Kibana and ElasticSearch

Kibana dashboard used to query the EHRs can be accessed directly in browser via URL: http://localhost:5601/. The data can be queried using a number of ElasticSearch indices, e.g. sample_observations_view. Usually, each index will correspond to the database view in db_samples (samples-db PostgreSQL database) from which the data was ingested. However, when entering Kibana dashboard for the first time, an index pattern needs to be configured in the Kibana management panel – for more information about its creation, please refer to the official Kibana documentation.

In addition, ElasticSearch REST end-point can be accessed via URL http://localhost:9200/. It can be used to perform manual queries or to be used by other external services – for example, one can list the available indices:

curl 'http://localhost:9200/_cat/indices'

or query one of the available indices – sample_observations_view:

curl 'http://localhost:9200/sample_observations_view'

For more information about possible documents querying or modification operations, please refer to the official ElasticSearch documentation.

As a side note, the name for ElasticSearch node in the Docker Compose has been set as elasticsearch-1. The -1 ending emphasizes that for larger-scale deployments, multiple ElasticSearch nodes can be used – typically, a minimum of 3.

PostgreSQL sample database

Moreover, the access PostgreSQL database with the input sample data is exposed directly at localhost:5555. The database name is db_sample with user test and password test. To connect, one can run:

psql -U 'test' -W -d 'db_samples' -h localhost -p 5555





Example 1

General information

This is a very basic example covering only processing and ingestion of structured synthetic data into ElasticSearch. However, it forms a good base for starting to work with CogStack data processing pipelines.

Database schema

Patients table

The first 5 records of patient data (file: patients.csv from Synthea-based samples) in CSV format is presented below:

ID,BIRTHDATE,DEATHDATE,SSN,DRIVERS,PASSPORT,PREFIX,FIRST,LAST,SUFFIX,MAIDEN,MARITAL,RACE,ETHNICITY,GENDER,BIRTHPLACE,ADDRESS,CITY,STATE,ZIP b9f5a11b-211d-4ced-b3ba-12012c83b937,1939-08-04,1996-03-15,999-11-9633,S99999830,X106007X,Mr.,Brady,Lynch,,,M,white,polish,M,Worcester,701 Schiller Esplanade,Fitchburg,Massachusetts,01420 fab43860-c3be-4808-b7b4-00423c02816b,1962-06-21,2011-03-10,999-67-8307,S99958025,X26840237X,Mrs.,Antonia,Benavides,,Padrón,M,hispanic,mexican,F,Rockland,643 Hand Bay,Boston,Massachusetts,02108 84dd6378-2ddc-44b6-9292-2a4461bcef53,1998-12-01,,999-50-5147,S99987241,,Mr.,Keith,Conn,,,,white,english,M,Rockland,461 Spinka Extension Suite 69,Framingham,Massachusetts,01701 9929044f-1f43-4453-b2c0-a2f45dcdd4be,2014-09-23,,999-64-4171,,,,Derrick,Lakin,,,,white,irish,M,Tewksbury,577 Hessel Lane,Hampden,Massachusetts,

The patients table definition in PostgreSQL according to the specification:

CREATE TABLE patients ( id UUID PRIMARY KEY, birthdate DATE NOT NULL, deathdate DATE, ssn VARCHAR(64) NOT NULL, drivers VARCHAR(64), passport VARCHAR(64), prefix VARCHAR(8), first VARCHAR(64) NOT NULL, last VARCHAR(64) NOT NULL, suffix VARCHAR(8), maiden VARCHAR(64), marital CHAR(1), race VARCHAR(64) NOT NULL, ethnicity VARCHAR(64) NOT NULL, gender CHAR(1) NOT NULL, birthplace VARCHAR(64) NOT NULL, address VARCHAR(64) NOT NULL, city VARCHAR(64) NOT NULL, state VARCHAR(64) NOT NULL, zip VARCHAR(64) ) ;

Encounters table

Similarly, the first 5 records of patient encounters data (file: encounters.csv)

with the corresponding encounters table definition:

Observations table

The next table is observations, where the first 5 rows of observations.csv file are:

and the corresponding table definition:

Here, with -- (*) have been marked additional fields with auto-generated values. These are: cid – an automatically generated primary key and created – a record's creation timestamp. They will be later used by CogStack engine for data partitioning when processing the records. The patient and encounters tables have their primary keys (id field) already defined (of UUID type) and are included in the input CSV files.

Database views

Next, we define a observations_view that will be used by CogStack data processing engine to ingest the records from input database:

The goal here is to denormalize the database schema for CogStack and ElasticSearch data ingestion, as the observations table is referencing both the patient and encounters tables by their primary key. In the current implementation, CogStack pipeline engine cannot yet perform dynamic joins over the relational data from specific database tables.

Some of the crucial fields required for configuring CogStack Pipeline engine with Document data model have been marked with --(*) – these are:

  • observation_id – the unique identifier of the observation record (typically, the primary key),

  • observation_timestamp – the record creation or last update time.

These fields will be later used when preparing the properties configuration file for CogStack data processing workflow.

Properties file

General information

Each CogStack data processing pipeline is configured using a number of parameters defined in the corresponding Java properties file</