Skip to content

Workshop

Binder

Digital History: The Story So Far

As the field of Digital History continues to grow, so too does the number of tools, software, and coding packages built to support and advance digital history in practice. The range of this is at times staggering: from applications suitable for the most novice of digital historians, to coding guides and tools for those working to more nuanced and specific end-goals, researchers have an ability to engage with their materials in digital, quantitative ways on a never before seen level. Often we focus primarily on the new findings that come of out this new way of approaching research - but what about the ways we get to those findings?

Regardless of the type of digital analysis being performed or even the software being used, the process is normally the same: input some data, click some buttons or run some code (perhaps a couple of times over to edit the code and adjust the outcomes), and get your end result.

You've got an outcome - but do you know how you've got from a to b? It's likely that variables have been written over several times along the way, and the data has changed from one type to another, been filtered or added to, and decision after decision has been made without necessarily knowing it. Each little adjustment or re-run of the code has contributed to the research process and is critical to the end output or findings.

But how do we keep track?

Hello kiara.

Introducing kiara, a new data orchestration tool.

This new tool incorporates a number of different digital research approaches, but most importantly documents and encourages users to critically reflect on the process and use of DH tools. In doing so, the software opens up the black box of digital research, moving away from button-clicking software and making digital research more transparent and open to commentary, replicability, and criticism. It not only makes the research process itself more open, allowing users to visualise and examine the individual steps from start to finish, but also allows them to track changes to the data itself, something that is either imperceptible or, perhaps more importantly, forgotten about in traditional digital history methods and tools. kiara therefore acts as a 'wrapper' to this digital reserach process, tracking and documenting the steps and changes to the data, producing a veritable map of the journey that can be reflected upon and shared.

This tutorial will walk you through installation of kiara in Jupyter Notebooks, and some basic but essential functions that can be built on in further notebooks. At the end, it will showcase the data lineage, having tracked the research process and changes to the data from start to finish.

This tutorial requires you to know python and SQL.

Installation

First, we need to check if kiara and its plugins are already installed, and install them if not. There are currently seven plugins:

All of these will be installed automatically alongside kiara, using the code below:

try:
    from kiara_plugin.jupyter import ensure_kiara_plugins
except:
    import sys
    print("Installing 'kiara_plugin.jupyter'...")
    !{sys.executable} -m pip install -q kiara_plugin.jupyter
    from kiara_plugin.jupyter import ensure_kiara_plugins

ensure_kiara_plugins()

Running kiara

In order to use kiara, we need to create a KiaraAPI instance. An API allows us to control and interact with kiara and its functions. In kiara this also allows us to get more information about what can be done (and what is happening) to our data as we go. For more on what can be done with the API, see the kiara API documentation here.

from kiara import KiaraAPI

kiara = KiaraAPI.instance()

Now we have an API in place, we can get more information about what we can do in kiara. Let's start by asking kiara to list all the operations that are included with the plugins we just installed.

kiara.list_operation_ids()
['create.database.from.file',
 'create.database.from.file_bundle',
 'create.database.from.table',
 'create.network_data.from.files',
 'create.network_data.from.tables',
 'create.stopwords_list',
 'create.table.from.file',
 'create.table.from.file_bundle',
 'date.check_range',
 'date.extract_from_string',
 'download.file',
 'download.file_bundle',
 'export.file.as.file',
 'export.network_data.as.csv_files',
 'export.network_data.as.graphml_file',
 'export.network_data.as.sql_dump',
 'export.network_data.as.sqlite_db',
 'export.table.as.csv_file',
 'extract.date_array.from.table',
 'file_bundle.pick.file',
 'file_bundle.pick.sub_folder',
 'filter.table',
 'generate.LDA.for.tokens_array',
 'import.database.from.local_file_path',
 'import.file',
 'import.file_bundle',
 'import.local.file',
 'import.local.file_bundle',
 'import.network_data.from.local_file_paths',
 'import.table.from.local_file_path',
 'import.table.from.local_folder_path',
 'jupyter.example',
 'kiara_plugin.my_kiara_module.my_kiara_module.tutorial_module',
 'list.contains',
 'logic.and',
 'logic.nand',
 'logic.nor',
 'logic.not',
 'logic.or',
 'logic.xor',
 'my_kiara_module.example',
 'onboard.zenodo_record',
 'parse.date_array',
 'preprocess.tokens_array',
 'query.database',
 'query.table',
 'remove_stopwords.from.tokens_array',
 'render.markdown.to.html',
 'string_filter.tokens',
 'table.pick.column',
 'table_filter.drop_columns',
 'table_filter.select_columns',
 'table_filter.select_rows',
 'tokenize.string',
 'tokenize.texts_array']

