Slight re-organization of C/C++ tools. Significant modifications to support observational data. Python and pipeline scripts added

This commit is contained in:
P.M. Sutter 2012-10-31 10:43:15 -05:00
parent 15496df4ff
commit 14abbc2018
42 changed files with 16252 additions and 557 deletions

View file

@ -0,0 +1,56 @@
#include <vector>
#include <healpix_map.h>
#include <healpix_map_fitsio.h>
#include "contour_pixels.hpp"
using namespace std;
static const bool DEBUG = true;
void computeFilledPixels(Healpix_Map<float>& m, vector<int>& filled)
{
filled.clear();
for (int p = 0; p < m.Npix(); p++)
if (m[p] > 0)
filled.push_back(p);
}
void computeContourPixels(Healpix_Map<float>& m, vector<int>& contour)
{
contour.clear();
for (int p = 0; p < m.Npix(); p++)
{
fix_arr<int, 8> result;
m.neighbors(p, result);
for (int q = 0; q < 8; q++)
{
if (result[q] < 0)
continue;
float delta = (m[p]-0.5)*(m[result[q]]-0.5);
if (delta < 0)
{
contour.push_back(p);
// This is boundary go to next pixel
break;
}
}
}
if (DEBUG)
{
Healpix_Map<int> contour_map;
contour_map.SetNside(m.Nside(), RING);
contour_map.fill(0);
for (int p = 0; p < contour.size(); p++)
{
contour_map[contour[p]]=1;
}
fitshandle h;
h.create("!contour_map.fits");
write_Healpix_map_to_fits(h, contour_map, planckType<int>());
}
}

View file

@ -0,0 +1,11 @@
#ifndef __CONTOUR_PIXELS_HPP
#define __CONTOUR_PIXELS_HPP
#include <vector>
#include <healpix_map.h>
void computeContourPixels(Healpix_Map<float>& map, std::vector<int>& contour);
void computeFilledPixels(Healpix_Map<float>& map, std::vector<int>& contour);
#endif

View file

@ -0,0 +1,33 @@
#ifndef __MYGSL_INTEGRATE_HPP
#define __MYGSL_INTEGRATE_HPP
#include <gsl/gsl_integration.h>
template<typename FunT>
double gslSpecialFunction(double x, void *param)
{
FunT *f = (FunT *)param;
return (*f)(x);
}
template<typename FunT>
double gslIntegrate(FunT& v, double a, double b, double prec, int NPTS = 1024)
{
gsl_integration_workspace *w = gsl_integration_workspace_alloc(NPTS);
gsl_function f;
double result;
double abserr;
f.function = &gslSpecialFunction<FunT>;
f.params = &v;
gsl_integration_qag(&f, a, b, prec, 0, NPTS, GSL_INTEG_GAUSS61,
w, &result, &abserr);
gsl_integration_workspace_free(w);
return result;
}
#endif

View file

