Verify the ecg signal strip logic for MIT BIH Data - signal-processing

I am trying to create strips from https://physionet.org/content/mitdb/1.0.0/ MIT BIH Strip data. Some how this strip logics seems to have issue. Anyone having experience with MIT BIH data can help me to verify the logic
def strip_validate(strip, pre_aux):
sym_list = list(set(strip['symbol']))
# print(sym_list)
aux_note_list = list(set(strip['aux_note']))
aux_note_list = list(filter(None,aux_note_list))
curr_aux = pre_aux # ''
if len(aux_note_list) > 0:
# print("start")
if 'AFIB' in aux_note_list:
curr_aux = 'AFIB'
# print("curr_aux_previous_1 = ", curr_aux)
elif 'AFL' in aux_note_list:
curr_aux = 'AFL'
# print("curr_aux_previous_2 = ", curr_aux)
elif 'N' in aux_note_list:
curr_aux = 'N'
# print("curr_aux_previous_3 = ", curr_aux)
elif len(aux_note_list) == 1:
curr_aux = aux_note_list[0]
else:
curr_aux = pre_aux # replace " " with curr_aux
if curr_aux != 'AFIB' and curr_aux != 'AFL' :
other_symbols = pd.Series(sym_list)
flag = sum(other_symbols!='N')
if flag > 0:
other_symbols = other_symbols[other_symbols!='N']
other_symbols = other_symbols[other_symbols!='+']
if len(other_symbols)>0:
curr_aux = curr_aux ### Issue curr_aux = other_symbols.values[0], change: (= curr_aux)
else:
curr_aux = curr_aux ### Issue curr_aux = other_symbols.values[0], change: (= curr_aux)
if curr_aux == "AFIB":
curr_aux = curr_aux.replace("AFIB", "A" )
elif curr_aux == "AFL":
curr_aux = curr_aux.replace("AFL", "A" )
return curr_aux,flag

Related

Error trying to convert pytorch regression model to a classification model

