Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ __pycache__
.DS_Store
*.egg-info/
.idea
test*
.env*
26 changes: 16 additions & 10 deletions brpylib/brMiscFxns.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@
pass


def openfilecheck(open_mode, file_name="", file_ext="", file_type=""):
def openfilecheck(open_mode, file_name="", file_ext="", file_type="", verbose=True, interactive=True):
"""
:param open_mode: {str} method to open the file (e.g., 'rb' for binary read only)
:param file_name: [optional] {str} full path of file to open
:param file_ext: [optional] {str} file extension (e.g., '.nev')
:param file_type: [optional] {str} file type for use when browsing for file (e.g., 'Blackrock NEV Files')
:param verbose: [optional] {bool} whether or not to print status information
:param interactive: [optional] {bool} will only try to prompt user for a file if True, otherwise allow failure
:return: {file} opened file
"""

while True:
if not file_name: # no file name passed
if interactive and not file_name: # no file name passed
if not HAS_QT:
raise ModuleNotFoundError(
"Qt required for file dialog. Install PySide + qtpy or provide file_name."
Expand Down Expand Up @@ -75,19 +77,23 @@ def openfilecheck(open_mode, file_name="", file_ext="", file_type=""):
test_extension = file_ext

if fext[0 : len(test_extension)] != test_extension:
file_name = ""
print(
"\n*** File given is not a "
+ file_ext
+ " file, try again ***\n"
)
continue
if interactive:
file_name = ""
print(
"\n*** File given is not a "
+ file_ext
+ " file, try again ***\n"
)
continue
else:
break
break
else:
file_name = ""
print("\n*** File given does exist, try again ***\n")
if verbose:
print("\n" + file_name.split("/")[-1] + " opened")

print("\n" + file_name.split("/")[-1] + " opened")
return open(file_name, open_mode)


Expand Down
34 changes: 18 additions & 16 deletions brpylib/brpylib.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ class NevFile:
basic header information.
"""

def __init__(self, datafile=""):
def __init__(self, datafile="", verbose=True, interactive=True):
self.datafile = datafile
self.basic_header = {}
self.extended_headers = []
Expand All @@ -523,6 +523,8 @@ def __init__(self, datafile=""):
file_name=self.datafile,
file_ext=".nev",
file_type="Blackrock NEV Files",
verbose=verbose,
interactive=interactive
)

# extract basic header information
Expand Down Expand Up @@ -732,16 +734,17 @@ def getdata(self, elec_ids="all", wave_read="read"):

# extract only the "true" comments, distinct from ROI packets
trueComments = np.setdiff1d(
list(range(0, len(commentPackets) - 1)), ROIPackets
)
list(range(0, len(commentPackets))), ROIPackets
).astype(int)
trueCommentsidx = np.asarray(commentPackets)[trueComments]
textComments = comments[trueCommentsidx]
textComments[:, -1] = "$"
stringarray = textComments.tostring()
stringvector = stringarray.decode("latin-1")
stringvector = stringvector[0:-1]
validstrings = stringvector.replace("\x00", "")
commentsFinal = validstrings.split("$")
commentsFinal = []
for text in textComments:
stringarray = text.astype(str).tobytes()
stringvector = stringarray.decode("latin-1")
stringvector = stringvector[0:-1]
validstring = stringvector.replace("\x00", "")
commentsFinal.append(validstring)

# Remove the ROI comments from the list
subsetInds = list(
Expand All @@ -761,7 +764,7 @@ def getdata(self, elec_ids="all", wave_read="read"):
nmCommentsidx = np.asarray(commentPackets)[ROIPackets]
nmcomments = comments[nmCommentsidx]
nmcomments[:, -1] = ":"
nmstringarray = nmcomments.tostring()
nmstringarray = nmcomments.astype(str).tobytes()
nmstringvector = nmstringarray.decode("latin-1")
nmstringvector = nmstringvector[0:-1]
nmvalidstrings = nmstringvector.replace("\x00", "")
Expand Down Expand Up @@ -1020,7 +1023,7 @@ class NsxFile:
basic header information.
"""

def __init__(self, datafile=""):
def __init__(self, datafile="", interactive=True, verbose=True):

self.datafile = datafile
self.basic_header = {}
Expand All @@ -1032,6 +1035,8 @@ def __init__(self, datafile=""):
file_name=self.datafile,
file_ext=".ns*",
file_type="Blackrock NSx Files",
interactive=interactive,
verbose=verbose
)

