Skip to content

Commit

Permalink
Create text feature (#333)
Browse files Browse the repository at this point in the history
* Add manual Save feature.

* Fix the incorrect style

* Add feature to record Text
  • Loading branch information
jetfuel committed Mar 23, 2018
1 parent aa56f33 commit 3afc5b2
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 3 deletions.
27 changes: 24 additions & 3 deletions visualdl/logic/pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,14 @@ PYBIND11_MODULE(core, m) {
#undef READER_ADD_HISTOGRAM

// clang-format on
.def("get_image", [](vs::LogReader& self, const std::string& tag) {
.def("get_image",
[](vs::LogReader& self, const std::string& tag) {
auto tablet = self.tablet(tag);
return vs::components::ImageReader(self.mode(), tablet);
})
.def("get_text", [](vs::LogReader& self, const std::string& tag) {
auto tablet = self.tablet(tag);
return vs::components::ImageReader(self.mode(), tablet);
return vs::components::TextReader(tablet);
});

// clang-format on
Expand Down Expand Up @@ -113,7 +118,11 @@ PYBIND11_MODULE(core, m) {
int step_cycle) {
auto tablet = self.AddTablet(tag);
return vs::components::Image(tablet, num_samples, step_cycle);
});
})
.def("new_text", [](vs::LogWriter& self, const std::string& tag) {
auto tablet = self.AddTablet(tag);
return vs::components::Text(tablet);
});

//------------------- components --------------------
#define ADD_SCALAR_READER(T) \
Expand Down Expand Up @@ -198,6 +207,18 @@ PYBIND11_MODULE(core, m) {
.def("record", &cp::ImageReader::record)
.def("timestamp", &cp::ImageReader::timestamp);

py::class_<cp::Text>(m, "TextWriter")
.def("set_caption", &cp::Text::SetCaption)
.def("add_record", &cp::Text::AddRecord);

py::class_<cp::TextReader>(m, "TextReader")
.def("records", &cp::TextReader::records)
.def("ids", &cp::TextReader::ids)
.def("timestamps", &cp::TextReader::timestamps)
.def("caption", &cp::TextReader::caption)
.def("total_records", &cp::TextReader::total_records)
.def("size", &cp::TextReader::size);

#define ADD_HISTOGRAM_WRITER(T) \
py::class_<cp::Histogram<T>>(m, "HistogramWriter__" #T, \
R"pbdoc(PyBind class. Must instantiate through the LogWriter.)pbdoc") \
Expand Down
31 changes: 31 additions & 0 deletions visualdl/logic/sdk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,37 @@ DECL_BASIC_TYPES_CLASS_IMPL(class, ScalarReader)
DECL_BASIC_TYPES_CLASS_IMPL(struct, Histogram)
DECL_BASIC_TYPES_CLASS_IMPL(struct, HistogramReader)

std::vector<std::string> TextReader::records() const {
std::vector<std::string> res;
for (int i = 0; i < total_records(); i++) {
res.push_back(reader_.record(i).data(0).template Get<std::string>());
}
return res;
}

std::vector<int> TextReader::ids() const {
std::vector<int> res;
for (int i = 0; i < reader_.total_records(); i++) {
res.push_back(reader_.record(i).id());
}
return res;
}

std::vector<time_t> TextReader::timestamps() const {
std::vector<time_t> res;
for (int i = 0; i < reader_.total_records(); i++) {
res.push_back(reader_.record(i).timestamp());
}
return res;
}

std::string TextReader::caption() const {
CHECK(!reader_.captions().empty()) << "no caption";
return reader_.captions().front();
}

size_t TextReader::size() const { return reader_.total_records(); }

} // namespace components

} // namespace visualdl
34 changes: 34 additions & 0 deletions visualdl/logic/sdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,40 @@ struct HistogramReader {
TabletReader reader_;
};

struct Text {
Text(Tablet tablet) : tablet_(tablet) {}
void SetCaption(const std::string cap) {
tablet_.SetCaptions(std::vector<std::string>({cap}));
}

void AddRecord(int id, std::string value) {
auto record = tablet_.AddRecord();
record.SetId(id);
auto entry = record.AddData();

time_t time = std::time(nullptr);
record.SetTimeStamp(time);
entry.Set(value);
}

private:
Tablet tablet_;
};

struct TextReader {
TextReader(TabletReader reader) : reader_(reader) {}

std::vector<std::string> records() const;
std::vector<int> ids() const;
std::vector<time_t> timestamps() const;
std::string caption() const;
size_t total_records() const { return reader_.total_records(); }
size_t size() const;

private:
TabletReader reader_;
};

} // namespace components
} // namespace visualdl

Expand Down
8 changes: 8 additions & 0 deletions visualdl/python/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ def histogram(self, tag, type='float'):
check_tag_name_valid(tag)
return type2scalar[type](tag)

def text(self, tag):
check_tag_name_valid(tag)
return self.reader.get_text(tag)

def __enter__(self):
return self

Expand Down Expand Up @@ -222,6 +226,10 @@ def histogram(self, tag, num_buckets, type='float'):
}
return types[type](tag, num_buckets)

def text(self, tag):
check_tag_name_valid(tag)
return self.writer.new_text(tag)

def save(self):
self.writer.save()

Expand Down

0 comments on commit 3afc5b2

Please sign in to comment.