示例#1
0
def test_paper_parse_two():
    my_paper = Paper(filename='tests/test_files/paper-a.json')
    my_paper2 = Paper(filename='tests/test_files/paper-k.json')
    my_network = PaperNetwork()
    my_network.collection.append(my_paper)
    my_network.collection.append(my_paper2)

    #my_network.parse_papers()

    assert my_network.graph.size() == 6
    assert len(my_network.graph.nodes()) == 6  #-> ['title - x', 'title - a', 'title - y', 'title - b', 'title - c', 'title - k']
    assert my_network.graph.nodes['a']['year'] == "2000"
    assert my_network.graph.nodes['b']['year'] == "1990"
    assert my_network.graph.nodes['x']['year'] == "2003"


    my_network.collection.remove(my_paper2)

    assert my_network.graph.size() == 4
    assert len(my_network.graph.nodes()) == 5


    # Testing the deepcopy function. Everytime we access the graph, we need to check whether the collection changed
    assert my_network.graph.nodes[my_network.collection[0]['paperId']]['year'] == my_network.collection[0]['year']
    my_network.collection[0]['year'] = "9999"
    assert my_network.graph.nodes[my_network.collection[0]['paperId']]['year'] == my_network.collection[0]['year'] # Test that the deepcopy of the Paperlist is functioning
    assert my_network.graph.size() == 4
示例#2
0
def test_pn_init():
    list_of_jsons = ['tests/test_files/paper-a.json', 'tests/test_files/paper-k.json']
    my_network = PaperNetwork(filename_list=list_of_jsons)
    assert len(my_network.collection) == 2

# Turns of while devving
    list_of_dois = ['10.1093/nar/gkw1328']
    my_network = PaperNetwork(filename_list=list_of_jsons, doi_list=list_of_dois)
    assert len(my_network.collection) == 3
示例#3
0
def test_append_not_unique():
    my_paper = Paper(filename='tests/test_files/paper-a.json')
    my_paper_duplicate = Paper(filename='tests/test_files/paper-a.json')
    my_network = PaperNetwork()

    my_network.collection.append(my_paper)
    with pytest.raises(ValueError, match=r"This paperId is already present, only unique paperIds allowed"):
        my_network.collection.append(my_paper_duplicate)
示例#4
0
def test_append():
    my_paper = Paper(filename='tests/test_files/paper-a.json')
    my_paper2 = Paper(filename='tests/test_files/paper-k.json')
    my_network = PaperNetwork()
    my_network.collection.append(my_paper)

    assert my_paper in my_network.collection 
    assert my_paper2 not in my_network.collection 
示例#5
0
def test_copy():
    #my_paper = Paper(filename='tests/test_files/paper-a.json')
    #my_paper2 = Paper(filename='tests/test_files/paper-k.json')
    my_network = PaperNetwork()

    with pytest.raises(TypeError, match=r"JSON argument needs to be a Paper or dict"):
        my_network._copy_paper_attributes_to_graph('foo node', 'a string not a dict or paper')

    with pytest.raises(ValueError, match=r"The node is not part of the graph, add the node first"):
        my_network._copy_paper_attributes_to_graph('foo node', {})

    with pytest.raises(TypeError, match=r"Node identifier needs to be a string, such as the paperId"):
        my_network._copy_paper_attributes_to_graph(2, {})