Downloading Files

Great, now we know the different kind of operations we can use with kiara. Let's start by introducing some files to our notebook, using the download.file function.
First we want to find out what this operation does, and just as importantly, what inputs it needs to work.

kiara.retrieve_operation_info('download.file')
                                                                                                                                            
 Documentation                                                                                                                              
                     Download a single file from a remote location.                                                                         
                                                                                                                                            
                     The result of this operation is a single value of type 'file' (basically an array of raw bytes), which can then be     
                     used in other modules to create more meaningful data structures.                                                       
                                                                                                                                            
 Author(s)                                                                                                                                  
                     Markus Binsteiner   markus@frkl.io                                                                                     
                                                                                                                                            
 Context                                                                                                                                    
                     Tags         onboarding                                                                                                
                     Labels       package: kiara_plugin.onboarding                                                                          
                     References   source_repo: https://github.com/DHARPA-Project/kiara_plugin.onboarding                                    
                                  documentation: https://DHARPA-Project.github.io/kiara_plugin.onboarding/                                  
                                                                                                                                            
 Operation details                                                                                                                          
                     Documentation   Download a single file from a remote location.                                                         
                                                                                                                                            
                                     The result of this operation is a single value of type 'file' (basically an array of raw bytes),       
                                     which can then be used in other modules to                                                             
                                     create more meaningful data structures.                                                                
                                                                                                                                            
                     Inputs                                                                                                                 
                                       field name   type     description                                    Required   Default              
                                      ──────────────────────────────────────────────────────────────────────────────────────────────────    
                                       url          string   The url of the file to download.               yes        -- no default --     
                                       file_name    string   The file name to use for the downloaded        no         -- no default --     
                                                             file.                                                                          
                                                                                                                                            
                                                                                                                                            
                     Outputs                                                                                                                
                                       field name          type   description                                                               
                                      ──────────────────────────────────────────────────────────────────────────────────────────────────    
                                       file                file   The downloaded file.                                                      
                                       download_metadata   dict   Metadata about the download.                                              
                                                                                                                                            
                                                                                                                                            

So from this, we know that download.file will download a single file from a remote location for us to use in kiara.
We need to give the function a url and, if we want, a file name. These are the inputs.
In return, we will get the file and metadata about the file as our outputs.

Let's give this a go using some kiara sample data.

First we define our inputs, then use kiara.run_job with our chosen operation, download.file, and save this as our outputs.

inputs = {
        "url": "https://raw.githubusercontent.com/DHARPA-Project/kiara.examples/main/examples/data/journals/JournalNodes1902.csv",
        "file_name": "JournalNodes1902.csv"
}

outputs = kiara.run_job('download.file', inputs=inputs)

Let's print out our outputs and see what that looks like.

