Skip to content

Commit

Permalink
Merge pull request #1494 from h-mayorquin/plexon_for_apple_silicon
Browse files Browse the repository at this point in the history
Plexon2 support for apple 64 bits
  • Loading branch information
zm711 authored Jun 28, 2024
2 parents a8f0e35 + eac1624 commit 1804b65
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
30 changes: 18 additions & 12 deletions neo/rawio/plexon2rawio/plexon2rawio.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def __init__(self, filename, pl2_dll_file_path=None):
# download default PL2 dll once if not yet available
if pl2_dll_file_path is None:
architecture = platform.architecture()[0]
if architecture == "64bit" and platform.system() == "Windows":
if architecture == "64bit" and platform.system() in ["Windows", "Darwin"]:
file_name = "PL2FileReader64.dll"
else: # Apparently wine uses the 32 bit version in linux
file_name = "PL2FileReader.dll"
Expand All @@ -120,21 +120,27 @@ def __init__(self, filename, pl2_dll_file_path=None):
dist = urlopen(url=url)

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

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

self.pl2reader = PyPL2FileReader(pl2_dll_file_path=pl2_dll_file_path)

# Open the file.
self.pl2reader.pl2_open_file(self.filename)

# Verify the file handle is valid.
if self.pl2reader._file_handle.value == 0:
self.pl2reader._print_error()
raise Exception(f"Opening {self.filename} failed.")

reading_attempts = 10
for attempt in range(reading_attempts):
self.pl2reader.pl2_open_file(self.filename)

# Verify the file handle is valid.
if self.pl2reader._file_handle.value != 0:
# File handle is valid, exit the loop early
break
else:
if attempt == reading_attempts - 1:
self.pl2reader._print_error()
raise IOError(f"Opening {self.filename} failed after {reading_attempts} attempts.")

def _source_name(self):
return self.filename
Expand All @@ -151,7 +157,6 @@ def _parse_header(self):
Source = namedtuple("Source", "id name sampling_rate n_samples")
for c in range(self.pl2reader.pl2_file_info.m_TotalNumberOfAnalogChannels):
achannel_info = self.pl2reader.pl2_get_analog_channel_info(c)

# only consider active channels
if not achannel_info.m_ChannelEnabled:
continue
Expand Down Expand Up @@ -267,8 +272,10 @@ def _parse_header(self):
# python is 1..12 https://docs.python.org/3/library/datetime.html#datetime.datetime
# so month needs to be tm_mon+1; also tm_sec could cause problems in the case of leap
# seconds, but this is harder to defend against.
year = tmo.tm_year # This has base 1900 in the c++ struct specification so we need to add 1900
year += 1900
dt = datetime(
year=tmo.tm_year,
year=year,
month=tmo.tm_mon + 1,
day=tmo.tm_mday,
hour=tmo.tm_hour,
Expand Down Expand Up @@ -322,7 +329,6 @@ def _parse_header(self):
"m_OneBasedChannelInTrode",
"m_Source",
"m_Channel",
"m_Name",
"m_MaximumNumberOfFragments",
]

Expand Down
8 changes: 4 additions & 4 deletions neo/rawio/plexon2rawio/pypl2/pypl2lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
raise ImportError("Wine is not installed. Please install wine to use the PL2FileReader.dll")

from zugbruecke import CtypesSession

ctypes = CtypesSession(log_level=100)
if platform.system() == "Darwin":
ctypes = CtypesSession(log_level=100, arch = 'win64')
else:
ctypes = CtypesSession(log_level=100)


class tm(ctypes.Structure):
Expand Down Expand Up @@ -629,11 +631,9 @@ def pl2_get_spike_channel_info_by_name(self, channel_name):
result = self.pl2_dll.PL2_GetSpikeChannelInfoByName(
self._file_handle, channel_name, ctypes.byref(pl2_spike_channel_info)
)

if not result:
self._print_error()
return None

return pl2_spike_channel_info

def pl2_get_spike_channel_info_by_source(self, source_id, one_based_channel_index_in_source):
Expand Down

0 comments on commit 1804b65

Please sign in to comment.