Exemple #1
0
    def save(self, config, configs_child):

        if config.experiment.task_params is None or config.experiment.task_params['type'] == 'run':

            if isinstance(config.experiment_data['fig'], list):

                for fig_id, fig in enumerate(config.experiment_data['fig']):

                    if config.experiment.method == Method.scatter:
                        item = config.experiment_data['item'][fig_id]
                        config.experiment.method_params.pop('items', None)
                        config.experiment.method_params.pop('x_ranges', None)
                        config.experiment.method_params.pop('y_ranges', None)
                        config.experiment.method_params['item'] = item

                    file_name = config.experiment_data['item'][fig_id]
                    fn = get_save_path(config) + '/' + file_name
                    save_figure(fn, fig)
            else:

                if config.experiment.method == Method.scatter_comparison:
                    config.experiment.method_params.pop('items', None)
                    config.experiment.method_params.pop('aux', None)
                    config.experiment.method_params.pop('data_bases', None)
                    config.experiment.method_params.pop('x_ranges', None)
                    config.experiment.method_params.pop('y_ranges', None)

                file_name = 'regular'
                fn = get_save_path(config) + '/' + file_name
                save_figure(fn, config.experiment_data['fig'])

        elif config.experiment.task_params['type'] == 'prepare':
            pass
Exemple #2
0
def load_table_dict(config):
    fn_pkl = get_save_path(config) + '/' + get_file_name(config) + '.pkl'
    fn_xlsx = get_save_path(config) + '/' + get_file_name(config) + '.xlsx'
    if os.path.isfile(fn_pkl):
        table_dict = load_table_dict_pkl(config)
    elif os.path.isfile(fn_xlsx):
        table_dict = load_table_dict_xlsx(config)
    return table_dict
Exemple #3
0
def save_figure(config, fig, prefix=''):
    if prefix == '':
        fn = get_save_path(config) + '/' + \
             config.setup.get_file_name()
    else:
        fn = get_save_path(config) + '/' + \
             prefix + '_' + config.setup.get_file_name()
    plotly.offline.plot(fig, filename=fn + '.html', auto_open=False)
    pio.write_image(fig, fn + '.png')
    pio.write_image(fig, fn + '.pdf')
Exemple #4
0
 def is_result_exist(self, config, configs_child):
     fn = get_save_path(config) + '/' + \
         get_file_name(config) + '.*'
     if glob.glob(fn):
         return True
     else:
         return False
Exemple #5
0
 def is_result_exist(self, config, configs_child):
     file_name = 'regular'
     fn = get_save_path(config) + '/' + \
         file_name + '.pdf'
     if Path(fn).is_file():
         return True
     else:
         return False
Exemple #6
0
def load_table_dict_pkl(config):
    fn = get_save_path(config) + '/' + get_file_name(config) + '.pkl'
    if os.path.isfile(fn):
        f = open(fn, 'rb')
        table_dict = pickle.load(f)
        f.close()
        return table_dict
    else:
        raise IOError(f'No such file: {fn}')
Exemple #7
0
def save_table_dict(config, table_dict):
    fn = get_save_path(config) + '/' + \
         config.setup.get_file_name() + '.xlsx'
    df = pd.DataFrame(table_dict)
    writer = pd.ExcelWriter(fn, engine='xlsxwriter')
    df.to_excel(writer, index=False)
    writer.save()

    fn = get_save_path(config) + '/' + \
        config.setup.get_file_name() + '.csv'
    with open(fn, 'w') as csvfile:
        writer = csv.DictWriter(csvfile,
                                fieldnames=table_dict.keys(),
                                lineterminator='\n')
        writer.writeheader()
        for id in range(0, len(list(table_dict.values())[0])):
            tmp_dict = {}
            for key, values in table_dict.items():
                tmp_dict[key] = values[id]
            writer.writerow(tmp_dict)
Exemple #8
0
def load_table_dict(config):
    fn = get_save_path(config) + '/' + get_file_name(config) + '.xlsx'
    if os.path.isfile(fn):
        df = pd.read_excel(fn)
        tmp_dict = df.to_dict()
        table_dict = {}
        for key in tmp_dict:
            curr_dict = tmp_dict[key]
            table_dict[key] = list(curr_dict.values())
        return table_dict
    else:
        raise IOError(f'No such file: {fn}')