outputs
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│                                                                                                                                          │
│   field               value                                                                                                              │
│  ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────  │
│   download_metadata                                                                                                                      │
│                         dict data     {                                                                                                  │
│                                         "response_headers": [                                                                            │
│                                           {                                                                                              │
│                                             "connection": "keep-alive",                                                                  │
│                                             "content-length": "7436",                                                                    │
│                                             "cache-control": "max-age=300",                                                              │
│                                             "content-security-policy": "default-src 'none'; style-src 'unsafe-inline'; sandbox",         │
│                                             "content-type": "text/plain; charset=utf-8",                                                 │
│                                             "etag": "W/\"641ae85d69e5836d27ea8906aba0a33b48b0f3ed0ed4c40d21a07fccebdd238d\"",            │
│                                             "strict-transport-security": "max-age=31536000",                                             │
│                                             "x-content-type-options": "nosniff",                                                         │
│                                             "x-frame-options": "deny",                                                                   │
│                                             "x-xss-protection": "1; mode=block",                                                         │
│                                             "x-github-request-id": "BCA0:8395:120207:18015C:63D13F8F",                                   │
│                                             "content-encoding": "gzip",                                                                  │
│                                             "accept-ranges": "bytes",                                                                    │
│                                             "date": "Wed, 25 Jan 2023 14:45:37 GMT",                                                     │
│                                             "via": "1.1 varnish",                                                                        │
│                                             "x-served-by": "cache-mad22090-MAD",                                                         │
│                                             "x-cache": "HIT",                                                                            │
│                                             "x-cache-hits": "1",                                                                         │
│                                             "x-timer": "S1674657938.833565,VS0,VE1",                                                     │
│                                             "vary": "Authorization,Accept-Encoding,Origin",                                              │
│                                             "access-control-allow-origin": "*",                                                          │
│                                             "x-fastly-request-id": "fd2c4c0f401ab6fe7d99f59bd87eedb32cc2e35f",                           │
│                                             "expires": "Wed, 25 Jan 2023 14:50:37 GMT",                                                  │
│                                             "source-age": "258"                                                                          │
│                                           }                                                                                              │
│                                         ],                                                                                               │
│                                         "request_time": "2023-01-25T14:45:37.854290+00:00"                                               │
│                                       }                                                                                                  │
│                         dict schema   {                                                                                                  │
│                                         "title": "dict",                                                                                 │
│                                         "type": "object"                                                                                 │
│                                       }                                                                                                  │
│                                                                                                                                          │
│   file                Id,Label,JournalType,City,CountryNetworkTime,PresentDayCountry,Latitude,Longitude,Language                        │
│                       75,Psychiatrische en neurologische bladen,specialized: psychiatry and                                              │
│                       neurology,Amsterdam,Netherlands,Netherlands,52.366667,4.9,Dutch                                                    │
│                       36,The American Journal of Insanity,specialized: psychiatry and neurology,Baltimore,United States,United           │
│                       States,39.289444,-76.615278,English                                                                                │
│                       208,The American Journal of Psychology,specialized: psychology,Baltimore,United States,United                      │
│                       States,39.289444,-76.615278,English                                                                                │
│                       295,Die Krankenpflege,specialized: therapy,Berlin,German Empire,Germany,52.52,13.405,German                        │
│                       296,Die deutsche Klinik am Eingange des zwanzigsten Jahrhunderts,general medicine,Berlin,German                    │
│                       Empire,Germany,52.52,13.405,German                                                                                 │
│                       300,Therapeutische Monatshefte,specialized: therapy,Berlin,German Empire,Germany,52.52,13.405,German               │
│                       1,Allgemeine Zeitschrift für Psychiatrie,specialized: psychiatry and neurology,Berlin,German                       │
│                       Empire,Germany,52.52,13.405,German                                                                                 │
│                       7,Archiv für Psychiatrie und Nervenkrankheiten,specialized: psychiatry and neurology,Berlin,German                 │
│                       Empire,Germany,52.52,13.405,German                                                                                 │
│                       10,Berliner klinische Wochenschrift,general medicine,Berlin,German Empire,Germany,52.52,13.405,German              │
│                       13,Charité Annalen,general medicine,Berlin,German Empire,Germany,52.52,13.405,German                               │
│                       21,Monatsschrift für Psychiatrie und Neurologie,specialized: psychiatry and neurology,Berlin,German                │
│                       Empire,Germany,52.52,13.405,German                                                                                 │
│                       29,Virchows Archiv,"specialized: anatomy, physiology and pathology",Berlin,German                                  │
│                       Empire,Germany,52.52,13.405,German                                                                                 │
│                       31,Zeitschrift für pädagogische Psychologie und Pathologie,specialized: psychology and pedagogy,Berlin,German      │
│                       Empire,Germany,52.52,13.405,German                                                                                 │
│                       42,Vierteljahrsschrift für gerichtliche Medizin und öffentliches Sanitätswesen,"specialized: anthropology,         │
│                       criminology and forensics",Berlin,German Empire,Germany,52.52,13.405,German                                        │
│                       47,Centralblatt für Nervenheilkunde und Psychiatrie,specialized: psychiatry and neurology,Berlin,German            │
│                       Empire,Germany,52.52,13.405,German                                                                                 │
│                       50,Russische medicinische Rundschau,general medicine,Berlin,German Empire,Germany,52.52,13.405,German              │
│                       76,Deutsche Aerzte-Zeitung,general medicine,Berlin,German Empire,Germany,52.52,13.405,German                       │
│                       87,Monatsschrift für Geburtshülfe und Gynäkologie,specialized: gynecology,Berlin,German                            │
│                       Empire,Germany,52.52,13.405,German                                                                                 │
│                       108,Archiv für klinische Chirurgie,specialized: surgery,Berlin,German Empire,Germany,52.52,13.405,German           │
│                       113,Zeitschrift für klinische Medicin,general medicine,Berlin,German Empire,Germany,52.52,13.405,German            │
│                       159,Deutsche militärärztliche Zeitschrift,specialized: military medicine,Berlin,German                             │
│                       Empire,Germany,52.52,13.405,German                                                                                 │
│                       162,Jahresbericht über die Leistungen und Fortschritte auf dem Gebiete der Neurologie und                          │
│                       Psychiatrie,specialized: psychiatry and neurology,Berlin,German Empire,Germany,52.52,13.405,German                 │
│                       192,Ärztliche Sachverständigen-Zeitung,general medicine,Berlin,German Empire,Germany,52.52,13.405,German           │
│                       198,Zeitschrift für die Behandlung Schwachsinniger und Epileptischer,specialized: psychiatry and                   │
│                       neurology,Berlin,German Empire,Germany,52.52,13.405,German                                                         │
│                       258,Der Pfarrbote,news media,Berlin,German Empire,Germany,52.52,13.405,German                                      │
│                       71,Correspondenz-Blatt für Schweizer Aerzte,general                                                                │
│                       medicine,Bern,Switzerland,Switzerland,46.948056,7.4475,German                                                      │
│                       6,Archiv für mikroskopische Anatomie,"specialized: anatomy, physiology and pathology",Bonn,German                  │
│                       Empire,Germany,50.733333,7.1,German                                                                                │
│                       203,The Journal of Abnormal Psychology,specialized: psychology,Boston,United States,United                         │
│                       States,42.358056,-71.063611,English                                                                                │
│                       273,"Correspondenz-Blatt der Deutschen Gesellschaft für Anthropologie, Ethnologie und                              │
│                       Urgeschichte","specialized: anthropology, criminology and forensics",Braunschweig,German                           │
│                       Empire,Germany,52.266667,10.516667,German                                                                          │
│                       303,Policlinique de Bruxelles,general medicine,Brussels,Belgium,Belgium,50.85,4.35,French                          │
│                       306,Annales de la Société Belge de Neurologie,specialized: psychiatry and                                          │
│                       neurology,Brussels,Belgium,Belgium,50.85,4.35,French                                                               │
│                       19,Journal de neurologie,specialized: psychiatry and neurology,Brussels,Belgium,Belgium,50.85,4.35,French          │
│                       25,"Revue internationale d'électrothérapie, de physiologie, de médecine, de chirurgie, d'obstétrique, de           │
│                       thérapeutique, de chimie et de pharmacie",general medicine,Brussels,Belgium,Belgium,50.85,4.35,French              │
│                       35,Bulletin de la Société de Médecine Mentale de Belgique,specialized: psychiatry and                              │
│                       neurology,Brussels,Belgium,Belgium,50.85,4.35,French                                                               │
│                       ...                                                                                                                │
│                                                                                                                                          │
│                       ...                                                                                                                │
│                                                                                                                                          │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

