Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug of "UnboundLocalError: cannot access local variable 'dist_mat' where it is not associated with a value" #4

Open
IfReasonable opened this issue Dec 25, 2024 · 0 comments

Comments

@IfReasonable
Copy link

Traceback (most recent call last):
File "/home/chenhaolong/netperf/codes/softclt/softclt_catcc/main_semi_classification.py", line 203, in
sim_mat = load_sim_matrix(args.selected_dataset)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/chenhaolong/netperf/codes/softclt/softclt_catcc/dataloader/dataloader.py", line 296, in load_sim_matrix
sim_mat = save_sim_mat(normalize_TS(train), min_ = 0, max_ = 1)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/chenhaolong/netperf/codes/softclt/softclt_catcc/dataloader/dataloader.py", line 89, in save_sim_mat
N = dist_mat.shape[0]
^^^^^^^^
UnboundLocalError: cannot access local variable 'dist_mat' where it is not associated with a value


I think this is caused by the code in ./softclt_catcc/dataloader/dataloader.py :

def save_sim_mat(X_tr, min_ = 0, max_ = 1, multivariate=False, type_='DTW'):
N = dist_mat.shape[0] # **!!! this should be put after the definition of dist_mat **
if multivariate:
assert type=='DTW'
dist_mat = get_MDTW(X_tr)
else:
if type_=='DTW':
dist_mat = get_DTW(X_tr)
elif type_=='TAM':
dist_mat = get_TAM(X_tr)
elif type_=='COS':
dist_mat = get_COS(X_tr)
elif type_=='EUC':
dist_mat = get_EUC(X_tr)
elif type_=='GAK':
dist_mat = get_GAK(X_tr)

# (1) distance matrix
diag_indices = np.diag_indices(N)
mask = np.ones(dist_mat.shape, dtype=bool)
mask[diag_indices] = False
temp = dist_mat[mask].reshape(N, N-1)
dist_mat[diag_indices] = temp.min()

# (2) normalized distance matrix
scaler = MinMaxScaler(feature_range=(min_, max_))
dist_mat = scaler.fit_transform(dist_mat)

# (3) normalized similarity matrix
return 1 - dist_mat 

After debugging:

def save_sim_mat(X_tr, min_ = 0, max_ = 1, multivariate=False, type_='DTW'):
if multivariate:
assert type=='DTW'
dist_mat = get_MDTW(X_tr)
else:
if type_=='DTW':
dist_mat = get_DTW(X_tr)
elif type_=='TAM':
dist_mat = get_TAM(X_tr)
elif type_=='COS':
dist_mat = get_COS(X_tr)
elif type_=='EUC':
dist_mat = get_EUC(X_tr)
elif type_=='GAK':
dist_mat = get_GAK(X_tr)
N = dist_mat.shape[0] # **!!! this should be put after the definition of dist_mat **

# (1) distance matrix
diag_indices = np.diag_indices(N)
mask = np.ones(dist_mat.shape, dtype=bool)
mask[diag_indices] = False
temp = dist_mat[mask].reshape(N, N-1)
dist_mat[diag_indices] = temp.min()

# (2) normalized distance matrix
scaler = MinMaxScaler(feature_range=(min_, max_))
dist_mat = scaler.fit_transform(dist_mat)

# (3) normalized similarity matrix
return 1 - dist_mat 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant