run.py 3.3 KB

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