Great! We've successfully downloaded the file, and we can see there's lots of information here.

At the moment, we're most interested in the file output. This contains the actual contents of the file that we have just downloaded.

Let's separate this out and store it in a separate variable for us to use.

downloaded_file = outputs['file']

New Formats: Creating and Converting

What next? We could transform the downloaded file contents into a different format.
Let's use the operation list earlier, and look for something that allows us to create something out of our new file.

kiara.list_operation_ids('create')
['create.database.from.file',
 'create.database.from.file_bundle',
 'create.database.from.table',
 'create.network_data.from.files',
 'create.network_data.from.tables',
 'create.stopwords_list',
 'create.table.from.file',
 'create.table.from.file_bundle']

Our file was orginally in a CSV format, so let's make a table using create.table.from.file.

Just like when we used download.file, we can double check what this does, and what inputs and outputs this involves.

This time, we're also going to use a variable to store the operation in - this is especially handy if the operation has a long name, or if you want to use the same operation more than once without retyping it.

op_id = 'create.table.from.file'

kiara.retrieve_operation_info(op_id)
                                                                                                                                            
 Documentation                                                                                                                              
                     Create a table from a file, trying to auto-determine the format of said file.                                          
                                                                                                                                            
 Author(s)                                                                                                                                  
                     Markus Binsteiner   markus@frkl.io                                                                                     
                                                                                                                                            
 Context                                                                                                                                    
                     Tags         tabular                                                                                                   
                     Labels       package: kiara_plugin.tabular                                                                             
                     References   source_repo: https://github.com/DHARPA-Project/kiara_plugin.tabular                                       
                                  documentation: https://DHARPA-Project.github.io/kiara_plugin.tabular/                                     
                                                                                                                                            
 Operation details                                                                                                                          
                     Documentation   Create a table from a file, trying to auto-determine the format of said file.                          
                                                                                                                                            
                     Inputs                                                                                                                 
                                       field name   type   description                                      Required   Default              
                                      ──────────────────────────────────────────────────────────────────────────────────────────────────    
                                       file         file   The source value (of type 'file').               yes        -- no default --     
                                                                                                                                            
                                                                                                                                            
                     Outputs                                                                                                                
                                       field name   type    description                                                                     
                                      ──────────────────────────────────────────────────────────────────────────────────────────────────    
                                       table        table   The result value (of type 'table').                                             
                                                                                                                                            
                                                                                                                                            