I have a GNN that works for regression however I have changed the nature of the task from regression to classification. I thought it would be as simple as converting the loss functions and output size but I am getting multiple errors. This is my code:
Class GNN(torch.nn.Module):
def __init__(self, gnn, n_layer, tfeature_len, dim, mlp_hidden_unit, feature_mode):
super(GNN, self).__init__()
self.gnn = gnn
self.n_layer = n_layer
self.tfeature_len = tfeature_len
self.dim = dim
self.gnn_layers = ModuleList([])
if gnn in ['gcn', 'gat', 'sage', 'tag']:
for i in range(n_layer):
if gnn == 'gcn':
self.gnn_layers.append(GraphConv(in_feats=tfeature_len if i == 0 else dim,
out_feats=dim,
activation=None if i == n_layer - 1 else torch.relu))
elif gnn == 'gat':
num_heads = 16 # make sure that dim is dividable by num_heads
self.gnn_layers.append(GATConv(in_feats=tfeature_len if i == 0 else dim,
out_feats=dim // num_heads,
activation=None if i == n_layer - 1 else torch.relu,
num_heads=num_heads))
elif gnn == 'sage':
agg = 'pool'
self.gnn_layers.append(SAGEConv(in_feats=tfeature_len if i == 0 else dim,
out_feats=dim,
activation=None if i == n_layer - 1 else torch.relu,
aggregator_type=agg))
elif gnn == 'tag':
hops = 2
self.gnn_layers.append(TAGConv(in_feats=tfeature_len if i == 0 else dim,
out_feats=dim,
activation=None if i == n_layer - 1 else torch.relu,
k=hops))
elif gnn == 'sgc':
self.gnn_layers.append(SGConv(in_feats=tfeature_len, out_feats=dim, k=n_layer))
else:
raise ValueError('unknown GNN model')
self.factor = None
self.pooling_layer = SumPooling()
self.mlp_hidden_unit = mlp_hidden_unit
self.feature_mode = feature_mode
if self.feature_mode == 'concat':
self.mlp_hidden_layer = torch.nn.Linear(2 * self.dim, self.mlp_hidden_unit)
elif self.feature_mode == 'subtract':
self.mlp_hidden_layer = torch.nn.Linear(self.dim, self.mlp_hidden_unit)
else:
raise ValueError('unknown feature mode')
self.mlp_output_layer = torch.nn.Linear(self.mlp_hidden_unit, 2)
def forward(self, graph1, graph2):
graph1_embedding = self.calculate_embedding(graph1)
graph2_embedding = self.calculate_embedding(graph2)
if self.feature_mode == 'concat':
hidden = relu(self.mlp_hidden_layer(torch.concat([graph1_embedding, graph2_embedding], dim=-1)))
elif self.feature_mode == 'subtract':
hidden = relu(self.mlp_hidden_layer(graph1_embedding - graph2_embedding))
else:
raise ValueError('unknown feature mode')
output = self.mlp_output_layer(hidden)
return output
def calculate_embedding(self, graph):
feature = graph.ndata['feature']
h = one_hot(feature, num_classes=self.tfeature_len)
h = torch.sum(h, dim=1, dtype=torch.float)
for layer in self.gnn_layers:
h = layer(graph, h)
if self.gnn == 'gat':
h = torch.reshape(h, [h.size()[0], -1])
if self.factor is None:
self.factor = math.sqrt(self.dim) / float(torch.mean(torch.linalg.norm(h, dim=1)))
h *= self.factor
graph_embedding = self.pooling_layer(graph, h)
return graph_embedding
def train(data,model,optimizer):
train_loader, val_loader, test_loader, tfeature_len = data
loss_fn = torch.nn.CrossEntropyLoss()
epoch = 23
model = model.to(device)
print('start training\n')
#evaluate(model, 'train', train_loader)
evaluate(model, 'val', val_loader)
evaluate(model, 'test', test_loader)
epoch_losses = []
for i in range(epoch):
print('epoch %d:' % i)
epoch_loss = 0
model.train()
for graph1, graph2, target in train_loader:
pred = torch.squeeze(model(graph1, graph2))
loss = loss_fn(pred, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
epoch_loss += loss.detach().item()
epoch_loss /= (iter + 1)
print('Epoch {}, loss {:.4f}'.format(epoch, epoch_loss))
epoch_losses.append(epoch_loss)
#evaluate(model, 'train', train_loader)
evaluate(model, 'val', val_loader)
evaluate(model, 'test', test_loader)
print()
def evaluate(model, mode, data):
pred_list = []
target_list = []
model.eval()
with torch.no_grad():
for graph1, graph2, target in data:
outputs = torch.softmax(model(graph1, graph2), 1)
_, predicted = torch.max(outputs.data, 1)
pred_list.append(predicted)
target_list.append(target)
#torch.sum(preds == targets).detach().cpu().numpy().
pred_list = torch.concat(pred_list)
target_list = torch.concat(target_list)
#print('%s Acc: %.4f' % (mode (sklearn.metrics.accuracy_score(target_list, pred_list, normalize=False)) / len(pred_list) * 10
The accuracy is commented out at the moment because that too gave me an error. My first error however was the following:
start training
epoch 0:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-84-9de6c6dbd2ee> in <module>
----> 1 train(data,model,optimizer)
3 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction, label_smoothing)
3012 if size_average is not None or reduce is not None:
3013 reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 3014 return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
3015
3016
RuntimeError: "nll_loss_forward_reduce_cuda_kernel_2d_index" not implemented for 'Float'
My targets are as follows:
targets = training_data['Class'].tolist()
targets = torch.Tensor(targets)
targets = targets.to(device)
And is just a list of 1s and 0s.
I call my model as follows:
model = GNN('sage', 3, tfeature_len, 2048, 100, 'subtract')
optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
How I can fix this?

How to solve nqueen problem with Z3 solver

