Skip to content

Commit

Permalink
Added paralel generation of images
Browse files Browse the repository at this point in the history
  • Loading branch information
shamnvik committed Apr 22, 2020
1 parent 7e7a161 commit 6a8b452
Showing 1 changed file with 43 additions and 18 deletions.
61 changes: 43 additions & 18 deletions yolo4apnea.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
import lxml
from lxml import etree
from io import StringIO, BytesIO
from sklearn.metrics import confusion_matrix, accuracy_score
from sklearn.metrics import confusion_matrix, accuracy_score,f1_score,roc_auc_score,classification_report,precision_score,roc_curve
import math
from PIL import Image
from multiprocessing import Pool
import itertools
import timeit


XLIM = 90 * 10
Expand Down Expand Up @@ -98,27 +101,29 @@ def read_annotation_file(file):



def plot_and_write_interval(params):
signal,start = params
fig, ax = plt.subplots(figsize=(10, 10))
ax.set_xlim(start, start + XLIM)
fig.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
ax.set_ylim(-1, 1)
ax.plot(signal.index, signal["ABDO_RES"])
fig.savefig(f"tmp/{start}.png")
plt.close()



def generate_image_from_signal(signal):
"""
Plots images from the signal dataframe in a predictable way. Starts a new image for every OVERLAP/10 seconds to map the whole recording
"""

fig, ax = plt.subplots(figsize=(10, 10))
ax.plot(signal.index, signal["SUM"])
ax.set_ylim(-1, 1)
ax.grid(False)
fig.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
"""
pool = Pool(8)
pool.map(plot_and_write_interval,zip([signal for i in range(0, len(signal), OVERLAP)],[i for i in range(0, len(signal), OVERLAP)]))
print("Generated images")

# TODO Create stats showing how far along we are currently. Probably more optimizing and finetuning?

print("Creating images for analysis")
j = 0
with progressbar.ProgressBar(max_value=math.ceil(len(signal)/OVERLAP)) as prog:
for i in range(0, len(signal), OVERLAP):
ax.set_xlim(i, i + XLIM)
prog.update(j)
j += 1
fig.savefig(f"tmp/{i}.png")
print(f"DONE\nCreated {j} images")



Expand Down Expand Up @@ -299,6 +304,25 @@ def convert_events_to_array(events, length):
values[index] = 1
return values

def get_metrics_of_prediction(truth,pred):
print("Getting metrics")
print("Precision Score")
precision = precision_score(truth,pred)
print(precision)

print("Confusion matrix")
confusion = confusion_matrix(truth,pred)
print(confusion)

print("Classification_report")
report = classification_report(truth,pred)
print(report)

print("ROC_AUC")
auc_roc = roc_auc_score(truth,pred)
print(auc_roc)



def get_accuracy(tn, fp, fn, tp, threshold, length=False, total_predicted_events=None, total_annotated_events=None):
"""Predicts different versions of accuracy and precission
Expand Down Expand Up @@ -391,6 +415,7 @@ def compare_prediction_to_annotation(predictions, annotations, threshold, length
tn, fp, fn, tp = confusion_matrix(annotation_truths, apnea_prediction).ravel()
datapoint_results = get_accuracy(tn, fp, fn, tp, threshold)

get_metrics_of_prediction(annotation_truths,apnea_prediction)
# For events
tn, fp, fn, tp = event_confusion_matrix(
annotations, predictions, annotation_truths, apnea_prediction)
Expand Down Expand Up @@ -517,7 +542,7 @@ def predict_edf(file, output_png, output_xml, threshold=0, demo=False, replay=Fa
parser.add_argument(
"-x", '-xml', help='Output predictions annotations to xml file', action="store_true")
parser.add_argument("-t", '--thresh', '-threshold',
help='Change threshold for keeping prediction', type=int)
help='Change threshold for keeping prediction', type=int,default=0)
parser.add_argument("-c", '--compare', help='Compare to annotation file')
parser.add_argument(
"-d", '--display', help='Display predictions to screen', action="store_true")
Expand Down

0 comments on commit 6a8b452

Please sign in to comment.