Great, we have all the information we need now.

Let's go again.

First we define our inputs, the downloaded file we saved earlier.

Then use kiara.run_job with our chosen operation, this time stored as op_id.

Once this is saved as our outputs, we can print it out.

inputs = {
    "file": downloaded_file
}

outputs = kiara.run_job(op_id, inputs=inputs)

outputs
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│                                                                                                                                          │
│   field   value                                                                                                                          │
│  ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────  │
│   table                                                                                                                                  │
│             Id    Label            JournalType      City        CountryNetworkT   PresentDayCoun   Latitude    Longitude    Language     │
│            ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────    │
│             75    Psychiatrische   specialized: p   Amsterdam   Netherlands       Netherlands      52.366667   4.9          Dutch        │
│             36    The American J   specialized: p   Baltimore   United States     United States    39.289444   -76.615278   English      │
│             208   The American J   specialized: p   Baltimore   United States     United States    39.289444   -76.615278   English      │
│             295   Die Krankenpfl   specialized: t   Berlin      German Empire     Germany          52.52       13.405       German       │
│             296   Die deutsche K   general medici   Berlin      German Empire     Germany          52.52       13.405       German       │
│             300   Therapeutische   specialized: t   Berlin      German Empire     Germany          52.52       13.405       German       │
│             1     Allgemeine Zei   specialized: p   Berlin      German Empire     Germany          52.52       13.405       German       │
│             7     Archiv für Psy   specialized: p   Berlin      German Empire     Germany          52.52       13.405       German       │
│             10    Berliner klini   general medici   Berlin      German Empire     Germany          52.52       13.405       German       │
│             13    Charité Annale   general medici   Berlin      German Empire     Germany          52.52       13.405       German       │
│             21    Monatsschrift    specialized: p   Berlin      German Empire     Germany          52.52       13.405       German       │
│             29    Virchows Archi   specialized: a   Berlin      German Empire     Germany          52.52       13.405       German       │
│             31    Zeitschrift fü   specialized: p   Berlin      German Empire     Germany          52.52       13.405       German       │
│             42    Vierteljahrssc   specialized: a   Berlin      German Empire     Germany          52.52       13.405       German       │
│             47    Centralblatt f   specialized: p   Berlin      German Empire     Germany          52.52       13.405       German       │
│             50    Russische medi   general medici   Berlin      German Empire     Germany          52.52       13.405       German       │
│             ...   ...              ...              ...         ...               ...              ...         ...          ...          │
│             ...   ...              ...              ...         ...               ...              ...         ...          ...          │
│             277   L'arte medica    general medici   Turin       Italy             Italy            45.079167   7.676111     Italian      │
│             288   Allgemeine öst   specialized: a   Vienna      Austro-Hungaria   Austria          48.2        16.366667    German       │
│             18    Jahrbücher für   specialized: p   Vienna      Austro-Hungaria   Austria          48.2        16.366667    German       │
│             30    Wiener klinisc   general medici   Vienna      Austro-Hungaria   Austria          48.2        16.366667    German       │
│             44    Wiener klinisc   general medici   Vienna      Austro-Hungaria   Austria          48.2        16.366667    German       │
│             45    Wiener medizin   general medici   Vienna      Austro-Hungaria   Austria          48.2        16.366667    German       │
│             72    Wiener medizin   general medici   Vienna      Austro-Hungaria   Austria          48.2        16.366667    German       │
│             81    Monatsschrift    general medici   Vienna      Austro-Hungaria   Austria          48.2        16.366667    German       │
│             93    Klinisch-thera   general medici   Vienna      Austro-Hungaria   Austria          48.2        16.366667    German       │
│             151   Medicinisch-ch   specialized: s   Vienna      Austro-Hungaria   Austria          48.2        16.366667    German       │
│             199   Der Militärazt   specialized: m   Vienna      Austro-Hungaria   Austria          48.2        16.366667    German       │
│             261   Медицинская бе   general medici   Voronezh    Russian Empire    Russia           51.671667   39.210556    Russian      │
│             77    Medycyna         general medici   Warsaw      Russian Empire    Poland           52.233333   21.016667    Polish       │
│             150   Kronika Lekars   general medici   Warsaw      Russian Empire    Poland           52.233333   21.016667    Polish       │
│             86    Grenzfragen de   specialized: p   Wiesbaden   German Empire     Germany          50.0825     8.24         German       │
│             206   Ergebnisse der   specialized: a   Wiesbaden   German Empire     Germany          50.0825     8.24         German       │
│                                                                                                                                          │
│                                                                                                                                          │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