@ -0,0 +1,210 @@
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <sstream>
#include <algorithm>
#include "loadZobov.hpp"
#include <CosmoTool/fortran.hpp>
using namespace std;
using namespace CosmoTool;
bool loadZobov(const char *descName, const char *adjName, const char *voidsName,
const char *volName, ZobovRep& z)
{
ifstream descFile(descName);
ifstream adjFile(adjName);
ifstream volFile(voidsName);
int32_t numParticles, numZones, numPinZone;
int32_t totalParticles;
int32_t numVoids;
int32_t minParticlesInZone, maxParticlesInZone;
adjFile.read((char *)&numParticles, sizeof(numParticles));
adjFile.read((char *)&numZones, sizeof(numZones));
if (!adjFile)
return false;
cout << "Number of particles = " << numParticles << endl;
cout << "Number of zones = " << numZones << endl;
totalParticles = 0;
minParticlesInZone = -1;
maxParticlesInZone = -1;
z.allZones.resize(numZones);
for (int zone = 0; zone < numZones; zone++)
{
adjFile.read((char *)&numPinZone, sizeof(numPinZone));
if (!adjFile)
{
cout << "Problem on the zone " << zone << " / " << numZones << endl;
return false;
}
z.allZones[zone].pId.resize(numPinZone);
adjFile.read((char *)&z.allZones[zone].pId[0], sizeof(int)*numPinZone);
if (maxParticlesInZone < 0 || numPinZone > maxParticlesInZone)
maxParticlesInZone = numPinZone;
if (minParticlesInZone < 0 || numPinZone < minParticlesInZone)
minParticlesInZone = numPinZone;
totalParticles += numPinZone;
}
cout << "Zoned " << totalParticles << endl;
cout << "Minimum number of particles in zone = " << minParticlesInZone << endl;
cout << "Maximum number of particles in zone = " << maxParticlesInZone << endl;
if (totalParticles != numParticles)
{
cerr << "The numbers of particles are inconsistent ! (" << totalParticles << " vs " << numParticles << ")"<< endl;
abort();
}
volFile.read((char *)&numVoids, sizeof(numVoids));
if (!volFile)
return false;
cout << "Number of voids = " << numVoids << endl;
z.allVoids.resize(numVoids);
for (int v = 0; v < numVoids; v++)
{
int32_t numZinV;
volFile.read((char *)&numZinV, sizeof(numZinV));
if (!volFile)
return false;
z.allVoids[v].zId.resize(numZinV);
int *zId = new int[numZinV];
volFile.read((char *)zId, sizeof(int)*numZinV);
for (int k = 0; k < numZinV; k++)
z.allVoids[v].zId[k] = zId[k];
std::sort(&z.allVoids[v].zId[0], &z.allVoids[v].zId[numZinV]);
delete[] zId;
}
if (volName != 0)
{
cout << "Loading particle volumes (requested)" << endl;
ifstream f(volName);
int numParticles;
if (!f)
{
cerr << "No such file " << volName << endl;
abort();
}
f.read((char *)&numParticles, sizeof(int));
z.particleVolume.resize(numParticles);
f.read((char *)&z.particleVolume[0], sizeof(float)*numParticles);
}
cout << "Loading description" << endl;
string line;
getline(descFile, line);
getline(descFile, line);
getline(descFile, line);
while (!descFile.eof())
{
istringstream lineStream(line.c_str());
int orderId, volId, coreParticle, numParticlesInZone, numZonesInVoid, numInVoid;
float coreDensity, volumeZone, volumeVoid, densityContrast;
float probability;
lineStream
>> orderId
>> volId
>> coreParticle
>> coreDensity
>> volumeZone
>> numParticlesInZone
>> numZonesInVoid
>> volumeVoid
>> numInVoid
>> densityContrast
>> probability;
if (!lineStream)
{
cerr << "Error in text stream" << endl;
abort();
}
z.allVoids[volId].proba = probability;
z.allVoids[volId].volume = volumeVoid;
z.allVoids[volId].numParticles = numInVoid;
z.allVoids[volId].coreParticle = coreParticle;
// Sanity check
int actualNumber = 0;
for (int j = 0; j < z.allVoids[volId].zId.size(); j++)
{
int zzid = z.allVoids[volId].zId[j];
actualNumber += z.allZones[zzid].pId.size();
}
if (actualNumber != numInVoid)
{
cerr << "Sanity check failed."
<< " The number of particles in the description ("
<< numInVoid
<< ") is different from the one in the file ("
<< actualNumber << ")" << endl;
}
getline(descFile, line);
}
cout << "Done loading" << endl;
return true;
}
bool loadZobovParticles(const char *fname, std::vector<ZobovParticle>& particles)
{
UnformattedRead f(fname);
int N;
f.beginCheckpoint();
N = f.readInt32();
f.endCheckpoint();
particles.resize(N);
f.beginCheckpoint();
for (int i = 0; i < N; i++)
{
particles[i].x = f.readReal32();
}
f.endCheckpoint();
f.beginCheckpoint();
for (int i = 0; i < N; i++)
{
particles[i].y = f.readReal32();
}
f.endCheckpoint();
f.beginCheckpoint();
for (int i = 0; i < N; i++)
{
particles[i].z = f.readReal32();
}
f.endCheckpoint();
return true;
}

