Skip to main content

BioC data structures and encoder/decoder for Python

Project description

Build status Latest version on PyPI Downloads License

BioC XML / JSON format can be used to share text documents and annotations.

bioc exposes an API familiar to users of the standard library marshal and pickle modules.

Development of bioc happens on GitHub: https://github.com/bionlplab/bioc

Getting started

Installing bioc

$ pip install bioc

XML

Encoding the BioC collection object collection:

import bioc

# Serialize ``collection`` to a BioC formatted ``str``.
bioc.dumps(collection)

# Serialize ``collection`` as a BioC formatted stream to ``fp``.
with open(filename, 'w') as fp
    bioc.dump(collection, fp)

Compact encoding:

import bioc
bioc.dumps(collection, pretty_print=False)

Incremental BioC serialisation:

from bioc import biocxml
with biocxml.iterwrite(filename) as writer:
    writer.write_collection_info(collection)
    for document in collection.documents:
       writer.write_document(document)

Decoding the BioC XML file:

import bioc

# Deserialize ``s`` to a BioC collection object.
collection = bioc.loads(s)

# Deserialize ``fp`` to a BioC collection object.
with open(filename, 'r') as fp:
    collection = bioc.load(fp)

Incrementally decoding the BioC XML file:

from bioc import biocxml

# read from a file
with biocxml.iterparse(filename) as reader:
    collection_info = reader.get_collection_info()
    for document in reader:
        # process document
        ...

# read from a ByteIO
with biocxml.iterparse(open(filename, 'rb')) as reader:
    collection_info = reader.get_collection_info()
    for document in reader:
        # process document
        ...

get_collection_info can be called after the with statement.

Together with Python coroutines, this can be used to generate BioC XML in an asynchronous, non-blocking fashion.

from bioc import biocxml

with biocxml.iterparse(source) as reader, biocxml.iterwrite(dest) as writer:
    collection_info = reader.get_collection_info()
    writer.write_collection_info(collection_info)
    for document in reader:
        # modify the document
        ...
        writer.write_document(document)

Json

Encoding the BioC collection object collection:

import bioc

# Serialize ``collection`` to a BioC Json formatted ``str``.
bioc.dumps(collection, BioCFileType.BIOC_JSON, indent=2)

# Serialize ``collection`` as a BioC Json formatted stream to ``fp``.
with open(filename, 'w') as fp
    bioc.dump(collection, BioCFileType.BIOC_JSON, fp, indent=2)

Compact encoding:

import bioc
bioc.dumps(collection, BioCFileType.BIOC_JSON)

Decoding the BioC Json file:

import bioc

# Deserialize ``s`` to a BioC collection object.
collection = bioc.loads(s, BioCFileType.BIOC_JSON)

# Deserialize ``fp`` to a BioC collection object.
with open(filename, 'r') as fp:
    collection = bioc.load(fp, BioCFileType.BIOC_JSON)

Json Lines

Incrementally encoding the BioC structure:

from bioc import BioCJsonIterWriter
with open(filename, 'w', encoding='utf8') as fp:
    writer = BioCJsonIterWriter(fp, level=bioc.PASSAGE)
    for doc in collection.documents:
         for passage in doc.passages:
             writer.write(passage)

or

from bioc import toJSON
import jsonlines
with jsonlines.open(filename, 'w') as writer:
    for doc in collection.documents:
         for passage in doc.passages:
             writer.write(toJSON(passage))

Incrementally decoding the BioC Json lines file:

from bioc import BioCJsonIterReader
with open(filename, 'r', encoding='utf8') as fp:
    reader = BioCJsonIterReader(fp, level=bioc.PASSAGE)
    for passage in reader:
        # process passage
        ...

or

from bioc import fromJSON
import jsonlines
with jsonlines.open(filename) as reader:
    for obj in reader:
        passage = fromJSON(obj, level=bioc.PASSAGE)
        ...

Development

Test

$ pytest cov=bioc tests

Publish BioC to PyPI and TestPyPI

First, you need a PyPI user account. You can create an account using the form on the PyPI/TestPyPI website.

Now you’ll create a PyPI/TestPyPI API token so you will be able to securely upload your project.

Go to https://pypi.org/manage/account/#api-tokens and create a new API token; don’t limit its scope to a particular project, since you are creating a new project.

$ python -m build
$ python -m twine upload --repository testpypi dist\*

Developers

Webpage

The official BioC webpage is available with all up-to-date instructions, code, and corpora in the BioC format, and other research on, based on and related to BioC.

Reference

If you use bioc in your research, please cite the following paper:

  • Peng Y, Tudor CO, Torii M, Wu CH, Vijay-Shanker K. iSimp in BioC standard format: Enhancing the interoperability of a sentence simplification system. Database (Oxford). 2014, 1-8. bau038.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bioc-2.0.dev2.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bioc-2.0.dev2-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file bioc-2.0.dev2.tar.gz.

File metadata

  • Download URL: bioc-2.0.dev2.tar.gz
  • Upload date:
  • Size: 19.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for bioc-2.0.dev2.tar.gz
Algorithm Hash digest
SHA256 e9e818e17914b07074cb16ea99ce5cce1aa26205743b3aef73c25e441ef46f90
MD5 79bab0a31e7a5754dcd684d1e5d4c88b
BLAKE2b-256 22a84656d7742fe9427cabe32abebb6ede03257414df2aabfb6b72fb21ce53b8

See more details on using hashes here.

File details

Details for the file bioc-2.0.dev2-py3-none-any.whl.

File metadata

  • Download URL: bioc-2.0.dev2-py3-none-any.whl
  • Upload date:
  • Size: 24.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for bioc-2.0.dev2-py3-none-any.whl
Algorithm Hash digest
SHA256 7ea8ac0d5691eff283772c33a4ae63165ca00b76dfd87b7c8b87b6f55692b85d
MD5 6009207939fecaaa102c38f4a41b57cc
BLAKE2b-256 a04cd5dea01825c5433e18467b9e76e4ce0118c2f83ebc8cc77198d1e2819ee1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page