-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ETL split into extract, transform, load so it's easier to follow the …
…scope of each portion in the code
- Loading branch information
Showing
4 changed files
with
115 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #-*- coding: utf-8 -*- | ||
| """ | ||
| load.py | ||
| """ | ||
|
|
||
| import typing | ||
|
|
||
| import numpy as np | ||
| from pyspark.sql import DataFrame | ||
|
|
||
| def load(data: DataFrame, split=[0.99, 0.005, 0.005]): | ||
| category_dict = { | ||
| key: build_dict(data, key) for key in ["treatment", "target"] | ||
| } | ||
| splits = data.randomSplit(split, seed=42) | ||
| trainx, valx, testx = (trim(dset, "spectrogram") for dset in splits) | ||
| trainy, valy, testy = ( | ||
| [ | ||
| np.array(dset.select("treatment").collect()).squeeze(), | ||
| np.array(dset.select("target").collect()).squeeze() | ||
| ] for dset in splits | ||
| ) | ||
|
|
||
| return ((trainx, trainy), (valx, valy), (testx, testy), category_dict) | ||
|
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| #-*- coding: utf-8 -*- | ||
| """ | ||
| transform.py | ||
| """ | ||
|
|
||
| import typing | ||
|
|
||
| from pyspark.sql import DataFrame, SparkSession | ||
| from sklearn.preprocessing import OneHotEncoder | ||
|
|
||
| def merge_redundant_labels(dataframe: DataFrame) -> DataFrame: | ||
| """ | ||
| Merge redundant labels in the 'treatment' column of the dataframe. This | ||
| step is inefficient but necessary due to inconsistent naming conventions | ||
| used in the MATLAB onekey processing. | ||
| """ | ||
| dataframe.select("treatment").replace("virus", "cpv") \ | ||
| .replace("cont", "pbs") \ | ||
| .replace("control", "pbs") \ | ||
| .replace("dld", "pbs").distinct() | ||
| return dataframe | ||
|
|
||
| def transform(spark: SparkSession, dataframe: DataFrame, keys: list) \ | ||
| -> DataFrame: | ||
| """ | ||
| """ | ||
| dataframe = merge_redundant_labels(dataframe) | ||
| dataframe = dataframe.withColumn( | ||
| "index", functions.monotonically_increasing_id() | ||
| ) | ||
| bundle = {key: [ | ||
| arr.tolist() | ||
| for arr in OneHotEncoder(sparse_output=False) \ | ||
| .fit_transform(dataframe.select(key).collect()) | ||
| ] for key in keys | ||
| } | ||
|
|
||
| bundle = [dict(zip(bundle.keys(), values)) | ||
| for values in zip(*bundle.values())] | ||
| schema = types.StructType([ | ||
| types.StructField(key, types.ArrayType(types.FloatType()), True) | ||
| for key in keys | ||
| ]) | ||
| newframe = spark.createDataFrame(bundle, schema=schema).withColumn( | ||
| "index", functions.monotonically_increasing_id() | ||
| ) | ||
|
|
||
| for key in keys: | ||
| dataframe = dataframe.withColumnRenamed(key, f"{key}_str") | ||
| dataframe = dataframe.join(newframe, on="index", how="inner") | ||
|
|
||
| return dataframe |