This has done exactly what we wanted, and shown the contents from the downloaded file as a table. But we are also interested in some general (mostly internal) information and metadata, this time for the new table we have just created, rather than the original file itself.

Let's have a look.

outputs_table = outputs['table']

outputs_table
                                                                                                                                            
  value_id            cb61a4a4-2264-4ce4-b468-45bc5e6a6656                                                                                  
  kiara_id            76b2fda2-bbef-451c-aebf-3bf7a0e3e707                                                                                  
                                                                                                                                            
                      ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────  
  data_type_info                                                                                                                            
                        data_type_name     table                                                                                            
                        data_type_config   {}                                                                                               
                        characteristics    {                                                                                                
                                             "is_scalar": false,                                                                            
                                             "is_json_serializable": false                                                                  
                                           }                                                                                                
                        data_type_class                                                                                                     
                                             python_class_name    TableType                                                                 
                                             python_module_name   kiara_plugin.tabular.data_types.table                                     
                                             full_name            kiara_plugin.tabular.data_types.table.TableType                           
                                                                                                                                            
                                                                                                                                            
  destiny_backlinks   {}                                                                                                                    
  enviroments         None                                                                                                                  
  property_links      {                                                                                                                     
                        "metadata.python_class": "7953695f-b780-4f45-abc5-6f41bfb6295d",                                                    
                        "metadata.table": "5ed0b2c7-daad-4f2a-8c84-f87807edff31"                                                            
                      }                                                                                                                     
  value_hash          zdpuAn89Et1ENzfoASJRYcWEceyfRiPg664mN4nnHLFnjRLyg                                                                     
  value_schema                                                                                                                              
                        type          table                                                                                                 
                        type_config   {}                                                                                                    
                        default       __not_set__                                                                                           
                        optional      False                                                                                                 
                        is_constant   False                                                                                                 
                        doc           The result value (of type 'table').                                                                   
                                                                                                                                            
  value_size          42.79 KB                                                                                                              
  value_status        -- set --                                                                                                             

Querying our Data

So now we have downloaded our file and converted it into a table, we want to actually explore it.

To do this, we can query the table using SQL and some functions already included in kiara.

Let's take another look at that operation list, this time looking for functions that let us 'query'.

kiara.list_operation_ids('query')
['query.database', 'query.table']

Well, we already know our file has been converted into a table, so let's have a look at query.table.

kiara.retrieve_operation_info('query.table')
                                                                                                                                            
 Documentation                                                                                                                              
                     Execute a sql query against an (Arrow) table.                                                                          
                                                                                                                                            
                     The default relation name for the sql query is 'data', but can be modified by the 'relation_name' config               
                     option/input.                                                                                                          
                                                                                                                                            
                     If the 'query' module config option is not set, users can provide their own query, otherwise the pre-set one will be   
                     used.                                                                                                                  
                                                                                                                                            
 Author(s)                                                                                                                                  
                     Markus Binsteiner   markus@frkl.io                                                                                     
                                                                                                                                            
 Context                                                                                                                                    
                     Tags         tabular                                                                                                   
                     Labels       package: kiara_plugin.tabular                                                                             
                     References   source_repo: https://github.com/DHARPA-Project/kiara_plugin.tabular                                       
                                  documentation: https://DHARPA-Project.github.io/kiara_plugin.tabular/                                     
                                                                                                                                            
 Operation details                                                                                                                          
                     Documentation   Execute a sql query against an (Arrow) table.                                                          
                                                                                                                                            
                                     The default relation name for the sql query is 'data', but can be modified by the 'relation_name'      
                                     config option/input.                                                                                   
                                                                                                                                            
                                     If the 'query' module config option is not set, users can provide their own query, otherwise the       
                                     pre-set                                                                                                
                                     one will be used.                                                                                      
                                                                                                                                            
                     Inputs                                                                                                                 
                                       field name      type     description                                 Required   Default              
                                      ──────────────────────────────────────────────────────────────────────────────────────────────────    
                                       table           table    The table to query                          yes        -- no default --     
                                       query           string   The query, use the value of the             yes        -- no default --     
                                                                'relation_name' input as table, e.g.                                        
                                                                'select * from data'.                                                       
                                       relation_name   string   The name the table is referred to in the    no         data                 
                                                                sql query.                                                                  
                                                                                                                                            
                                                                                                                                            
                     Outputs                                                                                                                
                                       field name     type    description                                                                   
                                      ──────────────────────────────────────────────────────────────────────────────────────────────────    
                                       query_result   table   The query result.                                                             
                                                                                                                                            
                                                                                                                                            

