[973ab6]: / Features / FeatureParser.py

Download this file

150 lines (134 with data), 6.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# Copyright 2017 University of Westminster. 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.
# ==============================================================================
""" It reads and parses the variables, then it generate features.
"""
from typing import List, TypeVar, Dict
import sys
import pandas as pd
import numpy as np
import multiprocessing as mp
from functools import partial
from collections import Counter
from ReadersWriters.ReadersWriters import ReadersWriters
import logging
from Features.FeatureParserThread import FeatureParserThread
from Configs.CONSTANTS import CONSTANTS
PandasDataFrame = TypeVar('DataFrame')
NumpyNdarray = TypeVar('ndarray')
__author__ = "Mohsen Mesgarpour"
__copyright__ = "Copyright 2016, https://github.com/mesgarpour"
__credits__ = ["Mohsen Mesgarpour"]
__license__ = "GPL"
__version__ = "1.1"
__maintainer__ = "Mohsen Mesgarpour"
__email__ = "mohsen.mesgarpour@gmail.com"
__status__ = "Release"
class FeatureParser:
def __init__(self,
variables_settings: PandasDataFrame,
output_path: str,
output_table: str):
"""Initialise the objects and constants.
:param variables_settings:
:param output_path: the output path.
:param output_table: the output table name.
"""
self.__logger = logging.getLogger(CONSTANTS.app_name)
self.__logger.debug(__name__)
self.__variables_settings = variables_settings
self.__output_path = output_path
self.__output_table = output_table
self.__readers_writers = ReadersWriters()
self.__FeatureParserThread = FeatureParserThread()
def generate(self,
history_table: str,
features: PandasDataFrame,
variables: PandasDataFrame,
prevalence: Dict) -> PandasDataFrame:
"""
:param history_table: the source table alias name (a.k.a. history table name) that features belong to
(e.g. inpatient, or outpatient).
:param features: the output features.
:param variables: the input variables.
:param prevalence: the prevalence dictionary of values for all the variables.
:return: the output features.
"""
variables_settings = self.__variables_settings[self.__variables_settings["Table_History_Name"] == history_table]
for _, row in variables_settings.iterrows():
self.__logger.info("variable: " + row["Variable_Name"] + " ...")
if not pd.isnull(row["Variable_Aggregation"]):
postfixes = row["Variable_Aggregation"].replace(' ', '').split(',')
# aggregate stats
features_temp = self.__aggregate(
variables[row["Variable_Name"]], row["Variable_Type_Original"],
postfixes, prevalence[row["Variable_Name"]])
for p in range(len(postfixes)):
# feature name
feature_name = row["Variable_Name"] + "_" + postfixes[p]
# set
features[feature_name] = features_temp[:, p]
else:
# init and replace none by zero
features_temp = np.nan_to_num(variables[row["Variable_Name"]])
features_temp = np.where(features_temp == np.array(None), 0, features_temp)
# set
features[row["Variable_Name"]] = features_temp
return features
def __aggregate(self,
variable: PandasDataFrame,
variable_type: str,
postfixes: str,
prevalence: Dict) -> NumpyNdarray:
"""
:param variable: the input variable.
:param variable_type: the type of input variable.
:param postfixes: name of the aggregation functions.
:param prevalence: the prevalence dictionary of values for all the variables.
:return: the aggregated variable.
"""
try:
with mp.Pool() as pool:
features_temp = pool.map(
partial(self.__FeatureParserThread.aggregate_cell, postfixes, variable_type, prevalence), variable)
except ValueError as exception:
self.__logger.error(__name__ + " - Invalid configuration(s): " + str(exception))
sys.exit()
features_temp = np.asarray(features_temp)
return features_temp
def prevalence(self,
variable: PandasDataFrame,
variable_name: str) -> List:
"""
:param variable: the input variable.
:param variable_name: the name of the input variable.
:return: the prevalence of values for all the variables.
"""
try:
with mp.Pool() as pool:
prevalence_temp = pool.map(
partial(self.__FeatureParserThread.prevalence_cell), variable)
except ValueError as exception:
self.__logger.error(__name__ + " - Invalid configuration(s): " + str(exception))
sys.exit()
prevalence_temp = [sub2 for sub1 in prevalence_temp for sub2 in sub1]
prevalence = Counter(prevalence_temp).most_common()
self.__readers_writers.save_text(self.__output_path, self.__output_table,
[variable_name, '; '.join([str(p[0]) + ":" + str(p[1]) for p in prevalence])],
append=True, ext="txt")
prevalence = [p[0] for p in prevalence]
return prevalence