I write a formula to solve nqueen problem. It finds one of the solution but I want find all solution how I generalize to all solution for this formula:
from z3 import *
import time
def queens(n,all=0):
start = time.time()
sol = Solver()
# q = [Int("q_%s" % (i)) for i in range(n) ] # n=100: ???s
# q = IntVector("q", n) # this is much faster # n=100: 28.1s
q = Array("q", IntSort(), BitVecSort(8)) # n=100: ??s
# Domains
sol.add([And(q[i]>=0, q[i] <= n-1) for i in range(n)])
# Constraints
for i in range(n):
for j in range(i):
sol.add(q[i] != q[j], q[i]+i != q[j]+j, q[i]-i != q[j]-j)
if sol.check() == sat:
mod = sol.model()
ss = [mod.evaluate(q[i]) for i in range(n)]
print(ss)
# Show all solutions
if all==1:
num_solutions = 0
while sol.check() == sat:
m = sol.model()
ss = [mod.evaluate(q[i]) for i in range(n)]
sol.add( Or([q[i] != ss[i] for i in range(n)]) )
print("q=",ss)
num_solutions = num_solutions + 1
print("num_solutions:", num_solutions)
else:
print("failed to solve")
end = time.time()
value = end - start
print("Time: ", value)
for n in [8,10,12,20,50,100,200]:
print("Testing ", n)
queens(n,0)
For N=4 I try to show 2 solution
For N=8 I try to show all 92 solution
You got most of it correct, though there are issues with how you coded the find-all-solutions part. There's a solution for N-queens that comes with the z3 tutorial here: https://ericpony.github.io/z3py-tutorial/guide-examples.htm
You can turn it into an "find-all-solutions" versions like this:
from z3 import *
def queens(n):
Q = [Int('Q_%i' % (i + 1)) for i in range(n)]
val_c = [And(1 <= Q[i], Q[i] <= n) for i in range(n)]
col_c = [Distinct(Q)]
diag_c = [If(i == j, True, And(Q[i] - Q[j] != i - j, Q[i] - Q[j] != j - i)) for i in range(n) for j in range(i)]
sol = Solver()
sol.add(val_c + col_c + diag_c)
num_solutions = 0
while sol.check() == sat:
mod = sol.model()
ss = [mod.evaluate(Q[i]) for i in range(n)]
print(ss)
num_solutions += 1
sol.add(Or([Q[i] != ss[i] for i in range(n)]))
print("num_solutions:", num_solutions)
queens(4)
queens(8)
This'll print 2 solutions for N=4 and 92 for N=8.

Why Foreground Detection give me inconsistent detection?

