Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
SigmoidFilter.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file protocols/simple_filters/SigmoidFilter.cc
11 /// @brief
12 /// @author Gabi Pszolla & Sarel Fleishman
13 
14 
15 //Unit Headers
18 #include <utility/tag/Tag.hh>
19 //Project Headers
20 #include <basic/Tracer.hh>
21 #include <core/pose/Pose.hh>
23 #include <fstream>
24 #include <utility/io/izstream.hh>
25 #include <sstream>
26 
27 namespace protocols{
28 namespace simple_filters {
29 
30 static basic::Tracer TR( "protocols.simple_filters.Sigmoid" );
31 
34 
36 SigmoidFilterCreator::keyname() const { return "Sigmoid"; }
37 
38 //default ctor
40 protocols::filters::Filter( "Sigmoid" ),
41 filter_( NULL ),
42 steepness_( 1.0 ),
43 offset_( 0.0 ),
44 baseline_( 0.0 ),
45 negate_( false ),
46 threshold_( 0 ),
47 baseline_checkpointing_filename_( "" )
48 {
49 }
50 
52 
53 /// @brief The first MC trajectory should not read the baseline from the checkpoint file, instead, it should set the baseline
54 /// attempt_read_from_checkpoint determines whether a read attempt from checkpoint should be attempted
55 void
56 Sigmoid::reset_baseline( core::pose::Pose const & pose, bool const attempt_read_from_checkpoint ){
57  using namespace std;
58 
59  bool compute_new_baseline( false );
60  if( attempt_read_from_checkpoint )
61  TR<<"Reading baseline from checkpoint file, if one exists"<<std::endl;
62  else
63  TR<<"Not reading from checkpoint file"<<std::endl;
64  if( attempt_read_from_checkpoint && baseline_checkpointing_filename_ != "" ){
65  ifstream f( baseline_checkpointing_filename_.c_str(), ios::in );
66  if( !f.good() )
67  compute_new_baseline = true;
68  else{
69  core::Size const begin = f.tellg();
70  f.seekg( 0, ios::end );
71  core::Size const end = f.tellg();
72  f.seekg( 0, ios::beg );
73  if( end - begin == 0 )//file size == 0
74  compute_new_baseline = true;
75  }
76  if( !compute_new_baseline ){
77  std::string line;
78  getline( f, line );
79  std::istringstream line_stream( line );
80  line_stream >> baseline_;
81  TR<<"Loading Sigmoid baseline from checkpoint. Loaded baseline: "<<baseline_<<std::endl;
82  f.close();
83  return;
84  }
85  f.close();
86  }
87 
88  baseline_ = filter()->report_sm( pose );
89  TR<<"Computed new baseline and set to: "<<baseline_<<std::endl;
91  ofstream f;
92  f.open( baseline_checkpointing_filename_.c_str(), ios::out );
93  if( !f.good() )
94  utility_exit_with_message( "Unable to open Sigmoid checkpointing file: " + baseline_checkpointing_filename_ );
95  f << baseline_;
96  f.close();
97  TR<<"Wrote baseline "<<baseline_<<" to checkpointing file "<<baseline_checkpointing_filename_<<std::endl;
98  }//fi baseline_checkpointing_filename_
99 }
100 
101 void
103 {
104  steepness( tag->getOption< core::Real >( "steepness", 1.0 ) );
105  offset( tag->getOption< core::Real >( "offset", 0 ));
106  negate( tag->getOption< bool >( "negate", false ) );
107  threshold( tag->getOption< core::Real >( "threshold", 0 ) );
108  filter( protocols::rosetta_scripts::parse_filter( tag->getOption< std::string >( "filter" ), filters ) );
109  baseline_checkpointing_filename_ = tag->getOption< std::string >( "baseline_checkpoint", "" );
110  TR<<"Sigmoid with options: steepness "<<steepness()<<" offset "<<offset()<<" negate "<<negate()<<" threshold "<<threshold()<<" filter: "<<tag->getOption< std::string >( "filter" ) << " baseline checkpointing file: "<<baseline_checkpointing_filename_<<std::endl;
111 }
112 
113 bool
114 Sigmoid::apply( core::pose::Pose const & pose ) const {
115  core::Real const val ( compute( pose ) );
116  return( val >= threshold() );
117 }
118 
119 void
120 Sigmoid::report( std::ostream &o, core::pose::Pose const & pose ) const {
121  core::Real const val = compute( pose );
122  o << "Sigmoid returns "<<val<<std::endl;
123 }
124 
126 Sigmoid::report_sm( core::pose::Pose const & pose ) const {
127  return( compute( pose ) );
128 }
129 
132  core::pose::Pose const & pose
133 ) const {
134  core::Real const val( filter()->report_sm( pose ) - baseline_ );
135  core::Real const transform( 1.0 / ( ( 1.0 + std::exp( ( val - offset_ ) * steepness_ ) ) ) );
136  core::Real const complement( negate() ? 1.0 - transform : transform ); // negate means to take the complement of the transform
137  TR<<"filter val/transform: "<<val<<" "<<complement<<std::endl;
138  TR<<"returning: "<<complement<<std::endl;
139  return( complement );
140 }
141 
143 Sigmoid::filter() const{ return filter_; }
144 
145 void
147 }
148 }