/* * plot_test.c - testa a tinyblocks-plot pelos blocos: linha, pontos, * barras, histograma, serie com nome, legenda, grade e texto com acento; * valores de lista, de texto e de tensor. Grava /tmp/tb_plot_*.png. */ #include #include #include #include #include #include #include #include #include static int fails = 0; #define CHECK(c, m) do { if (c) printf("ok %s\n", m); else { printf("FAIL %s\n", m); fails++; } } while (0) static TinyBlocks* rt; static TinyBlocks_Block* run(const char* type, const char* a, const char* b, const char* c, const char* d) { TinyBlocks_Block* x = tb_block_create(rt, tb_type_block_find(rt, type)); const char* v[4] = { a, b, c, d }; for (int i = 0; i < 4; i++) if (v[i]) { TinyBlocks_Value t = tb_value_text(v[i]); tb_block_set_default(x, (uint32_t)i, t); tb_value_free(&t); } tb_block_eval(x, rt); return x; } /* A cor do pixel e (quase) a pedida? */ static bool near_color(const TinyBlocks_Image* img, int x, int y, uint32_t rgb, int tol) { uint8_t r, g, b, a; if (!tb_image_get_pixel(img, x, y, &r, &g, &b, &a)) return false; return abs(r - (int)(rgb >> 16 & 255)) <= tol && abs(g - (int)(rgb >> 8 & 255)) <= tol && abs(b - (int)(rgb & 255)) <= tol; } static int count_color(const TinyBlocks_Image* img, uint32_t rgb) { int n = 0; for (int y = 0; y < tb_image_height(img); y++) for (int x = 0; x < tb_image_width(img); x++) n += near_color(img, x, y, rgb, 30); return n; } int main(void) { setvbuf(stdout, NULL, _IOLBF, 0); tinyblocks_init(&rt, NULL); tb_library_register(rt, tb_png_get_library()); tb_library_register(rt, tb_tensor_get_library()); tb_library_register(rt, tb_plot_get_library()); run("Plot.Figure", "480", "320", NULL, NULL); run("Plot.Line", "[1, 4, 2, 5, 3]", "azul", "treino", NULL); run("Plot.Scatter", "1, 2, 3, 4, 5", "2, 3, 3.5, 4.5, 4", "laranja", "validação"); run("Plot.Title", "Perda por época", NULL, NULL, NULL); run("Plot.XLabel", "época", NULL, NULL, NULL); run("Plot.YLabel", "perda média", NULL, NULL, NULL); run("Plot.Legend", NULL, NULL, NULL, NULL); run("Plot.Grid", NULL, NULL, NULL, NULL); TinyBlocks_Block* im = run("Plot.Image", NULL, NULL, NULL, NULL); TinyBlocks_Image* img = im->outputs[0].kind == TB_VALUE_IMAGE ? im->outputs[0].as.image : NULL; CHECK(img && tb_image_width(img) == 480 && tb_image_height(img) == 320, "imagem da figura 480x320"); CHECK(img && count_color(img, 0x1f77b4) > 150, "a linha azul (tab10) foi desenhada"); CHECK(img && count_color(img, 0xff7f0e) > 60, "os pontos laranja foram desenhados"); CHECK(img && near_color(img, 2, 2, 0xffffff, 0), "fundo branco"); run("Plot.Save", "/tmp/tb_plot_linha.png", NULL, NULL, NULL); CHECK(!tinyblocks_last_error(rt)[0], "salva em PNG"); /* tensor, barras e serie com nome */ run("Plot.Figure", "400", "260", NULL, NULL); TinyBlocks_Block* tz = run("Tensor.Randn", "500", NULL, NULL, NULL); TinyBlocks_Block* h = tb_block_create(rt, tb_type_block_find(rt, "Plot.Hist")); tb_block_connect(h, 0, tz, 0); tz->dirty = false; tb_block_set_default(h, 1, tb_value_integer(15)); tb_block_eval(h, rt); run("Plot.Title", "Histograma de um tensor normal", NULL, NULL, NULL); TinyBlocks_Block* im2 = run("Plot.Image", NULL, NULL, NULL, NULL); CHECK(im2->outputs[0].kind == TB_VALUE_IMAGE && count_color(im2->outputs[0].as.image, 0x1f77b4) > 3000, "histograma de um tensor (as barras aparecem)"); run("Plot.Save", "/tmp/tb_plot_hist.png", NULL, NULL, NULL); run("Plot.Figure", "400", "260", NULL, NULL); for (int i = 0; i < 50; i++) { char v[32]; snprintf(v, sizeof(v), "%g", exp(-i / 12.0) + 0.05 * sin(i)); run("Plot.Push", v, "perda", NULL, NULL); } TinyBlocks_Block* len = run("Plot.SeriesLen", "perda", NULL, NULL, NULL); CHECK(len->outputs[0].as.integer == 50, "a série com nome cresce (50 valores)"); run("Plot.Series", "perda", "vermelho", NULL, NULL); run("Plot.Bar", "0.2, 0.4", "a, b", "verde", NULL); run("Plot.Legend", NULL, NULL, NULL, NULL); run("Plot.Save", "/tmp/tb_plot_serie.png", NULL, NULL, NULL); CHECK(!tinyblocks_last_error(rt)[0], "série + barras na mesma figura"); tinyblocks_deinit(&rt); if (fails) printf("\n%d FALHA(S)\n", fails); else printf("\nTUDO OK\n"); return fails != 0; }