View file

@ -0,0 +1,39 @@
#ifndef __LOAD_ZOBOV_HPP
#define __LOAD_ZOBOV_HPP
#include <vector>
struct ZobovZone
{
std::vector<int> pId;
};
struct ZobovVoid
{
std::vector<int> zId;
float proba;
int numParticles, coreParticle;
float volume;
float barycenter[3];
float nearestBoundary;
};
struct ZobovRep
{
std::vector<ZobovZone> allZones;
std::vector<ZobovVoid> allVoids;
std::vector<float> particleVolume;
};
struct ZobovParticle
{
float x, y, z;
};
bool loadZobov(const char *descName,
const char *adjName, const char *voidName,
const char *volName, ZobovRep& z);
bool loadZobovParticles(const char *fname, std::vector<ZobovParticle>& particles);
#endif

View file

@ -0,0 +1,69 @@
#include <netcdfcpp.h>
#include <CosmoTool/fortran.hpp>
#include "particleInfo.hpp"
using namespace std;
using namespace CosmoTool;
bool loadParticleInfo(ParticleInfo& info,
const std::string& particles,
const std::string& extra_info)
{
int numpart;
NcFile f_info(extra_info.c_str());
if (!f_info.is_valid())
return false;
info.ranges[0][0] = f_info.get_att("range_x_min")->as_double(0);
info.ranges[0][1] = f_info.get_att("range_x_max")->as_double(0);
info.ranges[1][0] = f_info.get_att("range_y_min")->as_double(0);
info.ranges[1][1] = f_info.get_att("range_y_max")->as_double(0);
info.ranges[2][0] = f_info.get_att("range_z_min")->as_double(0);
info.ranges[2][1] = f_info.get_att("range_z_max")->as_double(0);
info.mask_index = f_info.get_att("mask_index")->as_int(0); //PMS
for (int i = 0; i < 3; i++)
info.length[i] = info.ranges[i][1] - info.ranges[i][0];
try
{
UnformattedRead f(particles);
float mul, offset;
f.beginCheckpoint();
numpart = f.readInt32();
f.endCheckpoint();
info.particles.resize(numpart);
offset = info.ranges[0][0];
mul = info.ranges[0][1] - info.ranges[0][0];
f.beginCheckpoint();
for (int i = 0; i < numpart; i++)
info.particles[i].x = mul*f.readReal32();
f.endCheckpoint();
offset = info.ranges[1][0];
mul = info.ranges[1][1] - info.ranges[1][0];
f.beginCheckpoint();
for (int i = 0; i < numpart; i++)
info.particles[i].y = mul*f.readReal32();
f.endCheckpoint();
offset = info.ranges[2][0];
mul = info.ranges[2][1] - info.ranges[2][0];
f.beginCheckpoint();
for (int i = 0; i < numpart; i++)
info.particles[i].z = mul*f.readReal32();
f.endCheckpoint();
}
catch (const NoSuchFileException& e)
{
return false;
}
return true;
}

View file

@ -0,0 +1,25 @@
#ifndef _PARTICLE_INFO_HEADER_HPP
#define _PARTICLE_INFO_HEADER_HPP
#include <vector>
#include <string>
struct ParticleData {
float x, y, z;
};
typedef std::vector<ParticleData> ParticleVector;
struct ParticleInfo
{
ParticleVector particles;
float ranges[3][2];
float length[3];
int mask_index; // PMS
};
bool loadParticleInfo(ParticleInfo& info,
const std::string& particles,
const std::string& extra_info);
#endif

View file

