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

fix(l2g): calculate_feature_missingness_rate counts features annotated with 0 as incomplete #364

Merged
merged 3 commits into from
Dec 18, 2023
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
7 changes: 6 additions & 1 deletion src/otg/dataset/l2g_feature_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ def calculate_feature_missingness_rate(
raise ValueError("No features found")

return {
feature: (self._df.filter(self._df[feature].isNull()).count() / total_count)
feature: (
self._df.filter(
(self._df[feature].isNull()) | (self._df[feature] == 0)
).count()
/ total_count
)
for feature in self.features_list
}

Expand Down
7 changes: 3 additions & 4 deletions src/otg/method/l2g/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
import itertools
from typing import TYPE_CHECKING, Any, Dict

import wandb
from pyspark import keyword_only
from pyspark.ml.evaluation import (
BinaryClassificationEvaluator,
Evaluator,
MulticlassClassificationEvaluator,
)
from pyspark.ml.param import Param, Params, TypeConverters
from wandb.sdk.wandb_run import Run

if TYPE_CHECKING:
from pyspark.sql import DataFrame
from wandb.wandb_run import Run


class WandbEvaluator(Evaluator):
Expand Down Expand Up @@ -124,11 +123,11 @@ def getspark_ml_evaluator(self: WandbEvaluator) -> Evaluator:
"""
return self.getOrDefault(self.spark_ml_evaluator)

def getwandb_run(self: WandbEvaluator) -> wandb.sdk.wandb_run.Run:
def getwandb_run(self: WandbEvaluator) -> Run:
"""Get the wandb_run parameter.

Returns:
wandb.sdk.wandb_run.Run: Wandb run object.
Run: Wandb run object.
"""
return self.getOrDefault(self.wandb_run)

Expand Down
7 changes: 4 additions & 3 deletions src/otg/method/l2g/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Type

import wandb
from pyspark.ml import Pipeline, PipelineModel
from pyspark.ml.evaluation import (
BinaryClassificationEvaluator,
MulticlassClassificationEvaluator,
)
from pyspark.ml.feature import StringIndexer, VectorAssembler
from pyspark.ml.tuning import ParamGridBuilder
from wandb.data_types import Table
from wandb.sdk import init as wandb_init
from wandb.wandb_run import Run
from xgboost.spark.core import SparkXGBClassifierModel

Expand Down Expand Up @@ -126,7 +127,7 @@ def log_to_wandb(
## Track feature importance
wandb_run.log({"importances": self.get_feature_importance()})
## Track training set
training_table = wandb.Table(dataframe=training_data.df.toPandas())
training_table = Table(dataframe=training_data.df.toPandas())
wandb_run.log({"trainingSet": training_table})
# Count number of positive and negative labels
gs_counts_dict = {
Expand Down Expand Up @@ -224,7 +225,7 @@ def evaluate(
)

if wandb_run_name and training_data:
run = wandb.init(
run = wandb_init(
project=self.wandb_l2g_project_name,
config=hyperparameters,
name=wandb_run_name,
Expand Down
24 changes: 24 additions & 0 deletions tests/dataset/test_l2g.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,27 @@ def test_remove_false_negatives(spark: SparkSession) -> None:
)

assert observed_df.collect() == expected_df.collect()


def test_calculate_feature_missingness_rate(spark: SparkSession) -> None:
"""Test L2GFeatureMatrix.calculate_feature_missingness_rate."""
fm = L2GFeatureMatrix(
_df=spark.createDataFrame(
[
(1, "gene1", 100.0, None),
(2, "gene2", 1000.0, 0.0),
],
"studyLocusId LONG, geneId STRING, distanceTssMean DOUBLE, distanceTssMinimum DOUBLE",
),
_schema=L2GFeatureMatrix.get_schema(),
)

expected_missingness = {"distanceTssMean": 0.0, "distanceTssMinimum": 1.0}
observed_missingness = fm.calculate_feature_missingness_rate()
assert isinstance(observed_missingness, dict)
assert len(observed_missingness) == len(
fm.features_list # type: ignore
), "Missing features in the missingness rate dictionary."
assert (
observed_missingness == expected_missingness
), "Missingness rate is incorrect."