PROJECT 099 · PYTHON SOURCE
Biological measurement plausibility
Study-specific code. Shared modules, dependency versions, and reproduction instructions are included in all project files.
Download Python file ↓from pathlib import Path
import sys
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT))
import numpy as np
import pandas as pd
from scipy.stats import spearmanr
from sklearn.decomposition import PCA
from sklearn.ensemble import IsolationForest
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix, precision_score, recall_score
from portfolio.data import load
from portfolio.constants import *
from portfolio.methods import result, sql, rates, associations, distribution, regression, classification, cluster
from portfolio.engine import execute
META = {'id': 99, 'dataset': 'abalone', 'title': 'Biological measurement plausibility', 'question': 'Which observations violate simple dimensional or mass consistency checks?', 'method': 'Transparent physical measurement screens with explicit denominators.', 'action': 'Inspect original records before excluding extreme or inconsistent specimens.', 'limitations': 'Physical measurements use source scaling. Rings are the modeled target; the source describes an approximate age conversion. Site and animal group identifiers are unavailable, limiting biological generalization. '}
def analyze():
df = load('abalone').copy()
rules={'Nonpositive height':df.height<=0,'Diameter exceeds length':df.diameter>df.length,'Shell weight exceeds whole':df.shell_weight>df.whole_weight,'Shucked weight exceeds whole':df.shucked_weight>df.whole_weight,'Nonpositive ring count':df.rings<=0,'Measured components exceed whole':(df.shucked_weight+df.viscera_weight+df.shell_weight)>df.whole_weight}
t=pd.DataFrame([{'rule':k,'flagged_rows':int(v.sum()),'checked_rows':len(df),'flagged_share':v.mean()} for k,v in rules.items()])
out=result(t,'rule','flagged_rows','Rows flagged','Component weights can differ because of measurement timing and preparation. Flags are review candidates, not established errors.')
return out
if __name__ == "__main__":
execute(Path(__file__).parent, META, analyze)