def __init__(self, size=None, position=None, lambda_=None, theta=None, sigma=None, phase=None, trim=None, contrast=0.5, width=1, hide_dot=False): if not isinstance(np, ModuleType): message = """GaborPatch can not be initialized. The Python package 'Numpy' is not installed.""" raise ImportError(message) if not isinstance(pyplot, ModuleType): message = """GaborPatch can not be initialized. The Python package 'Matplotlib' is not installed.""" raise ImportError(message) fid, filename = tempfile.mkstemp( dir=stimuli.defaults.tempdir, suffix=".png") os.close(fid) Picture.__init__(self, filename, position) size = 200 diameter = 50 line_width = 20 * width inner_diameter = 5 * (int(hide_dot)-1) fig = plt.figure(frameon=False, figsize=(1, 1)) ax = plt.Axes(fig, [0., 0., 1., 1.]) ax.set_axis_off() fig.add_axes(ax) pixel_map = np.zeros((size, size)) ax.imshow(pixel_map, cmap=plt.get_cmap('gray')) ax.add_patch(plt.Circle((size / 2, size / 2), diameter, color='w')) ax.add_patch(plt.Rectangle((size / 2 - line_width / 2, 0), line_width, size, color='k')) ax.add_patch(plt.Rectangle((0, size / 2 - line_width / 2), size, line_width, color='k')) ax.add_patch(plt.Circle((size / 2, size / 2), inner_diameter, color='w')) fig.canvas.draw() # Now we can save it to a numpy array. data = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='') data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,)) #plt.show() self._pixel_array = data #save stimulus color_map = pyplot.get_cmap('gray') color_map.set_over(color="y") pyplot.imsave(fname = filename, arr = self._pixel_array, cmap = color_map, format="png", vmin=0, vmax=1) #plt.savefig(filename = filename, cmap = color_map, format="png") self._background_colour = [0, 0, 0]
def __init__(self, size, position=None, dot_size=(5,5), background_colour=None, dot_colour=None, dot_percentage=50, smoothing=3): """Create a visual mask. Parameters ---------- size : (int, int) size (x, y) of the mask position : (int, int), optional position of the mask stimulus dot_size : (int, int), optional size (x, y) of the dots (default=(5,5)) background_colour : (int, int), optional dot_colour : (int, int), optional dot_percentage : int, optional percentage of covered area by the dots (1 to 100) (default=50) smoothing : int, optional smoothing (default=3) """ if not isinstance(Image, ModuleType): message = """VisualMask can not be initialized. The Python package 'Python Imaging Library (PIL)' is not installed.""" raise ImportError(message) fid, filename = tempfile.mkstemp( dir=stimuli.defaults.tempdir, suffix=".png") os.close(fid) Picture.__init__(self, filename, position) self._size = size self.dot_size = dot_size if background_colour is not None: self.background_colour = background_colour else: self.background_colour = _internals.active_exp.background_colour if dot_colour is not None: self.dot_colour = dot_colour else: self.dot_colour = _internals.active_exp.foreground_colour self.dot_percentage = dot_percentage self.smoothing = smoothing self.create_mask()
def __init__(self, size=None, position=None, lambda_=None, theta=None, sigma=None, phase=None, trim=None): """Create a Gabor Patch. Parameters ---------- size : (int, int), optional size (x, y) of the mask position : (int, int), optional position of the mask stimulus lambda_ : int, optional Spatial frequency (pixel per cycle) theta : int or float, optional Grating orientation in degrees sigma : int or float, optional gaussian standard deviation (in pixels) phase : float 0 to 1 inclusive Notes ----- The background colour of the stimulus depends of the parameters of the Gabor patch and can be determined (e.g. for plotting) with the property `GaborPatch.background_colour`. """ # Parts of the code has be ported from http://www.icn.ucl.ac.uk/courses/MATLAB-Tutorials/Elliot_Freeman/html/gabor_tutorial.html import types if type(np) is not types.ModuleType: message = """GaborPatch can not be initialized. The Python package 'Numpy' is not installed.""" raise ImportError(message) if type(pyplot) is not types.ModuleType: message = """GaborPatch can not be initialized. The Python package 'Matplotlib' is not installed.""" raise ImportError(message) if size is None: size = defaults.gaborpatch_size if position is None: position = defaults.gaborpatch_position if lambda_ is None: lambda_ = defaults.gaborpatch_lambda_ if theta is None: theta = defaults.gaborpatch_theta if sigma is None: sigma = defaults.gaborpatch_sigma if phase is None: phase = defaults.gaborpatch_phase fid, filename = tempfile.mkstemp( dir=expyriment.stimuli.defaults.tempdir, suffix=".png") os.close(fid) Picture.__init__(self, filename, position) # make linear ramp X0 = (np.linspace(1, size, size) / size) - .5 # Set wavelength and phase freq = size / float(lambda_) phaseRad = phase * 2 * np.pi # Make 2D grating Xm, Ym = np.meshgrid(X0, X0) # Change orientation by adding Xm and Ym together in different proportions thetaRad = (theta / 360.) * 2 * np.pi Xt = Xm * np.cos(thetaRad) Yt = Ym * np.sin(thetaRad) grating = np.sin(((Xt + Yt) * freq * 2 * np.pi) + phaseRad) # 2D Gaussian distribution gauss = np.exp(-((Xm**2) + (Ym**2)) / (2 * (sigma / float(size))**2)) # Trim gauss[gauss < trim] = 0 self._pixel_array = grating * gauss #save stimulus color_map = pyplot.get_cmap('gray') color_map.set_over(color="y") pyplot.imsave(fname=filename, arr=self._pixel_array, cmap=color_map, format="png") # determine background color norm = pyplot.normalize(vmin=np.min(self._pixel_array), vmax=np.max(self._pixel_array)) bgc = color_map(norm(0)) self._background_colour = map(lambda x: int(x * 255), bgc[:3])
def __init__(self, size=None, position=None, lambda_=None, theta=None, sigma=None, phase=None, trim=None): """Create a Gabor Patch. Parameters ---------- size : (int, int), optional size (x, y) of the mask position : (int, int), optional position of the mask stimulus lambda_ : int, optional Spatial frequency (pixel per cycle) theta : int or float, optional Grating orientation in degrees sigma : int or float, optional gaussian standard deviation (in pixels) phase : float 0 to 1 inclusive Notes ----- The background colour of the stimulus depends of the parameters of the Gabor patch and can be determined (e.g. for plotting) with the property `GaborPatch.background_colour`. """ # Parts of the code has be ported from http://www.icn.ucl.ac.uk/courses/MATLAB-Tutorials/Elliot_Freeman/html/gabor_tutorial.html import types if type(np) is not types.ModuleType: message = """GaborPatch can not be initialized. The Python package 'Numpy' is not installed.""" raise ImportError(message) if type(pyplot) is not types.ModuleType: message = """GaborPatch can not be initialized. The Python package 'Matplotlib' is not installed.""" raise ImportError(message) if size is None: size = defaults.gaborpatch_size if position is None: position = defaults.gaborpatch_position if lambda_ is None: lambda_ = defaults.gaborpatch_lambda_ if theta is None: theta = defaults.gaborpatch_theta if sigma is None: sigma = defaults.gaborpatch_sigma if phase is None: phase = defaults.gaborpatch_phase fid, filename = tempfile.mkstemp( dir=expyriment.stimuli.defaults.tempdir, suffix=".png") os.close(fid) Picture.__init__(self, filename, position) # make linear ramp X0 = (np.linspace(1, size, size) / size) - .5 # Set wavelength and phase freq = size / float(lambda_) phaseRad = phase * 2 * np.pi # Make 2D grating Xm, Ym = np.meshgrid(X0, X0) # Change orientation by adding Xm and Ym together in different proportions thetaRad = (theta / 360.) * 2 * np.pi Xt = Xm * np.cos(thetaRad) Yt = Ym * np.sin(thetaRad) grating = np.sin(((Xt + Yt) * freq * 2 * np.pi) + phaseRad) # 2D Gaussian distribution gauss = np.exp(-((Xm ** 2) + (Ym ** 2)) / (2 * (sigma / float(size)) ** 2)) # Trim gauss[gauss < trim] = 0 self._pixel_array = grating * gauss #save stimulus color_map = pyplot.get_cmap('gray') color_map.set_over(color="y") pyplot.imsave(fname = filename, arr = self._pixel_array, cmap = color_map, format="png") # determine background color norm = pyplot.normalize(vmin = np.min(self._pixel_array), vmax = np.max(self._pixel_array)) bgc = color_map(norm(0)) self._background_colour = map(lambda x:int(x*255), bgc[:3])