def clean_retinotopy(hemi, retinotopy=Ellipsis, mask=Ellipsis, weight=Ellipsis,<EOL>surface='<STR_LIT>', min_weight=Ellipsis, min_eccentricity=<NUM_LIT>,<EOL>visual_area=Ellipsis, map_visual_areas=Ellipsis,<EOL>visual_area_field_signs=Ellipsis,<EOL>measurement_uncertainty=<NUM_LIT>, measurement_knob=<NUM_LIT:1>,<EOL>magnification_knob=<NUM_LIT:0>, fieldsign_knob=<NUM_LIT:6>, edge_knob=<NUM_LIT:0>,<EOL>yield_report=False, steps=<NUM_LIT:100>, rounds=<NUM_LIT:4>, output_style='<STR_LIT>',<EOL>jitter=None, average=None):
<EOL>if jitter in [True, Ellipsis, '<STR_LIT>', '<STR_LIT>']: jitter = (<NUM_LIT:4>, <NUM_LIT>, <NUM_LIT:1>)<EOL>if is_tuple(jitter) and len(jitter) > <NUM_LIT:0>:<EOL><INDENT>if len(jitter) > <NUM_LIT:3>:  raise ValueError('<STR_LIT>')<EOL>if len(jitter) == <NUM_LIT:1>: jitter = jitter + (<NUM_LIT>,)<EOL>if len(jitter) == <NUM_LIT:2>: jitter = jitter + (<NUM_LIT:1>,)<EOL>(jitter_mod, jitter_scale, jitter_phase) = jitter<EOL><DEDENT>else: jitter = None<EOL>if average in [True, Ellipsis, '<STR_LIT>', '<STR_LIT>']: average = (<NUM_LIT:4>, <NUM_LIT:3>)<EOL>if is_tuple(average) and len(average) > <NUM_LIT:0>:<EOL><INDENT>if len(average) > <NUM_LIT:2>:  raise ValueError('<STR_LIT>')<EOL>if len(average) == <NUM_LIT:1>: average = average + (<NUM_LIT:3>,)<EOL>(average_mod, average_phase) = average<EOL><DEDENT>else: average = None<EOL>if visual_area_field_signs is None: visual_area_field_signs = {}<EOL>f = clean_retinotopy_potential(hemi, retinotopy=retinotopy, mask=mask, weight=weight,<EOL>surface=surface, min_weight=min_weight,<EOL>min_eccentricity=min_eccentricity,<EOL>measurement_uncertainty=measurement_uncertainty,<EOL>measurement_knob=measurement_knob,<EOL>magnification_knob=magnification_knob,<EOL>fieldsign_knob=fieldsign_knob, edge_knob=edge_knob,<EOL>visual_area=visual_area, map_visual_areas=map_visual_areas,<EOL>visual_area_field_signs=visual_area_field_signs)<EOL>m = f if pimms.is_map(f) else {None: f}<EOL>(x,y) = np.full((<NUM_LIT:2>, hemi.vertex_count), np.nan) <EOL>tess = hemi if geo.is_tess(hemi) else hemi.tess<EOL>for (k,f) in six.iteritems(m):<EOL><INDENT>X0 = f.meta_data['<STR_LIT>']<EOL>submesh = f.meta_data['<STR_LIT>']<EOL>X = X0<EOL>for ii in range(rounds):<EOL><INDENT>mtd = '<STR_LIT>' if (ii % <NUM_LIT:2>) == <NUM_LIT:0> else '<STR_LIT>'<EOL>if jitter is not None and ii % jitter_mod == jitter_phase:<EOL><INDENT>ec = np.sqrt(np.sum(X**<NUM_LIT:2>, axis=<NUM_LIT:1>))<EOL>th = (np.random.rand(len(ec)) - <NUM_LIT:0.5>)*<NUM_LIT:2>*np.pi<EOL>r  = np.random.exponential(ec*jitter_scale)<EOL>X = X + np.transpose([r*np.cos(th), r*np.sin(th)])<EOL><DEDENT>if average is not None and ii % average_mod == average_phase:<EOL><INDENT>X = np.array([X[k] if len(nn) == <NUM_LIT:0> else np.mean(X[list(nn)],<NUM_LIT:0>)<EOL>for (k,nn) in enumerate(submesh.tess.indexed_neighborhoods)])<EOL><DEDENT>rr = f.minimize(X, method=mtd, options=dict(maxiter=steps, disp=False))<EOL>X = rr.x<EOL><DEDENT>X = np.reshape(X, X0.shape)<EOL>if X.shape[<NUM_LIT:0>] != <NUM_LIT:2>: X = X.T<EOL>for (u,v) in zip([x,y], X): u[tess.index(submesh.labels)] = v<EOL><DEDENT>return as_retinotopy({'<STR_LIT:x>':x, '<STR_LIT:y>':y}, output_style)<EOL>
clean_retinotopy(hemi) attempts to cleanup the retinotopic maps on the given cortical mesh by
  minimizing an objective function that tracks the smoothness of the fields, the orthogonality
  of polar angle to eccentricity, and the deviation of the values from the measured values; the
  yielded result is the smoothed retinotopy, as would be returned by
  as_retinotopy(..., 'visual').

The argument hemi may be a Cortex object or a Mesh. See also clean_retinotopy_potential for
information on the various parameters related to the potential function that is minimized by
clean_retinotopy(). The following additional options are also accepted:
  * output_style (default: 'visual') specifies the style of the output data that should be
    returned; this should be a string understood by as_retinotopy.
  * yield_report (False) may be set to True, in which case a tuple (retino, report) is returned,
    where the report is the return value of the scipy.optimization.minimize function.