| | 45 | |
|---|
| | 46 | |
|---|
| | 47 | |
|---|
| | 48 | def get_display(display): |
|---|
| | 49 | """ |
|---|
| | 50 | Returns a pylab object with a plot() function to draw the plots. |
|---|
| | 51 | |
|---|
| | 52 | Inputs: |
|---|
| | 53 | display - if True, a new figure is created. Otherwise, if display is a |
|---|
| | 54 | subplot object, this object is returned. |
|---|
| | 55 | """ |
|---|
| | 56 | if display is False: |
|---|
| | 57 | return None |
|---|
| | 58 | elif display is True: |
|---|
| | 59 | pylab.figure() |
|---|
| | 60 | return pylab |
|---|
| | 61 | else: |
|---|
| | 62 | return display |
|---|
| | 63 | |
|---|
| | 64 | |
|---|
| | 65 | |
|---|
| | 66 | def progress_bar(progress): |
|---|
| | 67 | """ |
|---|
| | 68 | Prints a progress bar to stdout. |
|---|
| | 69 | |
|---|
| | 70 | Inputs: |
|---|
| | 71 | progress - a float between 0. and 1. |
|---|
| | 72 | """ |
|---|
| | 73 | progressConditionStr = "ERROR: The argument of function NeuroTools.plotting.progress_bar(...) must be a float between 0. and 1.!" |
|---|
| | 74 | assert (type(progress) == float) and (progress >= 0.) and (progress <= 1.), progressConditionStr |
|---|
| | 75 | length = 50 |
|---|
| | 76 | filled = int(round(length*progress)) |
|---|
| | 77 | print "|" + "=" * filled + " " * (length-filled) + "|\r", |
|---|
| | 78 | sys.stdout.flush() |
|---|
| 69 | | def set_pylab_params(fig_width_pt=246.0, |
|---|
| 70 | | ratio=(numpy.sqrt(5)-1.0)/2.0,# Aesthetic golden mean ratio by default |
|---|
| 71 | | text_fontsize=10, tick_labelsize=8, useTex=False): |
|---|
| 72 | | """ |
|---|
| 73 | | Updates a set of parameters within the the pylab run command parameters dictionary 'pylab.rcParams' |
|---|
| 74 | | in order to achieve nicely formatted figures. |
|---|
| 75 | | |
|---|
| 76 | | Inputs: |
|---|
| 77 | | fig_width_pt - Figure width in points. If you want to use your figure inside LaTeX, |
|---|
| 78 | | get this value from LaTeX using '\showthe\columnwidth'. |
|---|
| 79 | | ratio - Ratio between the height and the width of the figure. |
|---|
| 80 | | text_fontsize - Size of axes and in-pic text fonts. |
|---|
| 81 | | tick_labelsize - Size of tick label font. |
|---|
| 82 | | useTex - Enables or disables the use of LaTeX for all labels and texts |
|---|
| 83 | | (for details on how to do that, see http://www.scipy.org/Cookbook/Matplotlib/UsingTex). |
|---|
| 84 | | |
|---|
| 85 | | """ |
|---|
| 86 | | pylab.rcParams.update(pylab_params(fig_width_pt=fig_width_pt, ratio=ratio, text_fontsize=text_fontsize, \ |
|---|
| 87 | | tick_labelsize=tick_labelsize, useTex=useTex)) |
|---|
| 88 | | |
|---|
| 89 | | |
|---|
| 90 | | |
|---|
| 91 | | def get_display(display): |
|---|
| 92 | | """ |
|---|
| 93 | | Returns a pylab object with a plot() function to draw the plots. |
|---|
| | 116 | def set_axis_limits(subplot, xmin, xmax, ymin, ymax): |
|---|
| | 117 | """ |
|---|
| | 118 | Defines the axis limits in a plot. |
|---|
| 96 | | display - if True, a new figure is created. Otherwise, if display is a |
|---|
| 97 | | subplot object, this object is returned. |
|---|
| 98 | | """ |
|---|
| 99 | | if display is False: |
|---|
| 100 | | return None |
|---|
| 101 | | elif display is True: |
|---|
| 102 | | pylab.figure() |
|---|
| 103 | | return pylab |
|---|
| 104 | | else: |
|---|
| 105 | | return display |
|---|
| | 121 | subplot - the targeted plot |
|---|
| | 122 | xmin, xmax - the limits of the x axis |
|---|
| | 123 | ymin, ymax - the limits of the y axis |
|---|
| | 124 | """ |
|---|
| | 125 | if hasattr(subplot, 'xlim'): |
|---|
| | 126 | subplot.xlim(xmin, xmax) |
|---|
| | 127 | subplot.ylim(ymin, ymax) |
|---|
| | 128 | elif hasattr(subplot, 'set_xlim'): |
|---|
| | 129 | subplot.set_xlim(xmin, xmax) |
|---|
| | 130 | subplot.set_ylim(ymin, ymax) |
|---|
| | 131 | else: |
|---|
| | 132 | raise Exception('ERROR: The plot passed to function NeuroTools.plotting.set_axis_limits(...) does not provide limit defining functions.') |
|---|
| 129 | | def set_axis_limits(subplot, xmin, xmax, ymin, ymax): |
|---|
| 130 | | """ |
|---|
| 131 | | Defines the axis limits in a plot. |
|---|
| 132 | | |
|---|
| 133 | | Inputs: |
|---|
| 134 | | subplot - the targeted plot |
|---|
| 135 | | xmin, xmax - the limits of the x axis |
|---|
| 136 | | ymin, ymax - the limits of the y axis |
|---|
| 137 | | """ |
|---|
| 138 | | if hasattr(subplot, 'xlim'): |
|---|
| 139 | | subplot.xlim(xmin, xmax) |
|---|
| 140 | | subplot.ylim(ymin, ymax) |
|---|
| 141 | | elif hasattr(subplot, 'set_xlim'): |
|---|
| 142 | | subplot.set_xlim(xmin, xmax) |
|---|
| 143 | | subplot.set_ylim(ymin, ymax) |
|---|
| 144 | | else: |
|---|
| 145 | | raise Exception('ERROR: The plot passed to function NeuroTools.plotting.set_axis_limits(...) does not provide limit defining functions.') |
|---|
| | 156 | def set_pylab_params(fig_width_pt=246.0, |
|---|
| | 157 | ratio=(numpy.sqrt(5)-1.0)/2.0,# Aesthetic golden mean ratio by default |
|---|
| | 158 | text_fontsize=10, tick_labelsize=8, useTex=False): |
|---|
| | 159 | """ |
|---|
| | 160 | Updates a set of parameters within the the pylab run command parameters dictionary 'pylab.rcParams' |
|---|
| | 161 | in order to achieve nicely formatted figures. |
|---|
| | 162 | |
|---|
| | 163 | Inputs: |
|---|
| | 164 | fig_width_pt - Figure width in points. If you want to use your figure inside LaTeX, |
|---|
| | 165 | get this value from LaTeX using '\showthe\columnwidth'. |
|---|
| | 166 | ratio - Ratio between the height and the width of the figure. |
|---|
| | 167 | text_fontsize - Size of axes and in-pic text fonts. |
|---|
| | 168 | tick_labelsize - Size of tick label font. |
|---|
| | 169 | useTex - Enables or disables the use of LaTeX for all labels and texts |
|---|
| | 170 | (for details on how to do that, see http://www.scipy.org/Cookbook/Matplotlib/UsingTex). |
|---|
| | 171 | |
|---|
| | 172 | """ |
|---|
| | 173 | pylab.rcParams.update(pylab_params(fig_width_pt=fig_width_pt, ratio=ratio, text_fontsize=text_fontsize, \ |
|---|
| | 174 | tick_labelsize=tick_labelsize, useTex=useTex)) |
|---|
| | 181 | |
|---|
| | 182 | |
|---|
| | 183 | |
|---|
| | 184 | def save_2D_image(mat, filename): |
|---|
| | 185 | """ |
|---|
| | 186 | Saves a 2D numpy array of gray shades between 0 and 1 to a PNG file. |
|---|
| | 187 | |
|---|
| | 188 | Inputs: |
|---|
| | 189 | mat - a 2D numpy array of floats between 0 and 1 |
|---|
| | 190 | filename - string specifying the filename where to save the data, has to end on '.png' |
|---|
| | 191 | """ |
|---|
| | 192 | assert PILIMAGEUSE, "ERROR: Since PIL has not been detected, the function NeuroTools.plotting.save_2D_image(...) is not supported!" |
|---|
| | 193 | matConditionStr = "ERROR: First argument of function NeuroTools.plotting.imsave(...) must be a 2D numpy array of floats between 0. and 1.!" |
|---|
| | 194 | filenameConditionStr = "ERROR: Second argument of function NeuroTools.plotting.imsave(...) must be a string ending on \".png\"!" |
|---|
| | 195 | assert (type(mat) == numpy.ndarray) and (mat.ndim == 2) and (mat.min() >= 0.) and (mat.max() <= 1.), matConditionStr |
|---|
| | 196 | assert (type(filename) == str) and (len(filename) > 4) and (filename[-4:].lower() == '.png'), filenameConditionStr |
|---|
| | 197 | mode = 'L' |
|---|
| | 198 | # PIL asks for a permuted (col,line) shape coresponding to the natural (x,y) space |
|---|
| | 199 | pilImage = Image.new(mode, (mat.shape[1], mat.shape[0])) |
|---|
| | 200 | data = numpy.floor(numpy.ravel(mat) * 256.) |
|---|
| | 201 | pilImage.putdata(data) |
|---|
| | 202 | pilImage.save(filename) |
|---|
| | 203 | |
|---|
| | 204 | |
|---|
| | 205 | |
|---|
| | 206 | def save_2D_movie(frame_list, filename, frame_duration): |
|---|
| | 207 | """ |
|---|
| | 208 | Saves a list of 2D numpy arrays of gray shades between 0 and 1 to a zipped tree of PNG files. |
|---|
| | 209 | |
|---|
| | 210 | Inputs: |
|---|
| | 211 | frame_list - a list of 2D numpy arrays of floats between 0 and 1 |
|---|
| | 212 | filename - string specifying the filename where to save the data, has to end on '.zip' |
|---|
| | 213 | frame_duration - specifier for the duration per frame, will be stored as additional meta-data |
|---|
| | 214 | """ |
|---|
| | 215 | try: |
|---|
| | 216 | import zipfile |
|---|
| | 217 | except ImportError: |
|---|
| | 218 | raise ImportError("ERROR: Python module zipfile not found! Needed by NeuroTools.plotting.save_2D_movie(...)!") |
|---|
| | 219 | try: |
|---|
| | 220 | import StringIO |
|---|
| | 221 | except ImportError: |
|---|
| | 222 | raise ImportError("ERROR: Python module StringIO not found! Needed by NeuroTools.plotting.save_2D_movie(...)!") |
|---|
| | 223 | assert PILIMAGEUSE, "ERROR: Since PIL has not been detected, the function NeuroTools.plotting.save_2D_movie(...) is not supported!" |
|---|
| | 224 | filenameConditionStr = "ERROR: Second argument of function NeuroTools.plotting.save_2D_movie(...) must be a string ending on \".zip\"!" |
|---|
| | 225 | assert (type(filename) == str) and (len(filename) > 4) and (filename[-4:].lower() == '.zip'), filenameConditionStr |
|---|
| | 226 | zf = zipfile.ZipFile(filename, 'w', zipfile.ZIP_DEFLATED) |
|---|
| | 227 | container = filename[:-4] # remove .zip |
|---|
| | 228 | frame_name_format = "frame%s.%dd.png" % ("%", pylab.ceil(pylab.log10(len(frame_list)))) |
|---|
| | 229 | for frame_num, frame in enumerate(frame_list): |
|---|
| | 230 | frame_data = [(p,p,p) for p in frame.flat] |
|---|
| | 231 | im = Image.new('RGB', frame.shape, 'white') |
|---|
| | 232 | im.putdata(frame_data) |
|---|
| | 233 | io = StringIO.StringIO() |
|---|
| | 234 | im.save(io, format='png') |
|---|
| | 235 | pngname = frame_name_format % frame_num |
|---|
| | 236 | arcname = "%s/%s" % (container, pngname) |
|---|
| | 237 | io.seek(0) |
|---|
| | 238 | zf.writestr(arcname, io.read()) |
|---|
| | 239 | progress_bar(float(frame_num)/len(frame_list)) |
|---|
| | 240 | |
|---|
| | 241 | # add 'parameters' and 'frames' files to the zip archive |
|---|
| | 242 | zf.writestr("%s/parameters" % container, |
|---|
| | 243 | 'frame_duration = %s' % frame_duration) |
|---|
| | 244 | zf.writestr("%s/frames" % container, |
|---|
| | 245 | '\n'.join(["frame%.3d.png" % i for i in range(len(frame_list))])) |
|---|
| | 246 | zf.close() |
|---|