<commit_before>/*
 * Distributed as part of Scalala, a linear algebra library.
 * 
 * Copyright (C) 2008- <NAME>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 
 */
package scalala;
package library;

import scala.collection.mutable.ArrayBuffer;

import collection.PartialMap;
import tensor.{Tensor,Vector,Matrix};
import tensor.dense.{DenseVector,DenseMatrix};

/**
 * Matlab-like plotting routines.
 * 
 * @author dramage
 */
trait Plotting extends Library with PartialMaps with Vectors with Matrices with Operators {
  import PlottingSupport._;
  
  /** Implicit graphics context */
  implicit var _scalala_figures = new Figures();
  implicit def _scalala_figure = _scalala_figures.figure;
  implicit def _scalala_xyplot = _scalala_figures.figure.plot;
  
  implicit def iFigure(figures : Figures) = figures.figure;
  implicit def iXYPlot(figures : Figures) = figures.figure.plot;
  implicit def iXYPlot(figure  : Figure)  = figure.plot;
  
  //
  // Plotting
  //

  /** Selects the given figure */
  def figure(select:Int)(implicit figures : Figures) : Figure = {
    figures.figure = select-1
    figures.figure.refresh
    return figures.figure
  }

  def figure()(implicit figures : Figures) : Figure =
    figures.figure;

  /** Clears the current figure */
  def clf()(implicit figure : Figure) = {
    figure.clear;
  }
  
  /** Saves the current figure at 72 dpi to the given filename. */
  def saveas(filename : String)(implicit figure : Figure) : Unit = {
    saveas(filename, 72)(figure);
  }
  
  /** Saves the current figure at the requested dpi to the given filename. */
  def saveas(filename : String, dpi : Int)(implicit figure : Figure) : Unit = {
    import java.io._;
    
    // make sure figure is visible or saved image will come up empty
    figure.refresh();
    
    lazy val fos = new FileOutputStream(filename);
    if (filename.toLowerCase.endsWith(".eps")) {
      figure.writeEPS(fos,dpi);
      fos.close();
    } else if (filename.toLowerCase.endsWith(".png")) {
      figure.writePNG(fos,dpi);
      fos.close();
    } else if (filename.toLowerCase.endsWith(".pdf")) {
      figure.writePDF(fos);
      fos.close();
    } else {
      throw new IOException("Unrecognized file extension: should be eps or png");
    }
  }
  
  /** Selects the given subplot */
  def subplot(rows:Int,cols:Int,select:Int)(implicit figure : Figure) : XYPlot = {
    figure.rows = rows
    figure.cols = cols
    figure.plot = select-1
    figure.refresh<commit_msg>dpi default value for saveas<commit_after>/*
 * Distributed as part of Scalala, a linear algebra library.
 * 
 * Copyright (C) 2008- <NAME>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110 USA 
 */
package scalala;
package library;

import scala.collection.mutable.ArrayBuffer;

import collection.PartialMap;
import tensor.{Tensor,Vector,Matrix};
import tensor.dense.{DenseVector,DenseMatrix};

/**
 * Matlab-like plotting routines.
 * 
 * @author dramage
 */
trait Plotting extends Library with PartialMaps with Vectors with Matrices with Operators {
  import PlottingSupport._;
  
  /** Implicit graphics context */
  implicit var _scalala_figures = new Figures();
  implicit def _scalala_figure = _scalala_figures.figure;
  implicit def _scalala_xyplot = _scalala_figures.figure.plot;
  
  implicit def iFigure(figures : Figures) = figures.figure;
  implicit def iXYPlot(figures : Figures) = figures.figure.plot;
  implicit def iXYPlot(figure  : Figure)  = figure.plot;
  
  //
  // Plotting
  //

  /** Selects the given figure */
  def figure(select:Int)(implicit figures : Figures) : Figure = {
    figures.figure = select-1
    figures.figure.refresh
    return figures.figure
  }

  def figure()(implicit figures : Figures) : Figure =
    figures.figure;

  /** Clears the current figure */
  def clf()(implicit figure : Figure) = {
    figure.clear;
  }
  
  /** Saves the current figure at the requested dpi to the given filename. */
  def saveas(filename : String, dpi : Int = 72)(implicit figure : Figure) : Unit = {
    import java.io._;
    
    // make sure figure is visible or saved image will come up empty
    figure.refresh();
    
    lazy val fos = new FileOutputStream(filename);
    if (filename.toLowerCase.endsWith(".eps")) {
      figure.writeEPS(fos,dpi);
      fos.close();
    } else if (filename.toLowerCase.endsWith(".png")) {
      figure.writePNG(fos,dpi);
      fos.close();
    } else if (filename.toLowerCase.endsWith(".pdf")) {
      figure.writePDF(fos);
      fos.close();
    } else {
      throw new IOException("Unrecognized file extension: should be eps, png, or pdf");
    }
  }
  
  /** Selects the given subplot */
  def subplot(rows:Int,cols:Int,select:Int)(implicit figure : Figure) : XYPlot = {
    figure.rows = rows
    figure.cols = cols
    figure.plot = select-1
    figure.refresh
{'fraction_non_alphanumeric': 0.09878738884397736, 'fraction_numerical': 0.00582053354890865, 'mean_word_length': 3.3779193205944797, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6637893', 'n_tokens_mistral': 1828, 'n_tokens_neox': 1779, 'n_words': 630}