Search in sources :

Example 6 with DatasetVariable

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();
}
Also used : DatasetVariable(org.obiba.mica.dataset.domain.DatasetVariable) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET)

Example 7 with DatasetVariable

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());
}
Also used : DatasetVariable(org.obiba.mica.dataset.domain.DatasetVariable) StudyTable(org.obiba.mica.core.domain.StudyTable) ArrayList(java.util.ArrayList) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) List(java.util.List) LocalizedString(org.obiba.mica.core.domain.LocalizedString) Matchers.anyString(org.mockito.Matchers.anyString) HarmonizationDataset(org.obiba.mica.dataset.domain.HarmonizationDataset) Test(org.junit.Test)

Example 8 with DatasetVariable

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;
}
Also used : Search(org.obiba.opal.web.model.Search) OpalTable(org.obiba.mica.core.domain.OpalTable) MicaConfig(org.obiba.mica.micaConfig.domain.MicaConfig) Taxonomy(org.obiba.opal.core.domain.taxonomy.Taxonomy) HarmonizationStudyTable(org.obiba.mica.core.domain.HarmonizationStudyTable) Inject(javax.inject.Inject) Term(org.obiba.opal.core.domain.taxonomy.Term) Strings(com.google.common.base.Strings) Lists(com.google.common.collect.Lists) Map(java.util.Map) JSONUtils(org.obiba.mica.JSONUtils) Attributes(org.obiba.mica.core.domain.Attributes) HarmonizationDatasetState(org.obiba.mica.dataset.domain.HarmonizationDatasetState) StudyDataset(org.obiba.mica.dataset.domain.StudyDataset) Nullable(javax.annotation.Nullable) SubjectAclService(org.obiba.mica.security.service.SubjectAclService) Math(org.obiba.opal.web.model.Math) Collection(java.util.Collection) DatasetCategory(org.obiba.mica.dataset.domain.DatasetCategory) DatasetVariable(org.obiba.mica.dataset.domain.DatasetVariable) NotNull(javax.validation.constraints.NotNull) Dataset(org.obiba.mica.dataset.domain.Dataset) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) PublishedStudyService(org.obiba.mica.study.service.PublishedStudyService) List(java.util.List) Component(org.springframework.stereotype.Component) HarmonizationDatasetStateRepository(org.obiba.mica.dataset.HarmonizationDatasetStateRepository) StudyDatasetState(org.obiba.mica.dataset.domain.StudyDatasetState) StudyDatasetStateRepository(org.obiba.mica.dataset.StudyDatasetStateRepository) MicaConfigService(org.obiba.mica.micaConfig.service.MicaConfigService) HarmonizationDataset(org.obiba.mica.dataset.domain.HarmonizationDataset) Optional(java.util.Optional) Vocabulary(org.obiba.opal.core.domain.taxonomy.Vocabulary) Collections(java.util.Collections) StudyTable(org.obiba.mica.core.domain.StudyTable) Assert(org.springframework.util.Assert) MicaConfig(org.obiba.mica.micaConfig.domain.MicaConfig) DatasetCategory(org.obiba.mica.dataset.domain.DatasetCategory) HarmonizationStudyTable(org.obiba.mica.core.domain.HarmonizationStudyTable) StudyTable(org.obiba.mica.core.domain.StudyTable) HarmonizationStudyTable(org.obiba.mica.core.domain.HarmonizationStudyTable)

