run.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # main imports
  2. import os
  3. import time
  4. import numpy as np
  5. import pickle
  6. # django imports
  7. from django.conf import settings
  8. # module imports
  9. from ..utils import api
  10. from ..utils.processing import crop_images
  11. from .. import config as cfg
  12. # expe imports
  13. from .classes.quest_plus import QuestPlus
  14. from .classes.quest_plus import psychometric_fun
  15. def run_quest_one_image(request, model_filepath, output_file):
  16. # 1. get session parameters
  17. qualities = request.session.get('qualities')
  18. scene_name = request.session.get('scene')
  19. expe_name = request.session.get('expe')
  20. # by default
  21. iteration = 0
  22. # used to stop when necessary
  23. if 'iteration' in request.GET:
  24. iteration = int(request.GET.get('iteration'))
  25. else:
  26. request.session['expe_started'] = False
  27. # 2. Get expe information if started
  28. # first time only init `quest`
  29. # if experience is started we can save data
  30. if request.session.get('expe_started'):
  31. # does not change expe parameters
  32. if request.session['expe_previous_iteration'] == iteration:
  33. return None
  34. else:
  35. answer = int(request.GET.get('answer'))
  36. answer_time = time.time() - request.session['answer_time']
  37. print("Answer time is ", answer_time)
  38. previous_percentage = request.session.get('expe_percentage')
  39. previous_orientation = request.session.get('expe_orientation')
  40. previous_position = request.session.get('expe_position')
  41. previous_stim = request.session.get('expe_stim')
  42. # 3. Load or create Quest instance
  43. # default params
  44. thresholds = np.arange(50, 10000, 50)
  45. stim_space=np.asarray(qualities)
  46. slopes = np.arange(0.0001, 0.001, 0.00003)
  47. # check if necessary to construct `quest` object
  48. if not os.path.exists(model_filepath):
  49. qp = QuestPlus(stim_space, [thresholds, slopes], function=psychometric_fun)
  50. else:
  51. print('Load `qp` model')
  52. filehandler = open(model_filepath, 'rb')
  53. qp = pickle.load(filehandler)
  54. # 4. If expe started update and save experience information and model
  55. # if experience is already began
  56. if request.session.get('expe_started'):
  57. # TODO : check `i` variable
  58. # update of `quest`
  59. # qp.update(qualities[i], answer)
  60. qp.update(qualities[iteration], answer)
  61. entropy = qp.get_entropy()
  62. line = str(previous_stim)
  63. line += ";" + scene_name
  64. line += ";" + str(previous_percentage)
  65. line += ";" + str(previous_orientation)
  66. line += ";" + str(previous_position)
  67. line += ";" + str(answer)
  68. line += ";" + str(answer_time)
  69. line += ";" + str(entropy)
  70. line += '\n'
  71. output_file.write(line)
  72. output_file.flush()
  73. # save `quest` model
  74. file_pi = open(model_filepath, 'wb')
  75. pickle.dump(qp, file_pi)
  76. # 5. Contruct new image and save it
  77. # construct image
  78. if iteration < cfg.expes_configuration[expe_name]['params']['iterations']:
  79. # process `quest`
  80. next_stim = qp.next_contrast()
  81. print("Next quality ", next_stim)
  82. # construct new image
  83. noisy_image = api.get_image(scene_name, next_stim)
  84. # reconstruct reference image from list stored into session
  85. ref_image = api.get_image(scene_name, 'max')
  86. img_merge, percentage, orientation, position = crop_images(noisy_image, ref_image)
  87. else:
  88. request.session['expe_finished'] = True
  89. return None
  90. # save image using user information
  91. # create output folder for tmp files if necessary
  92. tmp_folder = os.path.join(settings.MEDIA_ROOT, cfg.output_tmp_folder)
  93. if not os.path.exists(tmp_folder):
  94. os.makedirs(tmp_folder)
  95. # generate tmp merged image (pass as BytesIO was complicated..)
  96. filepath_img = os.path.join(tmp_folder, request.session.get('id') + '_' + scene_name + '' + expe_name + '.png')
  97. # replace img_merge if necessary (new iteration of expe)
  98. if img_merge is not None:
  99. img_merge.save(filepath_img)
  100. # 6. Prepare session data for current iteration and data for view
  101. # set current step data
  102. request.session['expe_percentage'] = percentage
  103. request.session['expe_orientation'] = orientation
  104. request.session['expe_position'] = position
  105. request.session['answer_time'] = time.time()
  106. request.session['expe_previous_iteration'] = iteration
  107. request.session['expe_stim'] = str(next_stim)
  108. # expe is now started
  109. request.session['expe_started'] = True
  110. # here you can save whatever you need for you experience
  111. data_expe = {
  112. 'image_path': filepath_img
  113. }
  114. return data_expe