Vector Comparison Calculator Flow

This Node-RED flow implements a Vector Comparison Calculator that allows you to compare two vectors using different distance or similarity functions. The flow provides an HTTP endpoint ("/run") to receive POST requests with the vectors and the desired comparison parameters. It verifies the shape of the vectors and performs the specified calculation using the selected function. The calculated result is then sent as the response. Additionally, there is an initialization endpoint ("/init") for any required setup. The flow includes error handling and debugging nodes for better troubleshooting. It also contains a test input injection node to demonstrate the functionality of the Vector Comparison Calculator.

Here are the different methods implemented for vector comparison in the Vector Comparison Calculator:

  • Euclidean distance
  • Squared Euclidean distance
  • Additive symmetric distance
  • Average distance
  • Bhattacharyya distance
  • Cosine similarity
  • Czekanowski similarity
  • Dice similarity
  • Intersection similarity
[{"id":"f6f2187d.f17ca8","type":"tab","label":"Vector_Comparison_Calculator","disabled":false,"info":""},{"id":"0417b910463b88a8","type":"http in","z":"f6f2187d.f17ca8","name":"","url":"/run","method":"post","upload":false,"swaggerDoc":"","x":360,"y":180,"wires":[["3c657c44d2031ef3"]]},{"id":"bf55f7feb9c9588b","type":"http response","z":"f6f2187d.f17ca8","name":"","statusCode":"","headers":{},"x":910,"y":180,"wires":[]},{"id":"ee0b74960ab1a361","type":"debug","z":"f6f2187d.f17ca8","name":"debug 9","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":900,"y":100,"wires":[]},{"id":"3c657c44d2031ef3","type":"function","z":"f6f2187d.f17ca8","name":"Vector Comparison Function","func":"const distances_package = global.get('distances_package');\n\nconst vector_a = msg.payload.vector_a;\nconst vector_b = msg.payload.vector_b;\n\n// Check if the vectors have the same shape\nif (vector_a.length !== vector_b.length) {\n    msg.payload = { error: 'Vectors have different shapes' };\n    return msg;\n}\n\nconst operator = msg.payload.operator;\nconst function_ch = msg.payload.function_ch;\n\nlet loaded_function;\n\ntry {\n    switch (operator) {\n        case 'distance':\n            switch (function_ch) {\n                case 'euclidean':\n                    loaded_function = distances_package.distance.euclidean;\n                    break;\n                case 'squaredEuclidean':\n                    loaded_function = distances_package.distance.squaredEuclidean;\n                    break;\n                case 'additiveSymmetric':\n                    loaded_function = distances_package.distance.additiveSymmetric;\n                    break;\n                case 'avg':\n                    loaded_function = distances_package.distance.avg;\n                    break;\n                case 'bhattacharyya':\n                    loaded_function = distances_package.distance.bhattacharyya;\n                    break;\n                // Add more cases for other distance functions if needed\n\n                default:\n                    throw new Error('Invalid distance function_ch');\n            }\n            break;\n\n        case 'similarity':\n            switch (function_ch) {\n                case 'cosine':\n                    loaded_function = distances_package.similarity.cosine;\n                    break;\n                case 'czekanowski':\n                    loaded_function = distances_package.similarity.czekanowski;\n                    break;\n                case 'dice':\n                    loaded_function = distances_package.similarity.dice;\n                    break;\n                case 'intersection':\n                    loaded_function = distances_package.similarity.intersection;\n                    break;\n                // Add more cases for other similarity functions if needed\n\n                default:\n                    throw new Error('Invalid similarity function_ch');\n            }\n            break;\n\n        default:\n            throw new Error('Invalid operator');\n    }\n} catch (error) {\n    // Set the error message in msg.payload\n    msg.payload = { error: error.message };\n    return msg;\n}\n\nconst result = loaded_function(vector_a, vector_b)\nmsg.payload = { \"result\": result };\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":180,"wires":[["bf55f7feb9c9588b","ee0b74960ab1a361"]]},{"id":"10922089e2553f40","type":"http in","z":"f6f2187d.f17ca8","name":"","url":"/init","method":"post","upload":false,"swaggerDoc":"","x":500,"y":360,"wires":[["7f9de0a88607c658"]]},{"id":"7f9de0a88607c658","type":"http response","z":"f6f2187d.f17ca8","name":"","statusCode":"","headers":{},"x":670,"y":360,"wires":[]},{"id":"370c6cbac8985e68","type":"catch","z":"f6f2187d.f17ca8","name":"","scope":null,"uncaught":false,"x":500,"y":280,"wires":[["df611ba3dc35bc94"]]},{"id":"df611ba3dc35bc94","type":"function","z":"f6f2187d.f17ca8","name":"ADD ERROR INFO","func":"var payload=msg.payload;\nmsg.payload={};\n\nmsg.payload.error=msg.error;\nmsg.payload.error.payload=payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":710,"y":280,"wires":[["bf55f7feb9c9588b"]]},{"id":"949f650ae0f737d0","type":"inject","z":"f6f2187d.f17ca8","name":"Inject","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":350,"y":100,"wires":[["98e990a0b093d494"]]},{"id":"98e990a0b093d494","type":"function","z":"f6f2187d.f17ca8","name":"Test input","func":"msg.payload = {\n    operator: \"distance\",\n    function_ch: \"bhattacharyya\",\n    vector_a: [0.1, 0.9, 0.4],\n    vector_b: [0, 0.2, 1.1]\n};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":500,"y":100,"wires":[["3c657c44d2031ef3"]]}]

Collection Info

Flow Info

Created 2 years ago
Rating: not yet rated

Actions

Rate:

Node Types

Core
  • catch (x1)
  • debug (x1)
  • function (x3)
  • http in (x2)
  • http response (x2)
  • inject (x1)
Other
  • tab (x1)

Tags

  • similarity
  • distances
  • vectors
  • comparison
  • ml
  • ai
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option