use of org.obiba.mica.dataset.domain.DatasetVariable in project mica2 by obiba.
the class PublishedDataschemaDatasetVariableResource method getContingencyExcel.
@GET
@Path("/contingency/_export")
@Produces("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
@Timed
public Response getContingencyExcel(@QueryParam("by") String crossVariable) throws IOException {
Pair<DatasetVariable, DatasetVariable> variables = getContingencyVariables(crossVariable);
ByteArrayOutputStream value = new ExcelContingencyWriter(variables.getFirst(), variables.getSecond()).write(getDatasetVariableContingenciesDto(variables.getFirst(), variables.getSecond()));
return Response.ok(value.toByteArray()).header("Content-Disposition", String.format("attachment; filename=\"contingency-table-%s-%s.xlsx\"", variableName, crossVariable)).build();
}
use of org.obiba.mica.dataset.domain.DatasetVariable in project mica2 by obiba.
the class HarmonizedDatasetServiceTest method testPopulateHarmonizedVariablesMap.
@Test
public void testPopulateHarmonizedVariablesMap() {
List<DatasetVariable> l = new ArrayList<DatasetVariable>() {
{
add(new DatasetVariable(dataset, Variable.Builder.newVariable("v1", BooleanType.get(), "test").build(), st));
add(new DatasetVariable(dataset, Variable.Builder.newVariable("v2", BooleanType.get(), "test").build(), st2));
}
};
doReturn(dataset).when(datasetService).findById(anyString());
when(helper.asyncGetDatasetVariables(any(Supplier.class))).thenReturn(new AsyncResult<>(l));
doReturn(l).when(datasetService).getDatasetVariables(any(HarmonizationDataset.class));
doReturn(l).when(datasetService).getDatasetVariables(any(HarmonizationDataset.class), any(StudyTable.class));
Map<String, List<DatasetVariable>> res = datasetService.populateHarmonizedVariablesMap(dataset);
assertEquals(2, res.keySet().size());
assertEquals(2, res.get("testds:v1:Dataschema").size());
assertEquals(2, res.get("testds:v2:Dataschema").size());
}
use of org.obiba.mica.dataset.domain.DatasetVariable in project mica2 by obiba.
the class DatasetDtos method asContingencyDto.
public Mica.DatasetVariableContingencyDto.Builder asContingencyDto(@NotNull OpalTable opalTable, DatasetVariable variable, DatasetVariable crossVariable, @Nullable Search.QueryResultDto results) {
Mica.DatasetVariableContingencyDto.Builder crossDto = Mica.DatasetVariableContingencyDto.newBuilder();
if (opalTable instanceof StudyTable)
crossDto.setStudyTable(asDto((StudyTable) opalTable, true));
else if (opalTable instanceof HarmonizationStudyTable)
crossDto.setHarmonizationStudyTable(asDto((HarmonizationStudyTable) opalTable));
Mica.DatasetVariableAggregationDto.Builder allAggBuilder = Mica.DatasetVariableAggregationDto.newBuilder();
if (results == null) {
allAggBuilder.setN(0);
allAggBuilder.setTotal(0);
crossDto.setAll(allAggBuilder);
return crossDto;
}
allAggBuilder.setTotal(results.getTotalHits());
MicaConfig micaConfig = micaConfigService.getConfig();
int privacyThreshold = micaConfig.getPrivacyThreshold();
crossDto.setPrivacyThreshold(privacyThreshold);
boolean privacyChecks = crossVariable.hasCategories() ? validatePrivacyThreshold(results, privacyThreshold) : true;
boolean totalPrivacyChecks = validateTotalPrivacyThreshold(results, privacyThreshold);
// add facet results in the same order as the variable categories
variable.getCategories().forEach(cat -> results.getFacetsList().stream().filter(facet -> facet.hasFacet() && cat.getName().equals(facet.getFacet())).forEach(facet -> {
boolean privacyCheck = privacyChecks && checkPrivacyThreshold(facet.getFilters(0).getCount(), privacyThreshold);
Mica.DatasetVariableAggregationDto.Builder aggBuilder = Mica.DatasetVariableAggregationDto.newBuilder();
aggBuilder.setTotal(totalPrivacyChecks ? results.getTotalHits() : 0);
aggBuilder.setTerm(facet.getFacet());
DatasetCategory category = variable.getCategory(facet.getFacet());
aggBuilder.setMissing(category != null && category.isMissing());
addSummaryStatistics(crossVariable, aggBuilder, facet, privacyCheck, totalPrivacyChecks);
crossDto.addAggregations(aggBuilder);
}));
// add total facet for all variable categories
results.getFacetsList().stream().filter(facet -> facet.hasFacet() && "_total".equals(facet.getFacet())).forEach(facet -> {
boolean privacyCheck = privacyChecks && facet.getFilters(0).getCount() > micaConfig.getPrivacyThreshold();
addSummaryStatistics(crossVariable, allAggBuilder, facet, privacyCheck, totalPrivacyChecks);
});
crossDto.setAll(allAggBuilder);
return crossDto;
}
use of org.obiba.mica.dataset.domain.DatasetVariable in project mica2 by obiba.
the class CollectedDatasetService method processVariablesForStudyDataset.
public List<DatasetVariable> processVariablesForStudyDataset(StudyDataset dataset, Iterable<DatasetVariable> variables) {
if (!dataset.hasStudyTable()) {
return Lists.newArrayList(variables);
}
StudyTable studyTable = dataset.getStudyTable();
BaseStudy study = studyService.findStudy(dataset.getStudyTable().getStudyId());
Population population = study.findPopulation(studyTable.getPopulationId());
if (population == null) {
return Lists.newArrayList(variables);
}
int populationWeight = population.getWeight();
DataCollectionEvent dataCollectionEvent = population.findDataCollectionEvent(studyTable.getDataCollectionEventId());
if (dataCollectionEvent == null) {
return Lists.newArrayList(variables);
}
int dataCollectionEventWeight = dataCollectionEvent.getWeight();
return StreamSupport.stream(variables.spliterator(), false).map(datasetVariable -> {
datasetVariable.setPopulationWeight(populationWeight);
datasetVariable.setDataCollectionEventWeight(dataCollectionEventWeight);
return datasetVariable;
}).collect(toList());
}
use of org.obiba.mica.dataset.domain.DatasetVariable in project mica2 by obiba.
the class AbstractPublishedDatasetResource method getHarmonizedDatasetVariable.
private DatasetVariable getHarmonizedDatasetVariable(String datasetId, String variableId, String variableName) {
String dataSchemaVariableId = DatasetVariable.IdResolver.encode(datasetId, variableName, DatasetVariable.Type.Dataschema, null, null, null, null);
DatasetVariable harmonizedDatasetVariable = getDatasetVariableInternal(Indexer.HARMONIZED_VARIABLE_TYPE, variableId, variableName);
DatasetVariable dataSchemaVariable = getDatasetVariableInternal(Indexer.VARIABLE_TYPE, dataSchemaVariableId, variableName);
dataSchemaVariable.getAttributes().asAttributeList().forEach(a -> {
if (!a.getName().startsWith("Mlstr_harmo"))
harmonizedDatasetVariable.addAttribute(a);
});
return harmonizedDatasetVariable;
}
Aggregations