@ -0,0 +1,346 @@
#ifndef _VOID_TREE_HPP
#define _VOID_TREE_HPP
#include <iostream>
#include <stdint.h>
#include "loadZobov.hpp"
#include <list>
#include <set>
#include <vector>
struct VoidNode
{
int vid;
VoidNode *parent;
std::list<VoidNode *> children;
};
struct VoidNodeOnDisk
{
int vid;
int parent;
};
class VoidTree
{
protected:
uint32_t totalNumNodes, activeNodes;
VoidNode *nodes;
VoidNode *rootNode;
ZobovRep& zobov;
public:
typedef std::list<VoidNode *> VoidList;
void dumpTree(std::ostream& o)
{
VoidNodeOnDisk data;
o.write((char*)&activeNodes, sizeof(uint32_t));
for (uint32_t i = 0; i < activeNodes; i++)
{
data.vid = nodes[i].vid;
if (nodes[i].parent == 0)
data.parent = -1;
else
data.parent = nodes[i].parent - nodes;
o.write((char *)&data, sizeof(VoidNodeOnDisk));
}
}
int lookupParent(int voidId, const std::vector<std::list<int> >& voids_for_zones)
{
int lastSize = 0x7fffffff;
int goodParent = -1;
ZobovVoid &ref_void = zobov.allVoids[voidId];
const std::list<int>& candidateList = voids_for_zones[ref_void.zId.front()];
std::list<int>::const_iterator iter_candidate = candidateList.begin();
// std::cout << "candidate list size is " << candidateList.size() << std::endl;
while (iter_candidate != candidateList.end())
{
int vid_candidate = *iter_candidate;
if (vid_candidate == voidId)
break;
++iter_candidate;
}
if (iter_candidate == candidateList.end())
{
// std::cout << "Failure to lookup parent" << std::endl;
return -1;
}
// voidId must be in the list.
// assert(iter_candidate != candidateList.end());
// Go back
iter_candidate = candidateList.end();
int vid_good_candidate = -1;
int old_good_candidate_size = zobov.allZones.size()+1;
do
{
int vid_candidate;
--iter_candidate;
vid_candidate = *iter_candidate;
std::vector<int>& candidate_zIds = zobov.allVoids[vid_candidate].zId;
if (voidId == vid_candidate)
continue;
if (candidate_zIds.size() < ref_void.zId.size())
{
continue;
}
int counter = 0;
// All zones id are sorted in each void. So we just have parse the
// vector of zones and check whether all the zones in ref_void.zId is
// in iter_candidate->zId, the list is analyzed only once.
// THOUGHT: candidateList may contain directly the information. It would suffice to have the void ids sorted according to volume. Then we just have to jump to the indice just smaller than voidId.
int k = 0;
for (int j = 0; j < candidate_zIds.size() && k < ref_void.zId.size(); j++)
{
if (candidate_zIds[j] == ref_void.zId[k])
k++;
else if (candidate_zIds[j] > ref_void.zId[k])
break;
}
if (k==ref_void.zId.size())
{
if (candidate_zIds.size() < old_good_candidate_size)
{
vid_good_candidate = vid_candidate;
old_good_candidate_size = candidate_zIds.size();
}
// std::cout << "Found parent " << vid_candidate << std::endl;
// return vid_candidate;
}
// Go bigger, though I would say we should not to.
}
while (iter_candidate != candidateList.begin()) ;
if (vid_good_candidate < 0)
std::cout << "Failure to lookup parent (2)" << std::endl;
return vid_good_candidate;
}
VoidTree(ZobovRep& rep, std::istream& disk)
: zobov(rep)
{
totalNumNodes = rep.allVoids.size();
disk.read((char *)&activeNodes, sizeof(uint32_t));
nodes = new VoidNode[activeNodes];
rootNode = 0;
for (uint32_t i = 0; i < activeNodes; i++)
{
VoidNodeOnDisk data;
disk.read((char *)&data, sizeof(data));
nodes[i].vid = data.vid;
if (data.parent < 0)
{
if (rootNode != 0)
{
std::cerr << "Multiple root to the tree !!!" << std::endl;
abort();
}
nodes[i].parent = 0;
rootNode = &nodes[i];
}
else
{
nodes[i].parent = nodes + data.parent;
nodes[i].parent->children.push_back(&nodes[i]);
}
}
computeMaxDepth();
computeChildrenByNode();
}
VoidTree(ZobovRep& rep)
: zobov(rep)
{
totalNumNodes = rep.allVoids.size();
std::vector<std::list<int> > voids_for_zones;
voids_for_zones.resize(rep.allZones.size());
for (int i = 0; i < rep.allVoids.size(); i++)
{
ZobovVoid& v = rep.allVoids[i];
for (int j = 0; j < v.zId.size(); j++)
voids_for_zones[v.zId[j]].push_back(i);
}
// One additional for the mega-root
nodes = new VoidNode[totalNumNodes+1];
for (int i = 0; i <= totalNumNodes; i++)
{
nodes[i].vid = i;
nodes[i].parent = 0;
}
std::cout << "Linking voids together..." << std::endl;
double volMin = 0;// 4*M_PI/3*pow(4.*512/500.,3);
int inserted = 0;
for (int i = 0; i < rep.allVoids.size(); i++)
{
if (rep.allVoids[i].volume < volMin) continue;
int p = lookupParent(i, voids_for_zones);
if ((i % 1000) == 0) std::cout << i << std::endl;
if (p >= 0)
{
nodes[p].children.push_back(&nodes[i]);
nodes[i].parent = &nodes[p];
}
inserted++;
}
assert(inserted <= totalNumNodes);
rootNode = &nodes[inserted];
rootNode->vid = -1;
rootNode->parent = 0;
for (int i = 0; i < inserted; i++)
if (nodes[i].parent == 0)
{
nodes[i].parent = rootNode;
rootNode->children.push_back(&nodes[i]);
}
activeNodes = inserted+1;
computeMaxDepth();
computeChildrenByNode();
}
~VoidTree()
{
delete[] nodes;
}
int _depth_computer(VoidNode *node)
{
VoidList::iterator i = node->children.begin();
int d = 0;
while (i != node->children.end())
{
d = std::max(d,_depth_computer(*i));
++i;
}
return d+1;
}
void computeMaxDepth()
{
std::cout << "maximum depth is " << _depth_computer(rootNode) << std::endl;
}
struct _children_stat {
int num, min_num, max_num, num_zero,num_one, num_multiple;
};
void _children_computer(VoidNode *node, _children_stat& s)
{
VoidList::iterator i = node->children.begin();
int d = 0, j = 0;
while (i != node->children.end())
{
_children_computer(*i, s);
++i;
++j;
}
s.num += j;
if (j!= 0)
s.min_num = std::min(s.min_num, j);
else s.num_zero ++;
if (j==1) s.num_one++;
if (j>1) s.num_multiple++;
s.max_num = std::max(s.max_num, j);
}
void computeChildrenByNode()
{
_children_stat s;
s.num = 0;
s.min_num = activeNodes+1;
s.max_num = s.num_zero = s.num_one =s.num_multiple= 0;
_children_computer(rootNode, s);
std::cout << "Average children by node " << s.num*1.0/activeNodes << " , " << s.min_num << " " << s.max_num << " " << s.num_zero << " " << s.num_one << " " << s.num_multiple << std::endl;
}
int getParent(int vid) const
{
assert(nodes[vid].parent != 0);
return nodes[vid].parent->vid;
}
const VoidList& getChildren(int vid) const
{
return nodes[vid].children;
}
VoidNode *getRoot() { return rootNode; }
template<typename T>
void walkNode(VoidNode *node, T& traverse)
{
if (!traverse(node))
return;
VoidList::iterator i = node->children.begin();
while (i != node->children.end())
{
walkNode(*i, traverse);
++i;
}
}
template<typename T>
void walk(T& traverse)
{
walkNode(rootNode, traverse);
}
template<typename T, typename T2>
void walkNodeWithMark(VoidNode *node, T& traverse, const T2& mark)
{
T2 new_mark = mark;
if (!traverse(node, new_mark))
return;
VoidList::iterator i = node->children.begin();
while (i != node->children.end())
{
walkNodeWithMark(*i, traverse, new_mark);
++i;
}
}
template<typename T,typename T2>
void walkWithMark(T& traverse, T2 mark)
{
walkNodeWithMark(rootNode, traverse, mark);
}
};
#endif