xio¶
Module: xio
¶
Utility and I/O functions to read and write data files or Synapse tables.
- Authors:
- Arno Klein, 2015 (arno@sagebase.org) http://binarybottle.com
Copyright 2015, Sage Bionetworks (http://sagebase.org), Apache v2.0 License
Functions¶
-
mhealthx.xio.
convert_audio_file
(old_file, new_file, command='ffmpeg', input_args='-i', output_args='-ac 2')¶ Convert audio file to new format.
- old_file : string
- full path to the input file
- new_file : string
- full path to the output file
- command : string
- executable command without arguments
- input_args : string
- arguments preceding input file name in command
- output_args : string
- arguments preceding output file name in command
- new_file : string
- full path to the output file
>>> from mhealthx.xio import convert_audio_file >>> old_file = '/Users/arno/mhealthx_cache/mhealthx/feature_files/test.m4a' >>> new_file = 'test.wav' >>> command = 'ffmpeg' >>> input_args = '-y -i' >>> output_args = '-ac 2' >>> new_file = convert_audio_file(old_file, new_file, command, input_args, output_args)
-
mhealthx.xio.
extract_synapse_rows
(synapse_table, save_path=None, limit=None, username='', password='')¶ Extract rows from a Synapse table.
- synapse_table : string or Schema
- a synapse ID or synapse table Schema object
- save_path : string
- save rows as separate files in this path, unless empty or None
- limit : integer or None
- limit to number of rows returned by the query
- username : string
- Synapse username (only needed once on a given machine)
- password : string
- Synapse password (only needed once on a given machine)
- rows : list of pandas Series
- each row of a Synapse table
- row_files: list of strings
- file names corresponding to each of the rows
>>> from mhealthx.xio import extract_synapse_rows >>> import synapseclient >>> syn = synapseclient.Synapse() >>> syn.login() >>> synapse_table = 'syn4590865' >>> save_path = '.' >>> limit = 3 >>> username = '' >>> password = '' >>> rows, row_files = extract_synapse_rows(synapse_table, save_path, limit, username='', password='')
-
mhealthx.xio.
get_accel
(synapse_table, row, column_name, start=0, device_motion=True, out_path='.', username='', password='')¶ Read accelerometer json data from Synapse table row.
- Calls ::
- from mhealthx.xio import read_file_from_synapse_table from mhealthx.xio import read_accel_json
- synapse_table : string or Schema
- a synapse ID or synapse table Schema object
- row : pandas Series or string
- row of a Synapse table converted to a Series or csv file
- column_name : string
- name of file handle column
- start : integer
- starting index (remove beginning)
- device_motion : Boolean
- use deviceMotion vs. accelerometer json file?
- out_path : string or None
- a local path in which to store downloaded files. If None, stores them in (~/.synapseCache)
- username : string
- Synapse username (only needed once on a given machine)
- password : string
- Synapse password (only needed once on a given machine)
- t : list
- time points for accelerometer data
- ax : list
- x-axis acceleration
- ay : list
- y-axis acceleration
- az : list
- z-axis acceleration
- gx : list
- x-axis gravity acceleration
- gy : list
- y-axis gravity acceleration
- gz : list
- z-axis gravity acceleration
- rx : list
- x-axis rotationRate
- ry : list
- y-axis rotationRate
- rz : list
- z-axis rotationRate
- uw : list
- w of attitude quaternion
- ux : list
- x of attitude quaternion
- uy : list
- y of attitude quaternion
- uz : list
- z of attitude quaternion
- sample_rate : float
- sample rate
- duration : float
- duration of time series
- row : pandas Series
- same as passed in: row of a Synapse table as a file or Series
- file_path : string
- path to accelerometer file
>>> from mhealthx.xio import extract_synapse_rows, read_file_from_synapse_table, get_accel >>> import synapseclient >>> syn = synapseclient.Synapse() >>> syn.login() >>> synapse_table = 'syn4590866' >>> row_series, row_files = extract_synapse_rows(synapse_table, save_path='.', limit=3, username='', password='') >>> column_name = 'deviceMotion_walking_outbound.json.items' >>> device_motion = True >>> start = 150 >>> out_path = None >>> username = '' >>> password = '' >>> for i in range(1): >>> row = row_series[i] >>> row, filepath = read_file_from_synapse_table(synapse_table, row, >>> column_name, out_path, username, password) >>> print(row) >>> t, ax, ay, az, gx, gy, gz, rx, ry, rz, uw, ux, uy, uz, sample_rate, duration, row, file_path = get_accel(synapse_table, >>> row, column_name, >>> start, device_motion, >>> out_path, username, password)
-
mhealthx.xio.
get_convert_audio
(synapse_table, row, column_name, convert_file_append='', convert_command='ffmpeg', convert_input_args='-y -i', convert_output_args='-ac 2', out_path='.', username='', password='')¶ Read data from a row of a Synapse table and convert audio file.
- Calls ::
- from mhealthx.xio import read_file_from_synapse_table from mhealthx.xio import convert_audio_file
- synapse_table : string or Schema
- a synapse ID or synapse table Schema object
- row : pandas Series or string
- row of a Synapse table converted to a Series or csv file
- column_name : string
- name of file handle column
- convert_file_append : string
- append to file name to indicate converted file format (e.g., ‘.wav’)
- convert_command : string
- executable command without arguments
- convert_input_args : string
- arguments preceding input file name for convert_command
- convert_output_args : string
- arguments preceding output file name for convert_command
- out_path : string or None
- a local path in which to store downloaded files.
- username : string
- Synapse username (only needed once on a given machine)
- password : string
- Synapse password (only needed once on a given machine)
- row : pandas Series
- same as passed in: row of a Synapse table as a file or Series
- new_file : string
- full path to the converted file
>>> from mhealthx.xio import get_convert_audio >>> from mhealthx.xio import extract_synapse_rows, read_file_from_synapse_table >>> import synapseclient >>> syn = synapseclient.Synapse() >>> syn.login() >>> synapse_table = 'syn4590865' >>> row_series, row_files = extract_synapse_rows(synapse_table, save_path='.', limit=3, username='', password='') >>> column_name = 'audio_audio.m4a' #, 'audio_countdown.m4a'] >>> convert_file_append = '.wav' >>> convert_command = 'ffmpeg' >>> convert_input_args = '-y -i' >>> convert_output_args = '-ac 2' >>> out_path = '.' >>> username = '' >>> password = '' >>> for i in range(1): >>> row = row_series[i] >>> row, filepath = read_file_from_synapse_table(synapse_table, row, >>> column_name, out_path, username, password) >>> print(row) >>> row, new_file = get_convert_audio(synapse_table, >>> row, column_name, >>> convert_file_append, >>> convert_command, >>> convert_input_args, >>> convert_output_args, >>> out_path, username, password)
-
mhealthx.xio.
get_tap
(synapse_table, row, column_name, start=0, out_path='.', username='', password='')¶ Read screen tapping json data from Synapse table row.
- Calls ::
- from mhealthx.xio import read_file_from_synapse_table from mhealthx.xio import read_tap_json
- synapse_table : string or Schema
- a synapse ID or synapse table Schema object
- row : pandas Series or string
- row of a Synapse table converted to a Series or csv file
- column_name : string
- name of file handle column
- start : integer
- starting index (remove beginning)
- out_path : string or None
- a local path in which to store downloaded files. If None, stores them in (~/.synapseCache)
- username : string
- Synapse username (only needed once on a given machine)
- password : string
- Synapse password (only needed once on a given machine)
- tx : list
- x-axis screen tap data
- ty : list
- y-axis screen tap data
- t : list
- time points for accelerometer data
- sample_rate : float
- sample rate
- duration : float
- duration of time series
- row : pandas Series
- same as passed in: row of a Synapse table as a file or Series
- file_path : string
- path to accelerometer file
>>> from mhealthx.xio import extract_synapse_rows, read_file_from_synapse_table, get_tap >>> import synapseclient >>> syn = synapseclient.Synapse() >>> syn.login() >>> synapse_table = 'syn4590866' >>> row_series, row_files = extract_synapse_rows(synapse_table, save_path='.', limit=3, username='', password='') >>> column_name = 'deviceMotion_walking_outbound.json.items' >>> start = 150 >>> out_path = None >>> username = '' >>> password = '' >>> for i in range(1): >>> row = row_series[i] >>> row, filepath = read_file_from_synapse_table(synapse_table, row, >>> column_name, out_path, username, password) >>> print(row) >>> tx, ty, t, sample_rate, duration, row, file_path = get_tap(synapse_table, >>> row, column_name, >>> start, out_path, username, password)
-
mhealthx.xio.
read_accel_json
(input_file, start=0, device_motion=True)¶ Read accelerometer or deviceMotion json file.
- input_file : string
- name of input accelerometer json file
- start : integer
- starting index (remove beginning)
- device_motion : Boolean
- use deviceMotion vs. accelerometer json file?
- t : list
- time points for accelerometer data
- axyz : list of lists
- x-, y-, and z-axis accelerometer data
- gxyz : list of lists
- x-, y-, and z-axis gravity (if deviceMotion)
- wxyz : list of lists
- w, x, y, z attitude quaternion (if deviceMotion)
- rxyz : list of lists
- x-, y-, and z-axis rotationRate (if deviceMotion)
- sample_rate : float
- sample rate
- duration : float
- duration of time series
>>> from mhealthx.xio import read_accel_json >>> input_file = '/Users/arno/DriveWork/mhealthx/mpower_sample_data/deviceMotion_walking_outbound.json.items-90f7096a-84ac-4f29-a4d1-236ef92c3d262549858224214804657.tmp' >>> start = 150 >>> device_motion = True >>> t, axyz, gxyz, wxyz, rxyz, sample_rate, duration = read_accel_json(input_file, start, device_motion)
-
mhealthx.xio.
read_file_from_synapse_table
(synapse_table, row, column_name, out_path=None, username='', password='')¶ Read data from a row of a Synapse table.
- synapse_table : string or Schema
- a synapse ID or synapse table Schema object
- row : pandas Series or string
- row of a Synapse table converted to a Series or csv file
- column_name : string
- name of file handle column
- out_path : string
- a local path in which to store downloaded files
- username : string
- Synapse username (only needed once on a given machine)
- password : string
- Synapse password (only needed once on a given machine)
- row : pandas Series
- same as passed in: row of a Synapse table as a file or Series
- filepath : string
- downloaded file (full path)
>>> from mhealthx.xio import extract_synapse_rows, read_file_from_synapse_table >>> import synapseclient >>> syn = synapseclient.Synapse() >>> syn.login() >>> synapse_table = 'syn4590865' >>> save_path = '.' >>> limit = 3 >>> username = '' >>> password = '' >>> rows, row_files = extract_synapse_rows(synapse_table, save_path, limit, username='', password='') >>> column_name = 'audio_audio.m4a' #, 'audio_countdown.m4a'] >>> out_path = None >>> for i in range(3): >>> row = rows[i] >>> row, filepath = read_file_from_synapse_table(synapse_table, row, column_name, out_path, username, password) >>> print(row)
-
mhealthx.xio.
read_tap_json
(input_file, start=0)¶ Read screen tap json file.
- input_file : string
- name of input screen tap json file
- start : integer
- starting index (remove beginning)
- t : list
- time points for tap data
- tx : list
- x coordinates of touch screen
- ty : list
- y coordinates of touch screen
- button : list
- buttons tapped
- sample_rate : float
- sample rate
- duration : float
- duration of time series
>>> from mhealthx.xio import read_tap_json >>> input_file = '/Users/arno/DriveWork/mhealthx/mpower_sample_data/tapping_results.json.TappingSamples-49d2531d-dbda-4b6d-b403-f8763b8e05841011283015383434299.tmp' >>> start = 0 >>> t, tx, ty, button, sample_rate, duration = read_tap_json(input_file, start)
-
mhealthx.xio.
row_to_table
(row_data, output_table)¶ Add row to table using nipype (thread-safe in multi-processor execution).
(Requires Python module lockfile)
- row_data : pandas Series
- row of data
- output_table : string
- add row to this table file
>>> import pandas as pd >>> from mhealthx.xio import row_to_table >>> row_data = pd.Series({'A': ['A0'], 'B': ['B0'], 'C': ['C0']}) >>> output_table = 'test.csv' >>> row_to_table(row_data, output_table)
-
mhealthx.xio.
write_wav
(data, file_stem, file_append, sample_rate=44100, amplitude=32700)¶ Convert a list or array of numbers to a .wav format audio file.
After: http://blog.acipo.com/wave-generation-in-python/ and https://gist.github.com/Pretz/1773870 and http://codingmess.blogspot.com/2008/07/
how-to-make-simple-wav-file-with-python.html- data : list or array of floats or integers
- input data to convert to audio file
- file_stem : string
- stem of file name of output audio file (including absolute path)
- file_append : string
- append string to file_stem for full output audio file path and name
- sample_rate : integer
- number of desired samples per second for audio file
- amplitude : integer
- maximum amplitude for audio file (32700 is within signed short)
- wav_file : string
- name of output .wav audio file
>>> from mhealthx.xio import write_wav >>> import numpy as np >>> from scipy.signal import resample >>> file_stem = '/desk/temp' >>> file_append = 'write_wav.wav' >>> sample_rate = 44100 >>> amplitude = 32700 >>> data = np.random.random(500000) >>> data /= np.max(np.abs(data)) >>> #data = resample(data, sample_rate/framerate) >>> wav_file = write_wav(data, file_stem, file_append, sample_rate, amplitude)