Skip to content

Commit 596cb3a

Browse files
committed
Try-except for better exceptions
1 parent 12773f2 commit 596cb3a

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

datastream/dataset.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Tuple, Callable, Any, Union, List, TypeVar, Generic, Dict, Optional
55
)
66
from pathlib import Path
7+
import inspect
78
import numpy as np
89
import pandas as pd
910
import torch
@@ -125,12 +126,30 @@ def map(
125126
... )[-1]
126127
4
127128
'''
129+
130+
def composed_fn(dataframe, index):
131+
item = self.get_item(dataframe, index)
132+
try:
133+
return function(item)
134+
except Exception as e:
135+
item_text = str(item)
136+
if len(item_text) > 79:
137+
item_text = item_text[:79] + ' ...'
138+
139+
raise Exception('\n'.join([
140+
repr(e),
141+
'Above exception occured in the pipeline for',
142+
'item:',
143+
item_text,
144+
f'file: {inspect.getfile(function)}',
145+
'source:',
146+
inspect.getsource(function),
147+
])) from e
148+
128149
return Dataset(
129150
dataframe=self.dataframe,
130151
length=self.length,
131-
get_item=lambda dataframe, index: function(
132-
self.get_item(dataframe, index)
133-
),
152+
get_item=composed_fn,
134153
)
135154

136155
def starmap(

0 commit comments

Comments
 (0)