BradleyA / Linux-admin

Shell scripts to automate download of GitHub traffic statistics, cluster administration, and create an animated GIF.
MIT License
29 stars 6 forks source link

github-repository-traffic/parse.repository.data.sh - parse RAW data to create .md table files #30

Closed BradleyA closed 4 years ago

BradleyA commented 5 years ago

github-repository/parse.repository.data.sh - parse RAW data to create .png files

How to create .png files from raw text data with labels ?

https://mathematica.stackexchange.com/questions/17826/how-to-automate-generation-of-image-files

7 Let's suppose that these are your imported data (three sets of random numbers):

importedData = Table[RandomReal[{0, 1}, {10, 2}], {3}];

Now we can directly produce and export the PNG files:

Table[
  Export[
    "plot_" <> ToString[i] <> ".png",
    ListPlot[importedData[[i]]],
    "PNG"
  ],
  {i, Length[importedData]}
]

    {"plot_1.png", "plot_2.png", "plot_3.png"}

"plot_" <> ToString[i] <> ".png" can be replaced by StringTemplate["plot_``.png"]  @i
shareimprove this answer
edited Aug 7 '18 at 12:34
Kuba♦
110k1212 gold badges220220 silver badges560560 bronze badges
answered Jan 15 '13 at 15:52
VLC
8,75011 gold badge2323 silver badges5454 bronze badges

    3
    IntegerString is useful because it makes it easy to specify the total number of digits and will insert the necessary number of leading zeros. For example, IntegerString[4, 10, 3] --> "004" – Szabolcs Jun 16 '14 at 21:24

24

To export a sequence of plots, you don't need to do the sequential numbering by hand (or by using ToString). Instead, there is the Export option "VideoFrames" that does the numbering automatically:

plots =
  Table[Plot[Sin[x + a], {x, 0, Pi},
    PlotRange -> {{0, Pi}, {-1.1, 1.1}}], {a, 0, Pi, .1}];

Quiet[CreateDirectory["output"]];
SetDirectory["output"];

Export["plot001.png", plots, "VideoFrames"];

FileNames[]

(*
==> {"plot001.png", "plot002.png", "plot003.png", "plot004.png", \
"plot005.png", "plot006.png", "plot007.png", "plot008.png", \
"plot009.png", "plot010.png", "plot011.png", "plot012.png", \
"plot013.png", "plot014.png", "plot015.png", "plot016.png", \
"plot017.png", "plot018.png", "plot019.png", "plot020.png", \
"plot021.png", "plot022.png", "plot023.png", "plot024.png", \
"plot025.png", "plot026.png", "plot027.png", "plot028.png", \
"plot029.png", "plot030.png", "plot031.png", "plot032.png"}
*)

ResetDirectory[];

The number of digits in the numbered output files is determined from the number you specify in the first file name.
BradleyA commented 5 years ago
function printTable()
{
    local -r delimiter="${1}"
    local -r data="$(removeEmptyLines "${2}")"

    if [[ "${delimiter}" != '' && "$(isEmptyString "${data}")" = 'false' ]]
    then
        local -r numberOfLines="$(wc -l <<< "${data}")"

        if [[ "${numberOfLines}" -gt '0' ]]
        then
            local table=''
            local i=1

            for ((i = 1; i <= "${numberOfLines}"; i = i + 1))
            do
                local line=''
                line="$(sed "${i}q;d" <<< "${data}")"

                local numberOfColumns='0'
                numberOfColumns="$(awk -F "${delimiter}" '{print NF}' <<< "${line}")"

                # Add Line Delimiter

                if [[ "${i}" -eq '1' ]]
                then
                    table="${table}$(printf '%s#+' "$(repeatString '#+' "${numberOfColumns}")")"
                fi

                # Add Header Or Body

                table="${table}\n"

                local j=1

                for ((j = 1; j <= "${numberOfColumns}"; j = j + 1))
                do
                    table="${table}$(printf '#| %s' "$(cut -d "${delimiter}" -f "${j}" <<< "${line}")")"
                done

                table="${table}#|\n"

                # Add Line Delimiter

                if [[ "${i}" -eq '1' ]] || [[ "${numberOfLines}" -gt '1' && "${i}" -eq "${numberOfLines}" ]]
                then
                    table="${table}$(printf '%s#+' "$(repeatString '#+' "${numberOfColumns}")")"
                fi
            done

            if [[ "$(isEmptyString "${table}")" = 'false' ]]
            then
                echo -e "${table}" | column -s '#' -t | awk '/^\+/{gsub(" ", "-", $0)}1'
            fi
        fi
    fi
}

function removeEmptyLines()
{
    local -r content="${1}"

    echo -e "${content}" | sed '/^\s*$/d'
}

function repeatString()
{
    local -r string="${1}"
    local -r numberToRepeat="${2}"

    if [[ "${string}" != '' && "${numberToRepeat}" =~ ^[1-9][0-9]*$ ]]
    then
        local -r result="$(printf "%${numberToRepeat}s")"
        echo -e "${result// /${string}}"
    fi
}

function isEmptyString()
{
    local -r string="${1}"

    if [[ "$(trimString "${string}")" = '' ]]
    then
        echo 'true' && return 0
    fi

    echo 'false' && return 1
}

function trimString()
{
    local -r string="${1}"

    sed 's,^[[:blank:]]*,,' <<< "${string}" | sed 's,[[:blank:]]*$,,'
}

SAMPLE RUNS

$ cat data-1.txt
HEADER 1,HEADER 2,HEADER 3

$ printTable ',' "$(cat data-1.txt)"
+-----------+-----------+-----------+
| HEADER 1  | HEADER 2  | HEADER 3  |
+-----------+-----------+-----------+

$ cat data-2.txt
HEADER 1,HEADER 2,HEADER 3
data 1,data 2,data 3

$ printTable ',' "$(cat data-2.txt)"
+-----------+-----------+-----------+
| HEADER 1  | HEADER 2  | HEADER 3  |
+-----------+-----------+-----------+
| data 1    | data 2    | data 3    |
+-----------+-----------+-----------+

$ cat data-3.txt
HEADER 1,HEADER 2,HEADER 3
data 1,data 2,data 3
data 4,data 5,data 6

$ printTable ',' "$(cat data-3.txt)"
+-----------+-----------+-----------+
| HEADER 1  | HEADER 2  | HEADER 3  |
+-----------+-----------+-----------+
| data 1    | data 2    | data 3    |
| data 4    | data 5    | data 6    |
+-----------+-----------+-----------+

$ cat data-4.txt
HEADER
data

$ printTable ',' "$(cat data-4.txt)"
+---------+
| HEADER  |
+---------+
| data    |
+---------+

$ cat data-5.txt
HEADER

data 1

data 2

$ printTable ',' "$(cat data-5.txt)"
+---------+
| HEADER  |
+---------+
| data 1  |
| data 2  |
+---------+

REF LIB at: https://github.com/gdbtek/linux-cookbooks/blob/master/libraries/util.bash
BradleyA commented 5 years ago
github-repository/parse.repository.data.sh  2.98.370  2019-08-08T23:47:37.538761-05:00 (CDT)  https://github.com/BradleyA/Linux-admin  uadmin  two-rpi3b.cptx86.com 2.97  
    github-repository/parse.repository.data.sh design complete, ready to create test cases
BradleyA commented 4 years ago

Used a simple markdown table format