/usr/lib/stimfit/hdf5tools.py is in stimfit 0.15.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | import sys
sys.argv = ""
import tables
import numpy as np
class Recording():
def __init__(self, channels, comment, date, time):
self.channels = channels
self.comment = comment
self.date = date
self.time = time
def __getitem__( self, i ):
return self.channels[i]
def get_list( self ):
return [ [ s.data for s in c.sections ] for c in self.channels ]
def __len__( self ):
return len( self.channels )
class Channel():
def __init__(self, sections, name):
self.sections = sections
self.name = name
def __len__( self ):
return len( self.sections )
def __getitem__( self, i ):
return self.sections[i]
class Section():
def __init__(self, data, dt, xunits, yunits):
self.data = data
self.dt = dt
self.xunits = xunits
self.yunits = yunits
def __len__( self ):
return len( self.data )
def __getitem__( self, i ):
return self.data[i]
class RecordingDescription(tables.IsDescription):
channels = tables.Int32Col()
date = tables.StringCol(128)
time = tables.StringCol(128)
class ChannelDescription(tables.IsDescription):
n_sections = tables.Int32Col()
class SectionDescription(tables.IsDescription):
dt = tables.Float64Col()
xunits = tables.StringCol(16)
yunits = tables.StringCol(16)
def save_hdf5( rec, filename ):
h5file = tables.openFile(filename, mode = "w", title = "%s, converted to hdf5" % filename)
# write global file description
root_table = h5file.createTable(h5file.root, "description", RecordingDescription, "Description of %s" % filename)
root_row = root_table.row
root_row['channels'] = len(rec)
root_row['date'] = rec.date
root_row['time'] = rec.time
root_row.append()
root_table.flush()
# write comment
comment_group = h5file.createGroup("/", "comment", "multiline file comment")
strarray = h5file.createArray(comment_group, "comment", [rec.comment,], "multiline file comment")
# create group for channel names:
chroot_group = h5file.createGroup("/", "channels", "channel names")
# loop through channels:
for n_c in range(len(rec)):
channel_name = rec[n_c].name
if channel_name == "":
channel_name = "ch%d" % (n_c)
channel_group = h5file.createGroup("/", channel_name, "channel%d" % (n_c))
# write channel name to root group:
strarray = h5file.createArray(chroot_group, "ch%d" % n_c, [channel_name,], "channel name")
channel_table = h5file.createTable(channel_group, "description", ChannelDescription, "Description of %s" % channel_name)
channel_row = channel_table.row
channel_row['n_sections'] = len(rec[n_c])
channel_row.append()
channel_table.flush()
if len(rec[n_c])==1:
max_log10 = 0
else:
max_log10 = int(np.log10(len(rec[n_c])-1))
for n_s in range(len(rec[n_c])):
# construct a number with leading zeros:
if n_s==0:
n10 = 0
else:
n10 = int(np.log10(n_s))
strZero = ""
for n_z in range(n10,max_log10):
strZero += "0"
# construct a section name:
section_name = "sec%d" % (n_s)
# create a child group in the channel:
section_group = h5file.createGroup(channel_group, "section_%s%d" % (strZero, n_s), section_name)
# add data and description:
array = h5file.createArray(section_group, "data", np.array(rec[n_c][n_s].data, dtype=np.float32), "data in %s" % section_name)
desc_table = h5file.createTable(section_group, "description", SectionDescription, "description of %s" % section_name)
desc_row = desc_table.row
desc_row['dt'] = rec[n_c][n_s].dt
desc_row['xunits'] = rec[n_c][n_s].xunits
desc_row['yunits'] = rec[n_c][n_s].yunits
desc_row.append()
desc_table.flush()
h5file.close()
return True
def export_hdf5( filename="" ):
"""
Exports a file in hdf5 format using PyTables.
"""
stf = __import__("stf")
if filename=="":
filename = "%s.h5" % (stf.get_filename())
# loop through channels:
channel_list = list()
for n_c in range(stf.get_size_recording()):
section_list = [
Section( stf.get_trace(n_s, n_c), stf.get_sampling_interval(), stf.get_xunits(n_s, n_c), stf.get_yunits(n_s, n_c) ) \
for n_s in range(stf.get_size_channel(n_c))
]
channel_list.append( Channel( section_list, stf.get_channel_name() ) )
rec = Recording( channel_list, stf.get_recording_comment(), stf.get_recording_date(), stf.get_recording_time() )
save_hdf5( rec, filename )
return True
def import_hdf5( filename ):
"""
Imports a file in hdf5 format stored by stimfit using PyTables, returns a Recording object.
"""
h5file = tables.openFile( filename, mode='r' )
# read global file description
root_node = h5file.getNode("/", "description")
date = root_node.col("date")[0]
time = root_node.col("time")[0]
n_channels = root_node.col("channels")[0]
# read comment
com = h5file.getNode("/", "comment")
comment = ""
for entry in h5file.walkNodes(com,classname='Array'):
comment += "%s" % entry.read()[0]
# read channel names
channel_names = list()
chan = h5file.getNode("/", "channels")
for entry in h5file.walkNodes(chan, classname='Array'):
channel_names.append( "%s" % entry.read()[0] )
# read data from channels:
channel_list = list()
for n_c in range(n_channels):
channel_node = h5file.getNode("/", channel_names[n_c])
chdesc_node = h5file.getNode( channel_node, "description" )
n_sections = chdesc_node.col("n_sections")[0]
# required number of zeros:
if n_sections==1:
max_log10 = 0
else:
max_log10 = int(np.log10(n_sections-1))
# loop through sections:
section_list = list()
for n_s in range(n_sections):
# construct a number with leading zeros:
if n_s==0:
n10 = 0
else:
n10 = int(np.log10(n_s))
strZero = ""
for n_z in range(n10,max_log10):
strZero += "0"
section_name = "section_%s%d" % ( strZero, n_s)
section_node = h5file.getNode( channel_node, section_name )
secdesc_node = h5file.getNode( section_node, "description" )
dt = secdesc_node.col("dt")[0]
xunits = secdesc_node.col("xunits")[0]
yunits = secdesc_node.col("yunits")[0]
data = h5file.getNode( section_node, "data").read()
section_list.append( Section(data, dt, xunits, yunits) )
channel_list.append( Channel(section_list, channel_names[n_c]) )
h5file.close()
return Recording( channel_list, comment, date, time )
def open_hdf5( filename ):
"""
Opens and shows an hdf5 file with stimfit
"""
rec = import_hdf5( filename )
stf = __import__("stf")
stf._gNames_resize( len(rec.channels) )
for n_c in range(len(rec.channels)):
stf._gNames_at( rec.channels[n_c].name, n_c )
stf.new_window_list( rec.get_list() )
n_channels = stf.get_size_recording()
dt = rec.channels[0].sections[0].dt
stf.set_sampling_interval( dt )
stf.set_recording_comment( rec.comment )
stf.set_recording_date( rec.date )
stf.set_recording_time( rec.time )
for n_c in range(stf.get_size_recording()):
for n_s in range(stf.get_size_channel(n_c)):
xunits = rec.channels[n_c].sections[n_s].xunits
yunits = rec.channels[n_c].sections[n_s].yunits
stf.set_xunits( xunits )
stf.set_yunits( yunits, n_s, n_c )
return True
def show_rec( rec ):
"""
Shows recording object in a new stimfit window
"""
stf = __import__("stf")
stf._gNames_resize( len(rec.channels) )
for n_c in range(len(rec.channels)):
stf._gNames_at( rec.channels[n_c].name, n_c )
stf.new_window_list( rec.get_list() )
n_channels = stf.get_size_recording()
dt = rec.channels[0].sections[0].dt
stf.set_sampling_interval( dt )
stf.set_recording_comment( rec.comment )
stf.set_recording_date( rec.date )
stf.set_recording_time( rec.time )
for n_c in range(stf.get_size_recording()):
for n_s in range(stf.get_size_channel(n_c)):
xunits = rec.channels[n_c].sections[n_s].xunits
yunits = rec.channels[n_c].sections[n_s].yunits
stf.set_xunits( xunits )
stf.set_yunits( yunits, n_s, n_c )
return True
def test():
export_hdf5()
open_hdf5("/home/cs/data/EE07_04_11_2AC.dat.h5")
|