So from this information, we only need to provide the table itself, and our query.

Let's work out how many of these journals were published in Berlin.

inputs = {
    "table": outputs_table,
    "query": "SELECT * from data where City like 'Berlin'"
}

outputs = kiara.run_job('query.table', inputs=inputs)

outputs
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│                                                                                                                                          │
│   field          value                                                                                                                   │
│  ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────  │
│   query_result                                                                                                                           │
│                    Id    Label            JournalType      City     CountryNetwor   PresentDayCoun   Latitude   Longitude   Language     │
│                   ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────    │
│                    295   Die Krankenpfl   specialized: t   Berlin   German Empire   Germany          52.52      13.405      German       │
│                    296   Die deutsche K   general medici   Berlin   German Empire   Germany          52.52      13.405      German       │
│                    300   Therapeutische   specialized: t   Berlin   German Empire   Germany          52.52      13.405      German       │
│                    1     Allgemeine Zei   specialized: p   Berlin   German Empire   Germany          52.52      13.405      German       │
│                    7     Archiv für Psy   specialized: p   Berlin   German Empire   Germany          52.52      13.405      German       │
│                    10    Berliner klini   general medici   Berlin   German Empire   Germany          52.52      13.405      German       │
│                    13    Charité Annale   general medici   Berlin   German Empire   Germany          52.52      13.405      German       │
│                    21    Monatsschrift    specialized: p   Berlin   German Empire   Germany          52.52      13.405      German       │
│                    29    Virchows Archi   specialized: a   Berlin   German Empire   Germany          52.52      13.405      German       │
│                    31    Zeitschrift fü   specialized: p   Berlin   German Empire   Germany          52.52      13.405      German       │
│                    42    Vierteljahrssc   specialized: a   Berlin   German Empire   Germany          52.52      13.405      German       │
│                    47    Centralblatt f   specialized: p   Berlin   German Empire   Germany          52.52      13.405      German       │
│                    50    Russische medi   general medici   Berlin   German Empire   Germany          52.52      13.405      German       │
│                    76    Deutsche Aerzt   general medici   Berlin   German Empire   Germany          52.52      13.405      German       │
│                    87    Monatsschrift    specialized: g   Berlin   German Empire   Germany          52.52      13.405      German       │
│                    108   Archiv für kli   specialized: s   Berlin   German Empire   Germany          52.52      13.405      German       │
│                    113   Zeitschrift fü   general medici   Berlin   German Empire   Germany          52.52      13.405      German       │
│                    159   Deutsche milit   specialized: m   Berlin   German Empire   Germany          52.52      13.405      German       │
│                    162   Jahresbericht    specialized: p   Berlin   German Empire   Germany          52.52      13.405      German       │
│                    192   Ärztliche Sach   general medici   Berlin   German Empire   Germany          52.52      13.405      German       │
│                    198   Zeitschrift fü   specialized: p   Berlin   German Empire   Germany          52.52      13.405      German       │
│                    258   Der Pfarrbote    news media       Berlin   German Empire   Germany          52.52      13.405      German       │
│                                                                                                                                          │
│                                                                                                                                          │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

The function has returned the table with just the results we were looking for from the SQL query.

Let's narrow this further, and find all the journals that are just about general medicine and published in Berlin.

We can re-use the query.table function and the table we've just made, stored in outputs['query_result']

inputs = {
    "table" : outputs['query_result'],
    "query" : "SELECT * from data where JournalType like 'general medicine'"
}

outputs = kiara.run_job('query.table', inputs=inputs)

