Skip to content

Commit

Permalink
Merge pull request nilearn#971 from AlexandreAbraham/better_csv_to_array
Browse files Browse the repository at this point in the history
Robustify csv_to_array
  • Loading branch information
lesteve committed Feb 2, 2016
2 parents e3a92f0 + fe3ff1f commit 182087b
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions nilearn/_utils/numpy_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,14 @@ def csv_to_array(csv_path, delimiters=' \t,;', **kwargs):
if not isinstance(csv_path, _basestring):
raise TypeError('CSV must be a file path. Got a CSV of type: %s' %
type(csv_path))
# First, we try genfromtxt which works in most cases.
array = np.genfromtxt(csv_path, **kwargs)

if array.ndim <= 1 and np.all(np.isnan(array)):
# If the delimiter is not known genfromtxt generates an array full of
# nan. In that case, we try to guess the delimiter
try:
# First, we try genfromtxt which works in most cases.
array = np.genfromtxt(csv_path, loose=False, **kwargs)
except ValueError:
# There was an error during the conversion to numpy array, probably
# because the delimiter is wrong.
# In that case, we try to guess the delimiter.
try:
with open(csv_path, 'r') as csv_file:
dialect = csv.Sniffer().sniff(csv_file.readline(), delimiters)
Expand Down

0 comments on commit 182087b

Please sign in to comment.