featureselection

#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
#  Copyright 2019 The FATE Authors. All Rights Reserved.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import json
import os
import subprocess
import sys
import time

# from examples.load_file.load_file import load_file

home_dir = os.path.split(os.path.realpath(__file__))[0]
config_path = home_dir + '/conf'
data_path = home_dir + '/../data'
load_file_program = home_dir + '/../load_file/load_file.py'

# data_set = 'breast'
data_set = 'default_credit'
# data_set = 'give_credit'

mode = 'fit'
# mode = 'transform'


def make_config_file(work_mode, job_id, role, guest_partyid, host_partyid):
    with open(config_path + '/{}_runtime_conf.json'.format(role), 'r', encoding='utf-8') as load_f:
        role_config = json.load(load_f)

    if role == 'guest':
        role_config['local']['party_id'] = guest_partyid
        data_suffix = 'b'
    else:
        role_config['local']['party_id'] = host_partyid
        data_suffix = 'a'

    role_config['role']['host'][0] = host_partyid
    role_config['role']['guest'][0] = guest_partyid
    role_config['WorkFlowParam']['work_mode'] = int(work_mode)

    # role_config['FeatureSelectionParam']['method'] = mode
    role_config['WorkFlowParam']['train_input_table'] = "{}_{}_{}".format(data_set, role, job_id)

    # guest_config_path = config_path + '/guest_runtime_conf.json_' + str(job_id)
    role_config_path = "{}/{}_runtime_conf.json_{}".format(
        config_path, role, job_id
    )

    with open(role_config_path, 'w', encoding='utf-8') as json_file:
        json.dump(role_config, json_file, ensure_ascii=False)

    with open(config_path + '/load_file.json', 'r', encoding='utf-8') as load_f:
        load_config = json.load(load_f)
    load_config['work_mode'] = work_mode

    # load_config['file'] = data_path + '/' + data_set + '_b.csv'
    load_config['file'] = "{}/{}_{}.csv".format(data_path, data_set, data_suffix)

    load_config['table_name'] = "{}_{}_{}".format(data_set, role, job_id)

    # load_file_role = config_path + '/load_file.json_guest_' + str(job_id)
    load_file_path = "{}/load_file.json_{}_{}".format(config_path, role, job_id)

    with open(load_file_path, 'w', encoding='utf-8') as json_file:
        json.dump(load_config, json_file, ensure_ascii=False)

    return role_config_path, load_file_path


def attach_all_result_log(running_process, job_id, role):
    while True:
        poll = running_process.poll()
        if poll is None:
            print('Workflow is still running')
            time.sleep(5)
        else:
            break

    log_dir = home_dir + '/../../logs/{}'.format(job_id)

    role = role.lower()
    if role == 'guest':
        role_list = ['Guest', 'guest', 'GUEST']
    else:
        role_list = ['Host', 'host', 'HOST']

    result_log_list = []
    for log_file in os.listdir(log_dir):
        # print('current log_file: {}'.format(log_file))
        with open(log_dir + '/' + log_file, 'r', encoding='utf-8') as f:
            for log_line in f:
                if 'Result' in log_line:
                    for r in role_list:
                        if r in log_line:
                            result_log_list.append(log_line)
                            break

    log_list = sorted(result_log_list, key=get_time)
    for log_line in log_list:
        print(log_line)


def get_time(log_line):
    time_str = log_line.split(' - ')[0]
    time_str = time_str[1:]
    time_str.strip()

    time_sec, time_mili_sec = time_str.split(',')

    timestamp = time.mktime(time.strptime(time_sec, '%Y-%m-%d %H:%M:%S'))
    timestamp += float(time_mili_sec) / 1000
    return timestamp


def load_file(load_file_path):
    load_process = subprocess.Popen(["python",
                                     load_file_program,
                                     "-c",
                                     load_file_path,
                                     ])
    # load_process.communicate()
    returncode = load_process.wait()
    print("Load file return code : {}".format(returncode))


if __name__ == '__main__':
    work_mode = int(sys.argv[1])
    jobid = sys.argv[2]
    role = sys.argv[3]
    guest_partyid = int(sys.argv[4])
    host_partyid = int(sys.argv[5])

    role_config_path, load_file_path = make_config_file(work_mode, jobid, role, guest_partyid, host_partyid)

    load_file(load_file_path)

    work_path = home_dir + '/../../workflow/hetero_feature_selection_workflow/' \
                           'hetero_feature_selection_{}_workflow.py'.format(role)

    p = subprocess.Popen(["python",
                          work_path,
                          "-c",
                          role_config_path,
                          "-j",
                          jobid
                          ])

    attach_all_result_log(p, jobid, role)

 

转载于:https://www.cnblogs.com/suntorlearning/p/11337582.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Matlab中好用的数据降维和特征选择工具包 Copyright (c) 2018, Giorgio Roffo All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution * Neither the name of University of Glasgow nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值