Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init comment for sdk #117

Merged
merged 3 commits into from
Jan 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions visualdl/logic/sdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ namespace visualdl {

const static std::string kDefaultMode{"default"};

/**
* LogWriter is common Data Structure used to write data
* into a low level storage data structure.
*/
class LogWriter {
public:
LogWriter(const std::string& dir, int sync_cycle) {
Expand All @@ -34,6 +38,9 @@ class LogWriter {
return writer;
}

/**
* create a new tablet
*/
Tablet AddTablet(const std::string& tag) {
// TODO(ChunweiYan) add string check here.
auto tmp = mode_ + "/" + tag;
Expand All @@ -51,6 +58,10 @@ class LogWriter {
std::string mode_{kDefaultMode};
};

/**
* LogReader is common Data Structure used to read data
* from a low level storage data structure.
*/
class LogReader {
public:
LogReader(const std::string& dir) : reader_(dir) {}
Expand Down Expand Up @@ -180,19 +191,21 @@ struct Image {
writer_.SetNumSamples(num_samples);
SetCaption(tablet.reader().tag());
}

void SetCaption(const std::string& c) {
writer_.SetCaptions(std::vector<std::string>({c}));
}

/*
* Start a sample period.
* Start a sampling period.
*/
void StartSampling();
/*
* Will this sample will be taken.
* Will this sample be taken.
*/
int IsSampleTaken();
/*
* End a sample period.
* End a sampling period.
*/
void FinishSampling();

Expand Down Expand Up @@ -265,6 +278,9 @@ struct ImageReader {
std::string mode_;
};

/*
* Histogram component writer.
*/
template <typename T>
struct Histogram {
Histogram(Tablet tablet, int num_buckets)
Expand All @@ -279,6 +295,9 @@ struct Histogram {
Tablet writer_;
};

/*
* Histogram reader.
*/
template <typename T>
struct HistogramReader {
HistogramReader(TabletReader tablet) : reader_(tablet) {}
Expand Down
72 changes: 59 additions & 13 deletions visualdl/python/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,60 @@


class LogReader(object):

cur_mode = None
"""LogReader is a Python wrapper to read and analysis the data that
saved with data format defined in storage.proto. user can get
Scalar Reader/Image Reader/Histogram Reader from this module and use
them to reade the data you need.
"""

def __init__(self, dir, reader=None):
"""
create a LogReader
:param dir: the dir where log file is.
:param reader: create a new LogReader with a formal one
"""
self.dir = dir
self.reader = reader if reader else core.LogReader(dir)

def mode(self, mode):
"""
Set the current mode of reader.

:param mode: the mode is something like a scope, it's used to
put some related data together. for example: train or test.
data generated during training can be marked mode train, and data
generated during testing can be marked test.
:return: the reader itself
"""
self.reader.set_mode(mode)
return self

def as_mode(self, mode):
"""
create a new LogReader with mode and return it to user.
"""
tmp = LogReader(dir, self.reader.as_mode(mode))
return tmp

def modes(self):
"""
Get all modes of the log file
:return:
"""
return self.reader.modes()

def tags(self, kind):
return self.reader.tags(kind)
def tags(self, component):
"""
Get all tags from the current log file for one kind of component
:param component: Scalar|Histogram|Images
:return: all the tags
"""
return self.reader.tags(component)

def scalar(self, tag, type='float'):
"""
Get a scalar reader with tag and data type
"""
type2scalar = {
'float': self.reader.get_scalar_float,
'double': self.reader.get_scalar_double,
Expand All @@ -36,9 +68,15 @@ def scalar(self, tag, type='float'):
return type2scalar[type](tag)

def image(self, tag):
"""
Get a image reader with tag
"""
return self.reader.get_image(tag)

def histogram(self, tag, type='float'):
"""
Get a histogram reader with tag and data type
"""
type2scalar = {
'float': self.reader.get_histogram_float,
'double': self.reader.get_histogram_double,
Expand All @@ -54,6 +92,10 @@ def __exit__(self, type, value, traceback):


class LogWriter(object):
"""LogWriter is a Python wrapper to write data to log file with the data
format defined in storage.proto. user can get Scalar Reader/Image Reader/
Histogram Reader from this module and use them to write the data to log file.
"""

cur_mode = None

Expand All @@ -67,13 +109,16 @@ def mode(self, mode):
return self

def as_mode(self, mode):
"""
create a new LogWriter with mode and return it.
"""
LogWriter.cur_mode = LogWriter(self.dir, self.sync_cycle, self.writer.as_mode(mode))
return LogWriter.cur_mode

def scalar(self, tag, type='float'):
'''
Create a scalar component.
'''
"""
Create a scalar writer with tag and type to write scalar data.
"""
type2scalar = {
'float': self.writer.new_scalar_float,
'double': self.writer.new_scalar_double,
Expand All @@ -82,15 +127,16 @@ def scalar(self, tag, type='float'):
return type2scalar[type](tag)

def image(self, tag, num_samples, step_cycle):
'''
Create an image component.
'''
"""
Create an image writer that used to write image data.
"""
return self.writer.new_image(tag, num_samples, step_cycle)

def histogram(self, tag, num_buckets, type='float'):
'''
Create a histogram component.
'''
"""
Create a histogram writer that used to write
histogram related data.
"""
types = {
'float': self.writer.new_histogram_float,
'double': self.writer.new_histogram_double,
Expand Down
6 changes: 2 additions & 4 deletions visualdl/python/test_storage.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import random
import time
import pprint
import sys
import unittest

import numpy as np
from PIL import Image

import sys, pprint
pprint.pprint(sys.path)

from visualdl import LogWriter, LogReader
Expand Down Expand Up @@ -119,7 +118,6 @@ def test_with_syntax(self):
self.assertEqual(scalar.caption(), "train")

def test_modes(self):
dir = "./tmp/storagetest0"
store = LogWriter(
self.dir, sync_cycle=1)

Expand Down