Skip to content

Instantly share code, notes, and snippets.

View marty1885's full-sized avatar
πŸ‘¨β€πŸ’»
Writing code

Martin Chang marty1885

πŸ‘¨β€πŸ’»
Writing code
View GitHub Profile
//Released under BSD-3 Cluse licsence
//How to run: root -b stackedsp.cpp
//or: c++ stackedsp.cpp -o stackedsp -O3 -lEtaler `root-config --cflags --ldflags --glibs` && ./stackedsp
//Assuming you have Etaler installed in /use/local/lib. Change this if you have it in other places
#pragma cling load("/usr/local/lib/libEtaler.so")
#include <Etaler/Etaler.hpp>
#include <Etaler/Algorithms/SpatialPooler.hpp>
#include <Etaler/Encoders/GridCell1d.hpp>
#include <Etaler/Encoders/Scalar.hpp>
#include <Etaler/Backends/OpenCLBackend.hpp>
void to_root_raw()
{
// Create a reader from CSV
csv::CSVReader reader("checkouts-by-title.csv");
// Create a ROOT File. For the first pass, we'll be saving everything as a string
auto f = new TFile("library.root", "recreate");
TTree *t = new TTree("library_raw","raw data from csv file");
auto fields = reader.get_col_names();
auto buf = std::vector<std::string>(fields.size());
void count_zeros()
{
// Now we can use the new and improved RDataFrame interface
auto rdf = ROOT::RDataFrame("library_raw", "library.root");
auto columns = rdf.GetColumnNames();
auto empty_lst = std::map<std::string, size_t>();
ProgressDisplay disp(*rdf.Count()*columns.size());
// A basic parser, it might get stuff wrong, but good enought for EDA
Int_t parse_year(std::string year)
{
if(year == "")
throw std::runtime_error("year is empty");
if(year[0] == '[' || year[0] == 'c' || year[0] == 'p') // Handle format of [2000], c2000 and p2000
return std::stoi(std::string(year.begin()+1, year.begin()+5));
else if(isdigit(year[0]) == true)
return std::stoi(std::string(year.begin(), year.begin()+4));
throw std::runtime_error("cannot parse format");
void missing_creator()
{
// Now we can use the new and improved RDataFrame interface
auto rdf = ROOT::RDataFrame("library", "library.root");
auto rdf_missing = rdf.Filter("creator != \"\" && publication_year != 0x7fffffff");
std::map<std::string, int> books;
rdf_missing.Foreach([&](std::string title, int year) {books[title] = year;}, {"title", "publication_year"});
auto h1 = new TH1F("h1", "books with missing creator", 100, 1850, 2019);
for(auto [title, year] : books)
h1->Fill(year);
void missing_creator_pct()
{
// Now we can use the new and improved RDataFrame interface
auto rdf = ROOT::RDataFrame("library", "library.root");
auto rdf_with_year = rdf.Filter("publication_year != 0x7fffffff");
auto rdf_missing = rdf_with_year.Filter("creator != \"\"");
std::map<std::string, int> books;
std::map<std::string, int> all_books;
rdf_with_year.Foreach([&](std::string title, int year) {all_books[title] = year;}, {"title", "publication_year"});
rdf_missing.Foreach([&](std::string title, int year) {books[title] = year;}, {"title", "publication_year"});
import torch
from torch import nn, optim
import torch.functional as F
import matplotlib.pyplot as plt
import numpy as np
# Use ROOT as a binding between C++ and Python
import ROOT
gROOT = ROOT.gROOT
#include <htm/algorithms/TemporalMemory.hpp>
#include <htm/encoders/ScalarEncoder.hpp>
using namespace htm;
float benchmarkTemporalMemory(const std::vector<UInt>& out_shape, const std::vector<SDR>& x, size_t num_epoch)
{
TemporalMemory tm;
tm.initialize(out_shape, 16, 2, 0.21, 0.1, 1, 1024, 0.1, 0.1, 0, 42, 1, 1024, false);
// sp.permanences_ = sp.permanences_.cast(DType::Half);
#include <Etaler/Etaler.hpp>
#include <Etaler/Backends/CPUBackend.hpp>
//#include <Etaler/Backends/OpenCLBackend.hpp>
#include <Etaler/Algorithms/TemporalMemory.hpp>
#include <Etaler/Encoders/Scalar.hpp>
using namespace et;
#include <vector>
#include <chrono>
#include <random>
@marty1885
marty1885 / c1.cpp
Last active November 16, 2019 02:58
HTM hyper parameter plot generated by ROOT
void c1()
{
//=========Macro generated from canvas: c1/Canvas
//========= (Sat Nov 16 10:57:34 2019) by ROOT version 6.18/04
gStyle->SetCanvasPreferGL(kTRUE);
TCanvas *c1 = new TCanvas("c1", "Canvas",10,570,700,500);
gStyle->SetOptStat(0);
c1->Range(-1.006644,-1.180079,1.006644,1.180079);