void ChangeResolutionAction::IncreaseFrequency(TimeFrequencyData &originalData, const TimeFrequencyData &changedData, bool restoreImage, bool restoreMask) { if(restoreImage) { size_t imageCount = originalData.ImageCount(); if(imageCount != changedData.ImageCount()) throw std::runtime_error("When restoring resolution in change resolution action, original data and changed data do not have the same number of images"); for(size_t i=0;i<imageCount;++i) { Image2DCPtr image = changedData.GetImage(i); Image2DPtr newImage(new Image2D(image->EnlargeVertically(_frequencyDecreaseFactor, originalData.ImageHeight()))); originalData.SetImage(i, newImage); } } if(restoreMask) { originalData.SetMask(changedData); size_t maskCount = originalData.MaskCount(); for(size_t i=0;i<maskCount;++i) { Mask2DCPtr mask = changedData.GetMask(i); Mask2DPtr newMask = Mask2D::CreateUnsetMaskPtr(originalData.ImageWidth(), originalData.ImageHeight()); newMask->EnlargeVerticallyAndSet(*mask, _frequencyDecreaseFactor); originalData.SetMask(i, newMask); } } }
std::pair<TimeFrequencyData,TimeFrequencyMetaDataPtr> RSPReader::ReadSingleBeamlet(unsigned long timestepStart, unsigned long timestepEnd, unsigned beamletCount, unsigned beamletIndex) { std::pair<TimeFrequencyData,TimeFrequencyMetaDataPtr> data = ReadAllBeamlets(timestepStart, timestepEnd, beamletCount); const unsigned width = timestepEnd - timestepStart; Image2DPtr realX = Image2D::CreateZeroImagePtr(width, 1); Image2DPtr imaginaryX = Image2D::CreateZeroImagePtr(width, 1); Image2DPtr realY = Image2D::CreateZeroImagePtr(width, 1); Image2DPtr imaginaryY = Image2D::CreateZeroImagePtr(width, 1); Mask2DPtr mask = Mask2D::CreateUnsetMaskPtr(width, 1); TimeFrequencyData allX = data.first.Make(Polarization::XX); TimeFrequencyData allY = data.first.Make(Polarization::YY); Image2DCPtr xr = allX.GetRealPart(); Image2DCPtr xi = allX.GetImaginaryPart(); Image2DCPtr yr = allY.GetRealPart(); Image2DCPtr yi = allY.GetImaginaryPart(); Mask2DCPtr maskWithBeamlets = data.first.GetSingleMask(); for(unsigned x=0;x<width;++x) { realX->SetValue(x, 0, xr->Value(x, beamletIndex)); imaginaryX->SetValue(x, 0, xi->Value(x, beamletIndex)); realY->SetValue(x, 0, yr->Value(x, beamletIndex)); imaginaryY->SetValue(x, 0, yi->Value(x, beamletIndex)); mask->SetValue(x, 0, maskWithBeamlets->Value(x, beamletIndex)); } data.first = TimeFrequencyData(Polarization::XX, realX, imaginaryX, Polarization::YY, realY, imaginaryY); data.first.SetGlobalMask(mask); BandInfo band = data.second->Band(); band.channels[0] = data.second->Band().channels[beamletIndex]; band.channels.resize(1); data.second->SetBand(band); return data; }
void StatisticalFlagger::FlagFrequency(Mask2DPtr mask, size_t y) { for(size_t x=0;x<mask->Width();++x) { mask->SetValue(x, y, true); } }
void StatisticalFlagger::FlagTime(Mask2DPtr mask, size_t x) { for(size_t y=0;y<mask->Height();++y) { mask->SetValue(x, y, true); } }
void RFIGuiController::PlotPowerTime() { if(IsImageLoaded()) { Plot2D &plot = _plotManager->NewPlot2D("Power over time"); plot.SetLogarithmicYAxis(true); TimeFrequencyData activeData = ActiveData(); Image2DCPtr image = activeData.GetSingleImage(); Mask2DPtr mask = Mask2D::CreateSetMaskPtr<false>(image->Width(), image->Height()); Plot2DPointSet &totalPlot = plot.StartLine("Total"); RFIPlots::MakePowerTimePlot(totalPlot, image, mask, MetaData()); mask = Mask2D::CreateCopy(activeData.GetSingleMask()); if(!mask->AllFalse()) { Plot2DPointSet &uncontaminatedPlot = plot.StartLine("Uncontaminated"); RFIPlots::MakePowerTimePlot(uncontaminatedPlot, image, mask, MetaData()); mask->Invert(); Plot2DPointSet &rfiPlot = plot.StartLine("RFI"); RFIPlots::MakePowerTimePlot(rfiPlot, image, mask, MetaData()); } _plotManager->Update(); } }
void RFIGuiController::PlotPowerRMS() { if(IsImageLoaded()) { Plot2D &plot = _plotManager->NewPlot2D("Spectrum RMS"); plot.SetLogarithmicYAxis(true); TimeFrequencyData activeData = ActiveData(); Image2DCPtr image = activeData.GetSingleImage(); Mask2DPtr mask = Mask2D::CreateSetMaskPtr<false>(image->Width(), image->Height()); Plot2DPointSet &beforeSet = plot.StartLine("Before"); RFIPlots::MakeRMSSpectrumPlot(beforeSet, image, mask); mask = Mask2D::CreateCopy(activeData.GetSingleMask()); if(!mask->AllFalse()) { Plot2DPointSet &afterSet = plot.StartLine("After"); RFIPlots::MakeRMSSpectrumPlot(afterSet, image, mask); //mask->Invert(); //Plot2DPointSet &rfiSet = plot.StartLine("RFI"); //RFIPlots::MakeRMSSpectrumPlot(rfiSet, _timeFrequencyWidget.Image(), mask); } _plotManager->Update(); } }
void RFIGuiController::PlotPowerSNR() { Image2DCPtr image = ActiveData().GetSingleImage(), model = RevisedData().GetSingleImage(); if(IsImageLoaded()) { Plot2D &plot = _plotManager->NewPlot2D("SNR spectrum"); plot.SetLogarithmicYAxis(true); Mask2DPtr mask = Mask2D::CreateSetMaskPtr<false>(image->Width(), image->Height()); Plot2DPointSet &totalPlot = plot.StartLine("Total"); RFIPlots::MakeSNRSpectrumPlot(totalPlot, image, model, mask); mask = Mask2D::CreateCopy(ActiveData().GetSingleMask()); if(!mask->AllFalse()) { Plot2DPointSet &uncontaminatedPlot = plot.StartLine("Uncontaminated"); RFIPlots::MakeSNRSpectrumPlot(uncontaminatedPlot, image, model, mask); mask->Invert(); Plot2DPointSet &rfiPlot = plot.StartLine("RFI"); RFIPlots::MakeSNRSpectrumPlot(rfiPlot, image, model, mask); } _plotManager->Update(); } }
void StatisticalFlagger::ApplyMarksInFrequency(Mask2DPtr mask, int **flagMarks) { for(size_t x=0;x<mask->Width();++x) { int startedCount = 0; for(size_t y=0;y<mask->Height();++y) { startedCount += flagMarks[y][x]; if(startedCount > 0) mask->SetValue(x, y, true); } } }
void StatisticalFlagger::EnlargeFlags(Mask2DPtr mask, size_t timeSize, size_t frequencySize) { Mask2DCPtr old = Mask2D::CreateCopy(mask); for(size_t y=0;y<mask->Height();++y) { size_t top, bottom; if(y > frequencySize) top = y - frequencySize; else top = 0; if(y + frequencySize < mask->Height() - 1) bottom = y + frequencySize; else bottom = mask->Height() - 1; for(size_t x=0;x<mask->Width();++x) { size_t left, right; if(x > timeSize) left = x - timeSize; else left = 0; if(x + timeSize < mask->Width() - 1) right = x + timeSize; else right = mask->Width() - 1; if(SquareContainsFlag(old, left, top, right, bottom)) mask->SetValue(x, y, true); } } }
void StatisticalFlagger::LineRemover(Mask2DPtr mask, size_t maxTimeContamination, size_t maxFreqContamination) { for(size_t x=0;x<mask->Width();++x) { size_t count = 0; for(size_t y=0;y<mask->Height();++y) { if(mask->Value(x,y)) ++count; } if(count > maxFreqContamination) FlagTime(mask, x); } for(size_t y=0;y<mask->Height();++y) { size_t count = 0; for(size_t x=0;x<mask->Width();++x) { if(mask->Value(x,y)) ++count; } if(count > maxTimeContamination) FlagFrequency(mask, y); } }
void StatisticalFlagger::DensityTimeFlagger(Mask2DPtr mask, num_t minimumGoodDataRatio) { num_t width = 2.0; size_t iterations = 0, step = 1; bool reverse = false; //"sums represents the number of flags in a certain range int **sums = new int*[mask->Height()]; // flagMarks are integers that represent the number of times an area is marked as the // start or end of a flagged area. For example, if flagMarks[0][0] = 0, it is not the start or // end of an area. If it is 1, it is the start. If it is -1, it is the end. A range of // [2 0 -1 -1 0] produces a flag mask [T T T T F]. int **flagMarks = new int*[mask->Height()]; for(size_t y=0;y<mask->Height();++y) { sums[y] = new int[mask->Width()]; flagMarks[y] = new int[mask->Width()]; for(size_t x=0;x<mask->Width();++x) flagMarks[y][x] = 0; } MaskToInts(mask, sums); while(width < mask->Width()) { ++iterations; SumToLeft(mask, sums, (size_t) width, step, reverse); const int maxFlagged = (int) floor((1.0-minimumGoodDataRatio)*(num_t)(width)); ThresholdTime(mask, flagMarks, sums, maxFlagged, (size_t) width); num_t newWidth = width * 1.05; if((size_t) newWidth == (size_t) width) newWidth = width + 1.0; step = (size_t) (newWidth - width); width = newWidth; reverse = !reverse; } ApplyMarksInTime(mask, flagMarks); for(size_t y=0;y<mask->Height();++y) { delete[] sums[y]; delete[] flagMarks[y]; } delete[] sums; delete[] flagMarks; }
void ThresholdMitigater::VerticalSumThresholdLarge(Image2DCPtr input, Mask2DPtr mask, num_t threshold) { Mask2DPtr maskCopy = Mask2D::CreateCopy(mask); const size_t width = mask->Width(), height = mask->Height(); if(Length <= height) { for(size_t x=0;x<width;++x) { num_t sum = 0.0; size_t count = 0, yTop, yBottom; for(yBottom=0;yBottom<Length-1;++yBottom) { if(!mask->Value(x, yBottom)) { sum += input->Value(x, yBottom); ++count; } } yTop = 0; while(yBottom < height) { // add the sample at the bottom if(!mask->Value(x, yBottom)) { sum += input->Value(x, yBottom); ++count; } // Check if(count>0 && fabs(sum/count) > threshold) { for(size_t i=0;i<Length;++i) maskCopy->SetValue(x, yTop + i, true); } // subtract the sample at the top if(!mask->Value(x, yTop)) { sum -= input->Value(x, yTop); --count; } ++yTop; ++yBottom; } } } mask->Swap(maskCopy); }
void ImageTile::LineThreshold(bool evaluateBaseline, long double mean, long double variance, bool convolve) { Image2DPtr input = Image2D::CreateEmptyImagePtr(_scanCount, _channelCount); Mask2DPtr output = Mask2D::CreateSetMaskPtr<false>(_scanCount, _channelCount); if(evaluateBaseline) { for(unsigned channel = 0;channel<_channelCount;++channel) for(unsigned scan = 0;scan<_scanCount;++scan) input->SetValue(scan, channel, GetValueAt(channel, scan) - EvaluateBaselineFunction(scan, channel)); } else { for(unsigned channel = 0;channel<_channelCount;++channel) for(unsigned scan = 0;scan<_scanCount;++scan) input->SetValue(scan, channel, GetValueAt(channel, scan) - mean); } ThresholdMitigater::SumThreshold(input, output, 1, _trigger * variance); ThresholdMitigater::SumThreshold(input, output, 2, _trigger * variance * 1.6); ThresholdMitigater::SumThreshold(input, output, 3, _trigger * variance * 2.2); ThresholdMitigater::SumThreshold(input, output, 5, _trigger * variance * 3.0); ThresholdMitigater::SumThreshold(input, output, 10, _trigger * variance * 5.0); unsigned count = 0; for(unsigned channel = 0;channel<_channelCount;++channel) { for(unsigned scan = 0;scan<_scanCount;++scan) { if(output->Value(scan, channel)) { Window(scan, channel); count++; } } } while(count*2 > _channelCount*_scanCount) { size_t x = (size_t) (RNG::Uniform()*_scanCount); size_t y = (size_t) (RNG::Uniform()*_channelCount); if(_isWindowed[y][x]) { count--; _isWindowed[y][x]=false; } } if(convolve) ConvolveWindows(); }
Mask2DCPtr ImageWidget::GetActiveMask() const { if(!HasImage()) throw std::runtime_error("GetActiveMask() called without image"); const bool originalActive = _showOriginalMask && _originalMask != 0, altActive = _showAlternativeMask && _alternativeMask != 0; if(originalActive) { if(altActive) { Mask2DPtr mask = Mask2D::CreateCopy(_originalMask); mask->Join(_alternativeMask); return mask; } else return _originalMask; } else { if(altActive) return _alternativeMask; else return Mask2D::CreateSetMaskPtr<false>(_image->Width(), _image->Height()); } }
void ThresholdMitigater::VerticalSumThreshold(Image2DCPtr input, Mask2DPtr mask, num_t threshold) { if(Length <= input->Height()) { size_t height = input->Height()-Length+1; for(size_t y=0;y<height;++y) { for(size_t x=0;x<input->Width();++x) { num_t sum = 0.0; size_t count = 0; for(size_t i=0;i<Length;++i) { if(!mask->Value(x, y+i)) { sum += input->Value(x, y + i); count++; } } if(count>0 && fabs(sum/count) > threshold) { for(size_t i=0;i<Length;++i) mask->SetValue(x, y + i, true); } } } } }
void ThresholdMitigater::HorizontalSumThresholdLarge(Image2DCPtr input, Mask2DPtr mask, num_t threshold) { Mask2DPtr maskCopy = Mask2D::CreateCopy(mask); const size_t width = mask->Width(), height = mask->Height(); if(Length <= width) { for(size_t y=0;y<height;++y) { num_t sum = 0.0; size_t count = 0, xLeft, xRight; for(xRight=0;xRight<Length-1;++xRight) { if(!mask->Value(xRight, y)) { sum += input->Value(xRight, y); count++; } } xLeft = 0; while(xRight < width) { // add the sample at the right if(!mask->Value(xRight, y)) { sum += input->Value(xRight, y); ++count; } // Check if(count>0 && fabs(sum/count) > threshold) { maskCopy->SetHorizontalValues(xLeft, y, true, Length); } // subtract the sample at the left if(!mask->Value(xLeft, y)) { sum -= input->Value(xLeft, y); --count; } ++xLeft; ++xRight; } } } mask->Swap(maskCopy); }
void RFIGuiController::PlotDist() { if(IsImageLoaded()) { Plot2D &plot = _plotManager->NewPlot2D("Distribution"); TimeFrequencyData activeData = ActiveData(); Image2DCPtr image = activeData.GetSingleImage(); Mask2DPtr mask = Mask2D::CreateSetMaskPtr<false>(image->Width(), image->Height()); Plot2DPointSet &totalSet = plot.StartLine("Total"); RFIPlots::MakeDistPlot(totalSet, image, mask); Plot2DPointSet &uncontaminatedSet = plot.StartLine("Uncontaminated"); mask = Mask2D::CreateCopy(activeData.GetSingleMask()); RFIPlots::MakeDistPlot(uncontaminatedSet, image, mask); mask->Invert(); Plot2DPointSet &rfiSet = plot.StartLine("RFI"); RFIPlots::MakeDistPlot(rfiSet, image, mask); _plotManager->Update(); } }
void RFIGuiController::PlotPowerSpectrum() { if(IsImageLoaded()) { Plot2D &plot = _plotManager->NewPlot2D("Power spectrum"); plot.SetLogarithmicYAxis(true); TimeFrequencyData data = ActiveData(); Image2DCPtr image = data.GetSingleImage(); Mask2DPtr mask = Mask2D::CreateSetMaskPtr<false>(image->Width(), image->Height()); Plot2DPointSet &beforeSet = plot.StartLine("Before"); RFIPlots::MakePowerSpectrumPlot(beforeSet, image, mask, MetaData()); mask = Mask2D::CreateCopy(data.GetSingleMask()); if(!mask->AllFalse()) { Plot2DPointSet &afterSet = plot.StartLine("After"); RFIPlots::MakePowerSpectrumPlot(afterSet, image, mask, MetaData()); } _plotManager->Update(); } }
void Morphology::floodFill(Mask2DCPtr mask, SegmentedImagePtr output, Mask2DPtr *matrices, size_t x, size_t y, size_t z, size_t value, int **hCounts, int **vCounts) { std::stack<MorphologyPoint3D> points; MorphologyPoint3D startPoint; startPoint.x = x; startPoint.y = y; startPoint.z = z; points.push(startPoint); do { MorphologyPoint3D p = points.top(); points.pop(); if(mask->Value(p.x, p.y)) { if(output->Value(p.x, p.y) == 0) { output->SetValue(p.x, p.y, value); } else { // now we need to decide whether to change this sample to the new segment or not if(hCounts[p.y][p.x] < vCounts[p.y][p.x] && p.z == 2) output->SetValue(p.x, p.y, value); } } Mask2DPtr matrix = matrices[p.z]; matrix->SetValue(p.x, p.y, false); if((p.z == 0 || p.z == 2) && matrices[1]->Value(p.x,p.y)) { MorphologyPoint3D newP; newP.x = p.x; newP.y = p.y; newP.z = 1; points.push(newP); } if(p.x > 0 && matrix->Value(p.x-1,p.y)) { MorphologyPoint3D newP; newP.x = p.x-1; newP.y = p.y; newP.z = p.z; points.push(newP); } if(p.x < mask->Width()-1 && matrix->Value(p.x+1,p.y)) { MorphologyPoint3D newP; newP.x = p.x+1; newP.y = p.y; newP.z = p.z; newP.z = p.z; points.push(newP); } if(p.y > 0 && matrix->Value(p.x,p.y-1)) { MorphologyPoint3D newP; newP.x = p.x; newP.y = p.y-1; newP.z = p.z; points.push(newP); } if(p.y < mask->Height()-1 && matrix->Value(p.x,p.y+1)) { MorphologyPoint3D newP; newP.x = p.x; newP.y = p.y+1; newP.z = p.z; points.push(newP); } } while(points.size() != 0); }
void StatisticalFlagger::DensityFrequencyFlagger(Mask2DPtr mask, num_t minimumGoodDataRatio) { num_t width = 2.0; size_t iterations = 0, step = 1; bool reverse = false; Mask2DPtr newMask = Mask2D::CreateCopy(mask); int **sums = new int*[mask->Height()]; int **flagMarks = new int*[mask->Height()]; for(size_t y=0;y<mask->Height();++y) { sums[y] = new int[mask->Width()]; flagMarks[y] = new int[mask->Width()]; for(size_t x=0;x<mask->Width();++x) flagMarks[y][x] = 0; } MaskToInts(mask, sums); while(width < mask->Height()) { ++iterations; SumToTop(mask, sums, (size_t) width, step, reverse); const int maxFlagged = (int) floor((1.0-minimumGoodDataRatio)*(num_t)(width)); ThresholdFrequency(mask, flagMarks, sums, maxFlagged, (size_t) width); num_t newWidth = width * 1.05; if((size_t) newWidth == (size_t) width) newWidth = width + 1.0; step = (size_t) (newWidth - width); width = newWidth; reverse = !reverse; } ApplyMarksInFrequency(mask, flagMarks); for(size_t y=0;y<mask->Height();++y) { delete[] sums[y]; delete[] flagMarks[y]; } delete[] sums; delete[] flagMarks; }
void ThresholdMitigater::HorizontalVarThreshold(Image2DCPtr input, Mask2DPtr mask, size_t length, num_t threshold) { size_t width = input->Width()-length+1; for(size_t y=0;y<input->Height();++y) { for(size_t x=0;x<width;++x) { bool flag = true; for(size_t i=0;i<length;++i) { if(input->Value(x+i, y) < threshold && input->Value(x+i, y) > -threshold) { flag = false; break; } } if(flag) { for(size_t i=0;i<length;++i) mask->SetValue(x + i, y, true); } } } }
void ThresholdMitigater::VerticalVarThreshold(Image2DCPtr input, Mask2DPtr mask, size_t length, num_t threshold) { size_t height = input->Height()-length+1; for(size_t y=0;y<height;++y) { for(size_t x=0;x<input->Width();++x) { bool flag = true; for(size_t i=0;i<length;++i) { if(input->Value(x, y+i) <= threshold && input->Value(x, y+i) >= -threshold) { flag = false; break; } } if(flag) { for(size_t i=0;i<length;++i) mask->SetValue(x, y + i, true); } } } }
void ImageWidget::update(Cairo::RefPtr<Cairo::Context> cairo, unsigned width, unsigned height) { Image2DCPtr image = _image; Mask2DCPtr mask = GetActiveMask(), originalMask = _originalMask, alternativeMask = _alternativeMask; unsigned int startX = (unsigned int) round(_startHorizontal * image->Width()), startY = (unsigned int) round(_startVertical * image->Height()), endX = (unsigned int) round(_endHorizontal * image->Width()), endY = (unsigned int) round(_endVertical * image->Height()); size_t imageWidth = endX - startX, imageHeight = endY - startY; if(imageWidth > 30000) { int shrinkFactor = (imageWidth + 29999) / 30000; image = image->ShrinkHorizontally(shrinkFactor); mask = mask->ShrinkHorizontally(shrinkFactor); if(originalMask != 0) originalMask = originalMask->ShrinkHorizontally(shrinkFactor); if(alternativeMask != 0) alternativeMask = alternativeMask->ShrinkHorizontally(shrinkFactor); startX /= shrinkFactor; endX /= shrinkFactor; imageWidth = endX - startX; } num_t min, max; findMinMax(image, mask, min, max); // If these are not yet created, they are 0, so ok to delete. delete _horiScale; delete _vertScale; delete _colorScale; delete _plotTitle; if(_showXYAxes) { _vertScale = new VerticalPlotScale(); _vertScale->SetDrawWithDescription(_showYAxisDescription); _horiScale = new HorizontalPlotScale(); _horiScale->SetDrawWithDescription(_showXAxisDescription); } else { _vertScale = 0; _horiScale = 0; } if(_showColorScale) { _colorScale = new ColorScale(); _colorScale->SetDrawWithDescription(_showZAxisDescription); } else { _colorScale = 0; } if(_showXYAxes) { if(_metaData != 0 && _metaData->HasBand()) { _vertScale->InitializeNumericTicks(_metaData->Band().channels[startY].frequencyHz / 1e6, _metaData->Band().channels[endY-1].frequencyHz / 1e6); _vertScale->SetUnitsCaption("Frequency (MHz)"); } else { _vertScale->InitializeNumericTicks(-0.5 + startY, 0.5 + endY - 1.0); } if(_metaData != 0 && _metaData->HasObservationTimes()) { _horiScale->InitializeTimeTicks(_metaData->ObservationTimes()[startX], _metaData->ObservationTimes()[endX-1]); _horiScale->SetUnitsCaption("Time"); } else { _horiScale->InitializeNumericTicks(-0.5 + startX, 0.5 + endX - 1.0); } if(_manualXAxisDescription) _horiScale->SetUnitsCaption(_xAxisDescription); if(_manualYAxisDescription) _vertScale->SetUnitsCaption(_yAxisDescription); } if(_metaData != 0) { if(_showColorScale && _metaData->ValueDescription()!="") { if(_metaData->ValueUnits()!="") _colorScale->SetUnitsCaption(_metaData->ValueDescription() + " (" + _metaData->ValueUnits() + ")"); else _colorScale->SetUnitsCaption(_metaData->ValueDescription()); } } if(_showColorScale) { if(_scaleOption == LogScale) _colorScale->InitializeLogarithmicTicks(min, max); else _colorScale->InitializeNumericTicks(min, max); if(_manualZAxisDescription) _colorScale->SetUnitsCaption(_zAxisDescription); } if(_showTitle && !actualTitleText().empty()) { _plotTitle = new Title(); _plotTitle->SetText(actualTitleText()); _plotTitle->SetPlotDimensions(width, height, 0.0); _topBorderSize = _plotTitle->GetHeight(cairo); } else { _plotTitle = 0; _topBorderSize = 10.0; } // The scale dimensions are depending on each other. However, since the height of the horizontal scale is practically // not dependent on other dimensions, we give the horizontal scale temporary width/height, so that we can calculate its height: if(_showXYAxes) { _horiScale->SetPlotDimensions(width, height, 0.0, 0.0); _bottomBorderSize = _horiScale->GetHeight(cairo); _rightBorderSize = _horiScale->GetRightMargin(cairo); _vertScale->SetPlotDimensions(width - _rightBorderSize + 5.0, height - _topBorderSize - _bottomBorderSize, _topBorderSize); _leftBorderSize = _vertScale->GetWidth(cairo); } else { _bottomBorderSize = 0.0; _rightBorderSize = 0.0; _leftBorderSize = 0.0; } if(_showColorScale) { _colorScale->SetPlotDimensions(width - _rightBorderSize, height - _topBorderSize, _topBorderSize); _rightBorderSize += _colorScale->GetWidth(cairo) + 5.0; } if(_showXYAxes) { _horiScale->SetPlotDimensions(width - _rightBorderSize + 5.0, height -_topBorderSize - _bottomBorderSize, _topBorderSize, _vertScale->GetWidth(cairo)); } class ColorMap *colorMap = createColorMap(); const double minLog10 = min>0.0 ? log10(min) : 0.0, maxLog10 = max>0.0 ? log10(max) : 0.0; if(_showColorScale) { for(unsigned x=0;x<256;++x) { num_t colorVal = (2.0 / 256.0) * x - 1.0; num_t imageVal; if(_scaleOption == LogScale) imageVal = exp10((x / 256.0) * (log10(max) - minLog10) + minLog10); else imageVal = (max-min) * x / 256.0 + min; double r = colorMap->ValueToColorR(colorVal), g = colorMap->ValueToColorG(colorVal), b = colorMap->ValueToColorB(colorVal); _colorScale->SetColorValue(imageVal, r/255.0, g/255.0, b/255.0); } } _imageSurface.clear(); _imageSurface = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, imageWidth, imageHeight); _imageSurface->flush(); unsigned char *data = _imageSurface->get_data(); size_t rowStride = _imageSurface->get_stride(); Mask2DPtr highlightMask; if(_highlighting) { highlightMask = Mask2D::CreateSetMaskPtr<false>(image->Width(), image->Height()); _highlightConfig->Execute(image, highlightMask, true, 10.0); } const bool originalActive = _showOriginalMask && originalMask != 0, altActive = _showAlternativeMask && alternativeMask != 0; for(unsigned long y=startY;y<endY;++y) { guint8* rowpointer = data + rowStride * (endY - y - 1); for(unsigned long x=startX;x<endX;++x) { int xa = (x-startX) * 4; unsigned char r,g,b,a; if(_highlighting && highlightMask->Value(x, y) != 0) { r = 255; g = 0; b = 0; a = 255; } else if(originalActive && originalMask->Value(x, y)) { r = 255; g = 0; b = 255; a = 255; } else if(altActive && alternativeMask->Value(x, y)) { r = 255; g = 255; b = 0; a = 255; } else { num_t val = image->Value(x, y); if(val > max) val = max; else if(val < min) val = min; if(_scaleOption == LogScale) { if(image->Value(x, y) <= 0.0) val = -1.0; else val = (log10(image->Value(x, y)) - minLog10) * 2.0 / (maxLog10 - minLog10) - 1.0; } else val = (image->Value(x, y) - min) * 2.0 / (max - min) - 1.0; if(val < -1.0) val = -1.0; else if(val > 1.0) val = 1.0; r = colorMap->ValueToColorR(val); g = colorMap->ValueToColorG(val); b = colorMap->ValueToColorB(val); a = colorMap->ValueToColorA(val); } rowpointer[xa]=b; rowpointer[xa+1]=g; rowpointer[xa+2]=r; rowpointer[xa+3]=a; } } delete colorMap; if(_segmentedImage != 0) { for(unsigned long y=startY;y<endY;++y) { guint8* rowpointer = data + rowStride * (y - startY); for(unsigned long x=startX;x<endX;++x) { if(_segmentedImage->Value(x,y) != 0) { int xa = (x-startX) * 4; rowpointer[xa]=IntMap::R(_segmentedImage->Value(x,y)); rowpointer[xa+1]=IntMap::G(_segmentedImage->Value(x,y)); rowpointer[xa+2]=IntMap::B(_segmentedImage->Value(x,y)); rowpointer[xa+3]=IntMap::A(_segmentedImage->Value(x,y)); } } } } _imageSurface->mark_dirty(); while(_imageSurface->get_width() > (int) width || _imageSurface->get_height() > (int) height) { unsigned newWidth = _imageSurface->get_width(), newHeight = _imageSurface->get_height(); if(newWidth > width) newWidth = width; if(newHeight > height) newHeight = height; downsampleImageBuffer(newWidth, newHeight); } _isInitialized = true; _initializedWidth = width; _initializedHeight = height; redrawWithoutChanges(cairo, width, height); }
void StatisticalFlagger::DilateFlagsHorizontally(Mask2DPtr mask, size_t timeSize) { if(timeSize != 0) { Mask2DPtr destination = Mask2D::CreateUnsetMaskPtr(mask->Width(), mask->Height()); if(timeSize > mask->Width()) timeSize = mask->Width(); const int intSize = (int) timeSize; for(size_t y=0;y<mask->Height();++y) { int dist = intSize + 1; for(size_t x=0;x<timeSize;++x) { if(mask->Value(x, y)) dist = - intSize; dist++; } for(size_t x=0;x<mask->Width() - timeSize;++x) { if(mask->Value(x + timeSize, y)) dist = -intSize; if(dist <= intSize) { destination->SetValue(x, y, true); dist++; } else { destination->SetValue(x, y, false); } } for(size_t x=mask->Width() - timeSize;x<mask->Width();++x) { if(dist <= intSize) { destination->SetValue(x, y, true); dist++; } else { destination->SetValue(x, y, false); } } } mask->Swap(destination); } }
void ComplexPlanePlotWindow::onPlotPressed() { if(_msWindow.HasImage()) { try { Plot2D &plot = _plotManager.NewPlot2D("Complex plane"); size_t x = (size_t) _xPositionScale.get_value(); size_t y = (size_t) _yPositionScale.get_value(); size_t length = (size_t) _lengthScale.get_value(); size_t avgSize = (size_t) _ySumLengthScale.get_value(); bool realVersusImaginary = _realVersusImaginaryButton.get_active(); const TimeFrequencyData &data = _msWindow.GetActiveData(); if(_allValuesButton.get_active()) { Plot2DPointSet *pointSet; if(realVersusImaginary) pointSet = &plot.StartLine("Data", Plot2DPointSet::DrawPoints); else pointSet = &plot.StartLine("Data (real)", Plot2DPointSet::DrawPoints); Mask2DPtr mask = Mask2D::CreateSetMaskPtr<false>(_msWindow.AltMask()->Width(), _msWindow.AltMask()->Height()); RFIPlots::MakeComplexPlanePlot(*pointSet, data, x, length, y, avgSize, mask, realVersusImaginary, false); if(!realVersusImaginary) { pointSet = &plot.StartLine("Data (imaginary)", Plot2DPointSet::DrawPoints); RFIPlots::MakeComplexPlanePlot(*pointSet, data, x, length, y, avgSize, mask, realVersusImaginary, true); } } if(_unmaskedValuesButton.get_active()) { Plot2DPointSet *pointSet = &plot.StartLine("Without RFI"); RFIPlots::MakeComplexPlanePlot(*pointSet, data, x, length, y, avgSize, _msWindow.AltMask(), realVersusImaginary, false); if(!realVersusImaginary) { pointSet = &plot.StartLine("Without RFI (I)"); RFIPlots::MakeComplexPlanePlot(*pointSet, data, x, length, y, avgSize, _msWindow.AltMask(), realVersusImaginary, true); } } if(_maskedValuesButton.get_active()) { Plot2DPointSet *pointSet = &plot.StartLine("Only RFI"); Mask2DPtr mask = Mask2D::CreateCopy(_msWindow.AltMask()); mask->Invert(); RFIPlots::MakeComplexPlanePlot(*pointSet, data, x, length, y, avgSize, mask, realVersusImaginary, false); if(!realVersusImaginary) { pointSet = &plot.StartLine("Only RFI (I)"); RFIPlots::MakeComplexPlanePlot(*pointSet, data, x, length, y, avgSize, mask, realVersusImaginary, true); } } if(_fittedValuesButton.get_active()) { Plot2DPointSet *pointSet; if(realVersusImaginary) pointSet = &plot.StartLine("Fit"); else pointSet = &plot.StartLine("Fit (real)"); size_t middleY = (2*y + avgSize) / 2; Baseline baseline(_msWindow.TimeFrequencyMetaData()->Antenna1(), _msWindow.TimeFrequencyMetaData()->Antenna2()); long double fringeCount = UVImager::GetFringeCount(x, x+length, middleY, _msWindow.TimeFrequencyMetaData()); long double fringeFrequency = fringeCount / length; Mask2DPtr mask = Mask2D::CreateSetMaskPtr<false>(_msWindow.AltMask()->Width(), _msWindow.AltMask()->Height()); RFIPlots::MakeFittedComplexPlot(*pointSet, data, x, length, y, avgSize, mask, fringeFrequency, realVersusImaginary, false); if(!realVersusImaginary) { pointSet = &plot.StartLine("Fit (imaginary)"); RFIPlots::MakeFittedComplexPlot(*pointSet, data, x, length, y, avgSize, mask, fringeFrequency, realVersusImaginary, true); } } if(_individualSampleFitButton.get_active()) { FringeStoppingFitter fitter; fitter.Initialize(data); fitter.SetFitChannelsIndividually(true); fitter.SetFringesToConsider(1.0L); fitter.SetMaxWindowSize(256); fitter.SetReturnFittedValue(true); fitter.SetReturnMeanValue(false); fitter.SetMetaData(_msWindow.TimeFrequencyMetaData()); fitter.PerformStaticFrequencyFitOnOneChannel(y); Plot2DPointSet *pointSet; if(realVersusImaginary) pointSet = &plot.StartLine("Fit"); else pointSet = &plot.StartLine("Fit (real)"); Mask2DPtr mask = Mask2D::CreateSetMaskPtr<false>(_msWindow.AltMask()->Width(), _msWindow.AltMask()->Height()); RFIPlots::MakeComplexPlanePlot(*pointSet, fitter.Background(), x, length, y, avgSize, mask, realVersusImaginary, false); fitter.SetReturnFittedValue(false); fitter.SetReturnMeanValue(true); if(!realVersusImaginary) { pointSet = &plot.StartLine("Fit (imaginary)"); RFIPlots::MakeComplexPlanePlot(*pointSet, fitter.Background(), x, length, y, avgSize, mask, realVersusImaginary, true); } fitter.PerformStaticFrequencyFitOnOneChannel(y); pointSet = &plot.StartLine("Center"); RFIPlots::MakeComplexPlanePlot(*pointSet, fitter.Background(), x, length, y, avgSize, mask, realVersusImaginary, false); if(!realVersusImaginary) { pointSet = &plot.StartLine("Center (I)"); RFIPlots::MakeComplexPlanePlot(*pointSet, fitter.Background(), x, length, y, avgSize, mask, realVersusImaginary, true); } } if(_fringeFitButton.get_active() || _dynamicFringeFitButton.get_active()) { /*FringeStoppingFitter fitter; Image2DPtr zero = Image2D::CreateZeroImagePtr(data.ImageWidth(), data.ImageHeight()); Image2DPtr ones = Image2D::CreateZeroImagePtr(data.ImageWidth(), data.ImageHeight()); for(size_t yi=0;yi<ones->Height();++yi) for(size_t xi=0;xi<ones->Width();++xi) ones->SetValue(xi, yi, 1.0L); TimeFrequencyData data(StokesIPolarisation, ones, zero); fitter.Initialize(data); fitter.SetFitChannelsIndividually(true); fitter.SetMetaData(_msWindow.TimeFrequencyMetaData()); fitter.PerformFringeStop(); plot.StartLine("Fringe rotation"); Mask2DPtr mask = Mask2D::CreateSetMaskPtr<false>(_msWindow.AltMask()->Width(), _msWindow.AltMask()->Height()); RFIPlots::MakeComplexPlanePlot(plot, fitter.Background(), x, length, y, avgSize, mask, realVersusImaginary, false); if(!realVersusImaginary) { plot.StartLine("Fringe rotation (I)"); RFIPlots::MakeComplexPlanePlot(plot, fitter.Background(), x, length, y, avgSize, mask, realVersusImaginary, true); }*/ FringeStoppingFitter fitter; fitter.Initialize(data); fitter.SetMetaData(_msWindow.TimeFrequencyMetaData()); //fitter.PerformFringeStop(); fitter.SetReturnFittedValue(true); if(_dynamicFringeFitButton.get_active()) fitter.PerformDynamicFrequencyFit(y, y + avgSize, 200); else fitter.PerformDynamicFrequencyFit(y, y + avgSize); Plot2DPointSet *pointSet; if(realVersusImaginary) pointSet = &plot.StartLine("Fit"); else pointSet = &plot.StartLine("Fit (real)"); Mask2DPtr mask = Mask2D::CreateSetMaskPtr<false>(_msWindow.AltMask()->Width(), _msWindow.AltMask()->Height()); RFIPlots::MakeComplexPlanePlot(*pointSet, fitter.Background(), x, length, y, avgSize, mask, realVersusImaginary, false); if(!realVersusImaginary) { pointSet = &plot.StartLine("Fit (imaginary)"); RFIPlots::MakeComplexPlanePlot(*pointSet, fitter.Background(), x, length, y, avgSize, mask, realVersusImaginary, true); } } _plotManager.Update(); } catch(std::exception &e) { Gtk::MessageDialog dialog(*this, e.what(), false, Gtk::MESSAGE_ERROR); dialog.run(); } } }
std::pair<TimeFrequencyData,TimeFrequencyMetaDataPtr> RSPReader::ReadAllBeamlets(unsigned long timestepStart, unsigned long timestepEnd, unsigned beamletCount) { const unsigned width = timestepEnd - timestepStart; Image2DPtr realX = Image2D::CreateZeroImagePtr(width, beamletCount); Image2DPtr imaginaryX = Image2D::CreateZeroImagePtr(width, beamletCount); Image2DPtr realY = Image2D::CreateZeroImagePtr(width, beamletCount); Image2DPtr imaginaryY = Image2D::CreateZeroImagePtr(width, beamletCount); Mask2DPtr mask = Mask2D::CreateSetMaskPtr<true>(width, beamletCount); std::ifstream file(_rawFile.c_str(), std::ios_base::binary | std::ios_base::in); size_t frame = 0; std::set<short> stations; TimeFrequencyMetaDataPtr metaData = TimeFrequencyMetaDataPtr(new TimeFrequencyMetaData()); BandInfo band; for(size_t i=0;i<beamletCount;++i) { ChannelInfo channel; channel.frequencyHz = i+1; channel.frequencyIndex = i; band.channels.push_back(channel); } metaData->SetBand(band); std::vector<double> observationTimes; // Read a header and determine the reading start position // Because timestepStart might fall within a block, the RCPApplicationHeader firstHeader; firstHeader.Read(file); const unsigned long bytesPerFrame = beamletCount * firstHeader.nofBlocks * RCPBeamletData::SIZE + RCPApplicationHeader::SIZE; const unsigned long startFrame = timestepStart / (unsigned long) firstHeader.nofBlocks; const unsigned long startByte = startFrame * bytesPerFrame; const unsigned long offsetFromStart = timestepStart - (startFrame * firstHeader.nofBlocks); //Logger::Debug << "Seeking to " << startByte << " (timestepStart=" << timestepStart << ", offsetFromStart=" << offsetFromStart << ", startFrame=" << startFrame << ",bytesPerFrame=" << bytesPerFrame << ")\n"; file.seekg(startByte, std::ios_base::beg); // Read the frames unsigned long x=0; while(x < width + offsetFromStart && file.good()) { RCPApplicationHeader header; header.Read(file); if(header.versionId != 2) { std::stringstream s; s << "Corrupted header found in frame " << frame << "!"; throw std::runtime_error(s.str()); } if(stations.count(header.stationId)==0) { stations.insert(header.stationId); AntennaInfo antenna; std::stringstream s; s << "LOFAR station with index " << header.stationId; antenna.name = s.str(); metaData->SetAntenna1(antenna); metaData->SetAntenna2(antenna); } for(size_t j=0;j<beamletCount;++j) { for(size_t i=0;i<header.nofBlocks;++i) { RCPBeamletData data; data.Read(file); if(i + x < width + offsetFromStart && i + x >= offsetFromStart) { const unsigned long pos = i + x - offsetFromStart; realX->SetValue(pos, j, data.xr); imaginaryX->SetValue(pos, j, data.xi); realY->SetValue(pos, j, data.yr); imaginaryY->SetValue(pos, j, data.yi); mask->SetValue(pos, j, false); } } } x += header.nofBlocks; ++frame; } //Logger::Debug << "Read " << frame << " frames.\n"; for(unsigned long i=0;i<width;++i) { const unsigned long pos = i + timestepStart; const double time = (double) pos * (double) STATION_INTEGRATION_STEPS / (double) _clockSpeed; observationTimes.push_back(time); } metaData->SetObservationTimes(observationTimes); std::pair<TimeFrequencyData,TimeFrequencyMetaDataPtr> data; data.first = TimeFrequencyData(Polarization::XX, realX, imaginaryX, Polarization::YY, realY, imaginaryY); data.first.SetGlobalMask(mask); data.second = metaData; return data; }
std::pair<TimeFrequencyData,TimeFrequencyMetaDataPtr> RSPReader::ReadChannelBeamlet(unsigned long timestepStart, unsigned long timestepEnd, unsigned beamletCount, unsigned beamletIndex) { const unsigned width = timestepEnd - timestepStart; std::pair<TimeFrequencyData,TimeFrequencyMetaDataPtr> data = ReadSingleBeamlet(timestepStart*(unsigned long) 256, timestepEnd*(unsigned long) 256, beamletCount, beamletIndex); TimeFrequencyData allX = data.first.Make(Polarization::XX); TimeFrequencyData allY = data.first.Make(Polarization::YY); Image2DCPtr xr = allX.GetRealPart(); Image2DCPtr xi = allX.GetImaginaryPart(); Image2DCPtr yr = allY.GetRealPart(); Image2DCPtr yi = allY.GetImaginaryPart(); Mask2DCPtr mask = data.first.GetSingleMask(); Image2DPtr outXR = Image2D::CreateUnsetImagePtr(width, 256), outXI = Image2D::CreateUnsetImagePtr(width, 256), outYR = Image2D::CreateUnsetImagePtr(width, 256), outYI = Image2D::CreateUnsetImagePtr(width, 256); Mask2DPtr outMask = Mask2D::CreateUnsetMaskPtr(width, 256); std::vector<double> observationTimes; for(unsigned long timestep = 0;timestep < timestepEnd-timestepStart;++timestep) { unsigned long timestepIndex = timestep * 256; SampleRow realX = SampleRow::MakeFromRow(xr.get(), timestepIndex, 256, 0), imaginaryX = SampleRow::MakeFromRow(xi.get(), timestepIndex, 256, 0), realY = SampleRow::MakeFromRow(yr.get(), timestepIndex, 256, 0), imaginaryY = SampleRow::MakeFromRow(yi.get(), timestepIndex, 256, 0); FFTTools::FFT(realX, imaginaryX); FFTTools::FFT(realY, imaginaryY); realX.SetVerticalImageValues(outXR.get(), timestep); imaginaryX.SetVerticalImageValues(outXI.get(), timestep); realY.SetVerticalImageValues(outYR.get(), timestep); imaginaryY.SetVerticalImageValues(outYI.get(), timestep); observationTimes.push_back(data.second->ObservationTimes()[timestepIndex + 256/2]); size_t validValues = 0; for(unsigned y=0;y<256;++y) { if(!mask->Value(timestepIndex + y, 0)) ++validValues; } for(unsigned y=0;y<256;++y) { outMask->SetValue(timestep, y , validValues == 0); } } data.first = TimeFrequencyData(Polarization::XX, outXR, outXI, Polarization::YY, outYR, outYI); data.first.SetGlobalMask(outMask); BandInfo band = data.second->Band(); band.channels.clear(); for(unsigned i=0;i<256;++i) { ChannelInfo channel; channel.frequencyHz = i+1; channel.frequencyIndex = i; band.channels.push_back(channel); } data.second->SetBand(band); data.second->SetObservationTimes(observationTimes); return data; }
void BHFitsImageSet::loadImageData(TimeFrequencyData &data, const TimeFrequencyMetaDataPtr &metaData, const BHFitsImageSetIndex &index) { std::vector<num_t> buffer(_width * _height); _file->ReadCurrentImageData(0, &buffer[0], _width * _height); int rangeStart = _timeRanges[index._imageIndex].start, rangeEnd = _timeRanges[index._imageIndex].end; Image2DPtr image = Image2D::CreateZeroImagePtr(rangeEnd-rangeStart, _height); std::vector<num_t>::const_iterator bufferPtr = buffer.begin() + _height*rangeStart; for(int x=rangeStart; x!=rangeEnd; ++x) { for(int y=0; y!=_height; ++y) { image->SetValue(x-rangeStart, y, *bufferPtr); ++bufferPtr; } } data = TimeFrequencyData(TimeFrequencyData::AmplitudePart, SinglePolarisation, image); try { FitsFile flagFile(flagFilePath()); flagFile.Open(FitsFile::ReadOnlyMode); flagFile.ReadCurrentImageData(0, &buffer[0], _width * _height); bufferPtr = buffer.begin() + _height*rangeStart; Mask2DPtr mask = Mask2D::CreateUnsetMaskPtr(rangeEnd-rangeStart, _height); for(int x=rangeStart; x!=rangeEnd; ++x) { for(int y=0; y!=_height; ++y) { bool flag = false; if(*bufferPtr == 0.0) flag = false; else if(*bufferPtr == 1.0) flag = true; else std::runtime_error("Expecting a flag file with only ones and zeros, but this file contained other values."); mask->SetValue(x-rangeStart, y, flag); ++bufferPtr; } } data.SetGlobalMask(mask); } catch(std::exception &) { // Flag file could not be read; probably does not exist. Ignore this, flags will be initialized to false. } double frequencyDelta = _file->GetDoubleKeywordValue("CDELT1"), timeDelta = _file->GetDoubleKeywordValue("CDELT2"); BandInfo band; for(int ch=0; ch!=_height; ++ch) { ChannelInfo channel; channel.frequencyHz = ch * frequencyDelta * 1000000.0; band.channels.push_back(channel); } metaData->SetBand(band); const int rangeWidth = rangeEnd-rangeStart; std::vector<double> observationTimes(rangeWidth); for(int t=0; t!=rangeWidth; ++t) observationTimes[t] = (t + rangeStart) * timeDelta; metaData->SetObservationTimes(observationTimes); AntennaInfo antennaInfo; antennaInfo.id = 0; antennaInfo.name = RangeName(index._imageIndex); antennaInfo.diameter = 0.0; antennaInfo.mount = "Unknown"; antennaInfo.station = GetTelescopeName(); metaData->SetAntenna1(antennaInfo); metaData->SetAntenna2(antennaInfo); }