audrey06100 commited on
Commit
d71917f
·
1 Parent(s): 3ab4789

update utils

Browse files
Files changed (1) hide show
  1. utils.py +21 -16
utils.py CHANGED
@@ -1,10 +1,12 @@
1
  import numpy as np
2
  import csv
3
- from model import cumbersome_model2
4
- from model import UNet_family
5
- from model import UNet_attention
6
  from model import tf_model
7
  from model import tf_data
 
 
8
 
9
  import time
10
  import torch
@@ -13,6 +15,8 @@ import random
13
  import shutil
14
  from scipy.signal import decimate, resample_poly, firwin, lfilter
15
 
 
 
16
 
17
  os.environ["CUDA_VISIBLE_DEVICES"]="0"
18
 
@@ -65,12 +69,12 @@ def read_train_data(file_name):
65
  return data
66
 
67
 
68
- def cut_data(raw_data):
69
  raw_data = np.array(raw_data).astype(np.float64)
70
  total = int(len(raw_data[0]) / 1024)
71
  for i in range(total):
72
  table = raw_data[:, i * 1024:(i + 1) * 1024]
73
- filename = './temp2/' + str(i) + '.csv'
74
  with open(filename, 'w', newline='') as csvfile:
75
  writer = csv.writer(csvfile)
76
  writer.writerows(table)
@@ -114,8 +118,8 @@ def dataDelete(path):
114
  except OSError as e:
115
  print(e)
116
  else:
117
- pass
118
- #print("The directory is deleted successfully")
119
 
120
 
121
  def decode_data(data, std_num, mode=5):
@@ -181,11 +185,12 @@ def decode_data(data, std_num, mode=5):
181
 
182
  def preprocessing(filename, samplerate):
183
  # establish temp folder
 
184
  try:
185
- os.mkdir("./temp2/")
186
  except OSError as e:
187
- dataDelete("./temp2/")
188
- os.mkdir("./temp2/")
189
  print(e)
190
 
191
  # read data
@@ -198,17 +203,17 @@ def preprocessing(filename, samplerate):
198
  signal = FIR_filter(signal, 1, 50)
199
  #print(signal.shape)
200
  # cutting data
201
- total_file_num = cut_data(signal)
202
 
203
  return total_file_num
204
 
205
 
206
  # model = tf.keras.models.load_model('./denoise_model/')
207
- def reconstruct(model_name, total, outputfile):
208
  # -------------------decode_data---------------------------
209
  second1 = time.time()
210
  for i in range(total):
211
- file_name = './temp2/{}.csv'.format(str(i))
212
  data_noise = read_train_data(file_name)
213
 
214
  std = np.std(data_noise)
@@ -220,13 +225,13 @@ def reconstruct(model_name, total, outputfile):
220
  d_data = decode_data(data_noise, std, model_name)
221
  d_data = d_data[0]
222
 
223
- outputname = "./temp2/output{}.csv".format(str(i))
224
  save_data(d_data, outputname)
225
 
226
  # --------------------glue_data----------------------------
227
- glue_data("./temp2/", total, outputfile)
228
  # -------------------delete_data---------------------------
229
- dataDelete("./temp2/")
230
  second2 = time.time()
231
 
232
  print("Using", model_name,"model to reconstruct", outputfile, " has been success in", second2 - second1, "sec(s)")
 
1
  import numpy as np
2
  import csv
3
+ #from model import cumbersome_model2
4
+ #from model import UNet_family
5
+ #from model import UNet_attention
6
  from model import tf_model
7
  from model import tf_data
8
+ #from opts import get_opts
9
+ #from tools import pick_models
10
 
11
  import time
12
  import torch
 
15
  import shutil
16
  from scipy.signal import decimate, resample_poly, firwin, lfilter
17
 
18
+ from pathlib import Path
19
+
20
 
21
  os.environ["CUDA_VISIBLE_DEVICES"]="0"
22
 
 
69
  return data
70
 
71
 
72
+ def cut_data(filepath, raw_data):
73
  raw_data = np.array(raw_data).astype(np.float64)
74
  total = int(len(raw_data[0]) / 1024)
75
  for i in range(total):
76
  table = raw_data[:, i * 1024:(i + 1) * 1024]
77
+ filename = filepath + '/temp2/' + str(i) + '.csv'
78
  with open(filename, 'w', newline='') as csvfile:
79
  writer = csv.writer(csvfile)
80
  writer.writerows(table)
 
118
  except OSError as e:
119
  print(e)
120
  else:
121
+ #pass
122
+ print("The directory is deleted successfully")
123
 
124
 
125
  def decode_data(data, std_num, mode=5):
 
185
 
186
  def preprocessing(filename, samplerate):
187
  # establish temp folder
188
+ tmp_filepath = str(Path(str(filename)).parent)
189
  try:
190
+ os.mkdir(tmp_filepath+"/temp2/")
191
  except OSError as e:
192
+ dataDelete(tmp_filepath+"/temp2/")
193
+ os.mkdir(tmp_filepath+"/temp2/")
194
  print(e)
195
 
196
  # read data
 
203
  signal = FIR_filter(signal, 1, 50)
204
  #print(signal.shape)
205
  # cutting data
206
+ total_file_num = cut_data(tmp_filepath, signal)
207
 
208
  return total_file_num
209
 
210
 
211
  # model = tf.keras.models.load_model('./denoise_model/')
212
+ def reconstruct(model_name, total, filepath, outputfile):
213
  # -------------------decode_data---------------------------
214
  second1 = time.time()
215
  for i in range(total):
216
+ file_name = filepath + '/temp2/{}.csv'.format(str(i))
217
  data_noise = read_train_data(file_name)
218
 
219
  std = np.std(data_noise)
 
225
  d_data = decode_data(data_noise, std, model_name)
226
  d_data = d_data[0]
227
 
228
+ outputname = filepath + '/temp2/output{}.csv'.format(str(i))
229
  save_data(d_data, outputname)
230
 
231
  # --------------------glue_data----------------------------
232
+ glue_data(filepath+"/temp2/", total, filepath+'/'+outputfile)
233
  # -------------------delete_data---------------------------
234
+ dataDelete(filepath+"/temp2/")
235
  second2 = time.time()
236
 
237
  print("Using", model_name,"model to reconstruct", outputfile, " has been success in", second2 - second1, "sec(s)")