Skip to main content

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 2-channel
BWII 3-Channel ("BWII-MINI")
PSHIELD
GAMRY VFP600
GAMRY EXPLAIN
DATA VIEWER single-channel
DATA VIEWER 2-channel
DATA VIEWER 3-channel
CH Instruments (txt)

TODO: documentation pending

Check demo/parser.py for some example usage.

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 (pip install --upgrade 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/*

Release files for psense-common 0.4.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for psense-common 0.4.11
File Size Uploaded
psense_common-0.4.11.tar.gz 29.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for psense-common 0.4.11
File Interpreter ABI Platform
psense_common-0.4.11-py3-none-any.whl Python 3 none any Details

Total release size: 59.4 kB

Release files / psense_common-0.4.11.tar.gz

Download URL psense_common-0.4.11.tar.gz
Size 29.3 kB
Tags Source
SHA-256 checksum
How to use checksums
8604d55f43b9ef60e543ae2179f2a9fc642b4a69a024389f449a78b4df77d0c2
BLAKE2b-256 checksum
How to use checksums
faabecb8a6167eb79268b9ef64d98c39cba4622bc169be7bfd289561ef408437
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.0 CPython/3.11.6 Linux/5.10.101

Release files / psense_common-0.4.11-py3-none-any.whl

Download URL psense_common-0.4.11-py3-none-any.whl
Size 30.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
598553ada2d20d7818929900564fd877fba4cb7f3ac0ccb5590ea30359d80f26
BLAKE2b-256 checksum
How to use checksums
c705d851b2e5fe1e123e9dab64fa688ad080e3a70eff1ec309ac221c2f63f3a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.0 CPython/3.11.6 Linux/5.10.101

Release history Release notifications | RSS feed

This release

0.4.11 This release

2 release files

0.4.9

2 release files

0.4.8

2 release files

0.4.7

2 release files

0.4.6

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.20

2 release files

0.3.19

2 release files

0.3.18

2 release files

0.3.17

2 release files

0.3.16

2 release files

0.3.14

2 release files

0.3.12

2 release files

0.3.11

2 release files

0.3.10

2 release files

0.3.9

2 release files

0.3.8

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.2.13

2 release files

0.2.12

2 release files

0.2.11

2 release files

0.2.10

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.14

1 release file

0.1.13

1 release file

0.1.12

1 release file

0.1.11

1 release file

0.1.10

1 release file

0.1.9

2 release files

0.1.8

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page