示例#6
0
def test_paper_json():
    my_paper = Paper(filename='tests/test_files/paper-a.json')
    my_paper2 = Paper(filename='tests/test_files/paper-k.json')
    my_network = PaperNetwork()
    my_network.collection.append(my_paper)
    my_network.collection.append(my_paper2)


    assert my_network.calculate_json() == {'directed': True, 'multigraph': False, 'graph': {}, 'nodes': [{'title': 'title - a', 'year': '2000', 'url': [], 'authors': [], 'venue': [], 'warning': ['Serious warning: reference list seems very short (<10)', 'Note: seems like not widely cited (<5)'], 'in_degree': 2, 'corpus': 1, 'out_degree': 0, 'id': 'a'}, {'title': 'title - x', 'year': '2003', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 1, 'corpus': 5, 'out_degree': 1, 'id': 'x'}, {'title': 'title - y', 'year': '2011', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 0, 'corpus': 5, 'out_degree': 1, 'id': 'y'}, {'title': 'title - b', 'year': '1990', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 1, 'corpus': 0, 'out_degree': 0, 'id': 'b'}, {'title': 'title - c', 'year': '1991', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 2, 'corpus': 0, 'out_degree': 0, 'id': 'c'}, {'title': 'title - k', 'year': '2005', 'url': [], 'authors': [], 'venue': [], 'warning': ['Serious warning: reference list seems very short (<10)', 'Note: seems like not widely cited (<5)'], 'in_degree': 0, 'corpus': 1, 'out_degree': 0, 'id': 'k'}], 'links': [{'source': 'a', 'target': 'b'}, {'source': 'a', 'target': 'c'}, {'source': 'x', 'target': 'a'}, {'source': 'y', 'target': 'a'}, {'source': 'k', 'target': 'x'}, {'source': 'k', 'target': 'c'}]}

    # these three cases should not  change the output
    assert my_network.calculate_json(mimimum_citation_count_of_references=1) == {'directed': True, 'multigraph': False, 'graph': {}, 'nodes': [{'title': 'title - a', 'year': '2000', 'url': [], 'authors': [], 'venue': [], 'warning': ['Serious warning: reference list seems very short (<10)', 'Note: seems like not widely cited (<5)'], 'in_degree': 2, 'corpus': 1, 'out_degree': 0, 'id': 'a'}, {'title': 'title - x', 'year': '2003', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 1, 'corpus': 5, 'out_degree': 1, 'id': 'x'}, {'title': 'title - y', 'year': '2011', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 0, 'corpus': 5, 'out_degree': 1, 'id': 'y'}, {'title': 'title - b', 'year': '1990', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 1, 'corpus': 0, 'out_degree': 0, 'id': 'b'}, {'title': 'title - c', 'year': '1991', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 2, 'corpus': 0, 'out_degree': 0, 'id': 'c'}, {'title': 'title - k', 'year': '2005', 'url': [], 'authors': [], 'venue': [], 'warning': ['Serious warning: reference list seems very short (<10)', 'Note: seems like not widely cited (<5)'], 'in_degree': 0, 'corpus': 1, 'out_degree': 0, 'id': 'k'}], 'links': [{'source': 'a', 'target': 'b'}, {'source': 'a', 'target': 'c'}, {'source': 'x', 'target': 'a'}, {'source': 'y', 'target': 'a'}, {'source': 'k', 'target': 'x'}, {'source': 'k', 'target': 'c'}]}
    assert my_network.calculate_json(mimimum_citation_count_of_references=1, minimum_times_citing_collection=1) == {'directed': True, 'multigraph': False, 'graph': {}, 'nodes': [{'title': 'title - a', 'year': '2000', 'url': [], 'authors': [], 'venue': [], 'warning': ['Serious warning: reference list seems very short (<10)', 'Note: seems like not widely cited (<5)'], 'in_degree': 2, 'corpus': 1, 'out_degree': 0, 'id': 'a'}, {'title': 'title - x', 'year': '2003', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 1, 'corpus': 5, 'out_degree': 1, 'id': 'x'}, {'title': 'title - y', 'year': '2011', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 0, 'corpus': 5, 'out_degree': 1, 'id': 'y'}, {'title': 'title - b', 'year': '1990', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 1, 'corpus': 0, 'out_degree': 0, 'id': 'b'}, {'title': 'title - c', 'year': '1991', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 2, 'corpus': 0, 'out_degree': 0, 'id': 'c'}, {'title': 'title - k', 'year': '2005', 'url': [], 'authors': [], 'venue': [], 'warning': ['Serious warning: reference list seems very short (<10)', 'Note: seems like not widely cited (<5)'], 'in_degree': 0, 'corpus': 1, 'out_degree': 0, 'id': 'k'}], 'links': [{'source': 'a', 'target': 'b'}, {'source': 'a', 'target': 'c'}, {'source': 'x', 'target': 'a'}, {'source': 'y', 'target': 'a'}, {'source': 'k', 'target': 'x'}, {'source': 'k', 'target': 'c'}]}
    assert my_network.calculate_json(minimum_times_citing_collection=1) == {'directed': True, 'multigraph': False, 'graph': {}, 'nodes': [{'title': 'title - a', 'year': '2000', 'url': [], 'authors': [], 'venue': [], 'warning': ['Serious warning: reference list seems very short (<10)', 'Note: seems like not widely cited (<5)'], 'in_degree': 2, 'corpus': 1, 'out_degree': 0, 'id': 'a'}, {'title': 'title - x', 'year': '2003', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 1, 'corpus': 5, 'out_degree': 1, 'id': 'x'}, {'title': 'title - y', 'year': '2011', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 0, 'corpus': 5, 'out_degree': 1, 'id': 'y'}, {'title': 'title - b', 'year': '1990', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 1, 'corpus': 0, 'out_degree': 0, 'id': 'b'}, {'title': 'title - c', 'year': '1991', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 2, 'corpus': 0, 'out_degree': 0, 'id': 'c'}, {'title': 'title - k', 'year': '2005', 'url': [], 'authors': [], 'venue': [], 'warning': ['Serious warning: reference list seems very short (<10)', 'Note: seems like not widely cited (<5)'], 'in_degree': 0, 'corpus': 1, 'out_degree': 0, 'id': 'k'}], 'links': [{'source': 'a', 'target': 'b'}, {'source': 'a', 'target': 'c'}, {'source': 'x', 'target': 'a'}, {'source': 'y', 'target': 'a'}, {'source': 'k', 'target': 'x'}, {'source': 'k', 'target': 'c'}]}

    # with restrictions
    assert my_network.calculate_json(mimimum_citation_count_of_references=2) == {'directed': True, 'multigraph': False, 'graph': {}, 'nodes': [{'title': 'title - a', 'year': '2000', 'url': [], 'authors': [], 'venue': [], 'warning': ['Serious warning: reference list seems very short (<10)', 'Note: seems like not widely cited (<5)'], 'in_degree': 2, 'corpus': 1, 'out_degree': 0, 'id': 'a'}, {'title': 'title - x', 'year': '2003', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 1, 'corpus': 5, 'out_degree': 1, 'id': 'x'}, {'title': 'title - y', 'year': '2011', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 0, 'corpus': 5, 'out_degree': 1, 'id': 'y'}, {'title': 'title - c', 'year': '1991', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 2, 'corpus': 0, 'out_degree': 0, 'id': 'c'}, {'title': 'title - k', 'year': '2005', 'url': [], 'authors': [], 'venue': [], 'warning': ['Serious warning: reference list seems very short (<10)', 'Note: seems like not widely cited (<5)'], 'in_degree': 0, 'corpus': 1, 'out_degree': 0, 'id': 'k'}], 'links': [{'source': 'a', 'target': 'c'}, {'source': 'x', 'target': 'a'}, {'source': 'y', 'target': 'a'}, {'source': 'k', 'target': 'x'}, {'source': 'k', 'target': 'c'}]}
    assert my_network.calculate_json(minimum_times_citing_collection=2) == {'directed': True, 'multigraph': False, 'graph': {}, 'nodes': [{'title': 'title - a', 'year': '2000', 'url': [], 'authors': [], 'venue': [], 'warning': ['Serious warning: reference list seems very short (<10)', 'Note: seems like not widely cited (<5)'], 'in_degree': 2, 'corpus': 1, 'out_degree': 0, 'id': 'a'}, {'title': 'title - x', 'year': '2003', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 1, 'corpus': 5, 'out_degree': 1, 'id': 'x'}, {'title': 'title - c', 'year': '1991', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 2, 'corpus': 0, 'out_degree': 0, 'id': 'c'}, {'title': 'title - k', 'year': '2005', 'url': [], 'authors': [], 'venue': [], 'warning': ['Serious warning: reference list seems very short (<10)', 'Note: seems like not widely cited (<5)'], 'in_degree': 0, 'corpus': 1, 'out_degree': 0, 'id': 'k'}], 'links': [{'source': 'a', 'target': 'c'}, {'source': 'x', 'target': 'a'}, {'source': 'k', 'target': 'x'}, {'source': 'k', 'target': 'c'}]}

    # double restrictions
    assert my_network.calculate_json(mimimum_citation_count_of_references=2, minimum_times_citing_collection=2 ) == {'directed': True, 'multigraph': False, 'graph': {}, 'nodes': [{'title': 'title - a', 'year': '2000', 'url': [], 'authors': [], 'venue': [], 'warning': ['Serious warning: reference list seems very short (<10)', 'Note: seems like not widely cited (<5)'], 'in_degree': 1, 'corpus': 1, 'out_degree': 0, 'id': 'a'}, {'title': 'title - c', 'year': '1991', 'url': [], 'authors': [], 'venue': [], 'warning': [], 'in_degree': 2, 'corpus': 0, 'out_degree': 0, 'id': 'c'}, {'title': 'title - k', 'year': '2005', 'url': [], 'authors': [], 'venue': [], 'warning': ['Serious warning: reference list seems very short (<10)', 'Note: seems like not widely cited (<5)'], 'in_degree': 0, 'corpus': 1, 'out_degree': 0, 'id': 'k'}], 'links': [{'source': 'a', 'target': 'c'}, {'source': 'k', 'target': 'c'}]}
