-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_dataset_v01.py
78 lines (69 loc) · 2.8 KB
/
prepare_dataset_v01.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 11 14:06:20 2019
@author: drsmith
"""
import pickle
from pathlib import Path
import numpy as np
import pandas as pd
from pims.cine import Cine
shotlist = 'shotlist_v01.csv'
cameradir= Path('/p/nstxcam-archive/Phantom710-9205/2010')
df = pd.read_csv(shotlist)
df = df.set_index('category')
df = df.set_index('shot', append=True)
total_frames = 0
nskip = 20*15
framepool = None
frames = np.empty((20000,64,80), dtype=np.uint16)
iframe = 0
noffset = 0
for cat, catdf in df.groupby(level=0):
catdf = catdf.reset_index(level=0, drop=True)
print('Category {} with {} entries'.format(cat, len(catdf)))
cat_frames = 0
for shot, row in catdf.iterrows():
cine_filename = cameradir / 'nstx_5_{:d}.cin'.format(np.int(shot))
assert(cine_filename.exists())
framesequence = Cine(cine_filename)
assert(framesequence.frame_rate==397660)
# calculate discharges times for each frame
# list of 2-tuples
delta = [(ts[0]-framesequence.trigger_time['datetime'],
ts[1]-framesequence.trigger_time['second_fraction'])
for ts in framesequence.frame_time_stamps]
shottimes = np.array([d[0].seconds + d[0].microseconds/1e6 + d[1]
for d in delta])*1e3
assert(row.start>shottimes.min())
assert(row.stop<shottimes.max())
validtimes = shottimes[(shottimes>row.start) & (shottimes<row.stop)]
validindices, = np.nonzero((shottimes>row.start) & (shottimes<row.stop))
poolindices = validindices[noffset::nskip]
for i in poolindices:
frames[iframe,:,:] = framesequence[i]
iframe += 1
data = {'category':np.array([cat]*poolindices.size, dtype=np.int),
'shot':np.array([shot]*poolindices.size, dtype=np.int),
'iframe':poolindices,
'shottime':shottimes[poolindices],
'tstart':np.array([row.start]*poolindices.size),
'tstop':np.array([row.stop]*poolindices.size),
}
if framepool is None:
framepool = pd.DataFrame(data)
else:
framepool = pd.concat([framepool, pd.DataFrame(data)], ignore_index=True)
print(' {} nframes {} valid fr. {} fr pool {} {}'.
format(shot, framesequence.image_count, validtimes.size,
validtimes.size//nskip, poolindices.size))
cat_frames += validtimes.size
print('Category {} frames: {}'.format(cat, cat_frames))
total_frames += cat_frames
print('Total frames: {}'.format(total_frames))
frames = frames[:iframe,...]
assert(frames.shape[0]==len(framepool))
filename = 'frame_category_small.pickle'
with open(filename, 'wb') as f:
pickle.dump({'frames':frames, 'frameinfo':framepool}, f)