generate_sine_wave_grid_stim.py 2.7 KB

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