Skip to main content

PercuSense Common Modules

Project description

PSENSE-COMMON

PyPI PyPI - Python Version PyPI - License

Common Modules used by internal PercuSense python applications. This project follows semantic versioning (breaking.major.minor release).

Getting Started

General Instructions

Steps to try this out yourself:

  1. Install the psense-common library:

From PyPi

$ pip install psense-common

Local Installation

$ git clone git@bitbucket.org:psense/psense-common.git
$ python setup.py install
  1. Import the modules you need in your script.
from psense_common import PSenseParser)
  1. Access module methods directly.
from psense_common import PSenseParser

filename = '/path/to/data/file'
parser = PSenseParser()
parser.identify_file_source(filename)

if parser.source:
    parser.load_rawfile(filename)
    print(parser.data) # this is of type pandas.DataFrame

AWS

psense_common/psense_aws_itfc.py introduces the class PSenseAWSInterface, which provides an interface to the Amazon DynamoDB datastore. Currently, data is stored on a per-record basis (NoSQL) for Experiment, Sensor, and Event data ("Experiments", "SensorData", and "Calibration" -- not fully supported, will be changed to "EventData" in migration from 0.x.x to 1.x.x).

The class contains query and put operations but does not provide provisions for deleting items from the datastore.

Credentials

At initialization, the class will create an AWS session that is persisted. The session object allows the user to authenticate just a single time (rather than for every communication).

In generating the session, bot3 will attempt to load a profile (if not provided, will try "PShield") that contains the necessary credentials for authentication/authorization to our dynamodb instances. If the profile doesn't exist or cannot be loaded, the class reverts to the environment default AWS credentials in the 'us-east-1' region.

config and credential files for aws are stored in the user's home directory: ~/.aws/. In Windows, the comparable location is C:\Users\[username]\.aws\

Usage

Get Sensor Data

from psense_common import PSenseAWSInterface
import pytz

aws = PSenseAWSInterface(debugmode=True)
aws.set_query_config(req_size=7200, query_count=2)

experiments = ['[my experiment id 1]',
               '[my experiment id 2]']

for expid in experiments:
    if not aws.verif_experiment(expid):
        print('invalid experiment id skipping {}'.format(expid))
        pass

    count, data = aws.get_sensor_data()
    data.index = data.index.tz_localize(pytz.utc).tz_convert(localtz)

    print(data)

Add Experiment and real-time Sensor data

from psense_common import (PSenseAWSInterface, PSenseParser)

# user variables
filename = '[vfp600 gamry file].txt'
expid = 'experimentid'

# initialize classes + helper func
parser = PSenseParser(debugmode=True)
aws = PSenseAWSInterface(debugmode=True)
aws.set_query_config(req_size=7200, query_count=2)

def tail(fin):
    "Listen for new lines added to file."
    while True:
        where = fin.tell()            
        line = fin.readline()
        if not line:
            time.sleep(SLEEP_INTERVAL)
            fin.seek(where)
        else:
            yield line

# identify the type of file we are parsing
if not parser.identify_file_source(filename):
    print('unknown file type. exiting')
    assert False


# confirm that the experiment id is valid
if not aws.verif_experiment(expid):
    print('invalid experiment id skipping {}'.format(expid))
    assert False


# add experiment to database
add_experiment_success, if_fail_reason = aws.add_experiment()
if if_fail_reason == 'error':
    print('Aborted. Error occurred in communication with AWS.')
    assert False
elif if_fail_reason == 'exists':
    print('Experiment already exists in database. Must delete existing experiment sensor data before uploading new values.')


# "tail" the file and send each record to ddb
with open(filename, 'r') as fin:
    # skip to the end of the file before beginning tail
    fin.seek(0, os.SEEK_END)

    for line in tail(fin):
        row = parser.parse_record(line.strip())
        aws.add_sensordata(*row)

Future notes: Sensor and Event data should remain as a blob-store -- we expect schema to change depending on the type of sensor (or event). For example, sensors with 3 working electrodes should contain more properties than sensors with a single signal.

Experiment ID Formatting

All sensor data is associated with a particular experiment ID. Experiment IDs should be unique and follow the PercuSense naming scheme.

Usage

Decoding an experiment:

from psense_common import (psense_format)

experiment = '[my experiment id]'
validate_name = psense_format.psense_exp_title()
validate_name.decode(experiment)

Generating an experiment id through console IO (must provide 3-digit device id):

from psense_common import (psense_format)

device_id = 'P01'
experiment = psense_format.setup_new_experiment(device_id)

Data Parsing

The PSenseParser class will parse data from flat-file into Pandas DataFrame. Output object will contain columns appropriate for PercuSense analysis scripts (and PercuSense Data Viewer web application). The parser works on individual records (live streaming) as well as full files (retrospective analysis).

Supported formats:

BWII dual-channel
PSHIELD
GAMRY VFP600
GAMRY EXPLAIN
DATA VIEWER single-single channel
DATA VIEWER dual-channel
EMSTAT Export

TODO: documentation pending

See above sections (General Instructions, AWS) for basic usage.

PercuSense Data Filter

TODO: documentation pending

Development

Tests

$ python setup.py test

.. or with code coverage:

$ coverage run --source=psense_common/ setup.py test
$ coverage report -m

Publishing

Bitbucket has been configured to run tests and publish directly to pypi after code is merged to master. See bitbucket-pipelines.yml for configuration details.

Manual publishing (not recommended):

$ rm -rf dist
$ python setup.py sdist
$ twine upload dist/*

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

psense_common-0.2.6.tar.gz (30.8 kB view details)

Uploaded Source

Built Distribution

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

psense_common-0.2.6-py3-none-any.whl (33.7 kB view details)

Uploaded Python 3

File details

Details for the file psense_common-0.2.6.tar.gz.

File metadata

  • Download URL: psense_common-0.2.6.tar.gz
  • Upload date:
  • Size: 30.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.2

File hashes

Hashes for psense_common-0.2.6.tar.gz
Algorithm Hash digest
SHA256 61afc9e334ace9fe41dd2deb3f6f8c28c44625c8fc43fe66d5e01408f48ac0bc
MD5 89987728f1c1f2907335780bda7b0ad8
BLAKE2b-256 0b4e5ba06da1bfd584959f5c2f84102f92f9dbe22dba3aef5b50c5c78eec98b5

See more details on using hashes here.

File details

Details for the file psense_common-0.2.6-py3-none-any.whl.

File metadata

  • Download URL: psense_common-0.2.6-py3-none-any.whl
  • Upload date:
  • Size: 33.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.2

File hashes

Hashes for psense_common-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 a4e2001e3fd89df21ed62a06819f6e397dcafa0c099fcf700ccf4dfc8b392d7d
MD5 fdaddddcb0753a8751166a2717b2d8fb
BLAKE2b-256 aecc4a8969915d35bcf680d49c5e0f3f90880ba5ac34f19b65c1a00a88477e27

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