Options handling improvements.
Browse files- app.py +13 -49
- requirements.txt +1 -1
app.py
CHANGED
@@ -145,7 +145,7 @@ def predict(numberOfECGs: int = 1,
|
|
145 |
deepfakeecg.generateDeepfakeECGs(numberOfECGs,
|
146 |
ecgType = ecgType,
|
147 |
ecgLengthInSeconds = ecgLengthInSeconds,
|
148 |
-
ecgScaleFactor =
|
149 |
outputFormat = deepfakeecg.OUTPUT_TENSOR,
|
150 |
showProgress = False,
|
151 |
runOnDevice = runOnDevice)
|
@@ -157,7 +157,10 @@ def predict(numberOfECGs: int = 1,
|
|
157 |
for result in Sessions[request.session_hash].Results:
|
158 |
|
159 |
# ====== Plot ECG =====================================================
|
160 |
-
|
|
|
|
|
|
|
161 |
# print(result)
|
162 |
|
163 |
# ------ ECG-12 -------------------------------------------------------
|
@@ -199,51 +202,6 @@ def select(event: gradio.SelectData,
|
|
199 |
log(f'Session "{request.session_hash}": Selected ECG #{Sessions[request.session_hash].Selected + 1}')
|
200 |
|
201 |
|
202 |
-
# ###### Produce ECG CSV file from Tensor ###################################
|
203 |
-
def dataToCSV(ecgResult, ecgType, outputFileName):
|
204 |
-
|
205 |
-
data = ecgResult.detach().cpu().numpy()
|
206 |
-
|
207 |
-
if ecgType == deepfakeecg.DATA_ECG8:
|
208 |
-
header = 'Timestamp,LeadI,LeadII,V1,V2,V3,V4,V5,V6'
|
209 |
-
elif ecgType == deepfakeecg.DATA_ECG12:
|
210 |
-
header = 'Timestamp,LeadI,LeadII,V1,V2,V3,V4,V5,V6,LeadIII,aVL,aVR,aVF'
|
211 |
-
else:
|
212 |
-
raise Exception('Invalid ECG type!')
|
213 |
-
|
214 |
-
numpy.savetxt(outputFileName, data,
|
215 |
-
header = header,
|
216 |
-
comments = '',
|
217 |
-
delimiter = ',',
|
218 |
-
fmt = '%i')
|
219 |
-
|
220 |
-
|
221 |
-
# ###### Produce ECG PDF file from Tensor ###################################
|
222 |
-
def dataToPDF(ecgResult, ecgType, outputFileName):
|
223 |
-
|
224 |
-
data = ecgResult.detach().cpu().numpy()
|
225 |
-
outputLeads = deepfakeecg.ECG_LEADS
|
226 |
-
|
227 |
-
matplotlib.pyplot.figure(figsize=(15, 3))
|
228 |
-
for outputLead in outputLeads:
|
229 |
-
try:
|
230 |
-
outputLeadIndex = deepfakeecg.ECG_LEADS[outputLead][0]
|
231 |
-
outputLeadLabel = deepfakeecg.ECG_LEADS[outputLead][1]
|
232 |
-
outputLeadType = deepfakeecg.ECG_LEADS[outputLead][2]
|
233 |
-
except:
|
234 |
-
raise Exception('Invalid lead ' + outputLead + '!')
|
235 |
-
if outputLeadType > ecgType:
|
236 |
-
raise Exception('Invalid lead ' + outputLead + ' for this ECG type!')
|
237 |
-
matplotlib.pyplot.plot(data[:, outputLeadIndex], label = outputLeadLabel)
|
238 |
-
matplotlib.pyplot.legend()
|
239 |
-
matplotlib.pyplot.title('Generated ECG — ID ' + str(i))
|
240 |
-
matplotlib.pyplot.xlabel('Time [s]')
|
241 |
-
matplotlib.pyplot.ylabel('Amplitude [μV]')
|
242 |
-
matplotlib.pyplot.grid(True)
|
243 |
-
matplotlib.pyplot.ylim(-1000, +1000)
|
244 |
-
matplotlib.pyplot.savefig(outputFileName)
|
245 |
-
|
246 |
-
|
247 |
# ###### Download CSV #######################################################
|
248 |
def downloadCSV(request: gradio.Request) -> pathlib.Path:
|
249 |
|
@@ -251,7 +209,7 @@ def downloadCSV(request: gradio.Request) -> pathlib.Path:
|
|
251 |
ecgType = Sessions[request.session_hash].Type
|
252 |
fileName = pathlib.Path(Sessions[request.session_hash].TempDirectory.name) / \
|
253 |
('ECG-' + str(Sessions[request.session_hash].Selected + 1) + '.csv')
|
254 |
-
dataToCSV(ecgResult, ecgType, fileName)
|
255 |
|
256 |
log(f'Session "{request.session_hash}": Download CSV file {fileName}')
|
257 |
return fileName
|
@@ -265,7 +223,13 @@ def downloadPDF(request: gradio.Request):
|
|
265 |
ecgType = Sessions[request.session_hash].Type
|
266 |
fileName = pathlib.Path(Sessions[request.session_hash].TempDirectory.name) / \
|
267 |
('ECG-' + str(Sessions[request.session_hash].Selected + 1) + '.pdf')
|
268 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
269 |
|
270 |
log(f'Session "{request.session_hash}": Download PDF file {fileName}')
|
271 |
return fileName
|
|
|
145 |
deepfakeecg.generateDeepfakeECGs(numberOfECGs,
|
146 |
ecgType = ecgType,
|
147 |
ecgLengthInSeconds = ecgLengthInSeconds,
|
148 |
+
ecgScaleFactor = deepfakeecg.ECG_DEFAULT_SCALE_FACTOR,
|
149 |
outputFormat = deepfakeecg.OUTPUT_TENSOR,
|
150 |
showProgress = False,
|
151 |
runOnDevice = runOnDevice)
|
|
|
157 |
for result in Sessions[request.session_hash].Results:
|
158 |
|
159 |
# ====== Plot ECG =====================================================
|
160 |
+
# 1. Convert to NumPy
|
161 |
+
# 2. Remove the Timestamp column (0)
|
162 |
+
# 3. Convert from µV to mV
|
163 |
+
result = result.t().detach().cpu().numpy()[1:] / 1000
|
164 |
# print(result)
|
165 |
|
166 |
# ------ ECG-12 -------------------------------------------------------
|
|
|
202 |
log(f'Session "{request.session_hash}": Selected ECG #{Sessions[request.session_hash].Selected + 1}')
|
203 |
|
204 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
# ###### Download CSV #######################################################
|
206 |
def downloadCSV(request: gradio.Request) -> pathlib.Path:
|
207 |
|
|
|
209 |
ecgType = Sessions[request.session_hash].Type
|
210 |
fileName = pathlib.Path(Sessions[request.session_hash].TempDirectory.name) / \
|
211 |
('ECG-' + str(Sessions[request.session_hash].Selected + 1) + '.csv')
|
212 |
+
deepfakeecg.dataToCSV(ecgResult, ecgType, fileName)
|
213 |
|
214 |
log(f'Session "{request.session_hash}": Download CSV file {fileName}')
|
215 |
return fileName
|
|
|
223 |
ecgType = Sessions[request.session_hash].Type
|
224 |
fileName = pathlib.Path(Sessions[request.session_hash].TempDirectory.name) / \
|
225 |
('ECG-' + str(Sessions[request.session_hash].Selected + 1) + '.pdf')
|
226 |
+
if ecgType == deepfakeecg.DATA_ECG12:
|
227 |
+
outputLeads = [ 'I', 'II', 'III', 'aVL', 'aVR', 'aVF', 'V1', 'V2', 'V3', 'V4' , 'V5' , 'V6' ]
|
228 |
+
else:
|
229 |
+
outputLeads = [ 'I', 'II', 'V1', 'V2', 'V3', 'V4' , 'V5' , 'V6' ]
|
230 |
+
|
231 |
+
deepfakeecg.dataToPDF(ecgResult, ecgType, outputLeads, fileName,
|
232 |
+
Sessions[request.session_hash].Selected + 1)
|
233 |
|
234 |
log(f'Session "{request.session_hash}": Download PDF file {fileName}')
|
235 |
return fileName
|
requirements.txt
CHANGED
@@ -4,4 +4,4 @@ matplotlib
|
|
4 |
Pillow
|
5 |
pydantic==2.10.6
|
6 |
torch
|
7 |
-
git+https://github.com/dreibh/deepfake-ecg@
|
|
|
4 |
Pillow
|
5 |
pydantic==2.10.6
|
6 |
torch
|
7 |
+
git+https://github.com/dreibh/deepfake-ecg@dreibh/export-improvements#egg=deepfake_ecg
|