diff --git a/MANIFEST.in b/MANIFEST.in index 47ff55a..1cb9497 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,2 +1,2 @@ include README.rst -include pyscholar/keys.cfg \ No newline at end of file +include ~/.pyscholar \ No newline at end of file diff --git a/README.rst b/README.rst index 0db6083..24908a4 100644 --- a/README.rst +++ b/README.rst @@ -10,7 +10,7 @@ A library to create collaboration science networks. Requirements ------------- -- `Python `_ >= 2.7 +- `Python `_ ≥ 2.7 - `Networkx `_ - `Matplotlib `_ diff --git a/docs/source/conf.py b/docs/source/conf.py index 7970ced..dc3a914 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -18,7 +18,7 @@ # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. -sys.path.insert(0, os.path.abspath('../../src')) +sys.path.insert(0, os.path.abspath('../../pyscholar')) # -- General configuration ------------------------------------------------ @@ -267,4 +267,6 @@ # Example configuration for intersphinx: refer to the Python standard library. -intersphinx_mapping = {'http://docs.python.org/': None} +intersphinx_mapping = {'http://docs.python.org/': None, + 'pandas': ('http://pandas.pydata.org/pandas-docs/stable/', None), + 'networkx': ('https://networkx.readthedocs.io/en/stable/', None)} diff --git a/docs/source/index.rst b/docs/source/index.rst index 9c833c6..c372bfb 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -7,16 +7,16 @@ Welcome to Pyscholar's documentation! ===================================== -Pyscholar gives you easy, Pythonic access to Scopus's API. +Pyscholar gives you easy, pythonic access to Scopus' API. -Some of the functionalities implemented in Pyscholar are as follows: +Some of the functionalities implemented in Pyscholar are: -* Retrieve information about the affiliated authors to Scopus. -* Retrieve information about the articles registered in Scopus. -* Retrieve information about the Institutions registered in Scopus. +* Retrieve information about authors affiliated to Scopus. +* Retrieve information about articles registered in Scopus. +* Retrieve information about institutions registered in Scopus. -From the information obtained in the above points, Pyscholar can with this information build citation graphs and coauthor graphs, since it has features that allow you to perform this task. +Pyscholar can also build citation and coauthor graphs with this information. Contents: @@ -25,7 +25,7 @@ Contents: Installation - Pyscholar Module + Scopus Module Tutorial @@ -35,5 +35,4 @@ Indices and tables * :ref:`genindex` * :ref:`modindex` -* :ref:`search` - +* :ref:`search` \ No newline at end of file diff --git a/docs/source/installation.rst b/docs/source/installation.rst index a97c256..2fd5ebe 100644 --- a/docs/source/installation.rst +++ b/docs/source/installation.rst @@ -1,7 +1,8 @@ .. _installation: -================ - Installation -================ + +============ +Installation +============ Requirements ================ diff --git a/docs/source/pyscholar.rst b/docs/source/pyscholar.rst deleted file mode 100644 index af1a0cc..0000000 --- a/docs/source/pyscholar.rst +++ /dev/null @@ -1,54 +0,0 @@ -.. _pyscholar: - -================ -pyscholar module -================ - -.. currentmodule:: pyscholar - -Scopus Author/Co-Authors -========================== -.. autofunction:: find_author_scopus_id_by_name -.. autofunction:: get_authors_by_id_affiliation - -.. autofunction:: get_coauthors (id_author,min_year="",max_year="") - -.. autofunction:: get_ids_authors_by_id_paper -.. autofunction:: load_authors_from_file -.. autofunction:: search_author - - -Scopus Paper -============== -.. autofunction:: count_citations_by_id_paper -.. autofunction:: get_common_papers -.. autofunction:: get_papers -.. autofunction:: get_references_by_paper -.. autofunction:: get_title_abstract_by_idpaper -.. autofunction:: get_title_abstract_by_idpaper -.. autofunction:: load_papers_from_file - - - -Scopus Affiliation -==================== -.. autofunction:: find_affiliation_scopus_id_by_name -.. autofunction:: search_affiliation_by_id - - -Graph Generators -=================== -.. autofunction:: get_citation_graph -.. autofunction:: get_coauthors_graph - - -Reading and Writing Graphs -============================= -.. autofunction:: save_graph_pickle -.. autofunction:: load_graph_pickle - - -Other Functions -================= -.. autofunction:: disable_graphical_interface -.. autofunction:: enable_graphical_interface diff --git a/docs/source/scopus.rst b/docs/source/scopus.rst new file mode 100644 index 0000000..c1c2ead --- /dev/null +++ b/docs/source/scopus.rst @@ -0,0 +1,122 @@ +.. _scopus: + +================ +scopus module +================ + +.. currentmodule:: scopus + +Scopus Author/Co-Authors +========================== +.. autofunction:: find_author_scopus_id_by_name +.. autofunction:: get_authors_by_id_affiliation + +.. autofunction:: get_coauthors (id_author,min_year="",max_year="") + +.. autofunction:: get_ids_authors_by_id_paper +.. autofunction:: load_authors_from_file + + :param path: The directory where the file is located. + :type path: :class:`str` + :returns: A list with the authors ids. + :rtype: :class:`list` + + :Example: + + >>> import pyscholar + >>> pyscholar.load_authors_from_file("/home/dir/authors.txt") + >>> ['56013555800', '12645109800', '12645615700', '12646275800', '15740951000', '23388216300', '23398643600', '24171073600', '24512697200', '26032291700', '34870304900', '35566511400', '36117667600', '36117782600', '36118513000', '36141027800', '37107692000', '37111010900', '54792735300', '55439178900', '55468916100', '55669785100', '55918277800', '55943170800', '55993686500', '56002701400', '56013629200', '56013731100', '56013734400', '56240672400', '56263920000', '56279187000'] + >>> + +.. autofunction:: search_author + + +Scopus Paper +============== +.. autofunction:: count_citations_by_id_paper +.. autofunction:: get_common_papers +.. autofunction:: get_papers +.. autofunction:: get_references_by_paper +.. autofunction:: get_title_abstract_by_idpaper +.. autofunction:: get_title_abstract_by_idpaper +.. autofunction:: load_papers_from_file + + :param path: The directory where the file is located. + :type path: :class:`str` + :returns: A list with the papers ids. + :rtype: :class:`list` + + :Example: + + >>> import pyscholar + >>> pyscholar.load_papers_from_file("/home/dir/papers.txt") + >>> ['78149425675', '79959254005', '84883029526', '84887837079', '84892691931', '84893836569', '84897620666', '84904102369', '84908190310', '84919922003', '84924004559', '84925067887', '84928486421', '84928490197', '84928742486', '84939945186', '84939973084', '84945466401', '84961373040'] + + + +Scopus Affiliation +==================== +.. autofunction:: find_affiliation_scopus_id_by_name + + :param organization: The name of the affiliation to search. + :type organization: :class:`str` + :returns: A data frame with the following attributes: id,city,country,name_variant,eid,affiliation_name,identifier,document_count. + :rtype: :class:`pandas.DataFrame` + + :Example: + + >>> import pyscholar + >>> pyscholar.find_affiliation_scopus_id_by_name("CINVESTAV") + id city country name_variant eid affiliation_name identifier document_count + 0 60017323 Mexico City Mexico [CINVESTAV-IPN, CINVESTAV] 10-s2.0-60017323 Centro de Investigacion y de Estudios Avanzados AFFILIATION_ID:60017323 19254 + 1 60010531 Mérida Mexico [CINVESTAV-IPN] 10-s2.0-60010531 CINVESTAV Unidad Merida AFFILIATION_ID:60010531 1811 + 2 60018216 Guadalajara Mexico [CINVESTAV, CINVESTAV Unidad Guadalajara] 10-s2.0-60018216 CINVESTAV Unidad Guadalajara AFFILIATION_ID:60018216 1010 + . + . + . + . + +.. autofunction:: search_affiliation_by_id + + +Graph Generators +=================== +.. autofunction:: get_citation_graph +.. autofunction:: get_coauthors_graph + + +Reading and Writing Graphs +============================= +.. autofunction:: save_graph_pickle + + :param G: The graph object to be saved. + :param path: The directory where the graph will be saved. + :param name_graph: The name of the graph. + :type G: :ref:`networkx.graph ` + :type path: :class:`str` + :type name_graph: :class:`str` + :returns: :class:`NoneType` + + :Example: + + >>> import pyscholar + >>> psycholar.save_graph_pickle(G,"~/dir/","my_graph") + +.. autofunction:: load_graph_pickle + + :param path: The directory where the pickle object is located. + :type path: :class:`str` + :returns: A graph G. + :rtype: :ref:`networkx.graph ` + + :Example: + + >>> import pyscholar + >>> my_graph = psycholar.load_graph_pickle("~/dir/graph.pickle") + >>> my_graph + + +Other Functions +================= +.. autofunction:: disable_graphical_interface +.. autofunction:: enable_graphical_interface diff --git a/pyscholar/__init__.py b/pyscholar/__init__.py index be55be4..6dc91ec 100644 --- a/pyscholar/__init__.py +++ b/pyscholar/__init__.py @@ -1 +1,19 @@ -import pyscholar \ No newline at end of file +import os, ConfigParser +#Check if .pyscholar exists and create it if necessary +pyscholarDir = os.path.join(os.path.expanduser("~"), ".pyscholar") +if not os.path.exists(pyscholarDir): + os.makedirs(pyscholarDir) + +#Same for .pyscholar/keys.cfg +if not os.path.exists(os.path.join(pyscholarDir, "keys.cfg")): + keysParser = ConfigParser.ConfigParser() + keysParser.add_section("Keys") + keysParser.set('Keys', 'Scopus', "") + originalMask = os.umask(0) + keysDescriptor = os.open(os.path.join(pyscholarDir, 'keys.cfg'), os.O_WRONLY | os.O_CREAT, 0666) + keysFile = os.fdopen(keysDescriptor, 'w') + os.umask(originalMask) + keysParser.write(keysFile) + keysFile.close() + +import scopus \ No newline at end of file diff --git a/pyscholar/pyscholar.py b/pyscholar/scopus.py similarity index 91% rename from pyscholar/pyscholar.py rename to pyscholar/scopus.py index 2f85533..ca1760f 100644 --- a/pyscholar/pyscholar.py +++ b/pyscholar/scopus.py @@ -1,6 +1,3 @@ -# from scopus_key import MY_API_KEY -# print "got", MY_API_KEY - import ConfigParser, os class Key_Exception(Exception): @@ -8,8 +5,8 @@ def __str__(self): return "Scopus key not set." keys = ConfigParser.ConfigParser() -dirPath = os.path.dirname(os.path.abspath(__file__)) -keys.read(os.path.join(dirPath, 'keys.cfg')) +pyscholarDir = os.path.join(os.path.expanduser("~"), ".pyscholar") +keys.read(os.path.join(pyscholarDir, 'keys.cfg')) MY_API_KEY = keys.get('Keys', 'Scopus') if MY_API_KEY == "": @@ -17,7 +14,7 @@ def __str__(self): if ans.lower() == 'y': key = raw_input("Your scopus api key: ") keys.set('Keys', 'Scopus', key) - cfgfile = open(os.path.join(dirPath, 'keys.cfg'), 'w') + cfgfile = open(os.path.join(pyscholarDir, 'keys.cfg'), 'w') keys.write(cfgfile) cfgfile.close() MY_API_KEY = key @@ -39,8 +36,6 @@ def __str__(self): search_api_affiliation_url = "http://api.elsevier.com/content/search/affiliation?" retrieve_api_affiliation_url="http://api.elsevier.com/content/affiliation/affiliation_id/" - - headers = {"Accept":"application/json", "X-ELS-APIKey": MY_API_KEY} scopus_authors_by_idpapers_cache=dict() @@ -70,21 +65,7 @@ def __str__(self): def load_authors_from_file(path=""): """ - This function reads a file where each line of the file is - an author id. - - param path: The directory where the file is located. - :type path: String - :returns: List with the authors ids. - :rtype: List - - :Example: - - >>> import pyscholar - >>> pyscholar.load_authors_from_file("/home/dir/authors.txt") - >>> ['56013555800', '12645109800', '12645615700', '12646275800', '15740951000', '23388216300', '23398643600', '24171073600', '24512697200', '26032291700', '34870304900', '35566511400', '36117667600', '36117782600', '36118513000', '36141027800', '37107692000', '37111010900', '54792735300', '55439178900', '55468916100', '55669785100', '55918277800', '55943170800', '55993686500', '56002701400', '56013629200', '56013731100', '56013734400', '56240672400', '56263920000', '56279187000'] - >>> - + Reads a file where each line of the file is an author id. """ try: with open(directory, 'r') as f: @@ -95,22 +76,7 @@ def load_authors_from_file(path=""): def load_papers_from_file(path=""): """ - This function reads a file where each line of the file is - an paper id. - - param path: The directory where the file is located. - :type path: String - :returns: List with the papers ids. - :rtype: List - - :Example: - - >>> import pyscholar - >>> pyscholar.load_papers_from_file("/home/dir/papers.txt") - >>> ['78149425675', '79959254005', '84883029526', '84887837079', '84892691931', '84893836569', '84897620666', '84904102369', '84908190310', '84919922003', '84924004559', '84925067887', '84928486421', '84928490197', '84928742486', '84939945186', '84939973084', '84945466401', '84961373040'] - - >>> - + Reads a file where each line of the file is an paper id. """ try: with open(directory, 'r') as f: @@ -120,8 +86,10 @@ def load_papers_from_file(path=""): def _add_scopus_id(scopus_id): - """Adds a scopus id to the merged list. Returns False if the ID - is already in the merged list and false otherwise""" + """ + Adds a scopus id to the merged list. Returns False if the ID is already in + the merged list and false otherwise + """ #Buscamos a ver si ya estaba en algunas lista, de ser #asi no hacemos nada @@ -169,20 +137,17 @@ def _union_alias_id(scopus_id_1,scopus_id_2): def _get_alias_id(scopus_id): pass - - - ###FIN DE FUNCIONES de IDs def disable_graphical_interface(): """ - This function disables the graphical environment. + Disables the graphical environment. """ #import matplotlib.pyplot as plt plt.switch_backend('Agg') def enable_graphical_interface(): """ - This function enables the graphical environment. + Enables the graphical environment. """ #['pdf', 'pgf', 'Qt4Agg', 'GTK', 'GTKAgg', 'ps', 'agg', 'cairo', 'MacOSX', 'GTKCairo', 'WXAgg', 'template', 'TkAgg', 'GTK3Cairo', 'GTK3Agg', 'svg', 'WebAgg', 'CocoaAgg', 'emf', 'gdk', 'WX'] plt.switch_backend('GTK') @@ -192,68 +157,19 @@ def enable_graphical_interface(): def load_graph_pickle(path=""): """ - Loads a graph object in :mod:`pickle` format. - - This function loads a graphical object from a directory in the pickle format and returns an object graph of the networkx library. - - :param path: The directory where the object pickle is located. - :type path: String. - :returns: Return a graph G. - :rtype: Return a NetworkX graph object. - - :Example: - - >>> import pyscholar - >>> my_graph = psycholar.load_graph_pickle("~/dir/graph.pickle") - >>> my_graph + Loads a graph object saved using :mod:`pickle` and returns a networkx graph object. """ return nx.read_gpickle(path) def save_graph_pickle(G,path="",name_graph=""): """ - Saves graph in :mod:`pickle` format. - - This function saves a graphic object in pickle format in a directory. - - :param G: The graph that will be saved. - :param path: The directory where the graph will be saved. - :param name_graph: The name of the graph. - :type G: NetworkX graph object. - :type path: String. - :type name_graph: String. - :returns: The function doesn't return any value. - - :Example: - - >>> import pyscholar - >>> psycholar.save_graph_pickle(G,"~/dir/","my_graph") - >>> + Saves a graph object using :mod:`pickle`. """ nx.write_gpickle(G,path+name_graph+".gpickle") def find_affiliation_scopus_id_by_name(organization=""): """ - This function returns a data frame which contains all matches found by the search affiliation name. - - :param organization: The name of the affiliation that you want to search. - :type organization: String - :returns: A data frame that has different attributes (id,city,country,name_variant,eid,affiliation_name,identifier,document_count). - :rtype: DataFrame - - :Example: - - >>> import pyscholar - >>> pyscholar.find_affiliation_scopus_id_by_name("CINVESTAV") - id city country name_variant eid affiliation_name identifier document_count - 0 60017323 Mexico City Mexico [CINVESTAV-IPN, CINVESTAV] 10-s2.0-60017323 Centro de Investigacion y de Estudios Avanzados AFFILIATION_ID:60017323 19254 - 1 60010531 Mérida Mexico [CINVESTAV-IPN] 10-s2.0-60010531 CINVESTAV Unidad Merida AFFILIATION_ID:60010531 1811 - 2 60018216 Guadalajara Mexico [CINVESTAV, CINVESTAV Unidad Guadalajara] 10-s2.0-60018216 CINVESTAV Unidad Guadalajara AFFILIATION_ID:60018216 1010 - . - . - . - . - >>> - + Returns a data frame which contains all matches found by the search of an affiliation name. """ searchQuery = "query=affil("+organization+")" fields = "" @@ -1089,4 +1005,4 @@ def get_author_affiliations(firstName="", lastName=""): if 'affiliation-current' in entry: affiliations.append(entry['affiliation-current']) - return affiliations + return affiliations \ No newline at end of file diff --git a/setup.py b/setup.py index b6e8c1e..8b66b7e 100644 --- a/setup.py +++ b/setup.py @@ -3,26 +3,13 @@ import sys import os -if "install" in sys.argv: - #We create keys.cfg before installing - keysParser = ConfigParser.ConfigParser() - keysParser.add_section("Keys") - keysParser.set('Keys', 'Scopus', "") - dirPath = os.path.dirname(os.path.abspath(__file__)) - originalMask = os.umask(0) - keysDescriptor = os.open(os.path.join(dirPath, 'pyscholar/keys.cfg'), os.O_WRONLY | os.O_CREAT, 0666) - keysFile = os.fdopen(keysDescriptor, 'w') - os.umask(originalMask) - keysParser.write(keysFile) - keysFile.close() - def readme(): with open('README.rst') as f: return f.read() setup(name='pyscholar', - version='1.0.0.dev1', - description='A python library to access Scopus', + version='1.0.0.dev3', + description='A python library to access academic APIs', url='http://rfabila.github.io/Pyscholar/', author='DesarrolloDeSoftware', author_email='rfabila@math.cinvestav.edu.mx',