示例#7
0
def test_paper_parse():
    my_paper = Paper(filename='tests/test_files/paper-a.json')
    my_paper2 = Paper(filename='tests/test_files/paper-k.json')
    my_network = PaperNetwork()
    my_network.collection.append(my_paper)
    my_network.collection.append(my_paper2)

    assert my_network.graph.size() == 6
    assert len(my_network.graph.nodes()) == 6 #-> ['title - x', 'title - a', 'title - y', 'title - b', 'title - c', 'title - k']
    assert my_network.graph.nodes['a']['year'] == "2000"
    assert my_network.graph.nodes['b']['year'] == "1990"
    assert my_network.graph.nodes['x']['year'] == "2003"

    assert my_network.graph.nodes['c']['year'] == "1991"
    assert my_network.graph.nodes['k']['year'] == "2005"
    assert my_network.graph.nodes['y']['year'] == "2011"
示例#8
0
from papernetwork.core import PaperNetwork, Paper, PaperList

# Define a list of DOI's you would like to download
list_of_dois = [
    '10.1093/nar/gkw1328', '10.1128/CMR.00016-17', '10.1038/s41564-019-0626-z'
]

# Pull the papers from semanticscholar.org via the API and parse them
my_network = PaperNetwork(doi_list=list_of_dois)

# Show the titles in the PaperNetwork object
print(
    my_network
)  # Rapid resistome mapping using nanopore sequencing, Whole-Genome Sequencing of Bacterial Pathogens: the Future of Nosocomial Outbreak Analysis., Rapid MinION profiling of preterm microbiota and antimicrobial-resistant pathogens

# The papers are internally stored in a OrderedSet-like object, accessible via the collection
my_collection = my_network.collection

# The papers themselves are individually stored in a Dict-like object
my_paper = my_collection[0]  # Take the first paper in the list

print(my_paper['title'])

# They keys of the object are defined by the Semantic Scholar API, see https://api.semanticscholar.org/
# To get an overview of the available keys
for key, value in my_collection[0].items():
    print(key)  # or print(key,value) to see the value

# Currently the following keys are supplied by semanticscholar.org
# arxivId
# authors
示例#9
0
def test_append_wrong_type():
    with pytest.raises(Exception) as execinfo:
        my_network = PaperNetwork()
        my_network.collection.append('this is not a Paper object but a string')
    assert str(execinfo.value) == 'the item you are trying to add should be of the type Paper'