def checkgrad_block(self, analytic_hess, numeric_hess, verbose=False, step=<NUM_LIT>, tolerance=<NUM_LIT>, block_indices=None, plot=False):
if analytic_hess.dtype is np.dtype('<STR_LIT:object>'):<EOL><INDENT>real_size = get_block_shapes(analytic_hess)<EOL>num_elements = np.sum(real_size)<EOL>if (num_elements, num_elements) == numeric_hess.shape:<EOL><INDENT>numeric_hess = get_blocks(numeric_hess, real_size)<EOL><DEDENT>else:<EOL><INDENT>tmp_numeric_hess = get_blocks(np.zeros((num_elements, num_elements)), real_size)<EOL>tmp_numeric_hess[block_indices] = numeric_hess.copy()<EOL>numeric_hess = tmp_numeric_hess<EOL><DEDENT><DEDENT>if block_indices is not None:<EOL><INDENT>analytic_hess = analytic_hess[block_indices]<EOL>numeric_hess = numeric_hess[block_indices]<EOL><DEDENT>else:<EOL><INDENT>if analytic_hess.dtype is np.dtype('<STR_LIT:object>'):<EOL><INDENT>analytic_hess = unblock(analytic_hess)<EOL>numeric_hess = unblock(numeric_hess)<EOL><DEDENT><DEDENT>ratio = numeric_hess / (numpy.where(analytic_hess==<NUM_LIT:0>, <NUM_LIT>, analytic_hess))<EOL>difference = numpy.abs(analytic_hess - numeric_hess)<EOL>check_passed = numpy.all((numpy.abs(<NUM_LIT:1> - ratio)) < tolerance) or numpy.allclose(numeric_hess, analytic_hess, atol = tolerance)<EOL>if verbose:<EOL><INDENT>if block_indices:<EOL><INDENT>print("<STR_LIT>".format(block_indices))<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>header = ['<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>']<EOL>header_string = map(lambda x: '<STR_LIT>'.join(header), [header])<EOL>separator = '<STR_LIT:->' * len(header_string[<NUM_LIT:0>])<EOL>print('<STR_LIT:\n>'.join([header_string[<NUM_LIT:0>], separator]))<EOL>min_r = '<STR_LIT>' % float(numpy.min(ratio))<EOL>max_r = '<STR_LIT>' % float(numpy.max(ratio))<EOL>max_d = '<STR_LIT>' % float(numpy.max(difference))<EOL>min_d = '<STR_LIT>' % float(numpy.min(difference))<EOL>cols = [max_r, min_r, min_d, max_d]<EOL>if check_passed:<EOL><INDENT>checked = "<STR_LIT>"<EOL><DEDENT>else:<EOL><INDENT>checked = "<STR_LIT>"<EOL><DEDENT>grad_string = "<STR_LIT>".format(checked, cols[<NUM_LIT:0>], cols[<NUM_LIT:1>], cols[<NUM_LIT:2>], cols[<NUM_LIT:3>])<EOL>print(grad_string)<EOL>if plot:<EOL><INDENT>from matplotlib import pyplot as pb<EOL>fig, axes = pb.subplots(<NUM_LIT:2>, <NUM_LIT:2>)<EOL>max_lim = numpy.max(numpy.vstack((analytic_hess, numeric_hess)))<EOL>min_lim = numpy.min(numpy.vstack((analytic_hess, numeric_hess)))<EOL>msa = axes[<NUM_LIT:0>,<NUM_LIT:0>].matshow(analytic_hess, vmin=min_lim, vmax=max_lim)<EOL>axes[<NUM_LIT:0>,<NUM_LIT:0>].set_title('<STR_LIT>')<EOL>axes[<NUM_LIT:0>,<NUM_LIT:0>].xaxis.set_ticklabels([None])<EOL>axes[<NUM_LIT:0>,<NUM_LIT:0>].yaxis.set_ticklabels([None])<EOL>axes[<NUM_LIT:0>,<NUM_LIT:0>].xaxis.set_ticks([None])<EOL>axes[<NUM_LIT:0>,<NUM_LIT:0>].yaxis.set_ticks([None])<EOL>msn = axes[<NUM_LIT:0>,<NUM_LIT:1>].matshow(numeric_hess, vmin=min_lim, vmax=max_lim)<EOL>pb.colorbar(msn, ax=axes[<NUM_LIT:0>,<NUM_LIT:1>])<EOL>axes[<NUM_LIT:0>,<NUM_LIT:1>].set_title('<STR_LIT>')<EOL>axes[<NUM_LIT:0>,<NUM_LIT:1>].xaxis.set_ticklabels([None])<EOL>axes[<NUM_LIT:0>,<NUM_LIT:1>].yaxis.set_ticklabels([None])<EOL>axes[<NUM_LIT:0>,<NUM_LIT:1>].xaxis.set_ticks([None])<EOL>axes[<NUM_LIT:0>,<NUM_LIT:1>].yaxis.set_ticks([None])<EOL>msr = axes[<NUM_LIT:1>,<NUM_LIT:0>].matshow(ratio)<EOL>pb.colorbar(msr, ax=axes[<NUM_LIT:1>,<NUM_LIT:0>])<EOL>axes[<NUM_LIT:1>,<NUM_LIT:0>].set_title('<STR_LIT>')<EOL>axes[<NUM_LIT:1>,<NUM_LIT:0>].xaxis.set_ticklabels([None])<EOL>axes[<NUM_LIT:1>,<NUM_LIT:0>].yaxis.set_ticklabels([None])<EOL>axes[<NUM_LIT:1>,<NUM_LIT:0>].xaxis.set_ticks([None])<EOL>axes[<NUM_LIT:1>,<NUM_LIT:0>].yaxis.set_ticks([None])<EOL>msd = axes[<NUM_LIT:1>,<NUM_LIT:1>].matshow(difference)<EOL>pb.colorbar(msd, ax=axes[<NUM_LIT:1>,<NUM_LIT:1>])<EOL>axes[<NUM_LIT:1>,<NUM_LIT:1>].set_title('<STR_LIT>')<EOL>axes[<NUM_LIT:1>,<NUM_LIT:1>].xaxis.set_ticklabels([None])<EOL>axes[<NUM_LIT:1>,<NUM_LIT:1>].yaxis.set_ticklabels([None])<EOL>axes[<NUM_LIT:1>,<NUM_LIT:1>].xaxis.set_ticks([None])<EOL>axes[<NUM_LIT:1>,<NUM_LIT:1>].yaxis.set_ticks([None])<EOL>if block_indices:<EOL><INDENT>fig.suptitle("<STR_LIT>".format(block_indices))<EOL><DEDENT>pb.show()<EOL><DEDENT><DEDENT>return check_passed<EOL>
Checkgrad a block matrix