Browse Source

Misc. Updates including

  - Renames Filter to ObsFilter and introduces new Filter class that
    acts on vectors, rather than event by event.
  - Fixes some styling on Gen-Level object graphs
  - Adds new plot type which plots the same variable from different
    datasets on the same canvas, but not stacked.
  - Updates Docs
Caleb Fangmeier 7 years ago
parent
commit
74fb44edfe
5 changed files with 86 additions and 22 deletions
  1. 5 5
      api.hpp
  2. 4 3
      container.hpp
  3. 2 1
      dataset.hpp
  4. 11 11
      filter.hpp
  5. 64 2
      value.hpp

+ 5 - 5
api.hpp

@@ -15,10 +15,10 @@ namespace fv{
         return tv;
     }
 
-    Filter* lookup_filter(const std::string& name){
-        Filter* f =  dynamic_cast<Filter*>(GenValue::get_value(name));
+    ObsFilter* lookup_obs_filter(const std::string& name){
+        ObsFilter* f =  dynamic_cast<ObsFilter*>(GenValue::get_value(name));
         if(f == nullptr){
-            CRITICAL("Filter: "+f->get_name() + "has improper type.",-1);
+            CRITICAL("ObsFilter: "+f->get_name() + "has improper type.",-1);
         }
         return f;
     }
@@ -107,8 +107,8 @@ namespace fv{
         return count<T>(selector, lookup<std::vector<T>>(v_name), alias);
     }
 
-    Filter* filter(const std::string& name, std::function<bool()> filter_function, const std::string& impl=""){
-        return new Filter(name, filter_function, impl);
+    ObsFilter* obs_filter(const std::string& name, std::function<bool()> filter_function, const std::string& impl=""){
+        return new ObsFilter(name, filter_function, impl);
     }
 }
 #endif // API_HPP

+ 4 - 3
container.hpp

@@ -78,7 +78,7 @@ class GenContainer{
     private:
         std::string name;
         std::string desc;
-        std::vector<Filter*> filters;
+        std::vector<ObsFilter*> filters;
     protected:
         virtual void _fill() = 0;
     public:
@@ -88,8 +88,9 @@ class GenContainer{
         GenContainer(const std::string name)
           :GenContainer(name,"N/A"){ }
 
-        void add_filter(GenValue* filter){
-            filters.push_back(dynamic_cast<Filter*>(filter));
+        GenContainer* add_filter(GenValue* filter){
+            filters.push_back(dynamic_cast<ObsFilter*>(filter));
+            return this;
         }
 
         void fill(){

+ 2 - 1
dataset.hpp

@@ -82,11 +82,12 @@ class DataSet{
                 container.second->save();
         }
 
-        void register_container(GenContainer *container){
+        GenContainer* register_container(GenContainer *container){
             if (containers[container->get_name()] != nullptr){
                 CRITICAL("Container with name \""+container->get_name()+"\" already exists.", -1);
             }
             containers[container->get_name()] = container;
+            return container;
         }
 
         GenContainer* get_container(std::string container_name){

+ 11 - 11
filter.hpp

@@ -41,7 +41,7 @@
 #include "value.hpp"
 namespace fv {
 
-class Filter : public DerivedValue<bool>{
+class ObsFilter : public DerivedValue<bool>{
     private:
         Function<bool()>& filter_function;
 
@@ -52,40 +52,40 @@ class Filter : public DerivedValue<bool>{
         void verify_integrity(){ };
 
     public:
-        Filter(const std::string& name, std::function<bool()> filter_function, const std::string& impl="")
+        ObsFilter(const std::string& name, std::function<bool()> filter_function, const std::string& impl="")
           :DerivedValue<bool>(name),
            filter_function(GenFunction::register_function<bool()>("filter::"+name, filter_function, impl)){ }
 
         /** Return a new filter that is the conjuction of the two source
          * filters.
          */
-        Filter* operator*(Filter *f){
+        ObsFilter* operator*(ObsFilter *f){
             auto new_name = this->get_name() + "&&" + f->get_name();
-            return new Filter(new_name, [this, f](){return this->get_value() && f->get_value();});
+            return new ObsFilter(new_name, [this, f](){return this->get_value() && f->get_value();});
         }
 
         /** Return a new filter that is the disjunction of the two source
          * filters.
          */
-        Filter* operator+(Filter *f){
+        ObsFilter* operator+(ObsFilter *f){
             auto new_name = this->get_name() + "||" + f->get_name();
-            return new Filter(new_name, [this, f](){return this->get_value() || f->get_value();});
+            return new ObsFilter(new_name, [this, f](){return this->get_value() || f->get_value();});
         }
 
         /** Return a new filter that is the negation of the source filter.
          */
-        Filter* operator!(){
+        ObsFilter* operator!(){
             auto new_name = std::string("!(") + this->get_name() + std::string(")");
-            return new Filter(new_name, [this](){return !this->get_value();});
+            return new ObsFilter(new_name, [this](){return !this->get_value();});
         }
 };
 
 template <typename T>
-class RangeFilter : public Filter{
+class RangeObsFilter : public ObsFilter{
     private:
     public:
-        RangeFilter(const std::string name, Value<T>* test_value, T range_low, T range_high):
-          Filter(name, [test_value, range_low, range_high]{
+        RangeObsFilter(const std::string name, Value<T>* test_value, T range_low, T range_high):
+          ObsFilter(name, [test_value, range_low, range_high]{
                   T val = test_value->get_value();
                   return (val >= range_low) && (val < range_high);
                   }) { }

+ 64 - 2
value.hpp

@@ -164,6 +164,19 @@ class GenFunction {
             in_register_function = false;
             return *func;
         }
+
+        template <typename T>
+        static Function<T>& lookup_function(const std::string& name){
+            if (GenFunction::function_registry[name] == nullptr){
+                CRITICAL("Function \"" << name << "\" not previously registered", -1);
+            } else {
+                Function<T>* func = dynamic_cast<Function<T>*>(GenFunction::function_registry[name]);
+                if (func == nullptr){
+                    CRITICAL("Function \"" << name << "\" request and register have mismatched types", -1);
+                }
+                return *GenFunction::function_registry[name];
+            }
+        }
 };
 
 
@@ -564,8 +577,9 @@ template<typename> class Map; // undefined
 template <typename Ret, typename... ArgTypes>
 class Map<Ret(ArgTypes...)> : public DerivedValue<std::vector<Ret>>{
     private:
+        typedef Value<std::vector<std::tuple<ArgTypes...>>> arg_type;
         Function<Ret(ArgTypes...)>& fn;
-        Zip<ArgTypes...>* arg;
+        arg_type* arg;
 
         void update_value(){
             this->value.clear();
@@ -575,11 +589,36 @@ class Map<Ret(ArgTypes...)> : public DerivedValue<std::vector<Ret>>{
         }
 
     public:
-        Map(Function<Ret(ArgTypes...)>& fn, Zip<ArgTypes...>* arg, const std::string& alias)
+        Map(Function<Ret(ArgTypes...)>& fn, arg_type* arg, const std::string& alias)
           :DerivedValue<std::vector<Ret>>("map("+fn.get_name()+":"+arg->get_name()+")", alias),
            fn(fn), arg(arg){ }
 
 };
+/**
+ * Returns the elements in a vector that pass a test function. The elements on
+ * the vector must be tuples. Typically this will be used in conjunction with
+ * Zip and Map.
+ */
+template<typename... ArgTypes>
+class TupFilter : public DerivedValue<std::vector<std::tuple<ArgTypes...>>>{
+    private:
+        typedef std::vector<std::tuple<ArgTypes...>> value_type;
+        Function<bool(ArgTypes...)>& filter;
+        Value<value_type>* arg;
+
+        void update_value(){
+            this->value.clear();
+            for(auto val : arg->get_value()){
+                if(call(filter,val))
+                    this->value.push_back(val);
+            }
+        }
+
+    public:
+        TupFilter(Function<bool(ArgTypes...)>& filter, Value<value_type>* arg, const std::string alias)
+          :DerivedValue<value_type>("filter("+filter.get_name()+":"+arg->get_name()+")", alias),
+           filter(filter), arg(arg) { }
+};
 
 template<typename... T> class _Tuple;
 template<>
@@ -694,6 +733,29 @@ class Count : public DerivedValue<int>{
            selector(selector), v(v) { }
 };
 
+/**
+ * Returns the elements in a vector that pass a test function.
+ */
+template<typename T>
+class Filter : public DerivedValue<std::vector<T>>{
+    private:
+        Function<bool(T)>& filter;
+        Value<std::vector<T> >* v;
+
+        void update_value(){
+            this->value.clear();
+            for(auto val : v->get_value()){
+                if(selector(val))
+                    this->value.push_back(val);
+            }
+        }
+
+    public:
+        Filter(Function<bool(T)>& filter, Value<std::vector<T>>* v, const std::string alias)
+          :DerivedValue<std::vector<T>>("filter("+filter.get_name()+":"+v->get_name()+")", alias),
+           filter(filter), v(v) { }
+};
+
 
 /**
  * Reduce a Value of type vector<T> to just a T.