Skip to content

Commit

Permalink
treetop: improve handling of local SHAP values tab
Browse files Browse the repository at this point in the history
  • Loading branch information
natoscott committed Sep 25, 2024
1 parent 306b0bf commit c214a37
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 46 deletions.
44 changes: 20 additions & 24 deletions src/treetop/pcp/Feature.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ const FeatureFieldData Feature_fields[] = {
[LOCAL_FEATURE] = { .name = "LOCAL_FEATURE", .title = " Important Metrics ", .description = "Most important metrics (features) from local SHAP" },
[LOCAL_IMPORTANCE] = { .name = "LOCAL_IMPORTANCE", .title = "SHAP VALUE ", .description = "SHAP value importance measure" },
[LOCAL_MUTUALINFO] = { .name = "LOCAL_MUTUALINFO", .title = "MUTUALINFO ", .description = "Mutual information for high SHAP value features" },
[OPTMIN_FEATURE] = { .name = "OPTMIN_FEATURE", .title = " Key Metrics for Optimisation ", .description = "Important metrics for optimisation based on minima perturbations" },
[OPTMIN_CHANGE] = { .name = "OPTMIN_CHANGE", .title = "DELTA ", .description = "Change in prediction with minima perturbations" },
[OPTMIN_DIRECTION] = { .name = "OPTMIN_DIRECTION", .title = "DIRECTION", .description = "Direction of change with minima perturbations" },
[OPTMAX_FEATURE] = { .name = "OPTMAXFEATURE", .title = " Key Metrics for Optimisation ", .description = "Important metrics for optimisation based on maxima perturbations" },
[OPTMAX_CHANGE] = { .name = "OPTMAX_CHANGE", .title = "DELTA ", .description = "Change in prediction with maxima perturbations" },
[OPTMAX_DIRECTION] = { .name = "OPTMAX_DIRECTION", .title = "DIRECTION", .description = "Direction of change with maxima perturbations" },
[OPTIM_FEATURE] = { .name = "OPTMIN_FEATURE", .title = " Key Metrics for Optimisation ", .description = "Important metrics for optimisation based on minima perturbations" },
[OPTIM_MIN_MAX] = { .name = "OPTIM_MIN_MAX", .title = "MIN/MAX", .description = "Used minimum or maximum for perturbation" },
[OPTIM_DIFFERENCE] = { .name = "OPTIM_DIFFERENCE", .title = "DELTA ", .description = "Change in prediction from perturbation" },
[OPTIM_MUTUALINFO] = { .name = "OPTIM_MUTUALINFO", .title = "MUTUALINFO ", .description = "Mutual information with the target variable" },
// End of list
};

