<jupyter_start><jupyter_text>Covid Restrictions. **Setting up the environment**Initialization of the Ontology editor in Jupyter Notebook<jupyter_code>from cognipy.ontology import Ontology
from cognipy.ontology import CQL
from cognipy.ontology import ABoxBatch
from cognipy.ontology import encode_string_for_graph_label
from cognipy.edit import CnlEditBox
from cognipy.edit import CnlQueryForConcept

import random
import textwrap
import numpy as np
import pandas as pd
import pickle
import datetime

def graph_attribute_formatter(val):
    if isinstance(val,list) or isinstance(val,set):
        return " | ".join(list(map(lambda i:encode_string_for_graph_label(graph_attribute_formatter(i)),val)))
    elif isinstance(val,dict):
        return " | ".join(list(map(lambda i:i[0]+" : "+encode_string_for_graph_label(graph_attribute_formatter(i[1])),val.items())))
    else:
        return encode_string_for_graph_label(textwrap.fill(str(val),40))

from pint import UnitRegistry
ureg = UnitRegistry()
Q = ureg.Quantity

def Qstr(a):
    return "Q('"+str(a)+"')"
Q.__repr__=Qstr<jupyter_output><empty_output><jupyter_text>Lets setup the fist ontology editor for the general knowledge. The editor allows you to enter the knowledge in text format, and support you with the prdictive editor.<jupyter_code>policies=pd.read_csv('https://raw.githubusercontent.com/saudiwin/corona_tscs/master/data/CoronaNet/data_country/coronanet_release/coronanet_release_Australia.csv', encoding='cp1252',index_col=0)
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)
pd.set_option('display.max_colwidth', -1)
policies_cnl_lines=[]
for i,row in policies[policies['entry_type']=='new_entry'].iterrows():
    policies_cnl_lines.append(f"Australia introduced Policy-{row.policy_id}.")
    policies_cnl_lines.append(f"Policy-{row.policy_id} is a restriction and was-introduced equal-to {row.date_start}.")
    if not row.date_end!=row.date_end:
        policies_cnl_lines.append(f"Policy-{row.policy_id} was-withdrawn  equal-to {row.date_end}.")    
policies_cnl_lines
parts_editors={}
parts_editors['01']=CnlEditBox('countries.encnl')
parts_editors['02']=CnlEditBox('border_cross_quarantine.encnl')
parts_editors['03']=CnlEditBox('face_cover.encnl')
parts_editors['04']=CnlEditBox('lockdown.encnl')
parts_editors['05']=CnlEditBox('mass_coronavirus_testing.encnl')
parts_editors['01']
parts_editors['02']
parts_editors['03']
parts_editors['04']
parts_editors['05']<jupyter_output><empty_output><jupyter_text>Lets define the function that will build the ontology based on the dict of ontology editors and the optional additional editor for testing purposes. All it is doing it is concatenaiting the values of all the editors and puts them into the newly created ontology object.<jupyter_code>def buildOntology(parts_editors,test_editor=None):
    return Ontology("cnl/string",
                    "\n\n".join(k.getvalue() for k in parts_editors.values())+('' if test_editor is None else test_editor.getvalue()) + '\n'.join(policies_cnl_lines), 
                    evaluator = lambda e:eval(e,globals(),locals()), 
                    graph_attribute_formatter = graph_attribute_formatter)

onto=buildOntology(parts_editors)<jupyter_output><empty_output><jupyter_text>Lets draw our ontology.<jupyter_code>onto.draw_graph(layout='force directed')<jupyter_output><empty_output><jupyter_text>You can select and print out all instaces of given concept's name.<jupyter_code>regions=onto.select_instances_of("region")
regions<jupyter_output><empty_output><jupyter_text>Cognipy allows you to do queries on ontology using sparql language. In example we query for every region which has indtroduced some restriction and then we save it to pandas dataframe.<jupyter_code>df=onto.sparql_query(CQL("""select ?region ?restriction ?date{
                            ?region rdf:type <region>. 
                            ?restriction rdf:type <restriction>. 
                            ?region <introduceds> ?restriction. 
                            ?restriction <was-introduced> ?date.
                        }"""))
df['date'] = pd.to_datetime(pd.to_datetime(df['date']).apply(lambda x: x.strftime('%d/%m/%Y')))
df.set_index('date',inplace=True)
df<jupyter_output><empty_output><jupyter_text>Lets import covid cases data.<jupyter_code>covid_data = pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv')
per_country_covid_data=covid_data.drop(["Lat","Long","Province/State"],axis=1).groupby("Country/Region").sum()
per_country_covid_data=per_country_covid_data[per_country_covid_data.index.isin(regions["Instance"])].T
per_country_covid_data.index = pd.to_datetime(per_country_covid_data.index)
new_cases = per_country_covid_data.diff(periods=1).dropna()
new_cases
from matplotlib import pyplot as plt
%matplotlib inline 
plt.figure(figsize=(12,8))

rolling14 = new_cases.rolling(window=14).mean()
for column in rolling14.columns:
    plt.plot(rolling14.index, rolling14[column], label=column)

plt.legend(loc=4)
#plt.yscale("log")

plt.show()<jupyter_output><empty_output><jupyter_text>Figure shows covid new cases in time on logarythmic scale.<jupyter_code>from datetime import timedelta
for i, column in enumerate(rolling14.columns):
    plt.figure(figsize=(12,4))
    plt.plot(rolling14.index, rolling14[column], label=column)
    plt.legend()
    for datex,row in df[df["region"]==column].iterrows():
        datex=datex+timedelta(1)
        val = 0.0
        if datex in rolling14[column].index:
            val =rolling14[column].loc[datex]
        val = 0.0 if val!=val else val
        plt.scatter(datex, val , marker="|", s=500, c="red")
#        plt.annotate(row.restriction, (datex, val), xytext=(-20,20), 
#            textcoords='offset points', ha='center', va='bottom', arrowprops=dict(arrowstyle='->',
#            connectionstyle='arc3,rad=0.5', color='red'))
    plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12285620052770449, 'fraction_numerical': 0.013522427440633245, 'mean_word_length': 5.5005359056806, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 38, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '532905', 'license': 'Apache-2.0', 'n_tokens_mistral': 2103, 'n_tokens_neox': 2021, 'n_words': 378, 'path': 'docsrc/jupyter_notebooks/Covid/Virus.ipynb', 'repo_name': 'jswiatkowski/cognipy', 'size': '6064'}