generate_ori_stim.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. """
  2. Created on 19.02.2015
  3. @author: frank
  4. """
  5. import os
  6. import json
  7. import numpy as np
  8. import objsimpy.geom as geom
  9. from objsimpy.gitrepo import get_sha1_of_latest_commit
  10. from objsimpy.stim.stim_movie import StimulusSet
  11. from objsimpy.stim.stim_movie import write_meta_xml
  12. def main():
  13. generate_ori_stim(os.path.join(os.environ.get('HOME'),
  14. 'tmp',
  15. 'raised_cos_oriphi_o20_a20_p20_fwhm2.5.idlmov'),
  16. output_size=20,
  17. n_angles=20,
  18. n_phases=20,
  19. fwhm=2.5)
  20. def generate_ori_stim(file_path,
  21. output_size=20,
  22. n_angles=20,
  23. n_phases=20,
  24. fwhm=3.0):
  25. generator_args = locals()
  26. write_meta_files(file_path, n_angles, n_phases, __file__, 'generate_ori_stim', generator_args)
  27. center = 0.5*np.array([output_size, output_size]) - 0.5
  28. phase_positions = np.linspace(0, output_size-1, n_phases) - 0.5*output_size + 0.5
  29. angles = np.linspace(0, 180, n_angles, endpoint=False)
  30. pic_list = []
  31. for angle in angles:
  32. normal_angle = angle + 90
  33. normal_dx_dy = np.array([np.cos(geom.deg_to_rad(normal_angle)),
  34. np.sin(geom.deg_to_rad(normal_angle))])
  35. for phase in phase_positions:
  36. pos = center + normal_dx_dy * phase
  37. cos_filter = geom.cos2d(20, pos, fwhm=2.5, line_angle_deg=angle)
  38. pic_list.append(cos_filter)
  39. stim_set = StimulusSet(pics=pic_list)
  40. stim_set.save(file_path)
  41. def write_meta_files(file_path, nx, ny, source_file, generator_name, generator_args):
  42. toplevel, sha1 = get_sha1_of_latest_commit(source_file)
  43. meta_dict = {'MovieMetaFile':
  44. {'StimParas': {'MovieFileName': os.path.basename(file_path),
  45. 'NXParas': nx,
  46. 'NYParas': ny,
  47. },
  48. 'Generator': {'FunctionName': generator_name,
  49. 'SourceFile': os.path.relpath(source_file, toplevel),
  50. 'SHA1': sha1,
  51. 'Args': generator_args
  52. },
  53. }
  54. }
  55. fname_json = os.path.splitext(file_path)[0] + '.meta.json'
  56. with open(fname_json, 'w') as fobj_json:
  57. fobj_json.write(json.dumps(meta_dict, indent=4))
  58. fname_meta = os.path.splitext(file_path)[0] + '.meta.xml'
  59. write_meta_xml(meta_dict, fname_meta)
  60. if __name__ == '__main__':
  61. main()