Exemple #9
0
def cpg_proc_special_clock_linreg_dev(
    data,
    annotations,
    attributes,
    file,
    params=None,
):
    if os.path.isfile(file):

        head, tail = os.path.split(file)
        fn = os.path.splitext(tail)[0]
        ext = os.path.splitext(tail)[1]

        config_root = Config(data=copy.deepcopy(data),
                             experiment=Experiment(
                                 type=DataType.cpg,
                                 task=Task.clock,
                                 method=Method.linreg,
                                 params=copy.deepcopy(params)),
                             annotations=copy.deepcopy(annotations),
                             attributes=copy.deepcopy(attributes),
                             is_run=True,
                             is_root=True)
        root = Node(name=str(config_root), config=config_root)

        config_child = Config(data=copy.deepcopy(data),
                              experiment=Experiment(type=DataType.cpg,
                                                    task=Task.table,
                                                    method=Method.special,
                                                    params={'file_name': fn}),
                              annotations=copy.deepcopy(annotations),
                              attributes=copy.deepcopy(attributes),
                              is_run=False,
                              is_root=False)

        Node(name=str(config_child), config=config_child, parent=root)

        build_tree(root)

        new_file = get_save_path(config_child) + '/' + \
             get_file_name(config_child) + ext

        copyfile(file, new_file)

        calc_tree(root)

    else:
        raise FileNotFoundError(f'File {file} not found.')
Exemple #10
0
 def save(self, config, configs_child):
     fn = get_save_path(config) + '/' + \
         get_file_name(config)
     save_figure(fn, config.experiment_data['fig'])
Exemple #11
0
 def save(self, config, configs_child):
     fn = get_save_path(config) + '/' + \
         get_file_name(config)
     save_table_dict(fn, config.metrics)
Exemple #12
0
def betas_clock_special(
    data,
    annotations,
    attributes,
    file,
    method_params=None,
):
    """
        Producing epigentic clock, using best CpGs which are provided in input file.

        Epigentic clock represents as table:
        Each row corresponds to clocks, which are built on all CpGs from the previous rows including the current row.
        Columns:

        * item: CpG id.
        * aux: gene, on which CpG is mapped.
        * R2: determination coefficient of linear regression between real and predicted target observable.
          A statistical measure of how well the regression line approximates the data points.
        * r: correlation coefficient of linear regression between real and predicted target observable.
        * evs: explained variance regression score.
        * mae: mean absolute error regression loss.
        * rmse: root mean square error

        Possible parameters of experiment:

        * ``'type'``: type of clocks. \n
          Possible options: \n
          ``'all'``: iterative building of clocks starting from one element in the model,
          ending with ``'size'`` elements in the model. \n
          ``'single '``: building of clocks only with ``'size'`` elements in the model. \n
          ``'deep'``: iterative building of clocks starting from one element in the model,
          ending with ``'size'`` elements in the model, but choosing all possible combinations from ``'size'`` elements.
        * ``'part'``: the proportion of considered number of subject in the test set. From ``0.0`` to ``1.0``.
        * ``'size'``: maximum number of exogenous variables in a model.
        * ``'runs'`` number of bootstrap runs in model

        :param data: pdm.Data instance, which specifies information about dataset.
        :param annotations: pdm.Annotations instance, which specifies subset of CpGs.
        :param attributes: pdm.Attributes instance, which specifies information about subjects.
        :param method_params: parameters of experiment.
     """

    if os.path.isfile(file):

        head, tail = os.path.split(file)
        fn = os.path.splitext(tail)[0]
        ext = os.path.splitext(tail)[1]

        config_root = Config(data=copy.deepcopy(data),
                             experiment=Experiment(
                                 data=DataType.betas,
                                 task=Task.clock,
                                 method=Method.linreg,
                                 method_params=copy.deepcopy(method_params)),
                             annotations=copy.deepcopy(annotations),
                             attributes=copy.deepcopy(attributes),
                             is_run=True,
                             is_root=True)
        root = Node(name=str(config_root), config=config_root)

        config_child = Config(data=copy.deepcopy(data),
                              experiment=Experiment(
                                  data=DataType.betas,
                                  task=Task.table,
                                  method=Method.special,
                                  method_params={'file_name': fn}),
                              annotations=copy.deepcopy(annotations),
                              attributes=copy.deepcopy(attributes),
                              is_run=False,
                              is_root=False)

        Node(name=str(config_child), config=config_child, parent=root)

        build_tree(root)

        new_file = get_save_path(config_child) + '/' + \
            get_file_name(config_child) + ext

        copyfile(file, new_file)

        calc_tree(root)

    else:
        raise FileNotFoundError(f'File {file} not found.')