use of org.orcid.jaxb.model.common_v2.Source in project h2o-2 by h2oai.
the class GLMTest2 method testProximal.
@Test
public void testProximal() {
// glmnet's result:
// res2 <- glmnet(x=M,y=D$CAPSULE,lower.limits=-.5,upper.limits=.5,family='binomial')
// res2$beta[,58]
// AGE RACE DPROS PSA VOL GLEASON
// -0.00616326 -0.50000000 0.50000000 0.03628192 -0.01249324 0.50000000 // res2$a0[100]
// res2$a0[58]
// s57
// -4.155864
// lambda = 0.001108, null dev = 512.2888, res dev = 379.7597
Key parsed = Key.make("prostate_parsed");
Key modelKey = Key.make("prostate_model");
GLMModel model = null;
Frame fr = getFrameForFile(parsed, "smalldata/logreg/prostate.csv", new String[] { "ID" }, "CAPSULE");
Key k = Key.make("rebalanced");
H2O.submitTask(new RebalanceDataSet(fr, k, 64)).join();
fr.delete();
fr = DKV.get(k).get();
fr.remove("ID");
Key betaConsKey = Key.make("beta_constraints");
//String[] cfs1 = new String[]{"RACE", "AGE", "DPROS", "DCAPS", "PSA", "VOL", "GLEASON","Intercept"};
//double[] vals = new double[]{0, 0, 0.54788332,0.53816534, 0.02380097, 0, 0.98115670,-8.945984};
// [AGE, RACE, DPROS, DCAPS, PSA, VOL, GLEASON, Intercept]
FVecTest.makeByteVec(betaConsKey, "names, beta_given, rho\n AGE, 0.1, 1\nRACE, -0.1, 1 \n DPROS, 10, 1 \n DCAPS, -10, 1 \n PSA, 0, 1\n VOL, 0, 1\nGLEASON, 0, 1\n Intercept, 0, 0 \n");
Frame betaConstraints = ParseDataset2.parse(parsed, new Key[] { betaConsKey });
try {
// H2O differs on intercept and race, same residual deviance though
GLM2.Source src = new GLM2.Source((Frame) fr.clone(), fr.vec("CAPSULE"), false, true);
//.setHighAccuracy().doInit().fork().get();
new GLM2("GLM offset test on prostate.", Key.make(), modelKey, src, Family.binomial).setNonNegative(false).setRegularization(new double[] { 0 }, new double[] { 0.000 }).setBetaConstraints(betaConstraints).setHighAccuracy().doInit().fork().get();
model = DKV.get(modelKey).get();
fr.add("CAPSULE", fr.remove("CAPSULE"));
DataInfo dinfo = new DataInfo(fr, 1, true, false, TransformType.NONE, DataInfo.TransformType.NONE);
GLMIterationTask glmt = new GLMTask.GLMIterationTask(0, null, dinfo, new GLMParams(Family.binomial), false, true, true, model.beta(), 0, 1.0 / 380, ModelUtils.DEFAULT_THRESHOLDS, null).doAll(dinfo._adaptedFrame);
double[] beta = model.beta();
double[] grad = glmt.gradient(0, 0);
for (int i = 0; i < beta.length; ++i) Assert.assertEquals(0, grad[i] + betaConstraints.vec("rho").at(i) * (beta[i] - betaConstraints.vec("beta_given").at(i)), 1e-8);
// now standardized
src = new GLM2.Source((Frame) fr.clone(), fr.vec("CAPSULE"), true, true);
//.setHighAccuracy().doInit().fork().get();
new GLM2("GLM offset test on prostate.", Key.make(), modelKey, src, Family.binomial).setNonNegative(false).setRegularization(new double[] { 0 }, new double[] { 0.000 }).setBetaConstraints(betaConstraints).setHighAccuracy().doInit().fork().get();
model = DKV.get(modelKey).get();
fr.add("CAPSULE", fr.remove("CAPSULE"));
dinfo = new DataInfo(fr, 1, true, false, TransformType.STANDARDIZE, DataInfo.TransformType.NONE);
glmt = new GLMTask.GLMIterationTask(0, null, dinfo, new GLMParams(Family.binomial), false, true, true, model.norm_beta(0), 0, 1.0 / 380, ModelUtils.DEFAULT_THRESHOLDS, null).doAll(dinfo._adaptedFrame);
double[] beta2 = model.norm_beta(0);
double[] grad2 = glmt.gradient(0, 0);
for (int i = 0; i < beta.length - 1; ++i) Assert.assertEquals("grad[" + i + "] != 0", 0, grad2[i] + betaConstraints.vec("rho").at(i) * (beta2[i] - betaConstraints.vec("beta_given").at(i) * dinfo._adaptedFrame.vec(i).sigma()), 1e-8);
Assert.assertEquals("grad[intercept] != 0", 0, grad2[grad2.length - 1], 1e-8);
} finally {
fr.delete();
if (model != null)
model.delete();
}
}
use of org.orcid.jaxb.model.common_v2.Source in project h2o-2 by h2oai.
the class GLMTest2 method testPoissonRegression.
/**
* Test Poisson regression on simple and small synthetic dataset.
* Equation is: y = exp(x+1);
*/
@Test
public void testPoissonRegression() throws InterruptedException, ExecutionException {
Key raw = Key.make("poisson_test_data_raw");
Key parsed = Key.make("poisson_test_data_parsed");
Key modelKey = Key.make("poisson_test");
GLMModel model = null;
Frame fr = null;
try {
// make data so that the expected coefficients is icept = col[0] = 1.0
FVecTest.makeByteVec(raw, "x,y\n0,2\n1,4\n2,8\n3,16\n4,32\n5,64\n6,128\n7,256");
fr = ParseDataset2.parse(parsed, new Key[] { raw });
new GLM2("GLM test of poisson regression.", Key.make(), modelKey, new Source(fr, fr.lastVec(), false), Family.poisson).setRegularization(new double[] { 0 }, new double[] { 0 }).doInit().fork().get();
model = DKV.get(modelKey).get();
for (double c : model.beta()) assertEquals(Math.log(2), c, 1e-4);
// Test 2, example from http://www.biostat.umn.edu/~dipankar/bmtry711.11/lecture_13.pdf
//new byte []{1,2,3,4,5,6,7,8, 9, 10,11,12,13,14},
// new byte []{0,1,2,3,1,4,9,18,23,31,20,25,37,45});
model.delete();
fr.delete();
FVecTest.makeByteVec(raw, "x,y\n1,0\n2,1\n3,2\n4,3\n5,1\n6,4\n7,9\n8,18\n9,23\n10,31\n11,20\n12,25\n13,37\n14,45\n");
fr = ParseDataset2.parse(parsed, new Key[] { raw });
new GLM2("GLM test of poisson regression(2).", Key.make(), modelKey, new Source(fr, fr.lastVec(), false), Family.poisson).setRegularization(new double[] { 0 }, new double[] { 0 }).doInit().fork().get();
model = DKV.get(modelKey).get();
testHTML(model);
assertEquals(0.3396, model.beta()[1], 5e-3);
assertEquals(0.2565, model.beta()[0], 5e-3);
} finally {
if (fr != null)
fr.delete();
if (model != null)
model.delete();
}
}
use of org.orcid.jaxb.model.common_v2.Source in project ORCID-Source by ORCID.
the class ProfileKeywordManagerImpl method updateKeyword.
@Override
@Transactional
public Keyword updateKeyword(String orcid, Long putCode, Keyword keyword, boolean isApiRequest) {
SourceEntity sourceEntity = sourceManager.retrieveSourceEntity();
ProfileKeywordEntity updatedEntity = profileKeywordDao.getProfileKeyword(orcid, putCode);
Visibility originalVisibility = Visibility.fromValue(updatedEntity.getVisibility().value());
//Save the original source
String existingSourceId = updatedEntity.getSourceId();
String existingClientSourceId = updatedEntity.getClientSourceId();
// Validate the keyword
PersonValidator.validateKeyword(keyword, sourceEntity, false, isApiRequest, originalVisibility);
// Validate it is not duplicated
List<ProfileKeywordEntity> existingKeywords = profileKeywordDao.getProfileKeywors(orcid, getLastModified(orcid));
for (ProfileKeywordEntity existing : existingKeywords) {
if (isDuplicated(existing, keyword, sourceEntity)) {
Map<String, String> params = new HashMap<String, String>();
params.put("type", "keyword");
params.put("value", keyword.getContent());
throw new OrcidDuplicatedElementException(params);
}
}
orcidSecurityManager.checkSource(updatedEntity);
adapter.toProfileKeywordEntity(keyword, updatedEntity);
updatedEntity.setLastModified(new Date());
//Be sure it doesn't overwrite the source
updatedEntity.setSourceId(existingSourceId);
updatedEntity.setClientSourceId(existingClientSourceId);
profileKeywordDao.merge(updatedEntity);
return adapter.toKeyword(updatedEntity);
}
use of org.orcid.jaxb.model.common_v2.Source in project ORCID-Source by ORCID.
the class OrcidSecurityManagerImpl method checkAndFilter.
/**
* Check the permissions of a request over an element. Private
* implementation that will also include a parameter that indicates if we
* should check the token or, if it was already checked previously
*
* @param orcid
* The user owner of the element
* @param element
* The element to check
* @param requiredScope
* The required scope to access this element
* @param tokenAlreadyChecked
* Indicates if the token was already checked previously, so, we
* don't expend time checking it again
* @throws OrcidUnauthorizedException
* In case the token used was not issued for the owner of the
* element
* @throws OrcidAccessControlException
* In case the request doesn't have the required scopes
* @throws OrcidVisibilityException
* In case the element is not visible due the visibility
*/
private void checkAndFilter(String orcid, VisibilityType element, ScopePathType requiredScope, boolean tokenAlreadyChecked) {
if (element == null) {
return;
}
// Check the token was issued for this user
if (!tokenAlreadyChecked) {
isMyToken(orcid);
}
// Check if the client is the source of the element
if (element instanceof Filterable) {
Filterable filterable = (Filterable) element;
OAuth2Authentication oAuth2Authentication = getOAuth2Authentication();
if (oAuth2Authentication != null) {
OAuth2Request authorizationRequest = oAuth2Authentication.getOAuth2Request();
String clientId = authorizationRequest.getClientId();
if (clientId.equals(filterable.retrieveSourcePath())) {
// The client doing the request is the source of the element
return;
}
}
}
// /read-public scope
if (Visibility.PUBLIC.equals(element.getVisibility())) {
try {
checkScopes(ScopePathType.READ_PUBLIC);
// can return it
return;
} catch (OrcidAccessControlException e) {
// Just continue filtering
}
}
// Filter
filter(element, requiredScope);
}
use of org.orcid.jaxb.model.common_v2.Source in project ORCID-Source by ORCID.
the class FundingForm method valueOf.
public static FundingForm valueOf(Funding funding) {
FundingForm result = new FundingForm();
result.setDateSortString(PojoUtil.createDateSortString(funding.getStartDate(), funding.getEndDate()));
if (funding.getPutCode() != null)
result.setPutCode(Text.valueOf(funding.getPutCode()));
if (funding.getAmount() != null) {
if (StringUtils.isNotEmpty(funding.getAmount().getContent())) {
String cleanNumber = funding.getAmount().getContent().trim();
result.setAmount(Text.valueOf(cleanNumber));
}
if (funding.getAmount().getCurrencyCode() != null)
result.setCurrencyCode(Text.valueOf(funding.getAmount().getCurrencyCode()));
else
result.setCurrencyCode(new Text());
} else {
result.setAmount(new Text());
result.setCurrencyCode(new Text());
}
if (StringUtils.isNotEmpty(funding.getDescription()))
result.setDescription(Text.valueOf(funding.getDescription()));
else
result.setDescription(new Text());
if (funding.getStartDate() != null)
result.setStartDate(Date.valueOf(funding.getStartDate()));
if (funding.getEndDate() != null)
result.setEndDate(Date.valueOf(funding.getEndDate()));
if (funding.getType() != null)
result.setFundingType(Text.valueOf(funding.getType().value()));
else
result.setFundingType(new Text());
if (funding.getOrganizationDefinedType() != null) {
OrgDefinedFundingSubType OrgDefinedFundingSubType = new OrgDefinedFundingSubType();
OrgDefinedFundingSubType.setSubtype(Text.valueOf(funding.getOrganizationDefinedType().getContent()));
OrgDefinedFundingSubType.setAlreadyIndexed(false);
result.setOrganizationDefinedFundingSubType(OrgDefinedFundingSubType);
}
Source source = funding.getSource();
if (source != null) {
result.setSource(source.retrieveSourcePath());
if (source.getSourceName() != null) {
result.setSourceName(source.getSourceName().getContent());
}
}
if (funding.getTitle() != null) {
FundingTitleForm fundingTitle = new FundingTitleForm();
if (funding.getTitle().getTitle() != null)
fundingTitle.setTitle(Text.valueOf(funding.getTitle().getTitle().getContent()));
else
fundingTitle.setTitle(new Text());
if (funding.getTitle().getTranslatedTitle() != null) {
TranslatedTitleForm translatedTitle = new TranslatedTitleForm();
translatedTitle.setContent(funding.getTitle().getTranslatedTitle().getContent());
translatedTitle.setLanguageCode(funding.getTitle().getTranslatedTitle().getLanguageCode());
fundingTitle.setTranslatedTitle(translatedTitle);
}
result.setFundingTitle(fundingTitle);
} else {
FundingTitleForm fundingTitle = new FundingTitleForm();
fundingTitle.setTitle(new Text());
result.setFundingTitle(fundingTitle);
}
if (funding.getUrl() != null)
result.setUrl(Text.valueOf(funding.getUrl().getValue()));
else
result.setUrl(new Text());
if (funding.getVisibility() != null)
result.setVisibility(Visibility.valueOf(funding.getVisibility()));
// Set the disambiguated organization
Organization organization = funding.getOrganization();
result.setFundingName(Text.valueOf(organization.getName()));
DisambiguatedOrganization disambiguatedOrganization = organization.getDisambiguatedOrganization();
if (disambiguatedOrganization != null) {
if (StringUtils.isNotEmpty(disambiguatedOrganization.getDisambiguatedOrganizationIdentifier())) {
result.setDisambiguatedFundingSourceId(Text.valueOf(disambiguatedOrganization.getDisambiguatedOrganizationIdentifier()));
result.setDisambiguationSource(Text.valueOf(disambiguatedOrganization.getDisambiguationSource()));
}
}
OrganizationAddress organizationAddress = organization.getAddress();
if (organizationAddress != null) {
if (!PojoUtil.isEmpty(organizationAddress.getCity()))
result.setCity(Text.valueOf(organizationAddress.getCity()));
else
result.setCity(new Text());
if (!PojoUtil.isEmpty(organizationAddress.getRegion()))
result.setRegion(Text.valueOf(organizationAddress.getRegion()));
else
result.setRegion(new Text());
if (organizationAddress.getCountry() != null)
result.setCountry(Text.valueOf(organizationAddress.getCountry().value()));
else
result.setCountry(new Text());
} else {
result.setCountry(new Text());
result.setCity(new Text());
result.setRegion(new Text());
}
// Set contributors
if (funding.getContributors() != null) {
List<Contributor> contributors = new ArrayList<Contributor>();
for (FundingContributor fContributor : funding.getContributors().getContributor()) {
Contributor contributor = Contributor.valueOf(fContributor);
contributors.add(contributor);
}
result.setContributors(contributors);
}
List<FundingExternalIdentifierForm> externalIdentifiersList = new ArrayList<FundingExternalIdentifierForm>();
// Set external identifiers
if (funding.getExternalIdentifiers() != null) {
for (ExternalID fExternalIdentifier : funding.getExternalIdentifiers().getExternalIdentifier()) {
FundingExternalIdentifierForm fundingExternalIdentifierForm = FundingExternalIdentifierForm.valueOf(fExternalIdentifier);
externalIdentifiersList.add(fundingExternalIdentifierForm);
}
}
result.setExternalIdentifiers(externalIdentifiersList);
result.setCreatedDate(Date.valueOf(funding.getCreatedDate()));
result.setLastModified(Date.valueOf(funding.getLastModifiedDate()));
return result;
}
Aggregations