Forked from aythamimm/enroll_keystroke/36
Algorithms have at least one input and one output. All algorithm endpoints are organized in groups. Groups are used by the platform to indicate which inputs and outputs are synchronized together. The first group is automatically synchronized with the channel defined by the block in which the algorithm is deployed.
Endpoint Name | Data Format | Nature |
---|---|---|
keystroke | tutorial/atvs_keystroke/1 | Input |
template_id | system/text/1 | Input |
keystroke_model | aythamimm/keystroke_model/6 | Output |
xxxxxxxxxx
import numpy
##Del toolchain es la parte del modeling el cual tiene dos entradas que son keystroke y id que conectan con la base datos de templates con cuatro salidas.
## modeling tiene una salida llamada keystroke_model.
class Algorithm:##Trabajamos con objetos
def __init__(self):
self._features1 = [] ##variables globales definidas
self._features2 = []
self._features3 = []
self._features4 = []
self._features5 = []
def process(self, inputs, outputs):
# collect all the features for the current template
data = inputs['keystroke'].data#llamamos a la entrada y data para coger todos los valores de tutorial/atvs_keystroke/1(es como los ejemplos de cada usuario)
f1 = data.holdtime.given_name#En f1 tenemos los valores del holdtime de la parte del nombre
f2 = data.rplatency.given_name#Igual pero con rplatency ver tutorial/atvs_keystroke/1 en data formats para tener mas claro de que tipo son las variables
feature_vector=[]#creamos un vector
l_f=len(f1)#comprobamos la longitud de f1 (numero de elementos) ya que esta definido como lista y no como array
for i_k in range(l_f):#Creamos un for hasta el numero de elementos
feature_vector.append(float(f1[i_k]))#en feature_vector vamos poniendo los valores de f1 y transformandolos a float ya que son int32?
l_f=len(f2)#igual que hicimos con f1
for i_k in range(l_f):
feature_vector.append(float(f2[i_k])) #creamos una fila con los holdtime del nombre seguido de los rplatency
self._features1.append(feature_vector)#en la variable global self.features1 anadimos todo lo de feature_vector por filas vamos teniendo en cada fila el ejemplo de cada usuario 1.....6
f1 = data.holdtime.family_name#hacemos lo mismo con family_name
f2 = data.rplatency.family_name
feature_vector=[]
l_f=len(f1)
for i_k in range(l_f):
feature_vector.append(float(f1[i_k]))
l_f=len(f2)
for i_k in range(l_f):
feature_vector.append(float(f2[i_k]))
self._features2.append(feature_vector)
f1 = data.holdtime.email#igual con email
f2 = data.rplatency.email
feature_vector=[]
l_f=len(f1)
for i_k in range(l_f):
feature_vector.append(float(f1[i_k]))
l_f=len(f2)
for i_k in range(l_f):
feature_vector.append(float(f2[i_k]))
self._features3.append(feature_vector)
f1 = data.holdtime.nationality#igual con nationality
f2 = data.rplatency.nationality
feature_vector=[]
l_f=len(f1)
for i_k in range(l_f):
feature_vector.append(float(f1[i_k]))
l_f=len(f2)
for i_k in range(l_f):
feature_vector.append(float(f2[i_k]))
self._features4.append(feature_vector)
f1 = data.holdtime.id_number#con dni igual
f2 = data.rplatency.id_number
feature_vector=[]
l_f=len(f1)
for i_k in range(l_f):
feature_vector.append(float(f1[i_k]))
l_f=len(f2)
for i_k in range(l_f):
feature_vector.append(float(f2[i_k]))
self._features5.append(feature_vector)
# Calculate mean and std
if inputs["template_id"].isDataUnitDone():# En esta plataforma no existen los bucles asi que usamos esta operacion que usa la entrada template_id que es como el usuario por el que va, comprueba si el usuario ya ha mandado todos los ejemplos de 1 hasta 6 y si es asi entra en el if.
features11 = numpy.average(self._features1, axis=0)#guardamos en features11 el valor de usar la funcion de numpy average con axis=0 para que nos haga
#la media por columnas
features12 = numpy.std(self._features1, axis=0)#desviacion tipica por columnas
features21 = numpy.average(self._features2, axis=0)
features22 = numpy.std(self._features2, axis=0)
features31 = numpy.average(self._features3, axis=0)
features32 = numpy.std(self._features3, axis=0)
features41 = numpy.average(self._features4, axis=0)
features42 = numpy.std(self._features4, axis=0)
features51 = numpy.average(self._features5, axis=0)
features52 = numpy.std(self._features5, axis=0)
# outputs data
outputs["keystroke_model"].write({#grabamos los datos de salida en keystroke_model con esta operacion
'given_name_average': features11,#guardamos en media del nombre el valor que teniamos en features11 que tiene que ser float64 ver aythamimm/keystroke_model/6
'given_name_std': features12,
'family_name_average': features21,
'family_name_std': features22,
'email_average': features31,
'email_std': features32,
'nationality_average': features41,
'nationality_std': features42,
'id_number_average': features51,
'id_number_std': features52,
})
self._features1 = []#Reiniciamos las variables globales para un nuevo usuario
self._features2 = []
self._features3 = []
self._features4 = []
self._features5 = []
return True
The code for this algorithm in Python
The ruler at 80 columns indicate suggested POSIX line breaks (for readability).
The editor will automatically enlarge to accomodate the entirety of your input
Use keyboard shortcuts for search/replace and faster editing. For example, use Ctrl-F (PC) or Cmd-F (Mac) to search through this box
This algorithm is designed to be used as a simple enrollment strategy. It enrolls a model from several features by computing the average of the enrollment features.
Note
All features must have the same length.