Ejemplo n.º 1
0
def write_np_colormask(img, path, num_labels=8):
    create_directories_for_file_name(path)
    im_out = sitk.GetArrayFromImage(img)
    im_out = np.asarray(im_out, dtype=np.float32)
    im_out -= np.amin(im_out)
    im_out /= (num_labels - 1)
    #im_out /= np.amax(im_out)
    plt.imsave(path, im_out, cmap='tab20')
Ejemplo n.º 2
0
def save_string_txt(string, file_name):
    """
    Saves a string as a text file.
    :param string: The string to write.
    :param file_name: The file name.
    """
    create_directories_for_file_name(file_name)
    with open(file_name, 'w') as file:
        file.write(string)
Ejemplo n.º 3
0
 def save(self, image_canvas_merged, filename):
     """
     Save the merged image canvas to the filename.
     :param image_canvas_merged: A merged image canvas (returned by merge_image_canvas).
     :param filename: The filename to save the image canvas to.
     """
     create_directories_for_file_name(filename)
     image_canvas_merged_sitk = np_to_sitk(np.transpose(image_canvas_merged, [1, 2, 0]), is_vector=True)
     write(image_canvas_merged_sitk, filename, compress=True)
Ejemplo n.º 4
0
 def save(self, image_canvas_merged, filename):
     """
     Save the merged image canvas to the filename.
     :param image_canvas_merged: A merged image canvas (returned by merge_image_canvas).
     :param filename: The filename to save the image canvas to.
     """
     create_directories_for_file_name(filename)
     plt.tight_layout()
     plt.savefig(filename, dpi=self.dpi)
     plt.close()
Ejemplo n.º 5
0
def save_list_txt(string_list, file_name):
    """
    Saves string list as a text file. Each list entry is a new line.
    :param string_list: The string list to write.
    :param file_name: The file name.
    """
    create_directories_for_file_name(file_name)
    with open(file_name, 'w') as file:
        string_list_with_endl = [string + '\n' for string in string_list]
        file.writelines(string_list_with_endl)
Ejemplo n.º 6
0
def write(img, path, compress=True):
    """
    Write a volume to a file path.

    :param img: the volume
    :param path: the target path
    :return:
    """
    create_directories_for_file_name(path)
    writer = sitk.ImageFileWriter()
    writer.Execute(img, path, compress)
Ejemplo n.º 7
0
def save_json(obj, file_name, *args, **kwargs):
    """
    Saves an object as a json file.
    :param obj: The object to save.
    :param file_name: The filename.
    :param args: args to pass to json.dump()
    :param kwargs: kwargs to pass to json.dump()
    :return:
    """
    create_directories_for_file_name(file_name)
    with open(file_name, 'w') as f:
        json.dump(obj, f, *args, **kwargs)
Ejemplo n.º 8
0
def write(img, path, compress=True):
    """
    Write an sitk image to a file path.
    :param img: The sitk image.
    :param path: The target path.
    :param compress: If true, compress the file.
    """
    create_directories_for_file_name(path)
    writer = sitk.ImageFileWriter()
    writer.SetFileName(path)
    writer.SetUseCompression(compress)
    writer.Execute(img)
def save_points_csv(landmarks_dict, filename, id_preprocessing=None):
    create_directories_for_file_name(filename)
    with open(filename, 'w') as csv_file:
        writer = csv.writer(csv_file)
        for key, values in sorted(landmarks_dict.items()):
            current_id = key
            if id_preprocessing is not None:
                current_id = id_preprocessing(current_id)
            row = [current_id]
            for landmark in values:
                row += list(landmark.coords)
            writer.writerow(row)
Ejemplo n.º 10
0
def save_list_csv(l, file_name, header=None, **kwargs):
    create_directories_for_file_name(file_name)
    with open(file_name, 'w') as file:
        writer = csv.writer(file, **kwargs)
        if header is not None:
            writer.writerow(header)
        for value in l:
            if isinstance(value, list):
                writer.writerow(value)
            elif isinstance(value, tuple):
                writer.writerow(list(value))
            else:
                writer.writerow([value])
Ejemplo n.º 11
0
def save_dict_csv(d, file_name, header=None, **kwargs):
    create_directories_for_file_name(file_name)
    with open(file_name, 'w') as file:
        writer = csv.writer(file, **kwargs)
        if header is not None:
            writer.writerow(header)
        for key, value in sorted(d.items()):
            if isinstance(value, list):
                writer.writerow([key] + value)
            elif isinstance(value, tuple):
                writer.writerow([key] + list(value))
            else:
                writer.writerow([key, value])
Ejemplo n.º 12
0
def save_points_idl(landmarks_dict, filename, id_preprocessing=None):
    create_directories_for_file_name(filename)
    with open(filename, 'w') as file:
        for key, values in sorted(landmarks_dict.items()):
            current_id = key
            if id_preprocessing is not None:
                current_id = id_preprocessing(current_id)
            string = '"' + current_id + '": '
            for landmark in values:
                point = landmark.coords
                if len(point) == 2:
                    string += '(' + '{:.3f}'.format(point[0]) + ',' + '{:.3f}'.format(point[1]) + ',0),'
                else:
                    string += '(' + '{:.3f}'.format(point[0]) + ',' + '{:.3f}'.format(point[1]) + ',' + '{:.3f}'.format(point[2]) + '),'

            string = string[:-1] + '\n'
            file.write(string)
Ejemplo n.º 13
0
def save_dict_csv(d, file_name, header=None):
    """
    Saves a dictionary as a .csv file. The key is written as the first column. If the value is a list or a tuple,
    each entry is written as a consecutive column. Otherwise, the value is written as the second column
    :param d: The dictionary to write
    :param file_name: The file name.
    :param header: If given, this list will be written as a header.
    """
    create_directories_for_file_name(file_name)
    with open(file_name, 'w') as file:
        writer = csv.writer(file)
        if header is not None:
            writer.writerow(header)
        for key, value in sorted(d.items()):
            if isinstance(value, list):
                writer.writerow([key] + value)
            elif isinstance(value, tuple):
                writer.writerow([key] + list(value))
            else:
                writer.writerow([key, value])
Ejemplo n.º 14
0
def save_list_csv(l, file_name, header=None, **kwargs):
    """
    Saves a list as a .csv file. If the list entries are a list or a tuple,
    each entry is written as a consecutive column. Otherwise, the value is written as the second column
    :param l: The (possibly nested) list to write
    :param file_name: The file name.
    :param header: If given, this list will be written as a header.
    """
    create_directories_for_file_name(file_name)
    with open(file_name, 'w') as file:
        writer = csv.writer(file, **kwargs)
        if header is not None:
            writer.writerow(header)
        for value in l:
            if isinstance(value, list):
                writer.writerow(value)
            elif isinstance(value, tuple):
                writer.writerow(list(value))
            else:
                writer.writerow([value])