mirror of
https://github.com/echemdata/galvani.git
synced 2025-12-14 17:15:36 +00:00
Tweaks to make things work on Python 2
This commit is contained in:
23
BioLogic.py
23
BioLogic.py
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
__all__ = ['MPTfileCSV', 'MPTfile']
|
__all__ = ['MPTfileCSV', 'MPTfile']
|
||||||
|
|
||||||
|
import sys
|
||||||
import re
|
import re
|
||||||
import csv
|
import csv
|
||||||
from os import SEEK_SET, SEEK_CUR
|
from os import SEEK_SET, SEEK_CUR
|
||||||
@@ -14,6 +15,12 @@ from warnings import warn
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
|
|
||||||
|
if sys.version_info.major <= 2:
|
||||||
|
str3 = str
|
||||||
|
else:
|
||||||
|
str3 = lambda b: str(b, encoding='ascii')
|
||||||
|
|
||||||
|
|
||||||
def fieldname_to_dtype(fieldname):
|
def fieldname_to_dtype(fieldname):
|
||||||
"""Converts a column header from the MPT file into a tuple of
|
"""Converts a column header from the MPT file into a tuple of
|
||||||
canonical name and appropriate numpy dtype"""
|
canonical name and appropriate numpy dtype"""
|
||||||
@@ -34,7 +41,7 @@ def fieldname_to_dtype(fieldname):
|
|||||||
return ("I/mA", np.float_)
|
return ("I/mA", np.float_)
|
||||||
else:
|
else:
|
||||||
raise ValueError("Invalid column header: %s" % fieldname)
|
raise ValueError("Invalid column header: %s" % fieldname)
|
||||||
|
|
||||||
|
|
||||||
def MPTfile(file_or_path):
|
def MPTfile(file_or_path):
|
||||||
"""Opens .mpt files as numpy record arrays
|
"""Opens .mpt files as numpy record arrays
|
||||||
@@ -152,7 +159,7 @@ def VMPdata_dtype_from_colIDs(colIDs):
|
|||||||
dtype_dict['(Q-Qo)/C'] = '<f4'
|
dtype_dict['(Q-Qo)/C'] = '<f4'
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError("column type %d not implemented" % colID)
|
raise NotImplementedError("column type %d not implemented" % colID)
|
||||||
return np.dtype(list(dtype_dict.items()))
|
return np.dtype(list(dtype_dict.items()))
|
||||||
|
|
||||||
|
|
||||||
def read_VMP_modules(fileobj, read_module_data=True):
|
def read_VMP_modules(fileobj, read_module_data=True):
|
||||||
@@ -224,7 +231,7 @@ class MPRfile:
|
|||||||
maybe_log_module = [m for m in modules if m['shortname'] == b'VMP LOG ']
|
maybe_log_module = [m for m in modules if m['shortname'] == b'VMP LOG ']
|
||||||
|
|
||||||
n_data_points = np.fromstring(data_module['data'][:4], dtype='<u4')
|
n_data_points = np.fromstring(data_module['data'][:4], dtype='<u4')
|
||||||
n_columns = int(data_module['data'][4])
|
n_columns = np.fromstring(data_module['data'][4:5], dtype='u1')
|
||||||
|
|
||||||
if data_module['version'] == 0:
|
if data_module['version'] == 0:
|
||||||
column_types = np.fromstring(data_module['data'][5:], dtype='u1',
|
column_types = np.fromstring(data_module['data'][5:], dtype='u1',
|
||||||
@@ -241,7 +248,9 @@ class MPRfile:
|
|||||||
raise ValueError("Unrecognised version for data module: %d" %
|
raise ValueError("Unrecognised version for data module: %d" %
|
||||||
data_module['version'])
|
data_module['version'])
|
||||||
|
|
||||||
assert(not any(remaining_headers))
|
for empty_byte in remaining_headers:
|
||||||
|
assert(empty_byte == b'\x00')
|
||||||
|
|
||||||
self.dtype = VMPdata_dtype_from_colIDs(column_types)
|
self.dtype = VMPdata_dtype_from_colIDs(column_types)
|
||||||
self.data = np.fromstring(main_data, dtype=self.dtype)
|
self.data = np.fromstring(main_data, dtype=self.dtype)
|
||||||
assert(self.data.shape[0] == n_data_points)
|
assert(self.data.shape[0] == n_data_points)
|
||||||
@@ -252,14 +261,12 @@ class MPRfile:
|
|||||||
self.cols = column_types
|
self.cols = column_types
|
||||||
self.npts = n_data_points
|
self.npts = n_data_points
|
||||||
|
|
||||||
tm = time.strptime(str(settings_mod['date'], encoding='ascii'),
|
tm = time.strptime(str3(settings_mod['date']), '%m/%d/%y')
|
||||||
'%m/%d/%y')
|
|
||||||
self.startdate = date(tm.tm_year, tm.tm_mon, tm.tm_mday)
|
self.startdate = date(tm.tm_year, tm.tm_mon, tm.tm_mday)
|
||||||
|
|
||||||
if maybe_log_module:
|
if maybe_log_module:
|
||||||
log_module, = maybe_log_module
|
log_module, = maybe_log_module
|
||||||
tm = time.strptime(str(log_module['date'], encoding='ascii'),
|
tm = time.strptime(str3(log_module['date']), '%m/%d/%y')
|
||||||
'%m/%d/%y')
|
|
||||||
self.enddate = date(tm.tm_year, tm.tm_mon, tm.tm_mday)
|
self.enddate = date(tm.tm_year, tm.tm_mon, tm.tm_mday)
|
||||||
|
|
||||||
## There is a timestamp at either 465 or 469 bytes
|
## There is a timestamp at either 465 or 469 bytes
|
||||||
|
|||||||
Reference in New Issue
Block a user