You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

generate.ipynb 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 10,
  6. "metadata": {},
  7. "outputs": [
  8. {
  9. "name": "stdout",
  10. "output_type": "stream",
  11. "text": [
  12. "\n",
  13. "Scoring: ((1, 1), (7, 7))\n",
  14. "1515\n",
  15. "\n",
  16. "Scoring: ((2, 1), (2, 2))\n",
  17. "9000\n",
  18. "\n",
  19. "Scoring: ((2, 7), (6, 3))\n",
  20. "4343\n",
  21. "\n",
  22. "Scoring: ((6, 3), (1, 2))\n",
  23. "4901\n",
  24. "\n",
  25. "Scoring: ((5, 6), (4, 6))\n",
  26. "9000\n",
  27. "\n",
  28. "Scoring: ((1, 2), (3, 3))\n",
  29. "7764\n",
  30. "\n",
  31. "Scoring: ((1, 1), (1, 1))\n",
  32. "10000\n"
  33. ]
  34. }
  35. ],
  36. "source": [
  37. "import json\n",
  38. "import copy\n",
  39. "from itertools import product\n",
  40. "from scipy import spatial\n",
  41. "import math\n",
  42. "import numpy\n",
  43. "\n",
  44. " \n",
  45. "f = open('./config.json')\n",
  46. "from_json_config = json.load(f)\n",
  47. "config = {}\n",
  48. "config[\"all_possible_responses\"] = from_json_config[\"scoreVals\"]\n",
  49. "config[\"magic\"] = from_json_config[\"magic\"]\n",
  50. "config[\"aspect_size\"] = 2\n",
  51. "config[\"write\"]= True\n",
  52. "config[\"file\"] = \"../generated/prescore_matrix.json\"\n",
  53. "config[\"delimiter\"] = \"-\"\n",
  54. "config[\"version\"] = \"0.1.0\"\n",
  55. "# max distance = distance between top left and bottom right indices in matrix\n",
  56. "config[\"max_euclidean_distance\"] = math.sqrt(\n",
  57. " math.pow(len(config[\"all_possible_responses\"])-1, 2) +\n",
  58. " math.pow(len(config[\"all_possible_responses\"])-1, 2)\n",
  59. " ) \n",
  60. "f.close()\n",
  61. "\n",
  62. "def createPermutations(possibilities, size):\n",
  63. " return tuple(product(possibilities, repeat=size))\n",
  64. "\n",
  65. "\n",
  66. "def getAspectFromSurveys(survey_a, survey_b, size):\n",
  67. " if (survey_a.__len__() < size | survey_b.__len__() < size):\n",
  68. " raise Exception(\"Surveys must both contain more items than size\")\n",
  69. "\n",
  70. " def store(survey, length):\n",
  71. " col = []\n",
  72. " for i in range(size):\n",
  73. " val = survey.pop(0)\n",
  74. " col.append(val)\n",
  75. " return col\n",
  76. "\n",
  77. " # Take the first <size> elements from the list\n",
  78. " col_a = tuple(store(survey_a, size))\n",
  79. " col_b = tuple(store(survey_b, size))\n",
  80. "\n",
  81. " if (col_a.__len__() != size | col_b.__len__() != size ):\n",
  82. " raise Exception(\"No aspect values found in survey\")\n",
  83. "\n",
  84. " return (col_a, col_b)\n",
  85. "\n",
  86. "\n",
  87. "def scoreAspect(aspect_ab):\n",
  88. " a = aspect_ab[0]\n",
  89. " b = aspect_ab[1]\n",
  90. " return (1 - spatial.distance.cosine(a,b)) * config[\"magic\"]\n",
  91. "\n",
  92. "def euclideanDistance(aspect_ab):\n",
  93. " a = numpy.array(aspect_ab[0])\n",
  94. " b = numpy.array(aspect_ab[1])\n",
  95. " return numpy.linalg.norm(a - b)\n",
  96. "\n",
  97. "def prescore_matrix_from(vals):\n",
  98. " m = {}\n",
  99. " for val in vals:\n",
  100. " m[val] = []\n",
  101. " for other_val in vals:\n",
  102. " distance = euclideanDistance((val,other_val))\n",
  103. " score = (config[\"max_euclidean_distance\"] - distance) * config[\"magic\"] + 1515\n",
  104. " # score = scoreAspect((val, other_val))\n",
  105. " adjusted_score = round(score)\n",
  106. " m[val].append(adjusted_score)\n",
  107. " return m\n",
  108. "\n",
  109. "\n",
  110. "def lookup_prescore_in(score_matrix, vals, aspect_ab):\n",
  111. " print(\"\\nScoring:\", aspect_ab)\n",
  112. " aspect_a, aspect_b = aspect_ab\n",
  113. " # Look-up using the index because\n",
  114. " # \n",
  115. " pos_b = vals.index(aspect_b)\n",
  116. " return score_matrix[aspect_a][pos_b]\n",
  117. "\n",
  118. "\n",
  119. "# !: Mutates your input\n",
  120. "def score_aspect(input_a, input_b, score_matrix, vals):\n",
  121. " aspect_ab = getAspectFromSurveys(input_a, input_b, config[\"aspect_size\"])\n",
  122. " return lookup_prescore_in(score_matrix, vals, aspect_ab)\n",
  123. "\n",
  124. "\n",
  125. "def run():\n",
  126. " # Set the keys for the look-up\n",
  127. " xy_axis_vals = createPermutations(config[\"all_possible_responses\"], config[\"aspect_size\"])\n",
  128. " m = prescore_matrix_from(xy_axis_vals)\n",
  129. "\n",
  130. " # Example:\n",
  131. " res = config[\"all_possible_responses\"]\n",
  132. " input_a = [\n",
  133. " res[0], res[0], # One aspect\n",
  134. " res[1], res[0],\n",
  135. " res[1], res[6],\n",
  136. " res[5], res[2],\n",
  137. " res[4], res[5],\n",
  138. " res[0], res[1],\n",
  139. " res[0], res[0],\n",
  140. " ]\n",
  141. " input_b = [\n",
  142. " res[6], res[6], # One aspect\n",
  143. " res[1], res[1],\n",
  144. " res[5], res[2],\n",
  145. " res[0], res[1],\n",
  146. " res[3], res[5],\n",
  147. " res[2], res[2],\n",
  148. " res[0], res[0],\n",
  149. " ]\n",
  150. " for i in range(round(input_a.__len__() / 2)):\n",
  151. " print(score_aspect(input_a, input_b, m, xy_axis_vals))\n",
  152. "\n",
  153. "\n",
  154. " if(config[\"write\"] == True):\n",
  155. " # Serializing json\n",
  156. " str_m = {}\n",
  157. " for key, values in m.items():\n",
  158. " delimiter = config[\"delimiter\"]\n",
  159. " str_key = delimiter.join([str(v) for v in key])\n",
  160. " str_m[str_key] = [int(val) for val in values]\n",
  161. " str_m[\"_config\"] = config\n",
  162. " json_object = json.dumps(str_m, indent = 4)\n",
  163. " with open(config[\"file\"], \"w\") as file:\n",
  164. " # write to file\n",
  165. " file.write(json_object)\n",
  166. "run()\n"
  167. ]
  168. }
  169. ],
  170. "metadata": {
  171. "kernelspec": {
  172. "display_name": "Python 3.10.6 64-bit",
  173. "language": "python",
  174. "name": "python3"
  175. },
  176. "language_info": {
  177. "codemirror_mode": {
  178. "name": "ipython",
  179. "version": 3
  180. },
  181. "file_extension": ".py",
  182. "mimetype": "text/x-python",
  183. "name": "python",
  184. "nbconvert_exporter": "python",
  185. "pygments_lexer": "ipython3",
  186. "version": "3.10.6"
  187. },
  188. "orig_nbformat": 4,
  189. "vscode": {
  190. "interpreter": {
  191. "hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e"
  192. }
  193. }
  194. },
  195. "nbformat": 4,
  196. "nbformat_minor": 2
  197. }