/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   Layer.cpp
 * Author: jens
 * 
 * Created on 25. Oktober 2019, 08:13
 */

#include "Layer.hpp"
using namespace std;

Layer::Layer(const string &name, size_t id, size_t numVisibleX, size_t numVisibleY, size_t numHidden)
: Rbm(numVisibleX*numVisibleY, numHidden)
, next(nullptr)	
, prev(nullptr)	
, m_name(name)
, m_id(id)
, m_numVisibleX(numVisibleX)
, m_numVisibleY(numVisibleY)
{
	cout << "Create Layer " << m_name << "." << to_string((int)m_id) << endl;
	m_weightsFile = m_name + "." + to_string((int)m_id) + string(".weights.dat");
}

Layer::Layer(const Layer& orig)
: Rbm(orig.bv().n_elem, orig.bh().n_elem)
, next(nullptr)	
, prev(nullptr)	
, m_name(orig.m_name)
, m_weightsFile(orig.m_weightsFile)
, m_id(orig.m_id)
, m_numVisibleX(orig.m_numVisibleX)
, m_numVisibleY(orig.m_numVisibleY)
{
}

Layer::~Layer()
{
}

bool Layer::loadWeights(const string &prjname)
{
	int numVisibleX;
	int numVisibleY;
	int numHidden;

	string filename = m_weightsFile;
	if (prjname.size() > 0)
	{
		filename = prjname + "." + m_weightsFile;
	}

	FILE *pFile = fopen(filename.c_str(),"r");
	if (!pFile)
	{
		std::cout << "loadWeights(): Could not open " << filename << " for reading!" << std::endl;
		return false;
	}

	std::cout << "Importing weights for " << m_name << "." << to_string((int)m_id) << std::endl;
	int result = fscanf(pFile, "%d %d %d\n", &numVisibleX, &numVisibleY, &numHidden);
	if (result < 0)
	{
		return false;
	}

	size_t numVisible = numVisibleX*numVisibleY;
	
	int i, j;
	float v;
	for (i=0; i < numVisible; i++)
	{
		result = fscanf(pFile, "%f", &v);
		if (result > 0)
		{
			m_bv(i) = v;
		}
	}
	for (i=0; i < numHidden; i++)
	{
		result = fscanf(pFile, "%f", &v);
		if (result > 0)
		{
			m_bh(i) = v;
		}
	}
	for (i=0; i < numVisible; i++)
	{
		for (j=0; j < numHidden; j++)
		{

			result = fscanf(pFile, "%f", &v);
			if (result > 0)
			{
				m_w(i, j) = v;
			}
		}
	}
	fclose(pFile);
	
	return true;
}
	
bool Layer::saveWeights(const string &prjname)
{
	int numHidden = m_bh.n_elem;
	int numVisible = m_bv.n_elem;
	string filename = m_weightsFile;
	if (prjname.size() > 0)
	{
		filename = prjname + "." + m_weightsFile;
	}

	FILE *pFile = fopen(filename.c_str(),"w");
	if (!pFile)
	{
		std::cout << "saveWeights(): Could not open " << filename << " for writing!" << std::endl;
		return false;
	}

	std::cout << "Exporting weights for " << m_name << "." << to_string((int)m_id) << std::endl;

	fprintf(pFile, "%d %d %d\n", (int)m_numVisibleX, (int)m_numVisibleY, numHidden);
	int i, j;

	for (i=0; i < numVisible; i++)
	{
		fprintf(pFile, "%3.6f\n", m_bv(i));
	}
	for (i=0; i < numHidden; i++)
	{
		fprintf(pFile, "%3.6f\n", m_bh(i));
	}
	for (i=0; i < numVisible; i++)
	{
		for (j=0; j < numHidden; j++)
		{
			fprintf(pFile, "%3.6f ", m_w(i,j));
		}
		fprintf(pFile, "\n");
	}
	fclose(pFile);

	return true;
}

Json::Value Layer::toJson() const
{
	std::cout << "Exporting Layer " << to_string((int)m_id) << std::endl;
	Json::Value layer;
	layer["name"] = m_name;
	layer["id"] = (int)m_id;
	layer["weights_file"] = m_weightsFile;
	layer["numVisibleX"] = (int)m_numVisibleX;
	layer["numVisibleY"] = (int)m_numVisibleY;
	layer["numHidden"] = (int)m_bh.n_elem;
	layer["rbm"] = Rbm::toJson();
	
	return layer;

}