Expand All @@ -47,8 +45,6 @@ Feature* Feature_new(const Machine* host) {

void Feature_done(Feature* this) {
Row_done(&this->super);
free(this->direction);
free(this->change);
}

static void Feature_delete(Object* cast) {
Expand All @@ -62,10 +58,12 @@ static const char* Feature_name(Row* rp) {
return fp->name;
}

static void Feature_writeChange(const Feature* fp, RichString* str) {
}

static void Feature_writeDirection(const Feature* fp, RichString* str) {
static void Feature_writeMinMax(const Feature* fp, RichString* str) {
char buffer[16]; buffer[16] = '\0';
int shadow = CRT_colors[PROCESS_SHADOW];
size_t n = sizeof(buffer) - 1;
snprintf(buffer, n, "%8s ", fp->min_max);
RichString_appendWide(str, shadow, buffer);
}

static void Feature_writeName(const Feature* fp, RichString* str) {
Expand Down Expand Up @@ -112,31 +110,29 @@ static void Feature_writeField(const Row* super, RichString* str, RowField field
const Feature* fp = (const Feature*) super;

switch ((int)field) {
case LOCAL_MUTUALINFO:
case MODEL_MUTUALINFO:
case OPTIM_MUTUALINFO:
Feature_writeValue(str, fp->mutualinfo);
return;
case LOCAL_IMPORTANCE:
case MODEL_IMPORTANCE:
Feature_writeValue(str, fp->importance);
return;
case LOCAL_MUTUALINFO:
case MODEL_MUTUALINFO:
Feature_writeValue(str, fp->mutualinfo);
case OPTIM_DIFFERENCE:
Feature_writeValue(str, fp->difference);
return;
case OPTMAX_DIRECTION:
case OPTMIN_DIRECTION:
Feature_writeDirection(fp, str);
case OPTMAX_CHANGE:
case OPTMIN_CHANGE:
Feature_writeChange(fp, str);
case OPTIM_MIN_MAX:
Feature_writeMinMax(fp, str);
return;
case LOCAL_FEATURE:
case MODEL_FEATURE:
case OPTMAX_FEATURE:
case OPTMIN_FEATURE:
case OPTIM_FEATURE:
Feature_writeName(fp, str);
return;
default:
break;
}
Feature_writeField(&fp->super, str, field);
}

static int Feature_compareByKey(const Row* v1, const Row* v2, int key) {
Expand Down
4 changes: 2 additions & 2 deletions src/treetop/pcp/Feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ typedef struct Feature_ {
/* default result offset to use for searching metrics */
unsigned int offset;

char* change;
char* direction;
float importance;
float mutualinfo;
float difference;
char min_max[16];
} Feature;

typedef struct FeatureFieldData_ {
Expand Down
50 changes: 46 additions & 4 deletions src/treetop/pcp/FeatureTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,21 @@ in the source distribution for its full text.
#include "pcp/Feature.h"


FeatureTable* FeatureTable_new(Machine* host) {
FeatureTable* FeatureTable_new(Machine* host, FeatureTableType type) {
FeatureTable* this = xCalloc(1, sizeof(FeatureTable));
Object_setClass(this, Class(FeatureTable));

Table* super = &this->super;
Table_init(super, Class(Row), host);

if (type == TABLE_LOCAL_IMPORTANCE)
this->feature = PCP_LOCAL_FEATURES;
else if (type == TABLE_OPTIM_IMPORTANCE)
this->feature = PCP_OPTIM_FEATURES;
else /* (type == TABLE_MODEL_IMPORTANCE) */
this->feature = PCP_MODEL_FEATURES;
this->table_type = type;

return this;
}

Expand All @@ -56,7 +64,7 @@ static inline float Feature_float(int metric, int id, int offset, float fallback
return fallback;
}

static void FeatureTable_updateInfo(Feature* fp, int id, int offset) {
static void FeatureTable_updateModelInfo(Feature* fp, int id, int offset) {
pmAtomValue value;

if (!Metric_instance(PCP_MODEL_FEATURES, id, offset, &value, PM_TYPE_STRING))
Expand All @@ -68,6 +76,35 @@ static void FeatureTable_updateInfo(Feature* fp, int id, int offset) {
fp->mutualinfo = Feature_float(PCP_MODEL_MUTUALINFO, id, offset, 0);
}

static void FeatureTable_updateLocalInfo(Feature* fp, int id, int offset) {
pmAtomValue value;

if (!Metric_instance(PCP_LOCAL_FEATURES, id, offset, &value, PM_TYPE_STRING))
value.cp = xStrdup("<unknown>");
String_safeStrncpy(fp->name, value.cp, sizeof(fp->name));
free(value.cp);

fp->importance = Feature_float(PCP_LOCAL_IMPORTANCE, id, offset, 0);
fp->mutualinfo = Feature_float(PCP_LOCAL_MUTUALINFO, id, offset, 0);
}

static void FeatureTable_updateOptimInfo(Feature* fp, int id, int offset) {
pmAtomValue value;

if (!Metric_instance(PCP_OPTIM_FEATURES, id, offset, &value, PM_TYPE_STRING))
value.cp = xStrdup("<unknown>");
String_safeStrncpy(fp->name, value.cp, sizeof(fp->name));
free(value.cp);

if (Metric_instance(PCP_OPTIM_MIN_MAX, id, offset, &value, PM_TYPE_STRING))
value.cp = xStrdup("<unknown>");
String_safeStrncpy(fp->min_max, value.cp, sizeof(fp->min_max));
free(value.cp);

fp->difference = Feature_float(PCP_OPTIM_DIFFERENCE, id, offset, 0);
fp->mutualinfo = Feature_float(PCP_OPTIM_MUTUALINFO, id, offset, 0);
}

static Feature* FeatureTable_getFeature(FeatureTable* this, int id, bool* preExisting) {
const Table* super = &this->super;
Feature* fp = (Feature*) Hashtable_get(super->table, id);
Expand All @@ -87,12 +124,17 @@ static void FeatureTable_goThroughEntries(FeatureTable* this) {

int id = -1, offset = -1;
/* for every important feature from the model ... */
while (Metric_iterate(PCP_MODEL_FEATURES, &id, &offset)) {
while (Metric_iterate(this->feature, &id, &offset)) {
bool preExisting;
Feature* fp = FeatureTable_getFeature(this, id, &preExisting);
fp->offset = offset >= 0 ? offset : 0;

FeatureTable_updateInfo(fp, id, offset);
if (this->table_type == TABLE_LOCAL_IMPORTANCE)
FeatureTable_updateLocalInfo(fp, id, offset);
else if (this->table_type == TABLE_OPTIM_IMPORTANCE)
FeatureTable_updateOptimInfo(fp, id, offset);
else /* (this->table_type == TABLE_MODEL_IMPORTANCE) */
FeatureTable_updateModelInfo(fp, id, offset);

Row* row = (Row*) fp;
if (!preExisting)
Expand Down
11 changes: 10 additions & 1 deletion src/treetop/pcp/FeatureTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,24 @@ in the source distribution for its full text.
#include <stdbool.h>
#include <sys/types.h>

#include "Metric.h"
#include "Table.h"

typedef enum FeatureTableType_ {
TABLE_MODEL_IMPORTANCE,
TABLE_LOCAL_IMPORTANCE,
TABLE_OPTIM_IMPORTANCE,
} FeatureTableType;

typedef struct FeatureTable_ {
Table super;
Metric feature;
FeatureTableType table_type;
} FeatureTable;

extern const TableClass FeatureTable_class;

FeatureTable* FeatureTable_new(struct Machine_* host);
FeatureTable* FeatureTable_new(struct Machine_* host, FeatureTableType type);

void FeatureTable_done(FeatureTable* this);

Expand Down
1 change: 0 additions & 1 deletion src/treetop/pcp/Metric.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ typedef enum Metric_ {
PCP_LOCAL_MUTUALINFO, /* treetop.server.explaining.local.mutual_information */
PCP_LOCAL_ELAPSED, /* treetop.server.explaining.shap.elapsed_time */
PCP_OPTIM_FEATURES, /* treetop.server.optimising.features */
PCP_OPTIM_INC_DEC, /* treetop.server.optimising.inc_dec */
PCP_OPTIM_MIN_MAX, /* treetop.server.optimising.min_max */
PCP_OPTIM_DIFFERENCE, /* treetop.server.optimising.difference */
PCP_OPTIM_MUTUALINFO, /* treetop.server.optimising.mutual_information */
Expand Down
25 changes: 17 additions & 8 deletions src/treetop/pcp/TreeTop.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ static const char* Platform_metricNames[] = {
[PCP_LOCAL_ELAPSED] = "mmv.treetop.server.explaining.local.elapsed_time",
[PCP_OPTIM_FEATURES] = "mmv.treetop.server.optimising.features",
[PCP_OPTIM_MIN_MAX] = "mmv.treetop.server.optimising.min_max",
[PCP_OPTIM_INC_DEC] = "mmv.treetop.server.optimising.inc_dec",
[PCP_OPTIM_DIFFERENCE] = "mmv.treetop.server.optimising.difference",
[PCP_OPTIM_MUTUALINFO] = "mmv.treetop.server.optimising.mutual_infomation",
[PCP_OPTIM_ELAPSED] = "mmv.treetop.server.optimising.elapsed_time",
Expand Down Expand Up @@ -444,10 +443,6 @@ void Platform_getHostname(char* buffer, size_t size) {
String_safeStrncpy(buffer, hostname, size);
}

void Platform_getRelease(char** string) {
*string = NULL;
}

void Platform_longOptionsUsage(ATTR_UNUSED const char* name) {
printf(
" --host=HOSTSPEC metrics source is PMCD at HOSTSPEC [see PCPIntro(1)]\n"
Expand Down Expand Up @@ -562,13 +557,27 @@ void Platform_addDynamicScreenAvailableColumns(Panel* availableColumns, const ch

ProcessTable* ProcessTable_new(Machine* host, Hashtable* idMatchList) {
(void)idMatchList;
pcp->model_features = FeatureTable_new(host);
pcp->model_features = FeatureTable_new(host, TABLE_MODEL_IMPORTANCE);
return (ProcessTable*) pcp->model_features;
}

void Platform_updateTables(Machine* host) {
pcp->local_features = FeatureTable_new(host);
pcp->optim_features = FeatureTable_new(host);
pcp->local_features = FeatureTable_new(host, TABLE_LOCAL_IMPORTANCE);
pcp->optim_features = FeatureTable_new(host, TABLE_OPTIM_IMPORTANCE);
}

Table* Platform_getTable(const char* name) {
for (unsigned int i = 0; i < Platform_numberOfDefaultScreens; i++) {
if (!String_eq(name, Platform_defaultScreens[i].name))
continue;
if (i == TABLE_LOCAL_IMPORTANCE)
return &pcp->local_features->super;
if (i == TABLE_OPTIM_IMPORTANCE)
return &pcp->optim_features->super;
break;
}
/* default: TABLE_MODEL_IMPORTANCE */
return &pcp->model_features->super;
}

void Platform_updateMap(void) {
Expand Down
6 changes: 1 addition & 5 deletions src/treetop/pcp/TreeTop.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,9 @@ pid_t Platform_getMaxPid(void);

double Platform_setCPUValues(Meter* this, int cpu);

char* Platform_getProcessEnv(pid_t pid);

void Platform_getPressureStall(const char* file, bool some, double* ten, double* sixty, double* threehundred);

void Platform_getHostname(char* buffer, size_t size);

void Platform_getRelease(char** string);
Table* Platform_getTable(const char* name);

enum {
PLATFORM_LONGOPT_HOST = 128,
Expand Down
2 changes: 1 addition & 1 deletion vendor/github.com/htop-dev/htop/Settings.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c214a37

Please sign in to comment.