<jupyter_start><jupyter_text>Citations by year of citation<jupyter_code>from dimcli.shortcuts import dslquery_json as dslquery
import pandas as pd
import matplotlib.pyplot as plt
from collections import Counter
from IPython.display import Image<jupyter_output><empty_output><jupyter_text>1)  Seed your citation graphDefine a function that will give you back the publications that you want to start your citation graph from.This example uses a single publication, but you could choose to look at a set of publications using any query.<jupyter_code>def searchPubs(limit=1000, skip=0):
    data = """search publications 
          where id="pub.1001131784"
          return publications[id+times_cited+year]
          limit {} skip {}
    """.format(limit,skip)
    return data<jupyter_output><empty_output><jupyter_text>2) Get the citing papersDefine a function that returns the citing papers, and the details that you will need to process them.<jupyter_code>def citedpublications(ids,limit=1000,skip=0):
    data = """search publications
                  where (references in [{}])
              return publications[id+year+references] sort by id
                  limit {} skip {}
    """.format(','.join(['"{}"'.format(id) for id in ids]),limit,skip)
    print(skip)
    return data<jupyter_output><empty_output><jupyter_text>3) Process the citing papers and get a 1 degree citation graphFor the set of papers that you are interested in, process the citing papers to extract the relevant 1d citation graph, partitoned by year<jupyter_code>def dslsearchcitedpublications(cited_ids):
    
    idchunks = [cited_ids[x:x + 500] for x in range(0, len(cited_ids), 500)]
    result = {}
    years = {}
    
    total_pubs = []
    for idc in idchunks:        
        skip = 0
        pubs = []        
        while skip == 0 or len(pubs)== 1000:
            pubs = dslquery(citedpublications(idc, skip=skip)).get('publications',[])
            total_pubs += pubs
            skip += 1000   
    
    for i, p in enumerate(total_pubs):

        rs= []
        
        for r in p['references']:
            try:
                rs.append(r)
            except:
                print("found this anomalous data {}".format(r))
                
        references_set = set(rs)
        
        #Only return those citations that refer to the cited_ids  
        #(you could actually speed this set up by using dataframes)
        focusedcites = list(references_set - set(references_set - set(cited_ids)))

        #Citation graph
        citelinks = ["{}-{}".format(p['id'],f) for f in focusedcites]

        #Cumulatively partition Citation graph by year of citing publication
        year_key = str(p.get('year','no_year'))
        if year_key in years.keys():
            years[year_key] = list(set(years[year_key] + citelinks))
        else:
            years[year_key] = citelinks
 
    return years<jupyter_output><empty_output><jupyter_text>Put the steps together* Get the publications you are interested in * Then get and process the publications that cite them<jupyter_code>def dslsearchpublications():
    skip = 0
    pubs = []
    total_pubs = []
    result = {}
    while (skip == 0) or (len(pubs) == 1000):
        pubs = dslquery(searchPubs(skip=skip)).get('publications',[])
        total_pubs += pubs
        skip += 1000   
        
    result['ciations_by_citing_year'] = dslsearchcitedpublications([p['id'] for p in total_pubs])
                         
    return result
result = dslsearchpublications()
cbyc = []
for k in result['ciations_by_citing_year'].keys():
    cbyc.append([ k, len(result['ciations_by_citing_year'][k])])
df = pd.DataFrame(cbyc)
df.columns = ['year','citation_count']
#Primary Citations by year of citation
df.set_index('year').sort_index().plot.bar()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10311436796650092, 'fraction_numerical': 0.013609002878827532, 'mean_word_length': 3.0020942408376965, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '637560', 'license': 'MIT', 'n_tokens_mistral': 1209, 'n_tokens_neox': 1134, 'n_words': 331, 'path': 'archive/2019-04-Technical-University-of-Denmark/5-Citations-by-year-of-citation.ipynb', 'repo_name': 'lcastle-cloud/dimensions-api-lab', 'size': '3821'}