adrian0010 / Percolation

1 stars 0 forks source link

[Statistics] Refactoring code for Simple Model #33

Closed discoleo closed 1 year ago

discoleo commented 1 year ago

Statistics for Simple Model

Steps:

R Code

Function flood.rev can be added to Analysis:

flood.rev = function(x, id.start=NULL) {
    if(is.null(id.start)) {
        id.start = max(x[,1]) + 1;
    }
    dim = dim(x);
    x = rev(x);
    x = array(x, dim);
    # Flood from "right"
    x = flood.all(x, id.start=id.start);
    #
    x = rev(x);
    x = array(x, dim);
    return(x);
}

Refactored code:

    ### Analyse

    # StatisticsSimple
    observe({
        m = values$rSimple;
        if(is.null(m)){
            return();
        }
        # Flood from Right
        m = flood.rev(m);
        #
        statChannels = analyse.Channels(m);
        statAreas = analyse.Area(m);
        output$Statistics = renderTable(statChannels);
        output$Area = renderTable(statAreas);
    })

    Channels = function(x) {
        nc = ncol(x);   # numar coloane
        id1 = unique(x[, 1]);
        id1 = id1[id1 > 0];
        id3 = unique(x[, nc]);
        id3 = id3[id3 > 0];
        id2 = intersect(id1, id3);
        if(length(id2) > 0) {
            id1 = setdiff(id1, id2);
            id3 = setdiff(id3, id2);
        }
        result = list(L = id1, P = id2, R = id3);
        return (result);
    }