fig.text(0.15, 0.95, "scaled design matrix for %s" % key, weight='bold', fontsize=18)
    ax.set_xlabel("Parameters", fontsize=16)
    ax.set_xticklabels([])
    ax.set_ylabel("Scan time course", fontsize=16)
    # save plot
    plt.savefig(figure_path + "design_matrix_%s" % key)
    # clean plot
    plt.clf()
# close pyplot window
plt.close()
print ("Design matrices are saved as figures")
print (separator)

# use maskfunc to generate mask
print ("Progress: generate mask for brain images")
mask, mean_data = msk.generateMaskedBrain(sub_clean_img)
print ("Generate mask for brain images: complete!")

# save mask as figure
for key, each in mask.items():
    for i in range(1, 90):
        plt.subplot(9, 10, i)
        plt.imshow(each[:, :, i], interpolation="nearest", cmap="gray", alpha=0.5)
        # label plot
        ax = plt.gca()
        ax.set_xticklabels([])
        ax.set_yticklabels([])
    # save plot
    plt.savefig(figure_path + "all_masks_for_%s.png" % key)
    # clear plot
    plt.clf()
Esempio n. 2
0
sub = sc.subject(subid)

# get image files of this subject:
sub_img = sub.run_img_result

# get data for those figures
print ("Get data from images...")
sub_data = {}
for key, img in sub_img.iteritems():
    sub_data[key] = img.get_data()
print ("Complete!")
print (separator)

brain1 = sub_data["sub005_run001"]
x1 = copy.deepcopy(sub_data)
maskedDict, volDict = msk.generateMaskedBrain(x1)
s5r1Masked = maskedDict["sub005_run001"]
s5r1Vol = volDict["sub005_run001"]

brain2 = sub_data["sub005_run002"]
s5r2Masked = maskedDict["sub005_run002"]
s5r2Vol = volDict["sub005_run002"]

brain3 = sub_data["sub005_run003"]
s5r3Masked = maskedDict["sub005_run003"]
s5r3Vol = volDict["sub005_run003"]

brain4 = sub_data["sub005_run004"]
s5r4Masked = maskedDict["sub005_run004"]
s5r4Vol = volDict["sub005_run004"]
# rescale design matrix
design_matrix = lm.batch_scale_matrix(design_matrix)
print("Design matrix rescaled")

# save scaled design matrix as figure
for key, matrix in design_matrix.iteritems():
    # plt.figure()
    plt.imshow(matrix, aspect=0.1, interpolation="nearest", cmap="gray")
    plt.title("scaled design matrix for %s" % key)
    plt.savefig(figure_path + "design_matrix_%s" % key)
    plt.clf()
plt.close()
print("Design matrices are saved as figures")

# use maskfunc to generate mask
mask, mean_data = msk.generateMaskedBrain(sub1_clean_img)

# run linear regression to generate betas
# first step: use mask to get data and reshape to 2D
sub1_cs_mask_img = lm.apply_mask(sub1_cs_img, mask)
sub1_cs_mask_img_2d = lm.batch_convert_2d_based(sub1_cs_mask_img, shape)
# sub1_cs_mask_img_2d = lm.batch_convert_2d(sub1_cs_mask_img)

# second step: run linear regression to get betas:
all_betas = {}
for key, img in sub1_cs_mask_img_2d.iteritems():
    # img_2d = np.reshape(img, (-1, img.shape[-1]))
    Y = img.T
    all_betas[key] = npl.pinv(design_matrix[key]).dot(Y)
print("Getting betas from linear regression: complete!")