outputs
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│                                                                                                                                          │
│   field          value                                                                                                                   │
│  ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────  │
│   query_result                                                                                                                           │
│                    Id    Label            JournalType      City     CountryNetwork   PresentDayCou   Latitude   Longitude   Language     │
│                   ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────    │
│                    296   Die deutsche K   general medici   Berlin   German Empire    Germany         52.52      13.405      German       │
│                    10    Berliner klini   general medici   Berlin   German Empire    Germany         52.52      13.405      German       │
│                    13    Charité Annale   general medici   Berlin   German Empire    Germany         52.52      13.405      German       │
│                    50    Russische medi   general medici   Berlin   German Empire    Germany         52.52      13.405      German       │
│                    76    Deutsche Aerzt   general medici   Berlin   German Empire    Germany         52.52      13.405      German       │
│                    113   Zeitschrift fü   general medici   Berlin   German Empire    Germany         52.52      13.405      German       │
│                    192   Ärztliche Sach   general medici   Berlin   German Empire    Germany         52.52      13.405      German       │
│                                                                                                                                          │
│                                                                                                                                          │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

Recording and Tracing our Data

We've quite a few changes to this table, so let's double check the information about this new table we've created with our queries.

query_output = outputs['query_result']

query_output
                                                                                                                                            
  value_id            424c687e-3b50-4d3b-aa6f-c03c5be2c7e3                                                                                  
  kiara_id            76b2fda2-bbef-451c-aebf-3bf7a0e3e707                                                                                  
                                                                                                                                            
                      ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────  
  data_type_info                                                                                                                            
                        data_type_name     table                                                                                            
                        data_type_config   {}                                                                                               
                        characteristics    {                                                                                                
                                             "is_scalar": false,                                                                            
                                             "is_json_serializable": false                                                                  
                                           }                                                                                                
                        data_type_class                                                                                                     
                                             python_class_name    TableType                                                                 
                                             python_module_name   kiara_plugin.tabular.data_types.table                                     
                                             full_name            kiara_plugin.tabular.data_types.table.TableType                           
                                                                                                                                            
                                                                                                                                            
  destiny_backlinks   {}                                                                                                                    
  enviroments         None                                                                                                                  
  property_links      {                                                                                                                     
                        "metadata.python_class": "98b5d853-40fc-407e-a6e2-8a0a42bad636",                                                    
                        "metadata.table": "21b4fc4f-6770-49a0-8d12-e6d34f7fa870"                                                            
                      }                                                                                                                     
  value_hash          zdpuB2LfZYHdiuR1sxy2ZkjPZ7JDnhysN48Y4RN9WNT4AvNN6                                                                     
  value_schema                                                                                                                              
                        type          table                                                                                                 
                        type_config   {}                                                                                                    
                        default       __not_set__                                                                                           
                        optional      False                                                                                                 
                        is_constant   False                                                                                                 
                        doc           The query result.                                                                                     
                                                                                                                                            
  value_size          5.22 KB                                                                                                               
  value_status        -- set --                                                                                                             

Looks good!

We might have changed things around, but we can still get lots of information about all our data.

More importantly, kiara is able to trace all of these changes, tracking the inputs and outputs and giving them all different identifiers, so you know exactly what has happened to your data.
Check it out!

query_output.lineage
query.table
├── input: query (string) = 393e91bc-ff4f-426d-a97a-978599991d5f
├── input: relation_name (string) = 430eb52a-6622-4c76-984a-293ee0920374
└── input: table (table) = f011c304-56e9-4cfc-8088-eae586d67f3f
    └── query.table
        ├── input: query (string) = b5d8072a-861d-48c5-b3e3-aa16d2d92c8b
        ├── input: relation_name (string) = 430eb52a-6622-4c76-984a-293ee0920374
        └── input: table (table) = cb61a4a4-2264-4ce4-b468-45bc5e6a6656
            └── create.table
                └── input: file (file) = c1f3fec4-6267-4c39-a524-3f711589a8af
                    └── download.file
                        ├── input: file_name (string) = 4f32424a-416d-4eea-93a0-9412e2529b2a
                        └── input: url (string) = 1df633f9-79c1-42dd-8467-0aa4bbe2735e

Even though we are only actually asking for the data lineage using the last SQL query and the table it made, kiara shows us everything that has happened since we first downloaded the file. This helps us keep an eye on the research process and the changes we are making to the data at the same time!

* Note: this can be updated/made more detailed esp. once we have Mariella's visualisations

What next...?

That's great, you've completed the first notebook and successfully installed kiara, downloaded files, tested out some functions, and are able to see what this does to your data.

Now you can check out the other plugin packages to explore how this helps you manage and trace your data while using digital analysis tools!