Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SY - inference module with examples #68

Merged
merged 17 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 63 additions & 25 deletions brails/imputers/knn_imputer/knn_imputer.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,33 +71,54 @@ class KnnImputer(Imputation):
Methods:





"""

def __init__(self):
pass

def impute(
self,
input_inventory: AssetInventory,
n_possible_worlds=1,
create_correlation=True,
exclude_features=[],
seed=1,
batch_size=50,
k_nn=5,
) -> AssetInventory:
def __init__(self,
input_inventory: AssetInventory,
n_possible_worlds=1,
create_correlation=True,
exclude_features=[],
seed=1,
batch_size=50,
k_nn=5,
nbldg_per_cluster=500
):
self.input_inventory = input_inventory
self.n_pw = n_possible_worlds
self.batch_size = batch_size
self.create_correlation = create_correlation
self.exclude_features = exclude_features
self.seed = seed
self.k_nn = k_nn # knn
if create_correlation:
self.batch_size = batch_size
self.k_nn = k_nn
self.nbldg_per_cluster = nbldg_per_cluster

# def impute(
# self,
# input_inventory: AssetInventory,
# n_possible_worlds=1,
# create_correlation=True,
# exclude_features=[],
# seed=1,
# batch_size=50,
# k_nn=5,
# nbldg_per_cluster=500
# ) -> AssetInventory:

def impute(self)-> AssetInventory:

#self.n_pw = n_possible_worlds
#self.batch_size = batch_size
#self.seed = seed
#self.k_nn = k_nn # knn

if self.create_correlation:
self.gen_method = "sequential"
else:
self.gen_method = "non-sequential"

exclude_features = self.exclude_features
nbldg_per_cluster = self.nbldg_per_cluster
input_inventory = self.input_inventory
#
# set seed
#
Expand Down Expand Up @@ -141,7 +162,7 @@ def impute(
bldg_properties_df = bldg_properties_df.drop(columns=column_entirely_missing)
mask = mask.drop(columns=column_entirely_missing)

if len(column_entirely_missing) > 1:
if len(column_entirely_missing) >= 1:
print(
"Features with no reference data cannot be imputed. Removing them from the imputation target: "
+ ", ".join(list(column_entirely_missing))
Expand Down Expand Up @@ -175,7 +196,7 @@ def impute(
cluster_ids, n_cluster = self.clustering(
bldg_properties_encoded,
bldg_geometries_df,
nbldg_per_cluster=500,
nbldg_per_cluster=nbldg_per_cluster,
seed=self.seed,
)

Expand Down Expand Up @@ -347,9 +368,10 @@ def category_in_df_to_indices(self, bldg_properties_df, mask):
idxs = np.array(mask[column] == False) # removing nans # noqa: E712

# if not is_numeric_dtype(values):

if math.isnan(sum(pd.to_numeric(values[idxs], errors="coerce"))):
is_category[column] = True
elif len(np.unique(values)) < 20:
elif len(np.unique(values[~np.isnan(values.astype(float))])) < 20:
is_category[column] = True
else:
is_category[column] = False
Expand Down Expand Up @@ -515,7 +537,16 @@ def sequential_imputer(
# sample indices
#

distances[distances==0] = np.min(distances[distances!=0])/100 # if dist is zero

ndup = np.sum(distances==0)
if ndup>0:
print(f'Warning: Found {ndup} duplicated assets that has distance 0.')


invdistance = 1 / distances


row_sums = invdistance.sum(axis=1)
weights = invdistance / row_sums[:, np.newaxis]

Expand All @@ -536,9 +567,16 @@ def sequential_imputer(
# global_nb = cluster_idx[missing_idx[nb]]
bldg_idx = bldg_inde_subset[missing_idx[nb]]

surrounding_building_sample = np.random.choice(
building_ids[nb, :], size=1, replace=True, p=weights[nb, :]
)
try:
surrounding_building_sample = np.random.choice(
building_ids[nb, :], size=1, replace=True, p=weights[nb, :]
)
except Exception as e:
print(weights[nb, :])
print(row_sums[nb, np.newaxis])
print(invdistance[nb, :])
exit(-1)

closet_building = building_ids[nb, np.argmin(weights[nb, :])]

imputed_index_sample = mytrainY[surrounding_building_sample]
Expand Down
34 changes: 34 additions & 0 deletions brails/inferers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2024 The Regents of the University of California
#
# This file is part of BRAILS++.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. 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.
#
# 3. Neither the name of the copyright holder 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 HOLDER 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.
#
# You should have received a copy of the BSD 3-Clause License along with
# BRAILS. If not, see <http://www.opensource.org/licenses/>.
34 changes: 34 additions & 0 deletions brails/inferers/hazus_inferer/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2024 The Regents of the University of California
#
# This file is part of BRAILS++.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. 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.
#
# 3. Neither the name of the copyright holder 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 HOLDER 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.
#
# You should have received a copy of the BSD 3-Clause License along with
# BRAILS. If not, see <http://www.opensource.org/licenses/>.
Loading
Loading