run.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # main imports
  2. import os
  3. import time
  4. import numpy as np
  5. import pickle
  6. # module imports
  7. from ..utils import api
  8. from ..utils.processing import crop_images
  9. from .. import config as cfg
  10. # expe imports
  11. from .quest_plus import QuestPlus
  12. from .quest_plus import psychometric_fun
  13. def run_quest_one_image(request, model_filepath, output_file):
  14. # get parameters
  15. qualities = request.session.get('qualities')
  16. scene_name = request.session.get('scene')
  17. expe_name = request.session.get('expe')
  18. # by default
  19. iterations = 0
  20. # first time only init `quest`
  21. # if experience is started we can save data
  22. if request.session.get('expe_started'):
  23. answer = int(request.GET.get('answer'))
  24. iterations = int(request.GET.get('iteration'))
  25. answer_time = time.time() - request.session['answer_time']
  26. print("Answer time is ", answer_time)
  27. previous_percentage = request.session.get('expe_percentage')
  28. previous_orientation = request.session.get('expe_orientation')
  29. previous_position = request.session.get('expe_position')
  30. # default params
  31. thresholds = np.arange(50, 10000, 50)
  32. stim_space=np.asarray(qualities)
  33. slopes = np.arange(0.0001, 0.001, 0.00003)
  34. # check if necessary to construct `quest` object
  35. if not os.path.exists(model_filepath):
  36. qp = QuestPlus(stim_space, [thresholds, slopes], function=psychometric_fun)
  37. else:
  38. print('Load `qp` model')
  39. filehandler = open(model_filepath, 'rb')
  40. qp = pickle.load(filehandler)
  41. # construct image and update `quest` only if necessary
  42. if iterations < cfg.expes_configuration[expe_name]['params']['iterations']:
  43. # process `quest`
  44. next_stim = qp.next_contrast()
  45. print("Next quality ", next_stim)
  46. # construct new image
  47. noisy_image = api.get_image(scene_name, next_stim)
  48. # reconstruct reference image from list stored into session
  49. ref_image = api.get_image(scene_name, 'max')
  50. img_merge, percentage, orientation, position = crop_images(noisy_image, ref_image)
  51. else:
  52. request.session['expe_finished'] = True
  53. return None
  54. # if experience is already begin
  55. if request.session.get('expe_started'):
  56. # TODO : check `i` variable
  57. # update of `quest`
  58. # qp.update(qualities[i], answer)
  59. qp.update(qualities[iterations], answer)
  60. entropy = qp.get_entropy()
  61. line = str(next_stim)
  62. line += ";" + scene_name
  63. line += ";" + str(previous_percentage)
  64. line += ";" + str(previous_orientation)
  65. line += ";" + str(previous_position)
  66. line += ";" + str(answer)
  67. line += ";" + str(answer_time)
  68. line += ";" + str(entropy)
  69. line += '\n'
  70. print(line)
  71. # TODO : add answer time from javascript
  72. output_file.write(line)
  73. output_file.flush()
  74. # save `quest` model
  75. file_pi = open(model_filepath, 'wb')
  76. pickle.dump(qp, file_pi)
  77. # set current step data
  78. request.session['expe_percentage'] = percentage
  79. request.session['expe_orientation'] = orientation
  80. request.session['expe_position'] = position
  81. request.session['answer_time'] = time.time()
  82. # expe is now started
  83. request.session['expe_started'] = True
  84. return img_merge