Example 9 with DatasetVariable

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());
}
Also used : Search(org.obiba.opal.web.model.Search) DatasetDeletedEvent(org.obiba.mica.dataset.event.DatasetDeletedEvent) FileUtils(org.obiba.mica.file.FileUtils) Cacheable(org.springframework.cache.annotation.Cacheable) LoggerFactory(org.slf4j.LoggerFactory) CacheEvict(org.springframework.cache.annotation.CacheEvict) AbstractGitPersistable(org.obiba.mica.core.domain.AbstractGitPersistable) NoSuchVariableException(org.obiba.magma.NoSuchVariableException) DatasetUnpublishedEvent(org.obiba.mica.dataset.event.DatasetUnpublishedEvent) Valid(javax.validation.Valid) StudyDatasetIndexedEvent(org.obiba.mica.dataset.event.StudyDatasetIndexedEvent) PublishCascadingScope(org.obiba.mica.core.domain.PublishCascadingScope) EntityStateRepository(org.obiba.mica.core.repository.EntityStateRepository) NoSuchDatasetException(org.obiba.mica.dataset.NoSuchDatasetException) StudyState(org.obiba.mica.study.domain.StudyState) QueryTermsUtil(org.obiba.mica.dataset.service.support.QueryTermsUtil) Set(java.util.Set) StudyService(org.obiba.mica.study.service.StudyService) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) NoSuchEntityException(org.obiba.mica.NoSuchEntityException) Sets(com.google.common.collect.Sets) List(java.util.List) NoSuchValueTableException(org.obiba.magma.NoSuchValueTableException) Stream(java.util.stream.Stream) Study(org.obiba.mica.study.domain.Study) StudyDatasetState(org.obiba.mica.dataset.domain.StudyDatasetState) StudyDatasetStateRepository(org.obiba.mica.dataset.StudyDatasetStateRepository) Lazy(org.springframework.context.annotation.Lazy) NetworkService(org.obiba.mica.network.service.NetworkService) BeanUtils(org.springframework.beans.BeanUtils) Async(org.springframework.scheduling.annotation.Async) DatasetPublishedEvent(org.obiba.mica.dataset.event.DatasetPublishedEvent) RestValueTable(org.obiba.opal.rest.client.magma.RestValueTable) OpalService(org.obiba.mica.micaConfig.service.OpalService) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Caching(org.springframework.cache.annotation.Caching) EventBus(com.google.common.eventbus.EventBus) Strings(com.google.common.base.Strings) Lists(com.google.common.collect.Lists) Service(org.springframework.stereotype.Service) StreamSupport(java.util.stream.StreamSupport) StudyDataset(org.obiba.mica.dataset.domain.StudyDataset) Validated(org.springframework.validation.annotation.Validated) DatasetUpdatedEvent(org.obiba.mica.dataset.event.DatasetUpdatedEvent) Logger(org.slf4j.Logger) DatasetVariable(org.obiba.mica.dataset.domain.DatasetVariable) BaseStudy(org.obiba.mica.study.domain.BaseStudy) DateTime(org.joda.time.DateTime) Population(org.obiba.mica.study.domain.Population) FileSystemService(org.obiba.mica.file.service.FileSystemService) DataCollectionEvent(org.obiba.mica.study.domain.DataCollectionEvent) NoSuchStudyException(org.obiba.mica.study.NoSuchStudyException) PublishedStudyService(org.obiba.mica.study.service.PublishedStudyService) IndividualStudyService(org.obiba.mica.study.service.IndividualStudyService) Component(org.springframework.stereotype.Component) Collectors.toList(java.util.stream.Collectors.toList) StudyDatasetRepository(org.obiba.mica.dataset.StudyDatasetRepository) StudyTable(org.obiba.mica.core.domain.StudyTable) DataCollectionEvent(org.obiba.mica.study.domain.DataCollectionEvent) StudyTable(org.obiba.mica.core.domain.StudyTable) Population(org.obiba.mica.study.domain.Population) BaseStudy(org.obiba.mica.study.domain.BaseStudy)

Example 10 with DatasetVariable

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;
}
Also used : DatasetVariable(org.obiba.mica.dataset.domain.DatasetVariable)

Aggregations

DatasetVariable (org.obiba.mica.dataset.domain.DatasetVariable)17 Timed (com.codahale.metrics.annotation.Timed)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 GET (javax.ws.rs.GET)6 Path (javax.ws.rs.Path)6 Produces (javax.ws.rs.Produces)6 List (java.util.List)4 BadRequestException (javax.ws.rs.BadRequestException)3 NoSuchValueTableException (org.obiba.magma.NoSuchValueTableException)3 NoSuchVariableException (org.obiba.magma.NoSuchVariableException)3 NoSuchEntityException (org.obiba.mica.NoSuchEntityException)3 StudyTable (org.obiba.mica.core.domain.StudyTable)3 StudyDataset (org.obiba.mica.dataset.domain.StudyDataset)3 DatasetPublishedEvent (org.obiba.mica.dataset.event.DatasetPublishedEvent)3 Strings (com.google.common.base.Strings)2 Lists (com.google.common.collect.Lists)2 Collectors (java.util.stream.Collectors)2 Collectors.toList (java.util.stream.Collectors.toList)2 Inject (javax.inject.Inject)2 NotNull (javax.validation.constraints.NotNull)2