I have a problem while running this foreground detection code from this website
this is my code (i did slight change, to fit the latest opencv version):
import numpy as np
import cv2
import keras
from keras.preprocessing.image import ImageDataGenerator
from keras.models import load_model
import tensorflow as tf
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession
config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)
#Load CNN Model
model = load_model("VGG16withALLTRAINABLE (Currently Best Model).h5")
#Creating ROI frame for capturing hand
top_ROI = 100
btm_ROI = 300
right_ROI = 50
left_ROI = 250
#Creating Background Removal Parameters
blur_size = 5
canny_low = 25
# min_area = 0
# max_area = 0
canny_high = 150
dilate_iter = 10
erode_iter = 10
mask_color = (0.0,0.0,0.0)
#Video Capture
cap = cv2.VideoCapture(0)
while True:
ret,frame = cap.read()
#flipping frame
# frame = cv2.flip(frame, 1)
#Create ROI inside Frame
roi = frame[top_ROI:btm_ROI, right_ROI:left_ROI]
cv2.rectangle(frame, (left_ROI, top_ROI), (right_ROI,btm_ROI), (255,128,0), 3) #Visual Rectangle for ROI
#Resizing and Reshaping to equalize model input size and shape
roi = cv2.resize(roi, (300, 300))
blurred_roi = cv2.GaussianBlur(roi, (blur_size,blur_size) , 0)
gray_roi = cv2.cvtColor(blurred_roi, cv2.COLOR_BGR2GRAY)
edge = cv2.Canny(gray_roi, canny_low, canny_high)
edge = cv2.dilate(edge, None)
edge = cv2.erode(edge, None)
cntr = []
cntr_area = []
contours,_= cv2.findContours(edge, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
contour_info = []
# for contour in contours:
# cntr.append(contour)
# # cntr_area.append(cv2.contourArea(contour))
# area = cv2.contourArea(contour)
# cntr = np.array(cntr)
# cntr_area = np.array(cntr_area)
# print(area)
for c in contours:
contour_info.append((c,cv2.contourArea(c), ))
contour_info = np.array(contour_info)
contour_info = sorted(contour_info, key=lambda x: x[1], reverse=True)
max_contour = contour_info[0]
mask = np.zeros(edge.shape)
cv2.fillConvexPoly(mask, max_contour[0], (255))
mask = cv2.dilate(mask, None, iterations=dilate_iter)
mask = cv2.erode(mask, None, iterations=erode_iter)
mask = cv2.GaussianBlur(mask, (blur_size, blur_size), 0)
mask_stack = np.dstack([mask]*3) # Create 3-channel alpha mask
#-- Blend masked img into MASK_COLOR background --------------------------------------
mask_stack = mask_stack.astype('float32') / 255.0 # Use float matrices,
roi = roi.astype('float32') / 255.0 # for easy blending
masked = (mask_stack * roi) + ((1-mask_stack) * mask_color) # Blend
masked = (masked * 255).astype('uint8') # Convert back to 8-bit
print(mask.shape)
print(mask_stack.shape)
# prediction_box = np.reshape(roi,(1,roi.shape[0],roi.shape[1],3))
#Prediction
# pred = model.predict(prediction_box)
# pred_class = np.argmax(pred)
# if pred_class == 0:
# pred_class = "A"
# elif pred_class == 1:
# pred_class = "B"
# elif pred_class == 2:
# pred_class = "C"
# elif pred_class == 3:
# pred_class = "D"
# elif pred_class == 4:
# pred_class = "E"
# elif pred_class == 5:
# pred_class = "F"
# elif pred_class == 6:
# pred_class = "G"
# elif pred_class == 7:
# pred_class = "H"
# elif pred_class == 8:
# pred_class = "I"
# elif pred_class == 9:
# pred_class = "J"
# elif pred_class == 10:
# pred_class = "K"
# elif pred_class == 11:
# pred_class = "L"
# elif pred_class == 12:
# pred_class = "M"
# elif pred_class == 13:
# pred_class = "N"
# elif pred_class == 14:
# pred_class = "O"
# elif pred_class == 15:
# pred_class = "P"
# elif pred_class == 16:
# pred_class = "Q"
# elif pred_class == 17:
# pred_class = "R"
# elif pred_class == 18:
# pred_class = "S"
# elif pred_class == 19:
# pred_class = "T"
# elif pred_class == 20:
# pred_class = "U"
# elif pred_class == 21:
# pred_class = "V"
# elif pred_class == 22:
# pred_class = "W"
# elif pred_class == 23:
# pred_class = "X"
# elif pred_class == 24:
# pred_class = "Y"
# elif pred_class == 25:
# pred_class = "Z"
# elif pred_class == 26:
# pred_class = "del"
# elif pred_class == 27:
# pred_class = "nothing"
# elif pred_class == 28:
# pred_class = "space"
# cv2.putText(frame, pred_class ,(10, 80), cv2.FONT_ITALIC, 3, (51,255,51), 5)
cv2.imshow("Frame", frame)
cv2.imshow("ROI", gray_roi)
cv2.imshow("Edges", edge)
cv2.imshow('Mask', masked)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()
My problem is, the output showing an inconsistent foreground detection. can someone help me what i did wrong ??
Outputs :
input image and outputs (the MASK frame is the bg removal result, but the removal is inconsistent, almost cant remove my bg perfectly)

differential evolution global minimum problem

Im trying to get the global minimum of a non linear function with linear constraints. I share you the code:
rho = 1.0
g = 9.8
C = 1.0 #roughness coefficient
J = rho*g/(float(1e6)) #constant of function
capacity = [0.3875, 0.607, 0.374] #Supply data
demand = [0.768, 0.315, 0.38,] #Demand data
m= len(capacity)
n = len(demand)
x0 = np.array(m*n*[0.01]) #Initial point for algorithm
# In[59]:
#READ L AND d FROM ARCGIS !!!
L = (314376.57, 277097.9663, 253756.9869 ,265786.5632, 316712.6028, 232857.1468, 112063.9914, 135762.94, 131152.8206)
h= (75, 75, 75, 75, 75, 75, 1320,75,75)
K = np.zeros(m*n)
N=np.zeros(m*n)
# In[60]:
#Each path has its own L,d, therefore, own constant
# In[61]:
#np.seterr(all='ignore')
def diameter(x):
d = 1.484
if x >= 0.276:
d = 1.484
elif x >= 0.212:
d = 1.299
elif x >= 0.148:
d = 1.086
elif x >= 0.0975:
d = 0.881
elif x >= 0.079:
d = 0.793
elif x >= 0.062:
d = 0.705
elif x >= 0.049:
d = 0.626
elif x >= 0.038:
d = 0.555
elif x >= 0.030:
d = 0.494
elif x >= 0.024:
d = 0.441
elif x >= 0.0198:
d = 0.397
elif x >= 0.01565:
d = 0.353
elif x >= 0.0123:
d = 0.313
elif x >= 0.0097:
d = 0.278
elif x >= 0.0077:
d = 0.247
elif x >= 0.0061:
d = 0.22
elif x >= 0.0049:
d = 0.198
elif x >= 0.00389:
d = 0.176
elif x >= 0.0032:
d = 0.159
elif x >= 0.0025:
d = 0.141
elif x >= 0:
d = 0.123
return d
#Definition of Objetive function
def objective(x):
sum_obj = 0.0
for i in range(len(L)):
K = 10.674*L[i]/(C**1.852*diameter(x[i])**4.871)
N[i] = K*x[i]**2.852*J+x[i]*J*h[i]
sum_obj = sum_obj + N[i]
return sum_obj
print(str(objective(x0)))
#Definition of the constraints
GB=[]
for i in range(m):
GB.append(n*i*[0]+n*[1]+(m-1-i)*n*[0])
P=[]
for i in range(n):
P.append(m*(i*[0]+[1]+(n-1-i)*[0]))
DU=np.array(GB+P)
lb = np.array(m*[0] + demand) # Supply
ub = np.array(capacity + n*[np.inf]) # Demand
# In[62]:
b = (0, 1)
bnds = []
for i in range(m*n):
bnds.append(b)
cons = LinearConstraint(DU,lb,ub)
solution = differential_evolution(objective,x0,cons,bnds)
x = solution.x
# show initial objective
print('Initial SSE Objective: ' + str(objective(x0)))
# show final objective
print('Final SSE Objective: ' + str(objective(x)))
# print solution
print('Solution Supply to Demand:')
print('Q = ' + str(((np.around(x,6)).reshape(10,18))))
I dont know why, but when I run appear the following:
"if strategy in self._binomial:
TypeError: unhashable type: 'list'
Anyone have gotten the same mistake ? Its my first time trying to solve optimization problem, so I need a little bit of help. Any advice is welcome !!
Thanks a lot !
You're not calling differential_evolution correctly. For your example you should call it as:
differential_evolution(objective, bnds, contraints=(cons,))
(I'm not sure if there are additional problems)

How do you reference a table with a key value that is numeric in Lua?

The output for the below script is:
AD[1] = [variable not found]
AD['2'] = bar
How can I modify the function getfield to return a value for v for both cases?
function getfield (f)
local v = _G
for w in string.gfind(f, "[%w_]+") do
v = v[w]
end
return v
end
AD = {[1] = 'foo', ['2'] = 'bar'}
data = {"AD[1]","AD['2']"}
for i,line in ipairs(data) do
s = getfield(line)
if s then
print(line .. " = " .. s)
else
print(line .. " = [variable not found]")
end
end
UPDATE:
I'm 90% sure, this is going to work for me:
function getfield (f)
local v = _G
for w in string.gfind(f, "['%w_]+") do
if (string.find(w,"['%a_]")==nil) then
w = loadstring('return '..w)()
else
w = string.gsub(w, "'", "")
end
v=v[w]
end
return v
end
This happens to work
function getfield (f)
local v = _G
for w in string.gfind(f, "['%w_]+") do
local x = loadstring('return '..w)()
print(w,x)
v = v[x] or v[w]
end
return v
end
AD = {[1] = 'foo', ['2'] = 'bar'}
data = {"AD[1]","AD['2']"}
for i,line in ipairs(data) do
s = getfield(line)
if s then
print(line .. " = " .. s)
else
print(line .. " = [variable not found]")
end
end
but it's pretty fragile.
Note that I added ' to the pattern.
The difficulty is that sometimes w is a string representing a name (key), and sometimes it's a string representing a number. In the second case it needs to be converted from string to number. But you need the context or some syntax to decide.
Here's the kind of fragility I mean:
> data = {"math[pi]","AD['2']"}
>
> for i,line in ipairs(data) do
>> s = getfield(line)
>> if s then
>> print(line .. " = " .. s)
>> else
>> print(line .. " = [variable not found]")
>> end
>> end
math table: 0x10ee05100
pi nil
math[pi] = 3.1415926535898
AD table: 0x10ee19ee0
'2' 2
AD['2'] = bar
> pi = 3
> math[3] = 42
> data = {"math[pi]","AD['2']"}>
> for i,line in ipairs(data) do
>> s = getfield(line)
>> if s then
>> print(line .. " = " .. s)
>> else
>> print(line .. " = [variable not found]")
>> end
>> end
math table: 0x10ee05100
pi 3
math[pi] = 42
AD table: 0x10ee19ee0
'2' 2
AD['2'] = bar
math[pi] is unchanged, but getfield interprets pi in the global context and gets 3 so the wrong field of math is returned.
You'll get the strings '1' and "'2'". You have to evaluate it to turn it into whatever object it is:
v = v[loadstring('return ' .. w)()]
Don't do this if the string came from an untrusted source though (like a user input or something) because they could execute arbitrary code.

Resources