-
Notifications
You must be signed in to change notification settings - Fork 1
/
unify_label_func.py
106 lines (83 loc) · 3.41 KB
/
unify_label_func.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import os
import pdb
import glob as glob
import cPickle as pickle
import numpy as np
from scipy.io import loadmat,savemat
def unify_newLabel_to_existing(matfiles, LabelName, IDName):
flab = [] #final labels
ftrjID = [] #final trjID
for matidx in range(len(matfiles)):
L1 = loadmat(matfiles[matidx])[LabelName][0]
# L1 = L1+1 # class label starts from 1 instead of 0
M1 = loadmat(matfiles[matidx])[IDName][0]
if len(flab)>0:
Labelnowmax = max(flab)
L1 = L1+Labelnowmax+1
commonidx = np.intersect1d(M1,ftrjID) #trajectories existing in both 2 trucations
print('flab : {0}, new labels : {1} ,common term : {2}').format(len(np.unique(flab)),len(np.unique(L1)),len(commonidx))
for i in commonidx:
labelnew = L1[M1==i][0]
labelnow = np.array(flab)[ftrjID==i][0]
idx1 = np.where(L1==labelnew)[0]
L1[idx1] = labelnow ## keep the first appearing label
flab[:] = flab +list(L1)
ftrjID[:] = ftrjID + list(M1)
ftrjID, indices= np.unique(ftrjID,return_index=True)
flab = np.array(flab)[indices]
return flab, ftrjID
def unify_label(matfiles,savename,label_choice):
DirName = ['upup','updown','downup','downdown']
for dirii in range(len(DirName)):
LabelName = label_choice+DirName[dirii]
IDName = 'trjID_'+DirName[dirii]
if useCC:
LabelName = 'c_'+DirName[dirii]
IDName = 'trjID_'+DirName[dirii]
try:
M1 = loadmat(matfiles[0])[IDName][0]
except: ## if no this direction, key error, continue
continue
flab, ftrjID = unify_newLabel_to_existing(matfiles, LabelName, IDName)
result = {}
result['label'] = flab
result['trjID'] = ftrjID
savename = os.path.join(DataPathobj.unifiedLabelpath,savename+label_choice)
savemat(savename,result)
# if __name__ == '__main__':
def unify_main(dataSource,VideoIndex):
import DataPathclass
global DataPathobj
DataPathobj = DataPathclass.DataPath(dataSource,VideoIndex)
import parameterClass
global Parameterobj
Parameterobj = parameterClass.parameter(dataSource,VideoIndex)
global useCC
useCC = False
if Parameterobj.useWarpped:
filePrefix = 'usewarpped'
else:
# filePrefix = 'Aug12'
# filePrefix = 'Aug10'
filePrefix = 'Aug15'
label_choice = Parameterobj.clustering_choice
if useCC:
matfilesAll = sorted(glob.glob(DataPathobj.adjpath +filePrefix+'*.mat'))
else:
matfilesAll = sorted(glob.glob(DataPathobj.sscpath +filePrefix+'*.mat'))
numTrunc = len(matfilesAll)
savename = ''
if numTrunc<=200:
if useCC:
savename = 'concomp'+savename
else:
savename = 'Complete_result'+savename
if Parameterobj.useWarpped:
savename = 'usewarpped_'+savename
unify_label(matfilesAll,savename,label_choice)
else:
for kk in range(0,numTrunc,25):
print "saved trunk",str(kk+1).zfill(3),'to' ,str(min(kk+25,numTrunc)).zfill(3)
matfiles = matfilesAll[kk:min(kk+25,numTrunc)]
savename = os.path.join(DataPathobj.unifiedLabelpath,'result_'+label_choice+str(kk+1).zfill(3)+'-'+str(min(kk+25,numTrunc)).zfill(3))
unify_label(matfiles,savename,label_choice)