# Determine File ID to determine if File Spec 2.1
Expand Down Expand Up @@ -1219,11 +1224,8 @@ def getdata(
# memmap moves the file pointer inconsistently depending on platform and numpy version
curr_loc = self.datafile.tell()
expected_loc = bod + num_data_pts * data_pt_size
if curr_loc == bod:
# It did not move the pointer at all. Move it manually.
self.datafile.seek(expected_loc - bod, 1)
elif curr_loc > expected_loc:
# Moved it too far (probably to end of file); move manually from beginning to expected.
if curr_loc != expected_loc:
# memmap moved the cursor to an unexpected location, update it manually
self.datafile.seek(expected_loc, 0)
else:
# 1 sample per packet. Reuse struct_arr.
Expand Down
1 change: 1 addition & 0 deletions build/lib/brpylib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .brpylib import NevFile, NsxFile, brpylib_ver
106 changes: 106 additions & 0 deletions build/lib/brpylib/brMiscFxns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"""
Random functions that may be useful elsewhere (or necessary)
current version: 1.2.0 --- 08/04/2016

@author: Mitch Frankel - Blackrock Microsystems

Version History:
v1.0.0 - 07/05/2016 - initial release
v1.1.0 - 07/12/2016 - minor editing changes to print statements and addition of version control
v1.2.0 - 08/04/2016 - minor modifications to allow use of Python 2.6+
"""
from os import getcwd, path

try:
from qtpy.QtWidgets import QApplication, QFileDialog

HAS_QT = True
except ModuleNotFoundError:
HAS_QT = False

# Version control
brmiscfxns_ver = "1.2.0"

# Patch for use with Python 2.6+
try:
input = raw_input
except NameError:
pass


def openfilecheck(open_mode, file_name="", file_ext="", file_type="", verbose=True, interactive=True):
"""
:param open_mode: {str} method to open the file (e.g., 'rb' for binary read only)
:param file_name: [optional] {str} full path of file to open
:param file_ext: [optional] {str} file extension (e.g., '.nev')
:param file_type: [optional] {str} file type for use when browsing for file (e.g., 'Blackrock NEV Files')
:param verbose: [optional] {bool} whether or not to print status information
:param interactive: [optional] {bool} will only try to prompt user for a file if True, otherwise allow failure
:return: {file} opened file
"""

while True:
if interactive and not file_name: # no file name passed
if not HAS_QT:
raise ModuleNotFoundError(
"Qt required for file dialog. Install PySide + qtpy or provide file_name."
)

# Ask user to specify a file path or browse
file_name = input(
"Enter complete " + file_ext + " file path or hit enter to browse: "
)

if not file_name:
if "app" not in locals():
app = QApplication([])
if not file_ext:
file_type = "All Files"
file_name = QFileDialog.getOpenFileName(
QFileDialog(),
"Select File",
getcwd(),
file_type + " (*" + file_ext + ")",
)
file_name = file_name[0]

# Ensure file exists (really needed for users type entering)
if path.isfile(file_name):
# Ensure given file matches file_ext
if file_ext:
_, fext = path.splitext(file_name)

# check for * in extension
if file_ext[-1] == "*":
test_extension = file_ext[:-1]
else:
test_extension = file_ext

if fext[0 : len(test_extension)] != test_extension:
if interactive:
file_name = ""
print(
"\n*** File given is not a "
+ file_ext
+ " file, try again ***\n"
)
continue
else:
break
break
else:
file_name = ""
print("\n*** File given does exist, try again ***\n")
if verbose:
print("\n" + file_name.split("/")[-1] + " opened")

return open(file_name, open_mode)


def checkequal(iterator):
try:
iterator = iter(iterator)
first = next(iterator)
return all(first == rest for rest in iterator)
except StopIteration:
return True
Loading