Skip to content

Commit 38fe0c5

Browse files
committed
Merge branch 'master' into fix_plexon1_error
2 parents 900970b + 1804b65 commit 38fe0c5

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

neo/rawio/plexon2rawio/plexon2rawio.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def __init__(self, filename, pl2_dll_file_path=None):
107107
# download default PL2 dll once if not yet available
108108
if pl2_dll_file_path is None:
109109
architecture = platform.architecture()[0]
110-
if architecture == "64bit" and platform.system() == "Windows":
110+
if architecture == "64bit" and platform.system() in ["Windows", "Darwin"]:
111111
file_name = "PL2FileReader64.dll"
112112
else: # Apparently wine uses the 32 bit version in linux
113113
file_name = "PL2FileReader.dll"
@@ -120,16 +120,27 @@ def __init__(self, filename, pl2_dll_file_path=None):
120120
dist = urlopen(url=url)
121121

122122
with open(pl2_dll_file_path, "wb") as f:
123-
print(f"Downloading plexon dll to {pl2_dll_file_path}")
123+
warnings.warn(f"dll file does not exist, downloading plexon dll to {pl2_dll_file_path}")
124124
f.write(dist.read())
125125

126126
# Instantiate wrapper for Windows DLL
127127
from neo.rawio.plexon2rawio.pypl2.pypl2lib import PyPL2FileReader
128128

129129
self.pl2reader = PyPL2FileReader(pl2_dll_file_path=pl2_dll_file_path)
130130

131-
# Open the file.
132-
self.pl2reader.pl2_open_file(self.filename)
131+
132+
reading_attempts = 10
133+
for attempt in range(reading_attempts):
134+
self.pl2reader.pl2_open_file(self.filename)
135+
136+
# Verify the file handle is valid.
137+
if self.pl2reader._file_handle.value != 0:
138+
# File handle is valid, exit the loop early
139+
break
140+
else:
141+
if attempt == reading_attempts - 1:
142+
self.pl2reader._print_error()
143+
raise IOError(f"Opening {self.filename} failed after {reading_attempts} attempts.")
133144

134145
def _source_name(self):
135146
return self.filename
@@ -146,7 +157,6 @@ def _parse_header(self):
146157
Source = namedtuple("Source", "id name sampling_rate n_samples")
147158
for c in range(self.pl2reader.pl2_file_info.m_TotalNumberOfAnalogChannels):
148159
achannel_info = self.pl2reader.pl2_get_analog_channel_info(c)
149-
150160
# only consider active channels
151161
if not achannel_info.m_ChannelEnabled:
152162
continue
@@ -262,8 +272,10 @@ def _parse_header(self):
262272
# python is 1..12 https://docs.python.org/3/library/datetime.html#datetime.datetime
263273
# so month needs to be tm_mon+1; also tm_sec could cause problems in the case of leap
264274
# seconds, but this is harder to defend against.
275+
year = tmo.tm_year # This has base 1900 in the c++ struct specification so we need to add 1900
276+
year += 1900
265277
dt = datetime(
266-
year=tmo.tm_year,
278+
year=year,
267279
month=tmo.tm_mon + 1,
268280
day=tmo.tm_mday,
269281
hour=tmo.tm_hour,
@@ -317,7 +329,6 @@ def _parse_header(self):
317329
"m_OneBasedChannelInTrode",
318330
"m_Source",
319331
"m_Channel",
320-
"m_Name",
321332
"m_MaximumNumberOfFragments",
322333
]
323334

neo/rawio/plexon2rawio/pypl2/pypl2lib.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
raise ImportError("Wine is not installed. Please install wine to use the PL2FileReader.dll")
2525

2626
from zugbruecke import CtypesSession
27-
28-
ctypes = CtypesSession(log_level=100)
27+
if platform.system() == "Darwin":
28+
ctypes = CtypesSession(log_level=100, arch = 'win64')
29+
else:
30+
ctypes = CtypesSession(log_level=100)
2931

3032

3133
class tm(ctypes.Structure):
@@ -610,7 +612,7 @@ def pl2_get_spike_channel_info_by_name(self, channel_name):
610612

611613
self.pl2_dll.PL2_GetSpikeChannelInfoByName.argtypes = (
612614
ctypes.c_int,
613-
ctypes.c_char * len(channel_name),
615+
ctypes.POINTER(ctypes.c_char),
614616
ctypes.POINTER(PL2SpikeChannelInfo),
615617
)
616618

@@ -629,11 +631,9 @@ def pl2_get_spike_channel_info_by_name(self, channel_name):
629631
result = self.pl2_dll.PL2_GetSpikeChannelInfoByName(
630632
self._file_handle, channel_name, ctypes.byref(pl2_spike_channel_info)
631633
)
632-
633634
if not result:
634635
self._print_error()
635636
return None
636-
637637
return pl2_spike_channel_info
638638

639639
def pl2_get_spike_channel_info_by_source(self, source_id, one_based_channel_index_in_source):
@@ -744,7 +744,7 @@ def pl2_get_spike_channel_data_by_name(self, channel_name):
744744

745745
self.pl2_dll.PL2_GetSpikeChannelDataByName.argtypes = (
746746
ctypes.c_int,
747-
ctypes.c_char,
747+
ctypes.POINTER(ctypes.c_char),
748748
ctypes.POINTER(ctypes.c_ulonglong),
749749
ctypes.POINTER(ctypes.c_ulonglong),
750750
ctypes.POINTER(ctypes.c_ushort),

0 commit comments

Comments
 (0)