Iam working using JSON files, the structure of the file is :
{“data”: [{
“ID”:…,
“SensorName”:… ,
“DigitalValue”: …,
“AnalogValue”:… ,
“SchematicName”:…,
“Pin”:…,
“Enable”:…,
“TimeStamp”:…,
}]}
each file is made of many structure like the one above I was wondering how to write a custumized dataset for pythorch using this I get errors:
def __len__(self):
return ()
def __getitem__(self, idx):
print('get item hello')
input()
data = []
for k, v in idx['data'][0].items():
print('v is ',v)
print()
print('k is ',k)
input()
if type(v) == str:
# convert it into int or float
v=int(v)
data.append(v)
# and append into data
else:
data.append(v)
torch_data = torch.Tensor(data)
label=Y
input()
Depending JSON file structure and which level you would like to iterate through, it will be different.
If the JSON file is a list of entries, and each entry is a dict like this {“serial_number”:"…",“timestamp”:…,“data”:[…]}, then:
dictionaries = json.load(f)
for row in dictionaries:
data = row['data']
If the JSON file is dict with keys such as “serial_number”, “timestamp”, “data” (i.e. {“serial_number”:"…",“timestamp”:…,“data”:[…]}) , then:
d = json.load(f)
serial_number = d['serial_number']
timestamp = d['timestamp']
data = d['data']
for point in data:
# You can iterate through the data here, or you can just convert it to a Tensor